-
Notifications
You must be signed in to change notification settings - Fork 22
/
tdx_day.py
77 lines (59 loc) · 2.22 KB
/
tdx_day.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
#pytdx
from pytdx.hq import TdxHq_API
import pandas as pd
from common.framework import *
from common.common import endday
import json
api = TdxHq_API(auto_retry=True)
hostname="https://klang.org.cn/api"
#hostname="http://klang.zhanluejia.net.cn"
filename_sl = os.path.expanduser("~/.klang_stock_list.csv")
session = ""
def updatestocklist(stname=filename_sl):
json = requests.get(hostname+"/industries").json()
for i in json:
cm_dict[i['code']] = i.get('chouma','50')
df = pd.json_normalize(json)
df = df.drop(columns=['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, 5)
info = api.get_finance_info(zone, code1)
datas = api.to_df(datas)
if len(datas) < 2:
return
liutonggu = float(info['liutongguben'])
datas = datas.assign(date=datas['datetime'].apply(lambda x: str(x)[0:10])).drop(['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1)
datas.rename(columns={'vol':'volume'},inplace = True)
print(len(datas),datas.iloc[-1].date)
datas = datas [datas['volume'] > 0]
df = datas.to_json(orient='table')
jsondatas = json.loads(df)['data']
for d in jsondatas:
d['name'] = name
d['code'] = code
d['volume'] = float("%.4f" % (d['volume'] * 100)) #股 = 手*100
d['turn'] = float("%.4f" %(d['volume']*100 / liutonggu))
del d['index']
#print(jsondatas)
#print(datas.iloc[-1],liutonggu,d)
try:
resp = session.post(hostname+"/dayks/updates",json=jsondatas,timeout=2000)
except:
time.sleep(2)
session.post(hostname+"/dayks/updates",json=jsondatas,timeout=2000)
if api.connect('119.147.212.81', 7709):
updatestocklist()
init_stock_list()
from common.framework import stocklist
session = requests.Session()
for stock in stocklist[:]:
code ,name ,tdxbk,tdxgn = getstockinfo(stock)
get_bar(name,code)