alpacahq / alpaca-trade-api-python

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

get_portfolio_history does not return accurate equity value #180

Open wuliwei9278 opened 4 years ago

wuliwei9278 commented 4 years ago

I find that the get_portfolio_history api does not return the portfolio value accurately. Does someone else share similar concern? I am testing codes using paper trading api.

umitanuki commented 4 years ago

How is it inaccurate? Can you provide more examples?

wuliwei9278 commented 4 years ago

How is it inaccurate? Can you provide more examples?

for example, for my paper API key: "PK5QD93IRC034B2UUM28", the website or mobile app shows my portfolio is $34,160.74 after I liquidate all my positions as of May 15th, but using python get_portfolio_history function, it shows equity is 27538.3709. So there is a large discrepancy. Could you please look into the issue? I assume the website one and mobile app value is correct and the api returns the wrong one.

dylanrandle commented 3 years ago

I will add to this. Changing the timeframe and even the period alters my equity values for the same timestamp. Of course, this should not be possible. At a given timestamp my equity value should be identical. My api version is 0.50.1. This is for live trading.

For example, running:

histpd={}
for pd in ['1D', '2D', '3D', '7D']:
    hist = api.get_portfolio_history(
        period=pd,
        extended_hours=True,
        timeframe='1Min'
    ).df
    histpd[pd] = hist

Yields: alpaca_hist_wrong

And running:

histtf={}
for tf in ['1Min', '1H', '1D']:
    hist = api.get_portfolio_history(
        period="7D",
        extended_hours=True,
        timeframe=tf
    ).df
    histtf[tf] = hist

Yields: alpaca_hist_wrong2

This is all in addition to the fact that the profit_loss and profit_loss_pct don't seem to match or make any sense. For example, how can my profit_loss be positive for 2020-09-01 but the profit_loss_pct be negative??

Screen Shot 2020-09-06 at 12 23 37 PM

Note that I added the pct_chg column which is just the percent difference between successive rows.

dylanrandle commented 3 years ago

At the risk of beating a dead horse, here is the comparison of the P/L reported by get_portfolio_history (first pic) vs the P/L I independently calculated (second pic) from all of my actual orders (using the filled prices). They are so different as to make this almost comical. I’m going to get off Alpaca immediately and would recommend anyone else do the same until they fix this bug. @umitanuki @yoshyoshi

Screen Shot 2020-09-06 at 3 21 50 PM Screen Shot 2020-09-06 at 3 21 45 PM
wuliwei9278 commented 3 years ago

Actually what is more interesting is they did not consider the corner case of AAPL & TSLA splits, so my paper account went down 4000 dollars in one day....that definitely affect my evaluations of paper trading == :(

dylanrandle commented 3 years ago

That's actually what I'm thinking could be causing these differences. Maybe they are not correctly adjusting prices for those types of situations... Unfortunately this is live trading so I have now lost hundreds of dollars more than I believe I should have

wuliwei9278 commented 3 years ago

Ah....You should have tested with paper trading first :( I thought they used better codes for living trading. Good to know that they are actually sharing the codes. I would try different api's. I just got the key from E*Trade but did not have time to follow the documentation to get started yet. I feel we should try different brokers and see which one works best: I am also exploring different options and strongly feel we should not put all eggs in one basket.

dylanrandle commented 3 years ago

Totally agree. I am also moving over to ETrade at this time.

wuliwei9278 commented 3 years ago

Yeah you can also try Interactive Brokers or TD before settling on ETrade :) let me know if you need a referral for the Interactive Brokers

jholland1 commented 3 years ago

I've also had some issues with the get_portfolio_history, when I last looked at it I thought there was an issue with the timestamps not being consistent between timeframes. This fixed it for my purposes:

port = api.get_portfolio_history(date_start=fd,date_end=td,timeframe='1D').df #Portfolio reporting 0930 as time but really end of day (close) I believe port.index = port.index.shift(1140,freq='Min')

(Honestly I forget why I ended up on 1140, but seems to work for my script). Day data in general through the API doesn't always return a HH:MM timestamp so have to handle that in order to do comparisons between higher frequency data.

dylanrandle commented 3 years ago

There still seems to be an issue... image Daily-aggregated equity shows $29000 but minutely/hourly shows ~$29250 (around the 2020-09-11 mark).

dylanrandle commented 3 years ago

Interestingly, running this ~1.25 hours later (at 8am), produced: image The plot from the above post was from 6:45am.

dylanrandle commented 3 years ago

image image

Results have been getting much better.

dylanrandle commented 3 years ago
Screen Shot 2020-10-23 at 1 23 24 PM

Results appearing to be quite off again

dylanrandle commented 3 years ago
Screen Shot 2020-10-23 at 1 25 13 PM

Also different periods do not agree.

dylanrandle commented 3 years ago

@umitanuki Here is a recent and simple example that seems very incorrect. The profit_loss does not even directionally line up with my actual equity. For example, in the third equity element I gained ~$70 but Alpaca shows loss of ~$330.

PortfolioHistory({   'base_value': 28481.09,
    'equity': [28481.09, 28398.65, 28474.22, 28324.1],
    'profit_loss': [-322.115, -331.4756, -368.7634, -372.49205],
    'profit_loss_pct': [   0,
                           -0.0116384450173782,
                           -0.02447256098573159,
                           -0.03723414727640694],
    'timeframe': '1D',
    'timestamp': [1604064600, 1604327400, 1604413800, 1604500200]})
dylanrandle commented 3 years ago

I have another example to share. Today I explicitly calculated my realized profit/loss by using Alpaca order fill prices (as exit) and the Alpaca position entry price (as entry) to calculate thee realized PnL. For example, this value showed -$100. However, Alpaca shows my change in equity from 9:30am to 4:00pm is -$70 (I don't trade in extended hours). At first I wasn't too concerned because I know that equity includes unrealized PnL. So I calculated my unrealized PnL from Alpaca positions using Polygon close prices, and it shows that my unrealized PnL is +$160. So my net equity change should have been +$160-$100 = +$60, but Alpaca is showing a decrease in equity of -$70?

I do not see any other transactions (e.g. dividends, short/margin fees) or anything else on my account. I don't think that it's possible the SEC/FINRA fees account for the $130 discrepancy (~0.46%) either? @umitanuki can you explain why Alpaca is showing this different value than what I expect?