Example #1
0
        protected override decimal OnAdd(Trade value)
        {
            if (value.Security.InitialMargin > 0)
            {
                _futures = value.Price;
            }
            else
            {
                _stock = value.Price * value.Security.LotSize;
            }

            if (_stock != 0 && _futures != 0)
            {
                return(_bollingerBandsTop.Add(_futures - _stock));
            }

            return(Default);
        }
Example #2
0
        protected override decimal OnAdd(Candle candle)
        {
            if (candle.Security.InitialMargin > 0)
            {
                _futures = candle.ClosePrice;
            }
            else
            {
                _stock = candle.ClosePrice * candle.Security.LotSize;
            }

            if (_stock != 0 && _futures != 0)
            {
                return(_bollingerBandsTop.Add(_futures - _stock));
            }

            return(Default);
        }
        private void ProcessCandle(Candle candle)
        {
            try
            {
                //Добавляем новую свечку для расчетов в индикаторы
                //Добавляем до проверки актуальности данных, так как возможна предзагрузка исторических данных
                var top    = _top.Add(candle.ClosePrice);
                var bottom = _bottom.Add(candle.ClosePrice);

                //Проверка актуальности данных
                if (!GetRealTimeData())
                {
                    return;
                }

                //Если индикаторы не сформированы, то ничего не делаем
                if (!_top.IsFormed || !_bottom.IsFormed)
                {
                    return;
                }

                //Текущая позиция по выбранному коннектору и инструменту
                var position = GetTradeInfo().Position;

                if (candle.ClosePrice <= bottom && position <= 0)
                {
                    var limitOrder = new Order
                    {
                        Type      = OrderType.Limit,
                        Direction = Direction.Buy,
                        Volume    = position < 0 ? -position : _volume, //Закрываем полностью позицию или открываем новую
                        Price     = candle.ClosePrice
                    };

                    _customValuesBuy.Add(candle.CloseTime, (double)(limitOrder.Price + GetSecurity().Tick * 25), $"BUY ORDER\nTime: {candle.CloseTime.ToString("dd.MM.yyyy HH:mm:ss.fff")}\nVolume: {limitOrder.Volume}");

                    //Отправляем лимитную заявку на регистрацию
                    RegisterOrder(limitOrder);
                }

                if (candle.ClosePrice >= top && position >= 0)
                {
                    var limitOrder = new Order
                    {
                        Type      = OrderType.Limit,
                        Direction = Direction.Sell,
                        Volume    = position < 0 ? -position : _volume,
                        Price     = candle.ClosePrice
                    };

                    _customValuesSell.Add(candle.CloseTime, (double)(limitOrder.Price - GetSecurity().Tick *25), $"SELL ORDER\nTime: {candle.CloseTime.ToString("dd.MM.yyyy HH:mm:ss.fff")}\nVolume: {limitOrder.Volume}");

                    //Отправляем лимитную заявку на регистрацию
                    RegisterOrder(limitOrder);
                }
            }
            catch (Exception ex)
            {
                ExceptionMessage(ex, "ProcessCandle");
            }
        }