量化交易源代码分析二:
#将提取的数据处理成表格数据
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:UsersAdministratorDesktop期货交易结算套保.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:UsersAdministratorDesktop期货交易升贴水.png',region=(553,771,1672-553,954-771))
options={'language':'chn_eng'}
aipcor=AipOcr(app_id,api_key,secret_key)
image=open(r'C:UsersAdministratorDesktop期货交易升贴水.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:UsersAdministratorDesktop期货交易升贴水.xlsx')
#将识别的数据处理excel数据
df=pd.read_excel(r'C:UsersAdministratorDesktop期货交易升贴水.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:UsersAdministratorDesktop期货交易升贴水.xlsx')
pyttsx3.speak('期货升贴水数据处理成功')
#实时期货数据获取,识别
def real_account_data():
'''
利用百度对账户进行实时数据识别获取
'''
#返回交易界面
#return_trader()
#点击交易资金
pyautogui.click(x=732,y=520)
time.sleep(1)
#截图分析
pyautogui.screenshot(r'C:UsersAdministratorDesktop期货交易实时期货账户数据.png',region=(429,543,1092-429,895-543))
options={'language':'chn_eng'}
aipcor=AipOcr(app_id,api_key,secret_key)
image=open(r'C:UsersAdministratorDesktop期货交易实时期货账户数据.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:UsersAdministratorDesktop期货交易实时期货账户数据.xlsx')
df=pd.read_excel(r'C:UsersAdministratorDesktop期货交易实时期货账户数据.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:UsersAdministratorDesktop期货交易实时期货账户数据.xlsx')
#期货锁仓,进行反手操作,及卖出多头,同时买进空头
def featurns_buy_lock(stock='ni2205',num='1'):
'''
看多锁仓,期货锁仓,进行反手操作,及卖出多头,同时买进空头
看跌同理
stock='期货代码'
num=买入的手数
'''
#点击输入
pyttsx3.speak('期货多头开始锁仓')
pyautogui.click(x=320,y=649)
#清楚数据
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清除完成')
pyautogui.typewrite(stock,interval=0.1)
pyttsx3.speak('开始输入')
pyautogui.press('enter')
pyttsx3.speak('输入完成')
#输入买入手数
#开始清楚
pyautogui.click(x=470,y=641)
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清除完成')
pyttsx3.speak('开始输入')
pyautogui.typewrite(num,interval=0.1)
pyautogui.press('enter')
pyttsx3.speak('输入完成')
time.sleep(1)
pyautogui.click(x=651,y=304)
#点击锁仓
pyautogui.click(x=463,y=669)
featurns_trader_stats()
#卖出期货多头
def featurns_buy_sell(stock='cu2205',num='1'):
'''
卖出期货多头
stock='期货代码'
num=买入的手数
'''
#点击输入
pyttsx3.speak('开始平多')
pyautogui.click(x=320,y=649)
#清楚数据
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清楚完成')
pyttsx3.speak('开始输入')
pyautogui.typewrite(stock,interval=0.1)
pyautogui.press('enter')
pyttsx3.speak('输入完成')
#输入买入手数
#开始清楚
pyautogui.click(x=470,y=641)
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清除完成')
pyautogui.typewrite(num,interval=0.1)
pyttsx3.speak('开始输入')
pyautogui.press('enter')
pyttsx3.speak('输入完成')
time.sleep(1)
#点击平多
pyautogui.click(x=598,y=723)
featurns_trader_stats()
#空头交易
def featurns_sell_buy_add_featuns(stock='ni2205',num='1'):
'''
期货空头增加多头,及对冲
stock='期货代码'
num=买入的手数
'''
#点击输入
pyttsx3.speak('期货空头开始加多建仓')
pyautogui.click(x=320,y=649)
#清楚数据
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清楚完成')
pyttsx3.speak('开始输入')
pyautogui.typewrite(stock,interval=0.1)
pyautogui.press('enter')
pyttsx3.speak('输入完成')
#输入买入手数
#开始清楚
pyautogui.click(x=470,y=641)
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清除完成')
pyautogui.typewrite(num,interval=0.1)
pyttsx3.speak('开始输入')
pyautogui.press('enter')
pyttsx3.speak('输入完成')
time.sleep(1)
#点击买入/加多
pyautogui.click(x=325,y=729)
featurns_trader_stats()
#期货空头继续加空
def featurns_sell_add_sell_futurns(stock='ni2205',num='1'):
'''
期货空头加多
stock='期货代码'
num=买入的手数
'''
#点击输入
pyttsx3.speak('期货空头开始加多')
pyautogui.click(x=320,y=649)
#清楚数据
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清除完成')
pyautogui.typewrite(stock,interval=0.1)
pyttsx3.speak('开始输入')
pyautogui.press('enter')
pyttsx3.speak('输入完成')
#输入买入手数
#开始清楚
pyautogui.click(x=470,y=641)
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清除完成')
pyttsx3.speak('开始输入')
pyautogui.typewrite(num,interval=0.1)
pyautogui.press('enter')
pyttsx3.speak('输入完成')
time.sleep(1)
pyautogui.click(x=651,y=304)
#点击锁仓
pyautogui.click(x=463,y=669)
featurns_trader_stats()
#期货空头平空
def featurns_sell_sell_featurns(stock='ni2205',num='1'):
'''
期货空头平空,卖出期货空头
stock='期货代码'
num=买入的手数
'''
#点击输入
pyttsx3.speak('开始平空')
pyautogui.click(x=320,y=649)
#清楚数据
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清楚完成')
pyttsx3.speak('开始输入')
pyautogui.typewrite(stock,interval=0.1)
pyautogui.press('enter')
pyttsx3.speak('输入完成')
#输入买入手数
#开始清楚
pyautogui.click(x=470,y=641)
pyttsx3.speak('开始清除')
for i in range(6):
pyautogui.press('backspace')
pyttsx3.speak('清除完成')
pyautogui.typewrite(num,interval=0.1)
pyttsx3.speak('开始输入')
pyautogui.press('enter')
pyttsx3.speak('输入完成')
time.sleep(1)
#点击平多
pyautogui.click(x=598,y=723)
featurns_trader_stats()
#期货多头和期货多头进行比例平仓
def featurns_buy_and_sell_ratio(stock='ni2205',num='1',ratio='30'):
'''
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69993319/viewspace-2897905/ ,如需转载,请注明出处,否则将追究法律责任。