0bps

Strategy vault / Trend following

EMA Crossover Strategy with Stop Loss and Take Profit Optimization System

This strategy is a quantitative trading system based on the crossover of 5-period and 15-period Exponential Moving Averages (EMA). It aims to achieve stable returns while protecting capital through reasonable…

Author
ChaoZhang
Complexity
Starter · 50 lines
Parameters
0
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

This strategy is a quantitative trading system based on the crossover of 5-period and 15-period Exponential Moving Averages (EMA). It aims to achieve stable returns while protecting capital through reasonable stop-loss and take-profit levels. The strategy uses classic moving average crossover signals to identify market trend changes and combines them with risk management mechanisms to control the risk-reward ratio of each trade.

Strategy Principles

The core of the strategy is monitoring the crossover between the fast-moving average (5-period EMA) and the slow-moving average (15-period EMA). A long signal is generated when the 5-period EMA crosses above the 15-period EMA, while a short signal is generated when the 5-period EMA crosses below the 15-period EMA. For each trading signal, the system automatically sets a 1.5% stop-loss level and a 3% take-profit level, ensuring a favorable risk-reward ratio. The stop-loss and take-profit levels are calculated based on the entry price, effectively controlling risk exposure.

Strategy Advantages

  1. Signal generation mechanism is objective and easy to understand, not affected by subjective judgment
  2. Uses exponential moving averages to reduce the impact of false breakouts
  3. Fixed percentage stop-loss and take-profit levels facilitate capital management
  4. Risk-reward ratio of 1:2 follows professional trading principles
  5. Simple strategy logic, easy to implement and maintain
  6. Applicable to multiple markets and timeframes

Strategy Risks

  1. May generate frequent false signals in ranging markets, increasing transaction costs
  2. Fixed stop-loss and take-profit settings may not suit all market conditions
  3. Fast EMA is sensitive to price movements, potentially leading to overtrading
  4. Does not consider market volatility changes, risk control lacks flexibility
  5. Stop-loss execution may be delayed in extreme market conditions

Strategy Optimization Directions

  1. Introduce volatility indicators to dynamically adjust stop-loss and take-profit levels
  2. Add trend filters to reduce false signals in ranging markets
  3. Dynamically adjust EMA periods based on different market characteristics
  4. Add volume confirmation mechanism to improve signal reliability
  5. Implement time filters to avoid trading during unfavorable periods
  6. Consider adding trailing stop mechanism to optimize profit-taking

Summary

This is a well-structured quantitative trading strategy with clear logic. It captures trend reversal points through moving average crossovers and implements risk control with fixed stop-loss and take-profit levels. The strategy is simple to use, suitable for beginners, and provides a good foundation for further optimization. Traders are advised to conduct thorough backtesting before live implementation and optimize parameters according to specific market characteristics.

Source Pine Script

/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-26 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("5 EMA and 15 EMA Crossover with Stop Loss and Target", overlay=true)

// Define EMAs
ema5 = ta.ema(close, 5)
ema15 = ta.ema(close, 15)

// Plot EMAs on the chart
plot(ema5, title="5 EMA", color=color.blue)
plot(ema15, title="15 EMA", color=color.red)

// Crossover conditions
longCondition = ta.crossover(ema5, ema15)
shortCondition = ta.crossunder(ema5, ema15)

// Stop-loss and take-profit percentage
stopLossPercent = 1.5  // Stop-loss at 1.5%
takeProfitPercent = 3.0  // Take-profit at 3%

// Calculate stop-loss and take-profit levels for long and short positions
longStopLoss = strategy.position_avg_price * (1 - stopLossPercent / 100)
longTakeProfit = strategy.position_avg_price * (1 + takeProfitPercent / 100)

shortStopLoss = strategy.position_avg_price * (1 + stopLossPercent / 100)
shortTakeProfit = strategy.position_avg_price * (1 - takeProfitPercent / 100)

// Enter long position with stop-loss and take-profit
if (longCondition)
    strategy.entry("Long", strategy.long)
    strategy.exit("Take Profit/Stop Loss", "Long", stop=longStopLoss, limit=longTakeProfit)

// Enter short position with stop-loss and take-profit
if (shortCondition)
    strategy.entry("Short", strategy.short)
    strategy.exit("Take Profit/Stop Loss", "Short", stop=shortStopLoss, limit=shortTakeProfit)

// Plot stop-loss and take-profit levels
plot(longStopLoss, title="Long Stop Loss", color=color.red, linewidth=1, style=plot.style_linebr)
plot(longTakeProfit, title="Long Take Profit", color=color.green, linewidth=1, style=plot.style_linebr)
plot(shortStopLoss, title="Short Stop Loss", color=color.red, linewidth=1, style=plot.style_linebr)
plot(shortTakeProfit, title="Short Take Profit", color=color.green, linewidth=1, style=plot.style_linebr)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-11-27. 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-Strategy-with-Stop-Loss-and-Take-Profit-Optimization-System.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