資源簡介
Python+pywinauto操作同花順客戶端自動(dòng)化執(zhí)行測試 快速下單 代碼為全代碼 可以選擇性摘抄。
代碼片段和文件信息
import?tkinter.messageboxos
from?tkinter?import?*
from?tkinter.ttk?import?*
from?tkinter?import?Menu
import?datetime
import?threading
import?pickle
import?time
#import?tushare?as?ts
import?pywinauto
import?pywinauto.clipboard
import?pywinauto.application
NUM_OF_STOCKS?=?5??#?自定義股票數(shù)量
is_start?=?False
is_monitor?=?True
set_stocks_info?=?[]
actual_stocks_info?=?[]
consignation_info?=?[]
is_ordered?=?[1]?*?NUM_OF_STOCKS??#?1:未下單??0:已下單
is_dealt?=?[0]?*?NUM_OF_STOCKS??#?0:?未成交???負(fù)整數(shù):賣出數(shù)量,?正整數(shù):買入數(shù)量
stock_codes?=?[‘‘]?*?NUM_OF_STOCKS
class?OperationThs:
????def?__init__(self):
????????try:
????????????self.__app?=?pywinauto.application.Application()
????????????self.__app.connect(title=‘網(wǎng)上股票交易系統(tǒng)5.0‘)
????????????top_hwnd?=?pywinauto.findwindows.find_window(title=‘網(wǎng)上股票交易系統(tǒng)5.0‘)
????????????dialog_hwnd?=?pywinauto.findwindows.find_windows(top_level_only=False?class_name=‘#32770‘?parent=top_hwnd)[0]
????????????wanted_hwnds?=?pywinauto.findwindows.find_windows(top_level_only=False?parent=dialog_hwnd)
????????????print(‘wanted_hwnds?length‘?len(wanted_hwnds))
????????????if?len(wanted_hwnds)==0:
????????????????tkinter.messagebox.showerror(‘錯(cuò)誤‘?‘無法獲得“同花順雙向委托界面”的窗口句柄請將同花順交易系統(tǒng)切換到“雙向委托界面”!‘)
????????????????exit()
????????????self.__main_window?=?self.__app.window_(handle=top_hwnd)
????????????self.__dialog_window?=?self.__app.window_(handle=dialog_hwnd)
????????except:
????????????pass
????def?__buy(self?code?quantity):
????????“““買函數(shù)
????????:param?code:?代碼,?字符串
????????:param?quantity:?數(shù)量,?字符串
????????“““
????????self.__dialog_window.Edit1.SetFocus()
????????time.sleep(0.2)
????????self.__dialog_window.Edit1.SetEditText(code)
????????time.sleep(0.2)
????????if?quantity?!=?‘0‘:
????????????self.__dialog_window.Edit3.SetEditText(quantity)
????????????time.sleep(0.2)
????????self.__dialog_window.Button1.Click()
????????time.sleep(0.2)
????def?__sell(self?code?quantity):
????????“““
????????賣函數(shù)
????????:param?code:?股票代碼,?字符串
????????:param?quantity:?數(shù)量,?字符串
????????“““
????????self.__dialog_window.Edit4.SetFocus()
????????time.sleep(0.2)
????????self.__dialog_window.Edit4.SetEditText(code)
????????time.sleep(0.2)
????????if?quantity?!=?‘0‘:
????????????self.__dialog_window.Edit6.SetEditText(quantity)
????????????time.sleep(0.2)
????????self.__dialog_window.Button2.Click()
????????time.sleep(0.2)
????def?__closePopupWindow(self):
????????“““
????????關(guān)閉一個(gè)彈窗。
????????:return:?如果有彈出式對話框,返回True,否則返回False
????????“““
????????popup_hwnd?=?self.__main_window.PopupWindow()
????????if?popup_hwnd:
????????????popup_window?=?self.__app.window_(handle=popup_hwnd)
????????????popup_window.SetFocus()
????????????popup_window.Button.Click()
????????????return?True
????????return?False
????def?__closePopupWindows(self):
????????“““
????????關(guān)閉多個(gè)彈出窗口
????????:return:
????????“““
????????while?self.__closePopupWindow():
????????????time.sleep(0.5)
????def?order(self?code?direc
評論
共有 條評論