<jupyter_start><jupyter_text>Video tutorial[How to Download Kline Data from Exchange (part I)](https://youtu.be/JwAK892TzT0)<jupyter_code>import sys
sys.path.append("../lib")
from BinanceAPI import *
from datetime import datetime
import pandas as pd
import numpy as np
import time
import os<jupyter_output><empty_output><jupyter_text>Global variables<jupyter_code>api_key, api_secret = "", ""
exchange = "Binance"
symbol = "BTCUSDT"<jupyter_output><empty_output><jupyter_text>Load kucoin API<jupyter_code>api = BinanceAPI(api_key, api_secret)<jupyter_output><empty_output><jupyter_text>Util function<jupyter_code>def toDatetime(ts):
    return datetime.fromtimestamp(ts)

def toTimestamp(dt):
    dt = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
    return datetime.timestamp(dt)<jupyter_output><empty_output><jupyter_text>Load klines data<jupyter_code># Example responses from API documentation
# [
#   [
#     1499040000000,      // Open time
#     "0.01634790",       // Open
#     "0.80000000",       // High
#     "0.01575800",       // Low
#     "0.01577100",       // Close
#     "148976.11427815",  // Volume
#     1499644799999,      // Close time
#     "2434.19055334",    // Quote asset volume
#     308,                // Number of trades
#     "1756.87402397",    // Taker buy base asset volume
#     "28.46694368",      // Taker buy quote asset volume
#     "17928899.62484339" // Ignore.
#   ]
# ]
# get klines
etimestr = "2022-04-30 23:00:00"
etime = int(toTimestamp(etimestr)) * 1000
stime = etime - 3600 * 24 * 30 * 1000 + 1
klines = api.getKlines(symbol, "1h", stime=stime, etime=etime, limit=1000)
klines
len(klines)
toDatetime(int(klines[-1][0]/1000))
toDatetime(int(klines[0][0]/1000))<jupyter_output><empty_output><jupyter_text>Pandas dataframe<jupyter_code>def computeDF(klines):
    klines = [[pd.Timestamp(int(x[0]/1000), unit='s', tz='Asia/Hong_Kong'), \
            float(x[1]), \
            float(x[2]), \
            float(x[3]), \
            float(x[4]), \
            float(x[5]), \
            float(x[6]), \
            float(x[7]), \
            float(x[8]), \
            float(x[9]), \
            float(x[10])] \
            for x in klines]
    df = pd.DataFrame(np.array(klines), \
        columns=['Timestamp', 'open', 'high', 'low', 'close', \
        'volume', 'close_time', 'quote_asset_volume', 'num_trades', \
        'take_buy_base_vol', 'take_buy_quote_vol'])
    df = df.set_index("Timestamp") 
    return df 
df = computeDF(klines)
df.head(10)<jupyter_output><empty_output><jupyter_text>Save pandas dataframe<jupyter_code>def saveParquet(df, path):
    df.to_parquet(path)
path2save = "test.parquet"
saveParquet(df, path2save)
!ls .<jupyter_output>binance-kline-data-download.ipynb  test.parquet<jupyter_text>Load pandas dataframe from file<jupyter_code>df = pd.read_parquet(path2save)
df.head(10)
df.tail(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15363321799307958, 'fraction_numerical': 0.07128027681660899, 'mean_word_length': 3.482170542635659, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '610803', 'license': 'MIT', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1080, 'n_words': 182, 'path': 'data_downloader/binance-kline-data-download.ipynb', 'repo_name': 'nicholasalgotrading/nicholas_algo_trading', 'size': '2890'}