Skip to content

Commit

Permalink
fixes re: @hiohiohio
Browse files Browse the repository at this point in the history
fix: prefer !python3 -m pip install alpaca-py

refactor: ratio_qty docstring added

fix: implement suggestion re: validating api returns
  • Loading branch information
aarjaneiro committed Jan 20, 2025
1 parent e77f87b commit 27ae701
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 37 deletions.
34 changes: 1 addition & 33 deletions alpaca/trading/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class Order(ModelWithID):
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 (Optional[PositionIntent]): Represents the desired position strategy.
ratio_qty (Optional[str]): The proportional quantity of this leg in relation to the overall multi-leg order quantity.
"""

client_order_id: str
Expand Down Expand Up @@ -242,9 +243,6 @@ class Order(ModelWithID):
position_intent: Optional[PositionIntent] = None
ratio_qty: Optional[Union[str, float]] = None

# internal to the SDK
_is_sub_mleg: bool = False

def __init__(self, **data: Any) -> None:
if "order_class" not in data or data["order_class"] == "":
data["order_class"] = OrderClass.SIMPLE
Expand All @@ -255,38 +253,8 @@ def __init__(self, **data: Any) -> None:
if k in data and data[k] == "":
data[k] = None

if "_is_sub_mleg" not in data:
data["_is_sub_mleg"] = False

super().__init__(**data)

@model_validator(mode="before")
def root_validator(cls, data: dict) -> dict:
# Check non-mleg requirements
if data["order_class"] != OrderClass.MLEG:
if "asset_id" not in data or data["asset_id"] == "":
raise ValueError("asset_id is required for non-mleg orders")
if "asset_class" not in data or data["asset_class"] == "":
raise ValueError("asset_class is required for non-mleg orders")
if "side" not in data or data["side"] == "":
raise ValueError("side is required for non-mleg orders")

else:
# Check mleg requirements
if "_is_sub_mleg" not in data or not data["_is_sub_mleg"]:
if "legs" not in data or data["legs"] == "":
raise ValueError("legs is required for mleg orders")
if (
data["legs"] is not None
): # it is possible when querying individual legs that this is None
if len(data["legs"]) < 1:
raise ValueError("legs must have at least one order")
for leg in data["legs"]:
leg["_is_sub_mleg"] = True

return data


class FailedClosePositionDetails(BaseModel):
"""API response for failed close position request.
Expand Down
2 changes: 1 addition & 1 deletion examples/crypto-trading-basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"try:\n",
" import alpaca\n",
"except ImportError:\n",
" !pip install alpaca-py\n",
" !python3 -m pip install alpaca-py\n",
" import alpaca"
],
"outputs": [],
Expand Down
2 changes: 1 addition & 1 deletion examples/options-trading-basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"try:\n",
" import alpaca\n",
"except ImportError:\n",
" !pip install alpaca-py\n",
" !python3 -m pip install alpaca-py\n",
" import alpaca"
],
"outputs": [],
Expand Down
2 changes: 1 addition & 1 deletion examples/options-trading-mleg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"try:\n",
" import alpaca\n",
"except ImportError:\n",
" !pip install alpaca-py\n",
" !python3 -m pip install alpaca-py\n",
" import alpaca"
],
"id": "3edec8c032b75388",
Expand Down
2 changes: 1 addition & 1 deletion examples/stocks-trading-basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"try:\n",
" import alpaca\n",
"except ImportError:\n",
" !pip install alpaca-py\n",
" !python3 -m pip install alpaca-py\n",
" import alpaca"
],
"outputs": [],
Expand Down

0 comments on commit 27ae701

Please sign in to comment.