Little EA is a moving-average crossover expert originally written for MetaTrader. The strategy observes the candle selected by the OHLC bar index parameter and reacts when that candle crosses a shifted moving average from below or above. The StockSharp port keeps the original multi-entry idea by allowing several tranches per direction while respecting a configurable maximum exposure.
Trading logic
Subscribe to the configured candle series and feed the selected moving average type with the chosen price source (close, open, high, low, median, typical, or weighted).
Store completed candles so the strategy can reference the candle at the OhlcBarIndex (the default value 1 means the last fully closed candle).
Apply the optional MaShift by reading the moving-average value from several bars back, replicating the MetaTrader visual shift.
When the reference candle closes above the shifted MA, treat it as a bullish cross. When it closes below the shifted MA, treat it as a bearish cross.
For a bullish cross:
If the net short exposure already equals the configured maximum, close the entire short position.
Otherwise, if the long exposure is still below the maximum, add one TradeVolume tranche to the long side.
For a bearish cross:
If the long exposure already equals the maximum, close the entire long position.
Otherwise, if the short exposure is below the limit, add one TradeVolume tranche to the short side.
The volume cap emulates the original expert’s Int_Max_Pos limit while working with StockSharp’s net positions.
Parameters
Name
Type
Default
Description
CandleType
DataType
1-minute time frame
Primary timeframe used for signals and indicator calculations.
OhlcBarIndex
int
1
Index of the historical candle used for crossover detection (0 = current forming candle, 1 = last finished candle).
MaxPositionsPerSide
int
15
Maximum number of TradeVolume tranches that can be accumulated per direction.
MaPeriod
int
64
Length of the moving average.
MaShift
int
0
Number of bars to shift the MA backwards when checking crossovers.
Money management is simplified: only fixed volume entries are supported. Percent-risk sizing from the original EA is not implemented.
StockSharp works with net positions, so opposite-direction positions are flattened before new exposure is accumulated. The MaxPositionsPerSide limit is enforced on the net exposure in lots.
Indicator values and candle history are processed through the high-level candle subscription API rather than manual buffer copies.
Usage tips
Adjust TradeVolume to match the instrument’s lot step before launching the strategy; the constructor also assigns the same value to Strategy.Volume so helper methods use the desired size by default.
Use MaShift in combination with OhlcBarIndex to recreate the visual alignment from the MetaTrader chart if needed.
Add the strategy to a chart to view candles, the moving-average overlay, and executed trades, which helps with verifying crossover behavior.
Indicators
One configurable moving average (Simple, Exponential, Smoothed, or Weighted).
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
public class LittleEaStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _momentumPeriod;
private decimal? _prevMom;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int MomentumPeriod { get => _momentumPeriod.Value; set => _momentumPeriod.Value = value; }
public LittleEaStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame()).SetDisplay("Candle Type", "Timeframe", "General");
_momentumPeriod = Param(nameof(MomentumPeriod), 10).SetGreaterThanZero().SetDisplay("Momentum Period", "Momentum lookback", "Indicators");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevMom = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevMom = null;
var mom = new Momentum { Length = MomentumPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(mom, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null) { DrawCandles(area, subscription); DrawOwnTrades(area); }
}
private void ProcessCandle(ICandleMessage candle, decimal momVal)
{
if (candle.State != CandleStates.Finished) return;
if (!IsFormedAndOnlineAndAllowTrading()) { _prevMom = momVal; return; }
if (_prevMom == null) { _prevMom = momVal; return; }
if (_prevMom.Value < 100m && momVal >= 100m && Position <= 0) { if (Position < 0) BuyMarket(); BuyMarket(); }
else if (_prevMom.Value > 100m && momVal <= 100m && Position >= 0) { if (Position > 0) SellMarket(); SellMarket(); }
_prevMom = momVal;
}
}
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 Momentum
from StockSharp.Algo.Strategies import Strategy
class little_ea_strategy(Strategy):
def __init__(self):
super(little_ea_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe", "General")
self._momentum_period = self.Param("MomentumPeriod", 10) \
.SetDisplay("Momentum Period", "Momentum lookback", "Indicators")
self._prev_mom = None
@property
def CandleType(self):
return self._candle_type.Value
@property
def MomentumPeriod(self):
return self._momentum_period.Value
def OnReseted(self):
super(little_ea_strategy, self).OnReseted()
self._prev_mom = None
def OnStarted2(self, time):
super(little_ea_strategy, self).OnStarted2(time)
self._prev_mom = None
mom = Momentum()
mom.Length = self.MomentumPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(mom, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _on_process(self, candle, mom_value):
if candle.State != CandleStates.Finished:
return
mv = float(mom_value)
if not self.IsFormedAndOnlineAndAllowTrading():
self._prev_mom = mv
return
if self._prev_mom is None:
self._prev_mom = mv
return
if self._prev_mom < 100.0 and mv >= 100.0 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif self._prev_mom > 100.0 and mv <= 100.0 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_mom = mv
def CreateClone(self):
return little_ea_strategy()