Book a free call

Stock Price Prediction Web App with Flask and TensorFlow

A Flask web app can serve TensorFlow predictions for a stock's next-day price. Here is how to build the data pipeline, the model and the interface.

Hertiv engineering teamReviewed by a senior engineer
7 min read
Stock Price Prediction Web App with Flask and TensorFlow
On this page

Short answer

A stock price prediction web app collects past prices, trains a TensorFlow model on them, and serves the forecast through a Flask backend with a simple chart in the browser. Flask handles requests and data, TensorFlow makes the prediction, and the front end shows the result. The app is a tool for exploring patterns, not a guarantee of future prices.

What is a stock price prediction web app?

A stock price prediction web app is a browser tool that shows a forecast of a stock's future price, usually the next day's closing price, based on patterns in past prices. The user types a ticker symbol, and the app returns a predicted price with a chart of recent history.

The app has three parts. A data pipeline fetches historical prices and turns them into features a model can read. A TensorFlow model learns from those features and produces a forecast. A Flask backend serves the model and the data, while a small front end displays the chart and the prediction.

Most such apps predict direction or a price range rather than an exact value. Even a good model is often wrong, because prices react to news, earnings and events that no price history contains. The app is useful for learning and for testing ideas, not for deciding what to buy.

How does the prediction model work?

The prediction model works by learning how past price movements relate to the next price, then applying that pattern to the latest data. A common choice is a long short-term memory network, or LSTM, which is a type of neural network built for sequences such as daily prices.

Training feeds the model sliding windows of past days. For example, 60 days of closing prices, volumes and moving averages become one training example, and the 61st day's close is the target. After seeing thousands of such windows, the model learns which patterns tend to precede rises or falls.

The model is trained with TensorFlow, usually in a notebook or a script, and saved to a file. The Flask app loads that file once at startup and uses it to answer requests. Keeping training separate from serving makes the app faster and easier to update. For a plain introduction to the field, see what machine learning is.

What data does the app need?

The app needs clean, dated price data, and a few derived features that help the model see trends. Raw closing prices alone carry little signal, so most apps add moving averages, volume and volatility measures before training.

DataWhat it isWhy it helps
Daily pricesOpen, high, low, closeThe basic series the model learns from
VolumeShares traded each dayShows how strong a move was
Moving averages10-day and 50-day averagesSmooth noise and show the trend
VolatilityTypical daily price rangeTells the model how noisy the stock is
ReturnsDay-to-day percentage changeScales prices so stocks compare fairly

Free price history is available from public market-data providers and from exchange data files. Download it once a day after the market closes and store it in a small database or CSV files. Check for missing days, stock splits and bad rows before training, because a single wrong value can distort the model.

How do you build the Flask backend?

You build the Flask backend by creating one small service that loads the trained model, fetches the latest data, runs the prediction and returns the result as JSON. Flask is a good fit because the app is small and the framework stays out of the way.

  1. Set up the project. Create a virtual environment, install Flask and TensorFlow, and organise the code into data, model and app modules.
  2. Load the model once. Read the saved TensorFlow model when the app starts, not on every request, so predictions stay fast.
  3. Add a data route. Fetch the last 60 days of prices for the requested ticker and build the same features used in training.
  4. Add a predict route. Run the model on the features and return the forecast, the last close and the date as JSON.
  5. Cache results. Store each ticker's prediction for the day so repeated requests do not rerun the model.
  6. Handle errors. Return a clear message for unknown tickers, missing data or a model that failed to load.

Keep the prediction logic in its own module so the web code stays thin. That way the same model can serve a web page, an API or a scheduled report without changes. TensorFlow and PyTorch differ in how models are built and saved, which is worth knowing before choosing a stack; see TensorFlow vs PyTorch vs OpenCV.

How do you build the front end?

You build the front end as one page with a ticker input, a price chart and a prediction card, so a user can go from symbol to forecast in a few seconds. The page calls the Flask predict route and draws the result with a JavaScript chart library.

The chart should show the last 60 days of actual closes plus the predicted next close, marked clearly as a forecast. A line chart works well, with the forecast drawn as a dashed line so nobody mistakes it for real data. Add the percentage change from the last close, and the date the prediction applies to.

