When the worlds of finance and programming come together it allows for some very fun and exciting projects. Cryptocurrencies take that excitement to a whole new level because of the massive amounts of data. That data is being generated 24 hours a day, 7 days a week. What can be done with this data to derive meaning and knowledge is limitless. However, it is crucial to build a foundation. To begin, we will leverage the programming language of python, a library created for Coinbase Pro and a core technical analysis tool called RSI (Relative Strength Index) — buy low, sell high momentum trading strategy.
Step 1: Set up an account with Coinbase and Coinbase Pro
I would recommend setting up an account with Coinbase because of their rewards program. It will give you some capital to play around with. As for Coinbase Pro, once you play around with this bot, this is where you would tie in with your API key(s) to actually trade.
Step 2: Pip install cbpro
cbpro is the unofficial python wrapper for Coinbase Pro. If you head over to coinbases docs, you will find that they have put a link to the github repo.
Step 3: Pip install ta
ta is a python package that makes it really easy to do technical analysis on time-series data. We will be using this package to perform the RSI calculation.
Step 4: Write the Python Script
Lets go through how to set things up for the script.
- First, we import all the packages that are necessary.
- Second, we add the variables for coinbase.
- Third, the rsi setup sets the number of days for the calculation, the overbought and oversold threshold.
- Fourth, the data storage creates some arrays and a dictionary to store the outcome of the bots activities.
To start the websocket client and test your crypto bot, run the following.
wsClient = myWebsocketClient()
Once the websocket client has begun, the on_open function will run first. Afterwards, for each piece of data sent from the websocket server the on_message function will run. That is where we collect the prices, calculate the 14 day RSI, and then check the last RSI value in an array and run it through some logic. Right now, this script is not connected to a coinbase pro api, it is just running on the public client and storing the outcome in a pandas dataframe. When the websocket client is closed, it runs the on_close function. See the output of each function below.
Step 5: Continue to build and learn
This is just a starter script that is intended to be used as a foundation to build upon. Next steps are to figure out the optimal time interval to maximize returns after fees. There are also a lot of other cryptocurrency pairs to test this with as well. Lastly, once it is profitable and reliable, connect some API keys and make some actual orders.
My next article will be to show how to run this in an EC2 instance in AWS, store the results in s3, and send SMS messages each time you make a trade.
The worlds of crypto, finance, technical analysis and programming have collided. We now have a foundation for working with websockets, financial technical analysis tools, and cryptocurrencies. Now, the biggest challenge we face is taking the initiative to be creative and continue building.