Главная
/
Примеры стратегий
Открыть на GitHub
Стратегия Ride Alligator
Реализация стратегии Ride Alligator. Используются три скользящие средние, известные как индикатор Alligator. Длинная позиция открывается, когда линия Губ пересекает линию Челюстей снизу и линия Зубов находится ниже Челюстей. Короткая позиция открывается при пересечении Губ сверху вниз при условии, что Зубы выше Челюстей. Открытая позиция защищается стопом по линии Челюстей, который перемещается вслед за ней.
Подробности
Условия входа :
Лонг: Lips > Jaws && Teeth < Jaws && previous Lips < previous Jaws
Шорт: Lips < Jaws && Teeth > Jaws && previous Lips > previous Jaws
Лонг/Шорт : Оба направления
Условия выхода :
Лонг: price <= Jaws
Шорт: price >= Jaws
Стопы : Подтягивающий стоп по линии Челюстей
Значения по умолчанию :
AlligatorPeriod = 5
MaType = MovingAverageTypeEnum.Weighted
CandleType = TimeSpan.FromMinutes(1).TimeFrame()
Фильтры :
Категория: Следование тренду
Направление: Обе стороны
Индикаторы: Alligator
Стопы: Да
Сложность: Базовая
Таймфрейм: Внутридневной
Сезонность: Нет
Нейросети: Нет
Дивергенция: Нет
Уровень риска: Средний
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>
/// Ride Alligator strategy using SMMA jaw/teeth/lips crossover.
/// </summary>
public class RideAlligatorStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private decimal _prevJaw;
private decimal _prevLips;
private bool _hasPrev;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public RideAlligatorStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevJaw = 0;
_prevLips = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var jaw = new SmoothedMovingAverage { Length = 13 };
var teeth = new SmoothedMovingAverage { Length = 8 };
var lips = new SmoothedMovingAverage { Length = 5 };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(jaw, teeth, lips, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal jaw, decimal teeth, decimal lips)
{
if (candle.State != CandleStates.Finished)
return;
if (!_hasPrev)
{
_prevJaw = jaw;
_prevLips = lips;
_hasPrev = true;
return;
}
// Lips crosses above jaw -> buy
if (_prevLips <= _prevJaw && lips > jaw && teeth < jaw)
{
if (Position < 0)
BuyMarket();
if (Position <= 0)
BuyMarket();
}
// Lips crosses below jaw -> sell
else if (_prevLips >= _prevJaw && lips < jaw && teeth > jaw)
{
if (Position > 0)
SellMarket();
if (Position >= 0)
SellMarket();
}
// Exit on price crossing jaw
if (Position > 0 && candle.ClosePrice < jaw)
SellMarket();
else if (Position < 0 && candle.ClosePrice > jaw)
BuyMarket();
_prevJaw = jaw;
_prevLips = lips;
}
}
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 SmoothedMovingAverage
from StockSharp.Algo.Strategies import Strategy
class ride_alligator_strategy(Strategy):
def __init__(self):
super(ride_alligator_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_jaw = 0.0
self._prev_lips = 0.0
self._has_prev = False
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(ride_alligator_strategy, self).OnReseted()
self._prev_jaw = 0.0
self._prev_lips = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(ride_alligator_strategy, self).OnStarted2(time)
jaw = SmoothedMovingAverage()
jaw.Length = 13
teeth = SmoothedMovingAverage()
teeth.Length = 8
lips = SmoothedMovingAverage()
lips.Length = 5
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(jaw, teeth, lips, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, jaw, teeth, lips):
if candle.State != CandleStates.Finished:
return
if not self._has_prev:
self._prev_jaw = jaw
self._prev_lips = lips
self._has_prev = True
return
# Lips crosses above jaw -> buy
if self._prev_lips <= self._prev_jaw and lips > jaw and teeth < jaw:
if self.Position < 0:
self.BuyMarket()
if self.Position <= 0:
self.BuyMarket()
# Lips crosses below jaw -> sell
elif self._prev_lips >= self._prev_jaw and lips < jaw and teeth > jaw:
if self.Position > 0:
self.SellMarket()
if self.Position >= 0:
self.SellMarket()
# Exit on price crossing jaw
if self.Position > 0 and candle.ClosePrice < jaw:
self.SellMarket()
elif self.Position < 0 and candle.ClosePrice > jaw:
self.BuyMarket()
self._prev_jaw = jaw
self._prev_lips = lips
def CreateClone(self):
return ride_alligator_strategy()