예제 #1
0
        /// <summary>
        ///     Обработчик события получения списка счетов
        /// </summary>
        private void AccountResolved(AdapterEventArgs <AccountsReport> args)
        {
            args.MarkHandled();

            // Инициализируем счета
            if (args.Message.brokerage != null)
            {
                using (accountsLock.WriteLock())
                {
                    foreach (
                        var account in
                        from brokerage in args.Message.brokerage
                        from ss in brokerage.sales_series
                        from account in ss.account
                        select account)
                    {
                        Logger.Debug().PrintFormat("Account resolved: {0};{1};{2}", account.brokerage_account_id, account.account_id, account.name);
                        AddAccount(account.name);

                        accountCodesById[account.account_id] = account.name;
                        accountIdsByCode[account.name]       = account.account_id;
                    }
                }

                OnMessageReceived(
                    new InitResponseMessage
                {
                    OrderRouters = new Dictionary <string, string[]>
                    {
                        { ServiceCode, AvailableAccounts.ToArray() }
                    }
                });
            }

            // Подписываемся на счета
            var subscription = new TradeSubscription
            {
                publication_type   = (uint)TradeSubscription.PublicationType.ALL_AUTHORIZED,
                subscription_scope =
                {
                    (uint)TradeSubscription.SubscriptionScope.ORDERS,
                    (uint)TradeSubscription.SubscriptionScope.POSITIONS,
                    (uint)TradeSubscription.SubscriptionScope.COLLATERAL
                },
                subscribe = true,
                id        = adapter.GetNextRequestId()
            };

            adapter.SendMessage(subscription);
        }
예제 #2
0
        /// <summary>
        ///     Получить Contract ID для инструмента
        /// </summary>
        /// <param name="instrument">
        ///     Инструмент
        /// </param>
        /// <returns>
        ///     Contract ID
        /// </returns>
        public async Task <uint> GetContractIdAsync(Instrument instrument)
        {
            using (LogManager.Scope())
            {
                // Ищем ID контракта в кеше
                uint contractId;
                using (cacheLock.Lock())
                {
                    if (cachedContractIds.TryGetValue(instrument, out contractId))
                    {
                        return(contractId);
                    }
                }

                // Ищем или создаем запрос инструмента
                ResolutionRequest request;
                var sendRequest = false;
                using (resolutionRequestsLock.Lock())
                {
                    if (!resolutionRequestsByInstrument.TryGetValue(instrument, out request))
                    {
                        var requestId = adapter.GetNextRequestId();
                        request = new ResolutionRequest(requestId, instrument);

                        resolutionRequestsById.Add(requestId, request);
                        resolutionRequestsByInstrument.Add(instrument, request);

                        sendRequest = true;
                    }
                }

                // Отправляем запрос инструмента, если он был создан
                if (sendRequest)
                {
                    await SendResolutionRequestAsync(request);
                }

                // Дожидаемся отработки запроса
                contractId = await request.Task;
                return(contractId);
            }
        }
        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);
        }