-
Notifications
You must be signed in to change notification settings - Fork 21
/
tdx_features.py
121 lines (98 loc) · 4.65 KB
/
tdx_features.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pytdx
from pytdx.hq import TdxHq_API
import pandas as pd
from common.framework import *
from common.common import endday
import json
import talib
import numpy as np
api = TdxHq_API(auto_retry=True)
hostname="http://klang.org.cn"
hostname="http://klang.zhanluejia.net.cn"
#hostname="http://127.0.0.1:1337"
filename_sl = os.path.expanduser("~/.klang_stock_list.csv")
filename_st = os.path.expanduser("~/.klang_stock_trader.csv")
def updatestocklist(stname=filename_sl):
json = requests.get(hostname+"/industries").json()
#json = requests.get('http://klang.org.cn'+"/industries").json()
for i in json:
cm_dict[i['code']] = i.get('chouma','50')
df = pd.json_normalize(json)
df = df.drop(columns=['_id','updatedAt','id','createdAt'])
# 结果集输出到csv文件
df.to_csv(stname, index=False,columns=['updateDate','code','code_name','industry','industryClassification','tdxbk','tdxgn'])
def get_bar(name,code):
zone,code1 = code.split('.')
if zone == "sz":
zone = 0
if zone == "sh":
zone = 1
print(name,code1)
datas = api.get_security_bars(9,zone,code1, 0, 300)
datas = api.to_df(datas)
if len(datas) < 2:
return
datas = datas.assign(date=datas['datetime'].apply(lambda x: str(x)[0:10])).drop(['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1)
C = datas.close
ma5 = talib.MA(datas.close,5) / C
ma10 = talib.MA(datas.close,10) / C
ma20 = talib.MA(datas.close,20) / C
ma30 = talib.MA(datas.close,30) / C
ma60 = talib.MA(datas.close,60) / C
ma120 = talib.MA(datas.close,120) / C
rise = (datas['close'].values[1:]/datas['close'].values[:-1] - 1) * 100
rise = np.insert(rise,0,np.NaN)
mavol10 = talib.MA(datas.vol,10)
risevol = datas.vol / mavol10
diff,dea,macd = talib.MACD(datas.close,fastperiod=12, slowperiod=26, signalperiod=9)
macd = macd * 2
func = lambda name :getattr(talib,name)(datas.open, datas.high, datas.low, datas.close)
"""
talibdict = {i:func(i) for i in ['CDL2CROWS','CDL3BLACKCROWS','CDL3INSIDE','CDL3LINESTRIKE','CDL3OUTSIDE','CDL3STARSINSOUTH','CDL3WHITESOLDIERS',
'CDLABANDONEDBABY','CDLADVANCEBLOCK','CDLBELTHOLD','CDLBREAKAWAY','CDLCLOSINGMARUBOZU','CDLCONCEALBABYSWALL',
'CDLCOUNTERATTACK','CDLDARKCLOUDCOVER','CDLDOJI','CDLDOJISTAR','CDLDRAGONFLYDOJI','CDLENGULFING','CDLEVENINGDOJISTAR',
'CDLEVENINGSTAR','CDLGAPSIDESIDEWHITE','CDLGRAVESTONEDOJI','CDLHAMMER','CDLHANGINGMAN','CDLHARAMI',
'CDLHARAMICROSS','CDLHIGHWAVE','CDLHIKKAKE','CDLHIKKAKEMOD','CDLHOMINGPIGEON','CDLIDENTICAL3CROWS',
'CDLINNECK','CDLINVERTEDHAMMER','CDLKICKING','CDLKICKINGBYLENGTH','CDLLADDERBOTTOM','CDLLONGLEGGEDDOJI',
'CDLLONGLINE','CDLMARUBOZU','CDLMATCHINGLOW','CDLMATHOLD','CDLMORNINGDOJISTAR','CDLMORNINGSTAR',
'CDLONNECK','CDLPIERCING','CDLRICKSHAWMAN','CDLRISEFALL3METHODS','CDLSEPARATINGLINES','CDLSHOOTINGSTAR',
'CDLSHORTLINE','CDLSPINNINGTOP','CDLSTALLEDPATTERN','CDLSTICKSANDWICH','CDLTAKURI','CDLTASUKIGAP',
'CDLTHRUSTING','CDLTRISTAR','CDLUNIQUE3RIVER','CDLUPSIDEGAP2CROWS','CDLXSIDEGAP3METHODS',
]}
"""
#个性化的
talibdict = {
'ma5':ma5,
'ma10':ma10,
'ma20':ma20,
'ma30':ma30,
'ma60':ma60,
'ma120':ma120,
'rise':rise,
'risevol':risevol,
'diff':diff,
'dea':dea,
'macd':macd,
'date':datas.date}
print(len(datas),datas.iloc[-1].date)
datas1 = pd.DataFrame(talibdict)
print(datas1)
df = datas1.to_json(orient='table')
jsondatas = json.loads(df)['data']
for d in jsondatas:
d['name'] = name
d['code'] = code
del d['index']
#print(jsondatas)
try:
requests.post(hostname+"/features/updates",json=jsondatas,timeout=2000)
except:
time.sleep(2)
requests.post(hostname+"/features/updates",json=jsondatas,timeout=2000)
if api.connect('119.147.212.81', 7709):
updatestocklist()
init_stock_list()
from common.framework import stocklist
for stock in stocklist :
code ,name ,tdxbk,tdxgn = getstockinfo(stock)
get_bar(name,code)