(.venv) PS C:\Users\samir manjra\Desktop\Unnamed_Trading_ALGO> & “C:/Users/samir manjra/Desktop/Unnamed_Trading_ALGO/.venv/Scripts/python.exe” “c:/Users/samir manjra/Desktop/Unnamed_Trading_ALGO/modules/market_data_master.py”
[2025-08-04 15:23:56] Starting Market Data Master
Using valid existing token.
[2025-08-04 15:23:56] Fetching instruments from Upstox official JSON…
[2025-08-04 15:23:57] Downloaded fresh NSE instruments JSON
[2025-08-04 15:23:57] Loaded 52489 instruments from JSON
[2025-08-04 15:23:57] Saved 1 NIFTY instruments → C:\Users\samir manjra\Desktop\Unnamed_Trading_ALGO\logs\2025-08-04\nifty_instruments.csv
[2025-08-04 15:23:57] Saved 1 BANKNIFTY instruments → C:\Users\samir manjra\Desktop\Unnamed_Trading_ALGO\logs\2025-08-04\banknifty_instruments.csv
[2025-08-04 15:23:57] Saved 0 SENSEX instruments → C:\Users\samir manjra\Desktop\Unnamed_Trading_ALGO\logs\2025-08-04\sensex_instruments.csv
[2025-08-04 15:23:57] DEBUG NIFTY instruments (top 5): [{‘weekly’: nan, ‘segment’: ‘NSE_INDEX’, ‘name’: ‘Nifty 50’, ‘exchange’: ‘NSE’, ‘expiry’: nan, ‘instrument_type’: ‘INDEX’, ‘asset_symbol’: nan, ‘underlying_symbol’: nan, ‘instrument_key’: ‘NSE_INDEX|Nifty 50’, ‘lot_size’: nan, ‘freeze_quantity’: nan, ‘exchange_token’: ‘26000’, ‘minimum_lot’: nan, ‘tick_size’: nan, ‘asset_type’: nan, ‘underlying_type’: nan, ‘trading_symbol’: ‘NIFTY’, ‘strike_price’: nan, ‘qty_multiplier’: nan, ‘isin’: nan, ‘short_name’: nan, ‘security_type’: nan, ‘asset_key’: nan, ‘underlying_key’: nan, ‘last_trading_date’: nan, ‘price_quote_unit’: nan}]
[2025-08-04 15:23:57] DEBUG BANKNIFTY instruments (top 5): [{‘weekly’: nan, ‘segment’: ‘NSE_INDEX’, ‘name’: ‘Nifty Bank’, ‘exchange’: ‘NSE’, ‘expiry’: nan, ‘instrument_type’: ‘INDEX’, ‘asset_symbol’: nan, ‘underlying_symbol’: nan, ‘instrument_key’: ‘NSE_INDEX|Nifty Bank’, ‘lot_size’: nan, ‘freeze_quantity’: nan, ‘exchange_token’: ‘26009’, ‘minimum_lot’: nan, ‘tick_size’: nan, ‘asset_type’: nan, ‘underlying_type’: nan, ‘trading_symbol’: ‘BANKNIFTY’, ‘strike_price’: nan, ‘qty_multiplier’: nan, ‘isin’: nan, ‘short_name’: nan, ‘security_type’: nan, ‘asset_key’: nan, ‘underlying_key’: nan, ‘last_trading_date’: nan, ‘price_quote_unit’: nan}]
[2025-08-04 15:23:57] No instruments found for SENSEX
[2025-08-04 15:23:58] Saved option chain for NIFTY → C:\Users\samir manjra\Desktop\Unnamed_Trading_ALGO\logs\2025-08-04\nifty_option_chain.csv
[2025-08-04 15:23:59] Saved option chain for BANKNIFTY → C:\Users\samir manjra\Desktop\Unnamed_Trading_ALGO\logs\2025-08-04\banknifty_option_chain.csv
[2025-08-04 15:23:59] No instruments found for SENSEX, skipping…
[2025-08-04 15:23:59] Instruments ready for live streaming.
[2025-08-04 15:23:59] Using instrument keys for subscription: [‘NSE_INDEX|Nifty 50’, ‘NSE_INDEX|Nifty Bank’]
[2025-08-04 15:23:59] Subscribing to instruments: [‘NSE_INDEX|Nifty 50’, ‘NSE_INDEX|Nifty Bank’]
[2025-08-04 15:23:59] Market already open. Proceeding…
[2025-08-04 15:23:59] Connecting to Upstox WebSocket feed…
[DEBUG] Connecting to feed URL: None
Using valid existing token.
[2025-08-04 15:24:00] Fetching authorized WebSocket URL…
[2025-08-04 15:24:00] Authorized WebSocket URL: wss://wsfeeder-api.upstox.com/market-data-feeder/v2/upstox-developer-api/feeds?requestId=55f76bde-ede8-45dd-a7fc-d98955ebb1f2&code=t1CTP-31d26854-0a88-4f5e-95a8-2d1e4fe3403c
[2025-08-04 15:24:00] Connecting WebSocket: wss://wsfeeder-api.upstox.com/market-data-feeder/v2/upstox-developer-api/feeds?requestId=55f76bde-ede8-45dd-a7fc-d98955ebb1f2&code=t1CTP-31d26854-0a88-4f5e-95a8-2d1e4fe3403c
[2025-08-04 15:24:00] WebSocket connected. Subscribing…
[2025-08-04 15:24:01] Subscription sent: {‘guid’: ‘sub_1’, ‘method’: ‘sub’, ‘data’: {‘mode’: ‘ltpc’, ‘instrumentKeys’: [‘NSE_INDEX|Nifty 50’, ‘NSE_INDEX|Nifty Bank’]}}
Hi @samir_40204238, this might be handy
Also please note: Deprecation of V2 Market Data Feed Websocket
Hello @everyone
Even i am facing same issue from Long Time, Currently Websocket is not providing realtime data for some instrument keys.
Its Mainlyb creating problem for Options Trades.
Check Logs.
[2025-08-06 10:51:02] local.INFO: Websockt is running For TRADE
[2025-08-06 10:51:06] local.INFO: SKIPPED stale tick for NSE_EQ|INE919I01024: LTP=8.6, LTT=2025-08-06 10:48:00 (diff: 186s)
[2025-08-06 10:51:06] local.INFO: SKIPPED stale tick for NSE_EQ|INE642Y01029: LTP=33.46, LTT=2025-08-06 10:47:12 (diff: 234s)
[2025-08-06 10:51:06] local.INFO: SKIPPED stale tick for NSE_EQ|INF204KC1022: LTP=132.79, LTT=2025-08-06 10:31:37 (diff: 1169s)
[2025-08-06 10:51:06] local.INFO: SKIPPED stale tick for NSE_EQ|INE350C01017: LTP=592.8, LTT=2025-08-06 10:48:59 (diff: 127s)
[2025-08-06 10:51:06] local.INFO: SKIPPED stale tick for NSE_EQ|INE483B01026: LTP=253.1, LTT=2025-08-06 10:50:04 (diff: 62s)
[2025-08-06 10:51:06] local.INFO: SKIPPED stale tick for NSE_EQ|INE338A01024: LTP=282.95, LTT=2025-08-06 10:47:43 (diff: 203s)
[2025-08-06 10:51:14] local.INFO: SKIPPED stale tick for NSE_EQ|INE483B01026: LTP=253.1, LTT=2025-08-06 10:50:04 (diff: 70s)
Code
$currentDateTime = new DateTime();
$bearerToken = getBearerTokenOfUpstox();
if ($bearerToken == null) {
// Log::info("Upstox token not updated");
return null;
}
Log::info("Websockt is running For TRADE");
$apiVersion = '3.0';
$accessToken = $bearerToken;
// Configure with your access token
$configuration = Configuration::getDefaultConfiguration();
$configuration->setAccessToken($accessToken);
// Get the authorized URL for the WebSocket connection
$response = $this->getMarketDataFeedAuthorizeV3($apiVersion, $configuration);
$connection = connect($response\['data'\]\['authorized_redirect_uri'\]);
//Log::info($response\['data'\]\['authorized_redirect_uri'\]);
// Fetch buy/sell data of recommendations
$getBuySellDataOfRecommendations = ManageRecommendation::select('id', 'instrument_key', 'recommendation_actions_xid')
->whereIn('recommendation_actions_xid', \[1, 2, 3\])
//->where('product_type_xid',2) //only for tradeoptions
->orderByDesc('id')
->get();
$instrumentKeyMap = \[\];
foreach ($getBuySellDataOfRecommendations as $recommendation) {
if ($recommendation->schedule_date_time < $currentDateTime->format('Y-m-d H:i:s') || $recommendation->schedule_date_time == null) {
$instrumentKeyMap\[$recommendation->instrument_key\]\[\] = $recommendation->id;
}
}
// Extract instrument keys
$instrumentKeys = array_keys($instrumentKeyMap);
if (empty($instrumentKeys)) {
//Log::warning('No valid instrument keys found. Aborting WebSocket connection in TRADEOption.');
return;
}
// Message payload to send to the server
$data = \[
"guid" => uniqid('guid\_', true),
"method" => "sub",
"data" => \[
"mode" => "ltpc",
"instrumentKeys" => $instrumentKeys
\]
\];
// Send the data as binary
$binaryData = json_encode($data);
$connection->sendBinary($binaryData);
// Define stop time (4:00 PM)
// $stopTime = now()->setTime(19, 9);
foreach ($connection as $message) {
$payload = $message->buffer();
usleep(2000000);
if ($payload === '100') {
$connection->close();
break;
}
if (!empty($payload)) {
$decodedData = $this->decodeProtobuf($payload);
$jsonData = json_decode($decodedData->serializeToJsonString(), true);
if (empty($jsonData\['feeds'\])) {
Log::info('No feed data found in payload For TRADEOption');
continue;
}
// Log::info("Trade Options running");
foreach ($jsonData\['feeds'\] as $instrumentKey => $instrumentData) {
// Get the recommendation ID using the instrument_key
$recommendationIds = $instrumentKeyMap\[$instrumentKey\] ?? \[\];
if (empty($recommendationIds) || !isset($instrumentData\['ltpc'\]\['ltp'\])) {
continue;
}
$ltp = $instrumentData\['ltpc'\]\['ltp'\];
$lttMs = $instrumentData\['ltpc'\]\['ltt'\] ?? null;
if ($lttMs) {
$lttCarbon = Carbon::createFromTimestampMs($lttMs);
$diffInSeconds = now()->diffInSeconds($lttCarbon);
if ($diffInSeconds > 60) {
Log::info("SKIPPED stale tick for {$instrumentKey}: LTP={$ltp}, LTT={$lttCarbon->toDateTimeString()} (diff: {$diffInSeconds}s)");
continue;
}
} else {
Log::error("lttMs not found for Instrument Key");
}
foreach ($recommendationIds as $recommendationId) {
$getBuySellDataOfRecommendationItem = ManageRecommendation::with('productTypeData')->find($recommendationId);
if (!$getBuySellDataOfRecommendationItem) {
continue;
}
// Parse range
$range = $getBuySellDataOfRecommendationItem->buy_price;
$lastTradedPrice = $ltp;
Log::info("RECEIVED-:{$lastTradedPrice}-{$getBuySellDataOfRecommendationItem->stock_name}-{$range}-LTT={$lttCarbon->toDateTimeString()}");
}
}
}
}
Please suggest changes or provide required solution.