Say, you want to get the current price of a cryptocurrency using Python’s inofficial binance API. How do you do this for a precise point in time?
Step 1: Install Python Binance with pip
If you haven’t already, install the Python Binance API using the pip package manager. Run
pip install python-binance in your command line, shell, or terminal. You will use this library to connect to the Binance API and access data such as the price data you seek.
$ pip install python-binance
Step 2: Register with Binance
You need to have an account with Binance or you won’t be able to connect to the Binance API. You can create an account here.
Step 3: Generate a Binance API Key
An API Key allows you to connect with Binance’s data set. It defines and restricts the permissions you have. For example, Binance may decide to give you a certain quota based on your access rights.
An API Secret, also referred to as API Private Key is a password used in combination with the API Key.
You can learn more about Binance API keys here.
Step 4: Create a Client Object
Client object serves as an access point to the Binance API. It wraps all the complexity needed to communicate with the Binance servers and actually issuing the requests to their servers. You create the Client object once by passing your API key and your API secret in the constructor (I should say, initialization) method. After creation, you can simply call methods on the method to perform all kinds of requests such as getting price data on certain cryptocurrencies.
Here’s how you can create a
Client object in your Python script:
client = Client(api_key, api_secret)
Step 5: Get the Historical Price Data using client.get_historical_klines()
The Binance API will retrieve “klines” or “candlestick” data for you if you call the method
- The first argument is the pair of assets you need. For example, to get the price data from Ethereum to Bitcoin, you’d pass the string “ETHBTC” into it. I’ll give you a list of common crypto abbreviations at the end of this article.
- The second argument is the time interval you seek price data for. There are several predefined interval attribute in the
Clientobject. For example,
Client.KLINE_INTERVAL_1MINUTEwill return an interval size of 1-minute chunks.
- The third and optional fourth arguments give you a way to specify the duration of the data you seek. For example,
"1 day ago UTC"will give you yesterday’s price data.
Here’s an example code snippet drawn from the documentation:
# fetch 1 minute klines for the last day up until now klines = client.get_historical_klines("BNBBTC", Client.KLINE_INTERVAL_1MINUTE, "1 day ago UTC") # fetch 30 minute klines for the last month of 2017 klines = client.get_historical_klines("ETHBTC", Client.KLINE_INTERVAL_30MINUTE, "1 Dec, 2017", "1 Jan, 2018") # fetch weekly klines since it listed klines = client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017")
These are some possible crypto asset abbreviations you may need:
- BCH – Bitcoin Cash
- BTC – Bitcoin
- DASH – Dash
- DOGE – Dogecoin
- ETC – Ethereum Classic
- ETH – Ether (also known as Ethereum)
- LTC – Litecoin
- NEO – Neo
- NMC – Namecoin
- Nxt – NXT
- POT – PotCoin
- PPC – Peercoin
- USDC – USD Coin (stablecoin)
- USDT – Tether
- XMR – Monero
- XRP – Ripple
You can find more in this article.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.