This repository contains Python scripts for managing and processing futures contract data, specifically for creating continuous contract data from individual contract files.
This script reads futures data from Sierra Chart .scid files and inserts it into a QuestDB database.
Features:
- Data Ingestion: Reads
.scidfiles, which contain tick data for futures contracts. - Database Integration: Inserts the data into a QuestDB time-series database.
- Checkpointing: Uses a
checkpoint.jsonfile to keep track of the last processed data, ensuring that the script can be run repeatedly without duplicating data. - Continuous Updates: The script can be run in a loop to continuously monitor for new data in the
.scidfile and update the database accordingly. - Table Creation: Automatically creates the necessary table in QuestDB if it doesn't already exist.
This script is similar to scid_to_qdb.py but is designed to work with a PostgreSQL database instead of QuestDB. It reads data from .scid files and loads it into a PostgreSQL table.
This script identifies the front contract for each day and updates the front_contract flag in the trades table.
This script creates a materialized view in QuestDB to aggregate the trade data into OHLC (Open, High, Low, Close) format for a specified time interval (e.g., 15 seconds).
The process of creating a continuous contract from individual .scid files involves the following steps:
The first step is to upload the raw futures data from the .scid files into a QuestDB database. This is done using the scid_to_qdb.py script.
-
Prerequisites:
- Ensure you have a running instance of QuestDB.
- Create a
qdb.envfile with the necessary database connection details (see.env.example).
-
Table Schema: The script assumes the following table schema in QuestDB. If the table does not exist, it will be created automatically.
CREATE TABLE trades ( time TIMESTAMP, open DOUBLE, high DOUBLE, low DOUBLE, close DOUBLE, volume INT, number_of_trades INT, bid_volume INT, ask_volume INT, symbol SYMBOL, symbol_period SYMBOL, front_contract BOOLEAN ) TIMESTAMP(time) PARTITION BY DAY WAL DEDUP UPSERT KEYS(time, symbol, symbol_period);
-
Execution: Run the
scid_to_qdb.pyscript, specifying the path to the.scidfile. The script will process the file and upload the data to thetradestable in QuestDB.python scid_to_qdb.py
The script will continuously monitor the
.scidfile for new data and update the database accordingly.
Once the data is in QuestDB, the next step is to identify the front contract for each day. The front contract is typically the contract with the highest trading volume for a given day.
-
Execution: Run the
compute_front_contract_questdb.pyscript.python lib/compute_front_contract_questdb.py
This script will iterate through the data, and for each day, it will identify the
symbol_periodwith the highest volume and set thefront_contractflag toTRUEfor that contract andFALSEfor all others.
To facilitate analysis, you can create materialized views that aggregate the raw trade data into OHLC format.
-
Execution: The
lib/create_materialized_views_qdb.pyscript contains the SQL query to create a materialized view. You can execute this script to create a view namedtrades_ohlc_15sthat aggregates the data into 15-second bars.CREATE MATERIALIZED VIEW IF NOT EXISTS trades_ohlc_15s WITH BASE 'trades' REFRESH IMMEDIATE AS ( SELECT time, symbol, symbol_period, first(open) AS open, max(high) AS high, min(low) AS low, last(close) AS close, sum(volume) AS volume, sum(number_of_trades) AS number_of_trades, sum(bid_volume) AS bid_volume, sum(ask_volume) AS ask_volume, front_contract FROM trades SAMPLE BY 15s ) PARTITION BY DAY;
You can run the script using:
python lib/qdb_create_materialized_views.py
This will create the materialized view in QuestDB, which can then be queried for aggregated OHLC data.