如何在Pine Script中按4种条件设置平仓止损?
Pine Script 多条件止损逻辑修正请求
开仓后需依据以下4种不同条件设置自动止损,但当前代码中strategy.exit的止损逻辑存在偏差,请求修正:
- 开仓后价格即刻跌破Vegas channel,且Volume Osc指标高于144参数线,立即以当前价格止损。
- 开仓后价格即刻跌破Vegas channel,且Volume Osc指标低于144,观察24小时内是否出现加速下跌(如跌幅5%),若无加速,止损设置在支撑位。
- 跌破后无明显加速,但24小时内无法回到Vegas channel顶部,也进行止损,最终止损位设置在Vegas channel下方。
- 价格未跌破Vegas channel时,常规设置6%的止损。
备注:Vegas channel=ema144与ema169之间的区间。
现有Pine Script代码如下:
strategy("Vegas", overlay=true, precision=2, pyramiding=6, initial_capital=6000) //---Volume Oscillator--- var cumVol = 0. cumVol += nz(volume) if barstate.islast and cumVol == 0 runtime.error("No volume is provided by the data vendor.") shortlen = input.int(1, minval=1, title = "Short Length") longlen = input.int(14, minval=1, title = "Long Length") short = ta.ema(volume, shortlen) long = ta.ema(volume, longlen) osc = 100 * (short - long) / long //---Support and Resistance Levels--- leftBars = input(15, title = "Left Bars ") rightBars = input(15, title = "Right Bars") volumeThresh = input(20, title = "Volume Threshold") highUsePivot = fixnan(ta.pivothigh(leftBars, rightBars)[1]) lowUsePivot = fixnan(ta.pivotlow(leftBars, rightBars)[1]) r1 = plot(highUsePivot, color=ta.change(highUsePivot) ? na : #ff113d, linewidth=3, offset=-(rightBars+1), title="Resistance") s1 = plot(lowUsePivot, color=ta.change(lowUsePivot) ? na : #007bff, linewidth=3, offset=-(rightBars+1), title="Support") //---Settings--- pc=input.float(title="tpsl percent",defval=6)/100 ordersize=1000/close vegas_long = (out144>out576 and out169>out576) and (out144>out676 and out169>out676) vegas_short = (out144<out576 and out169<out576) and (out144<out676 and out169<out676) vegas_max = math.max(out144,out169) vegas_min = math.min(out144,out169) distance_long = low-vegas_max distance_short = vegas_min-high vegas_long2 = out12>out144 and out12>out169 vegas_short2 = out12<out144 and out12<out169 opengap = input.int(title="Next Position Gap", defval=1) dist_min = input.float(title="Min Distance", defval=1) dist_max = input.float(title="Max Distance", defval=25) //---Long--- longCondition = vegas_long and vegas_long2 and (low>vegas_max and distance_long>dist_min and distance_long<=dist_max) if longCondition and (strategy.position_size == 0 or (strategy.position_size > 0 and close<strategy.position_avg_price and strategy.position_avg_price-close>=opengap)) longTP_pc = strategy.position_avg_price*(1+pc) longSL_pc = strategy.position_avg_price*(1-pc) longSL_support = lowUsePivot if strategy.position_size > 0 and (barstate.isnew and ta.crossunder(strategy.position_avg_price,vegas_max) and osc > 144) strategy.exit("Close Long","Long", limit=longTP_pc, stop=close, alert_message = "Close Long") if strategy.position_size > 0 and (barstate.isnew and ta.crossunder(strategy.position_avg_price,vegas_max) and osc < 144) strategy.exit("Close Long","Long", limit=longTP_pc, stop=longSL_support, alert_message = "Close Long") if strategy.position_size > 0 and (barstate.isnew and osc < 144) strategy.exit("Close Long","Long", limit=longTP_pc, stop=longSL_pc, alert_message = "Close Long")
内容的提问来源于stack exchange,提问作者Ying
相关产品推荐
相关产品推荐

