-
-
Notifications
You must be signed in to change notification settings - Fork 81
Order Placement
For placing new orders on the Alpaca you can use the method PostOrderAsync of the Alpaca.Markets.IAlpacaTradingClient interface. This method has two overloads:
- With the Alpaca.Markets.NewOrderRequest argument - low-level old-style method. You can use this approach if you already have code for the order placement and/or you want to have full control over the resulting JSON message that will be sent to the server. In this case, you have to properly fill all properties of the Alpaca.Markets.NewOrderRequest object, and SDK will not validate the correctness of the final object.
- With the Alpaca.Markets.OrderBase argument - high-level new-style approach. The SDK provides a set of helper factory methods for constructing the properly configured order placement objects. See the sections below for more details and examples.
The SDK provides a set of strongly-typed classes that represent different order types: Alpaca.Markets.MarketOrder, Alpaca.Markets.LimitOrder, Alpaca.Markets.StopOrder, Alpaca.Markets.StopLimitOrder, and Alpaca.Markets.TrailingStopOrder. You unable to create instances of these types directly but each class provides two factory methods Buy and Sell (with an appropriate number of price arguments) for creating buy and sell orders respectively.
Additionally, you can use the extension methods of the Alpaca.Markets.OrderSide enum provided by SDK: Market, Limit, Stop, StopLimit, and TrailingStop. All these methods call appropriate factory methods of related classes and can be used in cases when you have the order side calculated as a value of Alpaca.Markets.OrderSide enum.
In both cases (factory or extension methods) the Duration property value will be equal to Alpaca.Markets.TimeInForce.Day and if you need another order duration you can change this property using a setter or a fluent helper method (see below).
var client = Environments.Live
.GetAlpacaTradingClient(new SecretKey(KEY_ID, SECRET_KEY));
// The market orders - using the factory method and the extension method
await client.PostOrderAsync(MarketOrder.Buy("AAPL", 10));
await client.PostOrderAsync(OrderSide.Sell.Market("AAPL", 10));
// The limit orders - using the factory method and the extension method
await client.PostOrderAsync(LimitOrder.Sell("AAPL", 10, 1000M));
await client.PostOrderAsync(OrderSide.Buy.Limit("AAPL", 10, 1000M));
// The stop orders - using the factory method and the extension method
await client.PostOrderAsync(StopOrder.Buy("AAPL", 10, 1000M));
await client.PostOrderAsync(OrderSide.Sell.Stop("AAPL", 10, 1000M));
// The stop limit orders - using the factory method and the extension method
// The first price is the stop price and the second price is the limit price
await client.PostOrderAsync(StopLimitOrder.Sell("AAPL", 10, 1000M, 990M));
await client.PostOrderAsync(OrderSide.Buy.StopLimit("AAPL", 10, 1000M, 1010M));
// The trailing stop orders - using the factory method and the extension method
// The fist case is the dollar offset and the second case is the percent offset
await client.PostOrderAsync(TrailingStopOrder.Sell("AAPL", 10, TrailOffset.InDollars(10M)));
await client.PostOrderAsync(OrderSide.Buy.TrailingStop("AAPL", 10, TrailOffset.InPercent(0.90M)));
The SDK allows you to place market orders with non-integer quantity values specified as a dollar value or share numbers. The new helper type Alpaca.Markets.OrderQuantity added into the SDK - this type can represent both notional (in dollars) and fractional (in shares) values. You can construct instances of this type using the Notional and Fractinal static methods respectively. Any System.Int64 value can be easily converted into this type using the static method FromInt64 or implicit cast (this cast allows you to use your existing code that operates with integer quantities without any changes). You can use this new type in the both Alpaca.Markets.NewOrderRequest and Alpaca.Markets.MarketOrder classes constructors. You can also get the value of this type from the Alpaca.Market.IOrder interface instance using the GetOrderQuantity extensions method.
The advanced order types (stop-loss orders, OCO orders, brackets) can be constructed only from simple order types. So, you have to create a base order using one of the methods described in the previous section and call a static factory method for converting this simple order into the advanced one:
- The OneCancelsOther method converts Alpaca.Markets.LimitOrder into the OCO order.
- The StopLoss method converts any simple order into the stop-loss OTO order.
- The TakeProfit method converts any simple order into the take-profit OTO order.
- The Bracket method converts any simple order into the bracket order.
All three advanced orders' examples demonstrate how to place orders described on this Help page.
var client = Environments.Live
.GetAlpacaTradingClient(new SecretKey(KEY_ID, SECRET_KEY));
// The OCO order with the stop limit leg order
await client.PostOrderAsync(LimitOrder.Sell("SPY", 100, 301M).OneCancelsOther(299M, 298.5M));
// The stop loss order with the stop limit leg order
await client.PostOrderAsync(OrderSide.Buy.Market("SPY", 100).StopLoss(299M, 298.5M));
// The take profit order with the limit leg order
await client.PostOrderAsync(OrderSide.Buy.Market("SPY", 100).TakeProfit(299M));
// The bracket order with the two leg's orders - limit and stop limit
await client.PostOrderAsync(OrderSide.Buy.Market("SPY", 100).Bracket(301M, 299M, 298.5M));
The Alpaca.Markets.OrderBase class has some properties that initialized with default values. You can use property setters for changing these default values or extension methods provided by SDK. These extension methods are implemented as fluent interface methods and it allows you to pass fully configured order request into the PostOrderAsync method without creating the temporary object:
- The WithDuration method sets the Duration property value.
- The WithClientOrderId method sets the ClientOrderId property value.
- The WithExtendedHours method sets the ExtendedHours property value.