检查交易是否开放,如果中间线下方交叉两次,则触发止损。

huangapple go评论49阅读模式
英文:

While trade is open, check if middleBand is crossed below twice, if true trigger SL

问题

我试图检查价格是否"下穿"中间线两次:

longSLCondition = ta.crossover(close, middleBand ) and ta.crossover(close, middleBand )

条件会关闭交易,但是方向错误:

检查交易是否开放,如果中间线下方交叉两次,则触发止损。

在图表中,您可以看到价格第一次在开仓后闭合到中间线下方。然后价格再次上升到中间线上方,触发了止损(红圈)。

我希望止损在价格第二次下穿中间线时触发(绿圈),而不是上穿。

感谢您的专业知识。

英文:

I am trying to check if the price is "crossing down" twice the middleBand :

longSLCondition = ta.crossover(close, middleBand ) and ta.crossover(close, middleBand )

Condition closes the trade but the wrong way :

检查交易是否开放,如果中间线下方交叉两次,则触发止损。

In the chart, you can see that the price closes a first time below the middleBand just after the trade was opened. Then he closes above the middleBand and the SL is triggered (red circle).

I would like the SL triggered when the price closes a second time below the middleBand (green circle) and not above.

Thanks for your expertise.

答案1

得分: 0

以下是翻译好的部分:

"Idea was to trigger a stop loss if the price close twice below the middle band."

这个想法是,如果价格连续两次关闭在中间线以下,就触发止损。

"Here's the solution :"

以下是解决方案:

if ta.crossunder(close, middleBand)
    cross_count := cross_count + 1 // Increment the counter if condition is met
if delayElapsed
    strategy.entry('long', strategy.long)
    cross_count:=0 // Reset the counter after having entered the trade
    strategy.close('long', comment="SL Long")```

<details>
<summary>英文:</summary>

Idea was to trigger a stop loss if the price close twice below the middle band.

Here&#39;s the solution :

var int cross_count = 0 // Initialize the counter variable
if ta.crossunder(close, middleBand)
cross_count := cross_count + 1 // Increment the counter if condition is met

// --- Long Entry | Close
if delayElapsed
strategy.entry('long', strategy.long)
cross_count:=0 // Reset the counter after having entered the trade


f (cross_count >= 2 and strategy.position_size > 0) // Check if long trade and price crossed twice the middleBand
strategy.close('long', comment="SL Long")


</details>



huangapple
  • 本文由 发表于 2023年2月16日 15:50:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定