forked from wxylssy/LSSY
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunWork.py
executable file
·87 lines (80 loc) · 3.27 KB
/
runWork.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
#! /usr/bin/python3
from QuotationServer import historyQuotation
from QuotationServer import comm
from QuotationServer import runQuotation
from QuotationServer import zbStrategy
from WebServer import runWeb
import redisRW
import rpcTrade
from multiprocessing import Process
import time
import datetime
from dateutil.relativedelta import relativedelta
import sys
from decimal import *
commobj = None
def update(rdinfo, date: datetime.datetime):
# 3年历史数据
back_y = 3
historyQuotation.get_all_clear_finance_qfq_kline_from_start_date(date - relativedelta(years=back_y))
rdinfo.write_json('update', {'datetime': date})
print(date, '数据更新完毕,请进行今日复盘。')
def fupan():
# 选股
zbstrategy = zbStrategy.Strategy(None)
zbstrategy.stock_select(redisRW.redisrw(redisRW.db_chicang))
tk_date = datetime.datetime.now()
# 获取最新一天的ticks,并启动回测
commobj = comm.commObj(redisRW.redisrw(redisRW.db_backtest_chicang), 1, is_backtest=True)
getticks = runQuotation.get_ticks_from_xg_and_start_backtest(commobj, tk_date)
while not getticks:
print(tk_date, '没有ticks,尝试前一天。')
tk_date -= datetime.timedelta(days=1)
getticks = runQuotation.get_ticks_from_xg_and_start_backtest(commobj, tk_date)
print('策略提示', '将买入标记重点的股票。')
return True
def start_loop():
# 更新时间
hour = 18
rdinfo = redisRW.redisrw(redisRW.db_info)
# 上次入库时间
update_time = rdinfo.read_dec_datetime('update')
if update_time is not None:
last_up_date = update_time['datetime']
now_date = datetime.datetime.now()
diff_days = (now_date.date() - last_up_date.date()).days
if diff_days > 1 or (diff_days == 1 and last_up_date.hour < hour):
last_up_date = now_date
update(rdinfo, last_up_date)
else:
last_up_date = datetime.datetime.now()
update(rdinfo, last_up_date)
while True:
now_date = datetime.datetime.now()
if now_date.hour >= hour:
# 只有在今天指定时间更新才有效
if last_up_date.date() < now_date.date() or last_up_date.hour < hour:
last_up_date = now_date
update(rdinfo, last_up_date)
if comm.is_trade_time():
global commobj
print('登陆交易终端', rpcTrade.login())
# 清空数据
redisRW.redisrw(redisRW.db_backtest_chicang).del_db()
redisRW.redisrw(redisRW.db_sell).del_db()
runQuotation.start_quotation_process(commobj)
print('结束交易终端', rpcTrade.kill())
time.sleep(5)
def main():
global commobj
if len(sys.argv) > 1 and sys.argv[1] == 'b':
commobj = comm.commObj(redisRW.redisrw(redisRW.db_backtest_chicang), Decimal('1000000'), is_backtest=True)
print('进入回测系统。可用资金:', commobj.balance)
else:
commobj = comm.commObj(redisRW.redisrw(redisRW.db_chicang), Decimal(rpcTrade.get_ky_balance()),
is_backtest=False)
print('进入实盘系统。可用资金:', commobj.balance)
Process(target=runWeb.main, args=(commobj, )).start()
start_loop()
if __name__ == "__main__":
main()