0bps

Strategy vault / Mean reversion

Long Term Trend Reversal Strategy

The long-term trend reversal strategy is a mechanical trading system that combines trend following and short-term reversals. It uses the 7-day high and low to build a channel and the 200-day moving average to…

Author
ChaoZhang
Complexity
Starter · 54 lines
Parameters
3
Backtest bar
45m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

The long-term trend reversal strategy is a mechanical trading system that combines trend following and short-term reversals. It uses the 7-day high and low to build a channel and the 200-day moving average to determine the long-term trend direction. In a bull market, it buys on dips and sells in uptrends. In a bear market, it sells on rallies and buys on dips.

Strategy Logic

The strategy is mainly based on the following principles:

  1. Use the 7-day high-low to judge the rise and fall over the past week.

  2. The 200-day moving average determines the long-term trend direction.

  3. When the price breaks below the 7-day low and is above the 200-day moving average, a buy signal is generated. This indicates the end of the short-term downside correction and the trend may reverse upwards.

  4. When the price breaks above the 7-day high and is below the 200-day moving average, a sell signal is generated. This indicates the end of the short-term upside correction and the trend may reverse downwards.

  5. Use 2x ATR stop loss to control risk per trade.

The key is to consider both short-term and long-term timeframes. The 7-day channel judges recent price action and the 200-day MA judges the multi-month trend. Trading signals are only generated when both indicate the same direction. This avoids false signals from short-term corrections.

Advantage Analysis

The main advantages of this strategy are:

  1. Simple and clear signals based on price and moving average. Easy to implement.

  2. Considers both short-term and long-term trends, filters noise effectively.

  3. Trend following and mean reversion combined smooths returns.

  4. ATR stop loss controls risk, smaller max drawdown.

  5. Applicable to stocks, forex, crypto across markets.

  6. Can run in high and low frequency environments.

Risk Analysis

The main risks are:

  1. May miss large trends in strong trending markets.

  2. Stop loss may trigger frequently in choppy markets.

  3. Improper parameters may lead to over-trading.

  4. Improper short and long term trend metrics may filter too many signals.

  5. Model failure due to out of sample data.

The main risk management techniques:

  1. Optimize parameters for reasonable stop loss and trade frequency.

  2. Robust backtesting across markets and time frames.

  3. Portfolio diversification to lower single strategy risk.

  4. Exponential stop loss to limit loss per trade.

Optimization Directions

The strategy can be improved on:

  1. Optimize channel length for better short-term trend metric.

  2. Optimize MA length for better long-term trend metric.

  3. Try other stop loss techniques like percentage, trailing.

  4. Add volume condition. Trend reversals often see increase in volume.

  5. Machine learning to find optimal short and long term parameters.

  6. Dynamic exit rules based on fundamentals and sentiment.

  7. Optimize stop loss for exponential or profit locking algorithms.

Parameter optimization and combinations can further improve returns and risk metrics.

Summary

The long term trend reversal strategy combines both trend following and mean reversion. By judging both short-term and long-term trends, it generates signals at trend reversal points. Compared to pure trend or mean reversion strategies, it filters out market noise and achieves steady returns and risk control. Overall, this strategy suits algo traders with market views and provides stable performance for quantitative portfolios. With ongoing optimization and risk management, further improvements in risk-return are possible.

Parameters

NameDefaultWhat it controls
v_input_17Channel Length
v_input_2200Ma Length
v_input_32ATR Stop Loss

Source Pine Script

/*backtest
start: 2023-11-05 00:00:00
end: 2023-11-12 00:00:00
period: 45m
basePeriod: 5m
exchanges: [{"eid":"Futures_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/
// © racer8
//@version=4
// This Algo Strategy Has Only 3 rules and 62% Win Rate (Youtube) 

strategy("Trend Bounce", overlay=true)

nn = input(7,"Channel Length")
hi = highest(high,nn)
lo = lowest(low,nn)
n2 = input(200,"Ma Length")
ma = sma(close,n2)

if close>ma and close<lo[1]
    strategy.entry("Buy",strategy.long)
if close>hi[1]
    strategy.close("Buy") 
    
if close<ma and close>hi[1]
    strategy.entry("Sell",strategy.short)
if close<lo[1]
    strategy.close("Sell")


plot(hi,"high",color=color.aqua)
plot(lo,"low",color=color.aqua)
plot(ma,"sma",color=color.yellow)       

//-----------------------------------------Stop Loss-------------------------------------------------------

atr = sma(tr,10)[1]
bought = strategy.position_size[0] > strategy.position_size[1]
sold = strategy.position_size[0] < strategy.position_size[1]
slm = input(2.0,"ATR Stop Loss",minval=0)
StopPrice_Long  = strategy.position_avg_price - slm*atr              // determines stop loss's price 
StopPrice_Short  = strategy.position_avg_price + slm*atr              // determines stop loss's price 
FixedStopPrice_Long = valuewhen(bought,StopPrice_Long,0)                  // stores original StopPrice  
FixedStopPrice_Short = valuewhen(sold,StopPrice_Short,0)                  // stores original StopPrice  
plot(FixedStopPrice_Long,"ATR Stop Loss Long",color=color.blue,linewidth=1,style=plot.style_cross)
plot(FixedStopPrice_Short,"ATR Stop Loss Short",color=color.red,linewidth=1,style=plot.style_cross)
if strategy.position_size > 0
    strategy.exit(id="Stop", stop=FixedStopPrice_Long)    // commands stop loss order to exit!
if strategy.position_size < 0
    strategy.exit(id="Stop", stop=FixedStopPrice_Short)    // commands stop loss order to exit!

Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-13. 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/Long-Term-Trend-Reversal-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