Pine Script策略问题:如何确保Stochastic的K、D值同时满足超买超卖?
问题描述
编写Pine Script策略,需同时满足以下条件触发交易:
- 特定K线形态
- Stochastic指标超买/超卖(做多要求K、D值均≤30;做空要求K、D值均≥70)
- SMA均线交叉
目前遇到问题:99%场景条件正常,但偶尔触发交易的K线收盘时,Stochastic的K值满足要求,D值却未达标。尝试添加历史运算符、修改bar状态后问题仍存在。
(截图显示触发K线的Stochastic值未满足双值条件)
附原策略代码:
strategy("My Strategy", overlay=true, initial_capital = 10000, currency = 'USDT', default_qty_type = strategy.percent_of_equity, default_qty_value = 100) //Get user input var g_rsi = 'Rsi Settings' periodK = input.int(5, title="%K Length", minval=1, group = g_rsi) smoothK = input.int(3, title="%K Smoothing", minval=1, group = g_rsi) periodD = input.int(3, title="%D Smoothing", minval=1, group = g_rsi) upperBand = input.int(title="Upper Band", defval=70, group = g_rsi) lowerBand = input.int(title="Lower Band", defval=30, group = g_rsi) //Get sma Lookback input var g_sma = 'SMA Lookback' smaLookback = input.int(title = 'SMA Lookback', defval = 20, group = g_sma) smaLookback1 = input.int(title = 'SMA Lookback', defval = 50, group = g_sma) //Get R:R inputs var g_rr = 'Risk:Reward Settings' multiplier = input.float(title = 'ATR Multiplier', defval = 1.0, group = g_rr, tooltip = 'Multiplies SL and TP based on ATR eg. 1:1, 2:2') rr = input.float(title = 'Risk:Reward', defval = 3.4, group = g_rr, tooltip = 'Adjusts your profit Level, eg. 2:1, 3:1, 4:1') rrlookback = input.int(title = 'Lookback', defval = 1, group = g_rr) //Get ATR value atr = ta.atr(14) //Get SMA TA sma1 = ta.sma(close, smaLookback) sma2 = ta.sma(close, smaLookback1) //Get stoch TA k = ta.sma(ta.stoch(close, high, low, periodK), smoothK) d = ta.sma(k, periodD) //Get RSI ob/os filters isRSIos = k and d <= lowerBand isRSIob = k and d >= upperBand //Get SMA crossovers islongEntrysma = sma1 > sma2 isshortEntrysma = sma1 < sma2 //Get long candle patterns lessthanprevHigh = high < high[1] bullishCandle = close > open long = lessthanprevHigh and bullishCandle //Get short candle patterns greaterthanprevLow = low > low[1] bearishCandle = close < open short = greaterthanprevLow and bearishCandle //Get entry conditions buyCondition = long and islongEntrysma and isRSIos and not na(atr) and barstate.isconfirmed sellCondition = short and isshortEntrysma and isRSIob and not na(atr) and barstate.isconfirmed //Calculate stops and targets longStop = ta.lowest(low, rrlookback) - (atr * multiplier) shortStop = ta.highest(high, rrlookback) + (atr * multiplier) longStopDistance = close - longStop shortStoDistance = shortStop - close longTarget = close + (longStopDistance * rr) shortTarget = close - (shortStoDistance * rr) //Save stops and targets var t_stop = 0.0 var t_target = 0.0 //Enter our buy order if buyCondition and strategy.position_size == 0 t_stop := longStop t_target := longTarget strategy.entry(id = 'Long', direction = strategy.long)//here we are asking, is buySignal true? if so, enter our trade using strategy.entry with a id(title), of 'Long' and direction of long //Enter our sell order if sellCondition and strategy.position_size == 0 t_stop := shortStop t_target := shortTarget strategy.entry(id = 'Short', direction = strategy.short)//here we are asking, is buySignal true? if so, enter our trade using strategy.entry with a id(title), of 'Long' and direction of long //Manage exit orders strategy.exit(id = 'Long Exit', from_entry = 'Long', limit = t_target, stop = t_stop, when = strategy.position_size > 0) strategy.exit(id = 'Short Exit', from_entry = 'Short', limit = t_target, stop = t_stop, when = strategy.position_size < 0) //Plot SL and TP targets plot(strategy.position_size != 0 ? t_stop : na, color = color.red, style = plot.style_linebr) plot(strategy.position_size != 0 ? t_target : na, color = color.green, style = plot.style_linebr)
问题原因
核心错误出在Stochastic超买/超卖条件的逻辑判断上:
原代码中isRSIos = k and d <= lowerBand的写法错误,Pine Script中数值类型在布尔判断里会被当作非零即真,所以这行实际是判断k不为0 且 d≤30,而非k≤30 且 d≤30。同理做空条件isRSIob = k and d >= upperBand也是错误逻辑,导致偶尔出现K满足但D不达标时触发交易的情况。
修正方案
修改超买/超卖条件的判断逻辑,明确要求K和D同时满足阈值条件:
// 修正后的Stochastic超买/超卖条件 isRSIos = k <= lowerBand and d <= lowerBand isRSIob = k >= upperBand and d >= upperBand
完整修正后代码
strategy("My Strategy", overlay=true, initial_capital = 10000, currency = 'USDT', default_qty_type = strategy.percent_of_equity, default_qty_value = 100) //Get user input var g_rsi = 'Rsi Settings' periodK = input.int(5, title="%K Length", minval=1, group = g_rsi) smoothK = input.int(3, title="%K Smoothing", minval=1, group = g_rsi) periodD = input.int(3, title="%D Smoothing", minval=1, group = g_rsi) upperBand = input.int(title="Upper Band", defval=70, group = g_rsi) lowerBand = input.int(title="Lower Band", defval=30, group = g_rsi) //Get sma Lookback input var g_sma = 'SMA Lookback' smaLookback = input.int(title = 'SMA Lookback', defval = 20, group = g_sma) smaLookback1 = input.int(title = 'SMA Lookback', defval = 50, group = g_sma) //Get R:R inputs var g_rr = 'Risk:Reward Settings' multiplier = input.float(title = 'ATR Multiplier', defval = 1.0, group = g_rr, tooltip = 'Multiplies SL and TP based on ATR eg. 1:1, 2:2') rr = input.float(title = 'Risk:Reward', defval = 3.4, group = g_rr, tooltip = 'Adjusts your profit Level, eg. 2:1, 3:1, 4:1') rrlookback = input.int(title = 'Lookback', defval = 1, group = g_rr) //Get ATR value atr = ta.atr(14) //Get SMA TA sma1 = ta.sma(close, smaLookback) sma2 = ta.sma(close, smaLookback1) //Get stoch TA k = ta.sma(ta.stoch(close, high, low, periodK), smoothK) d = ta.sma(k, periodD) // 修正后的Stochastic超买/超卖条件 isRSIos = k <= lowerBand and d <= lowerBand isRSIob = k >= upperBand and d >= upperBand //Get SMA crossovers islongEntrysma = sma1 > sma2 isshortEntrysma = sma1 < sma2 //Get long candle patterns lessthanprevHigh = high < high[1] bullishCandle = close > open long = lessthanprevHigh and bullishCandle //Get short candle patterns greaterthanprevLow = low > low[1] bearishCandle = close < open short = greaterthanprevLow and bearishCandle //Get entry conditions buyCondition = long and islongEntrysma and isRSIos and not na(atr) and barstate.isconfirmed sellCondition = short and isshortEntrysma and isRSIob and not na(atr) and barstate.isconfirmed //Calculate stops and targets longStop = ta.lowest(low, rrlookback) - (atr * multiplier) shortStop = ta.highest(high, rrlookback) + (atr * multiplier) longStopDistance = close - longStop shortStoDistance = shortStop - close longTarget = close + (longStopDistance * rr) shortTarget = close - (shortStoDistance * rr) //Save stops and targets var t_stop = 0.0 var t_target = 0.0 //Enter our buy order if buyCondition and strategy.position_size == 0 t_stop := longStop t_target := longTarget strategy.entry(id = 'Long', direction = strategy.long) //Enter our sell order if sellCondition and strategy.position_size == 0 t_stop := shortStop t_target := shortTarget strategy.entry(id = 'Short', direction = strategy.short) //Manage exit orders strategy.exit(id = 'Long Exit', from_entry = 'Long', limit = t_target, stop = t_stop, when = strategy.position_size > 0) strategy.exit(id = 'Short Exit', from_entry = 'Short', limit = t_target, stop = t_stop, when = strategy.position_size < 0) //Plot SL and TP targets plot(strategy.position_size != 0 ? t_stop : na, color = color.red, style = plot.style_linebr) plot(strategy.position_size != 0 ? t_target : na, color = color.green, style = plot.style_linebr)
内容的提问来源于stack exchange,提问作者maxi974
相关产品推荐
相关产品推荐

