Market Data fetch issue..!

I am trying to fetch market data based on instrument list and trying to punch order on same index base. But, it is actually fetching data differently.
For ex: for 0, 1, 2 indexes data being fetched as 2, 0, 1.
Code:

import requests

with open('access_token.txt', 'r') as file:
    content = file.read()
lines = content.splitlines()
for line in lines:
    if line.startswith("access_token"):
        access_token = line.split('=')[1].strip().strip('"')
        break

full_market_url = 'https://api-v2.upstox.com/market-quote/quotes'
headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': f'Bearer {access_token}'
}

instrument = ['NSE_FO|52521', 'NSE_FO|52593', 'NSE_FO|52693']
#instrument = ['NIFTY2511623000CE', 'NIFTY2511623500CE', 'NIFTY2511624000CE']

params = {
    'symbol': ','.join(instrument)  # Corrected to use a comma-separated string of symbols
}

# Make the GET request to fetch market data
response = requests.get(full_market_url, headers=headers, params=params)

# Convert the response to JSON format
response_data = response.json()

print(response_data)

Output:

{'status': 'success', 'data': {'NSE_FO:NIFTY2511624000CE': {'ohlc': {'open': 40.6, 'high': 45.15, 'low': 15.45, 'close': 16.0}, 'depth': {'buy': [{'quantity': 825, 'price': 15.6, 'orders': 1}, {'quantity': 3825, 'price': 15.45, 'orders': 3}, {'quantity': 525, 'price': 15.4, 'orders': 1}, {'quantity': 975, 'price': 15.35, 'orders': 3}, {'quantity': 225, 'price': 15.3, 'orders': 2}], 'sell': [{'quantity': 825, 'price': 16.0, 'orders': 4}, {'quantity': 750, 'price': 16.1, 'orders': 1}, {'quantity': 825, 'price': 16.15, 'orders': 2}, {'quantity': 1125, 'price': 16.2, 'orders': 2}, {'quantity': 225, 'price': 16.3, 'orders': 2}]}, 'timestamp': '2025-01-12T14:26:32.848+05:30', 'instrument_token': 'NSE_FO|52693', 'symbol': 'NIFTY2511624000CE', 'last_price': 16.0, 'volume': 135200400, 'average_price': 26.99, 'oi': 8417100.0, 'net_change': -23.7, 'total_buy_quantity': 348825.0, 'total_sell_quantity': 1211850.0, 'lower_circuit_limit': 0.05, 'upper_circuit_limit': 69.75, 'last_trade_time': '1736503199936', 'oi_day_high': 9636675.0, 'oi_day_low': 4328325.0}, 'NSE_FO:NIFTY2511623000CE': {'ohlc': {'open': 590.05, 'high': 659.6, 'low': 451.05, 'close': 501.0}, 'depth': {'buy': [{'quantity': 150, 'price': 496.45, 'orders': 1}, {'quantity': 300, 'price': 496.4, 'orders': 2}, {'quantity': 1050, 'price': 495.5, 'orders': 1}, {'quantity': 375, 'price': 495.2, 'orders': 1}, {'quantity': 5550, 'price': 495.0, 'orders': 4}], 'sell': [{'quantity': 450, 'price': 499.65, 'orders': 1}, {'quantity': 1050, 'price': 500.0, 'orders': 1}, {'quantity': 900, 'price': 500.1, 'orders': 1}, {'quantity': 150, 'price': 501.6, 'orders': 1}, {'quantity': 75, 'price': 502.05, 'orders': 1}]}, 'timestamp': '2025-01-12T14:26:32.848+05:30', 'instrument_token': 'NSE_FO|52521', 'symbol': 'NIFTY2511623000CE', 'last_price': 501.0, 'volume': 2491650, 'average_price': 530.68, 'oi': 403350.0, 'net_change': -116.35, 'total_buy_quantity': 86400.0, 'total_sell_quantity': 35100.0, 'lower_circuit_limit': 0.05, 'upper_circuit_limit': 1006.85, 'last_trade_time': '1736503197061', 'oi_day_high': 420225.0, 'oi_day_low': 267075.0}, 'NSE_FO:NIFTY2511623500CE': {'ohlc': {'open': 243.0, 'high': 259.25, 'low': 122.75, 'close': 135.45}, 'depth': {'buy': [{'quantity': 75, 'price': 135.45, 'orders': 1}, {'quantity': 525, 'price': 135.4, 'orders': 1}, {'quantity': 375, 'price': 135.35, 'orders': 2}, {'quantity': 150, 'price': 135.2, 'orders': 1}, {'quantity': 750, 'price': 135.1, 'orders': 1}], 'sell': [{'quantity': 975, 'price': 137.1, 'orders': 1}, {'quantity': 450, 'price': 137.2, 'orders': 2}, {'quantity': 150, 'price': 137.3, 'orders': 1}, {'quantity': 375, 'price': 137.6, 'orders': 1}, {'quantity': 975, 'price': 137.65, 'orders': 1}]}, 'timestamp': '2025-01-12T14:26:32.848+05:30', 'instrument_token': 'NSE_FO|52593', 'symbol': 'NIFTY2511623500CE', 'last_price': 135.45, 'volume': 153718725, 'average_price': 179.26, 'oi': 4461300.0, 'net_change': -97.4, 'total_buy_quantity': 201375.0, 'total_sell_quantity': 513600.0, 'lower_circuit_limit': 0.05, 'upper_circuit_limit': 405.35, 'last_trade_time': '1736503199999', 'oi_day_high': 5637975.0, 'oi_day_low': 1654500.0}}}

Please suggest on thisā€¦!

Hi @Prasad_Gondala,

In the Full Market Quote API, you can subscribe to up to 500 instrument keys. Maintaining the sequential order of requests would require additional processing time on our end.

Since the Market Quote API provides live stock details, we prioritize minimizing latency and therefore do not maintain the sequential order of requests.

Thank you.

Thank you @Ketan,

As per above code and after, I am trying to punch orders based on this same index. Can you suggest any step in between data fetch and order punching to maintain the fetched data as per index and pass on to order punching code?

I donā€™t want to change these codes now.

Additionally, I am constructing the symbol to fetch instrument key to get live data before the data fetching step.

Hereā€™s a suggestion on how you can place orders using data from the Market Quote API:

Since we donā€™t maintain the sequence of instruments, you can use a map-like data structure (a dictionary in Python).

Use the instrument_token as the key in this map. With each Market API call, update the map with the latest values for the instruments. This can be done easily by iterating through the response. Since instrument_token is one of the fields in the input array, set it as the key and store the required fields as the corresponding value.

When your order placement condition is met for any instrument_token present as a key in the map, place an order for that specific instrument_token.

Thank you.

1 Like