老帅李华杰
管理员
管理员
  • UID2
  • 粉丝33
  • 关注0
  • 发帖数402
  • 社区居民
  • 忠实会员
  • 喜欢达人
  • 原创写手
阅读:148回复:1

python量化预警

楼主#
更多 发布于:2025-10-21 23:30
#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)
好的指标等于至高的阵地,明察秋毫自然马到成功; 微信手机同号:15907742318 老帅李华杰
老帅李华杰
管理员
管理员
  • UID2
  • 粉丝33
  • 关注0
  • 发帖数402
  • 社区居民
  • 忠实会员
  • 喜欢达人
  • 原创写手
沙发#
发布于: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编写
好的指标等于至高的阵地,明察秋毫自然马到成功; 微信手机同号:15907742318 老帅李华杰
游客

返回顶部