alpacahq / alpaca-py

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

[Bug]: Options Cost_Basis is not being used consistently. #455

Open SkyShadex opened 1 month ago

SkyShadex commented 1 month ago

Is there an existing issue for this?

Current Behavior

Options positions have an issue with cost_basis or avg_entry_price. All values based these values report wildly incorrectly. Seems like this is only in the paper environment. Might be related to what happened earlier in the week with misreported numbers?

image

print(f'[{row.symbol}] {row.current_price} {row.current_price/row.qty}') print(f'[{row.symbol}] {row.cost_basis} {row.avg_entry_price}') print(f'[{row.symbol}] {row.unrealized_plpc:.2%}')

image

Because avg_entry_price is 1/100th of what it should be, the percentage gain reported is misleading (as well as the cash value). It's possible to report a gain of 90$ on a 100$ cost basis, when actual loss is -10$.

Expected Behavior

avg_entry_price should be contract value. Cost Basis should either be Contract Value or Contract Value x 100. But there's currently a discrepancy.

SDK Version I encountered this issue in

alpaca-py == 0.21.0 But the issue seems to extend to alpaca's back end.

Steps To Reproduce

In the paper environment

api = TradingClient()
positions = api.get_all_positions()
options = pos.loc[(pos['asset_class'].str.contains("us_option",case=False))].copy()
print(f'[{options.symbol}] {options.current_price} {options.current_price/options.qty} {options.cost_per_unit} {options.avg_entry_price}')

print(options.symbol,options.current_price,options.avg_entry_price,options.cost_basis,options.unrealized_pl,options.unrealized_plpc)

all options contract's older than a day report incorrect values when it comes to cost_basis and everything based on it.

Filled out the Steps to Reproduce section?

Anything else?

From what I can tell, there’s a state management issue or a wrong variable used, maybe both? In the paper environment it's present. In the live environment.

Screenshot 2024-05-17 190751 (there’s also a text overflow problem in the Asset Column)

image Then what happens every day is this huge jump in p/l...