Skip to content

Strategy backtest does not work on Bitcoin OHLCV 1 minute data #151

Closed
@mebrammel

Description

@mebrammel

Expected Behavior

When using BTCUSDT 1minute ticker data with the default SMA strategy I would expect transactions and a report.

Actual Behavior

Backtest report is empty for ticker BTCUSDT.
Also no transactions plotted.
If you use another ticker like ETHUSDT the report is working properly.

Steps to Reproduce

Get BTCUSDT OHLCV data, 3 days history 1 minute candles.
Run sample strategy SMA crossover from your documentation.
See code below:

import pandas as pd
from binance.client import Client

# GET OHLCV DATA FROM BINANCE
ticker = 'BTCUSDT'
# ticker = 'ETHUSDT'
client = Client()
btc_data = client.get_historical_klines(ticker, Client.KLINE_INTERVAL_1MINUTE, "3 day ago UTC")
df = pd.DataFrame(btc_data)
df[0] = pd.to_datetime(df[0],unit='ms')
df[6] = pd.to_datetime(df[6],unit='ms')
df.rename(columns={0:'DateTime', 1:'open', 2:'high', 3:'low', 4:'close',5:'volume',6:'CloseTime',7:'QuoteAssetVolume',8:'NumberTrades',9:'TakerBuyBaseAssetVolume',10:'TakerBuyQuoteAssetVolume',11:'Ignore'}, inplace=True)
df[['open','high','low','close','volume','TakerBuyBaseAssetVolume','TakerBuyQuoteAssetVolume']] = df[['open','high','low','close','volume','TakerBuyBaseAssetVolume','TakerBuyQuoteAssetVolume']].astype(float)
df['NumberTrades'] = df['NumberTrades'].astype(int)
df.set_index('DateTime', inplace=True)
df['adj close'] = df['close'] # Some functions need adj close
btc_data_df = pd.DataFrame()
btc_data_df[['Open','High','Low','Close','Volume']] = df[['open','high','low','close','volume']]

# Run default strategy
from backtesting import Backtest, Strategy
from backtesting.lib import crossover

from backtesting.test import SMA, GOOG


class SmaCross(Strategy):
    def init(self):
        price = self.data.Close
        self.ma1 = self.I(SMA, price, 10)
        self.ma2 = self.I(SMA, price, 20)

    def next(self):
        if crossover(self.ma1, self.ma2):
            self.buy()
        elif crossover(self.ma2, self.ma1):
            self.sell()


bt = Backtest(btc_data_df, SmaCross, commission=.002,
              exclusive_orders=True)
stats = bt.run()
bt.plot()
stats

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis is not a (valid) bug report

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions