Strategy vault / Trend following
EMA Trading Strategy
EMA Trading Strategy This strategy trades based on EMA analysis, with the following rules: - Enter long if previous day's close is above EMA - Exit long if current candle closes below EMA Advantages of this strategy…
EMA Trading Strategy
This strategy trades based on EMA analysis, with the following rules:
Enter long if previous day's close is above EMA
Exit long if current candle closes below EMA
Advantages of this strategy:
- Uses EMA to determine trend direction
- Simple and clear rules, easy to implement
- Customizable EMA period for optimization
Potential issues:
- Prone to false signals during range-bound markets
- Late entry, risks being caught in whipsaws
- No stop loss, risks uncontrolled losses
- No trade frequency or position sizing rules
Overall, the EMA strategy works better in trending markets but should be used cautiously. Adding stops and filters would help optimize the strategy.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 21 | Length |
| v_input_2 | 2000 | Year |
Source Pine Script
/*backtest
start: 2023-01-01 00:00:00
end: 2023-09-10 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ericdwyang
//@version=5
strategy("EMA Strat", overlay=true, margin_long=100, margin_short=100)
// EMA Variables
emaInput = input(21, "Length")
ema = ta.ema(close, emaInput)
// Variable Declaration
p = 0
start = false
// Start Date
yearInput = input(2000, "Year")
if (time >= timestamp(2000,01,01,01,01))
start := true
// Check first candle relative to EMA
if (close > ema and start == true)
p += 1
strategy.entry("Long", strategy.long, comment = "Entry")
// Candle close above EMA (p + 1, count reset to 0)
above = close[1] > ema[1]
if (above)
p += 1
// Candle close below EMA (reset p to 0, count -1)
below = close < ema
if (below)
p := 0
strategy.close("Long", comment = "Flat")
// // Exit Position
// if (redCount == -2)
// strategy.close("Long", comment = "Flat")
// goLong = p[1] == 0 and p == 1
// flatten = p == 0
// // Restablish long
// if (goLong and start == true)
// strategy.entry("Long", strategy.long, comment = "Entry")
plot(p)
plot(ema)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-11. 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-Trading-Strategy.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