Flask makes it so easy to share your predictions (drl4t-06)

Xiaoguang Li
3 min readApr 1, 2023

Until now, we have a trained deep reinforcement learning trading model. Now it is time to try to use it to predict tomorrow’s stock market.

Predict Tomorrow

The first step is to download the trading data:

from drl4t_data import download

train_data, test_data = download('nyse.csv')

The second step is to load the trained trading model:

from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3 import DQN

model = DQN.load('nyse_dqn_model.pt')

In the third step, the trading data of each stock is provided to the trading model to learn and make prediction against the data of the latest trading day. The results are saved in a file called nyse_dqn_pred.csv.

import pandas as pd
from drl4t_env import DRL4TEnv, Actions

pred = pd.DataFrame(columns=['Symbol', 'Predict'])
for symbol, data in test_data.items():
env = DRL4TEnv({ symbol: data })
model.set_env(DummyVecEnv([lambda: env]))

obs = env.reset()
done = False

while(not done):
action, _ = model.predict(obs)
obs, _, done, info = env.step(action)

action, _ = model.predict(obs)
if action != Actions.Hold.value:
info['Predict'] = Actions(action).name
pred = pd.concat([pred, pd.DataFrame(info, index=[info['Date']])])

pred[['Symbol', 'Predict']].to_csv('nyse_dqn_pred.csv')

Now it’s time to think about what you can do with these predictions. I have some suggestions:

Don’t do:

  • Buy the stocks suggested by the trading model

Do:

  • Buy the stocks suggested by the trading model on the stock market simulator
  • Wait and observe the stock prices tomorrow
  • Share the trading model’s predictions

Share the Success

Share trading model predictions, you can email them to your friends, post them to social media, or even create an AI stock price prediction website. Thanks to Flask, it’s also very simple!

Flask is a lightweight web application framework for Python. It is designed to be easy to use and flexible, allowing developers to quickly build web applications and APIs with minimal code. Flask is popular for its simplicity, modularity, and extensibility, making it an excellent choice for small to medium-sized projects.

In order to use Flask, it first needs to be installed.

!pip install flask

Creating a website with Flask requires three steps. First, needs to create a Flask app object.

from flask import Flask

app = Flask(__name__)

Then needs to define a method and setup the route. This method will read the predictions of the trading model and convert them into HTML format.

from flask import render_template
import pandas as pd

@app.route('/', methods=['GET'])
def highlight():
pred = pd.read_csv('nyse_dqn_pred.csv')
table_html=pred[['Symbol', 'Predict']].to_html()
return render_template('index.html', table_html=table_html)

An HTML template (index.html) is used to do this conversion. This template needs to be saved in the templates folder.

<!doctype html>
<html>
<head>
<title>DRL4T</title>
</head>
<body>
<h1>Prediction</h1>
{{ table_html | safe }}
</body>
</html>

The last step is to run this Flask app object.

app.run(host='localhost', port=5000)

For the purpose of demo, the web server is set to localhost. Open http://localhost:5000 in browser, a page as shown below will show the predictions of the trading model.

So far, I have used six blog posts to show how to create a prototype of a deep reinforcement learning model for stock trading. All the necessary scripts and sample data are available on GitHub for reference:

https://github.com/li-xiao-guang/drl4t

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Xiaoguang Li
Xiaoguang Li

Written by Xiaoguang Li

Master of Science in Computational Data Analytics from Georgia Tech, Senior IT Consultant at Morgan Stanley

No responses yet

Write a response