alpacahq / alpaca-trade-api-python

Python client for Alpaca's trade API
https://pypi.org/project/alpaca-trade-api/
Apache License 2.0
1.73k stars 534 forks source link

[Bug]: #654

Open jabowery opened 2 years ago

jabowery commented 2 years ago

Is there an existing issue for this?

Current Behavior

Some filled orders are not being returned by the list_orders method.

Expected Behavior

The steps to reproduce should output "0".

SDK Version I encountered this issue in

alpaca_trade_api 2.3.0

Steps To Reproduce

Procedure to replicate:
1. Reset a paper trade's keys to start with an empty portfolio.
2. Run intraday trading but sell off the portfolio before the end of day.
3. Run the following script:

from load_alpaca_apis import api
import pandas as pd
after=None
while True:
    filled_orders = api.list_orders(status="closed", after=after)
    if len(filled_orders) == 0:
        break
    filled_orders = [l for l in filled_orders if l.status=='filled']
    df = pd.DataFrame(data=[r._raw for r in filled_orders], columns=filled_orders[0]._raw.keys())
    try:
        filled_orders_df = pd.concat([filled_orders_df, df])
    except:
        filled_orders_df = df
    after = filled_orders_df.submitted_at.iloc[-1]

filled_orders_df['signed_side'] = [1 if x == 'buy' else -1 for x in filled_orders_df.side]
filled_orders_df.filled_qty = [eval(x) for x in filled_orders_df.filled_qty]
filled_orders_df['signed_qty'] = filled_orders_df.filled_qty * filled_orders_df.signed_side
filled_orders_df.to_csv("orders.csv")
df = pd.read_csv("orders.csv", parse_dates=['filled_at'], infer_datetime_format=True).set_index("filled_at")
print(df.signed_qty.sum())


### Filled out the Steps to Reproduce section?

- [X] I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

### Anything else?

I had previously submitted this to the forum but it got deleted so I presume it was mistaken for a previously closed bug or something but I seriously doubt this is the case since there is [another closed bug that is easily mistaken for this one, but is for the beta API for crypto currency](https://github.com/alpacahq/alpaca-trade-api-python/issues/647) whereas this is for stock symbols API.