Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New to backtesting.py, very simple program is failing to open orders #1186

Closed
leecallen35 opened this issue Dec 19, 2024 · 3 comments
Closed

Comments

@leecallen35
Copy link

My code is executing the buy() and sell() methods, they are returning reasonable looking return-codes, but the trades are never opened. That is, self.position is never true, and the stats reports 0 trades.

I have simplified my program down to a minimum as shown below.

from backtesting import Strategy, Backtest
from getdata import get_data
import random

class Test(Strategy):
    def init(self):
        pass
        
    def next(self):
        if self.position:
            print(f"{self.data.index[-1].isoformat()} closing trade trade")
            self.position.close()
        elif random.random() > 0.5:
            if random.random() > 0.5:
                print(f"{self.data.index[-1].isoformat()} opening long trade")
                ret = self.buy()
                print("order returned:", ret)
            else:
                print(f"{self.data.index[-1].isoformat()} opening short trade")
                ret = self.sell()
                print("order returned:", ret)

data = get_data(ticker="BTCUSDT", start_dt=datetime(2023,1,1,8,0), end_dt=datetime(2023,12,31,8,0), interval = "1h" )
bt = Backtest(data, Test, cash=10000)
stats = bt.run()

A bit of the output:

2023-12-29T23:00:00-08:00 opening short trade
order returned: <Order size=-1.0, contingent=0>
2023-12-30T01:00:00-08:00 opening long trade
order returned: <Order size=1.0, contingent=0>
2023-12-30T02:00:00-08:00 opening short trade
order returned: <Order size=-1.0, contingent=0>

I think my problem must be in self.data. My get_data module returns a dataframe with an index of datetime type. The statement:

print(type(self.data.index[-1]))

produces: <class 'pandas._libs.tslibs.timestamps.Timestamp'>

Is that my okay or is that my problem?

@leecallen35
Copy link
Author

I have done some more research and experimenting. I tried the GOOG test data and my program works fine - it opens and closes trades. Then I compared the GOOG data and my own, in next(). There were minor differences but I eliminated them, now they are exactly the same. Except the GOOG data is daily bars and mine is 30 minute bars.

I'm really at a loss here. Is there a way to find out why the buy() method returns seemingly successful codes but the trade never opens?

@Ish2K
Copy link

Ish2K commented Dec 21, 2024

Could you share your get_data function?

@leecallen35
Copy link
Author

In comparing notes with https://github.com/Ish2K (issue 1187) I found I can get trades to open by giving specific order sizes, instead of leaving it to default. I think it has nothing to do with my data. It may have something to do with the very large prices in my data (Bitcoin) and so some question of whether the default orders exceeded my cash.

I am going to close this issue since its a duplicate of 1187. I think there is a valid point of having visibility of why orders fail to open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants