📜 ⬆️ ⬇️

Crypto Trading Automation with Django and Celery

In the light of the rapid development of cryptoindustry and cryptotrading in particular, our team, within the framework of the experiment, decided to create a trading robot, whose main goal is to trade on the poloniex crypto site . In this article I will try to talk about all the difficulties encountered while writing 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 the choice of development language.

Why django?
At first, we wrote a robot working in the terminal. After it became necessary to visually observe the work of the robot, add several accounts for trading, configure the configuration - for all this, a certain Web-interface was needed.


Beginning of the story


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


Bot home page

The algorithm of the robot


Initially, the robot polled the exchange once every few seconds, made some simple calculations using the Bollinger bands ( link ) calculation and placed orders if it thought 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 questioned the exchange for changes 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 that we used, poloniex, had such functionality. This functionality is WAMP protocol . He allowed to subscribe to the necessary channel on the exchange, and receive timely messages about changes in rates.


Generally, by subscribing to Web Socket poloniex, we get not only information about exchange rates. In addition, we receive information about the so-called "glasses" bids
& asks, about orders and even about messages in a local chat, but that’s another story.

After we received the most current information about the current situation on the stock exchange, it was necessary to figure out 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 determine the right moment for buying and selling. In our script, we decided to use the mechanism of directions, when the algorithm tries to predict the next step, in which the rate of the necessary currency will go. The following is an extract from the logs of the bot, where you can see the "directions" for a pair of BTC - ETH:


 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 itself of finding directions is not very difficult - 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 next time, otherwise it will be the other way around.

The first figure in this listing is the robot’s attempt to predict where the price will go next time, where 0 means the price will go down, 1 means the price will increase. As a result, the bot has a lot of such directions for each of the 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 say with confidence that this point is the point after which the course will change and will start to grow in the opposite direction. After long testing and changing the directional masks, we stopped at the variant where such a point is preceded by 5 (-1 for the assumption of correction on the exchange) of the same directions and 2 opposite ones. For example:


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

Such a list of directions 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 the order should be placed. This is where all the same data from WS comes in - the data on current orders on the exchange. Knowing the whole "glass" of orders on the stock exchange, we descend in it until we reach the condition when the amount of currency that the bot wants to set is equal to the sum of the number of tokens placed in the orders.


Other settings


Settings such as the desired amount of currency and the pairs themselves, with which we want to work, we decided to bring the interface to the Web, so that during the work we could choose what to trade. We set the lifetime of the order to 5 minutes - orders that are older than this time are simply canceled.


What is the result?


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


Botwebinterface
')

The screenshot above shows the final result Web interface. The chart shows when the robot decided to place an order and at what price, when the order was canceled and for what reason. In the right part of the interface, there are various settings, from disabling 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 .

Source: https://habr.com/ru/post/353732/


All Articles