forked from yijixiuxin/chanlun-pro
-
Notifications
You must be signed in to change notification settings - Fork 3
/
reboot_trader_currency.py
98 lines (77 loc) · 2.69 KB
/
reboot_trader_currency.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
import logging
import pickle
import sys
import time
import traceback
cur_path = sys.path[0]
sys.path.append(sys.path[0] + "/../..")
from cl_v2 import exchange_binance
from cl_v2 import rd
from cl_v2 import cl
from cl_v2.my import strategy_demo
from cl_v2.my import trader_currency
logging.basicConfig(filename=sys.path[0] + '/logs/trader_currency.log', level='INFO',
format='%(asctime)s - %(levelname)s : %(message)s')
logging.info('数字货币自动化交易程序')
def position_symbols(td: trader_currency.CurrencyTrader):
"""
获取当前持仓中的交易对儿
:param td:
:return:
"""
codes = []
for code in td.positions:
for mmd in td.positions[code]:
pos = td.positions[code][mmd]
if pos.balance > 0:
codes.append(code)
codes = list(set(codes))
return codes
try:
exchange = exchange_binance.ExchangeBinance()
run_num = 40
run_codes = exchange.ticker24HrRank(run_num)
p_redis_key = 'trader_currency'
# 从 Redis 中恢复交易对象
p_bytes = rd.get_byte(p_redis_key)
if p_bytes is not None:
TR = pickle.loads(p_bytes)
else:
STR = strategy_demo.Strategy_Demo()
TR = trader_currency.CurrencyTrader('Currency', is_stock=False, is_futures=True, log=logging.info, mmds=None)
TR.set_strategy(STR)
# 单独设置一些参数,更新之前缓存的参数
TR.allow_mmds = None
while True:
try:
seconds = int(time.time())
if seconds % (60 * 60) == 0:
# 每一个小时,更新 24 小时交易量排行代码
run_codes = exchange.ticker24HrRank(run_num)
logging.info('Run symbols: %s' % run_codes)
if seconds % (5 * 60) != 0:
time.sleep(1)
continue
# 增加当前持仓中的交易对儿
run_codes = position_symbols(TR) + run_codes
run_codes = list(set(run_codes))
for code in run_codes:
try:
# logging.info('Run Symbol: ' + symbol)
cl_datas = {}
for f in ['30m', '5m']:
klines = exchange.klines(code, f)
cl_datas[f] = cl.CL(code, klines, f)
TR.run(code, cl_datas)
except Exception as e:
logging.error(traceback.format_exc())
# 保存对象到 Redis 中
p_obj = pickle.dumps(TR)
rd.save_byte(p_redis_key, p_obj)
except Exception as e:
logging.error(str(e))
except Exception as e:
logging.error(str(e))
finally:
logging.info('Done')
exit()