Web application for predicting Stock Prices: Python, Flask & TensorFlow | Part 1

This tutorial guides you through creating a stock price predictor using TensorFlow and Flask, from setting up your environment to deploying your Flask web application. Learn to integrate TensorFlow with Alpha Vantage for data handling and build a user-friendly interface for real-time stock predictions. Ideal for AI enthusiasts and budding developers.
Santiago Sanchez
May 1, 2024
illustration for outsourcing

In this article we'll learn how to create an Stock price predictor PoC using TensorFlow & Alpha Vantage, integrating it with a Flask web application for User interaction.

1. Introduction

In this tutorial, you will learn how to create a web application using Python, Flask, and TensorFlow that can predict future stock prices using a trained machine learning model with data from Alpha Vantage. The main objective is to build an intelligent web application that allows users to input a stock symbol and receive a prediction of its future price based on historical data.

This tutorial is ideal for engineering students, software engineers, and AI enthusiasts who have basic knowledge of Python and are interested in learning how to combine web development with machine learning techniques. While no prior knowledge of Flask, TensorFlow, or finance is required, as all necessary concepts will be explained in detail, familiarity with Python programming and a willingness to learn new frameworks and libraries will be beneficial.

2. Setting up environment

To get started with the project, you'll need to have Python and several libraries installed on your system. Follow these steps to set up the development environment:

  1. ‍Install Python: Make sure you have Python installed on your machine. You can download the latest version of Python from the official website: https://www.python.org/downloads/‍
  2. Creating virtual environment: To create a new virtual environment (Assuming you already have virtualenv installed), navigate to your project directory and run the following command:
  1. This command will create a new virtual environment named env in your project directory. To activate the virtual environment, run the following command:
  • On Windows:
  • On macOS or Linux:
  1. ‍Install Required Libraries: This project relies on several Python libraries, including Flask for web development, TensorFlow for machine learning, Pandas for data manipulation, and Numpy for numerical operations. You can install these libraries using pip, Python's package installer. Open your terminal or command prompt and run the following command:
  1. ‍This command will install the latest versions of Flask, TensorFlow, Pandas, Numpy and Alpha Vantage.

3. Data Collection

One of the key components of this project is obtaining reliable historical stock price data, which will be used to train our machine learning model. In this section, we'll explore how to retrieve this data using a financial data API and how to preprocess it for use in our AI model.

There are various APIs available that provide access to historical stock price data. For this tutorial, we'll use the Alpha Vantage API, which offers a free tier with limited access to stock data. Here's how you can use Alpha Vantage to retrieve historical stock prices:

  1. ‍Sign up for an API key Visit the Alpha Vantage website (https://www.alphavantage.co/) and sign up for a free API key.‍
  2. Install the Alpha Vantage Python library In your terminal or command prompt, run the following command to install the Alpha Vantage Python library:
  1. ‍Fetch historical stock data by Creating a new Python file (e.g., data_collection.py) and add the following code:
  1. Replace 'YOUR_API_KEY' with the API key you obtained from Alpha Vantage, and 'AAPL' with the stock symbol you want to retrieve data for. When you run this code, it will fetch and print the historical daily stock prices for the specified stock symbol.

The data retrieved from the Alpha Vantage API will be in a Pandas DataFrame format, which makes it easy to manipulate and clean the data. Here are some common steps you may need to perform:

  1. Handle missing data: Check for and handle any missing values in the data using Pandas' built-in functions like dropna() or fillna().
  2. Convert data types: Ensure that the data types of the columns are correct (e.g., convert the date column to a datetime format).
  3. Feature engineering: Create new features or transformations of the existing data that may be useful for training the machine learning model (e.g., calculate moving averages, technical indicators, etc.).
  4. Split the data: Split data into training and testing sets to evaluate the performance of your machine learning model.

In this example, we convert the date column to datetime format, set it as the index, drop any rows with missing values, create a new feature 'daily_return' representing the daily percentage change in closing prices, and split the data into training and testing sets based on a specified train_end_date.

4. Building AI model with TensorFlow

In this section, we'll dive into the process of building and training a neural network model using TensorFlow, one of the most popular open-source libraries for machine learning. This model will be used to predict future stock prices based on the historical data we collected and preprocessed in the previous step. Let's begin by some basic definitions:

  • Neural networks are a type of machine learning model inspired by the structure and function of the human brain. They are particularly well-suited for tasks such as pattern recognition, image classification, and time series prediction, which is our focus in this project.
  • Time series prediction involves using historical data to forecast future values. Neural networks can learn from the patterns and trends present in the historical data, and then make predictions about future values based on that learned knowledge.

Preparing data for model training

Before we can train our neural network model, we need to prepare the data in a format suitable for training. This typically involves splitting the data into training and testing sets, scaling the features, and creating input sequences and target outputs.

In this example, we split the data into training and testing sets, create input sequences and target outputs using a function create_sequences, and scale the data using the MinMaxScaler from scikit-learn.

Building the neural network model with Tensorflow

With the data prepared, we can now build our neural network model using TensorFlow. Here's an example of how we can define and compile a simple recurrent neural network (RNN) model:

In this example, we define a sequential model with an LSTM (Long Short-Term Memory) layer and a Dense output layer. The LSTM layer is responsible for learning the patterns and dependencies in the input sequences, while the Dense layer outputs a single value, which is the predicted stock price.

Model training & evaluation

With the model defined, we can now train it on the prepared training data:

This code trains the model for 50 epochs, with a batch size of 32, and uses 10% of the training data for validation during training.

After training, we can evaluate the model's performance on the test data:

In this example, we make predictions on the test data, inverse scale the predictions and true values, and calculate evaluation metrics such as mean squared error (MSE) and mean absolute error (MAE) to assess the model's performance.

5. Creating the web application with Flask

Now that we have a trained AI model for predicting stock prices, it's time to create a web application using Flask, a lightweight and popular Python web framework. This web application will serve as an interface for users to interact with our model and visualize the predictions.

Basic structure of flask application

Here's a basic example of a Flask application structure:

In this example, we import the Flask class and the render_template function from the Flask library. We create an instance of the Flask class and assign it to the variable app. We then define a route for the root URL (/) using the @app.route decorator, and associate the index function to handle requests to this route.

Inside the index function, we use the render_template function to render the index.html template. Finally, we include a conditional statement to run the Flask application in debug mode when the script is executed directly.

Creating routes & views

For our stock prediction application, we'll need to create additional routes and views to handle user input and display the prediction results. Here's an example of how we can define a route to handle a form submission:

In this example, we define a route for the root URL (/) that can handle both GET and POST requests using the methods argument. Inside the index function, we check if the request method is POST, which means the user has submitted the form. We then retrieve the stock symbol from the form data using request.form['stock_symbol'].

Next, we call a function predict_stock_price (which we'll define later) to make a prediction using our trained model. Finally, we render the result.html template and pass the prediction as a variable.

HTML Template rendering

Flask allows us to create HTML templates using the Jinja2 templating engine. Templates are stored in a directory called templates (you'll need to create this directory if it doesn't exist). Here's an example of what the index.html template might look like:

This super simple template contains a form with an input field for the user to enter a stock symbol and a submit button. When the user submits the form, Flask will handle the POST request and render the result.html template with the prediction.

Integration of the trained AI model into the application

To integrate our trained AI model into the Flask application, we need to create a function that takes a stock symbol as input, preprocesses the data, and uses the trained model to make a prediction. Here's an example of how we can implement this:

In this example, we import functions from a hypothetical model module that loads the trained model (load_model) and preprocesses the data (preprocess_data). We define a function predict_stock_price that takes a stock symbol as input, fetches the historical data for that symbol (using a function fetch_stock_data that we'll define later), preprocesses the data using the preprocess_data function, makes a prediction using the trained model, and post-processes the prediction (e.g., inverse scaling) using a function postprocess_prediction (which we'll also define later).

6. UI Implementation

A well-designed user interface (UI) is crucial for creating an intuitive and engaging experience for users interacting with our stock price prediction web application. In this section, we'll focus on implementing an attractive and user-friendly UI using HTML, CSS, and JavaScript.

User Interface Design with HTML, CSS & JavaScript

The UI design process typically starts with creating a mockup or wireframe to visualize the layout and structure of the web pages. Once the design is finalized, we can implement it using HTML for the content structure, CSS for styling and layout, and JavaScript for adding interactivity and dynamic behavior.

Here's an example of how we can structure the HTML for the main page of our application:

In this HTML structure, we have a header with a navigation menu, a main content area with a heading, a form for entering the stock symbol, a div for displaying the prediction result, and a footer. We also include links to external CSS and JavaScript files using Flask's url_for function to correctly reference static files.

For the CSS styling, we can create a styles.css file in a static/css directory and include rules for layout, typography, colors, and other visual aspects of the UI.

JavaScript can be used to add interactivity and handle events, such as form submissions or AJAX requests to fetch prediction results without refreshing the page. We can create a app.js file in a static/js directory and include JavaScript code to enhance the user experience.

Creating a form to enter the symbol of the desired action

The form for entering the stock symbol is already present in the HTML structure shown above. Here's an example of how we can style the form using CSS:

This CSS code styles the form elements, including the label, input field, and submit button. It centers the form elements, adds spacing and borders, and styles the submit button with a green background color and white text.

Viewing prediction results

To display the prediction results, we can use the prediction-result div in the HTML structure. Here's an example of how we can update this div using JavaScript:

This code adds an event listener to a form's submit event. When the form is submitted, it prevents the default submission behavior, retrieves the stock symbol value from an input field, and sends an AJAX POST request to the Flask root route (/) with the stock symbol as form data.

If the response is successful, it parses the JSON response and updates an element with the predicted stock price. If there's an error, it logs the error message to the console. The corrected code aligns with the Flask route defined in the Python code.

7. Testing & deployment recommendations

After developing the stock price prediction web application, it's essential to test it thoroughly to ensure it functions as expected and then deploy it to a web server for public access.

Local testing of the web application

Before deploying the application, it's crucial to perform comprehensive testing in a local development environment. This helps identify and resolve any issues or bugs before making the application available to users.

Here are some steps you can follow for local testing:

  1. ‍Set up a local environment: Flask provides a built-in development server that you can use for local testing. In your Flask application file, you can start the development server by running the following command:

This will start the Flask development server and automatically reload the application whenever you make changes to the code.

  1. ‍Test different scenarios: Test various scenarios and edge cases to ensure your application functions correctly. For example:
  • Test with valid and invalid stock symbols
  • Test with different date ranges for historical data
  • Test with different model configurations and parameters
  • Test user interface for responsiveness and accesibility‍
  1. ‍Use debugging tools: Flask and other python libraries provide various debugging tools and techniques that can help with you indentify and fix issues. For example, you can use the built-in Flask debugger by setting debug=True in the app.run() function or use the external debugging tool like pdb (Python Debugger) or an IDE with debugging capabilities.‍
  2. Write unit tests: Consider writing unit tests for critical components of your application, such as the data preprocessing functions, model prediction functions and Flask route handlers. Unit tests help ensure that individual components work as expected and make it easier to identify and fix issues.‍
  3. Perform load testing: If you expect a significant amount of traffic to the web application, consider performing load testing to ensure your application can handle the expected load without performance issues.‍
  4. Test on diferrent browsers and devices: Test the web application's user interface on different browsers and devices (desktop, mobile, tablets) to ensure it renders and functions correctly across different platforms.

Deploy to a web server (e.g. PythonAnywhere)

There are several options for deploying your Flask web application to a web server, including cloud platforms like PythonAnywhere, Heroku, or AWS Elastic Beanstalk. In this section, we'll focus on deploying to PythonAnywhere, a cloud platform that provides a simple and beginner-friendly environment for hosting Python applications.

Here are the steps to deploy your application to PythonAnywhere:

  1. ‍Sign up for a PythonAnywhere account: Visit the Python Anywhere website (https://www.pythonanywhere.com) and sign up for a new account. You can start with a free "Beginner" account for testing purposes.‍
  2. Create a new web app: In the PythonAnywhere dashboard, create a new web app by navigating to the "Web" tab and clicking the "Add a new web app" button. Choose the "Manual configuration" option and select the Python version you used for the application.‍
  3. Upload your application files: You can upload your application files to PythonAnywhere using the built-in file editor ir by connection to the PythonAnywhere server using a tool like FileZilla or WinSCP. Make sure to upload all the neccessary files, including your Flask application file, HTML templates, static files (CSS, Javascript), and any other required files or directories.‍
  4. Configure the web app: In the PythonAnywhere dashboard, navigate to the "Web" tab and click on the web app you created earlier. Scroll down to the "Code" section and endter the path to your Flask application file (e.g., /home/santiagossaa/myapp/app.py). You may also need to configure the virtual enviroment or install any required packages using the PythonAnywhere console.‍
  5. Set up a custom domain (optional): If you want to use a custom domain for your application instead of the default PythonAnywhere URL, you can configure a custom domain by following the instructions in the PythonAnywhere documentation.‍
  6. Reload the web app: After making any cconfiguration changes, reload your web app by clicking the "Reload" button in the PythonAnywhere dashboard.‍
  7. Test your deployed app: Once the reload process is complete, you can access your deployed application by visiting the URL provided by PythonAnywhere (e.g., https://your-username.pythonanywhere.com/)

8. Conclusions & next steps

Throughout this tutorial, you learned the ropes of creating a web app using Python, Flask, and TensorFlow that can predict stock prices with a trained machine learning model. You picked up some solid skills in collecting data, getting it ready for analysis, building the model, developing for the web, and deploying the final product. While the stock price prediction web application you've built is functional and provides a solid foundation, there are several potential improvements and extensions you could consider:

  1. ‍Improve the model: Explore more advanced neural network architectures or ensemble models to improve prediction accuracy. You could also incorporate additional features or technical indicators into the model.‍
  2. Real-time data integration: Instead of relying solely on historical data, integrate real-time stock data feeds to make predictions based on the latest market information.‍
  3. User Authentication and Personalization: Implement user authentication and personalization features, allowing users to create accounts, save their preferences and track their prediction history.‍
  4. Portfolio Management: Extend the application to enable users to create and manage their stock portfolios, providing insights and reccomendations based on their holdings.‍
  5. Sentiment Analysis: Incorporate sentiment analysis techniques to analyze news, social media, and other textual data sources and use this information to improve stock price predictions.‍
  6. Visualizations and reporting: Enhance the user interface by adding interactive visualizations and reporting tools to better understand and analyze prediction results.

If you're interested in further expanding your knowledge in this domain, here are some additional resources you might find helpful:

  • Bookssome text
    • "Python for Finance" by Yves Hilpisch
    • "Deep learning with Python" by FranΓ§ois Chollet
    • "Flask web Development" by Miguel Grinberg
  • Online coursessome text
    • Coursera: "Machine Learning for Trading Specialization" by Jack Farmer
    • Udemy: "The Complete Python Developer" by Andrei Neagoie
    • edX: "Introduction to Computational finance and Financial Econometrics" by Eric Zivot


No items found.