英文:
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 < -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
答案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("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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论