Skip to content

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

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

Closed
mebrammel opened this issue Oct 4, 2020 · 2 comments
Closed

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

mebrammel opened this issue Oct 4, 2020 · 2 comments
Labels
invalid This is not a (valid) bug report

Comments

@mebrammel
Copy link

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
@mebrammel mebrammel changed the title Strategy backtest does not generate any information on Bitcoin OHLCV data Strategy backtest does not work on Bitcoin OHLCV 1 minute data Oct 4, 2020
@kernc
Copy link
Owner

kernc commented Oct 4, 2020

The issue is your initial cash is too low, and buying fractional units is not supported. You should apply the proposed workaround and trade in smaller units (e.g. μBTC).

@kernc kernc closed this as completed Oct 4, 2020
@kernc kernc added the invalid This is not a (valid) bug report label Oct 4, 2020
@mebrammel
Copy link
Author

Thank you very much, changing the CASH parameter to 100000 fixed the problem for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This is not a (valid) bug report
Projects
None yet
Development

No branches or pull requests

2 participants