Exemplo n.º 1
0
def run(engine: ScriptEngine):
    """
    脚本策略的主函数说明:
    1. 唯一入参是脚本引擎ScriptEngine对象,通用它来完成查询和请求操作
    2. 该函数会通过一个独立的线程来启动运行,区别于其他策略模块的事件驱动
    3. while循环的维护,请通过engine.strategy_active状态来判断,实现可控退出

    脚本策略的应用举例:
    1. 自定义篮子委托执行执行算法
    2. 股指期货和一篮子股票之间的对冲策略
    3. 国内外商品、数字货币跨交易所的套利
    4. 自定义组合指数行情监控以及消息通知
    5. 股票市场扫描选股类交易策略(龙一、龙二)
    6. 等等~~~
    """
    vt_symbols = ["IF1912.CFFEX", "rb2001.SHFE"]

    # 订阅行情
    engine.subscribe(vt_symbols)

    # 获取合约信息
    for vt_symbol in vt_symbols:
        contract = engine.get_contract(vt_symbol)
        msg = f"合约信息,{contract}"
        engine.write_log(msg)

    # 持续运行,使用strategy_active来判断是否要退出程序
    print(engine.strategy_active)
    while engine.strategy_active:
        # 轮询获取行情
        for vt_symbol in vt_symbols:
            tick = engine.get_tick(vt_symbol)
            msg = f"最新行情, {tick}"
            engine.write_log(msg)

        # 等待3秒进入下一轮
        sleep(3)
Exemplo n.º 2
0
def run(engine: ScriptEngine):
    """"""
    vt_symbols = ['btcusdt.HUOBI', 'ethusdt.HUOBI']

    # 订阅行情
    engine.subscribe(vt_symbols)

    # 获取合约信息
    for vt_symbol in vt_symbols:
        contract = engine.get_contract(vt_symbol)
        msg = f"合约信息,{contract}"
        engine.write_log(msg)

    # 持续运行,使用strategy_active来判断是否要退出程序
    engine.strategy_active = True
    while engine.strategy_active:
        # 轮询获取行情
        for vt_symbol in vt_symbols:
            tick = engine.get_tick(vt_symbol)
            msg = f"最新行情, {tick}"
            engine.write_log(msg)

        # 等待3秒进入下一轮
        time.sleep(3)
Exemplo n.º 3
0
def run(engine: ScriptEngine):
    """
     the main function of the script strategy explained :
    1.  the only parameter is the script engine ScriptEngine objects , general inquiries and requests it to complete the operation 
    2.  this function will start to run through a separate thread , event policy module is different from other drivers 
    3. while cycle maintenance , please engine.strategy_active state is determined , for controlled exit 

     for example application scripting policy :
    1.  custom baskets entrust execution algorithm 
    2.  hedging strategies between index futures and a basket of stocks 
    3.  domestic and foreign goods 、 cross-exchange digital currency arbitrage 
    4.  combination index custom monitoring, and message notification quote 
    5.  stock market trading strategies scanning picking class ( ryuichi 、 ryuji )
    6.  and many more ~~~
    """
    vt_symbols = ["IF1912.CFFEX", "rb2001.SHFE"]

    #  subscribe quotes
    engine.subscribe(vt_symbols)

    #  get contract information
    for vt_symbol in vt_symbols:
        contract = engine.get_contract(vt_symbol)
        msg = f" contract information ,{contract}"
        engine.write_log(msg)

    #  continuous operation , use strategy_active to determine whether to exit the program
    while engine.strategy_active:
        #  polling get quotes
        for vt_symbol in vt_symbols:
            tick = engine.get_tick(vt_symbol)
            msg = f" the latest market , {tick}"
            engine.write_log(msg)

        #  wait 3 seconds into the next round
        sleep(3)
Exemplo n.º 4
0
def run(engine: ScriptEngine):
    """
    脚本策略的主函数说明:
    1. 唯一入参是脚本引擎ScriptEngine对象,通用它来完成查询和请求操作
    2. 该函数会通过一个独立的线程来启动运行,区别于其他策略模块的事件驱动
    3. while循环的维护,请通过engine.strategy_active状态来判断,实现可控退出

    脚本策略的应用举例:
    1. 自定义篮子委托执行执行算法
    2. 股指期货和一篮子股票之间的对冲策略
    3. 国内外商品、数字货币跨交易所的套利
    4. 自定义组合指数行情监控以及消息通知
    5. 股票市场扫描选股类交易策略(龙一、龙二)
    6. 等等~~~
    """
    # 持续运行,使用strategy_active来判断是否要退出程序
    while engine.strategy_active:
        # 轮询获取行情

        current_contract = engine.get_current_contract()
        if not current_contract:
            print("没有合约信息,请连接CTP!")
            break

        current_tick = engine.get_tick(current_contract.symbol)
        tick_list = engine.get_current_tick_list()
        if current_tick:
            if tick_list:
                top_tick = tick_list[len(tick_list) - 1]
                msg = f"最新行情, {engine.get_new_tick_flag()}"
                engine.write_log(msg)
            else:
                msg = f"最新行情, {top_tick}"
                engine.write_log(msg)
        else:
            msg = f"最新行情, {current_tick}"
            engine.write_log(msg)
            break
        # 等待3秒进入下一轮
        sleep(0.001)