Strategy vault / Trend following
Stochastic Crossover Long and Short Strategy
This strategy generates trading signals based on the golden cross and death cross of %K line and %D line of the Stochastic indicator. It goes short when %K line crosses below %D line while both are in the overbought…

Overview
This strategy generates trading signals based on the golden cross and death cross of %K line and %D line of the Stochastic indicator. It goes short when %K line crosses below %D line while both are in the overbought area, and goes long when %K line crosses above %D line while both are in the oversold area. The strategy captures the reversal characteristic of Stochastic indicator and forms trading signals around trend turning points.
Strategy Logic
The strategy utilizes two lines, %K and %D, of the Stochastic indicator. %K line shows the current closing price relative to the highest and lowest prices over a certain period, and %D line is the M-day simple moving average of %K line.
When %K line crosses below %D line, it indicates the start of a downward trend, and together with both lines in the overbought area, it signals the critical point for price reversal, so a short position is taken.
When %K line crosses above %D line, it indicates the start of an upward trend, and together with both lines in the oversold area, it signals the critical point for price reversal, so a long position is taken.
By capturing the reversal moments of Stochastic indicator, trading signals can be generated around trend turning points.
Advantage Analysis
The main advantages of this strategy are:
- Captures trend reversals and enables contrarian trading
- Utilizes the reversal characteristic of Stochastic indicator for trade signals
- Combines overbought/oversold areas to avoid false reversals
- Simple and clear logic, easy to implement
Risk Analysis
The main risks of this strategy are:
- Stochastic indicator prone to false reversals, causing incorrect signals
- Fails to filter market noise effectively, potentially over-trading
- Unable to determine trend direction, needs trend filter
- No effective stop loss control, can lead to large losses
Corresponding solutions:
- Combine with other indicators to filter false signals
- Adjust parameters properly to ensure stable reliable signals
- Use with trend indicators to avoid counter-trend trading
- Incorporate stop loss mechanism to limit max loss per trade
Optimization Directions
The strategy can be optimized from the following aspects:
- Adjust Stochastic parameters, optimize %K, %D periods
- Add moving averages etc to filter signals, improve quality
- Add trend judgment rules to avoid counter-trend trades
- Incorporate stop loss and take profit rules for robustness
- Optimize entry and exit logic to reduce trading frequency
- Test adaptability across products and timeframes
- Strategy ensemble, combine with other strategies
Conclusion
This strategy generates trading signals based on the crossover of the short and long lines of the Stochastic indicator, aiming to capture reversals for contrarian trading. The logic is simple and clear, easy to implement, but also has some flaws. Better results can be achieved through parameter tuning, indicator combinations, risk control etc. It is a short-term trading strategy suitable for high frequency trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 7 | Length |
| v_input_2 | 3 | DLength |
| v_input_3 | 20 | Oversold |
| v_input_4 | 70 | Overbought |
| v_input_5 | false | Trade reverse |
Source Pine Script
/*backtest
start: 2022-12-08 00:00:00
end: 2023-12-14 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=2
////////////////////////////////////////////////////////////
// Copyright by HPotter v1.0 11/01/2017
// This back testing strategy generates a long trade at the Open of the following
// bar when the %K line crosses below the %D line and both are above the Overbought level.
// It generates a short trade at the Open of the following bar when the %K line
// crosses above the %D line and both values are below the Oversold level.
//
// You can change long to short in the Input Settings
// Please, use it only for learning or paper trading. Do not for real trading.
////////////////////////////////////////////////////////////
strategy(title="Strategy Stochastic Crossover", shorttitle="Strategy Stochastic Crossover1", overlay = true )
Length = input(7, minval=1)
DLength = input(3, minval=1)
Oversold = input(20, minval=1)
Overbought = input(70, minval=1)
reverse = input(false, title="Trade reverse")
vFast = stoch(close, high, low, Length)
vSlow = sma(vFast, DLength)
pos = iff(vFast < vSlow and vFast > Overbought and vSlow > Overbought, 1,
iff(vFast >= vSlow and vFast < Oversold and vSlow < Oversold, -1, nz(pos[1], 0)))
possig = iff(reverse and pos == 1, -1,
iff(reverse and pos == -1, 1, pos))
if (possig == 1)
strategy.entry("Long", strategy.long)
if (possig == -1)
strategy.entry("Short", strategy.short)
barcolor(possig == -1 ? red: possig == 1 ? green : blue )Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-15. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Stochastic-Crossover-Long-and-Short-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Dual Moving Average Crossover Strategy with Stop Loss and Take ProfitPine Script · Trend following · 4h
- EMA AROON ASHPine Script · Trend following · 3m
- EMA SMA Multi Indicator Comprehensive Trend Following StrategyPine Script · Trend following · 1h
- EMA200 and Stochastic RSI StrategyPine Script · Trend following · 1h
- Gaussian Channel Trend Following with Stochastic RSI Trading StrategyPine Script · Trend following · 1d