Original article was published by Adam Tibi on Deep Learning on Medium
To expose our model via RESTful API, we need to host it (wrap it) with a web server. Since we are using Python for the model, one popular non-production web server for Python is Flask.
You can use Windows or Linux, Flask works on both, but I would recommend using the same OS used for training, but not necessarily the same instance. Setting up Flask from your console:
conda install flask
pip install flask
The full source code setting up the system is on my GitHub page for setting up the server.
What is interesting from the source code is the following:
@app.route('/predict/<string:ticker>/<int:batch_size>/<int:window_size>/<int:ma_periods>/<float:abs_pips>/<int:pred_size>/<string:instance>/<string:series>', methods=['GET'])def predict(ticker, batch_size, window_size, ma_periods, abs_pips, pred_size, instance, series):
This allows Flask to accept a URL like this:
The server might host multiple models, so to differentiate them, I made them identifiable via ticker, batch size, window_size and moving average periods, so a model might be called: gbpusd-32-256–14.
ticker: The official name of the instrument, GBPUSD in our case.
batch_size and window_size: parameter required by the model.
ma_periods: simple moving average smoothing periods.
abs_pips: the limit when your prediction will stop and the prediction results will return. So, if it is 0.0008 and your current price for GBPUSD is 1.3000 then the model will stop predicting if it reaches 1.3008 or 1.2992 .
prediction_size: how much forward units max you want to predict and before you hit the abs_pips. Take the previous example, if your prediction_size is 30 units (minutes in our case) and you hit 1.2992 after 5 units, prediction will not continue and will return “Sell”, but if 30 units are reached without hitting the limits, the system will return “No Action”.
instance: a reserved integer for future use. For now, supply any integer.
The last section, series, of the URL is composed of a comma-separated prices:
length = window_size + moving_average_periods = 256 + 14 = 270
Your web server can run from the console:
To test if your server setup is working, try the example URL from the source code.
We have a prediction server. To use this prediction server, a client needs to supply a URL in the previous format and then get a prediction.
I used cTrader, a commercial manual and algo trading platform, as a client. The full source code and setting up the system is on my GitHub page for setting up the client. The language used for the bot is C#.
This is the bit where our code requests a prediction from our server:
The code above prepares a URL made of the inputs that our server requires. Then it passes it to the server and gets back a “1”, “-1” and “0” which respectively mean Buy, Sell and I don’t know.
The code above is executed on every unit pass, hence the name OnBar. In our example, it is using 1 minute as a unit.
I ran this bot in backtesting between 24/08/2020 and 30/08/2020 with £1000 capital, having the client server setup above and it made a small profit of £49.