post thumbnail

InfluxDB Stock Data with Python and Time-Series Queries

Learn how to build a Python web crawler to collect U.S. stock data, store it in InfluxDB, and analyze trends. Covers installation, data retrieval, database writing, querying, and visualization. Ideal for real-time financial analytics, time-series data management, and performance monitoring with Grafana.

2025-10-28

InfluxDB stock data fits naturally into time-series systems because stock prices and trading volumes change continuously over time. Developers often choose InfluxDB when they need to store and analyze metrics such as monitoring data, sensor readings, or application performance indicators. Stock data follows the same pattern.

In this tutorial, we use a Python web crawler to retrieve stock data, write it into InfluxDB, and query it efficiently for analysis.


Why InfluxDB Is Suitable for Stock Time-Series Data

CoInfluxDB focuses on timestamped data. Unlike traditional relational databases, it optimizes storage and queries for time-based access patterns. As a result, it handles sequential stock records efficiently.

Developers commonly apply InfluxDB to scenarios such as:

Stock prices share these characteristics. Therefore, teams often rely on InfluxDB to store and analyze stock market data.


InfluxDB vs MySQL for Time-Series Workloads

ScenarioInfluxDBMySQL
High-frequency writesDesigned for concurrent writesRequires sharding or buffering
Time-window aggregationBuilt-in time functionsComplex SQL queries
Long-term historical dataCompression-friendlyStorage grows quickly
Real-time monitoringTime-series nativeExternal tools required

Install InfluxDB on Ubuntu

InfluxDB supports Linux, macOS, and Windows. In this example, Ubuntu is used.

wget https://dl.influxdata.com/influxdb/releases/influxdb_1.8.10_amd64.deb
sudo dpkg -i influxdb_1.8.10_amd64.deb
sudo systemctl start influxdb

Verify installation:

influx -version

Create a Database and Connect

CREATE DATABASE db_stock;
SHOW DATABASES;

InfluxDB also exposes an HTTP API on port 8086, which allows database operations through REST requests.

At the same time, measurements, tags, and fields are created dynamically when data is written—no schema is required in advance.


Python Web Crawler to Fetch Stock Data

We retrieve U.S. stock data from a public financial source:

Using Python, the crawler fetches historical data and converts it into a DataFrame.

For example, the following code retrieves NVIDIA (NVDA) stock data:

def fetch_stock_data(ticker):
    df = stock_us_daily(symbol=ticker)
    print(df.tail())
    return df

fetch_stock_data("NVDA")

Write Stock Data into InfluxDB

Once the data is collected, it is written to InfluxDB as time-series points.

point = {
    "measurement": "stock_data",
    "time": row["date"],
    "tags": {
        "ticker": "NVDA"
    },
    "fields": {
        "open": row["open"],
        "close": row["close"],
        "high": row["high"],
        "low": row["low"],
        "volume": row["volume"]
    }
}

Here, tags are used for classification (such as stock symbol), while fields store numeric values.


Tags vs Fields Explained Simply

Although both store information, tags and fields serve different purposes:

FeatureTagField
Data typeString onlyNumeric, string, boolean
IndexedYesNo
PurposeFiltering & groupingActual values
Query speedFasterSlower

Therefore, stock symbols are ideal tags, while prices and volume belong in fields.


Query Data from InfluxDB

SELECT * FROM stock_data WHERE ticker = 'NVDA' ORDER BY time DESC;

The syntax is very similar to SQL, which makes InfluxDB easy to learn for developers with MySQL experience.


Calculate Moving Average with Python

After querying the data, Python can be used to compute indicators:

df['ma5'] = df['close'].rolling(window=5).mean()

As a result, you can quickly generate technical indicators without complex database queries.


Typical Use Cases After Data Storage

Once the stock data is stored, you can:

  1. Run basic time-based queries
  2. Calculate indicators like MA and MACD
  3. Visualize trends using Grafana
  4. Set up real-time alerts
  5. Optimize performance with pre-aggregation

Conclusion

Storing stock time-series data in InfluxDB provides a clean and efficient workflow. By combining Python and InfluxDB, you can collect, store, and analyze stock data with minimal overhead.

In the next step, this data can be visualized and monitored in real time.