Linear Regression Slope V1
Uses the slope of a linear regression and its shifted copy to detect trend changes.
Details
- Data: Price candles.
- Entry:
- Buy when the slope crosses below its shifted value.
- Sell when the slope crosses above its shifted value.
- Exit: Opposite signal closes the position.
- Instruments: Any instruments.
- Risk: No built-in stop or target.
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy using the slope of a linear regression with a shifted trigger line.
/// </summary>
public class LinearRegressionSlopeV1Strategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _length;
private readonly StrategyParam<int> _triggerShift;
private readonly StrategyParam<decimal> _stopLossPct;
private readonly StrategyParam<decimal> _takeProfitPct;
private decimal[] _slopeHistory = Array.Empty<decimal>();
private int _filled;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int Length { get => _length.Value; set => _length.Value = value; }
public int TriggerShift { get => _triggerShift.Value; set => _triggerShift.Value = value; }
public decimal StopLossPct { get => _stopLossPct.Value; set => _stopLossPct.Value = value; }
public decimal TakeProfitPct { get => _takeProfitPct.Value; set => _takeProfitPct.Value = value; }
public LinearRegressionSlopeV1Strategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
_length = Param(nameof(Length), 12)
.SetGreaterThanZero()
.SetDisplay("Length", "Bars for regression", "Parameters");
_triggerShift = Param(nameof(TriggerShift), 1)
.SetGreaterThanZero()
.SetDisplay("Trigger Shift", "Lag for trigger line", "Parameters");
_stopLossPct = Param(nameof(StopLossPct), 2m)
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");
_takeProfitPct = Param(nameof(TakeProfitPct), 3m)
.SetDisplay("Take Profit %", "Take profit percentage", "Risk");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_slopeHistory = Array.Empty<decimal>();
_filled = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_slopeHistory = new decimal[TriggerShift + 3];
_filled = 0;
var slope = new LinearReg { Length = Length };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(slope, ProcessCandle)
.Start();
StartProtection(
takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
useMarketOrders: true);
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, slope);
DrawOwnTrades(area);
}
}
private void Shift(decimal value)
{
for (var i = 0; i < _slopeHistory.Length - 1; i++)
_slopeHistory[i] = _slopeHistory[i + 1];
_slopeHistory[^1] = value;
if (_filled < _slopeHistory.Length)
_filled++;
}
private void ProcessCandle(ICandleMessage candle, decimal slopeVal)
{
if (candle.State != CandleStates.Finished)
return;
Shift(slopeVal);
if (_filled < _slopeHistory.Length)
return;
var s2 = _slopeHistory[_slopeHistory.Length - 3];
var s1 = _slopeHistory[_slopeHistory.Length - 2];
var t2 = _slopeHistory[0];
var t1 = _slopeHistory[1];
if (s2 > t2)
{
if (Position < 0)
BuyMarket();
if (s1 <= t1 && Position <= 0)
BuyMarket();
}
else if (t2 > s2)
{
if (Position > 0)
SellMarket();
if (t1 <= s1 && Position >= 0)
SellMarket();
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Indicators import LinearReg
from StockSharp.Algo.Strategies import Strategy
class linear_regression_slope_v1_strategy(Strategy):
def __init__(self):
super(linear_regression_slope_v1_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._length = self.Param("Length", 12) \
.SetDisplay("Length", "Bars for regression", "Parameters")
self._trigger_shift = self.Param("TriggerShift", 1) \
.SetDisplay("Trigger Shift", "Lag for trigger line", "Parameters")
self._stop_loss_pct = self.Param("StopLossPct", 2.0) \
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
self._take_profit_pct = self.Param("TakeProfitPct", 3.0) \
.SetDisplay("Take Profit %", "Take profit percentage", "Risk")
self._slope_history = []
self._filled = 0
@property
def candle_type(self):
return self._candle_type.Value
@property
def length(self):
return self._length.Value
@property
def trigger_shift(self):
return self._trigger_shift.Value
@property
def stop_loss_pct(self):
return self._stop_loss_pct.Value
@property
def take_profit_pct(self):
return self._take_profit_pct.Value
def OnReseted(self):
super(linear_regression_slope_v1_strategy, self).OnReseted()
self._slope_history = []
self._filled = 0
def OnStarted2(self, time):
super(linear_regression_slope_v1_strategy, self).OnStarted2(time)
max_len = int(self.trigger_shift) + 3
self._slope_history = [0.0] * max_len
self._filled = 0
slope = LinearReg()
slope.Length = self.length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(slope, self.process_candle).Start()
self.StartProtection(
takeProfit=Unit(self.take_profit_pct, UnitTypes.Percent),
stopLoss=Unit(self.stop_loss_pct, UnitTypes.Percent),
useMarketOrders=True)
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, slope)
self.DrawOwnTrades(area)
def _shift(self, value):
for i in range(len(self._slope_history) - 1):
self._slope_history[i] = self._slope_history[i + 1]
self._slope_history[-1] = value
if self._filled < len(self._slope_history):
self._filled += 1
def process_candle(self, candle, slope_val):
if candle.State != CandleStates.Finished:
return
slope_val = float(slope_val)
self._shift(slope_val)
if self._filled < len(self._slope_history):
return
n = len(self._slope_history)
s2 = self._slope_history[n - 3]
s1 = self._slope_history[n - 2]
t2 = self._slope_history[0]
t1 = self._slope_history[1]
if s2 > t2:
if self.Position < 0:
self.BuyMarket()
if s1 <= t1 and self.Position <= 0:
self.BuyMarket()
elif t2 > s2:
if self.Position > 0:
self.SellMarket()
if t1 <= s1 and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return linear_regression_slope_v1_strategy()