用python写一个模拟键盘操作同花顺下单
时间: 2023-07-18 10:31:09 浏览: 145
好的,下面是一个基于Python的模拟键盘操作同花顺下单的示例代码:
```python
import time
import win32api
import win32con
# 设置下单参数
stock_code = "000001" # 股票代码
trade_type = "1" # 1代表买入,2代表卖出
price = "10.00" # 下单价格
amount = "100" # 下单数量
# 打开同花顺并登录
win32api.ShellExecute(0, "open", "C:\\同花顺\\xiadan.exe", "", "", 1)
time.sleep(10) # 等待程序启动
# 模拟键盘操作登录
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x0D, 0, 0, 0) # 回车键
win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(5) # 等待登录
# 模拟键盘操作下单
win32api.keybd_event(0x10, 0, 0, 0) # Shift键
win32api.keybd_event(0x51, 0, 0, 0) # Q键
win32api.keybd_event(0x51, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x10, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(2) # 等待买入页面加载完成
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x0D, 0, 0, 0) # 回车键
win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(2) # 等待代码输入完成
for c in stock_code:
win32api.keybd_event(ord(c), 0, 0, 0) # 输入股票代码
win32api.keybd_event(ord(c), 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.1)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.5) # 等待交易类型选择框出现
if trade_type == "1":
win32api.keybd_event(0x57, 0, 0, 0) # W键,选择买入
elif trade_type == "2":
win32api.keybd_event(0x53, 0, 0, 0) # S键,选择卖出
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.5) # 等待价格输入框出现
for c in price:
win32api.keybd_event(ord(c), 0, 0, 0) # 输入价格
win32api.keybd_event(ord(c), 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.1)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.5) # 等待数量输入框出现
for c in amount:
win32api.keybd_event(ord(c), 0, 0, 0) # 输入数量
win32api.keybd_event(ord(c), 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.1)
win32api.keybd_event(0x0D, 0, 0, 0) # 回车键,提交订单
win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(5) # 等待订单提交完成
# 模拟键盘操作撤单
win32api.keybd_event(0x10, 0, 0, 0) # Shift键
win32api.keybd_event(0x57, 0, 0, 0) # W键
win32api.keybd_event(0x57, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x10, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(2) # 等待委托页面加载完成
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x09, 0, 0, 0) # Tab键
win32api.keybd_event(0x09, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x0D, 0, 0, 0) # 回车键,选择撤单
win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(2) # 等待撤单页面加载完成
win32api.keybd_event(0x0D, 0, 0, 0) # 回车键,提交撤单
win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(2) # 等待撤单结果
# 关闭同花顺程序
win32api.PostMessage(win32api.FindWindow(None, "同花顺"), win32con.WM_CLOSE, 0, 0)
```
需要注意的是,该示例代码仅供参考,实际使用时需根据自身需求进行修改。同时,为了避免因程序错误导致实际交易损失,请务必谨慎测试和使用。
阅读全文