alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
537 stars 134 forks source link

[Bug]: pydantic ValidationError when calling close_all_positions #335

Closed ben-arnao closed 11 months ago

ben-arnao commented 1 year ago

Is there an existing issue for this?

Current Behavior

When I run the code in Steps To Reproduce I get the error posted in Anything else?.

Not much else to add.

Expected Behavior

No response

SDK Version I encountered this issue in

0.10

Steps To Reproduce

trading_client = TradingClient(alpaca_key, secret, url_override='https://api.alpaca.markets')
trading_client.close_all_positions(cancel_orders=False)

Filled out the Steps to Reproduce section?

Anything else?

Traceback (most recent call last): File "C:\Users\Ben\PycharmProjects\StockTrader\main.py", line 418, in print(trading_client.close_all_positions(cancel_orders=True)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ben\PycharmProjects\StockTrader\venv\Lib\site-packages\alpaca\trading\client.py", line 287, in close_all_positions return parse_obj_as(List[ClosePositionResponse], response) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "pydantic\tools.py", line 38, in pydantic.tools.parse_obj_as File "pydantic\main.py", line 341, in pydantic.main.BaseModel.init account = trading_client.get_account() pydantic.error_wrappers.ValidationError: 18 validation errors for ParsingModel[List[alpaca.trading.models.ClosePositionResponse]] root -> 0 -> body -> available field required (type=value_error.missing) root -> 0 -> body -> existing_qty field required (type=value_error.missing) root -> 0 -> body -> held_for_orders field required (type=value_error.missing) root -> 0 -> body -> symbol field required (type=value_error.missing) root -> 0 -> body -> id field required (type=value_error.missing) root -> 0 -> body -> client_order_id field required (type=value_error.missing) root -> 0 -> body -> created_at field required (type=value_error.missing) root -> 0 -> body -> updated_at field required (type=value_error.missing) root -> 0 -> body -> submitted_at field required (type=value_error.missing) root -> 0 -> body -> asset_id field required (type=value_error.missing) root -> 0 -> body -> symbol field required (type=value_error.missing) root -> 0 -> body -> asset_class field required (type=value_error.missing) root -> 0 -> body -> order_type field required (type=value_error.missing) root -> 0 -> body -> type field required (type=value_error.missing) root -> 0 -> body -> side field required (type=value_error.missing) root -> 0 -> body -> time_in_force field required (type=value_error.missing) root -> 0 -> body -> status field required (type=value_error.missing) root -> 0 -> body -> extended_hours field required (type=value_error.missing)

alessiocastrica commented 11 months ago

@ben-arnao thanks for flagging this! I'm opening a PR to address the bug, for reference this is how the response from the API will look like: Screenshot 2023-10-06 at 12 16 13 Screenshot 2023-10-06 at 12 18 18