vn.py量化社区
By Traders, For Traders.
Member
加入于:
帖子: 7
声望: 0

1

Member
加入于:
帖子: 7
声望: 0
def on_bar(self, bar: BarData):
    """
    Callback of new bar data update.
    """
    am = self.am
    am.update_bar(bar)
    if not am.inited:
        return

    # 计算均线
    #ma5_arry = am.ma_s(self.ma5, array = True)
    ma5_arry = am.sma(self.ma5, array = True)
    ma10_arry = am.sma(self.ma10, array = True)
    ma20_arry = am.sma(self.ma20, array = True)
    ma30_arry = am.sma(self.ma30, array = True)

    self.ma5_value  = ma5_arry[-2]
    self.ma10_value  = ma10_arry[-2]
    self.ma20_value  = ma20_arry[-2]
    self.ma30_value  = ma30_arry[-2]

    # 多头排列
    fasan_long = self.ma5_value > self.ma10_value and self.ma10_value > self.ma20_value

    if fasan_long:
        #print("多头发散",self.iCount_L,bar.datetime,fasan_long)
        self.iCount_L += 1
    else:
        self.iCount_L = 1


    # 判断是否出多头排列来决定是否触发交易逻辑
    """   
    当出现了多头排列:如果没有持仓时,则直接买入开仓;或者持有空头,则先平空再买入开仓。
    """
    if self.iFlag == 0:
        #print("直接发散")
        print(self.pos, )
        #--------------------------------------------------------------------------------
        #                               开仓 --- 多开
        #--------------------------------------------------------------------------------
        if self.iCount_L >= self.nFS_Days:
            if self.pos == 0 :
                print(bar.datetime, "  ---多头排列开仓 --- ", bar.close_price, self.ma30_value)
                self.buy(bar.close_price, self.fixed_size)                                     # buy:买入开仓( Direction:多,Offset:开)
                self.OpenPrice = bar.close_price

            elif self.pos < 0:
                print("     前仓为空,平空仓,开多仓", bar.datetime," 持仓:",self.pos)
                #self.cover(bar.close_price, self.fixed_size)                                  # cover:买入平仓( Direction:多,Offset:平)
                #self.buy(bar.close_price, self.fixed_size)

        #--------------------------------------------------------------------------------
        #                               平仓 --- 平多
        #--------------------------------------------------------------------------------             
        if bar.close_price < self.ma30_value:
            if self.pos > 0:
                print(bar.datetime,"- 跌破MA30止损,",self.OpenPrice," 开<->平 ",bar.close_price," - MA30:",round(self.ma30_value,2), self.fixed_size)
                self.sell(bar.close_price, self.fixed_size)
                print(self.sell(bar.close_price, self.fixed_size))
                print(self.pos, self.fixed_size, " end stop m30")


    self.put_event()
Member
加入于:
帖子: 7
声望: 0

下一个K线的价格没有触发到报单的价格,会延续到下一个K线。CTA策略回测遵循T时刻发出的委托,永远只在T+1后才能进行撮合的规则

Member
avatar
加入于:
帖子: 148
声望: 33

self.sell(bar.close_price, abs(self.pos))

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3