Skip to content

Latest commit

 

History

History
 
 

analyze-intraday-stocks

Analyze historical intraday stock data

This project is an example on how to collect, store, and analyze intraday stock data with Python and TimescaleDB. To read the full tutorial, go to the TimescaleDB documentation!

Prerequisites

Installation and database setup

Clone repository and open analyze-intraday-stocks folder

git clone https://github.com/timescale/examples.git
cd analyze-intraday-stocks/ 

Create new virtual environment

virtualenv env
source env/bin/activate

Install requirements

pip install -r requirements.txt

Create table

Run sql_script/create_table.sql:

CREATE TABLE public.stocks_intraday (
    "time" timestamp(0) NOT NULL,
    symbol varchar NULL,
    price_open float8 NULL,
    price_close float8 NULL,
    price_low float8 NULL,
    price_high float8 NULL,
    trading_volume int4 NULL,
);

Turn it into a hypertable

Run sql_script/create_hypertable.sql

/* Enable the TimscaleDB extension */
CREATE EXTENSION IF NOT EXISTS timescaledb;

/* 
Turn the 'stocks_intraday' table into a hypertable.
This is important to be able to make use of TimescaleDB features later on.
*/
SELECT create_hypertable('stocks_intraday', 'time');

Edit configuration file

Edit config.py according to your database connection details.

# Make sure to edit this configuration file with your database connection details
# and Alpha Vantage API key
DB_USER = 'user'
DB_PASS = 'passwd'
DB_HOST = 'host'
DB_PORT = '000'
DB_NAME = 'db'
APIKEY = 'alpha_vantage_apikey'

Also, config.py holds your Alpha Vantage API key, so make sure to edit that too.

Usage

Run explore.py to run your first query against your database.

python explore.py