首页
/
策略示例
在 GitHub 上查看
Bill Williams Alligator 策略
本策略将 MetaTrader 5 专家顾问 “Bill Williams.mq5” (作者 Vladimir Karputov)完整移植到 StockSharp 高层 API。系统订阅单一 K 线序列,重建 Bill Williams 分形,并将其与向前平移的 Alligator 三条线(下颌、牙齿、嘴唇)进行比较。当最新完成的 K 线收盘价突破最近的向上或向下分形,且该分形位于所有 Alligator 线之外时,策略会开仓。额外的风控参数覆盖原始 EA 中的设置,包括止损、止盈、移动止损、信号反向以及强制平掉相反仓位。
交易逻辑
分形检测 :每根完成的 K 线都会更新最高价和最低价的滚动缓冲区,最多回溯 FractalsLookback 根已完成的 K 线,寻找最近一次确认的向上/向下分形(五根 K 线模式)。
Alligator 复现 :用 (High + Low) / 2 的中间价驱动三条 SmoothedMovingAverage 指标,分别代表下颌、牙齿和嘴唇,并按照参数要求向前平移指定的 K 线数量,与 MetaTrader 的绘制方式保持一致。
突破确认 :做多信号要求最新向上分形高于所有三条 Alligator 线,并且当前 K 线收盘价高于该分形。做空逻辑相反,需要价格跌破向下分形且分形位于三条线之下。
下单执行 :默认情况下,当出现突破并且没有持仓时,策略以 OrderVolume 的仓位开市价单。如果启用 CloseOppositePositions,会在开新仓前先平掉反向仓位。将 ReverseSignals 设为 true 可实现原始 EA 的信号反向模式。
风险管理 :止损和止盈价格在内部跟踪,并在每根 K 线上进行验证。移动止损在浮动盈利达到 TrailingStopPips + TrailingStepPips 后启动,并随着价格推进按步进调整。所有距离都使用根据交易标的 PriceStep 计算的“点数”,自动兼容 MetaTrader 常见的 3/5 位小数报价。
参数
名称
说明
默认值
OrderVolume
入场手数(或合约数)。
0.1
StopLossPips
初始止损距离(点)。设为 0 表示不使用止损。
50
TakeProfitPips
止盈距离(点)。设为 0 表示不使用止盈。
50
TrailingStopPips
移动止损距离(点)。0 表示禁用移动止损。
10
TrailingStepPips
每次移动止损前需要额外获得的点数;启用移动止损时必须为正。
5
JawPeriod
Alligator 下颌线的平滑移动平均周期。
13
JawShift
下颌线向前平移的 K 线数。
8
TeethPeriod
Alligator 牙齿线的平滑移动平均周期。
8
TeethShift
牙齿线向前平移的 K 线数。
5
LipsPeriod
Alligator 嘴唇线的平滑移动平均周期。
5
LipsShift
嘴唇线向前平移的 K 线数。
3
FractalsLookback
回溯的完成 K 线数量,用于寻找最近的分形。
100
ReverseSignals
true 时,向下分形触发买入,向上分形触发卖出。
false
CloseOppositePositions
true 时,在开新仓前先平掉反向仓位。
false
CandleType
用于计算和生成信号的 K 线类型。
TimeFrame(1h)
说明
策略仅处理 已完成的 K 线 ,忽略盘中 tick,完全遵循原始 EA 的逐 K 线逻辑。
为了兼容 MetaTrader 的 3/5 位报价,若 Security.Decimals 为 3 或 5,则将 PriceStep 乘以 10 以得到“点”大小。
止损、止盈和移动止损均由策略内部管理;若下一根 K 线触及触发价,则直接通过市价单平仓。
如果图表区域可用,策略会自动绘制 K 线和 Alligator 指标,方便与 MetaTrader 模板对比。
根据仓库约定,本次转换不包含 Python 版本和测试项目。
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;
public class BillWilliamsAlligatorStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _stopLossPoints;
private readonly StrategyParam<int> _takeProfitPoints;
private ExponentialMovingAverage _fast;
private ExponentialMovingAverage _slow;
private decimal _prevFast;
private decimal _prevSlow;
private decimal _entryPrice;
private int _cooldown;
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }
public BillWilliamsAlligatorStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
}
protected override void OnReseted()
{
base.OnReseted();
_fast = null; _slow = null;
_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_fast = new ExponentialMovingAverage { Length = FastPeriod };
_slow = new ExponentialMovingAverage { Length = SlowPeriod };
var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
subscription.Bind(_fast, _slow, ProcessCandle);
subscription.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }
var close = candle.ClosePrice;
var step = Security?.PriceStep ?? 1m;
if (Position > 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
}
else if (Position < 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
}
if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }
_prevFast = fastValue; _prevSlow = slowValue;
}
}
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 bill_williams_alligator_strategy(Strategy):
def __init__(self):
super(bill_williams_alligator_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 14) \
.SetDisplay("Fast Period", "Fast MA period", "Indicator")
self._slow_period = self.Param("SlowPeriod", 50) \
.SetDisplay("Slow Period", "Slow MA period", "Indicator")
self._stop_loss_points = self.Param("StopLossPoints", 200) \
.SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 400) \
.SetDisplay("Take Profit", "Take-profit in price steps", "Risk")
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def stop_loss_points(self):
return self._stop_loss_points.Value
@property
def take_profit_points(self):
return self._take_profit_points.Value
def OnReseted(self):
super(bill_williams_alligator_strategy, self).OnReseted()
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(bill_williams_alligator_strategy, self).OnStarted2(time)
self._fast = ExponentialMovingAverage()
self._fast.Length = self.fast_period
self._slow = ExponentialMovingAverage()
self._slow.Length = self.slow_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(5)))
subscription.Bind(self._fast, self._slow, self._process_candle)
subscription.Start()
def _process_candle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fast_val = float(fast_value)
slow_val = float(slow_value)
if not self._fast.IsFormed or not self._slow.IsFormed:
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_fast = fast_val
self._prev_slow = slow_val
return
close = float(candle.ClosePrice)
step = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 1.0
if self.Position > 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close <= self._entry_price - self.stop_loss_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close >= self._entry_price + self.take_profit_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
elif self.Position < 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close >= self._entry_price + self.stop_loss_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close <= self._entry_price - self.take_profit_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._prev_fast <= self._prev_slow and fast_val > slow_val and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = close
self._cooldown = 100
elif self._prev_fast >= self._prev_slow and fast_val < slow_val and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = close
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
def CreateClone(self):
return bill_williams_alligator_strategy()