分享

量化投资

 taozl 2022-09-23 发布于安徽

第十章:bar内模式与非bar内模式

1、MC对于bar的运行模式区分bar内模式与非bar内模式两种,区分这两种模式主要是为了(本人认为)是为了加速代码的运算速度。

2、可以根据信号设置开启bar内模式,也可以在代码上方写入[IntrabarOrderGeneration=True],表示开启bar内

 3、回测时:

  不开始bar内交易:每根K线收盘,计算一次

  开启bar内交易:根据精细资料的选择作为计算周期

  【精细资料】

4、实时:

  不开始bar内交易:每根K线收盘(收到收盘tick时),计算一次

  开启bar内交易:每逢新tick计算一次

5、图示:

6、开始bar内和不开启bar内,barStatus的异同

  开启bar内:

//历史行情读取时
time= 1309.00 barstatus= 2.00 time= 1309.00 barstatus= 0.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1309.00 barstatus= 1.00 time= 1310.00 barstatus= 2.00

//实时数据时

time= 1311.00 barstatus= 2.00
time= 1312.00 barstatus= 0.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00
time= 1312.00 barstatus= 1.00

time= 1312.00 barstatus= 2.00

  不开始bar内:

time= 1301.00 barstatus=    2.00
time= 1302.00 barstatus=    2.00
time= 1303.00 barstatus=    2.00
time= 1304.00 barstatus=    2.00
time= 1305.00 barstatus=    2.00
time= 1306.00 barstatus=    2.00
time= 1307.00 barstatus=    2.00
time= 1308.00 barstatus=    2.00
time= 1309.00 barstatus=    2.00

【总结】:

  在开启bar时,读取历史行情的情况下是基于精细数据而言,因此在开始的第一个状态会保证读取的是收盘tick,然后再用开盘tick覆盖掉,所以显示了2后再显示0,实时数据保持一致。

  在不开始bar时,数据是在bar结束时更新一次。

7、bar内模式和非bar内模式,bar数据取值的不同点

  非bar内模式下:

    根据bar的合成规则:当新的一笔tick来临,如果触发bar的完成,此时在bar的最后一个tick会计算一次,形成收盘价,此时新来的这一笔tick就叫做下一个bar的开盘tick。

    【举例】:

     第一步: 【上一个未完成的bar】 ----(新tick来临)-----(促成上一个bar完成)---- 【形成上一个bar的收盘价】 + 【形成下一个bar的开盘价】

     第二步:  (新tick来临) = (开盘tick)【下一个bar的】

     第三步:    【旧bar形成】←←←close[1]/open[1]/high[1]/low[1]... 瞬间完成...open of next bar  →→→  【新bar开始】

  【总结】

      在第三步的瞬间完成,我们发现此时可以取到两类值:    

                    第一类:旧bar的开高低收的值

                    第二类:新bar的开盘价

      因此,这就解释了在非bar内模式下,为什么我们下单可以下“buy 1 shares Next bar at open of Next bar”,此时“open of Next bar”等于右侧箭头的那一瞬间完成的新bar开盘价。

      在一种解释是,我们发现当前正在形成的这根bar上面显示的市价和本地时间不一致:

      

       如图所示,bar上面的实现显示是36分钟,而电脑bar显示的时间是35分钟,而我们在这个bar结束时,打印的是35分钟    

      

       这就解释了,在MC机制下,bar上的时间总是提前是因为当前的的这个时间是准备要完成的时间,而不是完成后的时间。直白说就是当一个新开盘tick来临时,等待这个bar的时间结束行程旧bar时的那一刻打印数据,此时已经完成了。

      也就是在交易代码中:

      if XXX > XXX then....

      这样就可以不用采用回溯的方式,在上面所说的“瞬时完成”的那一刻,旧的时间已经完成了,此时已是过去式,新的价格来的,此时是正在进行时。因此不用回溯。

      MC的这种机制和其他软件的不太一样。其他的软件都是等bar形式后,才公布此时形成的时间,而MC是新的tick来临时,按照这个规定的时间来完成tick。一个是过去完成时,一个是将来完成时。

  bar内模式:

    bar内模式的取值方式是非常不一样的。正如前面所说的,在非bar内模式下,在“瞬时完成”,这一刻会取到旧bar的开高低收价格和新bar的开盘价。因为MC对于非bar模式具有这样一种计算模式。但是bar内模式是每根tick都进行计算,因此不存在这种计算模式。因此不能取“open of next bar”这样的数值。而且从上面可以看到,在非bar内模式下,打印的是“顺势完成”这个时刻打印,而且打印的是上一个时间的值,但是bar内模式下是实时打印,打印的是当前时刻的值。

 【总结】:

  在牵扯到bar内交易和非bar内交易的时候,存在这样的不同点。因此在编写代码的时候,要思考你的交易逻辑是否考虑需要bar内交易。比如像:“当high/low”突破某一个价位的时候,需要做开平仓操作,此时如果设置为非bar内交易的话,这种交易是没有用的,因为非bar内交易只在bar的收盘时计算一次,盘中的这个过程是不会计算的,因此要写成bar内模式的形式。比如像“我只是在开盘价进行开平仓”操作,牵扯不到bar内价格的变化,此时就只需要采用非bar模式即可。

=================================================

之前的文章感谢大家的转载,希望转载时请注明出处,本人转自其它网站的图表一并感谢,谢谢~!

https://www.cnblogs.com/noah0532/

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多