Automating Crypto Portfolios with Trading Scripts
Automating your cryptocurrency portfolio can be a complex process if you try to implement the management logic from scratch. Instead of building the automation infrastructure from the ground up, this article will cover how to implement an automated portfolio strategy with only a few lines of code in Python.
The time consuming process of constructing a diverse portfolio is reduced to simple logic. No more logging into your cryptocurrency exchanges, or manually calculating the balances of each of your assets across exchanges.
Our mission is to simplify the cryptocurrency trading experience. To help with this goal, we’ve put together a series of tutorials on how to build your own trading bot. Keep up with these tutorials by joining our telegram.
Automate your portfolio to save time and manage your funds the way that works best for you.
Install Trading Library
Before we can start automating our trades with Python scripts, we will need to install the library which makes it all possible. Throughout this tutorial, we will use the Shrimpy Universal Crypto Trading APIs. These APIs provide simple endpoints to access data, execute trades, and manage exchange accounts across 16 of the most popular crypto exchanges.
Install the library using the following command.
pip install shrimpy-python
Import Trading Library into Your Script
Once installed, we can start constructing our script. The first step is to import the trading library into your script so you can access the convenient trading features it provides.
import shrimpy
API Keys
Two different API keys are required for a one time setup. After the initial linking to the Shrimpy trading library, you won’t need to link them again. The two sets of API keys are the Exchange API Keys and the Shrimpy Master Keys.
Exchange API Keys
You can access your exchange API keys by going to the exchange of your choice and generating new API keys.
Tutorials on how to generate API keys on each exchange can be found here. Once you get to the step where it says to input your API keys into Shrimpy, you can stop and come back to this tutorial.
exchange_name = 'bittrex'
exchange_public_key = 'fbnnn1xzimjustkeyboardmashing8xn1t8'
exchange_secret_key = '09672v4n09xn0morekeyboardmashing947'
Note: In order to execute trades on an exchange through APIs, you will need to enable both “Trading” and “Balance” permissions on the API key. You DO NOT need to provide “Withdrawal” permissions.
Shrimpy supports the following 16 different exchanges, which you can specify by replacing their name for “exchange_name”: binance, bittrex, bittrexinternational, kucoin, poloniex, bibox, huobi, hitbtc, bitstamp, bitfinex, coinbasepro, kraken, gemini, huobiglobal, bitmart, and okex.
Shrimpy Master Keys
To access API keys for trading with the Python library, we need to sign up for the Shrimpy Universal Crypto Trading APIs.
Once you sign up, you will be able to “Create Api Key” from your registered account. After generating your key and storing your public and secret key in a secure location, make sure you have selected the correct permissions for your Master Keys.
The Master Keys require permissions for “User”, “Account”, and “Trading”. Without all of these permissions enabled, you will not be able to complete this tutorial.
shrimpy_public_key = '8x7138n1xzimjustkeyboardmashing8xn1t8jv5098'
shrimpy_secret_key = '771dc5n72v4n09xn0morekeyboardmashing9475c0294n50'
Create Trading Client
We’re done with digging around for keys. The rest of our script will be simple coding here on out.
Start by creating the client that will be used to make each of the calls to the Shrimpy servers. Simply pass in the public and secret Shrimpy keys.
client = shrimpy.ShrimpyApiClient(shrimpy_public_key, shrimpy_secret_key)
Create Trading User
Before we can start connecting to exchanges and executing trades, we must create a user. Each exchange we link is required to have a user which owns the exchange account. This ideally would represent a real-world user who will be authorizing the APIs to trade on their behalf.
create_user_response = client.create_user('Satoshi')
user_id = create_user_response['id']
Connect Exchange Account
Once the user has been created, we can link each of the exchange accounts this user owns to the user. In this example, we will only link one exchange account, but the library allows you to link up to 20 individual exchange accounts to each user.
link_account_response = client.link_account(
user_id,
exchange_name,
exchange_public_key,
exchange_secret_key
)
account_id = link_account_response['id']
Get List of Exchange Assets
There are several public endpoints in the Shrimpy APIs. One of those public endpoints allows you to access the list of available assets on a particular exchange.
With this example, we will ask for assets available on Bittrex.
exchange_assets = client.get_exchange_assets('bittrex')
Select Assets for Portfolio
Once we know what assets we can buy or sell on the exchange, it’s time to construct our portfolio.
This can be done using any strategy you desire.
There is no limit to the methodology you use when selecting assets. The only requirement is 100% of the portfolio must be allocated. That means after passing in each of the asset percentages, the total should equal exactly 100%.
For this example, we will simply take the first 5 assets which were returned in the request to get exchange assets and put them in our portfolio with an equal weighting of 20%.
portfolio = []
# add the first 5 assets returned for the exchange to our portfolio
for i in range(0,5):
print("Allocating 20% " + exchange_assets[i]['tradingSymbol'])
Note: Selecting the first 5 assets that are returned from the “get_exchange_assets” call is not a real portfolio strategy developed by professionals. It’s simply an example. It would not be a good idea to actually use this as your portfolio strategy.
Allocate Portfolio
After constructing the list of assets you would like in your portfolio, simply allocate the portfolio using the trading library.
This will immediately begin executing trades to construct the portfolio you requested. It’s as simple as that. It could take a moment to complete the trades, so feel free to continue reading or take a second to reflect on how easy it was to trade without ever logging into your exchange account.
Warning: Running this next snippet will allocate a portfolio by executing trades on the real exchange you linked. This is not a toy. It’s literally going to buy and sell ALL of the assets you own to construct the portfolio you previously specified. We will not reimburse you for lost funds which result from trading fees, careless use of this script, or any other situation which may arise due to the use of these scripts. If you do not want to actually trade, stop now. You can always come back later when you have a strategy put together and know what you want to do.
client.allocate(
user_id,
account_id,
{
'isDynamic': False,
'allocations': portfolio
}
)
To check when it’s done allocating, you can call the following endpoint.
accounts = client.list_accounts(user_id)
Each account will have a flag for “isRebalancing”. Once this flag is set to False, the allocation will be complete.
After the portfolio is allocated, you can check the resulting balances by making this call to the Shrimpy APIs. This will return each asset you own in your portfolio and the value of that holding.
balance = client.get_balance(user_id, account_id)
Putting It All Together
Now that we understand everything required to construct a cryptocurrency portfolio through APIs, we can combine the steps into a single script.
This script can be run to rapidly construct a portfolio with the desired percent allocations.
Before running any scripts, we recommend reviewing the Shrimpy Developer API Docs to get a better understanding of how the trades are executed.
Warning: This script will actually trade on your exchange account. If you do not want to trade on your exchange account, do not run this script.
Before running the script, update the portfolio selection logic to implement your strategy. Don’t just randomly select assets like this script does.
# import required libraries
import shrimpy
# 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']
# get the list of assets available on the exchange
exchange_assets = client.get_exchange_assets('bittrex')
# select assets to put in your portfolio
portfolio = []
# add the first 5 assets returned for the exchange to our portfolio
# warning: insert your own portfolio construction logic here before running
for i in range(0,5):
portfolio.append({'symbol': exchange_assets[i]['tradingSymbol'], 'percent': '20'})
print("Allocating 20% " + exchange_assets[i]['tradingSymbol'])
# allocate the portfolio
client.allocate(
user_id,
account_id,
{
'isDynamic': False,
'allocations': portfolio
}
)
After running this script which links your exchange account and generates a user. You do not need to run the entire script again. Shrimpy will automatically store the linkage between your user and the exchange.
Subsequent Script Runs
Each next time, you can run the following script to update your portfolio allocations. As you can see, this leaves out the steps of creating a new user and linking the exchange to the user.
# 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)
# get the list of assets available on the exchange
exchange_assets = client.get_exchange_assets('bittrex')
# select assets to put in your portfolio
portfolio = []
# add the first 5 assets returned for the exchange to our portfolio
# warning: insert your own portfolio construction logic here before running
for i in range(0,5):
portfolio.append({'symbol': exchange_assets[i]['tradingSymbol'], 'percent': '20'})
print("Allocating 20% " + exchange_assets[i]['tradingSymbol'])
# allocate the portfolio
client.allocate(
user_id,
account_id,
{
'isDynamic': False,
'allocations': portfolio
}
)
Trading Bots for Cryptocurrency
Developers can use these simple scripts as the foundation for cryptocurrency trading bots. By selecting the assets to allocate in a portfolio, instantly execute trades to match the desired percentages.
It can’t get any easier.
Shrimpy integrates 16+ different exchanges which makes the development process seamless across every exchange. No unique behaviors or odd errors. The consistency is built into the platform.
Learn more about everything Shrimpy offers by joining our Telegram.
To access the complete Python and Node libraries, follow these links:
Don’t hesitate to reach out if you want to share with our team what you’re building!
About Shrimpy
Shrimpy leads the market as the premier portfolio management application. Users are able to configure a custom crypto portfolio and implement a passive rebalancing strategy, removing the hassle of having to actively trade crypto.
Shrimpy Web Application: Shrimpy - Cryptocurrency Portfolio Management
Shrimpy’s Developer Trading API is a unified way to integrating trading functionality across every major exchange. Collect historical market data, access real-time websockets, execute advanced trading strategies, and manage an unlimited number of users.
Shrimpy Crypto Trading API: Shrimpy | Crypto Trading APIs for Developers
Don’t forget to follow us on Twitter and Facebook for updates, and ask any questions to our amazing, active community on Telegram.
The Shrimpy Team