构建一个退出条件,检查是否符合特定的入场条件。

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

Pine Script - Build an exit condition that checks if I entered with a ceratin entry condition

问题

以下是您要翻译的代码部分:

  1. 我正在尝试编写一个仅在满足特定入场条件时才会发生的退出条件。
  2. 类似于:
  3. 如果 ExitCondition1 那么 strategy.close //常规退出条件,无其他条件//
  4. 如果 ExitCondition2 并且 *我是在 EntryCondition5 的条件下进入的* 那么 strategy.close
  5. 尝试在条件之前将一个变量设置为 0,当我使用相关的入场条件进入时,在其中放入值为 1,然后将其添加到退出条件中(如果 exitcondition2 并且 var == 1),并在其他情况下将其重置为 0
  6. 还尝试通过 'strategy.opentrades.entry_id' 来识别入场条件,但没有成功,并且尝试退出两个不同的入场 ID 时出现了混乱。
  7. 这是我尝试的代码:
  8. entryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
  9. RoundMacNorm = math.round(MacNorm * 100) / 100
  10. RoundTrigger = math.round(Trigger * 100) / 100
  11. RoundMacNormPrev = math.round(MacNorm[1] * 100) / 100
  12. RoundTriggerPrev = math.round(Trigger[1] * 100) / 100
  13. lastOrderID = strategy.opentrades.entry_id(strategy.opentrades - 1)
  14. entryid = false
  15. goLongCondition1 = (RoundMacNorm < -1.00) and (RoundTrigger < -1.00)
  16. goLongCondition2 = (RoundMacNormPrev == -1.00) and (RoundMacNorm == -1.00)
  17. goLongCondition3 = ((RoundTriggerPrev == 1.00) and (RoundTrigger > 1.00))
  18. goLongCondition4 = ((RoundMacNormPrev < 0.1) and (RoundMacNorm == 1.00))
  19. ExitLongCondition1 = (RoundMacNorm == 1.00) and (RoundTrigger == 1.00)
  20. ExitLongCondition2 = (RoundMacNormPrev == 1.00) and (RoundMacNorm < 1.00)
  21. goShortCondition1 = (RoundMacNorm == 1.00) and (RoundTrigger == 1.00)
  22. goShortCondition2 = (RoundMacNorm[1] == 1.00) and (RoundMacNorm < 1.00)
  23. if inTradeWindow and (goLongCondition1 or goLongCondition2)
  24. strategy.entry("long", strategy.long)
  25. entryid := false
  26. if inTradeWindow and (goLongCondition3 or goLongCondition4)
  27. strategy.entry("long", strategy.long)
  28. entryid:= true
  29. if inTradeWindow and goShortCondition1 and entryid == false
  30. strategy.entry("short", strategy.short)
  31. entryid := false
  32. if inTradeWindow and goShortCondition2 and entryid == true
  33. strategy.entry("short", strategy.short)
  34. entryid := false
  35. if inTradeWindow and (ExitLongCondition1 or ExitLongCondition2)
  36. strategy.close("long", comment='Exit')
  37. entryid := false

希望这对您有所帮助。如果您有其他问题或需要进一步的帮助,请随时告诉我。

英文:

I am trying to code an exit condition that happen only if I entered with a certain entry condition.
Something like:
If ExitCondition1 then strategy.close //regular exit condition, no other conditions for it//
If ExitCondition2 and I Entered with EntryCondition5 then strategy.close

Tried to set a var to 0 before the conditions, put a 1 value in it when I enter with the relevant entry conditions, add it (if exitcondition2 and var == 1) to the exit condition, and reset it to 0 on any other occasion.

Also tried to identify the entry condition through the 'strategy.opentrades.entry_id' but didn't work, and I got in a mess with trying to exit two different entry ID's

