Prepare dataset for backtest strategy

Binance Vision provide archive dataset of coins for easy download access. In this article I will guide you to get data of BTCUSDT (spot market) in 2020 kline format.

# run this code in Jupyter notebook or Google Colab
!for i in `seq -w 1 12`; do wget https://data.binance.vision/data/spot/monthly/klines/BTCUSDT/1h/BTCUSDT-1h-2020-$i.zip; done; \
unzip -o "*.zip"

Data should be downloaded in the current work directory.

Let’s process this data

import pandas as pd

def load_files(filenames):
  for filename in filenames:
    yield pd.read_csv(filename, usecols=(range(0, 6)), names=['Open Time', 'Open', 'High', 'Low', 'Close', 'Volume'])


df = pd.concat(load_files(glob.glob(os.path.join('', "*.csv"))))
df['Open Time'] = pd.to_datetime(df['Open Time'], unit='ms')
df = df.set_index('Open Time')
df = df.sort_index()

Dataset loaded and sorted to Pandas Dataframe.

Yo, try to visual to graph using matplotlib

import matplotlib.pyplot as plt

from matplotlib.pyplot import figure

figure(figsize=(30, 20), dpi=80)

plt.plot(df['Close'], label='Close')

plt.legend()
plt.title('BTCUSDT in 2020')
plt.show()

Recap:

  • Use archive data instead of api fetch for fast init dataset.
  • Load data to dataframe (must sort_index before) and visual.

Next step:

  • Add simple indicators for find trading signals.

1 thought on “Prepare dataset for backtest strategy

  1. Pingback: Calculate MA, SMA indicators in Pandas - Thiên Toán

Leave a Reply

Your email address will not be published. Required fields are marked *