0bps

Strategy vault / Trend following

Bilinear Regression Trend Following Strategy

The Bilinear Regression Trend Following strategy uses the difference between fast and slow linear regression to determine price trends and uses it as entry signals. It goes long when the fast linear regression…

Author
ChaoZhang
Complexity
Intermediate · 40 lines
Parameters
11
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

The Bilinear Regression Trend Following strategy uses the difference between fast and slow linear regression to determine price trends and uses it as entry signals. It goes long when the fast linear regression crosses above the threshold and exits when it crosses below. It also uses EMA as a filter to only enter when price is above EMA.

Strategy Logic

The strategy first calculates two linear regression lines with different periods, one fast with shorter period and one slow with longer period. Then it calculates the difference between the two, when fast regression is above slow regression, the difference is positive, indicating an uptrend. When fast is below slow, the difference is negative, indicating a downtrend.

The strategy enters long when the difference line crosses above the threshold and exits when it crosses below. It also requires price to be above 200-period EMA to filter out non-trending moves.

Advantage Analysis

  1. Dual linear regression can capture price trends well.

  2. EMA filter eliminates some false signals from non-trending moves.

  3. Simple and clear logic, easy to understand and implement.

Risk Analysis

  1. Improper LR periods may generate excessive noise.

  2. EMA filter may miss opportunities in strong trends.

  3. Prone to whipsaws and losses in ranging markets.

Solutions:

  1. Optimize LR periods to reduce noise.

  2. Dynamically adjust EMA period based on market conditions.

  3. Add stop loss to control losses.

Optimization

The strategy can be optimized in the following aspects:

  1. Optimize fast and slow LR periods to find best combination.

  2. Try other filters like Bollinger Bands, KDJ instead of EMA.

  3. Add dynamic stop loss to control risks.

  4. Combine with stock picking to select trending stocks.

  5. Develop adaptive parameters based on market conditions.

Summary

The Bilinear Regression strategy is simple and direct in capturing trends with dual linear regression and EMA filter. But it also has risks that need to be addressed through parameter optimization, stop loss, etc. When properly tuned, it can effectively trade trending markets.

Parameters

NameDefaultWhat it controls
v_input_12017Start Year
v_input_212Month
v_input_317Day
v_input_49999End Year
v_input_5trueMonth
v_input_6trueDay
v_input_713Fast LR
v_input_855Slow LR
v_input_9falseLag for fast
v_input_10falseLag for slow
v_input_11falseThreshold

Source Pine Script

/*backtest
start: 2022-11-10 00:00:00
end: 2023-11-16 00:00:00
period: 1d
basePeriod: 1h
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/
// © TradingAmmo

//@version=4
strategy("Linear trend", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.075, currency='USD')
startP = timestamp(input(2017, "Start Year"), input(12, "Month"), input(17, "Day"), 0, 0)
end   = timestamp(input(9999, "End Year"),   input(1, "Month"),   input(1, "Day"),   0, 0)
_testPeriod() =>
    iff(time >= startP and time <= end, true, false)

src = close
len1 = input(defval=13, minval=1, title="Fast LR")
len2 = input(defval=55, minval=1, title="Slow LR")

lag1 = input(0, title="Lag for fast")
lag2 = input(0, title="Lag for slow")

threshold  = input(0,step=0.5, title="Threshold")

fast_lr = linreg(src, len1, lag1)
slow_lr = linreg(src, len2, lag2)
lr = fast_lr - slow_lr
plot_fast = plot(lr, color = lr > 0 ? color.green : color.red)
plot(threshold, color=color.purple)

long_condition = crossover(lr, threshold) and close > ema(close, 200) and _testPeriod()
strategy.entry('BUY', strategy.long, when=long_condition) 

short_condition = crossunder(lr, threshold) 
strategy.close('BUY', when=short_condition) 

Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-17. 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/Bilinear-Regression-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