TemplateEAbyMarket is a direct StockSharp port of the original MetaTrader 4 expert advisor TemplateEAbyMarket.mq4. The strategy uses the Moving Average Convergence Divergence (MACD) indicator to detect momentum shifts. When the MACD main line crosses the signal line while both components are in the same positive or negative zone, the strategy opens a market position in the direction of the crossover. Exits are managed exclusively through protective orders (take profit and stop loss) configured via the built-in StartProtection helper.
The StockSharp version keeps the behaviour of the MQL program: it only opens new positions without trying to automatically close the opposite side. Once a position is filled, the trade is left to be managed by protective levels or manual intervention.
Trading Logic
Subscribe to the user-selected candle type (default: 15-minute time frame).
Calculate MACD (12/26/9 by default) on every finished candle.
Track the relative position of the MACD main and signal lines to detect a crossover event:
Bullish setup: previous candle had the main line below the signal line, the current candle closes with the main line above the signal line, and both lines are above zero. A market buy order with OrderVolume is submitted if the current exposure is below MaxOrders * OrderVolume.
Bearish setup: previous candle had the main line above the signal line, the current candle closes with the main line below the signal line, and both lines are below zero. A market sell order with OrderVolume is submitted subject to the same exposure cap.
Protective takeProfit and stopLoss levels are activated once at start-up. The strategy does not close opposite positions automatically; risk is controlled by the protection module or by the user.
Parameters
Name
Description
MacdFastPeriod
Fast EMA length for the MACD calculation.
MacdSlowPeriod
Slow EMA length for the MACD calculation.
MacdSignalPeriod
Signal EMA length for the MACD calculation.
CandleType
Candle type (time frame) that feeds the indicator.
OrderVolume
Volume submitted with each market order.
MaxOrders
Maximum number of concurrent orders, expressed as multiples of OrderVolume. The strategy checks abs(Position) < MaxOrders * OrderVolume before sending a new order.
TakeProfitPoints
Take-profit distance in price points. Value 0 disables the take profit.
StopLossPoints
Stop-loss distance in price points. Value 0 disables the stop loss.
Notes
Slippage and magic number settings from the MQL version are intentionally omitted because they are handled differently in StockSharp.
Ensure the connector provides proper price step metadata; StartProtection interprets distances in instrument price points.
The template is intentionally minimalistic and does not manage partial fills or pyramid entries beyond the MaxOrders limit.
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Template EA by Market strategy: MACD histogram crossover with signal line.
/// </summary>
public class TemplateEAbyMarketStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _signalPeriod;
private decimal _prevMacd;
private decimal _prevSignal;
private bool _hasPrev;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public int SignalPeriod { get => _signalPeriod.Value; set => _signalPeriod.Value = value; }
public TemplateEAbyMarketStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_fastPeriod = Param(nameof(FastPeriod), 12)
.SetGreaterThanZero()
.SetDisplay("Fast Period", "MACD fast EMA", "Indicators");
_slowPeriod = Param(nameof(SlowPeriod), 26)
.SetGreaterThanZero()
.SetDisplay("Slow Period", "MACD slow EMA", "Indicators");
_signalPeriod = Param(nameof(SignalPeriod), 9)
.SetGreaterThanZero()
.SetDisplay("Signal Period", "MACD signal period", "Indicators");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevMacd = 0m;
_prevSignal = 0m;
_hasPrev = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_hasPrev = false;
var macd = new MovingAverageConvergenceDivergenceSignal
{
Macd = { ShortMa = { Length = FastPeriod }, LongMa = { Length = SlowPeriod } },
SignalMa = { Length = SignalPeriod }
};
var subscription = SubscribeCandles(CandleType);
subscription.BindEx(macd, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue macdValue)
{
if (candle.State != CandleStates.Finished) return;
if (!macdValue.IsFinal) return;
var v = (MovingAverageConvergenceDivergenceSignalValue)macdValue;
if (v.Macd is not decimal macdLine || v.Signal is not decimal signal) return;
if (_hasPrev)
{
var prevHist = _prevMacd - _prevSignal;
var currHist = macdLine - signal;
if (prevHist <= 0 && currHist > 0 && Position <= 0)
BuyMarket();
else if (prevHist >= 0 && currHist < 0 && Position >= 0)
SellMarket();
}
_prevMacd = macdLine;
_prevSignal = signal;
_hasPrev = true;
}
}
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 MovingAverageConvergenceDivergenceSignal
from StockSharp.Algo.Strategies import Strategy
class template_e_aby_market_strategy(Strategy):
def __init__(self):
super(template_e_aby_market_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30)))
self._fast_period = self.Param("FastPeriod", 12)
self._slow_period = self.Param("SlowPeriod", 26)
self._signal_period = self.Param("SignalPeriod", 9)
self._prev_macd = 0.0
self._prev_signal = 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 FastPeriod(self):
return self._fast_period.Value
@FastPeriod.setter
def FastPeriod(self, value):
self._fast_period.Value = value
@property
def SlowPeriod(self):
return self._slow_period.Value
@SlowPeriod.setter
def SlowPeriod(self, value):
self._slow_period.Value = value
@property
def SignalPeriod(self):
return self._signal_period.Value
@SignalPeriod.setter
def SignalPeriod(self, value):
self._signal_period.Value = value
def OnReseted(self):
super(template_e_aby_market_strategy, self).OnReseted()
self._prev_macd = 0.0
self._prev_signal = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(template_e_aby_market_strategy, self).OnStarted2(time)
self._has_prev = False
macd = MovingAverageConvergenceDivergenceSignal()
macd.Macd.ShortMa.Length = self.FastPeriod
macd.Macd.LongMa.Length = self.SlowPeriod
macd.SignalMa.Length = self.SignalPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.BindEx(macd, self._process_candle).Start()
def _process_candle(self, candle, macd_value):
if candle.State != CandleStates.Finished:
return
if not macd_value.IsFinal:
return
macd_line = macd_value.Macd
signal = macd_value.Signal
if macd_line is None or signal is None:
return
macd_line = float(macd_line)
signal = float(signal)
if self._has_prev:
prev_hist = self._prev_macd - self._prev_signal
curr_hist = macd_line - signal
if prev_hist <= 0 and curr_hist > 0 and self.Position <= 0:
self.BuyMarket()
elif prev_hist >= 0 and curr_hist < 0 and self.Position >= 0:
self.SellMarket()
self._prev_macd = macd_line
self._prev_signal = signal
self._has_prev = True
def CreateClone(self):
return template_e_aby_market_strategy()