Crypto trading automation with Django and Celery

In the light of the rapid development of the crypto industry and crypto trading in particular, our team, as part of the experiment, decided to create a trading robot whose main purpose is to trade on the poloniex crypto platform . In this article I will try to talk about all the difficulties that arose during the writing of the robot, as well as the results that we were able to achieve.


Trade


Why python
While writing the robot, I actively studied Python 3 versions myself and one of its most popular frameworks - Django. Therefore, there were no issues with choosing a development language.

Why django
На первых порах мы написали робота, работающего в терминале. После появилась необходимость визуально наблюдать за работой робота, добавлять несколько аккаунтов для торговли, настраивать конфигурацию — для всего этого был необходим некий Web-интерфейс.


The beginning of the story


At the beginning of our journey, we needed a resource where we could track our “possessions” - that is, balances of different currencies on different exchanges in one place, with charts and full amounts. There was also a need to add wallets of major currencies, with a tracking of the transaction history on them.


Bot home page

Robot Operation Algorithm


Initially, the robot polled the exchange once every few seconds, made some simple calculations using the calculation of Bollinger bands ( link ), and placed orders if it considered that it was profitable.


This algorithm worked for several months, after which the idea arose to fundamentally change the logic of work - the robot should not have asked the exchange for a change in the exchange rate, it should have known about the price change at the very moment of its change. At the time of writing the script, one of the cryptocurrency platforms we used was such functionality - poloniex. This functionality is WAMP protocol . He allowed to subscribe to the necessary channel on the exchange, and to receive timely messages on changes in rates.


In general, by subscribing to Web Socket poloniex we get not only information about exchange rates. In addition, we receive information about the so-called “glasses” of bids
& asks, about orders and even about messages in the local chat, but this is a completely different story.

After we received the most relevant information about the current situation on the exchange, it was necessary to think up - when to place an order, under what conditions to cancel it and how long a buy or sell order will live if it cannot be executed.


When to place an order?


The main goal of any robot with similar tasks is to correctly determine the moment for buying and selling. In our script, we decided to use the direction mechanism, when the algorithm tries to predict the next step, according to which the necessary currency will go. The following is an excerpt from the bot’s logs, where you can see the “directions” for the BTC - ETH pair:


0, date: 2017-10-04 11: 14: 05.217362, last: 0.06349930 
1, date: 2017-10-04 11: 14: 41.798799, last: 0.06349955 
1, date: 2017-10-04 11: 19: 54.559547, last: 0.06360275 
1, date: 2017-10-04 11: 20: 37.940202, last: 0.06370000 
0, date: 2017-10-04 11: 20: 52.351764, last: 0.06371080 
1, date: 2017-10-04 11: 21: 23.222651, last: 0.06364501 
1, date: 2017-10-04 11: 21: 42.255728, last: 0.06365983 
1, date: 2017-10-04 11: 22: 02.589577, last: 0.06370500 

The algorithm for finding directions is not very complicated - if for a pair of currencies the highest bid and lower ask simultaneously increased relative to the previous values ​​- most likely the price will rise at the next moment, otherwise, on the contrary.

The first digit in this listing is the same attempt by the robot to predict where the price will go at the next moment, where 0 - the price will decrease, 1 - the price will increase. As a result of the work, the bot has a lot of such directions for each of more than 50 trading pairs.


Directions received. What's next?


Now we need to decide what the mask will look like from n consecutive directions, when we can confidently say that this point is the point after which the course will change and begin to grow in the opposite direction. After long testing and changing the direction masks, we settled on the option where such a point is preceded by 5 (-1 to allow for correction on the exchange) identical directions and 2 opposite. For example:


(1, 1, 1, 1, 1, 0, 0)

Such a list of destinations with a probability of 80% indicates that at that moment the exchange rate began to fall and should be sold.


At what price to exhibit?


Another important point was the issue of calculating the price at which you need to place an order. Here, all the same data from WS comes to our aid - data on current orders on the exchange. Knowing the entire “glass” of orders on the exchange, we sink in it until we reach the condition when the amount of currency that the bot wants to set is equal to the amount of tokens set in the orders.


Other settings


We decided to make such settings as the desired amount of currency and the pairs themselves with which we want to work with in the Web interface, so that in the course of work we could choose what to trade ourselves. We set the lifetime of orders for 5 minutes - we simply cancel orders older than this time.


What is the result?


After implementing the algorithm described above, we got the following:


Botwebinterface

The screenshot above shows the Web interface of the final result. The graph shows when the robot decided to place an order and at what price, when the order was canceled and for what reason. Various settings are located on the right side of the interface, from turning off the main currencies of the site to setting the desired number of tokens as a percentage of the total amount of funds.




The end result of our work can be found in our repository on GitHub .


Also popular now: