0bps

Strategy vault / Momentum

All About Momentum Trading Strategy with Stop Loss for Gold

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…

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

IMG

Overview

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 unifiedJudgment 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

Solutions:

  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

Conclusion

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.

Parameters

NameDefaultWhat it controls
v_input_int_121EMA Length

Source Pine Script

/*backtest
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"}]
*/

//@version=5
strategy("GC Momentum Strategy with Stoploss and Limits", overlay=true)

// Input for the length of the EMA
ema_length = input.int(21, 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)
    strategy.close("Long")
if (exit_short_condition)
    strategy.close("Short")
if (stop_loss_long)
    strategy.close("Long")
if (stop_loss_short)
    strategy.close("Short")

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

// Optional: Plot zero lines for reference
hline(0.5, "Upper Threshold", color=color.red)
hline(-0.5, "Lower Threshold", color=color.green)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-20. 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/All-About-Momentum-Trading-Strategy-with-Stop-Loss-for-Gold.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