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

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

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

问题

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

我正在尝试编写一个仅在满足特定入场条件时才会发生的退出条件。
类似于:
如果 ExitCondition1 那么 strategy.close //常规退出条件,无其他条件//
如果 ExitCondition2 并且 *我是在 EntryCondition5 的条件下进入的* 那么 strategy.close

尝试在条件之前将一个变量设置为 0,当我使用相关的入场条件进入时,在其中放入值为 1,然后将其添加到退出条件中(如果 exitcondition2 并且 var == 1),并在其他情况下将其重置为 0。

还尝试通过 'strategy.opentrades.entry_id' 来识别入场条件,但没有成功,并且尝试退出两个不同的入场 ID 时出现了混乱。

这是我尝试的代码:

entryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
RoundMacNorm = math.round(MacNorm * 100) / 100
RoundTrigger = math.round(Trigger * 100) / 100
RoundMacNormPrev = math.round(MacNorm[1] * 100) / 100
RoundTriggerPrev = math.round(Trigger[1] * 100) / 100
lastOrderID = strategy.opentrades.entry_id(strategy.opentrades - 1)
entryid = false

goLongCondition1 = (RoundMacNorm < -1.00) and (RoundTrigger < -1.00)
goLongCondition2 = (RoundMacNormPrev == -1.00) and (RoundMacNorm == -1.00)
goLongCondition3 = ((RoundTriggerPrev == 1.00) and (RoundTrigger > 1.00))
goLongCondition4 = ((RoundMacNormPrev < 0.1) and (RoundMacNorm == 1.00))

ExitLongCondition1 = (RoundMacNorm == 1.00) and (RoundTrigger == 1.00)
ExitLongCondition2 = (RoundMacNormPrev == 1.00) and (RoundMacNorm < 1.00)

goShortCondition1 = (RoundMacNorm == 1.00) and (RoundTrigger == 1.00)
goShortCondition2 = (RoundMacNorm[1] == 1.00) and (RoundMacNorm < 1.00)

if inTradeWindow and (goLongCondition1 or goLongCondition2)
    strategy.entry("long", strategy.long)
    entryid := false
if inTradeWindow and (goLongCondition3 or goLongCondition4)
    strategy.entry("long", strategy.long)
    entryid:= true
if inTradeWindow and goShortCondition1 and entryid == false
    strategy.entry("short", strategy.short)
    entryid := false
if inTradeWindow and goShortCondition2 and entryid == true
    strategy.entry("short", strategy.short)
    entryid := false

if inTradeWindow and (ExitLongCondition1 or ExitLongCondition2)
    strategy.close("long", comment='Exit')
    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:

entryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
RoundMacNorm = math.round(MacNorm * 100) / 100
RoundTrigger = math.round(Trigger * 100) / 100
RoundMacNormPrev = math.round(MacNorm[1] * 100) / 100
RoundTriggerPrev = math.round(Trigger[1] * 100) / 100
lastOrderID = strategy.opentrades.entry_id(strategy.opentrades - 1)
entryid = false

goLongCondition1 = (RoundMacNorm &lt; -1.00) and (RoundTrigger &lt; -1.00)
goLongCondition2 = (RoundMacNormPrev == -1.00) and (RoundMacNorm == -1.00)
goLongCondition3 = ((RoundTriggerPrev == 1.00) and (RoundTrigger &gt; 1.00)
goLongCondition4 = ((RoundMacNormPrev &lt; 0.1) and (RoundMacNorm == 1.00))

ExitLongCondition1 = (RoundMacNorm == 1.00) and (RoundTrigger == 1.00)
ExitLongCondition2 = (RoundMacNormPrev == 1.00) and (RoundMacNorm &lt; 1.00)


goShortCondition1 = (RoundMacNorm == 1.00) and (RoundTrigger == 1.00) 
goShortCondition2 = (RoundMacNorm[1] == 1.00) and (RoundMacNorm &lt; 1.00)


if inTradeWindow and (goLongCondition1 or goLongCondition2)
    strategy.entry(&quot;long&quot;, strategy.long)
    entryid := false 
if inTradeWindow and (goLongCondition3 or goLongCondition4)
    strategy.entry(&quot;long&quot;, strategy.long) 
    entryid:= true
if inTradeWindow and goShortCondition1 and entryid == false
    strategy.entry(&quot;short&quot;, strategy.short)
    entryid := false
if inTradeWindow and goShortCondition2 and entryid == true
    strategy.entry(&quot;short&quot;, strategy.short)
    entryid := false

if inTradeWindow and (ExitLongCondition1 or ExitLongCondition2)
    strategy.close(&quot;long&quot;, comment=&#39;Exit&#39;)
    entryid := false

答案1

得分: 0

option 1

var condition1Triggered = false

if inTradeWindow and (goLongCondition1 or goLongCondition2)
    strategy.entry("long", strategy.long)
    condition1Triggered := true

...

if inTradeWindow and strategy.position_size != 0 and condition1Triggered
    strategy.close("long", comment='Exit 1')
    condition1Triggered := false

option 2

if inTradeWindow and (goLongCondition1 or goLongCondition2)
    strategy.entry("long", strategy.long, comment = "Entered With Condition 1")

...

if inTradeWindow and strategy.opentrades.entry_comment(strategy.opentrades -1) == "Entered With Condition 1"
    strategy.close("long", comment='Exit 1')
英文:

option 1

var condition1Triggered = false

if inTradeWindow and (goLongCondition1 or goLongCondition2)
    strategy.entry(&quot;long&quot;, strategy.long)
    condition1Triggered := true

...

if inTradeWindow and strategy.position_size != 0 and condition1Triggered
    strategy.close(&quot;long&quot;, comment=&#39;Exit 1&#39;)
    condition1Triggered := false

option 2

if inTradeWindow and (goLongCondition1 or goLongCondition2)
    strategy.entry(&quot;long&quot;, strategy.long, comment = &quot;Entered With Condition 1&quot;)

...

if inTradeWindow and strategy.opentrades.entry_comment(strategy.opentrades -1) == &quot;Entered With Condition 1&quot;
    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:

确定