#region S# License /****************************************************************************************** NOTICE!!! This program and source code is owned and licensed by StockSharp, LLC, www.stocksharp.com Viewing or use of this code requires your acceptance of the license agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE Removal of this comment is a violation of the license agreement. Project: SampleHistoryTestingParallel.SampleHistoryTestingParallelPublic File: SmaStrategy.cs Created: 2015, 11, 11, 2:32 PM Copyright 2010 by StockSharp, LLC *******************************************************************************************/ #endregion S# License namespace SampleHistoryTestingParallel { using Ecng.Common; using StockSharp.Algo; using StockSharp.Algo.Candles; using StockSharp.Algo.Indicators; using StockSharp.Algo.Strategies; using StockSharp.Messages; class SmaStrategy : Strategy { private readonly CandleSeries _series; private bool _isShortLessThenLong; private readonly StrategyParam _longSmaPeriod; public int LongSmaPeriod => _longSmaPeriod.Value; private readonly StrategyParam _shortSmaPeriod; public int ShortSmaPeriod => _shortSmaPeriod.Value; public SmaStrategy(CandleSeries series, SimpleMovingAverage longSma, SimpleMovingAverage shortSma) { _series = series; LongSma = longSma; ShortSma = shortSma; _longSmaPeriod = this.Param("LongSmaPeriod", longSma.Length); _shortSmaPeriod = this.Param("ShortSmaPeriod", shortSma.Length); } public SimpleMovingAverage LongSma { get; } public SimpleMovingAverage ShortSma { get; } protected override void OnStarted() { this .WhenCandlesFinished(_series) .Do(ProcessCandle) .Apply(this); // store current values for short and long _isShortLessThenLong = ShortSma.GetCurrentValue() < LongSma.GetCurrentValue(); Start(_series); base.OnStarted(); } protected override void OnStopped() { Stop(_series); base.OnStopped(); } private void ProcessCandle(Candle candle) { // strategy are stopping if (ProcessState == ProcessStates.Stopping) { CancelActiveOrders(); return; } // process new candle LongSma.Process(candle); ShortSma.Process(candle); // calc new values for short and long var isShortLessThenLong = ShortSma.GetCurrentValue() < LongSma.GetCurrentValue(); // crossing happened if (_isShortLessThenLong != isShortLessThenLong) { // if short less than long, the sale, otherwise buy var direction = isShortLessThenLong ? Sides.Sell : Sides.Buy; // calc size for open position or revert var volume = Position == 0 ? Volume : Position.Abs() * 2; // register order (limit order) RegisterOrder(this.CreateOrder(direction, (decimal)(Security.GetCurrentPrice(this, direction) ?? 0), volume)); // or revert position via market quoting //var strategy = new MarketQuotingStrategy(direction, volume); //ChildStrategies.Add(strategy); // store current values for short and long _isShortLessThenLong = isShortLessThenLong; } } } }