-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktesting.py
More file actions
63 lines (50 loc) · 1.85 KB
/
backtesting.py
File metadata and controls
63 lines (50 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import importlib
import json
import logging
import os
from datetime import timedelta, datetime
from bot.backtest_bot import BackTestBot
from core.util import str_to_date, parse_timeframe
if __name__ == "__main__":
# Load configuration file
with open('./backtest_config.json') as f:
config = json.load(f)
# Create result directory
if not os.path.isdir(config['result_dir']):
os.mkdir(config['result_dir'])
# Create global directory
global_dir = f'{config["result_dir"]}global/'
if not os.path.isdir(global_dir):
os.mkdir(global_dir)
# Create output directory
result_dir = f'{config["result_dir"]}{datetime.now()}/'
os.mkdir(result_dir)
# Set logging
logging.basicConfig(format='%(message)s', level=logging.INFO,
handlers=[logging.StreamHandler(), logging.FileHandler(f'{result_dir}backtest.log')])
# Load trading bot
bot = BackTestBot(config)
# Load strategy
m = 'strategy.'
m += 'example.' + config['strategy'] if config['example'] else config['strategy']
strategy_cls = getattr(importlib.import_module(m), config['strategy'])
strategy = strategy_cls(bot)
# Execute strategy
start = str_to_date(config['start_time'])
end = str_to_date(config['end_time'])
timeframe_in_seconds = parse_timeframe(config['interval'])
current = start
i = 0
while current <= end:
current = start + timedelta(seconds=i * timeframe_in_seconds)
bot.next(current)
strategy.run(current)
i += 1
# Output order history
bot.output_order_history(result_dir, "filled")
# Output performance
bot.output_performance(result_dir, start, end)
# Output visualization of the result
bot.output_view(result_dir, global_dir, config['plot'])
# Output backtesting information
bot.output_config(result_dir, config)