Стратегия «New Random» полностью воспроизводит оригинальный советник MetaTrader и предлагает три режима выбора направления для следующей позиции. Одновременно открыта только одна сделка: после входа алгоритм ждёт полного закрытия позиции, а затем генерирует новое направление. Стратегия подписывается на поток Level 1, использует последние значения bid/ask для имитации исполнения по рынку и автоматически пересчитывает стоп-лосс и тейк-профит в пунктах, учитывая специфику трёх- и пятизначных котировок.
Режимы входа
Генератор случайных чисел — каждое решение формируется псевдослучайно, генератор инициализируется при старте стратегии.
Цикл Buy-Sell-Buy — направления строго чередуются: первая сделка открывается в лонг, затем следует шорт, потом снова лонг и так далее.
Цикл Sell-Buy-Sell — аналогичное чередование, но стартовая сделка — шорт.
Параметры
Random Mode (Mode) — выбор одного из трёх перечисленных режимов. По умолчанию используется генератор случайных чисел.
Minimal Lot Count (MinimalLotCount) — множитель минимального объёма инструмента (Security.VolumeMin). Значение 1 соответствует одному минимальному лоту, большие значения пропорционально увеличивают объём заявки.
Stop Loss (pips) (StopLossPips) — расстояние до стоп-лосса в пунктах от цены входа. Ноль отключает защитный стоп.
Take Profit (pips) (TakeProfitPips) — расстояние до тейк-профита в пунктах от цены входа. Ноль отключает фиксирование прибыли.
Логика работы
После запуска стратегия подписывается на Level 1 и хранит последние значения bid, ask и цены последней сделки.
При отсутствии открытой позиции и активных заявок определяется направление следующего входа согласно выбранному режиму.
Заявка отправляется по рынку (BuyMarket/SellMarket); сразу вычисляются уровни стоп-лосса и тейк-профита исходя из параметров в пунктах.
Пока позиция открыта, новые входы не инициируются, что гарантирует строго одно активное плечо.
Управление позицией
Для длинной позиции закрытие инициируется, если текущая цена опускается до стоп-лосса либо достигает тейк-профита.
Для короткой позиции закрытие выполняется при росте до стоп-уровня или падении до тейк-профита.
Для контроля используется последняя доступная цена сделки; если она недоступна, применяются соответствующие bid/ask.
После выхода внутренние переменные сбрасываются, цикл для последовательных режимов сдвигается и алгоритм ждёт следующего обновления котировки.
Дополнительные замечания
В стратегии отсутствует усреднение или наращивание позиции — открывается ровно один лотный блок за раз.
Случайный режим использует системный тик как сид, что обеспечивает уникальную последовательность сделок при каждом запуске.
В исходном коде добавлены подробные комментарии на английском языке, как того требуют правила репозитория.
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>
/// Randomized entry strategy that mimics the MetaTrader "New Random" expert.
/// </summary>
public class NewRandomStrategy : Strategy
{
/// <summary>
/// Available direction selection modes.
/// </summary>
public enum RandomModes
{
/// <summary>Use a pseudo random generator for every entry decision.</summary>
Generator,
/// <summary>Alternate buy-sell-buy.</summary>
BuySellBuy,
/// <summary>Alternate sell-buy-sell.</summary>
SellBuySell
}
private readonly StrategyParam<RandomModes> _mode;
private readonly StrategyParam<int> _stopLossPoints;
private readonly StrategyParam<int> _takeProfitPoints;
private readonly StrategyParam<DataType> _candleType;
private Sides? _sequenceLastSide;
private Sides? _positionSide;
private decimal _entryPrice;
private int _candleCount;
/// <summary>Direction selection mode.</summary>
public RandomModes Mode
{
get => _mode.Value;
set => _mode.Value = value;
}
/// <summary>Stop loss in price steps.</summary>
public int StopLossPoints
{
get => _stopLossPoints.Value;
set => _stopLossPoints.Value = value;
}
/// <summary>Take profit in price steps.</summary>
public int TakeProfitPoints
{
get => _takeProfitPoints.Value;
set => _takeProfitPoints.Value = value;
}
/// <summary>Candle type.</summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public NewRandomStrategy()
{
_mode = Param(nameof(Mode), RandomModes.Generator)
.SetDisplay("Random Mode", "Direction selection mode", "General");
_stopLossPoints = Param(nameof(StopLossPoints), 5)
.SetGreaterThanZero()
.SetDisplay("Stop Loss (pts)", "Stop loss in price steps", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 5)
.SetGreaterThanZero()
.SetDisplay("Take Profit (pts)", "Take profit in price steps", "Risk");
_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();
_sequenceLastSide = null;
_positionSide = null;
_entryPrice = 0m;
_candleCount = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_sequenceLastSide = Mode switch
{
RandomModes.BuySellBuy => Sides.Sell,
RandomModes.SellBuySell => Sides.Buy,
_ => null
};
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
_candleCount++;
if (_candleCount < 3)
return;
var step = Security?.PriceStep ?? 1m;
var stopDistance = StopLossPoints * step;
var takeDistance = TakeProfitPoints * step;
var price = candle.ClosePrice;
// Check SL/TP for current position
if (Position != 0 && _entryPrice > 0)
{
var hit = false;
if (_positionSide == Sides.Buy)
{
if (stopDistance > 0 && candle.LowPrice <= _entryPrice - stopDistance)
hit = true;
if (takeDistance > 0 && candle.HighPrice >= _entryPrice + takeDistance)
hit = true;
}
else if (_positionSide == Sides.Sell)
{
if (stopDistance > 0 && candle.HighPrice >= _entryPrice + stopDistance)
hit = true;
if (takeDistance > 0 && candle.LowPrice <= _entryPrice - takeDistance)
hit = true;
}
if (hit)
{
if (Position > 0)
SellMarket();
else if (Position < 0)
BuyMarket();
_positionSide = null;
_entryPrice = 0m;
}
}
// If flat, open new random position
if (Position == 0 && _positionSide == null)
{
var side = DetermineNextSide();
if (side == Sides.Buy)
BuyMarket();
else
SellMarket();
_positionSide = side;
_entryPrice = price;
_sequenceLastSide = side;
}
}
private Sides DetermineNextSide()
{
// All modes use deterministic alternating logic
return _sequenceLastSide == Sides.Buy ? Sides.Sell : Sides.Buy;
}
}
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.Strategies import Strategy
class new_random_strategy(Strategy):
"""Alternating entry strategy with SL/TP that mimics the MetaTrader New Random expert."""
def __init__(self):
super(new_random_strategy, self).__init__()
self._stop_loss_points = self.Param("StopLossPoints", 5) \
.SetGreaterThanZero() \
.SetDisplay("Stop Loss (pts)", "Stop loss in price steps", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 5) \
.SetGreaterThanZero() \
.SetDisplay("Take Profit (pts)", "Take profit in price steps", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._seq_last_side = 0 # 0=none, 1=buy, -1=sell
self._pos_side = 0
self._entry_price = 0.0
self._candle_count = 0
@property
def StopLossPoints(self):
return int(self._stop_loss_points.Value)
@property
def TakeProfitPoints(self):
return int(self._take_profit_points.Value)
@property
def CandleType(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(new_random_strategy, self).OnStarted2(time)
self._seq_last_side = -1 # start with sell, so first entry is buy
self._pos_side = 0
self._entry_price = 0.0
self._candle_count = 0
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
self._candle_count += 1
if self._candle_count < 3:
return
sec = self.Security
step = float(sec.PriceStep) if sec is not None and sec.PriceStep is not None and float(sec.PriceStep) > 0 else 1.0
stop_dist = self.StopLossPoints * step
take_dist = self.TakeProfitPoints * step
price = float(candle.ClosePrice)
# Check SL/TP
if self.Position != 0 and self._entry_price > 0:
hit = False
if self._pos_side == 1:
if stop_dist > 0 and float(candle.LowPrice) <= self._entry_price - stop_dist:
hit = True
if take_dist > 0 and float(candle.HighPrice) >= self._entry_price + take_dist:
hit = True
elif self._pos_side == -1:
if stop_dist > 0 and float(candle.HighPrice) >= self._entry_price + stop_dist:
hit = True
if take_dist > 0 and float(candle.LowPrice) <= self._entry_price - take_dist:
hit = True
if hit:
if self.Position > 0:
self.SellMarket()
elif self.Position < 0:
self.BuyMarket()
self._pos_side = 0
self._entry_price = 0.0
# If flat, open new position (alternating)
if self.Position == 0 and self._pos_side == 0:
side = -1 if self._seq_last_side == 1 else 1
if side == 1:
self.BuyMarket()
else:
self.SellMarket()
self._pos_side = side
self._entry_price = price
self._seq_last_side = side
def OnReseted(self):
super(new_random_strategy, self).OnReseted()
self._seq_last_side = 0
self._pos_side = 0
self._entry_price = 0.0
self._candle_count = 0
def CreateClone(self):
return new_random_strategy()