Trading Bot

Using Python and two APIs, I am creating a cryptocurrency trading algorithmic AI.

Brief

For my third year first semester project, I decided to be ambitious and experiment with APIs and focus on more backend, an area I hadn't been heavily involved with over the last 2 years. This project was a great opportunity to expand my understanding and broaden my skillset.

Development

I used Python with pycharm, the Binance API and the implementation of Bitmex API is also in development. Using Pycharm I was able to connect the Binance API and view live prices of assets, due to the Binance APIs open book accessbility. I decided on using the Binance API because it has the largest array of price lists and has the ability to connect to the live prices of assets. I had never worked with an API before so upon greater research I found the Binance API is a REST API. A REST API has advantages over alternatives, one primary benefit being the flexibility, it is very rarely limited in what it can recieve and it can interpret almost any method of sending or recieiving data.

...

The first few weeks were a challenge as I was establishing exactly what I wanted to do. I know I wanted to pursue a technology or idea to do with cryptocurrencies, a significant interest of mine. I decided to take on a trading bot as I had not heard of it before and thought it was very intriguing and would allow me to scope out my skills. I used a few different tutorials to help me craft a prototype, primarily from youtube and udemy. Unfortunately the user interface is not completed and still in development, however the program functions and can perform trades (currently using a testnet account).

...

I began by installing Pycharm and pycharm extensions like tkinter (which is being used currently to construct a user interface). I am lucky to understand the logic and functions of python having worked with the language GSCE through to A-level, it was a significant factor in helping me make the project function.

I began by retrieving the API key from the Binance testnet.

The next objective was to create the logger system that would display error messages and information such as warnings or basic information. As well as whether or not the program is connected to the Binance API. A youtube tutorial led me to the official Binance Futures website in which it displays all the keys for the unique API functions and code names for all the different pieces of data I wanted to retrieve. Such as contracts, prices, symbols (abbreviations for cryptocurrencies) and more, see https://binance-docs.github.io/apidocs/futures/en/#symbol-price-ticker

I attempted implementing the Bitmex API in addition to the Binance API, however I ran into many issues regarding the public key. I was unable to obtain an 'inactive API' which I could use for the testnet version of this program. The program needed to be done via the testnet version of both Binance and Bitmex (as it enables fake trading/trading with non-existent money for testing rather than using actual money) I could only receive the active version in which I have to log into an active account and it would require funds within that account to trade, whereas I want this to be prototyped and tested first, therefore this set me back as I had merged the code for both the Bitmex and Binance quite heavily.

I also ran into another issue regarding the server response time from Binance (shown below). The error states that the timestamp for the request is too slow by around 1000 milliseconds. Upon researching I quickly found a solution, syncing my computer clock restored connection and enabled me to conduct orders.

The longest part of this project so far has been programming the 'endpoints' the data we want to receive and display when we run the program. Each endpoint has its own URL with their own individual properties. For example, to get an order status/number you need to use the GET method, as well as having to define every piece of information that you want to receive, in my case it's everything. However, due to my existing knowledge of cryptocurrencies, finance, and the blockchain, I was able to identify integers, strings, booleans and dictionaries relatively easily. The prototype functions in regard to communicating well with the binance API and can actually conduct trade orders, although the bitmex integration recently has impaired the program's interface, the primary issue currently ongoing is bitmex prevents tkinter from working.

...
...

The two screenshots show the balance being retrieved via the GET method and the order ID being created, showing that the program can conduct trades and display them through the console. Binance gives the user 100,000USDT to begin using with their API. In the balances screenshot, the 'accounts_data' variable retrieves the attribute from the API (built in) and displays it on the console. The reasoning for the return attributes is to prevent errors and stop codes overlapping each other, it returns the attribute back to the original function it had when the code was first run. The bottom of the second screenshot displays an order id, demonstrating that the function has worked and conducted a trade. The program has a 'def cancel_order' function that can be performed also.

...
...

I approached this project becasue I have a substantial interest in cryptocurrencies and I am interested in aiding their development. I also believed that taking on APIs and using python would help me gain more confidence in working back-end, which it has. The accessbility and possibilities of APIs are an interesting avenue of self development and can pave the way for great opportunities in future. The program as a whole is still a work in progress. Using udemy and youtube were a huge help, using binance and coinbase tutorials as well as generic trading strategy coding videos. I am pleased with this project and excited to finish it with a UI, learning not only python but getting to understand the GET and POST method more is no doubt a beneficial leap towards becoming a developer in the real world of work.