-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
84 lines (63 loc) · 2.56 KB
/
bot.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
import sys, getopt
import time
import pprint
import copy
import shared
from botchart import BotChart
from botstrategy import BotStrategy
from botcandlestick import BotCandlestick
import ccxt
def main(argv):
live = False
try:
opts, args = getopt.getopt(argv, "", ["live"])
except getopt.GetoptError:
print('trading-bot.py')
sys.exit(2)
for opt, arg in opts:
if opt == "--live":
print("You're going live... Losses are your responsibility only!")
live = True
# START_DATE specified: we are in backtest mode
if shared.strategy['start_date']:
chart = BotChart()
strategy = BotStrategy()
strategy.showPortfolio()
for candlestick in chart.getPoints():
strategy.tick(candlestick)
chart.drawChart(strategy.candlesticks, strategy.trades, strategy.movingAverages)
strategy.showPortfolio()
else:
chart = BotChart(False)
strategy = BotStrategy(False, live)
strategy.showPortfolio()
candlestick = BotCandlestick()
x = 0
while True:
try:
currentPrice = chart.getCurrentPrice()
candlestick.tick(currentPrice)
strategy.tick(candlestick)
except ccxt.NetworkError as e:
print(type(e).__name__, e.args, 'Exchange error (ignoring)')
except ccxt.ExchangeError as e:
print(type(e).__name__, e.args, 'Exchange error (ignoring)')
except ccxt.DDoSProtection as e:
print(type(e).__name__, e.args, 'DDoS Protection (ignoring)')
except ccxt.RequestTimeout as e:
print(type(e).__name__, e.args, 'Request Timeout (ignoring)')
except ccxt.ExchangeNotAvailable as e:
print(type(e).__name__, e.args, 'Exchange Not Available due to downtime or maintenance (ignoring)')
except ccxt.AuthenticationError as e:
print(type(e).__name__, e.args, 'Authentication Error (missing API keys, ignoring)')
drawingCandles = copy.copy(strategy.candlesticks)
if not candlestick.isClosed():
drawingCandles.append(copy.copy(candlestick))
drawingCandles[-1].close = candlestick.currentPrice
chart.drawChart(drawingCandles, strategy.trades, strategy.movingAverages)
if candlestick.isClosed():
candlestick = BotCandlestick()
x += 1
time.sleep(shared.exchange['interval'])
if __name__ == "__main__":
main(sys.argv[1:])