|
阅读:148回复:1
python量化预警
#encoding:gbk
import time import os class a(): pass g = a() g.position = 0 def init(C): # 初始化股票、账户信息 C.accid = '410038' C.run_time("fu", '1nSecond', "2024-11-10 9:20:00") subscribe = True def fu(C): def read_new_lines(file_path): action = 'buy' if os.path.exists(file_path): with open(file_path, 'r', encoding='gbk') as file: file.seek(g.position) lines = file.readlines() if lines: new_lines = lines # 直接获取所有新行 g.position = file.tell() # 更新位置指针到文件末尾 for line in new_lines: stock_code = line.split('\t')[0] #print(stock_code) if stock_code.startswith(('83', '87', '88')): stock_code = f"{stock_code}.bj" # 北交所 #stock_code=f"'{stock_code}'" elif stock_code.startswith(('6', '3')): stock_code = f"{stock_code}.sh" # 上交所 #stock_code=f"'{stock_code}'" elif stock_code.startswith(('0', '2')): stock_code = f"{stock_code}.sz" # 深交所 #stock_code=f"'{stock_code}'" else: stock_code = "未知交易所" #print(type(stock_code)) #print(stock_code) if action == 'buy': passorder(23, 1101, C.accid, stock_code, 4, 0, 200, '测试', 2, '备注', C) print(stock_code,':买入') elif action == 'sell': passorder(24, 1101, C.accid, stock_code, 6, 0, 200, '测试', 2, '备注', C) print(stock_code,':卖出') time.sleep(1) else: time.sleep(1) print("无新信号产出") else: time.sleep(1) print("预警暂未开启") file_path = 'D:\\tondaxin\\yujing\\yujin.txt' # 指定信号文件路径 while True: read_new_lines(file_path) |
|
|
|
沙发#
发布于:2025-10-21 23:33
#encoding:gbk
import time import os import logging # 配置日志 logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', filename='qmt_trade.log' ) class GlobalVars(): def __init__(self): self.position = 0 g = GlobalVars() def init(C): # 初始化配置 C.accid = '4100' # 账户ID C.trade_quantity = 200 # 交易数量 C.file_path = './yujin.txt' # 预警文件路径 C.run_time("fu", '1nSecond', "2024-11-10 9:20:00") def fu(C): def read_new_lines(): try: if not os.path.exists(C.file_path): logging.warning("预警文件不存在") time.sleep(1) return with open(C.file_path, 'r', encoding='gbk') as file: file.seek(g.position) lines = file.readlines() if not lines: time.sleep(1) return g.position = file.tell() for line in lines: line = line.strip() if not line: continue try: stock_code = line.split('\t')[0] action = 'buy' # 默认买入,可根据需要修改 # 处理股票代码 if stock_code.startswith(('83', '87', '88')): stock_code = f"{stock_code}.bj" elif stock_code.startswith(('6', '3')): stock_code = f"{stock_code}.sh" elif stock_code.startswith(('0', '2')): stock_code = f"{stock_code}.sz" else: logging.warning(f"未知交易所代码: {stock_code}") continue # 执行交易 if action == 'buy': passorder(23, 1101, C.accid, stock_code, 4, 0, C.trade_quantity, '系统买入', 2, '', C) logging.info(f"{stock_code}: 买入{C.trade_quantity}股") elif action == 'sell': passorder(24, 1101, C.accid, stock_code, 6, 0, C.trade_quantity, '系统卖出', 2, '', C) logging.info(f"{stock_code}: 卖出{C.trade_quantity}股") time.sleep(1) except Exception as e: logging.error(f"处理交易时出错: {e}") continue except Exception as e: logging.error(f"读取文件时出错: {e}") while True: read_new_lines() time.sleep(1) AI编写 |
|
|