Skip to content

contango.data.storage.store_market_data

contango.data.storage.store_market_data

DataStorage

DataStorage(database_path: str | Path | None = None)

Allows for the storage of OHLCV data to prevent re-polling API data that has already been received before.

Initializes DataStorage.

Parameters:

Name Type Description Default
database_path str | Path | None

Where to store the database. If not provided, falls back to the OS-standard user data directory.

None
Source code in src\contango\data\storage\store_market_data.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def __init__(self, database_path: str | Path | None = None) -> None:
    """
    Initializes `DataStorage`.

    Args:
        database_path: Where to store the database. If not provided, falls
                       back to the OS-standard user data directory.
    """
    resolved_path = Path(database_path) if database_path is not None else _default_database_path()
    resolved_path.parent.mkdir(parents=True, exist_ok=True)

    self._connection = sqlite3.connect(resolved_path)
    self.database_path = resolved_path

    self._create_tables()

add_data_to_storage

add_data_to_storage(interval: str, data: list[MarketDataEvent]) -> None

Adds market data to storage. Any existig candles with the same symbol/interval/timestamp are ignored.

Parameters:

Name Type Description Default
interval str

The interval to save the data as (i.e. 1m, 5m, etc).

required
data list[MarketDataEvent]

The data to save into the database.

required
Source code in src\contango\data\storage\store_market_data.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def add_data_to_storage(
    self,
    interval: str,
    data: list[MarketDataEvent],
) -> None:
    """
    Adds market data to storage. Any existig candles with the same symbol/interval/timestamp are ignored.

    Args:
        interval: The interval to save the data as (i.e. `1m`, `5m`, etc).
        data: The data to save into the database.
    """
    self._connection.executemany(
        """
        INSERT OR IGNORE INTO market_data
        (
            symbol,
            interval,
            timestamp,
            open,
            high,
            low,
            close,
            volume
        )
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        """,
        [
            (
                event.symbol,
                interval,
                event.timestamp,
                event.open,
                event.high,
                event.low,
                event.close,
                event.volume,
            )
            for event in data
        ],
    )

    self._connection.commit()

close

close() -> None

Closes the connection to the database.

Source code in src\contango\data\storage\store_market_data.py
281
282
283
284
285
def close(self) -> None:
    """
    Closes the connection to the database.
    """
    self._connection.close()

get_data

get_data(symbol: str, interval: str, start_timestamp: int, end_timestamp: int) -> list[MarketDataEvent]

Retrieves market data in order by timestamp.

Parameters:

Name Type Description Default
symbol str

The symbol to derive data from.

required
interval str

The interval to retrieve data from for the symbol (i.e. 1m, 5m, etc).

required
start_timestamp int

The start timestamp in unix ms.

required
end_timestamp int

The end timestamp in unix ms.

required

Returns:

Type Description
list[MarketDataEvent]

list[MarketDataEvent]: The data for the provided parameters.

Source code in src\contango\data\storage\store_market_data.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def get_data(
    self,
    symbol: str,
    interval: str,
    start_timestamp: int,
    end_timestamp: int,
) -> list[MarketDataEvent]:
    """
    Retrieves market data in order by timestamp.

    Args:
        symbol: The symbol to derive data from.
        interval: The interval to retrieve data from for the symbol (i.e. `1m`, `5m`, etc).
        start_timestamp: The start timestamp in unix ms.
        end_timestamp: The end timestamp in unix ms.

    Returns:
        list[MarketDataEvent]: The data for the provided parameters.
    """
    cursor = self._connection.execute(
        """
        SELECT
            timestamp,
            symbol,
            open,
            high,
            low,
            close,
            volume
        FROM market_data
        WHERE symbol = ?
          AND interval = ?
          AND timestamp BETWEEN ? AND ?
        ORDER BY timestamp ASC
        """,
        (
            symbol,
            interval,
            start_timestamp,
            end_timestamp,
        ),
    )

    return [
        MarketDataEvent(
            timestamp=row[0],
            symbol=row[1],
            open=row[2],
            high=row[3],
            low=row[4],
            close=row[5],
            volume=row[6],
        )
        for row in cursor.fetchall()
    ]

get_earliest_available

get_earliest_available(symbol: str, interval: str) -> int | None

Returns the earliest timestamp (unix ms) known to be a valid lower bound for this symbol/interval's data, if one has been recorded. None if never recorded.

Source code in src\contango\data\storage\store_market_data.py
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
def get_earliest_available(self, symbol: str, interval: str) -> int | None:
    """
    Returns the earliest timestamp (unix ms) known to be a valid lower
    bound for this symbol/interval's data, if one has been recorded. None if never recorded.
    """
    cursor = self._connection.execute(
        """
        SELECT earliest_available_timestamp
        FROM symbol_coverage
        WHERE symbol = ? AND interval = ?
        """,
        (symbol, interval),
    )
    row = cursor.fetchone()
    return row[0] if row is not None else None

get_missing_timestamps

get_missing_timestamps(symbol: str, interval: str, expected_timestamps: Iterable[datetime], earliest_available: int | None = None) -> list[int]

Returns the timestamps that are NOT currently stored for the given symbol or interval, out of a supplied set of expected timestamps.

Parameters:

Name Type Description Default
symbol str

The symbol to check.

required
interval str

The interval to check (i.e. 1m, 5m, etc).

required
expected_timestamps Iterable[datetime]

The datetimes candles are expected to exist for. Must be timezone-aware (or assumed UTC if naive) since stored timestamps are unix ms in UTC.

required
earliest_available int | None

If provided, expected timestamps earlier than this (unix ms) are dropped before the missing-check, since they're already known to have no data available from the provider.

None

Returns:

Type Description
list[int]

list[int]: Sorted list of missing timestamps (unix ms).

Source code in src\contango\data\storage\store_market_data.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def get_missing_timestamps(
    self,
    symbol: str,
    interval: str,
    expected_timestamps: Iterable[datetime],
    earliest_available: int | None = None,
) -> list[int]:
    """
    Returns the timestamps that are NOT currently stored for the given
    symbol or interval, out of a supplied set of expected timestamps.

    Args:
        symbol: The symbol to check.
        interval: The interval to check (i.e. `1m`, `5m`, etc).
        expected_timestamps: The datetimes candles are expected to exist
                             for. Must be timezone-aware (or assumed UTC if naive) since
                             stored timestamps are unix ms in UTC.
        earliest_available: If provided, expected timestamps earlier than
                            this (unix ms) are dropped before the missing-check,
                            since they're already known to have no data
                            available from the provider.

    Returns:
        list[int]: Sorted list of missing timestamps (unix ms).
    """
    expected_ms = {
        int(
            (ts if ts.tzinfo else ts.replace(tzinfo=timezone.utc))
            .timestamp() * 1000
        )
        for ts in expected_timestamps
    }

    if earliest_available is not None:
        expected_ms = {ts for ts in expected_ms if ts >= earliest_available}

    if not expected_ms:
        return []

    cursor = self._connection.execute(
        """
        SELECT timestamp
        FROM market_data
        WHERE symbol = ?
        AND interval = ?
        AND timestamp BETWEEN ? AND ?
        """,
        (
            symbol,
            interval,
            min(expected_ms),
            max(expected_ms),
        ),
    )

    existing = {row[0] for row in cursor.fetchall()}

    return sorted(expected_ms - existing)

set_earliest_available

set_earliest_available(symbol: str, interval: str, timestamp: int) -> None

Records the earliest known-valid lower bound (unix ms) for the symbol/interval.

Source code in src\contango\data\storage\store_market_data.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
def set_earliest_available(self, symbol: str, interval: str, timestamp: int) -> None:
    """
    Records the earliest known-valid lower bound (unix ms) for the
    symbol/interval.
    """
    self._connection.execute(
        """
        INSERT INTO symbol_coverage (symbol, interval, earliest_available_timestamp)
        VALUES (?, ?, ?)
        ON CONFLICT(symbol, interval) DO UPDATE SET earliest_available_timestamp = excluded.earliest_available_timestamp
        """,
        (symbol, interval, timestamp),
    )
    self._connection.commit()