Hello guys, today I’ll show you a quick and dirty method on how to get Trades/aggTrades/K-line/Index K-line… data for free (I think up to 2020).
The data is from Binance only!
You can find the data here: https://www.binance.com/en/landing/data
If you click on “Trades” > “Spot Data” you will see that Binance let you download each day seperatly. Because I do not want to click everything seperatly I wrote the following Python script to download the zip files (with the trade data in it) all at once – example on BTCUSDT (spot), trade data, last 365 days:
from datetime import date, timedelta
import requests
def download_file(url):
# Send a request to the URL
response = requests.get(url)
# Check if the response is successful
if response.status_code == 200:
# Get the filename from the URL
file_name = url.split("/")[-1]
# Open a file in write mode
with open(file_name, "wb") as f:
# Write the contents of the response (in binary format) to the file
f.write(response.content)
print(f'Successfully downloaded {file_name}')
else:
print(f'Failed to download file. Status code: {response.status_code}')
# Get the current date
today = date.today()
# Set the number of days to go back
n = 365
# Iterate over the last n days
for i in range(1, n+1):
# Subtract the loop index from the current date
dateToPrint = today - timedelta(i)
# Format the date as a string in the desired format
date_str = dateToPrint.strftime("%Y-%m-%d")
# Print the date
print(date_str)
download_file("https://data.binance.vision/data/spot/daily/trades/BTCUSDT/BTCUSDT-trades-"+date_str+".zip")
I changed the code a bit to iterate through a list with different cryptocurrency pairs:
import requests
def download_file(url):
# Send a request to the URL
response = requests.get(url)
# Check if the response is successful
if response.status_code == 200:
# Get the filename from the URL
file_name = url.split("/")[-1]
# Open a file in write mode
with open(file_name, "wb") as f:
# Write the contents of the response (in binary format) to the file
f.write(response.content)
print(f'Successfully downloaded {file_name}')
else:
print(f'Failed to download file. Status code: {response.status_code}')
if __name__ == "__main__":
from datetime import date, timedelta
# Download the last n days of the crypto pairs in list x
# Get the current date
today = date.today()
# Set the number of days to go back
n = 7
# Cryptocurrency pairs to download data from
x = ["BTCUSDT", "ETHUSDT", "XRPUSDT", "BNBUSDT", "LTCUSDT", "SOLUSDT", "MANAUSDT", "DOGEUSDT", "LINKUSDT",
"TRXUSDT",
"MATICUSDT", "ADUSDT", "DREPUSDT", "ATOMUSDT", "BCHUSDT", "LUNCUSDT", "APEUSDT", "DOTUSDT", "ETCUSDT",
"AVAXUSDT",
"SANDUSDT", "HIVEUSDT", "MASKUSDT", "SHIBUSDT", "DAShUSDT", "XMRUSDT", "WAVESUSDT", "GALAUSDT", "UNIUSDT",
"EOSUSDT", "FILUSDT", "AXSUSDT", "CHZUSDT", "FTMUSDT", "VOXELUSDT", "ZECUSDT", "XLMUSDT", "DYDXUSDT",
"NEARUSDT",
"APTUSDT", "LOOMUSDT", "AAVEUSDT", "OPUSDT", "STXUSDT", "WBTCUSDT", "MTLUSDT", "SUSHIUSDT", "ALGOUSDT"]
for crypto_pair in x:
# Iterate over the last n days
for j in range(1, n + 1):
# Subtract the loop index from the current date
dateToPrint = today - timedelta(j)
# Format the date as a string in the desired format
date_str = dateToPrint.strftime("%Y-%m-%d")
# Print the date
print("Downloading", crypto_pair, date_str, "data...")
download_file(
"https://data.binance.vision/data/spot/daily/trades/" + crypto_pair + "/" + crypto_pair + "-trades-" + date_str + ".zip")
Now we also need to create one big csv out of these csv’s. The following script should do it:
import zipfile
import pandas as pd
import io
if __name__ == "__main__":
from datetime import date, timedelta
# Download the last n days of the crypto pairs in list x
# Get the current date
today = date.today()
# Set the number of days to go back
n = 7
# Cryptocurrency pairs to download data from
x = ["BTCUSDT", "ETHUSDT", "XRPUSDT", "BNBUSDT", "LTCUSDT", "SOLUSDT", "MANAUSDT", "DOGEUSDT", "LINKUSDT",
"TRXUSDT",
"MATICUSDT", "ADUSDT", "DREPUSDT", "ATOMUSDT", "BCHUSDT", "LUNCUSDT", "APEUSDT", "DOTUSDT", "ETCUSDT",
"AVAXUSDT",
"SANDUSDT", "HIVEUSDT", "MASKUSDT", "SHIBUSDT", "DAShUSDT", "XMRUSDT", "WAVESUSDT", "GALAUSDT", "UNIUSDT",
"EOSUSDT", "FILUSDT", "AXSUSDT", "CHZUSDT", "FTMUSDT", "VOXELUSDT", "ZECUSDT", "XLMUSDT", "DYDXUSDT",
"NEARUSDT",
"APTUSDT", "LOOMUSDT", "AAVEUSDT", "OPUSDT", "STXUSDT", "WBTCUSDT", "MTLUSDT", "SUSHIUSDT", "ALGOUSDT"]
for crypto_pair in x:
print("Creating one csv file with data from:", crypto_pair, " (saving as", crypto_pair + "_trades_data.csv) ...")
csv_contents = []
zip_file_names = []
# Iterate over the last n days (the other way - begin with old data and add newer data)
for j in range(n, 0, -1):
# Subtract the loop index from the current date
dateToPrint = today - timedelta(j)
# Format the date as a string in the desired format
date_str = dateToPrint.strftime("%Y-%m-%d")
# Print the date
zip_file_names.append((crypto_pair + "-trades-" + date_str + ".zip"))
for zip_file in zip_file_names:
print("Currently at zip:", zip_file)
# Open the zip file
with zipfile.ZipFile(zip_file, 'r') as z:
# Extract the CSV file from the zip file
csv_file = z.namelist()[0]
csv_data = z.read(csv_file)
# Read the CSV data into a DataFrame and append it to the list
df = pd.read_csv(io.BytesIO(csv_data))
csv_contents.append(df)
print("Saving csv...")
pd.concat(csv_contents).to_csv(crypto_pair+".csv", index=False)
print("Csv file created!")
To plot everything you can use this code:
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
# Use BTCUSDT's trades to plot - load in data
data = np.genfromtxt('BTCUSDT_trades_data.csv', delimiter=',')
# x: date time (timestamp in ms) - y: price the trade was made
plt.plot(np.array([datetime.fromtimestamp(ts/1000) for ts in data[:, 4]]), data[:, 1])
plt.show()
The code can be found here too: www.github.com/Heuristic-Analyst/…
Cheers!