barslast 如何取第一個滿足條件的

  •   76 
  • 最後發表   mina2310  2025 十二月 09
mina2310 發文於   2025/12/04

我希望在指標上面寫出當第一次condition100 條件出現的時候,用barslast 去算計距離現在有幾根,但似乎算出來的都是最新符合condition100 的,請問該怎麼改?截圖圖片

排序方式: 標準 | 最新
虎科大許教授 發文於   2025/12/04

var: intrabarpersist hasRet(false);

if hasRet=false and condition100 then

   begin

      value100=barslast(condition100);

      hasRet=true;

   end;

XS小編 發文於   2025/12/09

Hello mina2310,

 

小編補充,如果您需要計算條件第一次符合距離現在經過幾根Bar,可以用變數紀錄第一次條件符合當下的currentbar,再用目前的currentbar減去,即可得到距離。

var: _count(0), intraBarPersist _pos(0);

 

if _pos = 0 and condition100 then _pos = currentbar;

if _pos <> 0 then _count = currentBar - _pos;

 

這樣 _count 就會是條件第一次符合到當下K棒的距離。

發表回覆
Close