止损触发后如何实现反向开仓?(附Pine Script尝试代码)
需求与尝试代码
我建立了多头头寸,但价格下跌触发止损时,希望脚本不仅平仓多头,还能自动开仓空头。自行编写代码未成功,以下是我的几次尝试:
初始代码片段1
strategy.exit("Exit short", from_entry="short", stop=low * stoplossforSell)
接着编写:
if strategy.position_entry_name=="Exit short" strategy.entry("long from short",strategy.long)
编辑版本
if(bhigh[1]>upper[1] and bhigh[1]>bhigh and macd<signal2 ) strategy.close("long") strategy.entry("short",strategy.short) strategy.exit("Exit short", from_entry="short", stop=low * stoplossforSell) strategy.entry("Short", strategy.short, stop=low * stoplossforSell) if(blow[1]<lower[1] and blow[1]<blow and open[1]<close[1] and macd>signal2) strategy.close("short") strategy.entry("long",strategy.long) strategy.exit("Exit long", from_entry="long", stop=low * stoplossforBuy) strategy.entry("long", strategy.long, stop=low * stoplossforSell)
编辑版本2
stoplossforSell= input(defval=1.02,title="stoploss for sell") stoplossforBuy= input(defval=0.98,title="Stoploss for buy") if(bhigh[1]>upper[1] and bhigh[1]>bhigh and open[1]>close[1] and macd<signal2 ) strategy.close("long") strategy.entry("short",strategy.short) if (strategy.position_size < 0) strategy.exit("Exit short", from_entry="short", stop=low * stoplossforSell) strategy.entry("long", strategy.long, stop=low * stoplossforSell) if(blow[1]<lower[1] and blow[1]<blow and open[1]<close[1] and macd>signal2) strategy.close("short") strategy.entry("long",strategy.long) if (strategy.position_size > 0) strategy.exit("Exit long", from_entry="long", stop=low * stoplossforBuy) strategy.entry("short", strategy.short, stop=low * stoplossforBuy)
内容的提问来源于stack exchange,提问作者Kristen
相关产品推荐
相关产品推荐

