You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

策略测试:随机指标多空信号相互干扰问题求助

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:

  1. Flawed short signal logic: goshort=cu and k >=75 checks if the current k-value is above 75 when it crosses below d. But by definition, when k crosses below d (cu condition), 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.
  2. Position-dependent signals: When golong was set to co and k <=75, your strategy frequently opened long positions. Later, when a cu and k >=75 condition 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 tightened golong to k <=25, long positions almost never opened, so there was no existing long to close, and the standalone goshort condition 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() and crossunder() 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/OverBought instead 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 20:58:00