Exemplo n.º 1
0
        public void PlaceOrder(Order order)
        {
            RestClient  client  = new RestClient("https://api.tdameritrade.com/v1/accounts/" + AccountNumber + "/orders");
            RestRequest request = CreateRequest(Method.POST);
            string      body    = CreateOrderBodyString(order);

            request.AddJsonBody(body);
            IRestResponse response = ExecuteRequest(client, request);

            Log.Information("Response: {@Response}", response);

            FetchedOrderBody fetchedOrderBody = FetchOrderBody(order);

            if (fetchedOrderBody.Status == OrderStatus.REJECTED)
            {
                TDClientException ex = new TDClientException("Order rejected");
                Log.Error(ex, "Order rejected: Order {@Order}, OrderBody {@OrderBody}", order, fetchedOrderBody);
                throw ex;
            }
            else if (fetchedOrderBody.IsOpenOrder &&
                     order.CancelTime != null)
            {
                CancelOrderAtCancelTime(fetchedOrderBody.OrderId, (DateTime)order.CancelTime);
            }
        }
Exemplo n.º 2
0
        private FetchedOrderBody FetchOrderBody(Order order)
        {
            IEnumerable <FetchedOrderBody> orderBodies = GetOrderBodies()
                                                         .Where(body => body.Symbol == order.Symbol)
                                                         .OrderByDescending(body => body.EnteredDateTime);

            FetchedOrderBody?thisOrderBody = orderBodies.FirstOrDefault(body => body.ToOrder().Equals(order));

            if (thisOrderBody == null)
            {
                TDClientException ex = new TDClientException("Could not fetch order body from broker");
                Log.Error(ex, "Could not fetch order body from broker. Order {@Order}", order);
                throw ex;
            }
            else if (thisOrderBody != orderBodies.First())
            {
                Log.Warning("Fetched order body is not the most recent order for this symbol. Order {@Order}, OrderBody {@OrderBody}", order, thisOrderBody);
            }
            return(thisOrderBody);
        }