Skip to content

Commit

Permalink
feat: add position_intent into order model (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiohiohio authored Dec 20, 2024
1 parent 4fe5eb8 commit 9165487
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions alpaca/trading/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
OrderType,
OrderClass,
PDTCheck,
PositionIntent,
TimeInForce,
OrderSide,
PositionSide,
Expand Down Expand Up @@ -204,6 +205,7 @@ class Order(ModelWithID):
trail_percent (Optional[str]): The percent value away from the high water mark for trailing stop orders.
trail_price (Optional[str]): The dollar value away from the high water mark for trailing stop orders.
hwm (Optional[str]): The highest (lowest) market price seen since the trailing stop order was submitted.
position_intent (PositionIntent): Represents the desired position strategy.
"""

client_order_id: str
Expand Down Expand Up @@ -237,6 +239,7 @@ class Order(ModelWithID):
trail_percent: Optional[str] = None
trail_price: Optional[str] = None
hwm: Optional[str] = None
position_intent: PositionIntent

def __init__(self, **data: Any) -> None:
if "order_class" not in data or data["order_class"] == "":
Expand Down
5 changes: 5 additions & 0 deletions tests/broker/broker_client/test_trading_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def test_close_all_positions_for_account(reqmock, client: BrokerClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"subtag": null,
"source": null
}
Expand Down Expand Up @@ -336,6 +337,7 @@ def test_close_position_for_account_with_id(reqmock, client: BrokerClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}}
""",
Expand Down Expand Up @@ -389,6 +391,7 @@ def test_close_position_for_account_with_symbol(reqmock, client: BrokerClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}}
""",
Expand Down Expand Up @@ -442,6 +445,7 @@ def test_close_position_for_account_with_qty(reqmock, client: BrokerClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down Expand Up @@ -495,6 +499,7 @@ def test_close_position_for_account_with_percentage(reqmock, client: BrokerClien
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down
2 changes: 2 additions & 0 deletions tests/common/factories/trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
OrderClass,
OrderSide,
OrderType,
PositionIntent,
TimeInForce,
)

Expand Down Expand Up @@ -72,4 +73,5 @@ def create_dummy_order() -> Order:
trail_percent=None,
trail_price=None,
hwm=None,
position_intent=PositionIntent.BUY_TO_OPEN,
)
2 changes: 2 additions & 0 deletions tests/common/test_common_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
AssetExchange,
OrderType,
OrderClass,
PositionIntent,
TimeInForce,
OrderSide,
PositionSide,
Expand Down Expand Up @@ -173,6 +174,7 @@ def test_order_legs():
trail_percent=None,
trail_price=None,
hwm=None,
position_intent=PositionIntent.BUY_TO_OPEN,
)

assert isinstance(order_with_legs.legs, list)
Expand Down
13 changes: 10 additions & 3 deletions tests/trading/trading_client/test_order_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_market_order(reqmock, trading_client):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down Expand Up @@ -115,7 +116,8 @@ def test_get_orders(reqmock, trading_client: TradingClient):
"extended_hours": true,
"trail_percent": null,
"trail_price": null,
"hwm": "string"
"hwm": "string",
"position_intent": "buy_to_open"
}
]
""",
Expand Down Expand Up @@ -168,6 +170,7 @@ def test_get_order_by_id(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down Expand Up @@ -219,6 +222,7 @@ def test_get_order_by_client_id(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down Expand Up @@ -268,6 +272,7 @@ def test_replace_order(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down Expand Up @@ -423,6 +428,7 @@ def test_limit_order(reqmock, trading_client):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down Expand Up @@ -540,8 +546,8 @@ def test_order_position_intent(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"commission": 1.25,
"position_intent": "buy_to_open"
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
)
Expand Down Expand Up @@ -597,6 +603,7 @@ def test_order_position_intent(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "sell_to_open",
"commission": 1.25
}
""",
Expand Down
5 changes: 5 additions & 0 deletions tests/trading/trading_client/test_position_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def test_close_all_positions(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "sell_to_close",
"subtag": null,
"source": null
}
Expand Down Expand Up @@ -263,6 +264,7 @@ def test_close_position_with_id(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}}
""",
Expand Down Expand Up @@ -315,6 +317,7 @@ def test_close_position_with_symbol(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}}
""",
Expand Down Expand Up @@ -367,6 +370,7 @@ def test_close_position_with_qty(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down Expand Up @@ -419,6 +423,7 @@ def test_close_position_with_percentage(reqmock, trading_client: TradingClient):
"trail_percent": null,
"trail_price": null,
"hwm": null,
"position_intent": "buy_to_open",
"commission": 1.25
}
""",
Expand Down

0 comments on commit 9165487

Please sign in to comment.