Price Predictions using Stock Tick Data in Python

Explore how to leverage Python with Facebook Prophet and SARIMA models for precise stock price forecasting and enhance your trading strategies.

EODHD APIs
6 min readMay 2, 2024

EODHD APIs has recently included US Stock Tick Data in their services. So, what is tick data? It records every single transaction of a stock, detailing the price, volume, and exact time of each trade. This data is crucial for high-frequency traders and institutions that rely on immediate reactions in the market, where decisions are made in fractions of a second.

Tick data offers insights into market depth by showing the bid and ask prices along with the quantities available for a stock at any moment. This is critical for algorithmic trading strategies, which rely on rapid, automated trading decisions based on live market conditions. Tick data also assists in risk management by providing detailed information about price movements and trade executions.

We’ve used the demo API key for testing, which only works for the Apple Inc. (AAPL.US) ticker. We’re also limiting the response to two entries to make it easier to read. The API allows a maximum of 10,000 records per request, so the date range selected can’t be too wide.

According to the documentation provided, the keys mentioned above correspond to the following fields:

  • ex — the exchange on which the ticker is listed
  • mkt — the market where the trade took place
  • price — the price at which the transaction occurred
  • seq — the sequence number of the trade
  • shares — the number of shares involved in the transaction
  • sl — the sales condition
  • ts — the timestamp of the trade
  • sub_mkt — the sub-market where the trade occurred

Stock tick data is somewhat analogous to a Blockchain ledger as it records transactions or events in detail. Each “tick” represents a single trade, capturing all essential information about that trade.

The API response, when converted into a Pandas DataFrame, would appear as follows:

Stock tick data is a type of time-series data, which allows us to apply sophisticated prediction and forecasting methods. Techniques like Facebook Prophet or the Seasonal ARIMA (AutoRegressive Integrated Moving Average) model are particularly suited for analyzing this kind of data. These tools can help us forecast future stock prices based on patterns observed in the historical tick data.

Facebook Prophet

If you’re not familiar with Facebook Prophet, it’s a powerful forecasting tool that utilizes a Python library to handle time-series predictions effectively at scale.

To begin using Prophet, ensure that you have the correct Python library installed. Interestingly, the library is named “prophet” rather than “fbprophet” as one might expect. It depends on “pystan”, which isn’t automatically installed with Prophet.

% python3 -m pip install pystan prophet -U

Facebook Prophet requires just two inputs: the time series as “ds” (datestamp) and the prediction variable as “y”. Assuming the dataframe we are working with is named “df,” and to align with Prophet’s requirements, we need to rename the columns accordingly:

df.rename(columns={'ts': 'ds', 'price': 'y'}, inplace=True)

The process to set up and run the model is straightforward:

model = Prophet()
model.fit(df)

future = model.make_future_dataframe(periods=10, freq="S")
forecast = model.predict(future)
forecasted_prices = forecast[["ds", "yhat", "yhat_lower", "yhat_upper"]].tail(10)

print(forecasted_prices)

In the code snippet above, the frequency “freq” is set to “S” (seconds), because our time series data is in seconds. If you’re working with different time units, adjust this parameter accordingly.

The output columns “yhat”, “yhat_lower”, and “yhat_upper” represent the predicted value and the lower and upper bounds of our confidence interval, respectively.

While Facebook Prophet is highly effective for making forecasts on large datasets, installing it can be somewhat tricky. It isn’t immediately apparent that “pystan” is also required, and you might face dependency issues. If you encounter installation problems, consider installing these packages first: Cython, numpy, pandas, matplotlib, and setuptools.

Seasonal ARIMA Model

The Seasonal Autoregressive Integrated Moving Average (SARIMA) model enhances the ARIMA model by incorporating seasonal components, making it a powerful tool for handling seasonal variations in time series data. You can find more information about it here.

SARIMA tends to be more complex than Facebook Prophet due to its configurability and detailed parameter settings.

We have extensively tested SARIMA in cryptocurrency trading bots and found it impressively accurate for short-term predictions, particularly for projecting the next 3–10 data points.

To begin using SARIMA, you’ll first need to install the required Python libraries, `matplotlib` and `statsmodels`:

% python3 -m pip install matplotlib statsmodels

Here’s how you might set up and use a SARIMA model in Python:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.statespace.sarimax import SARIMAX

df.set_index("ts", inplace=True)
model = SARIMAX(df["price"], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
results = model.fit(disp=False)

forecasts = results.get_forecast(steps=10)
mean_forecast = forecasts.predicted_mean
conf_int = forecasts.conf_int()

print(mean_forecast)

In this setup:

- You don’t need to rename columns to “ds” and “y” as required by Prophet; instead, you use the existing “ts” and “price”.

- It’s important to set the dataframe’s index to the timestamp for the time series analysis.

Having a substantial dataset is beneficial for modeling, but forecasting too far into the future can lead to unreliable results. Our experiences, especially in the volatile crypto market, highlight this point, although SARIMA might perform better with more stable financial data like traditional stocks.

It’s worth noting that unlike Facebook Prophet, which also predicts future timestamps, SARIMA strictly forecasts the next ’n’ values — in this case, 10. This distinction can influence the choice between these models depending on whether specific future times need predicting or just the future values.

Conclusion

In summary, this article has introduced two powerful time-series forecasting methods: Facebook Prophet and SARIMA, each useful for predicting stock prices with Python. Facebook Prophet is user-friendly and great for beginners, while SARIMA offers precise control, ideal for handling complex seasonal patterns. Both tools can significantly enhance trading strategies by providing detailed and reliable forecasts, helping to make informed decisions in the financial markets. Remember, the effectiveness of these models can vary with market conditions and data quality, so continuous evaluation and adjustment are key to maintaining accuracy.

Please note that this article is for informational purposes only and should not be taken as financial advice. We do not bear responsibility for any trading decisions made based on the content of this article. Readers are advised to conduct their research or consult with a qualified financial professional before making any investment decisions.

For those eager to delve deeper into such insightful articles and broaden their understanding of different strategies in financial markets, we invite you to follow our account and subscribe for email notifications.

We publish 2 pieces per week!

Stay tuned for more valuable articles that aim to enhance your data science skills and market analysis capabilities.

--

--

EODHD APIs

eodhd.com — stock market fundamental and historical prices API for stocks, ETFs, mutual funds and bonds all over the world.