Strategy vault / Trend following
EMA Crossover Strategy 431678
This strategy uses the crossover of fast EMA and slow EMA lines as buy and sell signals to implement automated trading based on EMA crossovers. The fast EMA line closely follows price action while the slow EMA line…

Overview
This strategy uses the crossover of fast EMA and slow EMA lines as buy and sell signals to implement automated trading based on EMA crossovers. The fast EMA line closely follows price action while the slow EMA line smooths price action. When the fast EMA line crosses above the slow EMA line from below, a buy signal is generated. When the fast EMA line crosses below the slow EMA line from above, a sell signal is generated. The strategy is flexible and customizable by adjusting the parameters of the fast and slow EMAs to define custom signal points for entries and exits.
Strategy Logic
The strategy mainly generates trading signals by calculating fast and slow EMA lines and comparing their relationship.
First, the period of the fast EMA emaFast is set to 1 in the input parameters so that it can closely follow price changes. At the same time, the periods of the slow EMAs are set - emaSlowBuy for generating buy signals and emaSlowSell for sell signals.
Then, the fast EMA and slow EMAs are calculated according to the input periods. The fast EMA has a fixed period of 1 to follow prices closely while the slow EMAs are adjustable parameters to smooth price data.
Next, the relationship between the fast EMA and slow EMAs is compared to determine crossovers. If the fast EMA crosses above the slow EMA, forming a golden cross, the buy condition is met. If the fast EMA crosses below the slow EMA, forming a death cross, the sell condition is met.
Finally, entry and exit orders are executed when the buy and sell conditions are met to complete trades. Meanwhile, it checks that the current time is within the backtest date range to avoid erroneous trades outside the date range.
Advantage Analysis
- Using EMA crossovers to determine entry and exit points is a mature and reliable technical indicator
- Adjustable fast and slow EMA periods allow parameters to be tuned to find optimal trading opportunities in different market conditions
- The logic of buying on golden crosses and selling on death crosses is straightforward and easy to understand
- Flexible configuration of buy and sell EMAs enables full customization of the trading strategy
- Options for long-only, short-only or two-way trading provides flexibility for different market environments
- Customizable backtest date range allows optimization testing on specific time periods
Risk Analysis
- EMA crossover signals have lag and may miss the optimal timing of price changes
- Frequent crossover signals may occur in volatile markets, leading to over-trading
- Extensive testing is required to find the optimal EMA combinations, otherwise excessive false signals may occur
- The fixed 1-period fast EMA cannot filter noise effectively during market shock events
- Sideways choppy markets may generate unnecessary trade signals
Possible enhancements to mitigate risks:
Add filters using other indicators to validate EMA crossover signals and avoid false signals
Adjust EMA periods based on market volatility to reduce trade frequency
Incorporate stop loss and take profit to control risk
Optimize the fast EMA period for better performance in specific market conditions
Add trend determination to avoid over-trading in ranging markets
Enhancement Opportunities
Some ways the strategy can be further optimized:
Optimize EMA parameters by testing different period combinations to find the optimal settings
Add filters using other indicators like MACD, KDJ, Bollinger Bands to validate signals
Incorporate trend metrics like ATR to avoid ranging markets
Optimize stop loss and take profit strategies for better risk and profitability
Test other EMA combinations like dual or triple EMAs to find better parameters
Adjust parameters dynamically for different market cycles like faster EMAs for trending and slower EMAs for choppy markets
Conclusion
The EMA crossover strategy has clear, easy-to-understand logic using established technical indicators to determine entries and exits. It is highly customizable via EMA parameter tuning for optimization across different market conditions. However, EMA signals have lag and extensive testing is required to find the best parameters. Additionally, further enhancements are needed to mitigate risks by adding signal filters, optimizing stops, and avoiding ranging markets. With continuous optimization and testing, this strategy has potential for strong trading performance.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 20 | Slow EMA for Buy Signals |
| v_input_2 | 30 | Slow EMA for Sell Signals |
| v_input_3 | 0 | Trade Direction: Both | Short | Long |
| v_input_4 | timestamp(01 Jan 2018 00:00) | Start Date |
| v_input_5 | timestamp(31 Dec 2025 23:59) | End Date |
Source Pine Script
/*backtest
start: 2023-10-10 00:00:00
end: 2023-11-09 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy(
"EMA Cross Strategy with Custom Buy/Sell Conditions",
overlay=true
)
// INPUT:
// Options to enter fast Exponential Moving Average (EMA) value
emaFast = 1
// Options to enter slow EMAs for buy and sell signals
slowEMABuy = input(title="Slow EMA for Buy Signals", defval=20, minval=1, maxval=9999)
slowEMASell = input(title="Slow EMA for Sell Signals", defval=30, minval=1, maxval=9999)
// Option to select trade directions
tradeDirection = input(title="Trade Direction", options=["Long", "Short", "Both"], defval="Both")
// Options that configure the backtest date range
startDate = input(title="Start Date", type=input.time, defval=timestamp("01 Jan 2018 00:00"))
endDate = input(title="End Date", type=input.time, defval=timestamp("31 Dec 2025 23:59"))
// CALCULATIONS:
// Use a fixed fast EMA of 1 and calculate slow EMAs for buy and sell signals
fastEMA = ema(close, emaFast)
slowEMABuyValue = ema(close, slowEMABuy)
slowEMASellValue = ema(close, slowEMASell)
// PLOT:
// Draw the EMA lines on the chart
plot(series=fastEMA, color=color.orange, linewidth=2)
plot(series=slowEMABuyValue, color=color.blue, linewidth=2, title="Slow EMA for Buy Signals")
plot(series=slowEMASellValue, color=color.red, linewidth=2, title="Slow EMA for Sell Signals")
// CONDITIONS:
// Check if the close time of the current bar falls inside the date range
inDateRange = true
// Translate input into trading conditions for buy and sell signals
buyCondition = crossunder(slowEMABuyValue, fastEMA)
sellCondition = crossover(slowEMASellValue, fastEMA)
// Translate input into overall trading conditions
longOK = (tradeDirection == "Long") or (tradeDirection == "Both")
shortOK = (tradeDirection == "Short") or (tradeDirection == "Both")
// ORDERS:
// Submit entry (or reverse) orders based on buy and sell conditions
if (buyCondition and inDateRange)
strategy.entry("Buy", strategy.long)
if (sellCondition and inDateRange)
strategy.close("Buy")
// Submit exit orders based on opposite trade conditions
if (strategy.position_size > 0 and sellCondition)
strategy.close("Sell")
if (strategy.position_size < 0 and buyCondition)
strategy.close("Sell")Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-10. 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.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/EMA-Crossover-Strategy-431678.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- 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
- 200 EMA VWAP MFI Trend Following StrategyPine Script · Trend following · 1d
- 4 EMA Trend StrategyPine Script · Trend following · 1d
- ADX MA and EMA Long Only Trend Tracking StrategyPine Script · Trend following · 1d
- ATR Dynamic Trend Following and EMA Crossover Trading StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- ATR and T3 Moving Average StrategyPine Script · Trend following · 5m