在 GitHub 上查看
Above Below MA 回归策略
概览
Above Below MA 策略是 MetaTrader 4 专家顾问 “AboveBelowMA” 的 StockSharp 版本。原始脚本在 15 分钟 GBP/USD 图表上运行,并使用典型价 (最高价 + 最低价 + 收盘价) / 3 计算 1 周期指数移动平均线(EMA)。当价格在 EMA 上下方偏离且均线斜率反向时,策略尝试顺着 EMA 的方向重新入场。本移植版本完全保留信号结构,并使用 StockSharp 的高级 API(SubscribeCandles + Bind)。
交易逻辑
- 订阅所选的蜡烛类型(默认 15 分钟),并将数据传递给基于典型价的 EMA 指标。
- 记录当前与上一根 EMA 值,用来判断 EMA 的方向。EMA 上升时寻找做多机会,下降时寻找做空机会。
- 做多条件: 当前蜡烛开盘价至少低于 EMA 一个最小价位单位,收盘价位于 EMA 下方,同时上一根 EMA 值低于当前值。若存在空头持仓则先平仓,仓位归零后提交市价买入。
- 做空条件: 当前蜡烛开盘价至少高于 EMA 一个最小价位单位,收盘价位于 EMA 上方,同时上一根 EMA 值高于当前值。若存在多头持仓则先平仓,仓位归零后提交市价卖出。
- 所有判断只发生在已完成的蜡烛上,以避免在未完成的柱体上提前触发信号。
仓位管理
- 原始 EA 使用
AccountFreeMargin / 10000(上限 5 手)来确定下单手数。StockSharp 版本提供等效机制:启用 UseDynamicVolume 后,将当前账户资产除以 BalanceToVolumeDivider(默认 10000)。
- 计算出的手数受到
MaxVolume 的限制,与 EA 的 5 手上限保持一致。若关闭动态手数,则使用 InitialVolume 作为固定下单量。
- 所有手数都会按照交易品种的最小手数步长以及最小/最大手数限制进行对齐,以避免因为手数不合规而被拒单。
参数
| 参数 |
说明 |
EmaLength |
指数移动平均线的周期(默认为 1,与 EA 相同)。 |
CandleType |
构建信号所使用的蜡烛类型(默认 15 分钟)。 |
InitialVolume |
关闭动态手数时所使用的固定下单量。 |
UseDynamicVolume |
启用基于账户资产的手数计算(资产 / BalanceToVolumeDivider)。 |
BalanceToVolumeDivider |
动态手数计算时使用的分母,对应 EA 的 AccountFreeMargin / 10000。 |
MaxVolume |
策略允许的最大下单手数。 |
注意事项
- 在开仓之前,策略会调用
ClosePosition() 来平掉相反方向的仓位,复制了 EA 中 CheckOrders 的处理方式。
- 由于信号在蜡烛收盘后才会触发,开仓时点可能比基于 tick 的 MetaTrader 版本更晚,但可以提高回测与实时交易时的稳定性。
- 为了让动态手数模块正常工作,请确保所选标的提供有效的
PriceStep、VolumeStep 以及账户估值信息。
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>
/// Above/Below MA Rejoin strategy.
/// Buys when price is below a rising EMA (pullback in uptrend).
/// Sells when price is above a falling EMA (pullback in downtrend).
/// </summary>
public class AboveBelowMaRejoinStrategy : Strategy
{
private readonly StrategyParam<int> _emaLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevEma;
private decimal _prevClose;
private bool _hasPrev;
public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public AboveBelowMaRejoinStrategy()
{
_emaLength = Param(nameof(EmaLength), 20)
.SetDisplay("EMA Period", "EMA lookback period", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevEma = 0m;
_prevClose = 0m;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_hasPrev = false;
var ema = new ExponentialMovingAverage { Length = EmaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ema, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal emaValue)
{
if (candle.State != CandleStates.Finished)
return;
var close = candle.ClosePrice;
if (!_hasPrev)
{
_prevEma = emaValue;
_prevClose = close;
_hasPrev = true;
return;
}
var emaRising = emaValue > _prevEma;
var emaFalling = emaValue < _prevEma;
// Price rejoins from below in uptrend - buy
if (emaRising && _prevClose < _prevEma && close >= emaValue && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
}
// Price rejoins from above in downtrend - sell
else if (emaFalling && _prevClose > _prevEma && close <= emaValue && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
}
_prevEma = emaValue;
_prevClose = close;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class above_below_ma_rejoin_strategy(Strategy):
"""Above/Below MA Rejoin strategy.
Buys when price rejoins from below a rising EMA (pullback in uptrend).
Sells when price rejoins from above a falling EMA (pullback in downtrend)."""
def __init__(self):
super(above_below_ma_rejoin_strategy, self).__init__()
self._ema_length = self.Param("EmaLength", 20) \
.SetDisplay("EMA Period", "EMA lookback period", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._prev_ema = 0.0
self._prev_close = 0.0
self._has_prev = False
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def EmaLength(self):
return self._ema_length.Value
def OnReseted(self):
super(above_below_ma_rejoin_strategy, self).OnReseted()
self._prev_ema = 0.0
self._prev_close = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(above_below_ma_rejoin_strategy, self).OnStarted2(time)
self._has_prev = False
ema = ExponentialMovingAverage()
ema.Length = self.EmaLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(ema, self._process_candle).Start()
def _process_candle(self, candle, ema_value):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
ema_val = float(ema_value)
if not self._has_prev:
self._prev_ema = ema_val
self._prev_close = close
self._has_prev = True
return
ema_rising = ema_val > self._prev_ema
ema_falling = ema_val < self._prev_ema
# Price rejoins from below in uptrend - buy
if ema_rising and self._prev_close < self._prev_ema and close >= ema_val and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
# Price rejoins from above in downtrend - sell
elif ema_falling and self._prev_close > self._prev_ema and close <= ema_val and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_ema = ema_val
self._prev_close = close
def CreateClone(self):
return above_below_ma_rejoin_strategy()