Building a Trend-Trading Crypto Bot

Rohan Daggubati
5 min readApr 20, 2022

Bitcoin, Ethereum, Cardano, Solana, and many more. The rise of non-fiat currency, meaning non-government issued money, has created tremendous change in the financial world over the past several years. Leading this dramatic change was the extremely popular Bitcoin. Its increase in use and value since its genesis in 2009 has led to the ubiquitous impact cryptocurrency is having today.

However, a widespread stigma has developed surrounding the volatility of cryptocurrencies. Drastic shifts in value across a multitude of cryptocurrencies have left potential consumers wary in the process of acquisition. Some cryptocurrencies might gain 500% in value over 24 hours, whereas others might lose their value entirely.

Though it is often non as pronounced, we see shifts like this within the stock market as well. Shares offered by publicly-traded companies frequently mirror the performance of their respective companies. Investors who place their money within the stock market are tasked with overcoming sheer speculation. In order to do so, they employ algorithmic methods of analyzing stocks in order to potentially predict the future of a given commodity.

Since this practice has become so frequent among those who knowledgeably invest in the stock market, I decided to employ some of the strategies used to analyze stocks in the realm of cryptocurrencies. I wanted to see just how effective these traditional indicators, mechanisms that analyze the performance of a stock, would be for the crypto market.

This article will discuss the process behind building a python program that automatically buys and sells cryptocurrency based on a predetermined indicator mechanism. In order to do so, we will have to connect our program to a variety of libraries and a brokerage that will allow our code to run in real time markets.

Libraries

import websocket, json, pprint, talib, numpyimport configfrom binance.client import Clientfrom binance.enums import *

websocket — This client allows connects both the user and the server in a program. It opens a communication channel for APIs and operates over HTTP. Essentially, it allows for the passage of messages between a client and server.

json — This module allows for for elements of the python dictionary to be converted into a JSON string that can then be written into a file.

pprint — This native python library allows for the program to have a certain format when printed. The output is thus clearer to interperet.

talib — This technical analysis library has a variety of indicators that we can utilize when analyzing cryptocurrency price data.

numpy — This python library allows for a variety of mathematical operations to be conducted on arrays. When our information is aggregated into arrays, numpy will be used to run efficient calculations.

binance.client — By using the Binance API in our program, we allow Binance to be the broker for our crypto transactions. The commands executed within the program will be run through Binance services.

Method of Analyzing Cryptocurrency Data

As I mentioned, there are a variety of indicators that we can use in order to analyze the performance of a given cryptocurrency. For this program, we will solely be using the Relative Strength Index (RSI) indicator. The RSI is a momentum indicator that analyzes changes within the flow of cryptocurrency data.

The goal of the RSI is to see when a coin has been overbought or oversold. If a coin is oversold, it means that it has seen a dip in value, indicating an optimal time to buy. If a coin is overbought, it means that it has seen a jump in value, indicating an optimal time to sell.

The RSI calculates points where a coin is overbought or oversold by using a two-part formula:

First, the Relative Strength (RS) is calculated. The RS and RSI analyze data from a n number of days, generally 14. The RS looks at the market close data for each of these days. For each day that the market closes out higher than it started, the RS classifies that day as a gain. For each day that the market closes lower than it started, the RS classifies that day as a loss. The magnitudes of these gains and losses are averaged and divided to find the RS. Then, the RS is plugged into the RSI formula in order to get the final output for the indicator.

How the Program Interprets the Indicator

For this program, we will set both an overbought and oversold threshold.

RSI_OVERBOUGHT = 70RSI_OVERSOLD = 30TRADE_SYMBOL = 'ETHUSD'TRADE_QUANTITY = 0.007

Here we are trading Ethereum. The indicated coin value is 0.007 ETH, which amounts to roughly 20 USD. The overbought threshold is set to 70, meaning that when the RSI has a value of over 70, the program will sell any Ether within the account. The oversold threshold is set to 30, meaning that when the RSI has a value of below 30, the program will execute a buy order for 0.007 ETH through Binance.

Program Outcome

Ultimately, this program serves as a tool that can be utilized when investing in cryptocurrencies. Many people feel that they do not have the time to make diligent investments. This bot allows for the trading of a predetermined amount of crypto based on the objective analysis of an algorithmic indicator.

For more, take a look at this video describing the actual code behind the program and its outputs.

Thanks for checking my article out — if you liked it, give me a follow to see when I post new content! Feel free to reach out to me on LinkedIn or check out my website for more information.

--

--