3 Free APIs to Kickstart Your Automated Trading Strategies Today
...And Their Full Python Scripts
Access market data with these 3 free APIs using only a few lines of code.
Gain instant access to OHLCV (Open, High, Low, Close, Volume) data and more for stocks, digital assets, and forex.
You can use the ccxt library to fetch data from Kraken.
You can use direct HTTP requests to get data from Binance.
You can use the yfinance library to get data from Yahoo Finance.
We will fetch the market data without defining functions for simplification purposes. That way, beginner programmers can still access market data while understanding what’s happening in the code.
1. Fetching Data from Kraken using ccxt
By running the following script, you will obtain 720 days of OHLCV market data.
import ccxt
import pandas as pd
# Here we initialize the Kraken "connection"
kraken = ccxt.kraken()
# Setting the trading pair and the timeframe
symbol = 'BTC/USD'
timeframe = '1d'
# Fetch OHLCV data
ohlcv = kraken.fetch_ohlcv(symbol, timeframe)
# Then we convert the fetched data into a DataFrame and process timestamps
data_kraken = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
data_kraken['timestamp'] = pd.to_datetime(data_kraken['timestamp'], unit='ms')
data_kraken.set_index('timestamp', inplace=True)
data_kraken.tail()
2. Fetching Data from Binance using HTTP Requests
By running the following script, you will obtain 365 days of market data. Note that you can fetch more than just the OHLCV data.
import requests
# Below we fefine the symbol, interval, and time range
symbol_binance = 'BTCUSDT'
interval = '1d'
start_time = '1640995200000' # Example timestamp for 01-01-2022
end_time = '1672531199000' # Example timestamp for 01-01-2023
# Set the URL and fetching raw data
url = f"https://api.binance.com/api/v3/klines?symbol={symbol_binance}&interval={interval}&startTime={start_time}&endTime={end_time}"
response = requests.get(url)
data_binance_raw = response.json()
# Then we convert the raw data into a DataFrame and process timestamps
columns_binance = ['Open time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close time', 'Quote asset volume', 'Number of trades', 'Taker buy base asset volume', 'Taker buy quote asset volume', 'Ignore'] # Choose what you need
data_binance = pd.DataFrame(data_binance_raw, columns=columns_binance)
data_binance['Open time'] = pd.to_datetime(data_binance['Open time'], unit='ms')
data_binance['Close time'] = pd.to_datetime(data_binance['Close time'], unit='ms')
data_binance.tail()
Here, the variables fetched from Binance are:
Open time: The opening time of the time interval (in datetime format).
Open: The opening price of the asset for the given interval (as a string, convert to float for numerical operations).
High: The highest price of the asset during the interval (as a string, convert to float for numerical operations).
Low: The lowest price of the asset during the interval (as a string, convert to float for numerical operations).
Close: The closing price of the asset for the given interval (as a string, convert to float for numerical operations).
Volume: The trading volume of the asset during the interval (as a string, convert to float for numerical operations).
Close time: The closing time of the time interval (in datetime format).
Quote asset volume: The trading volume measured in the quote currency (as a string, convert to float for numerical operations).
Number of trades: The number of trades that occurred during the interval (as an integer).
Taker buy base asset volume: The volume of taker buy orders measured in the base asset (as a string, convert to float for numerical operations).
Taker buy quote asset volume: The volume of taker buy orders measured in the quote asset (as a string, convert to float for numerical operations).
Ignore: A placeholder for future use (as a string, generally ignored in analysis).
You can also choose to only fetch the OHLCV data and leave the other variables.
How to convert to float for numerical operations
data_binance['Open'] = data_binance['Open'].astype(float)
3. Fetching Data from Yahoo Finance using yfinance
You can use Yahoo Finance to retrieve stock data.
import yfinance as yf
# Define the symbol and fetch data from Yahoo Finance
symbol_yahoo = 'TSLA'
data_yahoo = yf.download(symbol_yahoo, start="2022-01-01", end="2023-01-01")
data_yahoo
Conclusion
You can now access market data from Kraken, Binance, and Yahoo Finance with just a few lines of code.
Don’t forget API rate limits and always verify the data before using it in a trading strategy!
Thanks for reading and stay tuned for more articles! If you enjoyed reading this article, please follow and clap for more full Python scripts.
Follow us on Medium
Disclaimer: While this article uses real market data for demonstration purposes, it is imperative to note that the content is provided for informational and educational purposes only. This article (python script included) is not under any circumstances financial advice. This article and code aim to enhance financial knowledge and Python programming skills. Always seek advice from a qualified financial professional before making any investment decisions. Investing involves risks, including the potential loss of principal.