this is the code I tried:

  1. entryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
  2. RoundMacNorm = math.round(MacNorm * 100) / 100
  3. RoundTrigger = math.round(Trigger * 100) / 100
  4. RoundMacNormPrev = math.round(MacNorm[1] * 100) / 100
  5. RoundTriggerPrev = math.round(Trigger[1] * 100) / 100
  6. lastOrderID = strategy.opentrades.entry_id(strategy.opentrades - 1)
  7. entryid = false
  8. goLongCondition1 = (RoundMacNorm &lt; -1.00) and (RoundTrigger &lt; -1.00)
  9. goLongCondition2 = (RoundMacNormPrev == -1.00) and (RoundMacNorm == -1.00)
  10. goLongCondition3 = ((RoundTriggerPrev == 1.00) and (RoundTrigger &gt; 1.00)
  11. goLongCondition4 = ((RoundMacNormPrev &lt; 0.1) and (RoundMacNorm == 1.00))
  12. ExitLongCondition1 = (RoundMacNorm == 1.00) and (RoundTrigger == 1.00)
  13. ExitLongCondition2 = (RoundMacNormPrev == 1.00) and (RoundMacNorm &lt; 1.00)
  14. goShortCondition1 = (RoundMacNorm == 1.00) and (RoundTrigger == 1.00)
  15. goShortCondition2 = (RoundMacNorm[1] == 1.00) and (RoundMacNorm &lt; 1.00)
  16. if inTradeWindow and (goLongCondition1 or goLongCondition2)
  17. strategy.entry(&quot;long&quot;, strategy.long)
  18. entryid := false
  19. if inTradeWindow and (goLongCondition3 or goLongCondition4)
  20. strategy.entry(&quot;long&quot;, strategy.long)
  21. entryid:= true
  22. if inTradeWindow and goShortCondition1 and entryid == false
  23. strategy.entry(&quot;short&quot;, strategy.short)
  24. entryid := false
  25. if inTradeWindow and goShortCondition2 and entryid == true
  26. strategy.entry(&quot;short&quot;, strategy.short)
  27. entryid := false
  28. if inTradeWindow and (ExitLongCondition1 or ExitLongCondition2)
  29. strategy.close(&quot;long&quot;, comment=&#39;Exit&#39;)
  30. entryid := false

答案1

得分: 0

option 1

  1. var condition1Triggered = false
  2. if inTradeWindow and (goLongCondition1 or goLongCondition2)
  3. strategy.entry("long", strategy.long)
  4. condition1Triggered := true
  5. ...
  6. if inTradeWindow and strategy.position_size != 0 and condition1Triggered
  7. strategy.close("long", comment='Exit 1')
  8. condition1Triggered := false

option 2

  1. if inTradeWindow and (goLongCondition1 or goLongCondition2)
  2. strategy.entry("long", strategy.long, comment = "Entered With Condition 1")
  3. ...
  4. if inTradeWindow and strategy.opentrades.entry_comment(strategy.opentrades -1) == "Entered With Condition 1"
  5. strategy.close("long", comment='Exit 1')
英文:

option 1

  1. var condition1Triggered = false
  2. if inTradeWindow and (goLongCondition1 or goLongCondition2)
  3. strategy.entry(&quot;long&quot;, strategy.long)
  4. condition1Triggered := true
  5. ...
  6. if inTradeWindow and strategy.position_size != 0 and condition1Triggered
  7. strategy.close(&quot;long&quot;, comment=&#39;Exit 1&#39;)
  8. condition1Triggered := false

option 2

  1. if inTradeWindow and (goLongCondition1 or goLongCondition2)
  2. strategy.entry(&quot;long&quot;, strategy.long, comment = &quot;Entered With Condition 1&quot;)
  3. ...
  4. if inTradeWindow and strategy.opentrades.entry_comment(strategy.opentrades -1) == &quot;Entered With Condition 1&quot;
  5. strategy.close(&quot;long&quot;, comment=&#39;Exit 1&#39;)

huangapple
  • 本文由 发表于 2023年3月8日 17:29:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671330.html
匿名

发表评论

匿名网友

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

确定