alpacahq / Alpaca-API

The Alpaca API is a developer interface for trading operations and market data reception through the Alpaca platform.
https://alpaca.markets/
143 stars 13 forks source link

I'm have troubles testing my sell logic, btest.py keeps on throwing errors #33

Closed graetkel closed 5 years ago

graetkel commented 5 years ago

(The error I'm getting is on the samplealgo01/samplealgo/btest.py)

I have spent the last two months or so analyzing how the samplealgo01 and multiple other algorithms work. This last week I finally though I had enough information to start building my own algorithm on top of the samplealgo01. I first modified how the order are bought, I had no troubles with this. The error only occurred when I when to modify how stocks are sold. I changed the logic and ran the code but once the code started to run, I got a error that said the snapshot in btest was empty. Since I got an error, I commented out my version of how to sell and uncommented the original version and ran it again. This time the error did not occur. I was a little curious to why, so then I commented out the order.append({...}) that happens in the original sell logic, once I did that an ran the code again I got the same error as before. It looks like if I don't sell it happens.

The next thing I did was go to the beginning of where the error occur and that is here in the btest.py:

        snapshot = {
            symbol: df[df.index < t]
            for symbol, df in price_map.items()
            # sanity check to exclude stale prices
            if t - df[df.index < t].index[-1] < pd.Timedelta('2 days')}

It turns out if you hide the order.append({}) in the sell logic or even modify how the sell logic works then the snapshot becomes empty because it does not pass this if statement:

if t - df[df.index < t].index[-1] < pd.Timedelta('2 days')

I would like to know why this is happening and is there a work around for it?

graetkel commented 5 years ago

I'm moving my issue to samplealgo01 instead https://github.com/alpacahq/samplealgo01/issues/