Last active
August 29, 2015 14:10
-
-
Save QuantTraderEd/a0ebd05aa6a46d570ea5 to your computer and use it in GitHub Desktop.
xing_test2.py
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Jan 22 09:01:06 2015 | |
@author: assa | |
""" | |
import os | |
import sqlite3 as lite | |
import pandas as pd | |
def convert_strike(strike): | |
if type(strike).__name__ == 'unicode': | |
if strike[2] == '2' or strike[2] == '7': | |
return '%s.5'%strike | |
else: | |
return strike | |
pass | |
print os.path.dirname(os.path.realpath(__file__)) + '\\' | |
filedbname = 'TAQ_20150127.db' | |
conn = lite.connect(filedbname) | |
sql_text = """ | |
SELECT Time, ShortCD, LastPrice | |
FROM FutOptTickData | |
WHERE TAQ = 'E' | |
and Time between '15:05:00.000' and '15:15:00.000' | |
and substr(ShortCD,4,2) = 'K2' | |
""" | |
df = pd.read_sql(sql_text,conn) | |
ShortCDSet = set(list(df['ShortCD'])) | |
df_call = df[df['ShortCD'].str[:3] == '201'] | |
df_put = df[df['ShortCD'].str[:3] == '301'] | |
CallShortCDSet = set(df_call['ShortCD']) | |
PutShortCDSet = set(df_put['ShortCD']) | |
df['ShortCDStrike'] = df['ShortCD'].str[-3:] | |
StrikeLst = list(df.drop_duplicates(cols=['ShortCDStrike'])['ShortCDStrike']) | |
StrikeLst.sort() | |
df_call_last = pd.DataFrame() | |
df_put_last = pd.DataFrame() | |
for strike in StrikeLst: | |
shortcd = '201K2%s'%strike | |
df_tmp = df[df['ShortCD'] == shortcd][-1:] | |
df_tmp['Strike'] = convert_strike(strike) | |
if len(df_call_last) == 0: df_call_last = df_tmp | |
else: df_call_last = df_call_last.append(df_tmp) | |
print shortcd | |
shortcd = '301K2%s'%strike | |
df_tmp = df[df['ShortCD'] == shortcd][-1:] | |
df_tmp['Strike'] = convert_strike(strike) | |
if len(df_put_last) == 0: df_put_last = df_tmp | |
else: df_put_last = df_put_last.append(df_tmp) | |
print shortcd | |
df_syth = df_call_last.merge(df_put_last,left_on='Strike',right_on='Strike',how='outer') | |
df_syth['SythPrice'] = df_syth['LastPrice_x'].astype(float) - df_syth['LastPrice_y'].astype(float) + df_syth['Strike'].astype(float) | |
df_syth['Differ'] = abs(df_syth['LastPrice_x'].astype(float) - df_syth['LastPrice_y'].astype(float)) | |
df_syth = df_syth.sort('Differ') | |
df_syth['Differ'] = abs(df_syth['LastPrice_x'].astype(float) - df_syth['LastPrice_y'].astype(float)) | |
df_syth = df_syth.sort('Differ') | |
call_atm_price = df_syth.iloc[0]['LastPrice_x'] | |
put_atm_price = df_syth.iloc[0]['LastPrice_y'] | |
call_condition = (df_call_last['LastPrice'] <= call_atm_price) & (df_call_last['LastPrice'].astype(float) < 2.88) | |
put_condition = (df_put_last['LastPrice'] <= put_atm_price) & (df_put_last['LastPrice'].astype(float) < 2.88) | |
call_target_shortcd = df_call_last[call_condition].sort('LastPrice').iloc[-1]['ShortCD'] | |
put_target_shortcd = df_put_last[put_condition].sort('LastPrice').iloc[-1]['ShortCD'] | |
print call_target_shortcd, put_target_shortcd |
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Jul 01 11:28:32 2014 | |
@author: assa | |
""" | |
import pyxing as px | |
import pythoncom | |
class observer_cmd: | |
def Update(self,subject): | |
for i in xrange(len(subject.data)): | |
print subject.data[i] | |
print "==================================" | |
subject.flag = False | |
pass | |
class observer_order: | |
def Update(self,subject): | |
for i in subject.data.keys(): | |
try: | |
print i, subject.data[i] | |
except: | |
print i, 'error' | |
print "==================================" | |
subject.flag = False | |
pass | |
XASession = px.XASession() | |
obs = observer_cmd() | |
obs_order = observer_order() | |
server = 'demo.etrade.co.kr' | |
port = 20001 | |
servertype = 0 # demo server | |
showcerterror = 1 | |
user = 'eddy7777' | |
password = 'c9792458' | |
certpw = "" | |
XASession.observer = obs | |
XASession.ConnectServer(server,port) | |
#print 'connect server' | |
ret = XASession.Login(user,password,certpw,servertype,showcerterror) | |
XASession.flag = True | |
while XASession.flag: | |
pythoncom.PumpWaitingMessages() | |
accountlist = XASession.GetAccountList() | |
servername = XASession.GetServerName() | |
print servername, accountlist | |
NewQuery = px.XAQuery_t0441() | |
NewQuery.observer = obs | |
NewQuery.SetFieldData('t0441InBlock','accno',0,accountlist[0]) | |
NewQuery.SetFieldData('t0441InBlock','passwd',0,'0000') | |
ret = NewQuery.Request(False) | |
print ret | |
NewQuery.flag = True | |
while NewQuery.flag: | |
pythoncom.PumpWaitingMessages() | |
NewQuery1 = px.XAQuery_t0434() | |
NewQuery1.observer = obs | |
NewQuery1.SetFieldData('t0434InBlock','accno',0,accountlist[0]) | |
NewQuery1.SetFieldData('t0434InBlock','passwd',0,'0000') | |
NewQuery1.SetFieldData('t0434InBlock','chegb',0,'0') | |
NewQuery1.SetFieldData('t0434InBlock','sortgb',0,'1') | |
NewQuery1.SetFieldData('t0434InBlock','cts_ordno',0,' ') | |
ret = NewQuery1.Request(False) | |
print ret | |
NewQuery1.flag = True | |
while NewQuery1.flag: | |
pythoncom.PumpWaitingMessages() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment