GitHub で見る

Kloss MQL/8186 Strategy

The Kloss MQL/8186 Strategy is a direct conversion of the MetaTrader 4 expert advisor Kloss.mq4. It combines a Commodity Channel Index (CCI), a Stochastic oscillator, and a shifted typical price filter to time single-position reversals. The StockSharp version keeps the original entry thresholds, stop-loss and take-profit distances, and volume logic (fixed lot size or percentage-based sizing) while using the high-level candle subscription API.

Trading Logic

  • Data: Completed candles of the configured timeframe (default 5 minutes). Indicators are calculated on the same series.
  • Indicators:
    • CCI with period 10. The absolute value is compared against ±CciThreshold (default 120).
    • Stochastic oscillator with %K=5, %D=3, smoothing =3. The main %K line is checked against oversold/overbought bands.
    • Typical price ( (High + Low + Close) / 3 ) delayed by five completed candles to replicate the shifted LWMA from the expert advisor.
  • Long Entry:
    • CCI <= -CciThreshold.
    • Stochastic %K < StochasticOversold (default 30).
    • Previous candle open > typical price from five candles ago.
    • No existing long position (Position <= 0). Any open short is closed and reversed into a long in a single market order.
  • Short Entry:
    • CCI >= CciThreshold.
    • Stochastic %K > StochasticOverbought (default 70).
    • Previous candle close < typical price from five candles ago.
    • No existing short position (Position >= 0). Any open long is closed and reversed into a short with one market order.
  • Position Management: StockSharp's StartProtection issues stop-loss and take-profit orders automatically using the specified point distances. The strategy otherwise holds a single position at all times (flat, long, or short).

Position Sizing

  • Fixed Volume: If FixedVolume > 0, the strategy always trades that exact volume (after aligning to the instrument's VolumeStep and MinVolume).
  • Risk Percent: When FixedVolume = 0, the strategy allocates RiskPercent (default 0.2) of the account value divided by the latest close to estimate order size. The volume is clamped by MaxVolume (default 5) and rounded to the instrument's step.
  • Safeguards: The method falls back to the minimum tradable volume if account information is missing or the computed value is non-positive.

Parameters

Name Description Default
CciPeriod Number of candles used to calculate the Commodity Channel Index. 10
CciThreshold Absolute CCI level that triggers entries. 120
StochasticKPeriod %K period of the Stochastic oscillator. 5
StochasticDPeriod %D smoothing period. 3
StochasticSmooth Additional smoothing applied to %K before the signal. 3
StochasticOversold %K threshold to confirm long entries. 30
StochasticOverbought %K threshold to confirm short entries. 70
StopLossPoints Distance in price points for the protective stop. 48
TakeProfitPoints Distance in price points for the profit target. 152
FixedVolume Positive value forces a fixed trade volume. 0
RiskPercent Portfolio fraction converted to volume when FixedVolume is zero. 0.2
MaxVolume Maximum allowed trade volume. 5
CandleType Candle type/timeframe for indicator calculations. 5-minute time frame

Execution Notes

  • Single Position: Only one position is kept open. Reversals close the existing position and open the new one with a single market order.
  • Indicator Synchronisation: The price shift uses the last five completed candles; at least six candles must be processed before the first trade can appear.
  • Stops/Targets: StartProtection converts point-based distances into absolute price offsets using the instrument's PriceStep. If PriceStep is unknown, the raw point value is applied.
  • Data Requirements: Works with any instrument providing OHLC candles; volume alignment honours MinVolume and VolumeStep when available.
  • Differences vs. MT4: MetaTrader margin calculations are approximated via account equity (Portfolio.CurrentValue). When equity data is not available, the strategy reverts to the minimal tradable volume.

Usage Tips

  1. Adjust CandleType to the market session used in MetaTrader (M5 in the original template).
  2. Review stop distances relative to tick size; point-to-price conversion happens automatically but the values may need tuning for non-forex instruments.
  3. For fixed contract sizes, set FixedVolume to the desired lot and RiskPercent to zero.
  4. Enable optimization for the indicator thresholds when calibrating the strategy on new symbols.
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>
/// Strategy converted from the MetaTrader 4 "Kloss" expert advisor (MQL/8186).
/// Implements the original combination of CCI and Stochastic oscillators with a price shift filter.
/// </summary>
public class KlossMql8186Strategy : Strategy
{
        private readonly StrategyParam<int> _cciPeriod;
        private readonly StrategyParam<decimal> _cciThreshold;
        private readonly StrategyParam<int> _stochasticKPeriod;
        private readonly StrategyParam<int> _stochasticDPeriod;
        private readonly StrategyParam<int> _stochasticSmooth;
        private readonly StrategyParam<decimal> _stochasticOversold;
        private readonly StrategyParam<decimal> _stochasticOverbought;
        private readonly StrategyParam<decimal> _stopLossPoints;
        private readonly StrategyParam<decimal> _takeProfitPoints;
        private readonly StrategyParam<decimal> _fixedVolume;
        private readonly StrategyParam<decimal> _riskPercent;
        private readonly StrategyParam<decimal> _maxVolume;
        private readonly StrategyParam<DataType> _candleType;

        private CommodityChannelIndex _cci = null!;
        private StochasticOscillator _stochastic = null!;

        private decimal? _previousOpen;
        private decimal? _previousClose;
        private readonly decimal?[] _typicalHistory = new decimal?[5];

        /// <summary>
        /// Initializes a new instance of the <see cref="KlossMql8186Strategy"/> class.
        /// </summary>
        public KlossMql8186Strategy()
        {
                _cciPeriod = Param(nameof(CciPeriod), 10)
                        .SetGreaterThanZero()
                        .SetDisplay("CCI Period", "Number of candles for the CCI calculation", "Indicators")
                        
                        .SetOptimize(5, 40, 5);

                _cciThreshold = Param(nameof(CciThreshold), 150m)
                        .SetGreaterThanZero()
                        .SetDisplay("CCI Threshold", "Absolute CCI level that triggers entries", "Indicators")
                        
                        .SetOptimize(80m, 200m, 10m);

                _stochasticKPeriod = Param(nameof(StochasticKPeriod), 5)
                        .SetGreaterThanZero()
                        .SetDisplay("Stochastic %K", "Period of the %K line", "Indicators")
                        
                        .SetOptimize(3, 15, 1);

                _stochasticDPeriod = Param(nameof(StochasticDPeriod), 3)
                        .SetGreaterThanZero()
                        .SetDisplay("Stochastic %D", "SMA length of the %D line", "Indicators")

                        .SetOptimize(1, 10, 1);

                _stochasticSmooth = Param(nameof(StochasticSmooth), 3)
                        .SetGreaterThanZero()
                        .SetDisplay("Stochastic Smoothing", "Smoothing applied to the %K calculation", "Indicators")
                        
                        .SetOptimize(1, 10, 1);

                _stochasticOversold = Param(nameof(StochasticOversold), 45m)
                        .SetNotNegative()
                        .SetDisplay("Stochastic Oversold", "Threshold under which %K confirms a long signal", "Signals")
                        
                        .SetOptimize(10m, 40m, 5m);

                _stochasticOverbought = Param(nameof(StochasticOverbought), 55m)
                        .SetNotNegative()
                        .SetDisplay("Stochastic Overbought", "Threshold above which %K confirms a short signal", "Signals")
                        
                        .SetOptimize(60m, 90m, 5m);

                _stopLossPoints = Param(nameof(StopLossPoints), 48m)
                        .SetNotNegative()
                        .SetDisplay("Stop Loss (pts)", "Stop loss distance expressed in price points", "Risk");

                _takeProfitPoints = Param(nameof(TakeProfitPoints), 152m)
                        .SetNotNegative()
                        .SetDisplay("Take Profit (pts)", "Take profit distance expressed in price points", "Risk");

                _fixedVolume = Param(nameof(FixedVolume), 0m)
                        .SetNotNegative()
                        .SetDisplay("Fixed Volume", "If greater than zero this volume will be used for orders", "Trading");

                _riskPercent = Param(nameof(RiskPercent), 0.2m)
                        .SetNotNegative()
                        .SetDisplay("Risk Percent", "Fraction of portfolio value used when Fixed Volume is zero", "Trading");

                _maxVolume = Param(nameof(MaxVolume), 5m)
                        .SetGreaterThanZero()
                        .SetDisplay("Max Volume", "Upper limit for the trade volume", "Trading");

                _candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
                        .SetDisplay("Candle Type", "Timeframe used for indicator calculations", "General");
        }

        /// <summary>
        /// CCI lookback period.
        /// </summary>
        public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }

        /// <summary>
        /// Absolute CCI level that triggers entries.
        /// </summary>
        public decimal CciThreshold { get => _cciThreshold.Value; set => _cciThreshold.Value = value; }

        /// <summary>
        /// Stochastic %K period.
        /// </summary>
        public int StochasticKPeriod { get => _stochasticKPeriod.Value; set => _stochasticKPeriod.Value = value; }

        /// <summary>
        /// Stochastic %D period.
        /// </summary>
        public int StochasticDPeriod { get => _stochasticDPeriod.Value; set => _stochasticDPeriod.Value = value; }

        /// <summary>
        /// Stochastic smoothing factor.
        /// </summary>
        public int StochasticSmooth { get => _stochasticSmooth.Value; set => _stochasticSmooth.Value = value; }

        /// <summary>
        /// Stochastic oversold threshold.
        /// </summary>
        public decimal StochasticOversold { get => _stochasticOversold.Value; set => _stochasticOversold.Value = value; }

        /// <summary>
        /// Stochastic overbought threshold.
        /// </summary>
        public decimal StochasticOverbought { get => _stochasticOverbought.Value; set => _stochasticOverbought.Value = value; }

        /// <summary>
        /// Stop loss distance in points.
        /// </summary>
        public decimal StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }

        /// <summary>
        /// Take profit distance in points.
        /// </summary>
        public decimal TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

        /// <summary>
        /// Fixed trading volume.
        /// </summary>
        public decimal FixedVolume { get => _fixedVolume.Value; set => _fixedVolume.Value = value; }

        /// <summary>
        /// Fraction of the portfolio value converted into volume when <see cref="FixedVolume"/> is zero.
        /// </summary>
        public decimal RiskPercent { get => _riskPercent.Value; set => _riskPercent.Value = value; }

        /// <summary>
        /// Maximum volume allowed per trade.
        /// </summary>
        public decimal MaxVolume { get => _maxVolume.Value; set => _maxVolume.Value = value; }

        /// <summary>
        /// Candle type used for indicator calculations.
        /// </summary>
        public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

        /// <inheritdoc />
        public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
        {
                return [(Security, CandleType)];
        }

        /// <inheritdoc />
        protected override void OnReseted()
        {
                base.OnReseted();

                _previousOpen = null;
                _previousClose = null;
                Array.Clear(_typicalHistory, 0, _typicalHistory.Length);
        }

        /// <inheritdoc />
        protected override void OnStarted2(DateTime time)
        {
                base.OnStarted2(time);

                _cci = new CommodityChannelIndex { Length = CciPeriod };
                _stochastic = new StochasticOscillator();
                _stochastic.K.Length = StochasticKPeriod;
                _stochastic.D.Length = StochasticDPeriod;

                var subscription = SubscribeCandles(CandleType);

                subscription
                        .Bind(ProcessCandle)
                        .Start();

                // Configure automatic position protection for stop loss and take profit.
                StartProtection(
                        takeProfit: CreatePriceUnit(TakeProfitPoints),
                        stopLoss: CreatePriceUnit(StopLossPoints));
        }

        private void ProcessCandle(ICandleMessage candle)
        {
                if (candle.State != CandleStates.Finished)
                        return;

                var cciResult = _cci.Process(candle).ToNullableDecimal();
                var stochResult = _stochastic.Process(candle);

                UpdateHistory(candle);

                if (cciResult is null)
                        return;

                if (!_stochastic.IsFormed)
                        return;

                var stochValue = (StochasticOscillatorValue)stochResult;
                if (stochValue.K is not decimal stochMain)
                        return;

                if (!IsFormedAndOnlineAndAllowTrading())
                        return;

                var cci = cciResult.Value;

                if (_previousOpen is decimal prevOpen &&
                        _previousClose is decimal prevClose &&
                        _typicalHistory[4] is decimal shiftedTypical)
                {
                        var buySignal = cci <= -CciThreshold && stochMain < StochasticOversold && prevOpen > shiftedTypical;
                        var sellSignal = cci >= CciThreshold && stochMain > StochasticOverbought && prevClose < shiftedTypical;

                        if (buySignal && Position <= 0)
                                BuyMarket();
                        else if (sellSignal && Position >= 0)
                                SellMarket();
                }
        }

        private void UpdateHistory(ICandleMessage candle)
        {
                // Shift stored typical prices to keep five previous values available.
                for (var i = _typicalHistory.Length - 1; i > 0; i--)
                        _typicalHistory[i] = _typicalHistory[i - 1];

                _typicalHistory[0] = (candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 3m;

                // Store previous candle prices for the next iteration.
                _previousOpen = candle.OpenPrice;
                _previousClose = candle.ClosePrice;
        }

        private Unit CreatePriceUnit(decimal points)
        {
                if (points <= 0)
                        return new Unit(0m, UnitTypes.Absolute);

                if (Security?.PriceStep is decimal priceStep && priceStep > 0)
                        return new Unit(points * priceStep, UnitTypes.Absolute);

                return new Unit(points, UnitTypes.Absolute);
        }
}