You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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?
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.
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.
A bit of the output:
I think my problem must be in self.data. My get_data module returns a dataframe with an index of datetime type. The statement:
produces: <class 'pandas._libs.tslibs.timestamps.Timestamp'>
Is that my okay or is that my problem?
The text was updated successfully, but these errors were encountered: