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'

Note: Shrimpy requires a monthly subscription to execute trades, manage users, and collect data on exchange accounts. The monthly plans start at $14/mo. To continue to the next step, select a plan which is most appropriate for you.

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.