0bps

Strategy vault / Trend following

Dual EMA Golden Cross Long Line Strategy

The Dual EMA Golden Cross Long Line strategy is a trend tracking strategy that only opens long positions. The strategy uses three moving averages, short-term EMA, medium-term EMA and long-term EMA. The specific entry…

Author
ChaoZhang
Complexity
Intermediate · 69 lines
Parameters
9
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

The Dual EMA Golden Cross Long Line strategy is a trend tracking strategy that only opens long positions. The strategy uses three moving averages, short-term EMA, medium-term EMA and long-term EMA. The specific entry rule is: open long when the price is above the long-term EMA and the short-term EMA crosses above the medium-term EMA to form a golden cross.

Strategy Logic

  1. Calculate short-term EMA, medium-term EMA and long-term EMA using three EMA periods that are configurable.

  2. If the price is above the long-term EMA, it proves that it is currently in a bullish trend.

  3. If the short-term EMA crosses above the medium-term EMA from below to form a golden cross, it further proves that the bullish trend is strengthening.

  4. When both conditions above are met at the same time, open long.

Advantage Analysis

The biggest advantage of this strategy is that it can effectively identify trends by using multi-period EMAs combined judgment to avoid being misled by short-term market noise. At the same time, stop loss is configured as a risk control means to keep losses within a certain range.

Risk Analysis

The main risk of this strategy is the long position. When the market reverses, it is unable to close positions in time, leading to the risk of expanding losses. In addition, improper EMAs period setting can also lead to frequent trading and increase transaction costs.

Optimization Direction

  1. Increase position sizing management to reduce positions when drawdowns reach a certain percentage.

  2. Increase stop loss settings when breaking new highs.

  3. Optimize EMAs period parameters to reduce trading frequency.

Summary

This strategy is overall a stable high-quality long-term holding strategy. It has strong ability to identify trends with proper risk control. With further optimization, it is expected to obtain better stable returns.

Parameters

NameDefaultWhat it controls
v_input_bool_1trueUsa Filtro di Volatilità
v_input_int_114Periodi ATR
v_input_float_11.5Moltiplicatore ATR
v_input_bool_2trueUsa Trailing Stop
v_input_float_215Percentuale Trailing Stop
v_input_bool_3trueUsa Uscita EMA Corta incrocia EMA Media al Ribasso
v_input_int_2200Periodo EMA Lungo Termine
v_input_int_35Periodo EMA Breve Termine
v_input_int_410Periodo EMA Medio Termine

Source Pine Script

/*backtest
start: 2024-01-01 00:00:00
end: 2024-01-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Strategia EMA Long con Opzioni di Uscita Avanzate e Periodi EMA Personalizzabili", overlay=true)

// Parametri di input generali
useVolatilityFilter = input.bool(true, title="Usa Filtro di Volatilità")
atrPeriods = input.int(14, title="Periodi ATR", minval=1)
atrMultiplier = input.float(1.5, title="Moltiplicatore ATR", step=0.1)
useTrailingStop = input.bool(true, title="Usa Trailing Stop")
trailingStopPercent = input.float(15.0, title="Percentuale Trailing Stop", minval=0.1, step=0.1) / 100.0
useEMAExit = input.bool(true, title="Usa Uscita EMA Corta incrocia EMA Media al Ribasso")

// Parametri di input per periodi EMA personalizzabili
emaLongTermPeriod = input.int(200, title="Periodo EMA Lungo Termine", minval=1)
emaShortTermPeriod = input.int(5, title="Periodo EMA Breve Termine", minval=1)
emaMidTermPeriod = input.int(10, title="Periodo EMA Medio Termine", minval=1)

// Calcolo delle EMA con periodi personalizzabili
longTermEMA = ta.ema(close, emaLongTermPeriod)
shortTermEMA = ta.ema(close, emaShortTermPeriod)
midTermEMA = ta.ema(close, emaMidTermPeriod)

// Calcolo ATR e soglia di volatilità
atr = ta.atr(atrPeriods)
atrThreshold = ta.sma(atr, atrPeriods) * atrMultiplier

// Condizione di entrata
enterLongCondition = close > longTermEMA and shortTermEMA > midTermEMA
enterLong = useVolatilityFilter ? (enterLongCondition and atr > atrThreshold) : enterLongCondition

if (enterLong)
    strategy.entry("Enter Long", strategy.long)

// Tracking del prezzo di entrata e del massimo prezzo raggiunto per il trailing stop
var float entryPrice = na
var float maxPriceSinceEntry = na
if (strategy.position_size > 0)
    maxPriceSinceEntry := math.max(na(maxPriceSinceEntry) ? high : maxPriceSinceEntry, high)
    entryPrice := na(entryPrice) ? strategy.position_avg_price : entryPrice
else
    maxPriceSinceEntry := na
    entryPrice := na

// Calcolo del valore del trailing stop
trailStopPrice = maxPriceSinceEntry * (1 - trailingStopPercent)

// Implementazione delle condizioni di uscita
exitCrossUnder = close < longTermEMA
emaCross = ta.crossunder(shortTermEMA, midTermEMA)

if (useEMAExit and emaCross)
    strategy.close("Enter Long", comment="EMA Cross Exit")

if (useTrailingStop)
    strategy.exit("Trailing Stop", from_entry="Enter Long", stop=trailStopPrice)

// Visualizzazioni
plot(longTermEMA, color=color.yellow, title="EMA Lungo Termine")
plot(shortTermEMA, color=color.blue, title="EMA Breve Termine")
plot(midTermEMA, color=color.green, title="EMA Medio Termine")
plot(useVolatilityFilter ? atrThreshold : na, color=color.purple, title="ATR Threshold")
plot(strategy.position_size > 0 ? trailStopPrice : na, color=color.orange, title="Trailing Stop Value", style=plot.style_linebr)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-23. 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/Dual-EMA-Golden-Cross-Long-Line-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