-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbacktest.py
181 lines (137 loc) · 5.09 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import logging
import random
from kuegi_bot.backtest_engine import BackTest
from kuegi_bot.bots.MultiStrategyBot import MultiStrategyBot
from kuegi_bot.bots.strategies.MACross import MACross
from kuegi_bot.bots.strategies.entry_filters import DayOfWeekFilter
from kuegi_bot.bots.strategies.SfpStrat import SfpStrategy
from kuegi_bot.bots.strategies.exit_modules import SimpleBE, ParaTrail, MaxSLDiff
from kuegi_bot.bots.strategies.kuegi_strat import KuegiStrategy
from kuegi_bot.utils.helper import load_bars, prepare_plot, load_funding
from kuegi_bot.utils import log
from kuegi_bot.indicators.kuegi_channel import KuegiChannel
from kuegi_bot.utils.trading_classes import Symbol
logger = log.setup_custom_logger(log_level=logging.INFO)
def plot(bars):
forplot= bars[:]
logger.info("initializing indicators")
indis = [KuegiChannel()]
logger.info("preparing plot")
fig= prepare_plot(forplot, indis)
fig.show()
def backtest(bars):
bots= []
for bot in bots:
BackTest(bot,bars).run()
def increment(min,max,steps,current)->bool:
current[0] += steps[0]
for idx in range(len(current)):
if min[idx] <= current[idx] <= max[idx]:
return True
current[idx]= min[idx]
if idx < len(current)-1:
current[idx+1] += steps[idx+1]
else:
return False
def runOpti(bars,funding,min,max,steps,symbol= None, randomCount= -1):
v= min[:]
total= 1
while len(steps) < len(min):
steps.append(1)
for i in range(len(min)):
total *= 1+(max[i]-min[i])/steps[i]
logger.info("running %d combinations" % total)
while True:
msg= ""
if randomCount > 0:
for i in range(len(v)):
v[i] = min[i] + random.randint(0, int((max[i] - min[i]) / steps[i])) * steps[i]
randomCount = randomCount-1
for i in v:
msg += str(i) + " "
logger.info(msg)
bot = MultiStrategyBot(logger=logger, directionFilter=0)
bot.add_strategy(KuegiStrategy()
)
BackTest(bot, bars= bars,funding=funding, symbol=symbol).run()
if randomCount == 0 or (randomCount < 0 and not increment(min,max,steps,v)):
break
def checkDayFilterByDay(bars,symbol= None):
for i in range(7):
msg = str(i)
logger.info(msg)
bot = MultiStrategyBot(logger=logger, directionFilter=0)
bot.add_strategy(SfpStrategy()
.withEntryFilter(DayOfWeekFilter(1 << i))
)
b= BackTest(bot, bars,symbol).run()
pair= "BTCUSD"
pair= "BTCUSDT"
#pair= "ETHUSD"
exchange= 'bybit'
tf= 240
monthsBack= 18
if exchange == 'bybit' and "USDT" in pair:
exchange= 'bybit-linear'
funding = load_funding(exchange,pair)
#bars_p = load_bars(30 * 12, 240,0,'phemex')
#bars_n = load_bars(30 * 12, 240,0,'binance_f')
#bars_ns = load_bars(30 * 24, 240,0,'binanceSpot')
bars_b = load_bars(30 * monthsBack, tf,0,exchange,pair)
#bars_m = load_bars(30 * 12, 240,0,'bitmex')
#bars_b = load_bars(30 * 12, 60,0,'bybit')
#bars_m = load_bars(30 * 24, 60,0,'bitmex')
#bars1= load_bars(24)
#bars2= process_low_tf_bars(m1_bars, 240, 60)
#bars3= process_low_tf_bars(m1_bars, 240, 120)
#bars4= process_low_tf_bars(m1_bars, 240, 180)
symbol=None
if pair == "BTCUSD":
symbol=Symbol(symbol="BTCUSD", isInverse=True, tickSize=0.5, lotSize=1.0, makerFee=-0.025,takerFee=0.075, quantityPrecision=2,pricePrecision=2)
elif pair == "XRPUSD":
symbol=Symbol(symbol="XRPUSD", isInverse=True, tickSize=0.0001, lotSize=0.01, makerFee=-0.025,takerFee=0.075, quantityPrecision=2,pricePrecision=4)
elif pair == "ETHUSD":
symbol=Symbol(symbol="ETHUSD", isInverse=True, tickSize=0.01, lotSize=0.1, makerFee=-0.025,takerFee=0.075, quantityPrecision=2,pricePrecision=2)
elif pair == "BTCUSDT":
symbol=Symbol(symbol="BTCUSDT", isInverse=False, tickSize=0.5, lotSize=0.0001, makerFee=-0.025,takerFee=0.075, quantityPrecision=5,pricePrecision=4)
#
#for binance_f
#symbol=Symbol(symbol="BTCUSDT", isInverse=False, tickSize=0.001, lotSize=0.00001, makerFee=0.02, takerFee=0.04, quantityPrecision=5)
bars_full= bars_b
oos_cut=int(len(bars_full)/4)
bars= bars_full[oos_cut:]
bars_oos= bars_full[:oos_cut]
'''
checkDayFilterByDay(bars,symbol=symbol)
#'''
'''
# profiling stats
# run it `python -m cProfile -o profile.data backtest.py`
import pstats
from pstats import SortKey
p = pstats.Stats('profile.data')
p.strip_dirs() # remove extra paths
p.sort_stats(SortKey.CUMULATIVE).print_stats(20)
p.sort_stats(SortKey.TIME).print_stats(10)
p.print_callers('<functionName>')
'''
'''
runOpti(bars_oos, funding=funding,
min= [-5,20],
max= [5,27],
steps= [1,1],
randomCount=-1,
symbol=symbol)
#'''
#'''
bot=MultiStrategyBot(logger=logger, directionFilter= 0)
bot.add_strategy(KuegiStrategy()
)
bot.add_strategy(SfpStrategy()
)
b= BackTest(bot, bars_full, funding=funding, symbol=symbol,market_slipage_percent=0.15).run()
#performance chart with lots of numbers
bot.create_performance_plot(bars).show()
# chart with signals:
b.prepare_plot().show()
#'''