forked from enarjord/passivbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtest.py
73 lines (62 loc) · 2.67 KB
/
backtest.py
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
64
65
66
67
68
69
70
71
72
73
import argparse
import asyncio
import os
import pprint
from time import time
import numpy as np
import pandas as pd
from downloader import Downloader
from njit_funcs import njit_backtest, round_
from plotting import dump_plots
from procedures import prep_config, make_get_filepath, load_live_config, add_argparse_args
from pure_funcs import create_xk, denumpyize, ts_to_date, analyze_fills
def backtest(config: dict, data: (np.ndarray,), do_print=False) -> (list, bool):
xk = create_xk(config)
return njit_backtest(data, config['starting_balance'], config['latency_simulation_ms'],
config['maker_fee'], **xk)
def plot_wrap(config, data):
n_days = round_((data[2][-1] - data[2][0]) / (1000 * 60 * 60 * 24), 0.1)
print('n_days', round_(n_days, 0.1))
print('starting_balance', config['starting_balance'])
print('backtesting...')
sts = time()
fills, info = backtest(config, data, do_print=True)
print(f'{time() - sts:.2f} seconds elapsed')
if not fills:
print('no fills')
return
fdf, result = analyze_fills(fills, {**config, **{'lowest_eqbal_ratio': info[1], 'closest_bkr': info[2]}},
data[2][0], data[2][-1])
config['result'] = result
config['plots_dirpath'] = make_get_filepath(os.path.join(
config['plots_dirpath'], f"{ts_to_date(time())[:19].replace(':', '')}", '')
)
fdf.to_csv(config['plots_dirpath'] + "fills.csv")
df = pd.DataFrame({**{'price': data[0], 'buyer_maker': data[1], 'timestamp': data[2]},
**{}})
print('dumping plots...')
dump_plots(config, fdf, df)
async def main():
parser = argparse.ArgumentParser(prog='Backtest', description='Backtest given passivbot config.')
parser.add_argument('live_config_path', type=str, help='path to live config to test')
parser = add_argparse_args(parser)
args = parser.parse_args()
config = await prep_config(args)
if config['exchange'] == 'bybit' and not config['inverse']:
print('bybit usdt linear backtesting not supported')
return
downloader = Downloader(config)
live_config = load_live_config(args.live_config_path)
config.update(live_config)
print()
for k in (keys := ['exchange', 'symbol', 'starting_balance', 'start_date', 'end_date',
'latency_simulation_ms']):
if k in config:
print(f"{k: <{max(map(len, keys)) + 2}} {config[k]}")
print()
data = await downloader.get_data()
config['n_days'] = round_((data[2][-1] - data[2][0]) / (1000 * 60 * 60 * 24), 0.1)
pprint.pprint(denumpyize(live_config))
plot_wrap(config, data)
if __name__ == '__main__':
asyncio.run(main())