using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _02_bolinger_band_strategy { using System; using StockSharp.Algo; using StockSharp.Algo.Candles; using StockSharp.Algo.Indicators; using StockSharp.Algo.Strategies; using StockSharp.Algo.Testing; class BoligerStrategy_001 : Strategy { private CandleSeries CandleSeries { get; } public BollingerBands BollingerBands { get; set; } public BoligerStrategy_001(CandleSeries series) { CandleSeries = series; } List _buffer = new List(); protected override void OnStarted() { //_candleManager = new CandleManager(Connector);// - out of date //_candleManager.WhenCandlesFinished(_candleSeries).Do(CandleManager_Processing).Apply();// - out of date //_candleManager.Start(_candleSeries);// - out of date Connector.WhenCandlesFinished(CandleSeries).Do(ProcessCandle).Apply(this); Connector.SubscribeCandles(CandleSeries); base.OnStarted(); } private bool IsRealTime(Candle candle) { return (Connector.CurrentTime - candle.CloseTime).TotalSeconds < 10; } private bool IsHistoryEmulationConnector => Connector is HistoryEmulationConnector; private void ProcessCandle(Candle candle) { //if (candle.State == CandleStates.Finished)// проверка окончена свеча или нет _buffer.Insert(0, candle); if (_buffer.Count > 10) _buffer.RemoveAt(10); BollingerBands.Process(candle); if (!BollingerBands.IsFormed) return; //if (!IsHistoryEmulationConnector && !IsRealTime(candle)) return; // Вход в позицию if(Position == 0) { if(candle.OpenPrice < BollingerBands.MovingAverage.GetCurrentValue()) { if(candle.OpenPrice > _buffer[1].ClosePrice) { RegisterOrder(this.BuyAtLimit(candle.OpenPrice, 1)); } } } // выход из позиции if (Position > 0 && candle.OpenPrice > BollingerBands.MovingAverage.GetCurrentValue() && candle.OpenPrice < _buffer[1].ClosePrice) { //RegisterOrder(this.SellAtMarket(1)); RegisterOrder(this.SellAtLimit(candle.OpenPrice, 1)); } } } }