Python Script for Extracting K-Line Candles Data from Binance: Enhancing Crypto Market Analysis

Introduction

In the fast-paced world of cryptocurrency trading, having access to reliable and up-to-date market data is crucial. This article discusses a Python script designed to extract K-line (candlestick) data from the Binance cryptocurrency exchange for a specific trading pair.

Understanding K-Line Data

K-line data, commonly known as candlestick data, is vital for technical analysis in trading. Each candlestick in a K-line chart represents the open, high, low, and close prices of an asset for a specific time frame. Traders use this data to predict future market movements and make informed decisions.

The Python Script

The script uses Python's requests library to interact with the Binance API. It fetches data within a specified date range and interval (or timeframe) for a given trading pair, such as BTC/USDT. The script is capable of handling multiple timeframes and neatly organizes the fetched data into CSV files for each timeframe.

import requests
import datetime
import time
import csv

# Functions for working with time
def get_milliseconds(date_str):
    epoch = datetime.datetime.utcfromtimestamp(0)
    return int((datetime.datetime.strptime(date_str, "%d %b %Y") - epoch).total_seconds() * 1000.0)

# Function for getting data from Binance
def get_binance_data(symbol, start_str, end_str, interval):
    url = "https://api.binance.com/api/v3/klines"
    start_time = get_milliseconds(start_str)
    end_time = get_milliseconds(end_str)
    limit = 1000

    all_data = []

    while start_time < end_time:
        params = {
            'symbol': symbol,
            'interval': interval,
            'limit': limit,
            'startTime': start_time,
            'endTime': end_time
        }

        response = requests.get(url, params=params)
        if response.status_code == 200:
            data = response.json()
            if not data:
                break
            all_data.extend(data)
            start_time = data[-1][0] + 1
        else:
            break

        time.sleep(0.5) # Delay to prevent exceeding request limits

    return all_data

# Binance timeframes
timeframes = ['1m', '3m', '5m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h', '1d', '3d', '1w', '1M']

# Retrieving and saving data for each timeframe
for timeframe in timeframes:
    print(f"Сбор данных для таймфрейма: {timeframe}")
    data = get_binance_data('BTCUSDT', '01 Jan 2022', '31 Dec 2022', timeframe)
    filename = f'btcusdt_{timeframe}_2022.csv'

    with open(filename, 'w', newline='') as file:
        writer = csv.writer(file)
        for row in data:
            writer.writerow(row)
    
    print(f"Data saved in {filename}")

print("All data has been successfully collected and saved.")

Key Features of the Script

  • Timeframe Flexibility: It supports various timeframes, including 1 minute, 3 minutes, and others.
  • Date Range Customization: Users can specify the start and end dates for the data extraction.
  • Automatic Pagination: The script automatically handles pagination to retrieve all available data within the given range.
  • Rate Limit Management: It includes a delay between requests to prevent hitting the API rate limits.

Practical Applications

This script is particularly useful for traders and analysts who need historical data for backtesting trading strategies, conducting market analysis, or building machine learning models for price prediction.
Back to articles

About

Explore Aber Vulgue's blog for expert articles on programming, software development, and DevOps. Dive into practical guides on JavaScript, C++, mobile app development, neural networks, and more to enhance your tech skills.

Subcategories

Non penis canina est!