Tags:

This strategy designs a simple trend following trading system based on linear regression line and moving average line. It goes long when the linear regression line crosses above the moving average and goes short when the linear regression line crosses below. Meanwhile, it uses the slope of the regression line to filter some trading signals and only enters when the trend direction matches.

Trend Following Regression Trading Strategy

The key components of this strategy include:

- Calculate N-day simple moving average (SMA)
- Calculate linear regression line of recent N days
- Go long when close price crosses above SMA and is higher than regression line
- Go short when close price crosses below SMA and is lower than regression line
- Set stop loss price and take profit price

The linear regression line can fit the trend direction well in recent periods. It can help judge the overall trend direction. When price breaks through the SMA line, we need to further determine whether the direction of the linear regression line is consistent with this breakout. Only when the two directions are consistent, a trading signal is generated. This can filter out some false breakouts.

In addition, the strategy also sets a stop loss mechanism. When the price hits the stop loss line, close positions to stop loss. It also sets a take profit line to lock in some profits.

The strategy has the following advantages:

- Combine trend indicator and breakout indicator to avoid false breakout and improve signal quality
- Use linear regression to determine trend direction for trend filtering, only go long in uptrend and go short in downtrend
- Set stop loss and take profit to control risk
- Clear and easy to understand rules
- Only few parameters to tune without too much complexity

The strategy also has some risks:

- More wrong trading signals may be generated in range-bound market
- The settings of moving average and regression periods need extensive testing and optimization, improper settings may affect strategy performance
- Stop loss may be broken in extreme market conditions resulting in large losses
- Based solely on technical indicators without combining fundamental factors

Regarding these risks, we can optimize from the following aspects:

- Consider suspending the strategy or using other indicators for filtering in range-bound market
- Backtest extensively to find the optimal parameters
- Optimize and dynamically adjust stop loss position
- Combine economic data and other fundamental factors

The main aspects to optimize the strategy further include:

- Add other auxiliary indicators to judge market conditions and avoid trading in range-bound periods
- Optimize moving average type, such as dual moving average, triple moving averages etc.
- Further analyze the slope of the regression line and add slope judgment rules
- Incorporate volatility indicators to set dynamic stop loss and take profit levels
- Utilize machine learning methods to automatically optimize parameters

This strategy integrates the trend following function of moving averages and the trend judging capability of linear regression, forming a relatively simple trend following trading system. It can achieve good results in strong trending markets. We still need extensive backtesting and optimization on the parameters and rules, and proper risk control. Then this strategy should be able to obtain steady investment returns.

/*backtest start: 2023-11-17 00:00:00 end: 2023-12-05 00:00:00 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy(title="Regression Trading Strategy", shorttitle="RTS", overlay=true) // Input parameters n = input(14, title="SMA Period") stop_loss_percentage = input(2, title="Stop Loss Percentage") take_profit_percentage = input(2, title="Take Profit Percentage") // Calculate the SMA sma = sma(close, n) // Linear regression function linear_regression(src, length) => sumX = 0.0 sumY = 0.0 sumXY = 0.0 sumX2 = 0.0 for i = 0 to length - 1 sumX := sumX + i sumY := sumY + src[i] sumXY := sumXY + i * src[i] sumX2 := sumX2 + i * i slope = (length * sumXY - sumX * sumY) / (length * sumX2 - sumX * sumX) intercept = (sumY - slope * sumX) / length line = slope * length + intercept line // Calculate the linear regression regression_line = linear_regression(close, n) // Plot the SMA and regression line plot(sma, title="SMA", color=color.blue) plot(regression_line, title="Regression Line", color=color.red) // Trading strategy conditions long_condition = crossover(close, sma) and close > regression_line short_condition = crossunder(close, sma) and close < regression_line // Exit conditions stop_loss_price = close * (1 - stop_loss_percentage / 100) take_profit_price = close * (1 + take_profit_percentage / 100) // Plot entry and exit points on the chart plotshape(series=long_condition, title="Long Entry", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small) plotshape(series=short_condition, title="Short Entry", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small) plotshape(series=crossunder(close, stop_loss_price), title="Stop Loss", location=location.abovebar, color=color.red, style=shape.labeldown, text="SL") plotshape(series=crossover(close, take_profit_price), title="Take Profit", location=location.belowbar, color=color.green, style=shape.labelup, text="TP") // Strategy orders strategy.entry("Long", strategy.long, when = long_condition) strategy.entry("Short", strategy.short, when = short_condition) strategy.exit("Exit", from_entry = "Long", when = crossover(close, stop_loss_price) or crossover(close, take_profit_price)) strategy.exit("Exit", from_entry = "Short", when = crossunder(close, stop_loss_price) or crossunder(close, take_profit_price))template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6