Linear Regression Trading System: Amibroker AFL Code

Quantitative Finance offers plethora of indicators and tools to predict future price movements of Stocks, commodities or any other traded instruments. Linear Regression is one of them through which price direction is speculated using statistical techniques. It has found it’s application in multiple proven trading systems used by hedge funds and large institutional investors. In this post, we will explore Linear regression trading system which is based on the principle of Mean reversion. We shall also understand the definition of linear regression and linear regression channel in simple terms.

Please visit Trading Tuitions Academy to learn AFL coding and create your own Trading systems.

Linear Regression line and Linear Regression channel

Linear Regression line is the straight line drawn through the price points in a chart, which most effectively covers all the prices for a given period. It is a kind of ‘best fit’ line which shows the fair value of trading instrument. Any deviation from the linear regression line is considered to be the deviation of price from it’s supposed to be fair value. It is like a trend line and gives an idea of ongoing price trend in the chart.

Linear Regression channel contains two extra lines above and below the standard linear regression line. These extra lines are formed by calculating 1 or 2 standard deviations of linear regression line. If 1 standard deviation is used then linear regression channel covers 68% of prices for a given period, while if 2 standard deviations are used then it covers 95% of prices. Hence linear regression channel can effectively determine overbought and oversold areas, which makes it a perfect tool to develop mean reversion strategies.

Below is the simple illustration for Linear regression channel.

Linear Regression Channel

Image Source: http://forextraininggroup.com/simple-yet-powerful-price-channel-techniques/#

Linear Regression Trading System – AFL Overview

Parameter Value
Preferred Time-frame
Indicators Used  Linear Regression, Standard Deviation
Buy Condition
  • First candle closes below the lower band of linear regression channel.
  • Second candle closes above the lower band.
  • Third candle closes above the second candle.
Short Condition
  • First candle closes above the upper band of linear regression channel.
  • Second candle closes below the upper band.
  • Third candle closes below the second candle.
Sell Condition
  • Same as Short
  • Stop Loss Hit
  • Target met
  • End of Trading day
Cover Condition
  • Same as Buy
  • Stop Loss Hit
  • Target met
  • End of Trading day
Stop Loss  0.5%
Targets  0.5%
Position Size  100% of Equity
Initial Equity  200000
Brokerage  100 per order
Margin  10%

Linear Regression Trading System– AFL Code

This AFL code was originally published at Marketcalls.in. We have modified it to make it suitable for intraday trading and added various backtesting conditions.

Click here to download AFL code for this system.

AFL Screenshot

Linear Regression Trading System

Also Read: Donchian Channel Trend following system

Linear Regression Trading System- Backtest Report

Linear Regression Trading system does not offer huge returns as it it based on mean reversion, but it is quite consistent owing to low drawdown value. It is suitable for intraday scalping.

Parameter Value
Initial Capital 200000
Final Capital 736893.90
Scrip Name NSE Nifty
Backtest Period 26-Feb-2008 to 30-Jun-2017
Timeframe Hourly
Net Profit % 268.45%
Annual Return % 14.71%
Number of Trades 136
Winning Trade % 55.88%
Average holding Period 3.05 periods
Max consecutive losses 4
Max system % drawdown -13.37%
Max Trade % drawdown -13.86%

Download the detailed backtest report here.

Equity Curve

The equity curve is smooth and stable.

Linear Regression- Equity Curve

Profit Table

Linear Regression-Profit Table

Additional Amibroker settings for backtesting

Goto Symbol–>Information, and specify the lot size and margin requirement. The below screenshot shows lot size of 75 and margin requirement of 10% for NSE Nifty:

Symbol Info_Nifty


All the AFL’s posted in this section are for learning purpose. Trading Tuitions does not necessarily own these AFL’s and we don’t have any intellectual property rights on them. We might copy useful AFL’s from public forums and post it in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at support@tradingtuitions.com

Related Posts


  1. sir is it continuous future or nifty spot? i suppose you have shown trend line and in Amibroker screen Bollinger look alike lines are shown. Is it possible …Can we have automatic trendline AFL? Back testing looks good though, was nifty lot size considered in it?

    • Hi Utkarsh,

      Backtesting was done on Nifty Spot. In the image we have shown Linear regression channel, they are not trendlines.

  2. yes but @admin AFL code and screenshot doesnt show up any straight line (linear regression channel) or can you share afl code for straight line(linear regression) channels draw?

  3. On this line:
    _N(Title = StrFormat(“{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}”, O, H, L, C ));

    I get Error 61 – #format specifiers not equal #args

    • Hi Sushil,

      Please comment out that line (add // in the beginning). It does not hold any logic of the strategy.

Leave a Reply

Your email address will not be published.