public static JsonNewOrder WithLimitPrice(
     this JsonNewOrder order,
     Decimal limitPrice)
 {
     order.LimitPrice = limitPrice;
     return(order);
 }
 public static JsonNewOrder WithOrderClass(
     this JsonNewOrder order,
     OrderClass orderClass)
 {
     order.OrderClass = orderClass;
     return(order);
 }
 public static JsonNewOrder WithStopPrice(
     this JsonNewOrder order,
     Decimal stopPrice)
 {
     order.StopPrice = stopPrice;
     return(order);
 }
 public static JsonNewOrder WithQuantity(
     this JsonNewOrder order,
     OrderQuantity quantity)
 {
     order.Quantity = quantity.AsFractional();
     order.Notional = quantity.AsNotional();
     return(order);
 }
 public static JsonNewOrder WithTakeProfit(
     this JsonNewOrder order,
     ITakeProfit takeProfit)
 {
     order.TakeProfit = new JsonNewOrderAdvancedAttributes
     {
         LimitPrice = takeProfit.LimitPrice
     };
     return(order);
 }
 public static JsonNewOrder WithStopLoss(
     this JsonNewOrder order,
     IStopLoss stopLoss)
 {
     order.StopLoss = new JsonNewOrderAdvancedAttributes
     {
         StopPrice  = stopLoss.StopPrice,
         LimitPrice = stopLoss.LimitPrice
     };
     return(order);
 }
Example #7
0
        /// <summary>
        /// Creates new order for execution using Alpaca REST API endpoint.
        /// </summary>
        /// <param name="symbol">Order asset name.</param>
        /// <param name="quantity">Order quantity.</param>
        /// <param name="side">Order size (buy or sell).</param>
        /// <param name="type">Order type.</param>
        /// <param name="duration">Order duration.</param>
        /// <param name="limitPrice">Order limit price.</param>
        /// <param name="stopPrice">Order stop price.</param>
        /// <param name="clientOrderId">Client order ID.</param>
        /// <param name="extendedHours">Whether or not this order should be allowed to execute during extended hours trading.</param>
        /// <returns>Read-only order information object for newly created order.</returns>
        public async Task <IOrder> PostOrderAsync(
            String symbol,
            Int64 quantity,
            OrderSide side,
            OrderType type,
            TimeInForce duration,
            Decimal?limitPrice    = null,
            Decimal?stopPrice     = null,
            String clientOrderId  = null,
            Boolean?extendedHours = null)
        {
            if (!string.IsNullOrEmpty(clientOrderId) &&
                clientOrderId.Length > 48)
            {
                clientOrderId = clientOrderId.Substring(0, 48);
            }

            var newOrder = new JsonNewOrder
            {
                Symbol        = symbol,
                Quantity      = quantity,
                OrderSide     = side,
                OrderType     = type,
                TimeInForce   = duration,
                LimitPrice    = limitPrice,
                StopPrice     = stopPrice,
                ClientOrderId = clientOrderId,
                ExtendedHours = extendedHours
            };

            await _alpacaRestApiThrottler.WaitToProceed();

            var serializer = new JsonSerializer();

            using (var stringWriter = new StringWriter())
            {
                serializer.Serialize(stringWriter, newOrder);

                using (var content = new StringContent(stringWriter.ToString()))
                    using (var response = await _alpacaHttpClient.PostAsync("orders", content))
                        using (var stream = await response.Content.ReadAsStreamAsync())
                            using (var textReader = new StreamReader(stream))
                                using (var reader = new JsonTextReader(textReader))
                                {
                                    if (response.IsSuccessStatusCode)
                                    {
                                        return(serializer.Deserialize <JsonOrder>(reader));
                                    }

                                    var error = serializer.Deserialize <JsonError>(reader);
                                    throw new RestClientErrorException(error);
                                }
            }
        }
 public static JsonNewOrder WithTrailOffset(
     this JsonNewOrder order,
     TrailOffset trailOffset)
 {
     if (trailOffset.IsInDollars)
     {
         order.TrailOffsetInDollars = trailOffset.Value;
     }
     else
     {
         order.TrailOffsetInPercent = trailOffset.Value;
     }
     return(order);
 }
        private async Task <IOrder> postOrderAsync(
            JsonNewOrder jsonNewOrder,
            CancellationToken cancellationToken = default)
        {
            await _alpacaRestApiThrottler.WaitToProceed(cancellationToken).ConfigureAwait(false);

            using var content  = toStringContent(jsonNewOrder);
            using var response = await _httpClient.PostAsync(
                      new Uri ("orders", UriKind.RelativeOrAbsolute), content, cancellationToken)
                                 .ConfigureAwait(false);

            return(await response.DeserializeAsync <IOrder, JsonOrder>()
                   .ConfigureAwait(false));
        }
