forked from WilliamZhuo/BASIC_PYTHON_TRADING_BOOK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_touchance.py
160 lines (144 loc) · 5.75 KB
/
read_touchance.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import time
from tcoreapi_mq import *
import tcoreapi_mq
import threading
import pandas as pd
import datetime
from dateutil import tz
g_QuoteZMQ = None
g_QuoteSession = ""
#實時行情回補
def OnRealTimeQuote(symbol):
print("商品:", symbol["Symbol"], "成交價:",symbol["TradingPrice"], "開:", symbol["OpeningPrice"], "高:", symbol["HighPrice"], "低:", symbol["LowPrice"])
#行情消息接收
def quote_sub_th(obj,sub_port,filter = ""):
socket_sub = obj.context.socket(zmq.SUB)
#socket_sub.RCVTIMEO=7000 #ZMQ超時設定
socket_sub.connect("tcp://127.0.0.1:%s" % sub_port)
socket_sub.setsockopt_string(zmq.SUBSCRIBE,filter)
while(True):
message = (socket_sub.recv()[:-1]).decode("utf-8")
index = re.search(":",message).span()[1] # filter
message = message[index:]
message = json.loads(message)
#for message in messages:
if(message["DataType"]=="REALTIME"):
OnRealTimeQuote(message["Quote"])
elif(message["DataType"]=="GREEKS"):
OnGreeks(message["Quote"])
elif(message["DataType"]=="TICKS" or message["DataType"]=="1K" or message["DataType"]=="DK" ):
#print("@@@@@@@@@@@@@@@@@@@@@@@",message)
strQryIndex = ""
while(True):
s_history = obj.GetHistory(g_QuoteSession, message["Symbol"], message["DataType"], message["StartTime"], message["EndTime"], strQryIndex)
historyData = s_history["HisData"]
if len(historyData) == 0:
break
last = ""
for data in historyData:
last = data
#print("歷史行情:Time:%s, Volume:%s, QryIndex:%s" % (data["Time"], data["Volume"], data["QryIndex"]))
strQryIndex = last["QryIndex"]
return
def HisDataToDF(HisData):
#UTC+0 to UTC+8
# https://stackoverflow.com/questions/4770297/convert-utc-datetime-string-to-local-datetime
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('Asia/Taipei')
#HisData to dataframe, and turn index to datetime
df=pd.DataFrame(HisData)
row=HisData[0]
df_date=df['Date'].tolist()
df_time=df['Time'].tolist()
l=[]
for i in range(0,len(df_time),1):
date=df_date[i]
time=df_time[i]
if(len(time)==4):
time='00'+time
elif(len(time)==5):
time='0'+time
date_obj = datetime.datetime.strptime(date, '%Y%m%d')
time_obj = datetime.datetime.strptime(time, '%H%M%S')
date_time_obj=datetime.datetime.combine(date_obj.date(),
time_obj.time())
date_time_obj = date_time_obj.replace(tzinfo=from_zone)
date_time_obj = date_time_obj.astimezone(to_zone)
l.append(date_time_obj)
df.index=l
return df
def read_data_to_df(contractName,#'FITX'
#time:YYYYMMDDHHmm 年/月/日/時/分
StrTim,#開始時間,ex:'2021031600'
EndTim,#結束時間,ex:'2021031700'
):
global g_QuoteZMQ
global g_QuoteSession
#登入(與 TOUCHANCE zmq 連線用,不可改)
g_QuoteZMQ = QuoteAPI("ZMQ","8076c9867a372d2a9a814ae710c256e2")
q_data = g_QuoteZMQ.Connect("51237")
#print("q_data=",q_data)
if q_data["Success"] != "OK":
#print("[quote]connection failed")
return
g_QuoteSession = q_data["SessionKey"]
#查詢指定合约訊息
quoteSymbol = "TC.F.TWF."+contractName+".HOT"
#print("查詢指定合約:",g_QuoteZMQ.QueryInstrumentInfo(g_QuoteSession, quoteSymbol))
#查詢指定類型合約列表
#期貨:Fut
#期權:Opt
#證券:Sto
#print("查詢合約:",g_QuoteZMQ.QueryAllInstrumentInfo(g_QuoteSession,"Fut"))
#####################################################################行情################################################
#建立一個行情線程
t2 = threading.Thread(target = quote_sub_th,args=(g_QuoteZMQ,q_data["SubPort"],))
t2.start()
#資料週期
type = "1K"
#資料頁數
QryInd = '0'
#訂閱歷史資料
SubHis = g_QuoteZMQ.SubHistory(g_QuoteSession,quoteSymbol,type,StrTim,EndTim)
#print("訂閱歷史資料:",SubHis)
#等待回補
#獲取回補的資料
i = 0
while(1): #等待訂閱回補
i=i+1
time.sleep(1)
QPong = g_QuoteZMQ.Pong(g_QuoteSession)
# print("第"+str(i)*5+"秒,Pong:",QPong)
HisData = g_QuoteZMQ.GetHistory(g_QuoteSession, quoteSymbol, type, StrTim, EndTim, QryInd)
if (len(HisData['HisData']) != 0):
#print("回補成功")
i = 0
break
df_result = pd.DataFrame()
while (1): # 獲取訂閱成功的全部歷史資料並另存
i = i + 1
HisData = g_QuoteZMQ.GetHistory(g_QuoteSession, quoteSymbol, type, StrTim, EndTim, QryInd)
if (len(HisData['HisData']) != 0):
HisData=HisData['HisData']
df_result=df_result.append(HisDataToDF(HisData))
#print(HisData['HisData'][0])
QryInd = str(int(QryInd) + 1)
else:
break
return df_result
def removeUnuesdData(df_input):
kbars_out = pd.DataFrame(columns = ['Open','High','Low','Close','Volume'])
kbars_out['Open'] = df_input['Open']
kbars_out['High'] = df_input['High']
kbars_out['Low'] = df_input['Low']
kbars_out['Close'] = df_input['Close']
kbars_out['Volume'] = df_input['Volume']
kbars_out=kbars_out.dropna()
return kbars_out
if __name__ == '__main__':
df_result=read_data_to_df(contractName='FITX',
StrTim='2021031600',
EndTim='2021031700'
)
df_clean=removeUnuesdData(df_result)