using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Sidus Alligator strategy using triple EMA alignment.
/// </summary>
public class SidusAlligatorStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _jawPeriod;
private readonly StrategyParam<int> _teethPeriod;
private readonly StrategyParam<int> _lipsPeriod;
private decimal? _prevLips;
private decimal? _prevTeeth;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int JawPeriod
{
get => _jawPeriod.Value;
set => _jawPeriod.Value = value;
}
public int TeethPeriod
{
get => _teethPeriod.Value;
set => _teethPeriod.Value = value;
}
public int LipsPeriod
{
get => _lipsPeriod.Value;
set => _lipsPeriod.Value = value;
}
public SidusAlligatorStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Timeframe", "General");
_jawPeriod = Param(nameof(JawPeriod), 50)
.SetGreaterThanZero()
.SetDisplay("Jaw Period", "Slow EMA (Jaw)", "Indicators");
_teethPeriod = Param(nameof(TeethPeriod), 25)
.SetGreaterThanZero()
.SetDisplay("Teeth Period", "Medium EMA (Teeth)", "Indicators");
_lipsPeriod = Param(nameof(LipsPeriod), 10)
.SetGreaterThanZero()
.SetDisplay("Lips Period", "Fast EMA (Lips)", "Indicators");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevLips = null;
_prevTeeth = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevLips = null;
_prevTeeth = null;
var jaw = new ExponentialMovingAverage { Length = JawPeriod };
var teeth = new ExponentialMovingAverage { Length = TeethPeriod };
var lips = new ExponentialMovingAverage { Length = LipsPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(jaw, teeth, lips, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, jaw);
DrawIndicator(area, teeth);
DrawIndicator(area, lips);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal jawVal, decimal teethVal, decimal lipsVal)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
{
_prevLips = lipsVal;
_prevTeeth = teethVal;
return;
}
if (_prevLips == null || _prevTeeth == null)
{
_prevLips = lipsVal;
_prevTeeth = teethVal;
return;
}
// Lips crosses above teeth with alligator aligned (lips > teeth > jaw)
if (_prevLips.Value <= _prevTeeth.Value && lipsVal > teethVal && lipsVal > jawVal)
{
if (Position < 0)
BuyMarket();
if (Position <= 0)
BuyMarket();
}
// Lips crosses below teeth with alligator aligned (lips < teeth < jaw)
else if (_prevLips.Value >= _prevTeeth.Value && lipsVal < teethVal && lipsVal < jawVal)
{
if (Position > 0)
SellMarket();
if (Position >= 0)
SellMarket();
}
_prevLips = lipsVal;
_prevTeeth = teethVal;
}
}
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class sidus_alligator_strategy(Strategy):
def __init__(self):
super(sidus_alligator_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Timeframe", "General")
self._jaw_period = self.Param("JawPeriod", 50) \
.SetDisplay("Jaw Period", "Slow EMA (Jaw)", "Indicators")
self._teeth_period = self.Param("TeethPeriod", 25) \
.SetDisplay("Teeth Period", "Medium EMA (Teeth)", "Indicators")
self._lips_period = self.Param("LipsPeriod", 10) \
.SetDisplay("Lips Period", "Fast EMA (Lips)", "Indicators")
self._prev_lips = None
self._prev_teeth = None
@property
def CandleType(self):
return self._candle_type.Value
@property
def JawPeriod(self):
return self._jaw_period.Value
@property
def TeethPeriod(self):
return self._teeth_period.Value
@property
def LipsPeriod(self):
return self._lips_period.Value
def OnReseted(self):
super(sidus_alligator_strategy, self).OnReseted()
self._prev_lips = None
self._prev_teeth = None
def OnStarted2(self, time):
super(sidus_alligator_strategy, self).OnStarted2(time)
self._prev_lips = None
self._prev_teeth = None
jaw = ExponentialMovingAverage()
jaw.Length = self.JawPeriod
teeth = ExponentialMovingAverage()
teeth.Length = self.TeethPeriod
lips = ExponentialMovingAverage()
lips.Length = self.LipsPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(jaw, teeth, lips, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, jaw)
self.DrawIndicator(area, teeth)
self.DrawIndicator(area, lips)
self.DrawOwnTrades(area)
def _on_process(self, candle, jaw_value, teeth_value, lips_value):
if candle.State != CandleStates.Finished:
return
jv = float(jaw_value)
tv = float(teeth_value)
lv = float(lips_value)
if self._prev_lips is None or self._prev_teeth is None:
self._prev_lips = lv
self._prev_teeth = tv
return
# Lips crosses above teeth with alligator aligned (lips > teeth > jaw)
if self._prev_lips <= self._prev_teeth and lv > tv and lv > jv:
if self.Position < 0:
self.BuyMarket()
if self.Position <= 0:
self.BuyMarket()
# Lips crosses below teeth with alligator aligned (lips < teeth < jaw)
elif self._prev_lips >= self._prev_teeth and lv < tv and lv < jv:
if self.Position > 0:
self.SellMarket()
if self.Position >= 0:
self.SellMarket()
self._prev_lips = lv
self._prev_teeth = tv
def CreateClone(self):
return sidus_alligator_strategy()