IV. Deploy a Poloniex trading bot
By: Sébastien Cararo
Tags: Trading, Machine Learning, Bitcoin, Cryptocurrency, Deep Learning

0 — Importing librairies and loading data

The very first step is to import the necessary librairies, and (re-)define the constants which we used along the construction of this trading bot. This includes the stoploss, the takeprofit, the period of candles, but also the pair which we trade — as referred to on the Poloniex API and website (e.g. USDT_BTC for Bitcoin trading) and the amount in dollar which we want to invest for each trade. Furthermore, we also set our threshold probability min_threshold from which we may follow our model during bullish predictions. By default it is set to 0.5 (i.e. we follow the model everytime it is bullish) but we can set a higher threshold to generate a more selective trading strategy (see article III — [1]). One should also pay attention to modifying the path on line 14 according to his directories structure.

We also import a custom library called Deployment_functions that will be described in section II of this article.

The initialization code is given below :

Figure 1: Initialization code for deployment

I — Initializing Poloniex API connection

Once we have imported the librairies, we can initialize the connection to the Poloniex API by inputing our API keys and secret. These hexadecimal codes are unique to each account, and allow the user to take trades directly from Python. More information regarding the API and how to get your keys can be found at [2]. With our program, you just need to set yours at lines 62 & 63 in Deployment_functions.py (next paragraph).

Furthermore, the code the initialize the connection is as simple as this :

Figure 2: Initialize Poloniex API private connection

II — Defining buy and sell functions, computing variables

As presented earlier, to lighten the trading script I decided to store all functions inside a custom library called Deployment_functions.py. This file contains previously introduced functions such as the computation of predictive variables (see article I — [3]). It also contains two very important functions, that are the buy_asset() and sell_asset() functions. These functions enables us to buy/sell a given amount at the best possible price available in the market, and are completed only if the entire amount is bought/sold. These functions are important to define well and clearly, in order to avoid pending orders which would falsen the real-life trading. The underlying idea is to make the code as performant as possible, in order to obtain real-life results that are as close as possible to the backtested results (see article III — [1]).

The code for these functions is given below, where as specified before you should change lines 62 & 63 :

Figure 3: Define trading functions

III — One trade at a time (1TAAT) trading bot

Now that we have built and defined all of our tools, time is now to create the trading loop. In our case (1TAAT), we simply repeat a sequential process over and over. We wait for the candle’s data to be delivered, then we apply these 3 tasks : Request the recent data, compute predictions by computing the variables and applying the model, then finally if our bullish criterion is reached (see article II [4] for how we define our min and max) we enter the trade and track it. When we enter into the trade, first we buy the predefined amount of the pair, then we wait until the price reaches either the stoploss or the takeprofit. When it finally reaches a limit, we sell our asset. And we repeat this process over and over.

The code was made to display some monitoring information in the console (Figure 4) and to store the characteristics of each transaction made (Figures 5 & 6). NB : The Poloniex website stores all trades’ information anyhow.

The console looks like this when a trade in progress, i.e. when we are waiting for the price to reach either the stoploss or the takeprofit:

Figure 4: Console when a trade is being monitored

Figure 5: Data stored during a Buy action (entry point of the trade)

Figure 6: Data stored during a Sell action (exit point of the trade)

The trading loop is quite refined, and it fits in 26 lines which makes it clear to understand :

Figure 7: Trading loop

IV — Conclusion

First we can recall that the entire project, along with the code, is available in a dedicated GitHub repository [5].

This project is a great accomplishment, as we managed to display how to build a profitable trading bot from scratch. The pipeline is easily adaptable and can fit various financial markets. Also, the required input data is limited, so that the access to historical data should not be an issue.

We showed that the pipeline was yielding robust and profitable trading strategies on backtested simulations (article III) and we exposed in this very last article how to implement the principle of a trading bot in a simple Python script.

We may also recall that the purpose of this series of articles was to make this notion accessible to everyone, and that obviously one can enhance the proposed pipeline by multiple means. In particular, one could think about adding some more predictive variables, increasing the number of layers in the Deep Learning architecture, or even ameliorate the implementation loop. It is also quite feasible to adapt the trading loop in order to take and monitor multiple trades (possibly over multiple markets) at the same time.

To give an overview of some possibilities, I can invite the interested reader to look at academic papers, especially MSc and PhD thesis. At an other level, I wrote a report concerning my construction of such advanced trading bot, available at [6].

Don’t hesitate to leave any feedback/questions/clap or to contact me for more information.

04/01/2021 : There a are a couple of bugs in the buy/sell functions. Looking forward to correct them asap.