Function Matrix Library
Demonstrates multiple linear regression using basic matrix algebra. The strategy computes coefficients for two explanatory variables without generating trades.
Details
- Entry Criteria: None
- Long/Short: None
- Exit Criteria: None
- Stops: No
- Default Values:
Lookback= 20Length1= 10Length2= 20Candle Type= 5-minute
- Filters:
- Category: Indicator showcase
- Direction: None
- Indicators: SMA
- Stops: No
- Complexity: Medium
- Timeframe: Any
- Seasonality: No
- Neural networks: No
- Divergence: No
- Risk level: Low
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>
/// Uses a simple two-factor matrix-style model based on fast/slow SMA values.
/// </summary>
public class FunctionMatrixLibraryStrategy : Strategy
{
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<decimal> _entryThresholdPercent;
private readonly StrategyParam<int> _signalCooldownBars;
private readonly StrategyParam<DataType> _candleType;
private SimpleMovingAverage _fastSma;
private SimpleMovingAverage _slowSma;
private int _barsFromSignal;
/// <summary>
/// Fast SMA length.
/// </summary>
public int FastLength
{
get => _fastLength.Value;
set => _fastLength.Value = value;
}
/// <summary>
/// Slow SMA length.
/// </summary>
public int SlowLength
{
get => _slowLength.Value;
set => _slowLength.Value = value;
}
/// <summary>
/// Minimum percent edge required to open or reverse a position.
/// </summary>
public decimal EntryThresholdPercent
{
get => _entryThresholdPercent.Value;
set => _entryThresholdPercent.Value = value;
}
/// <summary>
/// Minimum bars between market entries.
/// </summary>
public int SignalCooldownBars
{
get => _signalCooldownBars.Value;
set => _signalCooldownBars.Value = value;
}
/// <summary>
/// Candle type used for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of the strategy.
/// </summary>
public FunctionMatrixLibraryStrategy()
{
_fastLength = Param(nameof(FastLength), 12)
.SetGreaterThanZero()
.SetDisplay("Fast Length", "Fast SMA length", "General");
_slowLength = Param(nameof(SlowLength), 48)
.SetGreaterThanZero()
.SetDisplay("Slow Length", "Slow SMA length", "General");
_entryThresholdPercent = Param(nameof(EntryThresholdPercent), 0.25m)
.SetGreaterThanZero()
.SetDisplay("Entry Threshold %", "Required model edge in percent", "General");
_signalCooldownBars = Param(nameof(SignalCooldownBars), 12)
.SetGreaterThanZero()
.SetDisplay("Signal Cooldown Bars", "Minimum bars between entries", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(10).TimeFrame())
.SetDisplay("Candle Type", "Candles timeframe", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_fastSma = null;
_slowSma = null;
_barsFromSignal = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
StartProtection(null, null);
_fastSma = new() { Length = FastLength };
_slowSma = new() { Length = SlowLength };
_barsFromSignal = SignalCooldownBars;
var subscription = SubscribeCandles(CandleType);
subscription.Bind(_fastSma, _slowSma, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (!_fastSma.IsFormed || !_slowSma.IsFormed)
return;
var close = candle.ClosePrice;
if (close <= 0m)
return;
_barsFromSignal++;
if (_barsFromSignal < SignalCooldownBars)
return;
// Weighted linear combination emulates a compact matrix regression output.
var modelPrice = (2m * fastValue + slowValue) / 3m;
var edgePercent = (modelPrice - close) / close * 100m;
if (edgePercent >= EntryThresholdPercent && Position <= 0)
{
BuyMarket();
_barsFromSignal = 0;
}
else if (edgePercent <= -EntryThresholdPercent && Position >= 0)
{
SellMarket();
_barsFromSignal = 0;
}
}
}
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
from StockSharp.Algo.Indicators import SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class function_matrix_library_strategy(Strategy):
"""
Function Matrix Library: two-factor SMA model strategy.
Weighted combination of fast/slow SMA vs close price.
Enters when model edge exceeds threshold.
"""
def __init__(self):
super(function_matrix_library_strategy, self).__init__()
self._fast_length = self.Param("FastLength", 12) \
.SetDisplay("Fast Length", "Fast SMA length", "General")
self._slow_length = self.Param("SlowLength", 48) \
.SetDisplay("Slow Length", "Slow SMA length", "General")
self._entry_threshold_percent = self.Param("EntryThresholdPercent", 0.25) \
.SetDisplay("Entry Threshold %", "Required model edge in percent", "General")
self._signal_cooldown_bars = self.Param("SignalCooldownBars", 12) \
.SetDisplay("Signal Cooldown Bars", "Minimum bars between entries", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(10))) \
.SetDisplay("Candle Type", "Candles timeframe", "General")
self._bars_from_signal = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(function_matrix_library_strategy, self).OnReseted()
self._bars_from_signal = 0
def OnStarted2(self, time):
super(function_matrix_library_strategy, self).OnStarted2(time)
self._bars_from_signal = self._signal_cooldown_bars.Value
self._fast_sma = SimpleMovingAverage()
self._fast_sma.Length = self._fast_length.Value
self._slow_sma = SimpleMovingAverage()
self._slow_sma.Length = self._slow_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._fast_sma, self._slow_sma, self._process_candle).Start()
def _process_candle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
if not self._fast_sma.IsFormed or not self._slow_sma.IsFormed:
return
close = float(candle.ClosePrice)
if close <= 0:
return
fast = float(fast_value)
slow = float(slow_value)
self._bars_from_signal += 1
if self._bars_from_signal < self._signal_cooldown_bars.Value:
return
model_price = (2.0 * fast + slow) / 3.0
edge_percent = (model_price - close) / close * 100.0
threshold = self._entry_threshold_percent.Value
if edge_percent >= threshold and self.Position <= 0:
self.BuyMarket()
self._bars_from_signal = 0
elif edge_percent <= -threshold and self.Position >= 0:
self.SellMarket()
self._bars_from_signal = 0
def CreateClone(self):
return function_matrix_library_strategy()