-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrade.py
executable file
·72 lines (59 loc) · 1.98 KB
/
trade.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
#!/usr/bin/env python
import copy
import importlib
import gzip
import json
import time
import traceback
import sys
from bittrex_exchange import BittrexExchange
def load_trading_plan_class(module_name):
module = importlib.import_module(module_name)
return module.trading_plan_class
def main(exch=None):
if len(sys.argv) < 3:
print('Usage: %s <trading plan> [-b] <pair> [<args>]' % sys.argv[0])
sys.exit(1)
trading_plan_class = load_trading_plan_class(sys.argv[1])
buy = (sys.argv[2] == '-b')
if buy:
args = sys.argv[3:]
else:
args = sys.argv[2:]
if not exch:
exch = BittrexExchange(True)
trading_plan = trading_plan_class(exch, sys.argv[1], args, buy)
ticks = exch.get_candles(trading_plan.pair, 'oneMin')
try:
main_loop(exch, trading_plan.pair, trading_plan, ticks)
except KeyboardInterrupt:
print('\nInterrupted by user')
except BaseException:
print(traceback.format_exc())
if len(ticks) > 0:
filename = '%s-%s.trade' % (trading_plan.pair, ticks[0]['T'])
with gzip.open(filename, 'w') as fout:
data = {'candles': ticks,
'plan': sys.argv[1],
'pair': trading_plan.pair,
'balance': trading_plan.balance,
'available': trading_plan.available,
'args': trading_plan.args}
json_str = json.dumps(data) + '\n'
json_bytes = json_str.encode('utf-8')
fout.write(json_bytes)
print('Trade saved in %s' % filename)
def main_loop(exch, pair, trading_plan, ticks):
prev_tick = None
while True:
tick = exch.get_tick(pair)
if tick and tick != prev_tick:
prev_tick = tick
ticks.append(tick)
trading_plan.tick = copy.deepcopy(tick)
if not trading_plan.process_tick():
break
time.sleep(30)
if __name__ == "__main__":
main()
# trade.py ends here