Instead of collecting more market data, let’s try something different. Most cryptocurrency users currently have funds located on an exchange. We can leverage the exchange’s APIs to access the asset balances and calculate their value without ever going to the exchange.
The following tutorial will take a step-by-step approach to detail how you can connect to an exchange account through APIs and access your balance data without ever logging into your exchanges.
Before we can start writing our scripts and connecting to exchanges, let’s set up our development environment by first installing the libraries we need to complete our project.
pip install shrimpy-python
For this script, we will only need access to the Shrimpy Developer API Python library. This library makes it easy to connect to exchanges, manage assets, collect data, and build applications. The full APIs and their documentation can be found here.
Once the library is installed, we can import it into our script to access all the features it provides.
import shrimpy
That’s it for libraries, so we’re ready to start constructing the meat of our script.
There are 2 different sets of API keys we need in order to access our balance data on the exchange.
The first set of API keys we need is the exchange API keys. These keys are found by going to your exchange of choice and creating a new set of API keys.
Our script will require the ability to access balance data, so make sure you have enabled the permissions on your exchange API keys for reading balance data from your exchange account. In this tutorial, you won’t need to enable the permissions for trading or withdrawals.
You can find a number of helpful tutorials on how to access exchange API keys here.
Once you’ve generated your API keys, save them as variables which we will use in the next steps to access your exchange account balances.
exchange_name = 'bittrex'
exchange_public_key = 'fbnnn1xzimjustkeyboardmashing8xn1t8'
exchange_secret_key = '09672v4n09xn0morekeyboardmashing947'
Note: Shrimpy supports 16 different exchanges. Use the following names when creating your exchange_name variable: binance, bittrex, bittrexinternational, kucoin, poloniex, bibox, huobi, hitbtc, bitstamp, bitfinex, coinbasepro, kraken, gemini, huobiglobal, bitmart, and okex.
The second set of API keys we need to access our balance data on the exchange is the Shrimpy Developer API Master Keys.
To get your set of keys, sign up for your free Shrimpy account and select the option to “Create Api Master Key”.
When selecting the permissions for this key, ensure you have enabled the options for “User” and “Account”. These are required to link and access exchange accounts for users.
With your Shrimpy Developer API key in hand, assign it to variables which can be accessed in a future step.
shrimpy_public_key = '8x7138n1xzimjustkeyboardmashing8xn1t8jv5098'
shrimpy_secret_key = '771dc5n72v4n09xn0morekeyboardmashing9475c0294n50'
Use the keys we generated in the previous step to create your Shrimpy client.
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
The Shrimpy APIs require exchange accounts to be connected to users. That way we can generate any number of users who manage multiple individual exchange accounts. This provides a built-in way to determine who owns which exchange account.
Let’s create our user who will be the one which gets linked to our exchange accounts.
create_user_response = client.create_user('Satoshi Nakamoto')
user_id = create_user_response['id']
Once the user is created, linking an exchange to the user is straight forward. Simply specify the user, exchange name, and the API keys for that exchange.
link_account_response = client.link_account(
user_id,
exchange_name,
exchange_public_key,
exchange_secret_key
)
account_id = link_account_response['id']
As soon as the exchange account is linked, Shrimpy will automatically begin collecting data from the exchange regarding the account’s balances. This process can take a moment, so let’s take 5 seconds before going on to the next step.
Note: You can link up to 20 exchange accounts to a single user. That means if you have accounts with Bittrex, Binance, KuCoin, BitStamp, Kraken, and Coinbase Pro (and more) you can link them all to a single user.
We’re almost done. Let’s grab the exchange account balance from Shrimpy. Simply call the library and ask for the balance data for a specific user and account.
The response to this call will return every asset we own, the balances for these assets, and the dollar values.
balance = client.get_balance(user_id, account_id)
holdings = balance['balances']
The final step is to calculate the balances and display them. In this case, we will just print them out, but you can certainly do anything you wish with the balances.
We will iterate over each asset in the returned list and display the asset and the value of our asset holdings.
total = 0
for asset in holdings:
print(asset['symbol'] + ' ' + str(asset['usdValue']))
total += asset['usdValue']
print("Total" + ' $' + str(total))
That’s it! You have successfully connected to your exchange account and accessed your balances without logging into the exchange through the UI.
Whenever you wish to update your balances, you can simply request the latest balances from Shrimpy again by passing in your user ID and account ID. Shrimpy will handle the rest by accessing your linked exchange account and collecting the balance data. You DO NOT need to re-link your keys every time or create a new user each time. Those steps are a one-time thing.
The first time you want to connect to an exchange and access the balance data for the assets you hold on the exchange, you can run the entire script here. Each subsequent time, you only need to run the portion which collects the balance data
This script can also easily be modified to link multiple exchanges, so you can access your balances across every exchange for which you have an account.
Fill in the information for your Shrimpy Master API keys and Exchange Keys to run this script. I’ve used fake keys for this tutorial.
# import required libraries
import shrimpy
import time
# assign your Shrimpy Master API keys for later use
shrimpy_public_key = '8x7138n1xzimjustkeyboardmashing8xn1t8jv5098'
shrimpy_secret_key = '771dc5n72v4n09xn0morekeyboardmashing9475c0294n50'
# assign your exchange keys for which you wish to access the balance data
exchange_name = "bittrex"
exchange_public_key = 'fbnnn1xzimjustkeyboardmashing8xn1t8'
exchange_secret_key = '09672v4n09xn0morekeyboardmashing947'
# create the Shrimpy client
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
# create a user which will be linked to our exchange
create_user_response = client.create_user('The Shrimp Master')
user_id = create_user_response['id']
# link our first exchange so we can access balance data
link_account_response = client.link_account(
user_id,
exchange_name,
exchange_public_key,
exchange_secret_key
)
account_id = link_account_response['id']
# wait while Shrimpy collects data for the exchange account
# only required the first time linking
time.sleep(5)
# collect asset balances on the exchange
balance = client.get_balance(user_id, account_id)
holdings = balance['balances']
total = 0
# calculate and print balances for each asset.
for asset in holdings:
print(asset['symbol'] + ' $' + str(round(asset['usdValue'], 2)))
total += asset['usdValue']
print("Total" + ' $' + str(round(total, 2)))
After running the initial script, you can simply access your balances at any time by running this second script.
Notice this script removed the steps to create a user, link an exchange account, and sleep for a few seconds.
Shrimpy will automatically remember this information for you, so all you need to do is connect to the Shrimpy APIs again, tell Shrimpy what account you want balance data for, and print out the balance data which is returned in the response.
# import required libraries
import shrimpy
# input your user and account IDs
user_id = 'dc12349b-1234-12k4-123n12n12nnf'
account_id = 12345
# assign your Shrimpy Master API keys for later use
shrimpy_public_key = '8x7138n1xzimjustkeyboardmashing8xn1t8jv5098'
shrimpy_secret_key = '771dc5n72v4n09xn0morekeyboardmashing9475c0294n50'
# create the Shrimpy client
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
# collect asset balances on the exchange
balance = client.get_balance(user_id, account_id)
holdings = balance['balances']
total = 0
# calculate and print balances for each asset.
for asset in holdings:
print(asset['symbol'] + ' $' + str(asset['usdValue']))
total += asset['usdValue']
print("Total" + ' $' + str(total))
The Shrimpy community is becoming the largest network of builders. People who are looking to push the bounds of what’s possible with crypto application development, trading, and market data analysis.
Find the full Python and Node libraries here:
We would love to hear more about what you’re building, so don’t hesitate to reach out and share your story!
Thank for reading! Please share if you liked it!
#Python #node.js #Cryptocurrency #bitcoin #blockchain