Keep the interface honest. Show the model's recent accuracy, such as how often it got the direction right over the last 60 days, next to the prediction. A forecast without an accuracy note looks far more certain than it is, and users deserve to see the limits before they act on it.

Example (hypothetical): a prediction for one stock

Example (hypothetical): a team builds the app for a single stock, XYZ, which closed at $120.00 on Friday. The model was trained on five years of daily data, about 1,260 trading days, and the Flask app serves a prediction each morning before the market opens.

On Monday the app predicts a Tuesday close of $121.80, which is 1.5% above the last close. A user holding 100 shares would see a gain of $180 if the prediction came true, before fees. The app also shows that over the last 60 test days the model got the direction right 34 times, or about 57% of the time.

On Tuesday the stock actually closes at $119.40, so the prediction was wrong in both direction and size. The team logs the miss, retrains the model monthly, and treats the forecast as one input among many. The example shows why the app is a research tool, not a trading signal.

What are the common mistakes?

The most common mistakes with stock prediction apps are training on the wrong data, trusting the forecast too much, and building a slow or fragile service.

  • Training on future data. If the training set includes information from after the date being predicted, the model looks accurate in tests and fails in real use.
  • Using raw prices only. A model fed only closing prices learns little; add returns, volume and moving averages.
  • Ignoring splits and dividends. Unadjusted prices create fake jumps that the model treats as real moves.
  • Showing predictions without accuracy. A forecast with no backtest result hides how often the model is wrong.
  • Reloading the model per request. Loading a TensorFlow model on every request makes the app slow; load it once at startup.
  • No error handling. Unknown tickers and missing data should return a clear message, not a crash page.

Should you trust the predictions?

You should treat the predictions as a starting point for research, not as a reason to buy or sell. Stock prices are driven by earnings, news, interest rates and crowd behaviour, and most of that is invisible to a model trained only on price history.

Test the model honestly before trusting it. Hold out the last three months of data, predict each day in turn, and count how often the direction was right and how large the average error was. If the model cannot beat a simple rule, such as predicting that tomorrow's price equals today's, it adds nothing.

Even a model that passes backtests can fail when markets change, because the patterns it learned may belong to a calm period. Re-train regularly, watch for drift, and keep the app's disclaimers visible. If you are building the product for others, a partner for web app development can help with the data pipeline and the interface.

Key takeaways

  • A stock prediction web app pairs a TensorFlow model with a Flask backend and a chart front end
  • Train on sliding windows of past prices plus volume, moving averages and returns
  • Load the model once at startup and cache each day's prediction
  • Show backtest accuracy next to every forecast so users see the limits
  • Never present a prediction as a buy or sell recommendation

Frequently asked questions

Do I need deep learning to predict stock prices?

No. Simple models such as linear regression or a moving-average rule are a good first step, and they are easier to test. Deep learning helps only when you have enough clean data and a clear way to measure whether it beats the simple baseline.

Which is better for this app, Flask or Django?

Flask fits a small prediction service well because it is light and easy to shape around one model. Django fits better when the app grows into user accounts, admin panels and many database tables. Most single-purpose prediction apps start with Flask.

How much historical data should I train on?

Three to five years of daily data is a common starting point, which is roughly 750 to 1,260 trading days. Less data makes the model fragile, while much older data may describe a market that no longer behaves the same way.

Can the app predict prices in real time?

It can update predictions during the day, but the model is trained on daily closes, so intraday forecasts need different data and a different model. Most apps refresh once a day after the market closes, which matches how the model was trained.

What does TensorFlow add over plain Python code?

TensorFlow handles training, saving and loading of neural networks such as LSTMs, which would be slow and error-prone to write by hand. For simple models it adds little, so start simple and move to TensorFlow when the model needs it.

Is it legal to publish stock predictions?

Publishing a forecast is generally allowed, but presenting it as investment advice can trigger licensing rules in many countries. Label predictions as research, show the backtest, and add a clear disclaimer that the app is not financial advice.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: software products in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a data-driven web app with a prediction model? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Web App Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call