How is upstox sending me ticks from future?

Here is log from my app

on message message{'type': 'live_feed', 'feeds': {'BSE_EQ|INE009A01021': {'ff': {'marketFF': {'ltpc': {'ltp': 1831.35, 'ltt': '1731058068137', 'ltq': '195', 'cp': 1802.65}, 'marketLevel': {'bidAskQuote': [{'bq': 14, 'bp': 1831.2, 'aq': 7, 'ap': 1832.0, 'bidQ': '14', 'askQ': '7'}, {'bq': 14, 'bp': 1831.15, 'aq': 16, 'ap': 1832.1, 'bidQ': '14', 'askQ': '16'}, {'bq': 62, 'bp': 1831.05, 'aq': 55, 'ap': 1832.2, 'bidQ': '62', 'askQ': '55'}, {'bq': 473, 'bp': 1831.0, 'aq': 79, 'ap': 1832.25, 'bidQ': '473', 'askQ': '79'}, {'bq': 42, 'bp': 1830.95, 'aq': 83, 'ap': 1832.3, 'bidQ': '42', 'askQ': '83'}]}, 'optionGreeks': {}, 'marketOHLC': {'ohlc': [{'interval': '1d', 'open': 1815.95, 'high': 1840.0, 'low': 1813.1, 'close': 1831.35, 'volume': 81630, 'ts': '1731004200000', 'vol': '81630'}, {'interval': 'I1', 'open': 1833.05, 'high': 1833.05, 'low': 1832.35, 'close': 1832.35, 'volume': 74, 'ts': '1731057960000', 'vol': '74'}, {'interval': 'I1', 'open': 1832.7, 'high': 1832.7, 'low': 1831.35, 'close': 1831.35, 'volume': 3244, 'ts': '1731058020000', 'vol': '3244'}, {'interval': 'I30', 'open': 1834.0, 'high': 1838.0, 'low': 1832.35, 'close': 1833.1, 'volume': 9069, 'ts': '1731055500000', 'vol': '9069'}, {'interval': 'I30', 'open': 1833.6, 'high': 1835.85, 'low': 1831.35, 'close': 1831.35, 'volume': 4069, 'ts': '1731057300000', 'vol': '4069'}]}, 'eFeedDetails': {'atp': 1832.11, 'cp': 1802.65, 'vtt': '81630', 'tbq': 51276.0, 'tsq': 450904.0, 'lc': 1622.4, 'uc': 1982.9, 'yh': 1990.9, 'yl': 1359.1}}}}}, 'currentTs': '1731058088543'}
message late by -1

I am calculating message late = TimeUtils.getEpochMilliseconds() -
currentTs. My systemClock is set to automatic time. It shows message is late by -1 milliseconds.

How is that possible?

Hi @Kislay
It’s highly unlikely for the market feed to have a future timestamp. Could you share the code of your implementation where you observed this -1 difference?

Thank you.

Hi @Ketan

Here is the relevant code

@staticmethod
def getEpochMilliseconds():
    return  time.time_ns() // 1_000_000

def message(self, data):
    if not isinstance(data, dict):
        data = json.loads(data)
    print(f"on message message{data}")
    currentTimeStamp = TimeUtils.getEpochMilliseconds()
    messageTimeSTamp = int(data["currentTs"])
    diff = currentTimeStamp - messageTimeSTamp
    print(f"message late by {diff}")

I am using MarketDataStreamer of Python sdk 2.9.0

@Kislay

I see two potential reasons for this:

  • Minor Timing Differences: If currentTs is generated just after currentTimeStamp is retrieved in the code, a slight mismatch can occur, especially at millisecond precision.
  • Rounding Differences: Since time.time_ns() returns time in nanoseconds, and you’re dividing it by 1,000,000 to get milliseconds, there may be minor rounding discrepancies.

I’d look into these points, as it’s unlikely for the incoming message to carry a future timestamp.

Check these out and let us know if it helps.

Thanks!

Hi @shanmu ,
1.1 currentTs is generated on upStox servers.
1.2 upstox Sends tick
1.3 After tick is received, currentTimeStamp is retrieved in my code as shared in the code snippet.

So it is not possible that currentTimeStamp is retrieved before currentTs is generated.

  1. Yes there may be rounding error of at max 1 ms. But for
   diff = currentTimeStamp - messageTimeSTamp

diff to be -1, the tick needs to be delivered with 0ms delay, which is not possible. So rounding error doesn’t explain -1 ms diff.

Thanks

@shanmu
Today I am getting outputs like these

on message message{'type': 'live_feed', 'feeds': {'NSE_EQ|INE062A01020': {'ff': {'marketFF': {'ltpc': {'ltp': 816.45, 'ltt': '1731477207609', 'ltq': '2', 'cp': 826.7}, 'marketLevel': {'bidAskQuote': [{'bq': 30, 'bp': 816.25, 'aq': 10, 'ap': 816.3, 'bidQ': '30', 'askQ': '10'}, {'bq': 204, 'bp': 816.2, 'aq': 150, 'ap': 816.45, 'bidQ': '204', 'askQ': '150'}, {'bq': 31, 'bp': 816.15, 'aq': 1076, 'ap': 816.5, 'bidQ': '31', 'askQ': '1076'}, {'bq': 383, 'bp': 816.1, 'aq': 260, 'ap': 816.55, 'bidQ': '383', 'askQ': '260'}, {'bq': 1426, 'bp': 816.05, 'aq': 514, 'ap': 816.6, 'bidQ': '1426', 'askQ': '514'}]}, 'optionGreeks': {}, 'marketOHLC': {'ohlc': [{'interval': '1d', 'open': 823.35, 'high': 831.75, 'low': 813.5, 'close': 816.45, 'volume': 5366987, 'ts': '1731436200000', 'vol': '5366987'}, {'interval': 'I1', 'open': 815.25, 'high': 815.7, 'low': 814.9, 'close': 815.5, 'volume': 22878, 'ts': '1731477120000', 'vol': '22878'}, {'interval': 'I1', 'open': 815.35, 'high': 816.5, 'low': 815.25, 'close': 816.45, 'volume': 10623, 'ts': '1731477180000', 'vol': '10623'}, {'interval': 'I30', 'open': 818.1, 'high': 823.0, 'low': 814.5, 'close': 814.95, 'volume': 829256, 'ts': '1731474900000', 'vol': '829256'}, {'interval': 'I30', 'open': 815.0, 'high': 816.5, 'low': 813.5, 'close': 816.45, 'volume': 159020, 'ts': '1731476700000', 'vol': '159020'}]}, 'eFeedDetails': {'atp': 823.04, 'cp': 826.7, 'vtt': '5366987', 'tbq': 536815.0, 'tsq': 869784.0, 'lc': 744.05, 'uc': 909.35, 'yh': 912.0, 'yl': 555.15}}}}}, 'currentTs': '1731477207925'}
message late by -42

@Kislay

This doesn’t seem correct.

Please share your complete implementation file, and we’ll review it.

Thanks!

@shanmu
I think issue is with UpStox. What do you get when you calculate the diff at your end?

Here is another place where timeStamp issue is present.

The linked issue is easily reproducible, but no one has even acknowledged that this bug is present.