Einfache Alligator-Strategie
Übersicht
Die einfache Alligator-Strategie recreiert den MetaTrader Expert Advisor "Alligator Simple v1.0" mithilfe der StockSharp High-Level-API. Sie liest den Bill Williams Alligator-Indikator auf abgeschlossenen Kerzen und eröffnet eine Position, wenn die Lips-, Teeth- und Jaw-Linien auf dem vorherigen abgeschlossenen Balken in dieselbe Richtung expandieren. Jeder Trade kann optional pip-basiertes Stop-Loss-, Take-Profit- und Trailing-Stop-Management umfassen, das die ursprüngliche MQL-Implementierung widerspiegelt.
Indikatoren und Daten
- Alligator-Linien: drei Smoothed Moving Averages (SMMA), berechnet auf dem Kerzenmittelpunkt
(high + low) / 2 mit konfigurierbaren Längen und Vorwärtsverschiebungen für Jaw, Teeth und Lips.
- Kerzen: die Strategie abonniert einen einzigen konfigurierbaren
CandleType (standardmäßig Einstunden-Kerzen) und verarbeitet nur abgeschlossene Kerzen, um Look-Ahead-Bias zu vermeiden.
Handelslogik
- Signalauswertung
- Die verschobenen Alligator-Werte für die vorherige abgeschlossene Kerze abrufen.
- Long-Signal:
Lips[t-1] > Teeth[t-1] > Jaw[t-1].
- Short-Signal:
Lips[t-1] < Teeth[t-1] < Jaw[t-1].
- Ausführung
- Mit
OrderVolume in den Markt einsteigen, wenn keine Position offen ist.
- Es wird nur eine Position gleichzeitig gehalten; entgegengesetzte Signale werden ignoriert, bis die aktuelle Position geschlossen ist.
Ausstieg und Risikomanagement
- Anfänglicher Stop-Loss: wenn
StopLossPips > 0, versetzt die Strategie den Ausführungspreis um die pip-basierte Distanz, konvertiert mit dem Preisschritt des Instruments (einschließlich des 3/5-stelligen Pip-Multiplikators, der von MetaTrader-Symbolen verwendet wird).
- Take-Profit: wenn
TakeProfitPips > 0, wird ein Gewinnziel symmetrisch um den Eintrittspreis platziert. Ein Nullwert deaktiviert das Ziel.
- Trailing Stop: wenn sowohl
TrailingStopPips als auch TrailingStepPips positiv sind, wird der Stop zu close − TrailingStop (Longs) oder close + TrailingStop (Shorts) vorgerückt, sobald sich der Preis mindestens TrailingStop + TrailingStep zugunsten des Trades bewegt hat. Trailing-Updates stützen sich auf das Kerzenhoch/-tief, um Intra-Bar-Berührungen zu simulieren.
- Ausstiegshandhabung: Stop-Loss-, Take-Profit- und Trailing-Bedingungen geben Market-Orders aus, um die Position zu schließen und werden bei jeder abgeschlossenen Kerze ausgewertet.
Parameter
OrderVolume (Standard 1): Handelsgröße in Lots oder Kontrakten.
StopLossPips (Standard 100): anfängliche Stop-Loss-Distanz in Pips. Auf null setzen zum Deaktivieren.
TakeProfitPips (Standard 100): Take-Profit-Distanz in Pips. Auf null setzen zum Deaktivieren.
TrailingStopPips (Standard 5): Trailing-Stop-Distanz in Pips. Null deaktiviert das Trailing.
TrailingStepPips (Standard 5): zusätzliche Pip-Distanz, die der Preis zurücklegen muss, bevor der Trailing Stop vorrückt. Muss positiv sein, wenn Trailing aktiviert ist.
JawPeriod, TeethPeriod, LipsPeriod: SMMA-Längen für Jaw, Teeth und Lips (Standardwerte 13/8/5).
JawShift, TeethShift, LipsShift: Vorwärtsverschiebungen beim Lesen von Alligator-Werten (Standardwerte 8/5/3).
CandleType: Kerzendatentyp/Zeitrahmen für Berechnungen (Standard Einstunden-Kerzen).
Implementierungshinweise
- Pip-Abstände passen sich automatisch an die Tick-Größe des Wertpapiers an. Instrumente mit drei oder fünf Dezimalstellen multiplizieren den Preisschritt mit zehn, um die MetaTrader-Pip-Definition zu replizieren.
- Indikator-History-Buffer speichern genügend Werte, um die konfigurierten Vorwärtsverschiebungen zu respektieren, und eliminieren die manuelle Array-Manipulation.
- Die Strategie verwendet
BuyMarket- und SellMarket-Helfer zum Einreichen von Orders und hält den Code auf die Signalgenerierung und Risikohandhabung fokussiert.
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>
/// Simplified Bill Williams Alligator breakout strategy.
/// Buys when Lips > Teeth > Jaw (upward expansion).
/// Sells when Lips less than Teeth less than Jaw (downward expansion).
/// Uses stop-loss and take-profit for risk management.
/// </summary>
public class AlligatorSimpleStrategy : Strategy
{
private readonly StrategyParam<int> _jawPeriod;
private readonly StrategyParam<int> _teethPeriod;
private readonly StrategyParam<int> _lipsPeriod;
private readonly StrategyParam<int> _stopLossPoints;
private readonly StrategyParam<int> _takeProfitPoints;
private SmoothedMovingAverage _jaw;
private SmoothedMovingAverage _teeth;
private SmoothedMovingAverage _lips;
private decimal _entryPrice;
private int _cooldown;
/// <summary>
/// Period for the Alligator jaw (blue) smoothed moving average.
/// </summary>
public int JawPeriod
{
get => _jawPeriod.Value;
set => _jawPeriod.Value = value;
}
/// <summary>
/// Period for the Alligator teeth (red) smoothed moving average.
/// </summary>
public int TeethPeriod
{
get => _teethPeriod.Value;
set => _teethPeriod.Value = value;
}
/// <summary>
/// Period for the Alligator lips (green) smoothed moving average.
/// </summary>
public int LipsPeriod
{
get => _lipsPeriod.Value;
set => _lipsPeriod.Value = value;
}
/// <summary>
/// Stop-loss distance in price steps.
/// </summary>
public int StopLossPoints
{
get => _stopLossPoints.Value;
set => _stopLossPoints.Value = value;
}
/// <summary>
/// Take-profit distance in price steps.
/// </summary>
public int TakeProfitPoints
{
get => _takeProfitPoints.Value;
set => _takeProfitPoints.Value = value;
}
/// <summary>
/// Initialize <see cref="AlligatorSimpleStrategy"/>.
/// </summary>
public AlligatorSimpleStrategy()
{
_jawPeriod = Param(nameof(JawPeriod), 13)
.SetGreaterThanZero()
.SetDisplay("Jaw Period", "Alligator jaw period", "Alligator");
_teethPeriod = Param(nameof(TeethPeriod), 8)
.SetGreaterThanZero()
.SetDisplay("Teeth Period", "Alligator teeth period", "Alligator");
_lipsPeriod = Param(nameof(LipsPeriod), 5)
.SetGreaterThanZero()
.SetDisplay("Lips Period", "Alligator lips period", "Alligator");
_stopLossPoints = Param(nameof(StopLossPoints), 200)
.SetNotNegative()
.SetDisplay("Stop Loss", "Stop-loss distance in price steps", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 400)
.SetNotNegative()
.SetDisplay("Take Profit", "Take-profit distance in price steps", "Risk");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_jaw = null;
_teeth = null;
_lips = null;
_entryPrice = 0;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_jaw = new SmoothedMovingAverage { Length = JawPeriod };
_teeth = new SmoothedMovingAverage { Length = TeethPeriod };
_lips = new SmoothedMovingAverage { Length = LipsPeriod };
var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
subscription.Bind(_jaw, _teeth, _lips, ProcessCandle);
subscription.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal jawValue, decimal teethValue, decimal lipsValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!_jaw.IsFormed || !_teeth.IsFormed || !_lips.IsFormed)
return;
if (_cooldown > 0)
{
_cooldown--;
return;
}
var close = candle.ClosePrice;
var step = Security?.PriceStep ?? 1m;
// Check SL/TP for existing positions
if (Position > 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step)
{
SellMarket();
_entryPrice = 0;
_cooldown = 110;
return;
}
if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step)
{
SellMarket();
_entryPrice = 0;
_cooldown = 110;
return;
}
}
else if (Position < 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step)
{
BuyMarket();
_entryPrice = 0;
_cooldown = 110;
return;
}
if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step)
{
BuyMarket();
_entryPrice = 0;
_cooldown = 110;
return;
}
}
// Buy when lips > teeth > jaw (Alligator opening upward)
if (lipsValue > teethValue && teethValue > jawValue && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
_entryPrice = close;
_cooldown = 110;
}
// Sell when lips < teeth < jaw (Alligator opening downward)
else if (lipsValue < teethValue && teethValue < jawValue && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
_entryPrice = close;
_cooldown = 110;
}
}
}
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 CandleStates
from StockSharp.Algo.Indicators import SmoothedMovingAverage
from StockSharp.Algo.Strategies import Strategy
from datatype_extensions import *
class alligator_simple_strategy(Strategy):
"""
Simplified Bill Williams Alligator breakout strategy.
Buys when Lips > Teeth > Jaw (upward expansion).
Sells when Lips < Teeth < Jaw (downward expansion).
Uses stop-loss and take-profit for risk management.
"""
def __init__(self):
super(alligator_simple_strategy, self).__init__()
self._jaw_period = self.Param("JawPeriod", 13) \
.SetGreaterThanZero() \
.SetDisplay("Jaw Period", "Alligator jaw period", "Alligator")
self._teeth_period = self.Param("TeethPeriod", 8) \
.SetGreaterThanZero() \
.SetDisplay("Teeth Period", "Alligator teeth period", "Alligator")
self._lips_period = self.Param("LipsPeriod", 5) \
.SetGreaterThanZero() \
.SetDisplay("Lips Period", "Alligator lips period", "Alligator")
self._stop_loss_points = self.Param("StopLossPoints", 200) \
.SetDisplay("Stop Loss", "Stop-loss distance in price steps", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 400) \
.SetDisplay("Take Profit", "Take-profit distance in price steps", "Risk")
self._entry_price = 0.0
self._cooldown = 0
@property
def JawPeriod(self):
return self._jaw_period.Value
@JawPeriod.setter
def JawPeriod(self, value):
self._jaw_period.Value = value
@property
def TeethPeriod(self):
return self._teeth_period.Value
@TeethPeriod.setter
def TeethPeriod(self, value):
self._teeth_period.Value = value
@property
def LipsPeriod(self):
return self._lips_period.Value
@LipsPeriod.setter
def LipsPeriod(self, value):
self._lips_period.Value = value
@property
def StopLossPoints(self):
return self._stop_loss_points.Value
@StopLossPoints.setter
def StopLossPoints(self, value):
self._stop_loss_points.Value = value
@property
def TakeProfitPoints(self):
return self._take_profit_points.Value
@TakeProfitPoints.setter
def TakeProfitPoints(self, value):
self._take_profit_points.Value = value
def OnReseted(self):
super(alligator_simple_strategy, self).OnReseted()
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(alligator_simple_strategy, self).OnStarted2(time)
jaw = SmoothedMovingAverage()
jaw.Length = self.JawPeriod
teeth = SmoothedMovingAverage()
teeth.Length = self.TeethPeriod
lips = SmoothedMovingAverage()
lips.Length = self.LipsPeriod
subscription = self.SubscribeCandles(tf(5))
subscription.Bind(jaw, teeth, lips, self.ProcessCandle).Start()
def ProcessCandle(self, candle, jaw_value, teeth_value, lips_value):
if candle.State != CandleStates.Finished:
return
if self._cooldown > 0:
self._cooldown -= 1
return
close = float(candle.ClosePrice)
step = 1.0
# Check SL/TP for existing positions
if self.Position > 0 and self._entry_price > 0:
if self.StopLossPoints > 0 and close <= self._entry_price - self.StopLossPoints * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 110
return
if self.TakeProfitPoints > 0 and close >= self._entry_price + self.TakeProfitPoints * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 110
return
elif self.Position < 0 and self._entry_price > 0:
if self.StopLossPoints > 0 and close >= self._entry_price + self.StopLossPoints * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 110
return
if self.TakeProfitPoints > 0 and close <= self._entry_price - self.TakeProfitPoints * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 110
return
# Buy when lips > teeth > jaw (Alligator opening upward)
if lips_value > teeth_value and teeth_value > jaw_value and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = close
self._cooldown = 110
# Sell when lips < teeth < jaw (Alligator opening downward)
elif lips_value < teeth_value and teeth_value < jaw_value and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = close
self._cooldown = 110
def CreateClone(self):
"""!! REQUIRED!! Creates a new instance of the strategy."""
return alligator_simple_strategy()