0bps

Strategy vault / Trend following

Price Channel and Moving Average Based Trend Following Strategy

This strategy identifies and follows trends by constructing a Price Channel to calculate the deviation of price from the middle line and using moving averages to filter signals. Trading signals are generated when…

Author
ChaoZhang
Complexity
Starter · 56 lines
Parameters
6
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

This strategy identifies and follows trends by constructing a Price Channel to calculate the deviation of price from the middle line and using moving averages to filter signals. Trading signals are generated when price breaks through the Channel. The strategy has both trend following and breakout characteristics.

Strategy Logic

  1. Build Price Channel
  • Calculate highest high and lowest low over the past len periods
  • Middle line is average of highest high and lowest low
  • Distance is absolute deviation of price from middle line
  • Smooth distance to get upper and lower bands
  1. Determine Trend Direction
  • When price is below lower band, define as downtrend
  • When price is above upper band, define as uptrend
  1. Generate Trading Signals
  • In uptrend, long when price is below open or breaks below upper band
  • In downtrend, short when price is above open or breaks above lower band

Advantage Analysis

  1. Captures mid- to long-term trends
  2. Combines with breakout signals to avoid ineffective trading in range-bound markets
  3. Customizable parameters suit different products

Risk Analysis

  1. More minor losses possible in oscillating trends
  2. Inappropriate parameter settings may miss trend reversals
  3. Monitor trade frequency to prevent overtrading

Optimization Directions

  1. Add other indicators to filter signals
  2. Dynamically adjust Price Channel parameters
  3. Incorporate stop loss to optimize risk management

Conclusion

The strategy is overall quite robust in tracking mid- to long-term trends effectively while generating trading signals through trend breakouts. Further improvements can be made through parameter optimization and signal filtering to adapt the strategy to more products and market environments.

Parameters

NameDefaultWhat it controls
v_input_1trueLong
v_input_2trueShort
v_input_320Period
v_input_4trueColor
v_input_5trueShow Bands
v_input_6trueShow Background

Source Pine Script

/*backtest
start: 2023-01-30 00:00:00
end: 2024-02-05 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/


//@version=2
strategy("Noro's Bands Strategy v1.1", shorttitle = "NoroBands str 1.1", overlay=true)

//Settings
needlong = input(true, defval = true, title = "Long")
needshort = input(true, defval = true, title = "Short")
len = input(20, defval = 20, minval = 2, maxval = 200, title = "Period")
color = input(true, "Color")
needbb = input(true, defval = false, title = "Show Bands")
needbg = input(true, defval = false, title = "Show Background")
src = close

//PriceChannel 1
lasthigh = highest(src, len)
lastlow = lowest(src, len)
center = (lasthigh + lastlow) / 2

//dist
dist = abs(src - center)
distsma = sma(dist, len)
hd = center + distsma
ld = center - distsma

//Trend
trend = close < ld and high < hd ? -1 : close > hd and low > ld ? 1 : trend[1]

//Lines
colo = needbb == false ? na : black
plot(hd, color = colo, linewidth = 1, transp = 0, title = "High band")
plot(center, color = colo, linewidth = 1, transp = 0, title = "center")
plot(ld, color = colo, linewidth = 1, transp = 0, title = "Low band")

//Background
col = needbg == false ? na : trend == 1 ? lime : red
bgcolor(col, transp = 90)

//Signals
up = trend == 1 and ((close < open or color == false) or close < hd) ? 1 : 0
dn = trend == -1 and ((close > open or color == false) or close > ld) ? 1 : 0 

longCondition = up == 1
if (longCondition)
    strategy.entry("Long", strategy.long, needlong == false ? 0 : na)

shortCondition = dn == 1
if (shortCondition)
    strategy.entry("Short", strategy.short, needshort == false ? 0 : na)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-06. 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/Price-Channel-and-Moving-Average-Based-Trend-Following-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