Beispiel #1
0
        /// <summary>
        ///     Обработка заявки как новой
        /// </summary>
        private Message HandleOrderStateAsNewOrder(OrderStatus message, out Order order)
        {
            Logger.Debug().Print(
                "New order received",
                LogFields.ExchangeOrderId(message.order_id),
                LogFields.ChainOrderId(message.chain_order_id),
                LogFields.State(ConvertionHelper.GetOrderState(message)),
                LogFields.AccountId(message.account_id),
                LogFields.ExecOrderId(message.exec_order_id)
                );

            // Ищем счет для заявки
            string accountCode;

            using (accountsLock.ReadLock())
            {
                if (!accountCodesById.TryGetValue(message.account_id, out accountCode))
                {
                    Logger.Error().Print(
                        "Unable to process order: account is unknown",
                        LogFields.ExchangeOrderId(message.order_id),
                        LogFields.AccountId(message.account_id)
                        );
                    order = null;
                    return(null);
                }
            }

            // Ищем инструмент для заявки
            var instrument = instrumentResolver.GetInstrument(message.order.contract_id);

            if (instrument == null)
            {
                // Пришла заявка по неизвестному инструменту
                Logger.Error().Print(
                    "Received an order but no matching instrument is found. Put to pending orders",
                    LogFields.ExchangeOrderId(message.order_id),
                    LogFields.ContractId(message.order.contract_id),
                    LogFields.Account(accountCode)
                    );

                using (ordersLock.Lock())
                {
                    pendingOrders.Enqueue(message);
                    pendingOrderAddedEvent.Set();
                }

                order = null;
                return(null);
            }

            using (ordersLock.Lock())
            {
                if (ordersByOrderExchangeId.TryGetValue(message.chain_order_id, out order))
                {
                    // Race condition, такая заявка уже есть, надобно обработать ее через OSCM
                    return(HandleOrderStateAsOrderStateChange(order, message));
                }

                Guid transactionId;
                if (Guid.TryParse(message.order.cl_order_id, out transactionId))
                {
                    if (ordersByTransactionId.TryGetValue(transactionId, out order))
                    {
                        // Такая заявка уже есть, надобно обработать ее через OSCM
                        order.OrderExchangeId = message.chain_order_id;
                        ordersByOrderExchangeId.Add(message.chain_order_id, order);

                        return(HandleOrderStateAsOrderStateChange(order, message));
                    }
                }
                else
                {
                    transactionId = Guid.Empty;
                }

                // Создаем новую внешнюю заявку
                order = new Order
                {
                    OrderExchangeId = message.chain_order_id,
                    Instrument      = instrument,
                    Type            = ConvertionHelper.GetOrderType(message),
                    Account         = accountCode,
                    Price           = instrumentResolver.ConvertPriceBack(message.order.contract_id, message.order.limit_price),
                    Quantity        = message.order.qty,
                    ActiveQuantity  = message.remaining_qty,
                    Operation       = message.order.side == (uint)WebAPI.Order.Side.BUY ? OrderOperation.Buy : OrderOperation.Sell,
                    State           = ConvertionHelper.GetOrderState(message),
                    DateTime        = adapter.ResolveDateTime(message.status_utc_time),
                    Comment         = ConvertionHelper.GetComment(message),
                    TransactionId   = transactionId
                };

                ordersByOrderExchangeId.Add(message.chain_order_id, order);
                orderStatusByChainOrderId[message.chain_order_id] = message;

                return(new ExternalOrderMessage {
                    Order = order
                });
            }
        }
        private async Task <TimeBarRequest> PrepareTimeBarRequestAsync(
            Instrument instrument,
            DateTime begin,
            DateTime end,
            HistoryProviderSpan span,
            TimeBarRequest.RequestType type)
        {
            var contractId = await instrumentResolver.GetContractIdAsync(instrument);

            if (contractId == uint.MaxValue)
            {
                return(await Task.FromResult <TimeBarRequest>(null));
            }

            TimeBarParameters.BarUnit barUnit;
            uint unitsNumber = 0;

            switch (span)
            {
            case HistoryProviderSpan.Minute:
                barUnit     = TimeBarParameters.BarUnit.MIN;
                unitsNumber = 1;
                break;

            case HistoryProviderSpan.Minute5:
                barUnit     = TimeBarParameters.BarUnit.MIN;
                unitsNumber = 5;
                break;

            case HistoryProviderSpan.Minute10:
                barUnit     = TimeBarParameters.BarUnit.MIN;
                unitsNumber = 10;
                break;

            case HistoryProviderSpan.Minute15:
                barUnit     = TimeBarParameters.BarUnit.MIN;
                unitsNumber = 15;
                break;

            case HistoryProviderSpan.Minute30:
                barUnit     = TimeBarParameters.BarUnit.MIN;
                unitsNumber = 30;
                break;

            case HistoryProviderSpan.Hour:
                barUnit     = TimeBarParameters.BarUnit.HOUR;
                unitsNumber = 1;
                break;

            case HistoryProviderSpan.Hour4:
                barUnit     = TimeBarParameters.BarUnit.HOUR;
                unitsNumber = 4;
                break;

            case HistoryProviderSpan.Day:
                // Для DAY units_number не заполняется
                barUnit = TimeBarParameters.BarUnit.DAY;
                break;

            case HistoryProviderSpan.Week:
                // Для WEEK units_number не заполняется
                barUnit = TimeBarParameters.BarUnit.WEEK;
                break;

            case HistoryProviderSpan.Month:
                // Для MONTH units_number не заполняется
                barUnit = TimeBarParameters.BarUnit.MONTH;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(span), span, $"Invalid time span - {span}");
            }

            var message = new TimeBarRequest
            {
                request_id          = adapter.GetNextRequestId(),
                request_type        = (uint)type,
                time_bar_parameters = new TimeBarParameters
                {
                    contract_id   = contractId,
                    bar_unit      = (uint)barUnit,
                    units_number  = unitsNumber,
                    from_utc_time = adapter.ResolveDateTime(begin),
                    to_utc_time   = adapter.ResolveDateTime(end)
                }
            };

            return(message);
        }