Pinescript-退出位置错误。 Strategy.exit 随机打开相反的仓位,因为没有计算“strategy.close”平仓的仓位
我在一个条目上使用两个strategy.close 和一个strategy.exit。通常情况下它工作得很好。但我不知道是因为 close 和 exit 不会在同一个酒吧中相互交谈。有时,在strategy.close主动平仓50%之后,后面的strategy.exit仍然想退出100%的仓位。而这样的行为会造成相反的情况。
**///FIRST STOP LINE**
if Count1bull_check:=true
bull_low:= low
**////BUYING LOGIC**
bull_ctrend= bull_check and val[1]<val and bull_low <close and val<0
if bull_check and val[1]<val and val<0
bull_check:=false
**///RESET**
if strategy.opentrades ==0
bull_out1_trend_check:=false
bull_out2_trend_check:=false
bull_out1_trend_notdone :=true
bull_out2_trend_notdone :=true
bull_out1_low:=0
**///BUYING**
if bull_ctrend
strategy.entry('OpenLong_ctrend', strategy.long)
bull_check:=false
**////SET UP FOR CLOSE OR EXIT PRIORITY**
if bull_out1_ctrend
bull_out1_ctrend_check:=true
if bull_out2_ctrend
bull_out2_ctrend_check:=true
//counter trend exit order hierarchy
bull_1_ctrend =bull_out1_ctrend_check == true
bull_12_ctrend =bull_out1_ctrend_check== true and bull_out2_ctrend_check== true
**////CLOSE AND EXIT CON**
if (strategy.opentrades>0 and bull_out1_ctrend and bull_out1_ctrend_notdone) and ta.barssince(bull_ctrend)>=1
strategy.close("OpenLong_ctrend",qty_percent=50,comment="c50%")
bull_out1_ctrend_notdone := false
bull_out1_clow:=low
if bull_out1_ctrend_notdone==false and bull_out2_ctrend and bull_out2_ctrend_notdone
strategy.close("OpenLong_ctrend",comment="close all")
bull_out1_ctrend_notdone := true
**////EXIT**
strategy.exit("longcx","OpenLong_ctrend", stop=math.max(bull_low,bull_out1_clow),comment=bull_low<bull_out1_clow?"c1out":"clow")
红圈是susses,蓝圈是因为过度执行退出位置而打开相反的仓位
我也调整 exit qty_percent=50 ,但是正确的会变成错误的。所以.... 由于strategy.close将按市场价格执行,而strategy.exit将使用限价,我将需要它们。所以我不能只使用strategy.exit或strategy.close来完成整个工作。
大多数情况下,程序运行良好,但如果在同一柱中同时发生strategy.exit和strategy.close,则错误将随机发生(实际上,strategy.close将由于前一个柱而执行,而strategy.close将执行。退出将发生在同一柱策略上。关闭执行)
I use two strategy.close and one strategy.exit on one entry. Normally it works fine. however I don't know is that because close and exit don't talk to each other in the same bar or not. sometimes, after strategy.close active close the 50% of position, and the following strategy.exit still want to exit 100% of potion. And such action cause create the opposite position.
**///FIRST STOP LINE**
if Count1bull_check:=true
bull_low:= low
**////BUYING LOGIC**
bull_ctrend= bull_check and val[1]<val and bull_low <close and val<0
if bull_check and val[1]<val and val<0
bull_check:=false
**///RESET**
if strategy.opentrades ==0
bull_out1_trend_check:=false
bull_out2_trend_check:=false
bull_out1_trend_notdone :=true
bull_out2_trend_notdone :=true
bull_out1_low:=0
**///BUYING**
if bull_ctrend
strategy.entry('OpenLong_ctrend', strategy.long)
bull_check:=false
**////SET UP FOR CLOSE OR EXIT PRIORITY**
if bull_out1_ctrend
bull_out1_ctrend_check:=true
if bull_out2_ctrend
bull_out2_ctrend_check:=true
//counter trend exit order hierarchy
bull_1_ctrend =bull_out1_ctrend_check == true
bull_12_ctrend =bull_out1_ctrend_check== true and bull_out2_ctrend_check== true
**////CLOSE AND EXIT CON**
if (strategy.opentrades>0 and bull_out1_ctrend and bull_out1_ctrend_notdone) and ta.barssince(bull_ctrend)>=1
strategy.close("OpenLong_ctrend",qty_percent=50,comment="c50%")
bull_out1_ctrend_notdone := false
bull_out1_clow:=low
if bull_out1_ctrend_notdone==false and bull_out2_ctrend and bull_out2_ctrend_notdone
strategy.close("OpenLong_ctrend",comment="close all")
bull_out1_ctrend_notdone := true
**////EXIT**
strategy.exit("longcx","OpenLong_ctrend", stop=math.max(bull_low,bull_out1_clow),comment=bull_low<bull_out1_clow?"c1out":"clow")
the red circle is the susses one, and the blue one is the one which open the opposite position due to over execute exit position
I to adjust exit qty_percent=50 also, but the right one will become the wrong one. so....
Due to the strategy.close one will execute by market price and strategy.exit will use limit price, which I will need them both. So I couldn't use only strategy.exit or strategy.close to do the whole work.
most of time the program works fine, but if the strategy.exit and strategy.close happens at once in the same bar, the error will random happens (actually, the strategy.close will execute due to the previous bar, and the strategy.exit will happens on the same bar strategy.close execute)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论