0bps

Strategy vault / Trend following

ZeroLag MACD Long Short Strategy

This article introduces a long-short strategy based on the ZeroLag MACD indicator. The strategy uses an optimized ZeroLag MACD indicator to generate buy and sell signals, enabling automated trading on the Bitcoin…

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

IMG

####Overview This article introduces a long-short strategy based on the ZeroLag MACD indicator. The strategy uses an optimized ZeroLag MACD indicator to generate buy and sell signals, enabling automated trading on the Bitcoin USDT 1-hour chart. The strategy code is optimized by Albert Callisto (AC) to improve the profitability and stability of the strategy.

####Strategy Principle The core of this strategy is the ZeroLag MACD indicator, which generates trading signals by calculating the difference between the fast moving average and the slow moving average. The ZeroLag MACD indicator is an improved version of the traditional MACD indicator, designed to eliminate the lag effect and enhance its sensitivity and timeliness.

Specifically, the strategy first calculates the fast moving average (default: 12 periods) and the slow moving average (default: 26 periods). Then, it uses these two moving averages to calculate the two components of the ZeroLag MACD indicator: zerolagEMA and zerolagslowMA. The difference between these two components gives the value of the ZeroLag MACD indicator. Finally, it calculates the signal line (default: 9 periods) of the ZeroLag MACD indicator, which is used to generate buy and sell signals.

When the ZeroLag MACD indicator crosses above the signal line, the strategy generates a buy signal; when the ZeroLag MACD indicator crosses below the signal line, the strategy generates a sell signal. This way, the strategy can automatically perform long and short trades based on changes in the market trend.

####Strategy Advantages

  1. Eliminates lag effect: The ZeroLag MACD indicator improves upon the traditional MACD indicator, effectively eliminating the lag effect and enhancing its sensitivity and timeliness, allowing it to reflect changes in market trends more quickly.

  2. High adaptability: The strategy can adapt to different market conditions and trading instruments by adjusting parameters (such as fast moving average period, slow moving average period, and signal line period), offering strong adaptability and flexibility.

  3. Automated trading: Based on clear trading rules, the strategy enables fully automated trading, reducing the risk of human intervention and improving trading efficiency.

  4. Risk control: The strategy uses moving averages and the MACD indicator to generate trading signals, which help identify market trends and control risks. Furthermore, appropriate position management and stop-loss measures can further reduce the strategy's risk.

####Strategy Risks

  1. Parameter optimization risk: The performance of the strategy depends on the choice of parameters, and inappropriate parameter settings may lead to poor performance. Therefore, it is necessary to conduct thorough backtesting and optimization to find the best parameter combination.

  2. Market risk: The cryptocurrency market is highly volatile and influenced by various factors, exposing the strategy to uncontrollable market risks. Moreover, unexpected events (such as policy changes, black swan events, etc.) may significantly impact the strategy's performance.

  3. Overfitting risk: If the strategy parameters are over-optimized, it may lead to overfitting of historical data, resulting in poor performance in actual trading. Therefore, appropriate methods (such as out-of-sample testing, cross-validation, etc.) should be used during backtesting and optimization to avoid overfitting.

  4. Liquidity risk: In case of insufficient market liquidity, the strategy may not be able to execute trades in a timely manner or at favorable prices, affecting its performance. Therefore, it is necessary to choose trading instruments with good liquidity and set reasonable slippage and trading volume limits.

####Strategy Optimization Directions

  1. Dynamic parameter optimization: Consider using machine learning and other methods to achieve dynamic optimization of strategy parameters, adapting to constantly changing market conditions. This can improve the adaptability and robustness of the strategy.

  2. Multi-factor combination: Combine the ZeroLag MACD indicator with other technical indicators (such as RSI, Bollinger Bands, etc.) to form a multi-factor composite signal, improving the reliability and profitability of the strategy.

  3. Risk management optimization: Introduce more advanced risk management measures, such as dynamic stop-loss and volatility adjustment, to better control the risk exposure of the strategy.

  4. Incorporate market sentiment analysis: Combine market sentiment analysis (such as fear and greed index, social media sentiment, etc.) to filter and optimize the signals generated by the strategy, improving its adaptability and robustness.

####Summary This article introduces a long-short strategy based on the ZeroLag MACD indicator, which uses an optimized ZeroLag MACD indicator to generate buy and sell signals for automated trading on the Bitcoin USDT 1-hour chart. The strategy has advantages such as eliminating lag effect, high adaptability, automated trading, and risk control, while also facing challenges such as parameter optimization, market risk, overfitting, and liquidity risk. To further improve the strategy's performance, it can be optimized in aspects such as dynamic parameter optimization, multi-factor combination, risk management optimization, and market sentiment analysis.

Parameters

NameDefaultWhat it controls
v_input_112Fast MM period
v_input_226Slow MM period
v_input_39Signal MM period
v_input_49MACD EMA period
v_input_5trueUse EMA (otherwise SMA)
v_input_6falseUse Glaz algo (otherwise 'real' original zero lag)

Source Pine Script

/*backtest
start: 2024-03-18 00:00:00
end: 2024-04-17 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Zero Lag MACD Strategy", shorttitle="ZL_MACD Strategy", overlay=true)

// Input variables
fastLength = input(12, title="Fast MM period", minval=1)
slowLength = input(26, title="Slow MM period", minval=1)
signalLength = input(9, title="Signal MM period", minval=1)
MacdEmaLength = input(9, title="MACD EMA period", minval=1)
useEma = input(true, title="Use EMA (otherwise SMA)")
useOldAlgo = input(false, title="Use Glaz algo (otherwise 'real' original zero lag)")

// Calculate Zero Lag MACD components
ma1 = useEma ? ema(close, fastLength) : sma(close, fastLength) 
ma2 = useEma ? ema(ma1, fastLength) : sma(ma1, fastLength) 
zerolagEMA = ((2 * ma1) - ma2)

mas1 = useEma ? ema(close, slowLength) : sma(close, slowLength)
mas2 = useEma ? ema(mas1, slowLength) : sma(mas1, slowLength)
zerolagslowMA = ((2 * mas1) - mas2)

ZeroLagMACD = zerolagEMA - zerolagslowMA 

emasig1 = ema(ZeroLagMACD, signalLength)
emasig2 = ema(emasig1, signalLength)
signal = useOldAlgo ? sma(ZeroLagMACD, signalLength) : (2 * emasig1) - emasig2

// Generate buy and sell signals
buySignal = crossover(ZeroLagMACD, signal)
sellSignal = crossunder(ZeroLagMACD, signal)

// Strategy conditions
if (buySignal)
    strategy.entry("Buy", strategy.long)
if (sellSignal)
    strategy.entry("Sell", strategy.short)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-04-18. 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/ZeroLag-MACD-Long-Short-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