Multi Level ZigZag Percentage Retracement with Stochastic Trading Strategy
This strategy combines ZigZag percentage retracement with the Stochastic indicator to create a comprehensive trading system. It identifies key market turning points using a dynamically adjusted ZigZag indicator while…

Overview
This strategy combines ZigZag percentage retracement with the Stochastic indicator to create a comprehensive trading system. It identifies key market turning points using a dynamically adjusted ZigZag indicator while optimizing entry timing using Stochastic overbought/oversold signals. The system also incorporates stop-loss and take-profit mechanisms for risk management.
Strategy Principles
The core logic is based on several key components:
- Dynamic ZigZag indicator - Supports manual setting or ATR-based dynamic adjustment of retracement percentage across different periods (5-250)
- Stochastic filter - Uses 9-period K value with 3-period smoothing
- Trade signal generation:
- Buy signal: Price breaks above reversal line and Stochastic K value below 20
- Sell signal: Price breaks below reversal line and Stochastic K value above 80
- Risk management - Fixed point-based stop-loss (100 points) and take-profit (300 points)
Strategy Advantages
- High adaptability - ATR-based dynamic adjustment for different market conditions
- Multiple confirmations - Combines trend and momentum indicators to reduce false signals
- Controlled risk - Comprehensive stop-loss and take-profit mechanisms
- Clear signals - Trading signals are distinct and easy to execute
- Flexible parameters - Supports multiple customizable parameters for optimization
Strategy Risks
- Choppy market risk - May trigger frequent stop-losses in ranging markets
- Slippage risk - Potential for significant slippage in fast-moving markets
- Fixed stop-loss risk - Fixed point-based stops may not suit all market conditions
- False breakout risk - May generate false signals in consolidation zones
- Parameter sensitivity - Strategy performance heavily depends on parameter selection
Optimization Directions
- Dynamic stop-loss - Consider using ATR or volatility for dynamic stop adjustment
- Market environment filter - Add trend strength indicators for strong trend validation
- Signal confirmation enhancement - Consider adding volume confirmation
- Entry timing optimization - Incorporate price pattern recognition
- Position management improvement - Implement volatility-based position sizing
Summary
The strategy builds a comprehensive trading system by combining ZigZag and Stochastic indicators. Its main features are clear signals and controlled risk, but it requires parameter optimization based on actual market conditions. It's recommended to conduct thorough backtesting and make continuous improvements based on market experience before live trading.
Source Pine Script
/*backtest
start: 2025-02-18 14:00:00
end: 2025-02-20 00:00:00
period: 5m
basePeriod: 5m
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/
//@version=5
strategy("[RS]ZigZag Percent Reversal with Stochastic Strategy", overlay=true)
// ||---}---------------------------------------------------------------------||
// |--------------------------------------------------------------------------||
// | ZigZag: ||
// |--------------------------------------------------------------------------||
// |{
string percent_method = input.string(
defval="MANUAL",
title="Method to use for the zigzag reversal range:",
options=[
"MANUAL",
"ATR005 * X", "ATR010 * X", "ATR020 * X", "ATR050 * X", "ATR100 * X", "ATR250 * X"
]
)
var float percent = input.float(
defval=0.25,
title="Percent of last pivot price for zigzag reversal:",
minval=0.0, maxval=99.0
) / 100
float percent_multiplier = input.float(
defval=1.0,
title="Multiplier to apply to ATR if applicable:"
)
if percent_method == "ATR005 * X"
percent := ta.atr(5) / open * percent_multiplier
if percent_method == "ATR010 * X"
percent := ta.atr(10) / open * percent_multiplier
if percent_method == "ATR020 * X"
percent := ta.atr(20) / open * percent_multiplier
if percent_method == "ATR050 * X"
percent := ta.atr(50) / open * percent_multiplier
if percent_method == "ATR100 * X"
percent := ta.atr(100) / open * percent_multiplier
if percent_method == "ATR250 * X"
percent := ta.atr(250) / open * percent_multiplier
// ||-------------------------------------------------------------------------||
// || zigzag function:
// ||-------------------------------------------------------------------------||
// |{
f_zz(_percent)=>
// direction after last pivot
var bool _is_direction_up = na
// track highest price since last lower pivot
var float _htrack = na
// track lowest price since last higher pivot
var float _ltrack = na
// zigzag variable for plotting
var float _pivot = na
// range needed for reaching reversal threshold
float _reverse_range = 0.0
// real pivot time
var int _real_pivot_time = na
var int _htime = na
var int _ltime = na
// reverse line
var float _reverse_line = na
if bar_index >= 1
if na(_is_direction_up)
_is_direction_up := true
_reverse_range := nz(_pivot[1]) * _percent
if _is_direction_up
_ltrack := na
_ltime := time
if na(_htrack)
if high > high[1]
_htrack := high
_htime := time
else
_htrack := high[1]
_htime := time[1]
else
if high > _htrack
_htrack := high
_htime := time
// Reversal line calculation based on the current candle's closing price
_reverse_line := _htrack - _reverse_range
// If close <= reversal line, mark pivot and reverse direction
if close <= _reverse_line
_pivot := _htrack
_real_pivot_time := _htime
_is_direction_up := false
if not _is_direction_up
_htrack := na
_htime := na
if na(_ltrack)
if low < low[1]
_ltrack := low
_ltime := time
else
_ltrack := low[1]
_ltime := time[1]
else
if low < _ltrack
_ltrack := low
_ltime := time
// Reversal line calculation based on the current candle's closing price
_reverse_line := _ltrack + _reverse_range
// If close >= reversal line, mark pivot and reverse direction
if close >= _reverse_line
_pivot := _ltrack
_real_pivot_time := _ltime
_is_direction_up := true
[_pivot, _is_direction_up, _reverse_line, _real_pivot_time]
[pivot, direction_up, reverse_line, pivot_time] = f_zz(percent)
// Çizim
// Sabit Reversal Line (fiyat seviyesinde sabit)
var float static_reverse_line = na
if (not na(reverse_line))
static_reverse_line := reverse_line
plot(series=static_reverse_line, color=color.gray, style=plot.style_line, title="Reversal Line", trackprice=false)
// ||-------------------------------------------------------------------------||
// || Stochastic: ||
// ||-------------------------------------------------------------------------||
// |{
K_length = 9
K_smoothing = 3
// Stochastic %K hesaplama
stochK = ta.sma(ta.stoch(close, high, low, K_length), K_smoothing)
// ||-------------------------------------------------------------------------||
// || Custom Buy/Sell Signals:
// ||-------------------------------------------------------------------------||
// |{
// Buy sinyali: Fiyat reversal line'ının üstünde ve stochastic K değeri 20'nin altında ise
buy_signal = close > static_reverse_line and stochK < 20
// Sell sinyali: Fiyat reversal line'ının altındaysa ve stochastic K değeri 80'in üstünde ise
sell_signal = close < static_reverse_line and stochK > 80
// Alım ve satım sinyali için strateji girişlerini ekle
long_condition = buy_signal
short_condition = sell_signal
// **Burada Stop Loss ve Take Profit değerleri ayarlandı**
stop_loss_pips = 100 // Stop Loss 10 pip
take_profit_pips = 300 // Take Profit 30 pip
// Stop Loss ve Take Profit seviyeleri hesaplanıyor
long_stop_loss = close - stop_loss_pips * syminfo.mintick
long_take_profit = close + take_profit_pips * syminfo.mintick
short_stop_loss = close + stop_loss_pips * syminfo.mintick
short_take_profit = close - take_profit_pips * syminfo.mintick
// Long stratejisi: Alım sinyali ve stop loss/take profit seviyeleri ile
if long_condition
strategy.entry("Buy", strategy.long, stop=long_stop_loss, limit=long_take_profit)
strategy.exit("Take Profit / Stop Loss", from_entry="Buy", stop=long_stop_loss, limit=long_take_profit)
// Webhook ile alım bildirimi gönder
alert("Buy Signal Triggered!", alert.freq_once_per_bar)
// Short stratejisi: Satım sinyali ve stop loss/take profit seviyeleri ile
if short_condition
strategy.entry("Sell", strategy.short, stop=short_stop_loss, limit=short_take_profit)
strategy.exit("Take Profit / Stop Loss", from_entry="Sell", stop=short_stop_loss, limit=short_take_profit)
// Webhook ile satım bildirimi gönder
alert("Sell Signal Triggered!", alert.freq_once_per_bar)
// Alım sinyali gösterimi
plotshape(series=buy_signal, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Signal", text="BUY", textcolor=color.white)
// Satım sinyali gösterimi
plotshape(series=sell_signal, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Signal", text="SELL", textcolor=color.white)
Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-21. 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/Multi-Level-ZigZag-Percentage-Retracement-with-Stochastic-Trading-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
- Multi Indicator Trend Following and Volatility Breakout StrategyPine Script · Breakout · 2h
- Momentum Breakout Trading Strategy 435896Pine Script · Breakout · 1d
- Advanced Long Only Dynamic Trendline Breakout StrategyPine Script · Breakout · 1d
- Pivot Based Volume Weighted Breakout Reversal StrategyPine Script · Breakout · 1d
- Reversal Breakout StrategyPine Script · Breakout · 1d
- Dynamic Range Breakout Trading Strategy Based on Bollinger Bands and RSIPine Script · Breakout · 1d