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>
/// Hans123 Trader v2 breakout strategy converted from the original MQL expert.
/// Enters on breakout of recent range extremes and manages trailing protection.
/// </summary>
public class Hans123TraderV2Strategy : Strategy
{
private readonly StrategyParam<decimal> _stopLossPips;
private readonly StrategyParam<decimal> _takeProfitPips;
private readonly StrategyParam<decimal> _trailingStopPips;
private readonly StrategyParam<decimal> _trailingStepPips;
private readonly StrategyParam<int> _startHour;
private readonly StrategyParam<int> _endHour;
private readonly StrategyParam<int> _breakoutPeriod;
private readonly StrategyParam<DataType> _candleType;
private Highest _highest;
private Lowest _lowest;
private decimal _entryPrice;
private decimal _pipSize;
private decimal _stopLossDistance;
private decimal _takeProfitDistance;
private decimal _trailingStopDistance;
private decimal _trailingStepDistance;
private decimal _highestStopPrice;
private decimal? _prevBreakoutHigh;
private decimal? _prevBreakoutLow;
/// <summary>
/// Stop-loss distance in pips.
/// </summary>
public decimal StopLossPips
{
get => _stopLossPips.Value;
set => _stopLossPips.Value = value;
}
/// <summary>
/// Take-profit distance in pips.
/// </summary>
public decimal TakeProfitPips
{
get => _takeProfitPips.Value;
set => _takeProfitPips.Value = value;
}
/// <summary>
/// Trailing stop distance in pips.
/// </summary>
public decimal TrailingStopPips
{
get => _trailingStopPips.Value;
set => _trailingStopPips.Value = value;
}
/// <summary>
/// Trailing step in pips.
/// </summary>
public decimal TrailingStepPips
{
get => _trailingStepPips.Value;
set => _trailingStepPips.Value = value;
}
/// <summary>
/// Session start hour.
/// </summary>
public int StartHour
{
get => _startHour.Value;
set => _startHour.Value = value;
}
/// <summary>
/// Session end hour.
/// </summary>
public int EndHour
{
get => _endHour.Value;
set => _endHour.Value = value;
}
/// <summary>
/// Lookback length for calculating highs and lows.
/// </summary>
public int BreakoutPeriod
{
get => _breakoutPeriod.Value;
set => _breakoutPeriod.Value = value;
}
/// <summary>
/// Candle type processed by the strategy.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes <see cref="Hans123TraderV2Strategy"/>.
/// </summary>
public Hans123TraderV2Strategy()
{
_stopLossPips = Param(nameof(StopLossPips), 50m)
.SetNotNegative()
.SetDisplay("Stop Loss (pips)", "Stop distance", "Risk");
_takeProfitPips = Param(nameof(TakeProfitPips), 50m)
.SetNotNegative()
.SetDisplay("Take Profit (pips)", "Target distance", "Risk");
_trailingStopPips = Param(nameof(TrailingStopPips), 10m)
.SetNotNegative()
.SetDisplay("Trailing Stop (pips)", "Trailing distance", "Risk");
_trailingStepPips = Param(nameof(TrailingStepPips), 5m)
.SetNotNegative()
.SetDisplay("Trailing Step (pips)", "Extra profit before trailing", "Risk");
_startHour = Param(nameof(StartHour), 0)
.SetDisplay("Start Hour", "Session start hour", "Session");
_endHour = Param(nameof(EndHour), 23)
.SetDisplay("End Hour", "Session end hour", "Session");
_breakoutPeriod = Param(nameof(BreakoutPeriod), 10)
.SetGreaterThanZero()
.SetDisplay("Breakout Period", "High/low lookback", "Trading");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Processed candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_pipSize = 0m;
_stopLossDistance = 0m;
_takeProfitDistance = 0m;
_trailingStopDistance = 0m;
_trailingStepDistance = 0m;
_highest = null;
_lowest = null;
_entryPrice = 0m;
_highestStopPrice = 0m;
_prevBreakoutHigh = null;
_prevBreakoutLow = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_pipSize = Security?.PriceStep ?? 1m;
UpdateDistanceCache();
_highest = new Highest { Length = BreakoutPeriod };
_lowest = new Lowest { Length = BreakoutPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(_highest, _lowest, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void UpdateDistanceCache()
{
_stopLossDistance = StopLossPips * _pipSize;
_takeProfitDistance = TakeProfitPips * _pipSize;
_trailingStopDistance = TrailingStopPips * _pipSize;
_trailingStepDistance = TrailingStepPips * _pipSize;
}
private void ProcessCandle(ICandleMessage candle, decimal breakoutHigh, decimal breakoutLow)
{
if (candle.State != CandleStates.Finished)
return;
if (!_highest.IsFormed || !_lowest.IsFormed)
return;
// Manage existing position: trailing stop and SL/TP
if (Position != 0)
{
ManagePosition(candle);
return;
}
// Session filter
var hour = candle.OpenTime.TimeOfDay.Hours;
if (hour < StartHour || hour >= EndHour)
{
_prevBreakoutHigh = breakoutHigh;
_prevBreakoutLow = breakoutLow;
return;
}
// Breakout entry: buy when price breaks above previous bar's high, sell when below previous bar's low
if (_prevBreakoutHigh.HasValue && _prevBreakoutLow.HasValue)
{
if (candle.HighPrice > _prevBreakoutHigh.Value)
{
BuyMarket();
_entryPrice = candle.ClosePrice;
_highestStopPrice = 0m;
}
else if (candle.LowPrice < _prevBreakoutLow.Value)
{
SellMarket();
_entryPrice = candle.ClosePrice;
_highestStopPrice = 0m;
}
}
_prevBreakoutHigh = breakoutHigh;
_prevBreakoutLow = breakoutLow;
}
private void ManagePosition(ICandleMessage candle)
{
var price = candle.ClosePrice;
if (Position > 0)
{
// Check stop loss
if (_stopLossDistance > 0m && candle.LowPrice <= _entryPrice - _stopLossDistance)
{
SellMarket(Position);
return;
}
// Check take profit
if (_takeProfitDistance > 0m && candle.HighPrice >= _entryPrice + _takeProfitDistance)
{
SellMarket(Position);
return;
}
// Trailing stop
if (_trailingStopDistance > 0m)
{
var moveFromEntry = price - _entryPrice;
if (moveFromEntry > _trailingStopDistance + _trailingStepDistance)
{
var newStop = price - _trailingStopDistance;
if (newStop > _highestStopPrice + _trailingStepDistance)
_highestStopPrice = newStop;
if (_highestStopPrice > 0m && candle.LowPrice <= _highestStopPrice)
{
SellMarket(Position);
return;
}
}
}
}
else if (Position < 0)
{
var vol = Math.Abs(Position);
// Check stop loss
if (_stopLossDistance > 0m && candle.HighPrice >= _entryPrice + _stopLossDistance)
{
BuyMarket(vol);
return;
}
// Check take profit
if (_takeProfitDistance > 0m && candle.LowPrice <= _entryPrice - _takeProfitDistance)
{
BuyMarket(vol);
return;
}
// Trailing stop
if (_trailingStopDistance > 0m)
{
var moveFromEntry = _entryPrice - price;
if (moveFromEntry > _trailingStopDistance + _trailingStepDistance)
{
var newStop = price + _trailingStopDistance;
if (_highestStopPrice == 0m || newStop < _highestStopPrice - _trailingStepDistance)
_highestStopPrice = newStop;
if (_highestStopPrice > 0m && candle.HighPrice >= _highestStopPrice)
{
BuyMarket(vol);
return;
}
}
}
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from StockSharp.Algo.Indicators import Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
from StockSharp.Messages import DataType, CandleStates
from System import TimeSpan, Math
class hans123_trader_v2_strategy(Strategy):
def __init__(self):
super(hans123_trader_v2_strategy, self).__init__()
self._stop_loss_pips = self.Param("StopLossPips", 50.0)
self._take_profit_pips = self.Param("TakeProfitPips", 50.0)
self._trailing_stop_pips = self.Param("TrailingStopPips", 10.0)
self._trailing_step_pips = self.Param("TrailingStepPips", 5.0)
self._start_hour = self.Param("StartHour", 0)
self._end_hour = self.Param("EndHour", 23)
self._breakout_period = self.Param("BreakoutPeriod", 10)
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4)))
self._highest = None
self._lowest = None
self._entry_price = 0.0
self._pip_size = 1.0
self._sl_dist = 0.0
self._tp_dist = 0.0
self._trail_dist = 0.0
self._trail_step_dist = 0.0
self._highest_stop = 0.0
self._prev_high = None
self._prev_low = None
@property
def CandleType(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(hans123_trader_v2_strategy, self).OnStarted2(time)
self._pip_size = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 1.0
self._sl_dist = self._stop_loss_pips.Value * self._pip_size
self._tp_dist = self._take_profit_pips.Value * self._pip_size
self._trail_dist = self._trailing_stop_pips.Value * self._pip_size
self._trail_step_dist = self._trailing_step_pips.Value * self._pip_size
self._highest = Highest()
self._highest.Length = self._breakout_period.Value
self._lowest = Lowest()
self._lowest.Length = self._breakout_period.Value
sub = self.SubscribeCandles(self.CandleType)
sub.Bind(self._highest, self._lowest, self._process_candle).Start()
def _process_candle(self, candle, breakout_high_val, breakout_low_val):
if not self._highest.IsFormed or not self._lowest.IsFormed:
return
breakout_high = float(breakout_high_val)
breakout_low = float(breakout_low_val)
if self.Position != 0:
self._manage_position(candle)
return
hour = candle.OpenTime.TimeOfDay.Hours
if hour < self._start_hour.Value or hour >= self._end_hour.Value:
self._prev_high = breakout_high
self._prev_low = breakout_low
return
if self._prev_high is not None and self._prev_low is not None:
if float(candle.HighPrice) > self._prev_high:
self.BuyMarket()
self._entry_price = float(candle.ClosePrice)
self._highest_stop = 0.0
elif float(candle.LowPrice) < self._prev_low:
self.SellMarket()
self._entry_price = float(candle.ClosePrice)
self._highest_stop = 0.0
self._prev_high = breakout_high
self._prev_low = breakout_low
def _manage_position(self, candle):
price = float(candle.ClosePrice)
if self.Position > 0:
if self._sl_dist > 0 and float(candle.LowPrice) <= self._entry_price - self._sl_dist:
self.SellMarket(self.Position)
return
if self._tp_dist > 0 and float(candle.HighPrice) >= self._entry_price + self._tp_dist:
self.SellMarket(self.Position)
return
if self._trail_dist > 0:
move = price - self._entry_price
if move > self._trail_dist + self._trail_step_dist:
new_stop = price - self._trail_dist
if new_stop > self._highest_stop + self._trail_step_dist:
self._highest_stop = new_stop
if self._highest_stop > 0 and float(candle.LowPrice) <= self._highest_stop:
self.SellMarket(self.Position)
return
elif self.Position < 0:
vol = abs(self.Position)
if self._sl_dist > 0 and float(candle.HighPrice) >= self._entry_price + self._sl_dist:
self.BuyMarket(vol)
return
if self._tp_dist > 0 and float(candle.LowPrice) <= self._entry_price - self._tp_dist:
self.BuyMarket(vol)
return
if self._trail_dist > 0:
move = self._entry_price - price
if move > self._trail_dist + self._trail_step_dist:
new_stop = price + self._trail_dist
if self._highest_stop == 0 or new_stop < self._highest_stop - self._trail_step_dist:
self._highest_stop = new_stop
if self._highest_stop > 0 and float(candle.HighPrice) >= self._highest_stop:
self.BuyMarket(vol)
return
def OnReseted(self):
super(hans123_trader_v2_strategy, self).OnReseted()
self._entry_price = 0.0
self._highest_stop = 0.0
self._prev_high = None
self._prev_low = None
def CreateClone(self):
return hans123_trader_v2_strategy()