我们在自己设计期货交易接口时,少不了要用到一些代码,今日我们就来介绍几个比较常见的代码,希望能对各位投资者有帮助。
众所周知,虽然期货交易是证券交易的其中一种方式,但当中有很多交易模式和术语跟股票交易是很不一样的,正因如此,期货交易接口的编程也会跟股票交易接口不一样,下面就先来给大家介绍几个期货交易过程中比较常用的几组代码:
'''
进行期货阶数套保数据识别
'''
#点击合约资料
pyautogui.click(x=969,y=51)
#信息截图
time.sleep(1)
#点击结算套保
pyautogui.click(x=1343,y=151)
time.sleep(1)
pyautogui.screenshot(r'C:\Users\Administrator\Desktop\期货交易\结算套保.png',region=(553,293,1671-553,639-293))
options={'language':'chn_eng'}
aipcor=AipOcr(app_id,api_key,secret_key)
image=open(r'C:\Users\Administrator\Desktop\期货交易\结算套保.png','rb')
image1=image.read()
text_list=aipcor.general(image1,options=options)
df1=pd.json_normalize(text_list['words_result'])
df1.to_excel(r'C:\Users\Administrator\Desktop\期货交易\结算套保.xlsx')
df=pd.read_excel(r'C:\Users\Administrator\Desktop\期货交易\结算套保.xlsx')
#将提取的数据处理成表格数据
df_words=df['words'][10:]
date=[]
price=[]
buy_ratio_tj=[]
buy_ratio_trader=[]
sell_ratio_tj=[]
sell_ratio_trader=[]
for i in range(0,len(df_words.tolist()),6):
date.append(df_words.tolist()[i])
price.append(df_words.tolist()[i+1])
buy_ratio_tj.append(df_words.tolist()[i+2])
buy_ratio_trader.append(df_words.tolist()[i+3])
sell_ratio_tj.append(df_words.tolist()[i+4])
sell_ratio_trader.append(df_words.tolist()[i+5])
now_df=pd.DataFrame({'时间':date,'结算价格':price,'买入投机比例':buy_ratio_tj,'买入交易比例':buy_ratio_trader,
'卖出投资比例':sell_ratio_tj,'卖出交易比例':sell_ratio_trader})
print(now_df)
now_df.to_excel(r'C:\Users\Administrator\Desktop\期货交易\结算套保.xlsx')
pyttsx3.speak('期货结算套保数据提取成功')
#识别升贴书数据
def up_down_data():
'''
进行期货升贴水数据识别,处理
'''
#点击合约资料
pyautogui.click(x=969,y=51)
#信息截图
time.sleep(1)
#点击升贴书
pyautogui.click(x=1053,y=149)
#截图
pyautogui.screenshot(r'C:\Users\Administrator\Desktop\期货交易\升贴水.png',region=(553,771,1672-553,954-771))
options={'language':'chn_eng'}
aipcor=AipOcr(app_id,api_key,secret_key)
image=open(r'C:\Users\Administrator\Desktop\期货交易\升贴水.png','rb')
image1=image.read()
text_list=aipcor.general(image1,options=options)
df1=pd.json_normalize(text_list['words_result'])
df1.to_excel(r'C:\Users\Administrator\Desktop\期货交易\升贴水.xlsx')
#将识别的数据处理excel数据
df=pd.read_excel(r'C:\Users\Administrator\Desktop\期货交易\升贴水.xlsx')
df_words=df['words']
data=[]
#现货价格
new_price=[]
#合约代码
code=[]
#合约价格
price=[]
new_cha=[]
new_ratio=[]
#主力代码
stock=[]
main_price=[]
mian_new_cha=[]
mian_new_ratio=[]
for i in range(0,len(df_words.tolist()),10):
data.append(df_words.tolist()[i])
new_price.append(df_words.tolist()[i+1])
code.append(df_words.tolist()[i+2])
price.append(df_words.tolist()[i+3])
new_cha.append(df_words.tolist()[i+4])
new_ratio.append(df_words.tolist()[i+5])
stock.append(df_words.tolist()[i+6])
main_price.append(df_words.tolist()[i+7])
mian_new_cha.append(df_words.tolist()[i+8])
mian_new_ratio.append(df_words.tolist()[i+9])
now_df=pd.DataFrame({'时间':data,'现货价格':new_price,'合约代码':code,'合约价格':price,'期货现差':new_cha,
'期货现差比例':new_ratio,'主力合约代码':stock,'主力合约价格':main_price,'主力合约现差':mian_new_cha,'主力合约现差比例':mian_new_ratio})
print(now_df)
now_df.to_excel(r'C:\Users\Administrator\Desktop\期货交易\升贴水.xlsx')
pyttsx3.speak('期货升贴水数据处理成功')
#实时期货数据获取,识别
def real_account_data():
'''
对账户进行实时数据识别获取
'''
#返回交易界面
#return_trader()
#点击交易资金
pyautogui.click(x=732,y=520)
time.sleep(1)
#截图分析
pyautogui.screenshot(r'C:\Users\Administrator\Desktop\期货交易\实时期货账户数据.png',region=(429,543,1092-429,895-543))
options={'language':'chn_eng'}
aipcor=AipOcr(app_id,api_key,secret_key)
image=open(r'C:\Users\Administrator\Desktop\期货交易\实时期货账户数据.png','rb')
image1=image.read()
text_list=aipcor.general(image1,options=options)
df1=pd.json_normalize(text_list['words_result'])
df1.to_excel(r'C:\Users\Administrator\Desktop\期货交易\实时期货账户数据.xlsx')
df=pd.read_excel(r'C:\Users\Administrator\Desktop\期货交易\实时期货账户数据.xlsx')
df_words=df['words']
#列
clo=[]
#数据
data=[]
for i in range(0,len(df_words.tolist())):
if i%2==0:
clo.append(df_words.tolist()[i])
else:
data.append(df_words.tolist()[i])
del clo[-1]
new_df=pd.DataFrame(data[:20],clo)
new_df.to_excel(r'C:\Users\Administrator\Desktop\期货交易\实时期货账户数据.xlsx')
#期货锁仓,进行反手操作,及卖出多头,同时买进空头
def featurns_buy_lock(stock='ni2205',num='1'):
总的来说,期货交易接口虽然可以帮助我们更好地进行期货量化交易,但并不代码有了这套工具我们就一定能赚钱,很多时候,工具再好,人的思维不转变,也是很难有好多成果的。
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点