How to Download Cryptocurrencies Data in Python
A Quick Way of Importing Historical Cryptocurrency Data in Python
Historical data is needed to be able to do research and perform back-tests and therefore a nice and clean way of importing it becomes essential. This article discusses a way of quickly importing cryptocurrency data from Binance using Python.
The Fibonacci Trading Book is finally out! Filled with Fibonacci-based trading methods (tools, indicators, patterns, and strategies), this book will guide you through improving your trading and analysis by incorporating an important technical analysis approach that is Fibonacci [PDF Version available, see end of article).
The Fibonacci Trading Book
Amazon.com: The Fibonacci Trading Book: 9798394344046: Kaabar, Sofien: Booksamzn.to
Importing Cryptocurrency Data
Importing data requires requesting said data from a third party provider. We can do this in Python in many ways. Up until now, we have seen how to do this using a MT5 API. For cryptocurrency data, it may be better to use Binance. For starters, we have to import the needed libraries as follows.
import requests
import json
import pandas as pd
import numpy as np
import datetime as dt
Next, we select our preferred time frame in a variable we call frequency.
frequency = '1h'
Finally, the below function can be used to import the necessary cryptocurrency as we will see later.
def import_crypto(symbol, interval = frequency):
# Getting the original link from Binance
url = 'https://api.binance.com/api/v1/klines'
# Linking the link with the Cryptocurrency and the time frame
link = url + '?symbol=' + symbol + '&interval=' + interval
# Requesting the data in the form of text
data = json.loads(requests.get(link).text)
# Converting the text data to dataframe
data = np.array(data)
# data = data.astype(np.float)
data = data[:, 1:5]
return data
To import the currency on the hourly time frame, we simply have to call the function with the right name and store it in a variable (by default, it will be an array).
my_data = import_crypto('BTCUSDT')
As is implied, BTCUSDT is the value of Bitcoin relative to Tether (the greenback’s proxy in the crypto world). For example, if we wanted to import Cardano’s historical data, we would use ADAUSDT.
Plotting the Data Using Candlesticks
After importing the data, it is useful to visualize it through complete charts. One of these complete charts are candlesticks. Suppose we have the array called my_data that we imported earlier. We can define the following function that will allow us to chart it.
import matplotlib.pyplot as plt
def ohlc_plot_candles(data, window):
sample = data[-window:, ]
for i in range(len(sample)):
plt.vlines(x = i, ymin = sample[i, 2], ymax = sample[i, 1], color = 'black', linewidth = 1)
if sample[i, 3] > sample[i, 0]:
plt.vlines(x = i, ymin = sample[i, 0], ymax = sample[i, 3], color = 'green', linewidth = 3)
if sample[i, 3] < sample[i, 0]:
plt.vlines(x = i, ymin = sample[i, 3], ymax = sample[i, 0], color = 'red', linewidth = 3)
if sample[i, 3] == sample[i, 0]:
plt.vlines(x = i, ymin = sample[i, 3], ymax = sample[i, 0] + 0.00001, color = 'black', linewidth = 1.75)
plt.grid()
Now, we call it as follows.
ohlc_plot_candles(my_data, 250)
The result is the following chart.
As you can see, not much is needed to achieve a historical import and complex charting.
The PDF link for the Fibonacci Trading Book is the following (make sure to include your e-mail address in the notes part):
Pay Kaabar using PayPal.Me
Go to paypal.me/sofienkaabar and type in the amount. Since it’s PayPal, it’s easy and secure. Don’t have a PayPal…paypal.me