策略测试:随机指标多空信号相互干扰问题求助
Fixing Stochastic Strategy Signal Issues in Pine Script
Let's break down why your short signals disappear when adjusting the golong condition, and how to fix both your long/short signal logic for a proper stochastic strategy.
The Root Cause
Your current goshort condition has a logical flaw, and the behavior you're seeing is a side effect of how Pine Script's strategy handles existing positions:
- Flawed short signal logic:
goshort=cu and k >=75checks if the current k-value is above 75 when it crosses below d. But by definition, when k crosses below d (cucondition), the current k is already lower than d. Since d is a 3-period SMA of k, d will closely follow k—so when k is in the overbought zone and crosses down, the current k is almost certainly below 75, making this condition rarely trigger on its own. - Position-dependent signals: When
golongwas set toco and k <=75, your strategy frequently opened long positions. Later, when acu and k >=75condition was met (a rare scenario where k crossed down but still stayed above 75), the strategy would close the existing long position and open a short one—this is why you saw short signals. When you tightenedgolongtok <=25, long positions almost never opened, so there was no existing long to close, and the standalonegoshortcondition rarely triggered.
Corrected Strategy Code
We'll fix the signal logic to target crossovers from the correct overbought/oversold zones, and use your defined OverSold/OverBought values for consistency:
//@version=4 strategy("Stochastic Slow Strategy", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000) // Inputs periodK = input(8, title="K Period", minval=1) periodD = input(3, title="D Period", minval=1) smoothK = input(3, title="Smooth K", minval=1) OverSold = input(20, title="Oversold Level", minval=1) OverBought = input(80, title="Overbought Level", minval=1) // Stochastic Calculation k = sma(stoch(close, high, low, periodK), smoothK) d = sma(k, periodD) // Crossover/Crossunder Conditions k_crosses_above_d = crossover(k, d) k_crosses_below_d = crossunder(k, d) // Corrected Entry Signals golong = k_crosses_above_d and k[1] <= OverSold // Long when k crosses up from oversold zone goshort = k_crosses_below_d and k[1] >= OverBought // Short when k crosses down from overbought zone // Entries strategy.entry("Buy", strategy.long, comment="Buy", when=golong) strategy.entry("Sell", strategy.short, comment="Sell", when=goshort)
Key Improvements
- Used Pine Script's built-in
crossover()andcrossunder()functions instead of manual cross checks—they're more reliable and readable. - Adjusted signals to check the previous bar's k-value against overbought/oversold levels:
- For longs: We confirm k crossed up from below the oversold threshold (so the crossover happens as k exits the oversold zone).
- For shorts: We confirm k crossed down from above the overbought threshold (so the crossunder happens as k exits the overbought zone).
- Added proper input handling for
OverSold/OverBoughtinstead of defining them as unused variables. - Explicitly set strategy quantity type to cash for clearer position sizing.
This fix ensures your short signals trigger independently of long positions, and both signals follow standard stochastic strategy logic.
内容的提问来源于stack exchange,提问作者Sriram V
相关产品推荐
相关产品推荐

