コード例 #1
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)
コード例 #2
0
ファイル: demo_script.py プロジェクト: githmy/vnpymy
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)
コード例 #3
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)
コード例 #4
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)
コード例 #5
0
def run(engine: ScriptEngine):
    """
    """
    # 设置参数
    leg1_symbol = "TF2006.CFFEX"
    leg2_symbol = "T2006.CFFEX"
    entry_level = 5  # 入场位置
    tick_add = 1  # 买卖超价Tick
    trading_size = 100000  # 交易数量

    vt_symbols = [leg1_symbol, leg2_symbol]

    # 订阅行情
    engine.subscribe(vt_symbols)

    # 初始化变量
    pos_data = {}
    target_data = {}

    for vt_symbol in vt_symbols:
        pos_data[vt_symbol] = 0
        target_data[vt_symbol] = 0

    # 持续运行,使用strategy_active来判断是否要退出程序
    while engine.strategy_active:
        # 获取行情
        leg1_tick = engine.get_tick(leg1_symbol)
        leg2_tick = engine.get_tick(leg2_symbol)

        # 计算交易目标
        # 开仓
        if not target_data[leg1_symbol]:
            if leg1_tick.bid_price_1 >= leg2_tick.ask_price_1 + entry_level:
                print(f"满足开仓条件,卖{leg1_symbol},买{leg2_symbol}")
                target_data[leg1_symbol] = -trading_size
                target_data[leg2_symbol] = trading_size
            elif leg1_tick.ask_price_1 <= leg2_tick.bid_price_1 - entry_level:
                print(f"满足开仓条件,买{leg1_symbol},卖{leg2_symbol}")
                target_data[leg1_symbol] = trading_size
                target_data[leg2_symbol] = -trading_size
        # 平仓
        else:
            if target_data[leg1_symbol] > 0:
                if leg1_tick.ask_price_1 <= leg2_tick.bid_price_1:
                    print("满足平仓条件")
                    target_data[leg1_symbol] = 0
                    target_data[leg2_symbol] = 0
            else:
                if leg1_tick.bid_price_1 >= leg2_tick.ask_price_1:
                    print("满足平仓条件")
                    target_data[leg1_symbol] = 0
                    target_data[leg2_symbol] = 0

        # 检查委托情况
        active_orders = engine.get_all_active_orders()
        if active_orders:
            print("当前存在活动委托,执行撤单")
            for order in active_orders:
                engine.cancel_order(order.vt_orderid)

            continue

        # 执行交易
        for vt_symbol in vt_symbols:
            pos = pos_data[vt_symbol]
            target = target_data[vt_symbol]
            diff = target - pos

            contract = engine.get_contract(vt_symbol)
            price_add = tick_add * contract.pricetick

            tick = engine.get_tick(vt_symbol)

            # 持仓和目标一致,无需交易
            if not diff:
                continue
            # 大于则买入
            elif diff > 0:
                # 有空头持仓,买入平仓
                if pos < 0:
                    engine.cover(vt_symbol, tick.ask_price_1 + price_add,
                                 abs(diff))
                    print(f"cover {vt_symbol}")
                # 否则买入开仓
                else:
                    engine.buy(vt_symbol, tick.ask_price_1 + price_add,
                               abs(diff))
                    print(f"buy {vt_symbol}")
            # 小于则卖出
            elif diff < 0:
                # 有多头持仓,卖出平仓
                if pos > 0:
                    engine.sell(vt_symbol, tick.bid_price_1 - price_add,
                                abs(diff))
                    print(f"sell {vt_symbol}")
                # 否则卖出开仓
                else:
                    engine.short(vt_symbol, tick.bid_price_1 - price_add,
                                 abs(diff))
                    print(f"short {vt_symbol}")

        # 等待进入下一轮
        sleep(10)