Overview
This strategy is a trend tracking trading system based on the 5-day exponential moving average (EMA). It analyzes the position relationship between price and EMA and combines the dynamic adjustment of stop loss and profit targets to grasp the market trend. The strategy adopts a percentage position management method and takes into account transaction cost factors, which is highly practical and flexible.
Strategy Principle
The core logic of the strategy is to determine the entry time based on the interaction between the price and the 5-day EMA. Specifically, when the highest price of the previous cycle is lower than the EMA and a breakthrough occurs in the current cycle, the system will send a long signal. At the same time, the strategy also includes an optional additional condition, which requires the closing price to be higher than the previous cycle to increase the reliability of the signal. For risk control, the strategy provides two stop loss methods: dynamic stop loss based on the previous low point, and stop loss based on a fixed number of points. The profit target is dynamically set based on the risk-return ratio to ensure the profit potential of the transaction.
Strategy Advantages
- Strong ability to grasp trends: Through the coordination of EMA and price, the initial stage of the trend can be effectively captured.
- Perfect risk control: Flexible stop loss options are provided, which can use fixed point stop loss or dynamic stop loss.
- Reasonable profit target: Set profit targets based on risk-return ratio to ensure that each transaction has sufficient profit margin.
- Transaction costs are fully considered: the calculation of transaction costs is included in the strategy to be more in line with the actual trading environment.
- Flexible and adjustable parameters: key parameters such as stop loss distance, risk-return ratio, etc. can be adjusted according to different market conditions.
Strategy Risks
- False breakout risk: False breakout signals may occur in a volatile market, leading to stop-loss exit.
- Slippage impact: In a volatile market, the actual transaction price may deviate significantly from the signal price.
- EMA lag: As a moving average indicator, EMA has a certain lag, which may cause a slight delay in entry timing.
- Money management risk: Fixed percentage position management may lead to excessive capital drawdown in the event of consecutive losses.
Strategy Optimization Direction
- Multi-period confirmation: You can add longer-period trend confirmation, such as adding a 20-day EMA as a trend direction filter.
- Volatility Adaptation: Introduce the ATR indicator to dynamically adjust stop loss and profit targets, so that the strategy can better adapt to different market volatility environments.
- Position optimization: The position size can be dynamically adjusted according to market volatility and signal strength to improve the efficiency of capital use.
- Time Filter: Add time filters to avoid trading during volatile periods such as market opening and closing.
- Market environment identification: Add a market environment judgment mechanism and adopt different parameter settings under different market conditions.
Summary
This is a trend tracking strategy with a reasonable design and clear logic. It can effectively capture market trends through the combination of EMA indicators and price behavior. The strategy has a relatively complete mechanism in risk control and revenue management, and provides multiple directions for optimization, with strong practical value and room for improvement. In the future, the stability and profitability of the strategy can be further improved by adding multi-period analysis and adjusting the stop loss mechanism.
Strategy source code
/*backtest
start: 2024-12-29 00:00:00
end: 2025-01-05 00:00:00
period: 30m
basePeriod: 30m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Demo GPT - PowerOfStocks 5EMA", overlay=true)
// Inputs
enableSL = input.bool(false, title="Enable Extra SL")
usl = input.int(defval=5, title="SL Distance in Points", minval=1, maxval=100)
riskRewardRatio = input.int(defval=3, title="Risk to Reward Ratio", minval=3, maxval=25)
showSell = input.bool(true, title="Show Sell Signals")
showBuy = input.bool(true, title="Show Buy Signals")
buySellExtraCond = input.bool(false, title="Buy/Sell with Extra Condition")
startDate = input(timestamp("2018-01-01 00:00"), title="Start Date")
endDate = input(timestamp("2069-12-31 23:59"), title="End Date")
// EMA Calculation
ema5 = ta.ema(close, 5)
// Plot EMA
plot(ema5, "EMA 5", color=color.new(#882626, 0), linewidth=2)
// Variables for Buy
var bool longTriggered = na
var float longStopLoss = na
var float longTarget = na
// Variables for Sell (used for signal visualization but no actual short trades)
var bool shortTriggered = na
var float shortStopLoss = na
var float shortTarget = na
// Long Entry Logic
if true
if (showBuy)
longCondition = high[1] < ema5[1] and high[1] < high and (not buySellExtraCond or close > close[1])
if (longCondition and not longTriggered)
entryPrice = high[1]
stopLoss = enableSL ? low[1] - usl * syminfo.mintick : low[1]
target = enableSL ? entryPrice + (entryPrice - stopLoss) * riskRewardRatio : high[1] + (high[1] - low[1]) * riskRewardRatio
// Execute Buy Order
strategy.entry("Buy", strategy.long, stop=entryPrice)
longTriggered := true
longStopLoss := stopLoss
longTarget := target
label.new(bar_index, entryPrice, text="Buy@ " + str.tostring(entryPrice), style=label.style_label_up, color=color.green, textcolor=color.white)
// Short Signal Logic (Visual Only)
if (true)
if (showSell)
shortCondition = low[1] > ema5[1] and low[1] > low and (not buySellExtraCond or close < close[1])
if (shortCondition and not shortTriggered)
entryPrice = low[1]
stopLoss = enableSL ? high[1] + usl * syminfo.mintick : high[1]
target = enableSL ? entryPrice - (stopLoss - entryPrice) * riskRewardRatio : low[1] - (high[1] - low[1]) * riskRewardRatio
// Visual Signals Only
label.new(bar_index, entryPrice, text="Sell@ " + str.tostring(entryPrice), style=label.style_label_down, color=color.red, textcolor=color.white)
shortTriggered := true
shortStopLoss := stopLoss
shortTarget := target
// Exit Logic for Buy
if longTriggered
// Stop-loss Hit
if low <= longStopLoss
strategy.close("Buy", comment="SL Hit")
longTriggered := false
// Target Hit
if high >= longTarget
strategy.close("Buy", comment="Target Hit")
longTriggered := false
// Exit Logic for Short (Signals Only)
if shortTriggered
// Stop-loss Hit
if high >= shortStopLoss
shortTriggered := false
// Target Hit
if low <= shortTarget
shortTriggered := false
Strategy Parameters
The original address: 5-Day EMA Based Trend Following Strategy Optimization Model