forked from jptsantossilva/BEC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_backtesting.py
465 lines (371 loc) · 15.6 KB
/
my_backtesting.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import pandas as pd
import sys
import os
from datetime import date
from dateutil.relativedelta import relativedelta
import time
import logging
import timeit
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
import utils.telegram as telegram
import utils.database as database
# import utils.config as config
from exchanges.binance import client
# sets the output display precision in terms of decimal places to 8.
# this is helpful when trading against BTC. The value in the dataframe has the precision 8 but when we display it
# by printing or sending to telegram only shows precision 6
pd.set_option("display.precision", 8)
# log file to store error messages
log_filename = "symbol_by_market_phase.log"
logging.basicConfig(filename=log_filename, level=logging.INFO,
format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p -')
FOLDER_BACKTEST_RESULTS = "static/backtest_results"
# backtest with 4 years of price data
#-------------------------------------
today = date.today()
# today - 4 years - 200 days (DSMA200)
pastdate = today - relativedelta(years=4) - relativedelta(days=200)
# print(pastdate)
tuple = pastdate.timetuple()
timestamp = time.mktime(tuple)
startdate = str(timestamp)
# startdate = "15 Dec, 2018 UTC"
# startdate = "12 May, 2022 UTC"
# startdate = "4 year ago UTC"
# startdate = "10 day ago UTC"
#-------------------------------------
timeframe = ""
def EMA(values, n):
"""
Return exp moving average of `values`, at
each step taking into account `n` previous values.
"""
return pd.Series(values).ewm(span=n, adjust=False).mean()
def SMA(values, n):
"""
Return simple moving average of `values`, at
each step taking into account `n` previous values.
"""
return pd.Series(values).rolling(n).mean()
#-------------------------------------
# Use SMA50 and SMA200
# BUY when price close > SMA50 and price close > SMA200 and SMA50<SMA200 (Accumulation Phase)
# BUY when price close > SMA50 and price close > SMA200 and SMA50 > SMA200
# SELL when price close < SMA50 or SMA200 (whatever happens first)
#-------------------------------------
class market_phases(Strategy):
nFastSMA = 50
nSlowSMA = 200
def init(self):
self.sma50 = self.I(SMA, self.data.Close, self.nFastSMA)
self.sma200 = self.I(SMA, self.data.Close, self.nSlowSMA)
def next(self):
SMA50 = self.sma50
SMA200 = self.sma200
priceClose = self.data.Close
accumulationPhase = (priceClose > SMA50) and (priceClose > SMA200) and (SMA50 < SMA200)
bullishPhase = (priceClose > SMA50) and (priceClose > SMA200) and (SMA50 > SMA200)
if not self.position:
if (accumulationPhase or bullishPhase):
# if crossover(fastEMA, slowEMA):
self.buy()
else:
if not(accumulationPhase or bullishPhase):
self.position.close()
#-------------------------------------
# we will use 2 exponencial moving averages:
# BUY when fast ema > slow ema
# SELL when slow ema > fast ema
#-------------------------------------
class ema_cross(Strategy):
n1 = 2
n2 = 14
def init(self):
self.emaFast = self.I(EMA, self.data.Close, self.n1)
self.emaSlow = self.I(EMA, self.data.Close, self.n2)
def next(self):
fastEMA = self.emaFast
slowEMA = self.emaSlow
if not self.position:
if crossover(fastEMA, slowEMA):
self.buy()
else:
if crossover(slowEMA, fastEMA):
self.position.close()
class ema_cross_with_market_phases(Strategy):
n1 = 7
n2 = 8
nFastSMA = 50
nSlowSMA = 200
def init(self):
self.emaFast = self.I(EMA, self.data.Close, self.n1)
self.emaSlow = self.I(EMA, self.data.Close, self.n2)
self.sma50 = self.I(SMA, self.data.Close, self.nFastSMA)
self.sma200 = self.I(SMA, self.data.Close, self.nSlowSMA)
def next(self):
fastEMA = self.emaFast
slowEMA = self.emaSlow
SMA50 = self.sma50
SMA200 = self.sma200
priceClose = self.data.Close
accumulationPhase = (priceClose > SMA50) and (priceClose > SMA200) and (SMA50 < SMA200)
bullishPhase = (priceClose > SMA50) and (priceClose > SMA200) and (SMA50 > SMA200)
if not self.position:
if (accumulationPhase or bullishPhase) and crossover(fastEMA, slowEMA):
# if crossover(fastEMA, slowEMA):
self.buy()
else:
if crossover(slowEMA, fastEMA):
self.position.close()
def get_data(symbol, timeframe):
# makes 3 attempts to get historical data
max_retry = 3
retry_count = 1
success = False
while retry_count < max_retry and not success:
try:
frame = pd.DataFrame(client.get_historical_klines(symbol
,timeframe
# better get all historical data.
# Using a defined start date will affect ema values.
# To get same ema and sma values of tradingview all historical data must be used.
,startdate
))
success = True
except Exception as e:
# avoid error message in telegram if error is related to non-existing trading pair
# example: CREAMUSDT - BinanceAPIException(Response [400], 400, code:-1121,msg:Invalid symbol.)
msg = repr(e)
print(msg)
invalid_symbol_error = '"code":-1121,"msg":"Invalid symbol.'
if invalid_symbol_error in msg:
frame = pd.DataFrame()
return frame
retry_count += 1
msg = sys._getframe( ).f_code.co_name+" - "+pSymbol+" - "+repr(e)
print(msg)
if not success:
msg = f"Failed after {max_retry} tries to get historical data. Unable to retrieve data. "
msg = msg + sys._getframe( ).f_code.co_name+" - "+pSymbol
msg = telegram.telegram_prefix_market_phases_sl + msg
print(msg)
telegram.send_telegram_message(telegram.telegram_token_main, telegram.EMOJI_WARNING, msg)
frame = pd.DataFrame()
return frame
else:
frame = frame.iloc[:,:6] # use the first 5 columns
frame.columns = ['Time','Open','High','Low','Close','Volume'] #rename columns
frame[['Open','High','Low','Close','Volume']] = frame[['Open','High','Low','Close','Volume']].astype(float) #cast to float
frame.Time = pd.to_datetime(frame.Time, unit='ms') #make human readable timestamp
# frame.index = [dt.datetime.fromtimestamp(x/1000.0) for x in frame.Time]
frame = frame.set_index(pd.DatetimeIndex(frame['Time']))
frame = frame.drop(['Time'], axis=1)
return frame
def get_strategy_name(strategy):
# get strategy name from strategy class
strategy_name = str(strategy).split('.')[-1][:-2]
return strategy_name
def save_backtesting_to_html(bt, stats, strategy, timeframe, symbol):
# stats
df_stats = pd.DataFrame(stats)
# trades
df_trades = pd.DataFrame(stats._trades)
# remove Size column
df_trades = df_trades.drop(columns=['Size'])
strategy_name = get_strategy_name(strategy)
filename=f"{strategy_name} - {timeframe} - {symbol}"
# Create the folder if it doesn't exist
if not os.path.exists(FOLDER_BACKTEST_RESULTS):
os.makedirs(FOLDER_BACKTEST_RESULTS)
# Specify the CSV file path
csv_file_path = os.path.join(FOLDER_BACKTEST_RESULTS, filename+".csv")
# Export both DataFrames to the same CSV file
df_stats.to_csv(csv_file_path, index=True)
df_trades.to_csv(csv_file_path, mode='a', index=False, header=True)
filename_path = os.path.join(FOLDER_BACKTEST_RESULTS, filename)
bt.plot(
# plot_return = True,
# plot_drawdown = True,
filename = filename_path,
open_browser=False)
# add stats and trade to html file
# add style
html_file_path = os.path.join(FOLDER_BACKTEST_RESULTS, filename+".html")
with open(html_file_path, 'r') as file:
html_content = file.read()
# Locate the style tag in the HTML content
style_tag_start = html_content.find('<style>')
if style_tag_start == -1:
head_tag_end = html_content.find('</head>')
style_content_to_add = """<style>\n</style>"""
modified_html_content = (
html_content[:head_tag_end-1]
+ style_content_to_add
+ html_content[head_tag_end-1:]
)
with open(html_file_path, 'w') as file:
file.write(modified_html_content)
#-----
with open(html_file_path, 'r') as file:
html_content = file.read()
# Locate the style tag in the HTML content
style_tag_start = html_content.find('<style>')
style_tag_end = html_content.find('</style>', style_tag_start)
# Append or modify the content of the style tag
# dataframe {
# text-align: left;
# }
style_content_to_add = """
h2 {
text-align: center;
font-family: Helvetica, Arial, sans-serif;
}
table {
margin-left: auto;
margin-right: auto;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
font-family: Helvetica, Arial, sans-serif;
font-size: 90%;
}
table tbody tr:hover {
background-color: #dddddd;
}
.wide {
width: 90%;
}
"""
modified_html_content = (
html_content[:style_tag_end]
+ style_content_to_add
+ html_content[style_tag_end:]
)
with open(html_file_path, 'w') as file:
file.write(modified_html_content)
#-----
# Convert the DataFrame to an HTML table
stats_html_table = df_stats.to_html(index=True, header=False)
trades_html_table = df_trades.to_html(index=False)
#-----
# add style
# html_file_path = filename+".html"
with open(html_file_path, 'r') as file:
html_content = file.read()
# Locate the style tag in the HTML content
body_tag_start = html_content.find('<body>')
body_tag_end = html_content.find('</body>', body_tag_start)
# Append or modify the content of the style tag
stats_table_title = "<h2> STATS </h2>\n"
stats_content_to_add = stats_table_title + stats_html_table
trades_table_title = "<h2> TRADES </h2>\n"
trades_content_to_add = trades_table_title + trades_html_table
body_content_to_add = stats_content_to_add + trades_content_to_add
modified_html_content = (
html_content[:body_tag_end]
+ body_content_to_add
+ html_content[body_tag_end:]
)
with open(html_file_path, 'w') as file:
file.write(modified_html_content)
#------
def run_backtest(symbol, timeframe, strategy, optimize):
# vars initialization
n1 = 0
n2 = 0
df = get_data(symbol, timeframe)
if df.empty:
return # exit function
commission_value = float(0.005)
cash_value = float(100000)
# Checking the value of strategy
bt = Backtest(df, strategy=strategy, cash=cash_value, commission=commission_value)
stats = bt.run()
# print(stats)
# bt.plot()
if optimize:
stats, heatmap = bt.optimize(
n1=range(10, 100, 10),
n2=range(20, 200, 10),
constraint=lambda param: param.n1 < param.n2,
maximize='Equity Final [$]',
return_heatmap=True
)
dfbema = pd.DataFrame(heatmap.sort_values().iloc[-1:])
n1 = dfbema.index.get_level_values(0)[0]
n2 = dfbema.index.get_level_values(1)[0]
return_perc = round(stats['Return [%]'],2)
buy_hold_return_Perc = round(stats['Buy & Hold Return [%]'],2)
backtest_start_date = str(df.index[0])
backtest_end_date = str(df.index[-1])
# get strategy name from strategy class
strategy_name = get_strategy_name(strategy)
# lista
print(f"Strategy = {strategy_name}")
if optimize:
print("n1 = ",n1)
print("n2 = ",n2)
print("Return [%] = ",return_perc)
print("Buy & Hold Return [%] = ",buy_hold_return_Perc)
print("Backtest start date = ", backtest_start_date)
print("Backtest end date =" , backtest_end_date)
# save results as html file
save_backtesting_to_html(bt, stats, strategy, timeframe, symbol)
database.add_backtesting_results(database.conn,
timeframe=timeframe,
symbol=symbol,
ema_fast=n1,
ema_slow=n2,
return_perc=return_perc,
buy_hold_return_perc=buy_hold_return_Perc,
backtest_start_date=backtest_start_date,
backtest_end_date=backtest_end_date,
strategy_Id=strategy_name
)
def get_backtesting_results(strategy_id, symbol, time_frame):
# get best ema
df = database.get_backtesting_results_by_symbol_timeframe_strategy(connection=database.conn,
symbol=symbol,
time_frame=time_frame,
strategy_id=strategy_id)
if not df.empty:
fast_ema = int(df.Ema_Fast.values[0])
slow_ema = int(df.Ema_Slow.values[0])
strategy_name = df.Name.values[0]
else:
fast_ema = int("0")
slow_ema = int("0")
# strategy_name
# strategy_name = str(fast_ema)+"/"+str(slow_ema)+" "+strategy_name
return fast_ema, slow_ema
def calc_backtesting(symbol, timeframe, strategy, optimize):
result = False
try:
# calculate run time
start = timeit.default_timer()
print("")
# get strategy name from strategy class
strategy_name = get_strategy_name(strategy)
print(f"Backtest strategy {strategy_name} - {symbol} - {timeframe} - Start")
run_backtest(symbol, timeframe, strategy, optimize)
print(f"Backtest strategy {strategy_name} - {symbol} - {timeframe} - End")
stop = timeit.default_timer()
total_seconds = stop - start
duration = database.calc_duration(total_seconds)
msg = f'Execution Time: {duration}'
print(msg)
result = True
return result
except Exception as e:
msg = sys._getframe( ).f_code.co_name+f" - " + repr(e)
msg = telegram.telegram_prefix_market_phases_sl + msg
print(msg)
logging.exception(msg)
telegram.send_telegram_message(telegram.telegram_token_main, telegram.EMOJI_WARNING, msg)
return False