量化交易,散户也可实现全自动化交易,减少情绪干扰——量化02
读取数据库的交易数据
import send_email as se
import time
import pandas as pd
import numpy as np
from selenium import webdriver
from selenium.webdriver import Edge
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from PIL import Image
from chaojiying import Chaojiying_Client
def 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.18.cn/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(东财账号, 东财密码)
需人修改的内容
- 数据库的路径和数据表的名称
- 交易金额,用于计算交易数据
- 超级修改的账号密码和软件号,用于验证码的识别
- 东财的账号和密码
读取 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) # 读取行数
- 一键交易,财富自由!——利用Python自动化交易实现轻松理财
- 数据驱动的投资策略——Python实现基金交易数据分析
- 投资小白必备——Python实现东财自动化交易
- 轻松掌握股市节奏——Python自动化交易技术详解
- 码农也能成为投资高手!——利用Python打造智能交易系统
#金融 #数据分析 #自动化交易 #基金投资 #股票市场 #python量化
版权声明:
作者:余汉波
链接:https://www.sanrenjz.com/2023/06/25/%e9%87%8f%e5%8c%96%e4%ba%a4%e6%98%93%ef%bc%8c%e6%95%a3%e6%88%b7%e4%b9%9f%e5%8f%af%e5%ae%9e%e7%8e%b0%e5%85%a8%e8%87%aa%e5%8a%a8%e5%8c%96%e4%ba%a4%e6%98%93%ef%bc%8c%e5%87%8f%e5%b0%91%e6%83%85%e7%bb%aa/
文章版权归作者所有,未经允许请勿转载。
THE END