diff --git a/Alpaca.Markets.Tests/OrderTypeTest.cs b/Alpaca.Markets.Tests/OrderTypeTest.cs index 42d66356..2e60dca9 100644 --- a/Alpaca.Markets.Tests/OrderTypeTest.cs +++ b/Alpaca.Markets.Tests/OrderTypeTest.cs @@ -131,28 +131,35 @@ public void MultiLegOrderCreationWorks() const Decimal limitPrice = 100M; const Decimal ratioQuantity = 0.25M; - var legs = getOrderLegs() - .Select(tuple => new OrderLeg(Stock, ratioQuantity, tuple.Item1, tuple.Item2)) + var lfi = getOrderLegCreationInfo() + .Select(info => info.Intent.Leg(Stock, ratioQuantity, info.Side)) + .ToList(); + var lfs = getOrderLegCreationInfo() + .Select(info => info.Side.Leg(Stock, ratioQuantity, info.Intent)) .ToList(); - var marketOrder1 = MultiLegOrder.Market(Quantity, legs[0], legs[1]); - var marketOrder2 = MultiLegOrder.Market(Quantity, legs[0], legs[1], legs[2]); - var marketOrder3 = MultiLegOrder.Market(Quantity, legs[0], legs[1], legs[2], legs[3]); - - var limitOrder1 = MultiLegOrder.Limit(Quantity, limitPrice, legs[0], legs[1]); - var limitOrder2 = MultiLegOrder.Limit(Quantity, limitPrice, legs[0], legs[1], legs[2]); - var limitOrder3 = MultiLegOrder.Limit(Quantity, limitPrice, legs[0], legs[1], legs[2], legs[3]); - - assertOrdersAreEqual(marketOrder1, marketOrder1); - assertOrdersAreEqual(marketOrder2, marketOrder2); - assertOrdersAreEqual(marketOrder3, marketOrder3); + assertOrdersAreEqual( + MultiLegOrder.Market(Quantity, lfi[0], lfi[1]), + MultiLegOrder.Market(Quantity, lfs[0], lfs[1])); + assertOrdersAreEqual( + MultiLegOrder.Market(Quantity, lfi[0], lfi[1], lfi[2]), + MultiLegOrder.Market(Quantity, lfs[0], lfs[1], lfs[2])); + assertOrdersAreEqual( + MultiLegOrder.Market(Quantity, lfi[0], lfi[1], lfi[2], lfi[3]), + MultiLegOrder.Market(Quantity, lfs[0], lfs[1], lfs[2], lfs[3])); - assertOrdersAreEqual(limitOrder1, limitOrder1); - assertOrdersAreEqual(limitOrder2, limitOrder2); - assertOrdersAreEqual(limitOrder3, limitOrder3); + assertOrdersAreEqual( + MultiLegOrder.Limit(Quantity, limitPrice, lfi[0], lfi[1]), + MultiLegOrder.Limit(Quantity, limitPrice, lfs[0], lfs[1])); + assertOrdersAreEqual( + MultiLegOrder.Limit(Quantity, limitPrice, lfi[0], lfi[1], lfi[2]), + MultiLegOrder.Limit(Quantity, limitPrice, lfs[0], lfs[1], lfs[2])); + assertOrdersAreEqual( + MultiLegOrder.Limit(Quantity, limitPrice, lfi[0], lfi[1], lfi[2], lfi[3]), + MultiLegOrder.Limit(Quantity, limitPrice, lfs[0], lfs[1], lfs[2], lfs[3])); } - private static IEnumerable<(PositionIntent, OrderSide)> getOrderLegs() + private static IEnumerable<(PositionIntent Intent, OrderSide Side)> getOrderLegCreationInfo() { yield return (PositionIntent.BuyToClose, OrderSide.Buy); yield return (PositionIntent.SellToClose, OrderSide.Sell); diff --git a/Alpaca.Markets/Orders/OrderSideExtensions.cs b/Alpaca.Markets/Orders/OrderSideExtensions.cs index 8d6d218a..0782d55a 100644 --- a/Alpaca.Markets/Orders/OrderSideExtensions.cs +++ b/Alpaca.Markets/Orders/OrderSideExtensions.cs @@ -99,4 +99,73 @@ public static TrailingStopOrder TrailingStop( OrderQuantity quantity, TrailOffset trailOffset) => new(symbol, quantity, orderSide, trailOffset); -} + + /// + /// Creates new leg for the options multi-leg order. + /// + /// Order side (buy or sell). + /// Order asset symbol. + /// Order quantity. + /// + /// The argument is null. + /// + /// The new object instance. + [UsedImplicitly] + public static OrderLeg Leg( + this OrderSide orderSide, + String symbol, + Decimal ratioQuantity) => + new (symbol, ratioQuantity, orderSide); + + /// + /// Creates new leg for the options multi-leg order. + /// + /// Order side (buy or sell). + /// Order asset symbol. + /// Order quantity. + /// Order position intent. + /// + /// The argument is null. + /// + /// The new object instance. + public static OrderLeg Leg( + this OrderSide orderSide, + String symbol, + Decimal ratioQuantity, + PositionIntent positionIntent) => + new (symbol, ratioQuantity, positionIntent, orderSide); + + /// + /// Creates new leg for the options multi-leg order. + /// + /// Order position intent. + /// Order asset symbol. + /// Order quantity. + /// + /// The argument is null. + /// + /// The new object instance. + [UsedImplicitly] + public static OrderLeg Leg( + this PositionIntent positionIntent, + String symbol, + Decimal ratioQuantity) => + new (symbol, ratioQuantity, positionIntent); + + /// + /// Creates new leg for the options multi-leg order. + /// + /// Order position intent. + /// Order asset symbol. + /// Order quantity. + /// Order side (buy or sell). + /// + /// The argument is null. + /// + /// The new object instance. + public static OrderLeg Leg( + this PositionIntent positionIntent, + String symbol, + Decimal ratioQuantity, + OrderSide orderSide) => + new (symbol, ratioQuantity, positionIntent, orderSide);} diff --git a/Alpaca.Markets/PublicAPI.Shipped.txt b/Alpaca.Markets/PublicAPI.Shipped.txt index 2b673fb9..b154498b 100644 --- a/Alpaca.Markets/PublicAPI.Shipped.txt +++ b/Alpaca.Markets/PublicAPI.Shipped.txt @@ -1501,6 +1501,10 @@ static Alpaca.Markets.OrderBaseExtensions.WithDuration(this TOrder! orde static Alpaca.Markets.OrderBaseExtensions.WithExtendedHours(this TOrder! order, bool extendedHours) -> TOrder! static Alpaca.Markets.OrderBaseExtensions.WithPositionIntent(this TOrder! order, Alpaca.Markets.PositionIntent positionIntent) -> TOrder! static Alpaca.Markets.OrderExtensions.GetOrderQuantity(this Alpaca.Markets.IOrder! order) -> Alpaca.Markets.OrderQuantity +static Alpaca.Markets.OrderSideExtensions.Leg(this Alpaca.Markets.OrderSide orderSide, string! symbol, decimal ratioQuantity) -> Alpaca.Markets.OrderLeg! +static Alpaca.Markets.OrderSideExtensions.Leg(this Alpaca.Markets.OrderSide orderSide, string! symbol, decimal ratioQuantity, Alpaca.Markets.PositionIntent positionIntent) -> Alpaca.Markets.OrderLeg! +static Alpaca.Markets.OrderSideExtensions.Leg(this Alpaca.Markets.PositionIntent positionIntent, string! symbol, decimal ratioQuantity) -> Alpaca.Markets.OrderLeg! +static Alpaca.Markets.OrderSideExtensions.Leg(this Alpaca.Markets.PositionIntent positionIntent, string! symbol, decimal ratioQuantity, Alpaca.Markets.OrderSide orderSide) -> Alpaca.Markets.OrderLeg! static Alpaca.Markets.OrderSideExtensions.Limit(this Alpaca.Markets.OrderSide orderSide, string! symbol, Alpaca.Markets.OrderQuantity quantity, decimal limitPrice) -> Alpaca.Markets.LimitOrder! static Alpaca.Markets.OrderSideExtensions.Market(this Alpaca.Markets.OrderSide orderSide, string! symbol, Alpaca.Markets.OrderQuantity quantity) -> Alpaca.Markets.MarketOrder! static Alpaca.Markets.OrderSideExtensions.Stop(this Alpaca.Markets.OrderSide orderSide, string! symbol, Alpaca.Markets.OrderQuantity quantity, decimal stopPrice) -> Alpaca.Markets.StopOrder!