我正在尝试编写一个退出条件,只有当我以特定的进入条件进入时才会发生。 就像是: If ExitCondition1 then strategy.close //常规退出条件,没有其他条件// 如果 ExitCondition2 和 I Entered with EntryCondition5 那么 strategy.close
尝试在条件之前设置一个var为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
根据您提供的描述,这里有一个示例实现,它使用变量
entryConditionMet
来跟踪是否满足进入条件。仅当满足相关进入条件时,此变量才设置为真。之后,它会与退出条件一起检查以确定是否应该关闭交易。
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)
entryConditionMet = 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)
entryConditionMet = true
if inTradeWindow and (goLongCondition3 or goLongCondition4):
strategy.entry("long", strategy.long)
entryConditionMet = true
if inTradeWindow and goShortCondition1 and not entryConditionMet:
strategy.entry("short", strategy.short)
entryConditionMet = true
if inTradeWindow and goShortCondition2 and entryConditionMet:
strategy.entry("short", strategy.short)
entryConditionMet = true
if inTradeWindow and ((ExitLongCondition1 and not entryConditionMet) or (ExitLongCondition2 and entryConditionMet)):
strategy.close("long", comment='Exit')
entryConditionMet = false
请注意,此实现假设一次只有一个开放交易。如果您同时有多个未结交易,则需要修改代码以分别跟踪每笔交易的
entryConditionMet
变量,但您明白了..