Example #1
0
        /// <summary>
        ///     Постановка заявки
        /// </summary>
        private async void SendTransactionInternal(NewOrderTransaction transaction)
        {
            using (LogManager.Scope())
            {
                try
                {
                    // Получаем счет для транзакции
                    int accountId;
                    var hasAccountId = true;
                    using (accountsLock.ReadLock())
                    {
                        if (!accountIdsByCode.TryGetValue(transaction.Account, out accountId))
                        {
                            hasAccountId = false;
                        }
                    }

                    if (!hasAccountId)
                    {
                        OnMessageReceived(TransactionReply.Rejected(
                                              transaction,
                                              $"Account \"{transaction.Account}\" is unknown"));
                        return;
                    }

                    // Получаем инструмент для транзации
                    uint contractId;
                    try
                    {
                        contractId = await instrumentResolver.GetContractIdAsync(transaction.Instrument);

                        if (contractId == uint.MaxValue)
                        {
                            return;
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        OnMessageReceived(TransactionReply.Rejected(
                                              transaction,
                                              $"Instrument \"{transaction.Instrument.Code}\" is unknown"));
                        return;
                    }

                    // Пребразовываем цену
                    var price = instrumentResolver.ConvertPrice(contractId, transaction.Price);
                    if (price == null)
                    {
                        OnMessageReceived(TransactionReply.Rejected(transaction, "Unable to convert price"));
                        return;
                    }

                    // Формируем запрос
                    var msg = new OrderRequest
                    {
                        new_order = new NewOrder
                        {
                            order = new WebAPI.Order
                            {
                                cl_order_id = transaction.TransactionId.ToString("N"),
                                account_id  = accountId,
                                contract_id = contractId,
                                side        = (uint)ConvertionHelper.GetSide(transaction.Operation),
                                order_type  = (uint)ConvertionHelper.GetOrderType(transaction.Type),
                                duration    = (uint)ConvertionHelper.GetDuration(transaction.ExecutionCondition),
                                limit_price = price.Value,
                                qty         = transaction.Quantity,
                                is_manual   = transaction.IsManual,

                                user_attribute =
                                {
                                    new UserAttribute
                                    {
                                        name  = CommentAttributeName,
                                        value = transaction.Comment
                                    }
                                }
                            },
                            suspend = false
                        },
                        request_id = adapter.GetNextRequestId()
                    };

                    // Запоминаем заявку
                    using (ordersLock.Lock())
                    {
                        var order = new Order(transaction);
                        ordersByTransactionId.Add(transaction.TransactionId, order);
                    }

                    // Запоминаем транзакцию
                    StoreTransaction(msg.request_id, transaction);

                    Logger.Debug().PrintFormat("Sending {0}", transaction);
                    adapter.SendMessage(msg);
                }
                catch (Exception e)
                {
                    Logger.Error().Print(e, $"Failed to send {transaction}");
                    OnMessageReceived(TransactionReply.Rejected(transaction, "Unable to send order"));
                }
            }
        }
Example #2
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
                });
            }
        }