0bps

Strategy vault / Trend following

Exponential Moving Average Crossover Strategy

The exponential moving average crossover strategy is a simple quantitative trading strategy that tracks price trends. It uses crosses of two exponential moving averages with different parameter settings as buy and…

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

IMG

Overview

The exponential moving average crossover strategy is a simple quantitative trading strategy that tracks price trends. It uses crosses of two exponential moving averages with different parameter settings as buy and sell signals. When the short-term EMA crosses above the long-term EMA, a buy signal is generated. When the short-term EMA crosses below the long-term EMA, a sell signal is generated.

Strategy Logic

The core logic of this strategy is based on the EMA theory. Exponential moving averages can effectively smooth price fluctuations and determine the direction of the price trend. The fast EMA responds quickly to price changes while the slow EMA provides a reference for the price trend direction. When the fast EMA crosses above the slow EMA, it indicates that prices have started to rise and a buy signal is generated. When the fast EMA crosses below the slow EMA, it indicates that prices have started to fall and a sell signal is generated.

Specifically, this strategy first defines two exponential moving averages: fib_level and fib_price. fib_level is set by user input, and fib_price is calculated based on the highest and lowest prices of the most recent 100 bars. When the close price crosses above or below fib_price, buy and sell signals are generated, respectively. At the same time, the stop loss is set to the highest and lowest prices of the most recent 10 bars.

Advantage Analysis

  • Utilize dual EMA system to determine price trend direction and avoid wrong signals
  • Customizable strategy with user-defined parameters
  • Setting stop loss is beneficial for risk control

Risk Analysis

  • EMA lag may miss price reversal points
  • Frequent EMA crosses increase transaction costs and slippage losses
  • Improper stop loss setting may cause premature stop loss or excessive losses

Risks can be reduced by optimizing EMA parameters, using triple EMA system, or combining with other indicators for signal confirmation. Also loosen the stop loss appropriately to prevent excessive early stop outs.

Optimization Directions

This strategy can be optimized from the following aspects:

  1. Optimize EMA period parameters. Test different period combinations to find the best parameters.

  2. Add Volume and other filters. Generate buy signals when Volume rises and sell signals when Volume falls to avoid wrong signals during sharp price spikes.

  3. Utilize machine learning algorithms to automatically optimize parameters based on historical data.

  4. Add trailing stop mechanism to stop loss placement. Move up stop loss line with increased profits to prevent premature stop out.

Summary

The exponential moving average crossover strategy is an easy-to-use quantitative trading strategy overall. It leverages the strengths of EMAs to determine price trends and sets stops to control risks. The strategy is easy to understand, flexible in parameters, and applicable for quantitative trading across different products. Further optimizations in parameter tuning, additional filters, and trailing stops can lead to even better strategy performance.

Parameters

NameDefaultWhat it controls
v_input_10.5Fibonacci Level

Source Pine Script

/*backtest
start: 2023-12-08 00:00:00
end: 2024-01-07 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Fibonacci Strategy", overlay=true)

// Define Fibonacci 0.5 level
fib_level = input(0.5, title="Fibonacci Level")

// Calculate Fibonacci 0.5 level price
fib_price = ta.lowest(low, 100) + (ta.highest(high, 100) - ta.lowest(low, 100)) * fib_level

// Define entry and exit conditions
long_condition = ta.crossover(close, fib_price)
short_condition = ta.crossunder(close, fib_price)

// Set exit points (using previous high or low)
long_exit = ta.highest(high, 10)
short_exit = ta.lowest(low, 10)

// Plot Fibonacci 0.5 level
plot(fib_price, "Fib 0.5", color=color.blue, linewidth=1, style=plot.style_circles)

// Initialize variables
var inLong = false
var inShort = false

// Set trading signals
if (long_condition)
    if not inLong
        strategy.entry("Buy", strategy.long)
        inLong := true
    strategy.exit("Exit", "Buy", limit=long_exit)

if (short_condition)
    if not inShort
        strategy.entry("Sell", strategy.short)
        inShort := true
    strategy.exit("Exit", "Sell", limit=short_exit)

if (ta.crossover(close, long_exit) or ta.crossunder(close, short_exit))
    inLong := false
    inShort := false

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-08. 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/Exponential-Moving-Average-Crossover-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