Board logo

标题: 统计交易过程中连续盈利次数或连续亏损次数 [打印本页]

作者: 龙听    时间: 2022-7-7 15:21     标题: 统计交易过程中连续盈利次数或连续亏损次数

程式码部分:
  1. var: openEquity(0), closeEquity(0), closeProfit(0),mp(0);
  2. var: wlCount(0),barTime(0);

  3. mp = marketposition;
  4. barTime = date+time_s/1000000;

  5. //calc OpenEquity
  6. openEquity = i_OpenEquity; //i_OpenEquity = netprofit(has been exit) + openpositionprofit(still has position)
  7. closeEquity = i_ClosedEquity;
  8. closeProfit = 0;

  9. if mp <> mp[1]{this bar position changed!} and mp[1] <> 0{last bar have position} then {means new bar:1,no position or 2,opposite position} closeProfit = closeEquity - closeEquity[1]{covered earns};

  10. if closeProfit <> 0 then begin {have earn or loss}
  11.         if closeProfit > 0 then begin {have earn}
  12.                 if wlCount < 0 then wlCount = 0;{last is loss,reset time = 0}
  13.                 wlCount = wlCount + 1;{earn time +1 ,the first time earn}   
  14.          end;

  15.         if closeProfit < 0 then begin {have loss}
  16.                 if wlCount > 0 then wlCount = 0; {last trade is earn,the reset time = 0}
  17.                 wlCount = wlCount - 1; {record loss time -1}               
  18.         end;
  19. end;
复制代码
wlcount就是在交易过程中连续盈利或亏损的次数了。原则是这样的,1、确定上一根bar是有持仓的。2、最新bar持仓变动了,(或平仓了或反手了)。3、统计最新的这个平仓是盈利还是亏损。以及上次是盈利还是亏损。无外乎这么几种情况:

第一,最新是盈利的。1,之前也是盈利的,就顺着 + 1 ,即连续盈利多增加了一次。2,之前是亏损的,则重置之前的亏损计数为0 ,最新的这个盈利成1。

第二,最新是亏损的。1、之前也是亏损的,顺加一次亏损。2,之前是盈利的,则重置计数成0,现在开始计数亏损次数为1。(这里我将亏损设置成负1,即亏损一次,减1,这样通过看wlcount的正负及值就知道是连亏或连盈了)。

上面的这一段可以设计成计数模块,也就是增加到策略信号里面。也可以写成公式,然后在策略中调用,没有参数。同时里面的一些注释也可以删掉。不影响实际效果。
作者: 龙听    时间: 2022-7-7 15:31

还有一种用法就是写成全局变量,然后在策略中调用:具体来说

信号:earnloss (跟策略信号是放一起的)
本帖隐藏的内容需要回复才可以浏览


调用全局变量时这样调用:

本帖隐藏的内容需要回复才可以浏览


这里的earnloss就是调用出来的连续亏损或盈利的次数了,就可以按自己的思路写怎么处理策略了。
作者: 龙听    时间: 2022-7-8 06:45

也要参考视频网课资源:http://www.qhlt.cn/thread-127887-1-1.html




欢迎光临 龙听期货论坛 (http://www.qhlt.cn/) Powered by Discuz! 7.2