Exemplo n.º 1
0
 private static void SendRequest(SearchRequest request, int requestId, IConnection connection)
 {
     CodeContract.Requires(connection != null);
     var message = RequestContractDetailsMessage.Create(request);
     message.RequestId = requestId;
     connection.SendMessage(message);
 }
Exemplo n.º 2
0
 public Task<IReadOnlyCollection<Contract>> FindContracts(SearchRequest request,
     CancellationToken cancellationToken)
 {
     using (
         var cancellationTokenSource =
             CancellationTokenSource.CreateLinkedTokenSource(this.internalCancelationTokenSource.Token,
                 cancellationToken))
     {
         return this.objectsFactory.CreateAsyncFindContractOperation(request, cancellationTokenSource.Token);
     }
 }
Exemplo n.º 3
0
        public FindContractsOperation(IConnection connection, IIdsDispenser dispenser, SearchRequest request,
            CancellationToken cancellationToken)
        {
            CodeContract.Requires(connection != null);
            CodeContract.Requires(dispenser != null);

            cancellationToken.Register(() =>
            {
                this.subscriptions.Unsubscribe();
                this.taskCompletionSource.TrySetCanceled();
            });

            this.resultsToRetireve = request.NumberOfResults ?? int.MaxValue;
            this.Subscribe(connection, dispenser, request);
        }
Exemplo n.º 4
0
        private void Subscribe(IConnection connection, IIdsDispenser dispenser, SearchRequest request)
        {
            CodeContract.Requires(connection != null);
            CodeContract.Requires(dispenser != null);

            var requestId = dispenser.NextRequestId();
            
            this.subscriptions = new List<IDisposable>
            {
                connection.Subscribe((ContractDataMessage message) => message.RequestId == requestId,
                    this.OnContractDataMessage),
                connection.Subscribe((ContractDataEndMessage message) => message.RequestId == requestId,
                    this.OnContractDataEndMessage),
                connection.SubscribeForRequestErrors(requestId, this.OnError)
            };

            SendRequest(request, requestId, connection);
        }
Exemplo n.º 5
0
 public abstract Task<IReadOnlyCollection<Contract>> FindContracts(SearchRequest request,
     CancellationToken cancellationToken);
Exemplo n.º 6
0
 public Task<IReadOnlyCollection<Contract>> CreateAsyncFindContractOperation(SearchRequest searchRequest,
     CancellationToken cancellationToken)
 {
     return new FindContractsOperation(this.connection, this.idsDispenser, searchRequest, cancellationToken).Task;
 }
Exemplo n.º 7
0
        public async void Search()
        {
            this.Results.Clear();
            var request = new SearchRequest
            {
                Symbol = this.Ticker, NumberOfResults = this.ContractsToSearch,
                SecurityType = this.SelectedSecurityType
            };

            if (!string.IsNullOrEmpty(this.Currency))
            {
                request.Currency = this.Currency;
            }

            var contracts = await this.client.FindContracts(request, CancellationToken.None);

            this.Results.AddRange(ConvertToResults(contracts));
        }
Exemplo n.º 8
0
        private async void PlaceOrder(OrderAction action)
        {
            if (this.SelectedOrderType == OrderType.Limit && !this.LimitPrice.HasValue)
            {
                this.SetFieldError(true, "LimitPrice");
                return;
            }

            if (this.SelectedOrderType == OrderType.Stop && !this.StopPrice.HasValue)
            {
                this.SetFieldError(true, "StopPrice");
                return;
            }

            this.cancellationTokenSource = new CancellationTokenSource();

            var searchRequest = new SearchRequest{NumberOfResults = 1};

            if (this.Ticker.Length == 7 && this.Ticker[3] == '.')
            {
                searchRequest.LocalSymbol = this.Ticker;
                searchRequest.SecurityType = SecurityType.CASH;
            }
            else
            {
                searchRequest.Symbol = this.Ticker;
            }

            Contract contract;

            try
            {
                contract = (await this.client.FindContracts(searchRequest,
                    this.cancellationTokenSource.Token)).First();
            }
            catch (IbException)
            {
                this.SetFieldError(true, "Ticker");
                return;
            }

            try
            {
                var orderParams = new OrderParams
                {
                    Contract = contract,
                    LimitPrice = this.LimitPrice,
                    OrderAction = action,
                    OrderType = this.SelectedOrderType,
                    Quantity = this.Quantity,
                    StopPrice = this.StopPrice,
                };

                var account = this.client.Accounts.First(acc => acc.AccountId == this.SelectedAccount);

                await account.PlaceOrder(orderParams, this.cancellationTokenSource.Token);

            }
            catch (IbException)
            {
            }
        }