如何设置Pine Script策略避免首单未触发止损前新开交易
Pine Script策略问题修复方案
核心问题
- 未校验当前持仓状态就触发进场逻辑:只要多/空触发条件满足,无论当前是否有持仓都会执行下单操作,反向订单提交后会自动平掉原有持仓
- 代码中声明的
OpenTrade变量未提前初始化,也未实际用于持仓校验,属于无效逻辑 strategy.exit的止盈止损参数直接引用当前K线的计算值,且未做持仓状态绑定,参数有效性存在问题- 中周期SuperTrend的绘制逻辑错误引用了慢周期SuperTrend的方向参数,指标显示异常
后续开发 trailing stop loss 功能时,直接在持仓状态下更新对应方向的止损价变量,重新调用
strategy.exit传入新的止损参数即可实现动态跟踪止损。
修复后完整代码
//@version=5 strategy("My Strategy2", pyramiding = 1, overlay=true, calc_on_every_tick=true) // 持仓状态与订单参数持久化变量 var float fixedLongStop = na var float fixedLongProfit = na var float fixedShortStop = na var float fixedShortProfit = na //supertrend1 - supertrend slow GRP1 = "ST1" atrPeriod1 = input(12, "ATR Length1", group = GRP1) srcInput1 = input.source(hl2, "Source1") factor1 = input.float(3.0, "Factor1", step = 0.01, group = GRP1) [supertrend1, direction1] = ta.supertrend(factor1, atrPeriod1) bodyMiddle1 = plot((open + close) / 2, display=display.none) upTrend1 = plot(direction1 < 0 ? supertrend1 : na, "Up Trend", color = color.green, style=plot.style_linebr) downTrend1 = plot(direction1 < 0? na : supertrend1, "Down Trend", color = color.red, style=plot.style_linebr) //supertrend - medium GRP2 = "ST2" atrPeriod2 = input(11, "ATR Length2", group = GRP2) srcInput2= input.source(hl2, "Source2") factor2 = input.float(2.0, "Factor2", step = 0.01, group = GRP2) [supertrend2, direction2] = ta.supertrend(factor2, atrPeriod2) bodyMiddle2 = plot((open + close) / 2, display=display.none) // 修复方向参数引用错误 upTrend2 = plot(direction2 < 0 ? supertrend2 : na, "Up Trend", color = color.green, style=plot.style_linebr) downTrend2 = plot(direction2 < 0? na : supertrend2, "Down Trend", color = color.red, style=plot.style_linebr) // supertrend - fast GRP3 = "ST3" atrPeriod3 = input(10, "ATR Length3", group = GRP3) srcInput3 = input.source(hl2, "Source3") factor3 = input.float(1.0, "Factor3", step = 0.01, group = GRP3) [supertrend3, direction3] = ta.supertrend(factor3, atrPeriod3) bodyMiddle3 = plot((open + close) / 2, display=display.none) upTrend3 = plot(direction3 < 0 ? supertrend3 : na, "Up Trend", color = color.green, style=plot.style_linebr) downTrend3 = plot(direction3 < 0? na : supertrend3, "Down Trend", color = color.red, style=plot.style_linebr) GRP4 = "EMA1" len1 = input.int(50, minval=1, title="Length", group=GRP4) src1 = input(close, title="Source", group=GRP4) offset1 = input.int(title="Offset", defval=0, minval=-500, maxval=500, group=GRP4) out1 = ta.ema(src1, len1) plot(out1, linewidth=2, title="EMA", color=color.silver, offset=offset1) GRP8 = "StochRSI" smoothK = input.int(3, "K", minval=1, group=GRP8) smoothD = input.int(3, "D", minval=1, group=GRP8) lengthRSI = input.int(14, "RSI Length", minval=1, group=GRP8) lengthStoch = input.int(14, "Stochastic Length", minval=1, group=GRP8) src = input(close, title="RSI Source", group=GRP8) rsi1 = ta.rsi(src, lengthRSI) k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK) d = ta.sma(k, smoothD) plInput = input.float(1.5, title="P/L Ratio", step=0.1, minval=0.1) // long entery conditions longConditionsStoRSI = (k > d) longSupertrend = close > (direction2 < 0 ? supertrend2 : na) longConditionEMA = close > ta.ema(src1, len1) longconditions = longConditionsStoRSI and longSupertrend and longConditionEMA // short entery conditions shortConditionsStoRSI = (k < d) shortSupertrend = close < (direction2 < 0? na : supertrend2) shortconditionEMA = close < ta.ema(src1, len1) shortconditions = shortConditionsStoRSI and shortSupertrend and shortconditionEMA // 仅空仓时触发多单进场 if (longconditions and strategy.position_size == 0) strategy.entry("Long", strategy.long,1, oca_type = strategy.oca.cancel) // 固定进场时的止损止盈价格 fixedLongStop := direction1 < 0 ? supertrend1 : fixedLongStop[1] fixedLongProfit := close + ((close - fixedLongStop) * plInput) strategy.exit("Exit Long", "Long", limit = fixedLongProfit, stop = fixedLongStop) // 仅空仓时触发空单进场 if (shortconditions and strategy.position_size == 0) strategy.entry("Short", strategy.short,1, oca_type = strategy.oca.cancel) // 固定进场时的止损止盈价格 fixedShortStop := direction1 < 0? na : supertrend1 fixedShortProfit := close - ((fixedShortStop - close) * plInput) strategy.exit ("Exit Short", "Short", limit = fixedShortProfit, stop = fixedShortStop) // 仅对应持仓状态下显示止损止盈线 plot(strategy.position_size > 0 ? fixedLongStop : na, "LS", color = color.red, style=plot.style_line, linewidth = 5) plot(strategy.position_size > 0 ? fixedLongProfit : na, "LP", color = color.green, style=plot.style_line, linewidth = 5) plot(strategy.position_size < 0 ? fixedShortStop : na, "SS", color = color.orange, style=plot.style_line, linewidth = 5) plot(strategy.position_size < 0 ? fixedShortProfit : na, "SP", color = color.blue, style=plot.style_line, linewidth = 5)
内容的提问来源于stack exchange,提问作者coding_begginer
相关产品推荐
相关产品推荐

