0bps

Strategy vault / Trend following

EMA Crossover with Dual Take Profit and Stop Loss Strategy

The EMA Crossover with Dual Take Profit and Stop Loss Strategy is a quantitative trading approach that combines moving average crossover signals with dynamic risk management. This strategy utilizes the crossover of…

Author
ChaoZhang
Complexity
Starter · 68 lines
Parameters
0
Backtest bar
4h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

The EMA Crossover with Dual Take Profit and Stop Loss Strategy is a quantitative trading approach that combines moving average crossover signals with dynamic risk management. This strategy utilizes the crossover of short-term and long-term Exponential Moving Averages (EMAs) to generate entry signals, while employing a combination of fixed and dynamic take profit and stop loss mechanisms to manage risk and secure profits. This method aims to capture market trends while protecting trading capital through flexible risk control.

Strategy Principles

  1. Signal Generation:

    • Uses 20-period and 50-period Exponential Moving Averages (EMAs)
    • Triggers a long entry when the short-term EMA crosses above the long-term EMA
    • Triggers a short entry when the short-term EMA crosses below the long-term EMA
  2. Risk Management:

    • Initial take profit set at 200 pips from entry price
    • Initial stop loss set at 100 pips beyond the long-term EMA
    • Stop loss level adjusts as price moves, maintaining a 100-pip distance from the long-term EMA
  3. Trade Execution:

    • Uses strategy.entry function to execute buy and sell operations
    • Uses strategy.exit function to close positions based on take profit and stop loss levels
  4. Visualization:

    • Plots short-term and long-term EMA lines on the chart
    • Uses background color to indicate buy (green) and sell (red) signals

Strategy Advantages

  1. Trend Following: Captures market trends through EMA crossovers, beneficial in strong trending markets.

  2. Dynamic Risk Management: Stop loss level moves with the long-term EMA, adapting to market changes and providing better risk protection.

  3. Fixed Take Profit: 200-pip fixed take profit helps secure gains before trend reversals.

  4. Visual Aids: EMA lines and background colors provide intuitive trading signals, facilitating analysis and decision-making.

  5. Adjustable Parameters: Key parameters such as EMA periods, take profit, and stop loss pips can be adjusted for different markets and personal preferences.

  6. Fully Automated: The strategy is completely automated, reducing human intervention and emotional influences.

Strategy Risks

  1. Choppy Market Risk: In sideways or choppy markets, frequent EMA crossovers may lead to consecutive losses.

  2. Slippage Risk: In highly volatile markets, actual execution prices may significantly differ from ideal prices.

  3. Fixed Take Profit Limitation: The 200-pip fixed take profit might close positions too early in strong trends, missing out on potential profits.

  4. Drawdown Risk: The 100-pip stop loss might not be sufficient to effectively control risk in some situations, leading to larger drawdowns.

  5. Over-reliance on EMAs: Sole dependence on EMAs may overlook other important market information and indicators.

Strategy Optimization Directions

  1. Multi-Indicator Integration: Combine with other technical indicators like RSI, MACD, etc., to improve signal accuracy and reliability.

  2. Adaptive Parameters: Dynamically adjust EMA periods and take profit/stop loss pips based on market volatility to adapt to different market environments.

  3. Incorporate Volume Analysis: Consider volume factors to improve trend judgment accuracy and timing of trades.

  4. Time Filtering: Add trading time filters to avoid trading during low liquidity market sessions.

  5. Improve Take Profit Mechanism: Introduce trailing take profit to protect profits while allowing for continued growth.

  6. Risk Management Optimization: Dynamically adjust the proportion of funds for each trade based on account size and risk preference.

  7. Add Market Sentiment Analysis: Incorporate market sentiment indicators for better judgment of market trends and potential reversals.

Conclusion

The EMA Crossover with Dual Take Profit and Stop Loss Strategy is a quantitative trading method that combines technical analysis with risk management. By leveraging EMA crossover signals and dynamic stop loss mechanisms, this strategy aims to capture market trends while controlling risk. While the strategy performs well in trending markets, it may face challenges in choppy conditions. Through multi-indicator integration, parameter optimization, and improved risk management, the strategy has the potential to further enhance its performance and adaptability. Traders using this strategy should fully understand its strengths and limitations, and make appropriate adjustments based on individual risk tolerance and market conditions.

Source Pine Script

/*backtest
start: 2024-06-01 00:00:00
end: 2024-06-30 23:59:59
period: 4h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Estratégia com Médias Móveis", overlay=true)

// Parâmetros das médias móveis
ema_short_length = input.int(20, title="EMA Curta")
ema_long_length = input.int(50, title="EMA Longa")
tp_pips = input.int(200, title="Take Profit em Pips")
sl_pips = input.int(100, title="Stop Loss em Pips")

// Cálculo das médias móveis
ema_short = ta.ema(close, ema_short_length)
ema_long = ta.ema(close, ema_long_length)

// Definição do Take Profit e Stop Loss iniciais em pips
pip_size = syminfo.mintick
initial_take_profit_buy = tp_pips * pip_size
initial_take_profit_sell = tp_pips * pip_size
initial_stop_loss_buy = ema_long - sl_pips * pip_size
initial_stop_loss_sell = ema_long + sl_pips * pip_size

// Variáveis para controle de SL e TP móveis
var float stop_loss_level = na
var float take_profit_level = na

// Condições para Compra e Venda
buy_condition = ta.crossover(ema_short, ema_long)
sell_condition = ta.crossunder(ema_short, ema_long)

// Atualização do Stop Loss Móvel e Take Profit Móvel
if (buy_condition)
    stop_loss_level := ema_long - sl_pips * pip_size
    take_profit_level := close + initial_take_profit_buy

if (sell_condition)
    stop_loss_level := ema_long + sl_pips * pip_size
    take_profit_level := close - initial_take_profit_sell

// Execução da Estratégia de Compra
if (buy_condition)
    strategy.entry("Compra", strategy.long)

// Saída da Estratégia de Compra
if (strategy.position_size > 0)
    strategy.exit("Take Profit", "Compra", limit=take_profit_level, stop=stop_loss_level)

// Execução da Estratégia de Venda
if (sell_condition)
    strategy.entry("Venda", strategy.short)

// Saída da Estratégia de Venda
if (strategy.position_size < 0)
    strategy.exit("Take Profit", "Venda", limit=take_profit_level, stop=stop_loss_level)

// Plotagem das EMAs
plot(ema_short, color=color.blue, title="EMA Curta")
plot(ema_long, color=color.red, title="EMA Longa")

// Estilo de fundo baseado na posição
bgcolor(buy_condition ? color.green : sell_condition ? color.red : na, transp=80)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-07-29. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/EMA-Crossover-with-Dual-Take-Profit-and-Stop-Loss-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open