Стратегия Two Direction Martin Stylized
Стратегия реализует упрощённый двухсторонний мартингейл. При запуске открывает длинную и короткую позиции и размещает лимитные заявки на фиксированном расстоянии для фиксации прибыли.
Как это работает
- Вычисляет спред и устанавливает уровень Take Profit как процент от текущей цены ask.
- Отправляет начальную рыночную заявку на продажу с отложенным ордером Buy Limit ниже bid и рыночную заявку на покупку с Sell Limit выше ask.
- Когда одна из лимитных заявок отсутствует или цена выходит за допустимые границы, алгоритм пересчитывает объёмы согласно
Same Side %и переставляет заявки. При необходимости отправляются дополнительные рыночные ордера для балансировки. - Все заявки разбиваются на части, не превышающие параметр
Volume Limit.
Параметры
- Take Profit % – расстояние до цели фиксации прибыли от текущей цены.
- Base Volume – минимальный объём каждой начальной заявки.
- Volume Limit – максимальный объём одной заявки.
- Same Side % – процент суммарного объёма, выделяемый доминирующей стороне.
- Candle Type – тип свечей, используемый как таймер.
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>
/// Martingale-style hedging strategy that alternates buy/sell based on price movement.
/// Opens initial position and doubles down on adverse moves.
/// </summary>
public class TwoDirectionMartinStylizedStrategy : Strategy
{
private readonly StrategyParam<decimal> _takeProfitPercent;
private readonly StrategyParam<int> _maxSteps;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cooldownBars;
private decimal _entryPrice;
private int _stepCount;
private int _direction; // 1=long, -1=short
private int _cooldownRemaining;
public decimal TakeProfitPercent { get => _takeProfitPercent.Value; set => _takeProfitPercent.Value = value; }
public int MaxSteps { get => _maxSteps.Value; set => _maxSteps.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }
public TwoDirectionMartinStylizedStrategy()
{
_takeProfitPercent = Param(nameof(TakeProfitPercent), 0.35m)
.SetGreaterThanZero()
.SetDisplay("Take Profit %", "Take profit as percent of price", "General");
_maxSteps = Param(nameof(MaxSteps), 3)
.SetGreaterThanZero()
.SetDisplay("Max Steps", "Maximum martingale doublings", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
_cooldownBars = Param(nameof(CooldownBars), 4)
.SetDisplay("Cooldown Bars", "Completed candles to wait after a full cycle", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_entryPrice = 0m;
_stepCount = 0;
_direction = 0;
_cooldownRemaining = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema = new ExponentialMovingAverage { Length = 20 };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ema, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal emaValue)
{
if (candle.State != CandleStates.Finished)
return;
if (_cooldownRemaining > 0)
_cooldownRemaining--;
var price = candle.ClosePrice;
if (Position == 0)
{
if (_cooldownRemaining > 0)
return;
// Initial entry based on EMA trend
if (price > emaValue)
{
BuyMarket();
_direction = 1;
}
else
{
SellMarket();
_direction = -1;
}
_entryPrice = price;
_stepCount = 0;
return;
}
var tp = _entryPrice * TakeProfitPercent / 100m;
// Check take profit
if (_direction == 1 && price >= _entryPrice + tp)
{
// Close long
while (Position > 0) SellMarket();
_stepCount = 0;
_cooldownRemaining = CooldownBars;
}
else if (_direction == -1 && price <= _entryPrice - tp)
{
// Close short
while (Position < 0) BuyMarket();
_stepCount = 0;
_cooldownRemaining = CooldownBars;
}
// Check adverse move - double down
else if (_direction == 1 && price <= _entryPrice - tp && _stepCount < MaxSteps)
{
BuyMarket();
_entryPrice = (_entryPrice + price) / 2m;
_stepCount++;
}
else if (_direction == -1 && price >= _entryPrice + tp && _stepCount < MaxSteps)
{
SellMarket();
_entryPrice = (_entryPrice + price) / 2m;
_stepCount++;
}
// Max steps reached - cut loss
else if (_stepCount >= MaxSteps)
{
if (Position > 0) SellMarket();
else if (Position < 0) BuyMarket();
_stepCount = 0;
_cooldownRemaining = CooldownBars;
}
}
}
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 two_direction_martin_stylized_strategy(Strategy):
def __init__(self):
super(two_direction_martin_stylized_strategy, self).__init__()
self._take_profit_percent = self.Param("TakeProfitPercent", 0.35) \
.SetDisplay("Take Profit %", "Take profit as percent of price", "General")
self._max_steps = self.Param("MaxSteps", 3) \
.SetDisplay("Max Steps", "Maximum martingale doublings", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._cooldown_bars = self.Param("CooldownBars", 4) \
.SetDisplay("Cooldown Bars", "Completed candles to wait after a full cycle", "General")
self._entry_price = 0.0
self._step_count = 0
self._direction = 0
self._cooldown_remaining = 0
@property
def take_profit_percent(self):
return self._take_profit_percent.Value
@property
def max_steps(self):
return self._max_steps.Value
@property
def candle_type(self):
return self._candle_type.Value
@property
def cooldown_bars(self):
return self._cooldown_bars.Value
def OnReseted(self):
super(two_direction_martin_stylized_strategy, self).OnReseted()
self._entry_price = 0.0
self._step_count = 0
self._direction = 0
self._cooldown_remaining = 0
def OnStarted2(self, time):
super(two_direction_martin_stylized_strategy, self).OnStarted2(time)
ema = ExponentialMovingAverage()
ema.Length = 20
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def process_candle(self, candle, ema_value):
if candle.State != CandleStates.Finished:
return
ema_value = float(ema_value)
if self._cooldown_remaining > 0:
self._cooldown_remaining -= 1
price = float(candle.ClosePrice)
if self.Position == 0:
if self._cooldown_remaining > 0:
return
if price > ema_value:
self.BuyMarket()
self._direction = 1
else:
self.SellMarket()
self._direction = -1
self._entry_price = price
self._step_count = 0
return
tp_pct = float(self.take_profit_percent)
tp = self._entry_price * tp_pct / 100.0
if self._direction == 1 and price >= self._entry_price + tp:
while self.Position > 0:
self.SellMarket()
self._step_count = 0
self._cooldown_remaining = self.cooldown_bars
elif self._direction == -1 and price <= self._entry_price - tp:
while self.Position < 0:
self.BuyMarket()
self._step_count = 0
self._cooldown_remaining = self.cooldown_bars
elif self._direction == 1 and price <= self._entry_price - tp and self._step_count < self.max_steps:
self.BuyMarket()
self._entry_price = (self._entry_price + price) / 2.0
self._step_count += 1
elif self._direction == -1 and price >= self._entry_price + tp and self._step_count < self.max_steps:
self.SellMarket()
self._entry_price = (self._entry_price + price) / 2.0
self._step_count += 1
elif self._step_count >= self.max_steps:
if self.Position > 0:
self.SellMarket()
elif self.Position < 0:
self.BuyMarket()
self._step_count = 0
self._cooldown_remaining = self.cooldown_bars
def CreateClone(self):
return two_direction_martin_stylized_strategy()