Skip to content

Not properly accounting for market holydays (data shifts) #65

@pquan

Description

@pquan

It looks like market holydays are not taken into account. For example, Thanksgiving 2024 was on Nov.28th, 2024. There was no market.
Downloading the daily NQ future from TV, I get a candle on 2024-11-28 and I'm missing the candle on 2024-11-29 which was the real open market day as follows:

                      symbol      open      high       low     close    volume
datetime
2024-11-04  CME_MINI:NQZ2024  20138.00  20248.50  20013.00  20086.00  520521.0
....
2024-11-26  CME_MINI:NQZ2024  20906.25  21024.75  20755.00  20993.50  505254.0
2024-11-27  CME_MINI:NQZ2024  20990.75  21019.00  20675.00  20813.00  520026.0
2024-11-28  CME_MINI:NQZ2024  20813.00  21017.50  20805.50  20993.50  339955.0

The data for 2024-11-28 should actually be the candle of 2024-11-29 (OHLCV values are correct for the 2024-11-29 date).

Further, looking at the TradingView Chart, i can confirm that the Candle for 2024-11-28 is missing, and the data is present only for 2024-11-27 and 2024-11-29.

My guess (to be verified) is that the same issue occurs on all market holidays and the data is shifted.

EDIT: Verifying the data that comes from TV, I see that the json value contains:

['i"', '134', '"v"', '', '1732748400.0', '20813.0', '21017.5', '20805.5', '20993.5', '339955.0', '}']

This means that, assuming the TV data is correct, some offset/timezone mangling is necessary to properly align the timestamp to a correct date.

A proposed fix:

Digging further, my guess is that:

  • The timestamp returned by TV is localized in your particular timezone.
  • The timestamp is for the beginning of the business day, and if the timestamp happens on a holiday, then it must be moved to the next business day.
    With these assumpions, he below proposed fix produces the correct timestamps:
from datetime import datetime, timedelta
import pytz

# Define the timestamp
timestamp = 1732748400  # 2024-11-28 00:00:00

tz = pytz.timezone("<your-local-timezone>")
dt = datetime.fromtimestamp(timestamp,tz).date()
print(f"Unadjusted date : {dt}")
if not is_business_day(dt):
    dt = next_business_day(dt)
print(f"UAdjusted date : {dt}")

the two functions is_business_day and next_business_day can be implemented using the pypi "holidays" package or the pandas_market_calendars package. your-local-timezone is the timezone of your TV account, or the default timezone if not using an account.

With this proposed fix, here's the corrected dates from the NQZ2024 contact:

                      symbol      open      high       low     close    volume adjusted_index
datetime
2024-05-27  CME_MINI:NQZ2024  19354.75  19450.75  19349.25  19420.75      74.0     2024-05-28
2024-06-19  CME_MINI:NQZ2024  20456.50  20601.00  20218.25  20286.00     728.0     2024-06-20
2024-07-04  CME_MINI:NQZ2024  20670.75  20893.75  20624.00  20875.75     817.0     2024-07-05
2024-09-02  CME_MINI:NQZ2024  19850.75  19919.25  19143.75  19234.50    4192.0     2024-09-03
2024-11-28  CME_MINI:NQZ2024  20813.00  21017.50  20805.50  20993.50  339955.0     2024-11-29

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions