Created
July 15, 2019 01:59
-
-
Save Yvictor/d01c123d5a4abe9760a23b5b6fb094f6 to your computer and use it in GitHub Desktop.
shioaji quote tony version code review
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shioaji as sj | |
import pandas as pd | |
import sys | |
import os | |
import datetime | |
from datetime import datetime as datetime2 | |
from threading import Timer | |
'''user_information & barminute''' | |
min_k=1 | |
begin1='15:00:00.000000' | |
end1='17:00:00.000000' | |
begin2='15:00:00' | |
set_barfreq=60*min_k | |
begin1t=datetime2.strptime(begin1,'%H:%M:%S.%f') | |
end1t=datetime2.strptime(end1,'%H:%M:%S.%f') | |
today=str(datetime.date.today()) | |
txf_name='台指期貨' | |
dit_totalquote={} | |
lit_p=[] | |
df_bar_csv=pd.DataFrame(columns=['DateTime','Date','Time','Open','High','Low','Close','Volume']) | |
'''sj_login''' | |
api=sj.Shioaji(backend='http',simulation=False) | |
api.login(user_id,user_pw) | |
api.activate_ca(user_ca,user_id,user_id) | |
'''sj_event"e''' | |
def event_callback(resp_code, event_code, event): | |
print("Respone Code: {} | Event Code: {} | Event: {}".format(resp_code, event_code, event)) | |
def quote_callback(title_fromsj,quote_fromsj): | |
get_kbar(title=title_fromsj,quote=quote_fromsj) | |
def get_kbar(title,quote): | |
global fsttick | |
dit_totalquote[title]=quote | |
dit_key1=list(dit_totalquote.keys())[0] | |
date_p=dit_totalquote[dit_key1].get('Date') #str | |
time_p=datetime2.strptime(dit_totalquote[dit_key1].get('Time'),'%H:%M:%S.%f').time() | |
close_p=dit_totalquote[dit_key1].get('Close')[0] #float | |
vol_p=dit_totalquote[dit_key1].get('Volume')[0] #int | |
if all([time_p>=begin1t.time(),time_p<end1t.time()]): | |
if len(lit_p)==0: | |
inikstr=begin2 | |
inik=datetime2.strptime(inikstr,'%H:%M:%S') | |
nxtk=inik+datetime.timedelta(0,set_barfreq) | |
nxtkstr=str(nxtk)[-8:] | |
lit_p.append([date_p+' '+nxtkstr, | |
date_p, | |
nxtkstr, | |
close_p, | |
close_p, | |
close_p, | |
close_p, | |
vol_p, | |
inikstr, | |
nxtkstr]) | |
fsttick=dit_totalquote[dit_key1].get('Time') | |
elif all([time_p>=datetime2.strptime(lit_p[-1][8],'%H:%M:%S').time(), | |
time_p<datetime2.strptime(lit_p[-1][9],'%H:%M:%S').time()]): | |
if close_p>lit_p[-1][4]: | |
lit_p[-1][4]=close_p | |
elif close_p<lit_p[-1][5]: | |
lit_p[-1][5]=close_p | |
lit_p[-1][6]=close_p | |
lit_p[-1][7]+=vol_p | |
else: | |
inikstr=lit_p[-1][9] | |
inik=datetime2.strptime(inikstr,'%H:%M:%S') | |
nxtk=inik+datetime.timedelta(0,set_barfreq) | |
nxtkstr=str(nxtk)[-8:] | |
lit_p.append([date_p+' '+nxtkstr, | |
date_p, | |
nxtkstr, | |
close_p, | |
close_p, | |
close_p, | |
close_p, | |
vol_p, | |
inikstr, | |
nxtkstr]) | |
if len(lit_p)>=1: | |
tmp=len(lit_p)-1 | |
df_bar_csv.loc[tmp]=lit_p[tmp][0:8] | |
def savedf(): | |
df_bar_csv.to_csv(today+txf_name+'.csv',index=False) | |
sys.exit() | |
#save df to csv and shutdown module | |
now=datetime2.now() | |
finistr=today+' '+end1 | |
finitime=datetime2.strptime(finistr,'%Y-%m-%d %H:%M:%S.%f') | |
difftime=finitime-now | |
secsave=difftime.days*60*60*24+difftime.seconds | |
t=Timer(interval=secsave,function=savedf) | |
t.start() | |
'''sj_setting''' | |
txf_01=api.Contracts.Futures.TXF.TXF201907 | |
api.quote.subscribe(txf_01) | |
api.quote.set_event_callback(event_callback) | |
api.quote.set_callback(quote_callback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment