For the complete documentation index, see llms.txt. This page is also available as Markdown.

32 - Master Contract Download

Overview

Master contracts contain symbol mappings between OpenAlgo's standardized format and broker-specific formats. They are downloaded at most once per day, on broker login, and cached in the symtoken table plus an in-memory cache for fast symbol resolution.

Architecture Diagram

Master contract download: login or Force Download triggers, a check for a missing or stale symbol list (first run, broker change, new day or a download before the 08:00 IST cutoff, 00:00 UTC for crypto), background thread running the broker master_contract_db, symtoken, status table and memory cache

Download Process

1. Trigger on Broker Login

2. Background Download

3. Broker-Specific Download

Every broker plugin exposes the same entry point, master_contract_download().

Symbol Database Schema

symtoken Table

Symbol Mapping

OpenAlgo to Broker

Get Token

Get Symbol Info

Broker Implementations

Zerodha

Dhan

Angel One

Caching Strategy

In-Memory Cache

database/token_db.py is a thin compatibility shim that re-exports everything from database/token_db_enhanced.py, which keeps the whole symbol set (100,000+ rows) in memory for O(1) lookups instead of expiring individual entries.

After a successful download, database/master_contract_cache_hook.py (hook_into_master_contract_download) reloads the memory cache for the broker. Every lookup also has a *_dbquery twin (get_token_dbquery, get_symbol_dbquery, and so on) that bypasses the cache and hits the database directly.

Database Index

SymToken in database/symbol.py declares three composite indices, plus single column indices on symbol, brsymbol, exchange, brexchange and token.

Status Tracking

master_contract_status Table

Stuck Download Detection

The system auto-detects stuck master contract downloads. If a download stays in 'downloading' state for more than DOWNLOAD_TIMEOUT_MINUTES (5 minutes, a module level constant, not an environment variable), it is automatically marked as an error, and the UI enables the "Force Download" button for manual retry.

Force Download is exposed by blueprints/master_contract_status.py at POST /api/master-contract/download. The same blueprint serves GET /api/master-contract/status, GET /api/master-contract/ready and GET /api/master-contract/smart-status.

Error Handling

Download Failures

async_master_contract_download wraps this call and writes the outcome to the master_contract_status table with update_status(broker, "success"|"error", message).

Fallback to Cache

If download fails, use existing cached data:

Daily Refresh

There is no cron job. The refresh is login driven: should_download_master_contract(broker) in utils/auth_utils.py compares the last successful download against a per day cutoff time, so the first login after the cutoff downloads fresh data and every later login on the same day reuses the cached symbols.

Cutoff Configuration

Env Variable
Default in code
Timezone
Applies to

MASTER_CONTRACT_CUTOFF_TIME

08:00

IST

Indian exchange brokers

CRYPTO_MASTER_CONTRACT_CUTOFF_TIME

00:00

UTC

Crypto brokers in CRYPTO_BROKERS

CRYPTO_BROKERS is defined in utils/constants.py and currently contains deltaexchange. The format for both variables is HH:MM in 24-hour form.

When the download is skipped, load_existing_master_contract(broker) marks the status ready from the existing symtoken rows instead.

Key Files Reference

File
Purpose

broker/*/database/master_contract_db.py

Broker download (master_contract_download())

database/symbol.py

SymToken model, symtoken table

database/token_db.py

Symbol lookup (re-exports token_db_enhanced)

database/token_db_enhanced.py

Full memory symbol cache

database/master_contract_cache_hook.py

Cache reload after download

database/master_contract_status_db.py

Status tracking (MasterContractStatus)

blueprints/master_contract_status.py

Status and Force Download routes

utils/auth_utils.py

Smart download decision and async trigger

Last updated

Was this helpful?