Multi-Mode Take Profit/Stop Loss Trend Following Strategy Based on EMA, Madrid Ribbon and Donchian Channel

FMZQuant - Jan 20 - - Dev Community

Image description

Overview
This is a trend-following strategy that combines the Exponential Moving Average (EMA), the Madrid Ribbon, and the Donchian Channel. The uniqueness of the strategy is that it provides three switchable stop-loss and take-profit modes: based on points, based on amounts, and based on risk-return ratios. The reliability of transactions is improved through a secondary signal confirmation mechanism, and transactions are only made when a valid signal appears for the second time.

Strategy Principle
The strategy uses a combination of three technical indicators to identify trading opportunities:

  1. The 200-period exponential moving average is used to determine the overall trend direction
  2. Madrid Bands (crossover of 5-period and 100-period EMAs) are used to determine medium-term trends
  3. Donchian Channel Breakouts for Specific Entry Timing

Long trading conditions: price is above 200EMA, Madrid Band turns bullish and price breaks through the upper Donchian Channel. Short trading conditions: price is below 200EMA, Madrid Band turns bearish and price breaks through the lower Donchian Channel.
To reduce false signals, the strategy executes a trade only on the second valid signal.

Strategy Advantages

  1. Flexible stop-profit and stop-loss management system, which can switch modes according to different trading styles
  2. The combination of multiple technical indicators provides more reliable trading signals
  3. The secondary confirmation mechanism effectively reduces the impact of false signals
  4. The strategy completely avoids the look-ahead bias and there is no redrawing problem.
  5. Highly customizable to adapt to different market environments

Strategy Risks

  1. A large retracement may occur when the trend reverses
    Solution: The sensitivity of the strategy can be improved by adjusting the indicator parameters

  2. Over-reliance on technical indicators may lead to missing certain market opportunities
    Solution: It is recommended to combine fundamental analysis

  3. Fixed stop-loss and take-profit levels may not be suitable for all market conditions
    Solution: Dynamically adjust the stop-loss and take-profit levels based on volatility

Strategy Optimization Direction

  1. Introducing volatility indicators to dynamically adjust take-profit and stop-loss levels
  2. Added volume analysis to improve signal reliability
  3. Adding more market sentiment indicators
  4. Development of an adaptive parameter optimization system
  5. Add risk management modules, such as maximum drawdown control

Summary
This is a trend tracking strategy that combines multiple classic technical indicators, and improves the stability of transactions through flexible stop-profit and stop-loss management and secondary confirmation mechanisms. The high degree of customizability of the strategy enables it to adapt to different market environments and trading styles. It is recommended to conduct sufficient historical data backtesting before real-time use, and adjust parameter settings according to specific market characteristics.

Strategy source code

/*backtest
start: 2019-12-23 08:00:00
end: 2025-01-08 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/

//@version=6
strategy("Pamplona Enhanced TP/SL Toggleable", overlay=true, default_qty_type=strategy.fixed, default_qty_value=1)

// Input settings
use_tick_based = input.bool(false, title="Use Tick-Based TP/SL")
use_dollar_based = input.bool(false, title="Use Dollar-Based TP/SL")
use_risk_reward = input.bool(true, title="Use Risk-Reward TP/SL") // Default option

tick_size = input.float(0.1, title="Tick Size (for Tick-Based)", minval=0.0001, step=0.0001)
ticks = input.int(10, title="Ticks (for Tick-Based TP/SL)", minval=1)
dollar_tp = input.float(10.0, title="Dollar Take Profit (for Dollar-Based)", minval=0.01, step=0.01)
dollar_sl = input.float(10.0, title="Dollar Stop Loss (for Dollar-Based)", minval=0.01, step=0.01)
risk_reward_ratio = input.float(2.0, title="Risk-Reward Ratio (for Risk-Reward TP/SL)", minval=0.1, step=0.1)
contract_size = input.int(1, title="Contract Size", minval=1)

// Retrieve indicators
ema200 = ta.ema(close, 200)
src = close
ma05 = ta.ema(src, 5)
ma100 = ta.ema(src, 100)
madrid_green = ma05 > ma100
dlen = input.int(20, title="Donchian Channel Period")
highest_d = ta.highest(high, dlen)
lowest_d = ta.lowest(low, dlen)
donchian_green = close > highest_d[1]
donchian_red = close < lowest_d[1]

// Track signals
var int long_signal_count = 0
var int short_signal_count = 0

// Conditions
long_condition_raw = madrid_green and donchian_green and close > ema200
short_condition_raw = not madrid_green and donchian_red and close < ema200

// Update signal counters
if long_condition_raw
    long_signal_count += 1
else
    long_signal_count := 0

if short_condition_raw
    short_signal_count += 1
else
    short_signal_count := 0

// Final conditions to enter on the second signal
long_condition = long_signal_count == 2
short_condition = short_signal_count == 2

// Ensure exactly one TP/SL mode is enabled
tp_sl_mode_count = (use_tick_based ? 1 : 0) + (use_dollar_based ? 1 : 0) + (use_risk_reward ? 1 : 0)
if tp_sl_mode_count != 1
    runtime.error("Enable exactly ONE TP/SL mode (Tick-Based, Dollar-Based, or Risk-Reward).")

// Function to calculate TP/SL based on active mode
calc_tp_sl(entry_price, is_long) =>
    float tp = na
    float sl = na
    if use_tick_based
        tp := is_long ? entry_price + ticks * tick_size : entry_price - ticks * tick_size
        sl := is_long ? entry_price - ticks * tick_size : entry_price + ticks * tick_size
    else if use_dollar_based
        tp := is_long ? entry_price + (dollar_tp / contract_size) : entry_price - (dollar_tp / contract_size)
        sl := is_long ? entry_price - (dollar_sl / contract_size) : entry_price + (dollar_sl / contract_size)
    else if use_risk_reward
        risk = is_long ? close - low : high - close
        tp := is_long ? close + (risk * risk_reward_ratio) : close - (risk * risk_reward_ratio)
        sl := is_long ? close - risk : close + risk
    [tp, sl]

// Entry logic
if long_condition
    [take_profit, stop_loss] = calc_tp_sl(close, true)
    strategy.entry("Long", strategy.long, qty=contract_size)
    strategy.exit("Take Profit", from_entry="Long", limit=take_profit, stop=stop_loss)

if short_condition
    [take_profit, stop_loss] = calc_tp_sl(close, false)
    strategy.entry("Short", strategy.short, qty=contract_size)
    strategy.exit("Take Profit", from_entry="Short", limit=take_profit, stop=stop_loss)

// Plot indicators
plot(ema200, title="200 EMA", color=color.white, linewidth=2)
bgcolor(long_condition ? color.new(color.green, 90) : short_condition ? color.new(color.red, 90) : na)
Enter fullscreen mode Exit fullscreen mode

Strategy Parameters

Image description

The original address: Multi-Mode Take Profit/Stop Loss Trend Following Strategy Based on EMA, Madrid Ribbon and Donchian Channel

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .