예제 #1
0
        private async Task CandleProcessingProc(CandleResponse cr)
        {
            var candle = cr.Payload;
            var stock  = _mainModel.Stocks.Values.FirstOrDefault(s => s.Figi == candle.Figi);

            if (stock != null)
            {
                //stock.IsNotifying = false;
                if (candle.Interval == CandleInterval.Day)
                {
                    if (Instruments.TryGetValue(stock.Ticker, out var instrument))
                    {
                        if (candle.Time.Date < instrument.MarketStartDate.Date && stock.LastUpdate > DateTime.MinValue)
                        {
                            return;
                        }
                    }
                    stock.TodayOpen  = candle.Open;
                    stock.TodayDate  = candle.Time.ToLocalTime();
                    stock.LastUpdate = DateTime.Now;
                    stock.Price      = candle.Close;
                    if (stock.TodayOpen > 0)
                    {
                        stock.DayChange = (stock.Price - stock.TodayOpen) / stock.TodayOpen;
                    }
                    stock.DayVolume = Math.Truncate(candle.Volume);
                    if (stock.AvgDayVolumePerMonth > 0)
                    {
                        stock.DayVolChgOfAvg = stock.DayVolume / stock.AvgDayVolumePerMonth;
                    }
                    await _mainModel.OnStockUpdated(stock);

                    if (!_subscribedMinuteFigi.Contains(stock.Figi))
                    {
                        QueueBrokerAction(b => b.SendStreamingRequestAsync(
                                              SubscribeCandle(stock.Figi, CandleInterval.Minute)),
                                          $"Подписка на минутную свечу {stock.Ticker} ({stock.Figi})");
                        _subscribedMinuteFigi.Add(stock.Figi);
                    }
                }
                else if (candle.Interval == CandleInterval.Minute)
                {
                    if (candle.Time.Date > stock.TodayDate && candle.Time.ToLocalTime().Hour > 3 && stock.MinuteCandles.Count > 1)
                    {
                        await ResetConnection(
                            $"Новый день ({stock.Ticker} {stock.TodayDate} -> {candle.Time.Date})");

                        return;
                    }
                    stock.LastUpdate = DateTime.Now;
                    stock.LogCandle(candle);
                    await _mainModel.OnStockUpdated(stock);
                }
            }
        }
예제 #2
0
        public void DeserializeCandleTest()
        {
            var streamingResponse = JsonSerializer.Deserialize <StreamingResponse>(JsonFile.Read("streaming-candle-response"), new JsonSerializerOptions(JsonSerializerDefaults.Web));
            var response          = streamingResponse as StreamingResponse <CandlePayload>;

            var expectedResponse = new CandleResponse(
                new CandlePayload(
                    64.0575m,
                    64.0578m,
                    64.0579m,
                    64.0573m,
                    156,
                    new DateTime(2019, 08, 07, 15, 35, 00, DateTimeKind.Utc),
                    CandleInterval.FiveMinutes,
                    "BBG0013HGFT4"),
                new DateTime(2019, 08, 07, 15, 35, 01, 029, DateTimeKind.Utc).AddTicks(7212));

            response.Should().BeEquivalentTo(expectedResponse);
        }
예제 #3
0
 // Candle Websocket
 private void CandleMessageHandler(CandleResponse messageData)
 {
     var data = messageData;
 }
예제 #4
0
        /// <summary>
        /// Process the incoming messages
        /// </summary>
        /// <param name="message">Incoming message</param>
        private void processMessage(string message)
        {
            // Deserialize the incoming command to check wich message has just arrived
            // The action property brings the command name
            switch (Command.Deserialize(message).action)
            {
            case KeepAlive.CLASSNAME:
            {           // If it is a KeepAlive message, just answer Quantsis Conneciton Box another KeepAlive message
                        //CheckAsyncTaskInProgress
                this.send(new KeepAlive().Serialize());
                break;
            }

            case NewOrderResponse.CLASSNAME:
            {           // If it is an NewOrderResponse message and it contains error, show the error message
                var cmd = NewOrderResponse.Deserialize(message);
                if (!cmd.success)
                {
                    NotifyOMS(cmd.ToString());
                }
                break;
            }

            case NewStopOrderResponse.CLASSNAME:
            {           // If it is an NewStopOrderResponse message and it contains error, show the error message
                var cmd = NewStopOrderResponse.Deserialize(message);
                if (!cmd.success)
                {
                    NotifyOMS(cmd.ToString());
                }
                break;
            }

            case NewStopGainLossOrderResponse.CLASSNAME:
            {           // If it is an NewStopGainLossOrderResponse message and it contains error, show the error message
                var cmd = NewStopGainLossOrderResponse.Deserialize(message);
                if (!cmd.success)
                {
                    NotifyOMS(cmd.ToString());
                }
                break;
            }

            case CancelOrderResponse.CLASSNAME:
            {           // If it is an CancelOrderResponse message and it contains error, show the error message
                var cmd = CancelOrderResponse.Deserialize(message);
                if (!cmd.success)
                {
                    NotifyOMS(cmd.ToString());
                }
                break;
            }

            case ExecutionReport.CLASSNAME:
            {           // If it is an ExecutionReport message, check if it was successful and updates the order list or show error message
                var cmd = ExecutionReport.Deserialize(message);
                if (cmd.success)
                {
                    NotifyOMS(cmd.ToString());
                }
                else
                {
                    NotifyOMS(cmd.ToString());
                }

                break;
            }

            case OrderListResponse.CLASSNAME:
            {           // If it is an OrderListResponse, check if it was successful. In negative case show error message.
                var cmd = OrderListResponse.Deserialize(message);
                if (!cmd.success)
                {
                    NotifyOMS(cmd.ToString());
                }
                break;
            }

            case PositionResponse.CLASSNAME:
            {           // If it is a Position Response, check if it was successful and updates the position list. In case of failure, show error messasge
                var cmd = PositionResponse.Deserialize(message);
                if (cmd.success)
                {
                    NotifyOMS(cmd.ToString());
                }
                else
                {
                    NotifyOMS(cmd.ToString());
                }

                break;
            }

            case QuoteResponse.CLASSNAME:
            {           // If it is a Quote Response, check if it was successful and logs the message
                var cmd = QuoteResponse.Deserialize(message);
                if (cmd.success)
                {
                    OnQuoteResponse(cmd);
                }
                else
                {
                    NotifyQCB(cmd.ToString());
                }
                break;
            }

            case QuoteUpdate.CLASSNAME:
            {           // If it is a Quote Update, check if it was successful and logs the message
                var cmd = QuoteUpdate.Deserialize(message);
                if (cmd.success)
                {
                    OnQuoteUpdate(cmd);
                }
                else
                {
                    NotifyQCB(cmd.ToString());
                }
                break;
            }

            case QuoteUnsubscribeResponse.CLASSNAME:
            {           // If it is a Quote Unsubscribe Response, check if it was successful
                var cmd = QuoteUnsubscribeResponse.Deserialize(message);
                if (cmd.success)
                {
                    NotifyOMS(cmd.ToString());
                }
                else
                {
                    NotifyOMS(cmd.ToString());
                }
                break;
            }

            case CandleResponse.CLASSNAME:
            {           // If it is a Quote Update, check if it was successful and logs the message
                var cmd = CandleResponse.Deserialize(message);
                if (cmd.success)
                {
                    if (cmd.candles != null)
                    {
                        if (cmd.candles.Count <= 0)
                        {
                            NotifyOMS("Nenhum foi possivel retornar nenhum candle para " + cmd.security + " - Timeframe: " + cmd.timeframe);
                        }
                        else
                        {
                            NotifyOMS("Dados Historicos Intraday para: " + cmd.security + " - Qtd de Candles: " + cmd.candles.Count + " - Timeframe: " + cmd.timeframe);
                            //foreach (Candle candle in cmd.candles)
                            //    this.mainForm.log(candle.ToString());
                        }
                    }
                }
                else
                {
                    NotifyOMS(cmd.ToString());
                }

                break;
            }

            // Candle Update
            case CandleUpdate.CLASSNAME:
            {           // If it is a Candle Update, check if it was successful and logs the message
                var cmd = CandleUpdate.Deserialize(message);

                if (cmd.success)
                {
                    if (cmd.candle != null)
                    {
                        switch (cmd.timeframe)
                        {
                        case CandleUpdate.TIMEFRAME_INTRADAY_1_MIN:
                            NotifyOMS("Candle Intraday Update: " + cmd.security + " - Last: " + cmd.candle.close + " - Time: " + cmd.candle.date);
                            break;

                        case CandleUpdate.TIMEFRAME_DAILY:
                            NotifyOMS("Candle Daily Update: " + cmd.security + " - Last: " + cmd.candle.close + " - Time: " + cmd.candle.date);
                            break;
                        }
                    }
                }
                //this.mainForm.updatePositions(cmd);
                else
                {
                    NotifyOMS(cmd.ToString());
                }

                break;
            }

            // QCB has disconnected (shut down)
            case Disconnect.CLASSNAME:
                this.disconnect();
                break;
            }
        }