All About Momentum Trading Strategy with Stop Loss for Gold

Author: ChaoZhang, Date: 2024-02-20 16:27:18



This strategy calculates the deviation of gold price from its 21-day exponential moving average to determine overbought and oversold situations in the market. It adopts a momentum trading approach with stop loss mechanism to control risk when deviation reaches certain thresholds in terms of standard deviation.

Strategy Logic

  1. Calculate 21-day EMA as the baseline
  2. Compute deviation of price from EMA
  3. Standardize deviation into Z-Score
  4. Go long when Z-Score crosses over 0.5; Go short when Z-Score crosses below -0.5
  5. Close position when Z-Score falls back to 0.5/-0.5 threshold
  6. Set stop loss when Z-Score goes over 3 or below -3

Advantage Analysis

The advantages of this strategy are:

  1. EMA as dynamic support/resistance to capture trends
  2. Stddev and Z-Score effectively gauge overbought/oversold levels, reducing false signals
  3. Exponential EMA puts more weight on recent prices, making it more sensitive
  4. Z-Score standardizes deviation for unified判断 rules
  5. Stop loss mechanism controls risk and limits losses

Risk Analysis

Some risks to consider:

  1. EMA can generate wrong signals when price gaps or breaks out
  2. Stddev/Z-Score thresholds need proper tuning for best performance
  3. Improper stop loss setting could lead to unnecessary losses
  4. Black swan events may trigger stop loss and miss trend opportunity


  1. Optimize EMA parameter to identify major trends
  2. Backtest to find optimal Stddev/Z-Score thresholds
  3. Test stop loss rationality with trailing stops
  4. Reassess market post-event, adjust strategy accordingly

Optimization Directions

Some ways to improve the strategy:

  1. Use volatility indictors like ATR instead of simple Stddev to gauge risk appetite
  2. Test different types of moving averages for better baseline
  3. Optimize EMA parameter to find best period
  4. Optimize Z-Score thresholds for improved performance
  5. Add volatility-based stops for more intelligent risk control


Overall this is a solid trend following strategy. It uses EMA to define trend direction and standardized deviation to clearly identify overbought/oversold levels for trade signals. Reasonable stop loss controls risk while letting profits run. Further parameter tuning and adding conditions can make this strategy more robust for practical application.

start: 2024-01-20 00:00:00
end: 2024-02-19 00:00:00
period: 4h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

strategy("GC Momentum Strategy with Stoploss and Limits", overlay=true)

// Input for the length of the EMA
ema_length =, title="EMA Length", minval=1)

// Exponential function parameters
steepness = 2

// Calculate the EMA
ema = ta.ema(close, ema_length)

// Calculate the deviation of the close price from the EMA
deviation = close - ema

// Calculate the standard deviation of the deviation
std_dev = ta.stdev(deviation, ema_length)

// Calculate the Z-score
z_score = deviation / std_dev

// Long entry condition if Z-score crosses +0.5 and is below 3 standard deviations
long_condition = ta.crossover(z_score, 0.5)

// Short entry condition if Z-score crosses -0.5 and is above -3 standard deviations
short_condition = ta.crossunder(z_score, -0.5)

// Exit long position if Z-score converges below 0.5 from top
exit_long_condition = ta.crossunder(z_score, 0.5)

// Exit short position if Z-score converges above -0.5 from below
exit_short_condition = ta.crossover(z_score, -0.5)

// Stop loss condition if Z-score crosses above 3 or below -3
stop_loss_long = ta.crossover(z_score, 3)
stop_loss_short = ta.crossunder(z_score, -3)

// Enter and exit positions based on conditions
if (long_condition)
    strategy.entry("Long", strategy.long)
if (short_condition)
    strategy.entry("Short", strategy.short)
if (exit_long_condition)
if (exit_short_condition)
if (stop_loss_long)
if (stop_loss_short)

// Plot the Z-score on the chart
plot(z_score, title="Z-score",, linewidth=2)

// Optional: Plot zero lines for reference
hline(0.5, "Upper Threshold",
hline(-0.5, "Lower Threshold",