#### Dublin_Capital

##### Member

I thought it might be interesting to share a very basic Linear Regression Trading Strategy, and then see if we can build on it to improve results.

Here is the basic strategy - Using a 50 period Linear Regression Curve on an hourly chart, we enter / exit trades using the following rules:

Backtesting for 360 trading days, the results of the strategy are as follows on a portfolio of commonly traded futures instruments. Although profitable, the P&L curve is not ideal, commissions too high, and the win rate could be improved. The majority of the profits come during a small fraction of the overall trading.

Used just as is, this strategy would be a good supplement to a long-only portfolio of stocks, as it seems to keep it's head above water during normal markets, but delivers strong returns during higher volatility (early 2020). That said, I would like to see if we can turn it into a stronger stand-alone strategy.

A linear regression channel is a representation of trend direction and volatility. Buying / selling above and below the middle line doesn't even really make sense, as price is expected to travel up and down within the channel. I guess my main point with all of this is that a profitable trading strategy can be based on almost anything.

I will be taking suggestions and adding / adjusting the strategy in an effort to improve these metrics.

Here is the basic strategy - Using a 50 period Linear Regression Curve on an hourly chart, we enter / exit trades using the following rules:

- Long Entry: The Linear Regression Curve is rising AND the close of the previous bar is above the Linear Regression Curve
- Long Exit: The close of the previous bar is less than the Linear Regression Curve
- Short Entry: The Linear Regression Curve is falling AND the close of the previous bar is below the Linear Regression Curve
- Short Exit: The close of the previous bar is greater than the Linear Regression Curve

Code:

```
#DS_LinRegStrategy
#Basic framework for a Linear Regression based strategy
#@Dublin_Capital
#2020-07-03
input price = close;
input displace = 0;
input LinRegLength = 50;
#Definitions
def LinReg = Inertia(price[-displace], LinRegLength);
#Defining Instrument Fundamentals & P&L for labels
def tickval = TickValue();
def ticksize = TickSize();
def FloatPL = FPL();
#Defining Long/Short Filters (these instructions determine entries / exits)
#Entry / Exit Requirements
def Long1 = LinReg > LinReg[1]
and close > LinReg;
def ExitLong1 = close < LinReg;
def Short1 = LinReg < LinReg[1]
and close < LinReg;
def ExitShort1 = close > LinReg[1];
#Order Entry (Set 1)
AddOrder(OrderType.BUY_TO_OPEN, Long1, tickcolor = Color.DARK_GREEN, arrowcolor = Color.DARK_GREEN, name = "LONG1");
AddOrder(OrderType.SELL_TO_CLOSE, ExitLong1, tickcolor = Color.DARK_GREEN, arrowcolor = Color.DARK_GREEN, name = "EXITLONG1");
AddOrder(OrderType.SELL_TO_OPEN, Short1, tickcolor = Color.DARK_RED, arrowcolor = Color.DARK_RED, name = "SHORT1");
AddOrder(OrderType.BUY_TO_CLOSE, ExitShort1, tickcolor = Color.DARK_RED, arrowcolor = Color.DARK_RED, name = "EXITSHORT1");
#Adding Linear Regression Plots
plot LinReg3 = LinReg;
LinReg3.SetDefaultColor(Color.BLUE);
#Adding Alerts
Alert(Long1, "Long", Alert.BAR, Sound.Ding);
Alert(Short1, "Short", Alert.BAR, Sound.Ding);
Alert(ExitLong1, "ExitLong", Alert.BAR, Sound.Ring);
Alert(ExitShort1, "ExitShort", Alert.BAR, Sound.Ring);
#Adding Labels
AddLabel(yes, "$" + tickval);
AddLabel(yes, ticksize);
AddLabel(yes, "$" + FloatPL, if FloatPL > 0 then Color.DARK_GREEN
else if FloatPL < 0 then Color.DARK_RED
else Color.GRAY);
#Coloring Bars
AssignPriceColor(if Long1 then Color.DARK_GREEN else if Short1 then Color.DARK_RED else Color.GRAY);
```

Used just as is, this strategy would be a good supplement to a long-only portfolio of stocks, as it seems to keep it's head above water during normal markets, but delivers strong returns during higher volatility (early 2020). That said, I would like to see if we can turn it into a stronger stand-alone strategy.

A linear regression channel is a representation of trend direction and volatility. Buying / selling above and below the middle line doesn't even really make sense, as price is expected to travel up and down within the channel. I guess my main point with all of this is that a profitable trading strategy can be based on almost anything.

I will be taking suggestions and adding / adjusting the strategy in an effort to improve these metrics.

Last edited: