This repository has been archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathmain.py
executable file
·115 lines (94 loc) · 3.69 KB
/
main.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
#!/usr/bin/env python3
import time
import backtrader as bt
import datetime as dt
from ccxtbt import CCXTStore
from config import BINANCE, ENV, PRODUCTION, COIN_TARGET, COIN_REFER, DEBUG
from dataset.dataset import CustomDataset
from sizer.percent import FullMoney
from strategies.basic_rsi import BasicRSI
from utils import print_trade_analysis, print_sqn, send_telegram_message
def main():
cerebro = bt.Cerebro(quicknotify=True)
if ENV == PRODUCTION: # Live trading with Binance
broker_config = {
'apiKey': BINANCE.get("key"),
'secret': BINANCE.get("secret"),
'nonce': lambda: str(int(time.time() * 1000)),
'enableRateLimit': True,
}
store = CCXTStore(exchange='binance', currency=COIN_REFER, config=broker_config, retries=5, debug=DEBUG)
broker_mapping = {
'order_types': {
bt.Order.Market: 'market',
bt.Order.Limit: 'limit',
bt.Order.Stop: 'stop-loss',
bt.Order.StopLimit: 'stop limit'
},
'mappings': {
'closed_order': {
'key': 'status',
'value': 'closed'
},
'canceled_order': {
'key': 'status',
'value': 'canceled'
}
}
}
broker = store.getbroker(broker_mapping=broker_mapping)
cerebro.setbroker(broker)
hist_start_date = dt.datetime.utcnow() - dt.timedelta(minutes=30000)
data = store.getdata(
dataname='%s/%s' % (COIN_TARGET, COIN_REFER),
name='%s%s' % (COIN_TARGET, COIN_REFER),
timeframe=bt.TimeFrame.Minutes,
fromdate=hist_start_date,
compression=30,
ohlcv_limit=99999
)
# Add the feed
cerebro.adddata(data)
else: # Backtesting with CSV file
data = CustomDataset(
name=COIN_TARGET,
dataname="dataset/binance_nov_18_mar_19_btc.csv",
timeframe=bt.TimeFrame.Minutes,
fromdate=dt.datetime(2018, 9, 20),
todate=dt.datetime(2019, 3, 13),
nullvalue=0.0
)
cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=30)
broker = cerebro.getbroker()
broker.setcommission(commission=0.001, name=COIN_TARGET) # Simulating exchange fee
broker.setcash(100000.0)
cerebro.addsizer(FullMoney)
# Analyzers to evaluate trades and strategies
# SQN = Average( profit / risk ) / StdDev( profit / risk ) x SquareRoot( number of trades )
cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="ta")
cerebro.addanalyzer(bt.analyzers.SQN, _name="sqn")
# Include Strategy
cerebro.addstrategy(BasicRSI)
# Starting backtrader bot
initial_value = cerebro.broker.getvalue()
print('Starting Portfolio Value: %.2f' % initial_value)
result = cerebro.run()
# Print analyzers - results
final_value = cerebro.broker.getvalue()
print('Final Portfolio Value: %.2f' % final_value)
print('Profit %.3f%%' % ((final_value - initial_value) / initial_value * 100))
print_trade_analysis(result[0].analyzers.ta.get_analysis())
print_sqn(result[0].analyzers.sqn.get_analysis())
if DEBUG:
cerebro.plot()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("finished.")
time = dt.datetime.now().strftime("%d-%m-%y %H:%M")
send_telegram_message("Bot finished by user at %s" % time)
except Exception as err:
send_telegram_message("Bot finished with error: %s" % err)
print("Finished with error: ", err)
raise