Example #10
0
        /// <summary>
        /// Creates new order for execution using Alpaca REST API endpoint.
        /// </summary>
        /// <param name="symbol">Order asset name.</param>
        /// <param name="quantity">Order quantity.</param>
        /// <param name="side">Order side (buy or sell).</param>
        /// <param name="type">Order type.</param>
        /// <param name="duration">Order duration.</param>
        /// <param name="limitPrice">Order limit price.</param>
        /// <param name="stopPrice">Order stop price.</param>
        /// <param name="clientOrderId">Client order ID.</param>
        /// <param name="extendedHours">Whether or not this order should be allowed to execute during extended hours trading.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Read-only order information object for newly created order.</returns>
        public async Task <IOrder> PostOrderAsync(
            String symbol,
            Int64 quantity,
            OrderSide side,
            OrderType type,
            TimeInForce duration,
            Decimal?limitPrice    = null,
            Decimal?stopPrice     = null,
            String clientOrderId  = null,
            Boolean?extendedHours = null,
            CancellationToken cancellationToken = default)
        {
            if (!string.IsNullOrEmpty(clientOrderId) &&
                clientOrderId.Length > 48)
            {
                clientOrderId = clientOrderId.Substring(0, 48);
            }

            var newOrder = new JsonNewOrder
            {
                Symbol        = symbol,
                Quantity      = quantity,
                OrderSide     = side,
                OrderType     = type,
                TimeInForce   = duration,
                LimitPrice    = limitPrice,
                StopPrice     = stopPrice,
                ClientOrderId = clientOrderId,
                ExtendedHours = extendedHours
            };

            await _alpacaRestApiThrottler.WaitToProceed(cancellationToken).ConfigureAwait(false);

            var serializer = new JsonSerializer();

            using (var stringWriter = new StringWriter())
            {
                serializer.Serialize(stringWriter, newOrder);

                using (var content = new StringContent(stringWriter.ToString()))
                    using (var response = await _alpacaHttpClient.PostAsync(
                               new Uri("orders", UriKind.RelativeOrAbsolute), content, cancellationToken)
                                          .ConfigureAwait(false))
                    {
                        return(await deserializeAsync <IOrder, JsonOrder>(response)
                               .ConfigureAwait(false));
                    }
            }
        }
Example #11
0
 private Task <IOrder> postOrderAsync(
     JsonNewOrder jsonNewOrder,
     CancellationToken cancellationToken = default) =>
 _httpClient.PostAsync <IOrder, JsonOrder, JsonNewOrder>(
     "v2/orders", jsonNewOrder, cancellationToken, _alpacaRestApiThrottler);
 public static JsonNewOrder WithoutLimitPrice(
     this JsonNewOrder order)
 {
     order.LimitPrice = null;
     return(order);
 }
        /// <summary>
        /// Creates new order for execution using Alpaca REST API endpoint.
        /// </summary>
        /// <param name="symbol">Order asset name.</param>
        /// <param name="quantity">Order quantity.</param>
        /// <param name="side">Order side (buy or sell).</param>
        /// <param name="type">Order type.</param>
        /// <param name="duration">Order duration.</param>
        /// <param name="limitPrice">Order limit price.</param>
        /// <param name="stopPrice">Order stop price.</param>
        /// <param name="clientOrderId">Client order ID.</param>
        /// <param name="extendedHours">Whether or not this order should be allowed to execute during extended hours trading.</param>
        /// <param name="orderClass">Order class for advanced order types.</param>
        /// <param name="takeProfitLimitPrice">Profit taking limit price for advanced order types.</param>
        /// <param name="stopLossStopPrice">Stop loss stop price for advanced order types.</param>
        /// <param name="stopLossLimitPrice">Stop loss limit price for advanced order types.</param>
        /// <param name="nested">Whether or not child orders should be listed as 'legs' of parent orders. (Advanced order types only.)</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Read-only order information object for newly created order.</returns>
        public async Task <IOrder> PostOrderAsync(
            String symbol,
            Int64 quantity,
            OrderSide side,
            OrderType type,
            TimeInForce duration,
            Decimal?limitPrice           = null,
            Decimal?stopPrice            = null,
            String?clientOrderId         = null,
            Boolean?extendedHours        = null,
            OrderClass?orderClass        = null,
            Decimal?takeProfitLimitPrice = null,
            Decimal?stopLossStopPrice    = null,
            Decimal?stopLossLimitPrice   = null,
            Boolean?nested = false,
            CancellationToken cancellationToken = default)
        {
            if (clientOrderId != null &&
                clientOrderId.Length > 48)
            {
                clientOrderId = clientOrderId.Substring(0, 48);
            }

            JsonNewOrderAdvancedAttributes?takeProfit = null, stopLoss = null;

            if (takeProfitLimitPrice != null)
            {
                takeProfit = new JsonNewOrderAdvancedAttributes
                {
                    LimitPrice = takeProfitLimitPrice
                };
            }
            if (stopLossStopPrice != null || stopLossLimitPrice != null)
            {
                stopLoss = new JsonNewOrderAdvancedAttributes
                {
                    StopPrice  = stopLossStopPrice,
                    LimitPrice = stopLossLimitPrice
                };
            }

            var newOrder = new JsonNewOrder
            {
                Symbol        = symbol,
                Quantity      = quantity,
                OrderSide     = side,
                OrderType     = type,
                TimeInForce   = duration,
                LimitPrice    = limitPrice,
                StopPrice     = stopPrice,
                ClientOrderId = clientOrderId,
                ExtendedHours = extendedHours,
                OrderClass    = orderClass,
                TakeProfit    = takeProfit,
                StopLoss      = stopLoss,
            };

            var builder = new UriBuilder(_httpClient.BaseAddress)
            {
                Path  = _httpClient.BaseAddress.AbsolutePath + "orders",
                Query = new QueryBuilder()
                        .AddParameter("nested", nested)
            };

            await _alpacaRestApiThrottler.WaitToProceed(cancellationToken).ConfigureAwait(false);

            using var content  = toStringContent(newOrder);
            using var response = await _httpClient.PostAsync(
                      new Uri ("orders", UriKind.RelativeOrAbsolute), content, cancellationToken)
                                 .ConfigureAwait(false);

            return(await response.DeserializeAsync <IOrder, JsonOrder>()
                   .ConfigureAwait(false));
        }