分享

量化交易,散户也可实现全自动化交易,减少情绪干扰——量化02

 禁忌石 2023-08-11 发布于浙江

import send_email as seimport timeimport pandas as pdimport numpy as npfrom selenium import webdriverfrom selenium.webdriver import Edgefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.chrome.options import Optionsfrom PIL import Imagefrom chaojiying import Chaojiying_Clientdef login_and_trade(account, password): def main_func(BS, i): try: web.find_element(By.XPATH, BS).click() # 引入买卖 time.sleep(0.2) web.find_element(By.ID, 'btnReset').click() # 点击清空数据 time.sleep(0.2) web.find_element(By.ID, 'stockCode').send_keys(frame.loc[i, '代码']) # 输入代码 time.sleep(0.5) # web.find_element(By.ID, 'iptPrice').click() # 点击价格 # time.sleep(0.2) # web.find_element(By.ID, 'iptPrice').clear() # 清空默认输入的现价 # time.sleep(0.2) # web.find_element(By.ID, 'iptPrice').send_keys(frame.loc[i, '价格']) # 输入价格 # time.sleep(0.5) web.find_element(By.ID, 'iptCount').click() # 点击数量 time.sleep(0.5) web.find_element(By.ID, 'iptCount').clear() # 清空默认输入的数量 time.sleep(0.2) web.find_element(By.ID, 'iptCount').send_keys(frame.loc[i, '数量']) # 输入数量 time.sleep(0.2) web.find_element(By.ID, 'btnConfirm').click() # 点击操作 time.sleep(0.2) web.find_element(By.XPATH, '/html/body/table/tbody/tr[2]/td[2]/div/div[1]/div[2]/a[1]').click() # 确认交易 time.sleep(0.2) web.find_element(By.ID, 'btnCxcConfirm').click() # 提醒返回 time.sleep(0.2) except Exception as e: print(f'在循环 {i} 中发生错误: {str(e)}') return frame = se.get_recent_data('fund_grid_trading') # 读取最新获得的场内基金交易数据fund_grid_trading if frame.empty: print('没有可用数据。跳过登录和交易流程。') return # 计算数量并添加到数据框 frame['数量'] = frame['价格'].apply(lambda x: (np.ceil(交量金额 / x / 100)*10)) length = len(frame) # 读取行数 # 创建 ChromeOptions 对象并设置无头模式 options = Options() options.add_argument('--headless') # 启用无头模式 # 创建 ChromeDriver 对象,并将 ChromeOptions 传递给它 web = Edge(options=options) web.get('https://jywg./Trade/Buy') # 打开东财 web.find_element(By.ID, 'txtZjzh').send_keys(account) # 利用id获取输入框,利用send_keys输入东财账号 web.find_element(By.ID, 'txtPwd').send_keys(password) # 利用id获取输入框,利用send_keys输入东财密码 res = web.find_element(By.ID, 'imgValidCode') # 获取验证码图片的元素 png_path = 'login.png' # 保存文件路径 with open(png_path, 'wb') as file: file.write(res.screenshot_as_png) # 保存图片 cjy = Chaojiying_Client('超级鹰账号', '超级鹰密码', '软件号') # 用户中心>>软件ID 生成一个替换 96001 im = open('login.png', 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要// res = cjy.PostPic(im, 1902) # 1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加() data = res['pic_str'] web.find_element(By.ID, 'txtValidCode').send_keys(data) # 填入验证码 # web.find_element(By.ID, 'rdsc45').click() # 点击在线时间 web.find_element(By.ID, 'btnConfirm').click() # 点击登录 time.sleep(0.5) Buy = '//*[@id='main']/div/div[2]/div[1]/ul/li[2]/a' Sell = '//*[@id='main']/div/div[2]/div[1]/ul/li[3]/a' # 判断交易 for i in range(length): if frame.loc[i, '买卖'] == '1': # B对应值为1 main_func(Buy, i) elif frame.loc[i, '买卖'] == '-1': # S对应值为-1 main_func(Sell, i) else: print() time.sleep(3) web.quit()if __name__ == '__main__': login_and_trade(东财账号, 东财密码)
量化交易,散户也可实现全自动化交易,减少情绪干扰——量化02

需人修改的内容

  1. 数据库的路径和数据表的名称
  2. 交易金额,用于计算交易数据
  3. 超级修改的账号密码和软件号,用于验证码的识别
  4. 东财的账号和密码

读取 excel 交易数据

如果读取的是 excel 交易数据的话,那将 frame 进行相应的修改,然后将对函数 main_func(BS, i)进行相应的修改,可以参考以前的关于这方面的视频或文章

frame = pd.read_excel(im.path,sheet_name='东财交易') # 读取excel表frame = np.array(frame,dtype=str)   # 将数据转化为numpy,利于数据处理length = len(frame)     # 读取行数

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多