forked from nicolasbonnici/cryptobot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·104 lines (85 loc) · 2.9 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
#!/usr/bin/python3
import importlib
import signal
import sys
import threading
from decouple import config
from services.backtest import Backtest
from services.importer import Importer
exchange_name = config('EXCHANGE')
available_exchanges = config('AVAILABLE_EXCHANGES').split(',')
mode: str = config('MODE')
strategy: str = config('STRATEGY')
trading_mode: str = config('TRADING_MODE')
interval: int = int(config('CANDLE_INTERVAL'))
currency: str = config('CURRENCY')
asset: str = config('ASSET')
if trading_mode == 'real':
print("*** Caution: Real trading mode activated ***")
else:
print("Test mode")
# Parse symbol pair from first command argument
if len(sys.argv) > 1:
currencies = sys.argv[1].split('_')
if len(currencies) > 1:
currency = currencies[0]
asset = currencies[1]
# Load exchange
print("Connecting to {} exchange...".format(exchange_name[0].upper() + exchange_name[1:]))
exchangeModule = importlib.import_module('exchanges.' + exchange_name, package=None)
exchangeClass = getattr(exchangeModule, exchange_name[0].upper() + exchange_name[1:])
exchange = exchangeClass(config(exchange_name.upper() + '_API_KEY'), config(exchange_name.upper() + '_API_SECRET'))
# Load currencies
exchange.set_currency(currency)
exchange.set_asset(asset)
# Load strategy
strategyModule = importlib.import_module('strategies.' + strategy, package=None)
strategyClass = getattr(strategyModule, strategy[0].upper() + strategy[1:])
exchange.set_strategy(strategyClass(exchange, interval))
# mode
print("{} mode on {} symbol".format(mode, exchange.get_symbol()))
if mode == 'trade':
exchange.strategy.start()
elif mode == 'live':
exchange.start_symbol_ticker_socket(exchange.get_symbol())
elif mode == 'backtest':
period_start = config('PERIOD_START')
period_end = config('PERIOD_END')
print(
"Backtest period from {} to {} with {} seconds candlesticks.".format(
period_start,
period_end,
interval
)
)
Backtest(exchange, period_start, period_end, interval)
elif mode == 'import':
period_start = config('PERIOD_START')
period_end = config('PERIOD_END')
print(
"Import mode on {} symbol for period from {} to {} with {} seconds candlesticks.".format(
exchange.get_symbol(),
period_start,
period_end,
interval
)
)
importer = Importer(exchange, period_start, period_end, interval)
importer.process()
else:
print('Not supported mode.')
def signal_handler(signal, frame):
if (exchange.socket):
print('Closing WebSocket connection...')
exchange.close_socket()
sys.exit(0)
else:
print('stopping strategy...')
exchange.strategy.stop()
sys.exit(0)
# Listen for keyboard interrupt event
signal.signal(signal.SIGINT, signal_handler)
forever = threading.Event()
forever.wait()
exchange.strategy.stop()
sys.exit(0)