New Martin 策略
概述
New Martin 策略复刻了 MetaTrader 平台上的 “New Martin” 智能交易系统,通过在市场两侧同时建立对冲仓位并在移动均线交叉时扩展亏损腿的仓位来实现马丁格尔思路。策略始终持有一张多单和一张空单,当某一侧浮亏时,就按乘数扩大该侧仓位,同时平掉当前盈利最大的头寸。止盈触发后会立即补回缺失的方向,并可选地清理盈利和亏损极值持仓,使网格保持紧凑。
实现基于 StockSharp 高级 API,并假设组合账户支持对冲,从而允许多空仓位共存。为了贴近原始 MQL 实现,进出场均采用市价单,默认视为即时成交。
指标与信号
- 快速 SMMA(默认周期 5): 捕捉短期价格动量。
- 慢速 SMMA(默认周期 20): 描述主要趋势方向。
- 交叉检测: 使用上一根和上上一根已完成的 K 线,当两条 SMMA 交叉时触发马丁加仓。通过记录上次交叉的开盘时间,确保每根 K 线只响应一次信号。
仓位管理
- 初始对冲: 指标形成后立即按初始手数各开一张多单和空单,并设置对称的止盈距离。
- 止盈循环: 当价格触及任意一侧的止盈水平时,平掉该笔持仓,并可选择同时平掉当前盈利最高和亏损最大的仓位,以配对结算盈亏。若某个方向仓位被清空,会马上按照基准手数重新开仓,保证对冲结构持续存在。
- 马丁加仓: 每次 SMMA 交叉时,先找出浮动盈亏最差的持仓,在该方向按乘数放大仓位(按交易所的最小手数向下取整)。随后立即平掉浮动盈利最大的持仓以锁定收益。
风险控制
- 权益回撤限制: 记录账户历史最高权益,一旦从峰值回撤超过设定百分比,立刻清空全部仓位,等待下一根 K 线重新建立对冲。
- 动态基准手数: 当账户权益相对上次记录的余额增长达到乘数倍时,按同样的乘数提高基础对冲手数(同样会遵循交易所最小 / 最大手数限制)。这与原版 EA 将利润滚入网格的方式一致。
- 手数归一化: 每次下单都会把手数向下取整到交易所的步长,并限制在最小/最大手数之间,以避免被拒单。
参数
- Take Profit (pips): 每条腿距入场价的止盈距离,默认为 50 点。
- Initial Volume: 每个方向的基础手数,默认 0.1 手。
- Slow MA / Fast MA: 慢速与快速 SMMA 的周期(默认分别为 20 和 5),必须保持慢线周期大于快线周期。
- Equity DD %: 从权益峰值计算的最大允许回撤,默认 12%。
- Multiplier: 马丁加仓以及基准手数扩张所用的乘数,默认 1.6。
- Candle Type: 用于计算的 K 线周期,默认 15 分钟,可根据原策略的图表周期调整。
注意事项
- 账户需支持对冲模式,才能同时持有多头与空头。
- 策略使用市价单,若需要严格控制滑点,可在此基础上拓展委托逻辑。
- 请确认标的证券的价格步长、手数步长及最小/最大手数信息配置正确,以便手数归一化计算生效。
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Hedged martingale strategy that scales positions on moving average crossovers.
/// </summary>
public class NewMartinStrategy : Strategy
{
private readonly StrategyParam<decimal> _takeProfitPips;
private readonly StrategyParam<decimal> _initialVolume;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<decimal> _lossPercent;
private readonly StrategyParam<decimal> _multiplier;
private readonly StrategyParam<DataType> _candleType;
private readonly List<PositionEntry> _longPositions = new();
private readonly List<PositionEntry> _shortPositions = new();
private SmoothedMovingAverage _slowMa;
private SmoothedMovingAverage _fastMa;
private decimal? _slowPrev1;
private decimal? _slowPrev2;
private decimal? _fastPrev1;
private decimal? _fastPrev2;
private decimal _currentVolume;
private decimal _pipSize;
private decimal _startBalance;
private decimal _peakBalance;
private DateTimeOffset? _lastCrossTime;
private bool _positionsInitialized;
/// <summary>
/// Take profit distance in pips.
/// </summary>
public decimal TakeProfitPips
{
get => _takeProfitPips.Value;
set => _takeProfitPips.Value = value;
}
/// <summary>
/// Initial hedge volume per side.
/// </summary>
public decimal InitialVolume
{
get => _initialVolume.Value;
set => _initialVolume.Value = value;
}
/// <summary>
/// Period of the slow smoothed moving average.
/// </summary>
public int SlowPeriod
{
get => _slowPeriod.Value;
set => _slowPeriod.Value = value;
}
/// <summary>
/// Period of the fast smoothed moving average.
/// </summary>
public int FastPeriod
{
get => _fastPeriod.Value;
set => _fastPeriod.Value = value;
}
/// <summary>
/// Maximum equity drawdown percentage before all positions are liquidated.
/// </summary>
public decimal LossPercent
{
get => _lossPercent.Value;
set => _lossPercent.Value = value;
}
/// <summary>
/// Multiplier applied to the martingale additions and base volume growth.
/// </summary>
public decimal Multiplier
{
get => _multiplier.Value;
set => _multiplier.Value = value;
}
/// <summary>
/// Candle type used for indicator calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="NewMartinStrategy"/>.
/// </summary>
public NewMartinStrategy()
{
_takeProfitPips = Param(nameof(TakeProfitPips), 50m)
.SetGreaterThanZero()
.SetDisplay("Take Profit", "Target distance in pips", "Risk")
.SetOptimize(10m, 200m, 10m);
_initialVolume = Param(nameof(InitialVolume), 0.1m)
.SetGreaterThanZero()
.SetDisplay("Initial Volume", "Volume per hedge side", "Trading")
.SetOptimize(0.01m, 1m, 0.01m);
_slowPeriod = Param(nameof(SlowPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("Slow MA", "Slow smoothed MA period", "Indicators")
.SetOptimize(10, 80, 5);
_fastPeriod = Param(nameof(FastPeriod), 5)
.SetGreaterThanZero()
.SetDisplay("Fast MA", "Fast smoothed MA period", "Indicators")
.SetOptimize(2, 20, 1);
_lossPercent = Param(nameof(LossPercent), 12m)
.SetGreaterThanZero()
.SetDisplay("Equity DD %", "Maximum drawdown before reset", "Risk")
.SetOptimize(5m, 30m, 1m);
_multiplier = Param(nameof(Multiplier), 1.6m)
.SetGreaterThanZero()
.SetDisplay("Multiplier", "Martingale growth factor", "Trading")
.SetOptimize(1.1m, 3m, 0.1m);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Time frame for calculations", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_longPositions.Clear();
_shortPositions.Clear();
_slowPrev1 = null;
_slowPrev2 = null;
_fastPrev1 = null;
_fastPrev2 = null;
_currentVolume = 0m;
_pipSize = 0m;
_startBalance = 0m;
_peakBalance = 0m;
_lastCrossTime = null;
_positionsInitialized = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
if (SlowPeriod <= FastPeriod)
throw new InvalidOperationException("Slow period must be greater than fast period.");
_currentVolume = AdjustVolume(InitialVolume);
var step = Security?.PriceStep ?? 0m;
if (step <= 0m)
{
_pipSize = 1m;
}
else
{
_pipSize = step;
var decimals = Security?.Decimals ?? 0;
if (decimals == 3 || decimals == 5)
_pipSize = step * 10m;
}
_slowMa = new SmoothedMovingAverage { Length = SlowPeriod };
_fastMa = new SmoothedMovingAverage { Length = FastPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_slowMa, _fastMa, ProcessCandle)
.Start();
_startBalance = Portfolio?.CurrentValue ?? Portfolio?.BeginValue ?? 0m;
_peakBalance = _startBalance;
}
private void ProcessCandle(ICandleMessage candle, decimal slow, decimal fast)
{
if (candle.State != CandleStates.Finished)
return;
if (!_slowMa.IsFormed || !_fastMa.IsFormed)
{
UpdateAverageHistory(slow, fast);
return;
}
UpdateAccountMetrics();
if (ShouldCloseAllPositions())
{
CloseAllPositions();
_positionsInitialized = false;
}
if (!_positionsInitialized)
{
InitializeHedge(candle.ClosePrice);
}
var tpTriggered = CheckTakeProfits(candle);
if (tpTriggered)
{
CloseExtremePositions(candle.ClosePrice);
}
if (_longPositions.Count == 0)
OpenPosition(Sides.Buy, _currentVolume, candle.ClosePrice);
if (_shortPositions.Count == 0)
OpenPosition(Sides.Sell, _currentVolume, candle.ClosePrice);
HandleCrossing(candle, slow, fast);
UpdateAverageHistory(slow, fast);
}
private void InitializeHedge(decimal price)
{
if (_currentVolume <= 0m)
return;
// Start with symmetric hedge on both sides.
OpenPosition(Sides.Buy, _currentVolume, price);
OpenPosition(Sides.Sell, _currentVolume, price);
_positionsInitialized = _longPositions.Count > 0 && _shortPositions.Count > 0;
}
private void UpdateAccountMetrics()
{
var equity = Portfolio?.CurrentValue ?? 0m;
if (equity > _peakBalance)
_peakBalance = equity;
if (_startBalance > 0m && equity >= _startBalance * Multiplier)
{
_startBalance = equity;
var newVolume = AdjustVolume(_currentVolume * Multiplier);
if (newVolume > 0m)
_currentVolume = newVolume;
}
}
private bool ShouldCloseAllPositions()
{
if (_peakBalance <= 0m)
return false;
var equity = Portfolio?.CurrentValue ?? 0m;
if (equity <= 0m)
return false;
var drawdown = (_peakBalance - equity) / _peakBalance * 100m;
return drawdown >= LossPercent;
}
private bool CheckTakeProfits(ICandleMessage candle)
{
var triggered = false;
var offset = TakeProfitPips * _pipSize;
if (offset <= 0m)
return false;
foreach (var entry in _longPositions.ToArray())
{
if (entry is null)
continue;
if (candle.HighPrice >= entry.TakeProfit)
{
CloseEntry(entry);
triggered = true;
}
}
foreach (var entry in _shortPositions.ToArray())
{
if (entry is null)
continue;
if (candle.LowPrice <= entry.TakeProfit)
{
CloseEntry(entry);
triggered = true;
}
}
return triggered;
}
private void CloseExtremePositions(decimal price)
{
var (lossEntry, lossValue, profitEntry, profitValue) = GetExtremePositions(price);
if (lossEntry is not null && lossValue < 0m)
CloseEntry(lossEntry);
if (profitEntry is not null && profitEntry != lossEntry)
CloseEntry(profitEntry);
}
private void HandleCrossing(ICandleMessage candle, decimal slow, decimal fast)
{
if (!_slowPrev2.HasValue || !_slowPrev1.HasValue || !_fastPrev2.HasValue || !_fastPrev1.HasValue)
return;
var crossDetected = (_slowPrev2.Value > _fastPrev2.Value && _slowPrev1.Value < _fastPrev1.Value)
|| (_slowPrev2.Value < _fastPrev2.Value && _slowPrev1.Value > _fastPrev1.Value);
if (!crossDetected)
return;
if (_lastCrossTime == candle.OpenTime)
return;
_lastCrossTime = candle.OpenTime;
var (lossEntry, _, profitEntry, _) = GetExtremePositions(candle.ClosePrice);
if (lossEntry is null)
return;
var volume = AdjustVolume(lossEntry.Volume * Multiplier);
if (volume <= 0m)
return;
// Average down on the weakest side.
OpenPosition(lossEntry.Side, volume, candle.ClosePrice);
if (profitEntry is not null && profitEntry != lossEntry)
{
// Lock in profit on the strongest position after the new hedge.
CloseEntry(profitEntry);
}
}
private void UpdateAverageHistory(decimal slow, decimal fast)
{
_slowPrev2 = _slowPrev1;
_slowPrev1 = slow;
_fastPrev2 = _fastPrev1;
_fastPrev1 = fast;
}
private void OpenPosition(Sides side, decimal requestedVolume, decimal price)
{
var volume = AdjustVolume(requestedVolume);
if (volume <= 0m)
return;
var offset = TakeProfitPips * _pipSize;
if (offset <= 0m)
return;
var takeProfit = side == Sides.Buy ? price + offset : price - offset;
var entry = new PositionEntry(side, volume, price, takeProfit);
if (side == Sides.Buy)
{
_longPositions.Add(entry);
BuyMarket(volume);
}
else
{
_shortPositions.Add(entry);
SellMarket(volume);
}
}
private void CloseAllPositions()
{
foreach (var entry in _longPositions.ToArray())
CloseEntry(entry);
foreach (var entry in _shortPositions.ToArray())
CloseEntry(entry);
}
private void CloseEntry(PositionEntry entry)
{
if (entry.Side == Sides.Buy)
{
SellMarket(entry.Volume);
for (var i = _longPositions.Count - 1; i >= 0; i--)
{
if (_longPositions[i] == entry)
{
_longPositions.RemoveAt(i);
break;
}
}
}
else
{
BuyMarket(entry.Volume);
for (var i = _shortPositions.Count - 1; i >= 0; i--)
{
if (_shortPositions[i] == entry)
{
_shortPositions.RemoveAt(i);
break;
}
}
}
}
private (PositionEntry lossEntry, decimal lossValue, PositionEntry profitEntry, decimal profitValue) GetExtremePositions(decimal price)
{
PositionEntry lossEntry = null;
PositionEntry profitEntry = null;
var lossValue = 0m;
var profitValue = 0m;
foreach (var entry in _longPositions)
{
var pnl = (price - entry.EntryPrice) * entry.Volume;
if (lossEntry is null || pnl < lossValue)
{
lossEntry = entry;
lossValue = pnl;
}
if (profitEntry is null || pnl > profitValue)
{
profitEntry = entry;
profitValue = pnl;
}
}
foreach (var entry in _shortPositions)
{
var pnl = (entry.EntryPrice - price) * entry.Volume;
if (lossEntry is null || pnl < lossValue)
{
lossEntry = entry;
lossValue = pnl;
}
if (profitEntry is null || pnl > profitValue)
{
profitEntry = entry;
profitValue = pnl;
}
}
return (lossEntry, lossValue, profitEntry, profitValue);
}
private decimal AdjustVolume(decimal volume)
{
var security = Security;
if (security is null)
return volume;
var min = security.MinVolume ?? 0m;
if (min > 0m && volume < min)
return 0m;
var max = security.MaxVolume ?? 0m;
if (max > 0m && volume > max)
volume = max;
return volume;
}
private sealed record PositionEntry(Sides Side, decimal Volume, decimal EntryPrice, decimal TakeProfit);
}
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, Sides
from StockSharp.Algo.Indicators import SmoothedMovingAverage
from StockSharp.Algo.Strategies import Strategy
from datatype_extensions import *
from indicator_extensions import *
class new_martin_strategy(Strategy):
def __init__(self):
super(new_martin_strategy, self).__init__()
self._tp_pips = self.Param("TakeProfitPips", 50.0).SetGreaterThanZero().SetDisplay("Take Profit", "Target distance in pips", "Risk")
self._initial_volume = self.Param("InitialVolume", 0.1).SetGreaterThanZero().SetDisplay("Initial Volume", "Volume per hedge side", "Trading")
self._slow_period = self.Param("SlowPeriod", 20).SetGreaterThanZero().SetDisplay("Slow MA", "Slow smoothed MA period", "Indicators")
self._fast_period = self.Param("FastPeriod", 5).SetGreaterThanZero().SetDisplay("Fast MA", "Fast smoothed MA period", "Indicators")
self._loss_percent = self.Param("LossPercent", 12.0).SetGreaterThanZero().SetDisplay("Equity DD %", "Maximum drawdown before reset", "Risk")
self._multiplier = self.Param("Multiplier", 1.6).SetGreaterThanZero().SetDisplay("Multiplier", "Martingale growth factor", "Trading")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))).SetDisplay("Candle Type", "Time frame", "General")
@property
def CandleType(self): return self._candle_type.Value
@CandleType.setter
def CandleType(self, value): self._candle_type.Value = value
def OnReseted(self):
super(new_martin_strategy, self).OnReseted()
self._slow_prev1 = None
self._slow_prev2 = None
self._fast_prev1 = None
self._fast_prev2 = None
self._current_volume = 0
self._pip_size = 0
self._initialized = False
def OnStarted2(self, time):
super(new_martin_strategy, self).OnStarted2(time)
self._current_volume = self._initial_volume.Value
step = 1.0
if self.Security is not None and self.Security.PriceStep is not None and self.Security.PriceStep > 0:
step = float(self.Security.PriceStep)
self._pip_size = step
self._slow_prev1 = None
self._slow_prev2 = None
self._fast_prev1 = None
self._fast_prev2 = None
self._initialized = False
slow_ma = SmoothedMovingAverage()
slow_ma.Length = self._slow_period.Value
fast_ma = SmoothedMovingAverage()
fast_ma.Length = self._fast_period.Value
self._slow_ma = slow_ma
self._fast_ma = fast_ma
sub = self.SubscribeCandles(self.CandleType)
sub.Bind(slow_ma, fast_ma, self.OnProcess).Start()
def OnProcess(self, candle, slow, fast):
if candle.State != CandleStates.Finished:
return
if not self._slow_ma.IsFormed or not self._fast_ma.IsFormed:
self._update_history(slow, fast)
return
if not self._initialized:
self.BuyMarket(self._current_volume)
self.SellMarket(self._current_volume)
self._initialized = True
# Check MA crossover for martingale additions
if (self._slow_prev2 is not None and self._fast_prev2 is not None and
self._slow_prev1 is not None and self._fast_prev1 is not None):
cross = ((self._slow_prev2 > self._fast_prev2 and self._slow_prev1 < self._fast_prev1) or
(self._slow_prev2 < self._fast_prev2 and self._slow_prev1 > self._fast_prev1))
if cross:
vol = self._current_volume * self._multiplier.Value
if self.Position > 0:
self.SellMarket(vol)
elif self.Position < 0:
self.BuyMarket(vol)
self._update_history(slow, fast)
def _update_history(self, slow, fast):
self._slow_prev2 = self._slow_prev1
self._slow_prev1 = slow
self._fast_prev2 = self._fast_prev1
self._fast_prev1 = fast
def CreateClone(self):
return new_martin_strategy()