geting only one time ohcl data without error after time stamp error
-
from SmartApi import SmartConnect from SmartApi.smartWebSocketV2 import SmartWebSocketV2 import threading import pyotp,time import pandas as pd from datetime import datetime import numpy as np import requests import websocket from logzero import logger from time import localtime,strftime #websocket required connection varibale action = 1 mode = 3 token_list = [ { "exchangeType": 1, "tokens": ["26009"] } ] tick = {} df = pd.DataFrame() sws = SmartWebSocketV2(AUTH_TOKEN, api_key, username, feedToken,max_retry_attempt=2, retry_strategy=0, retry_delay=10, retry_duration=30) def on_data(wsapp, message): # logger.info("Ticks: {}".format(message)) tick['timestamp'] = datetime.fromtimestamp(message['exchange_timestamp']/1000) tick['ltp'] = round(float(message['last_traded_price'])/100,2) # print(tick) try: global df # df_the_dict = pd.DataFrame([tick]) df = pd.concat([df, pd.DataFrame([tick])], ignore_index=True) currentTime = time.strftime("%Y-%m-%d %H:%M:%S", localtime()) # print(df.tail()) except Exception as e: print('Error in parsing json : ',e) # close_connection() def on_control_message(wsapp, message): logger.info(f"Control Message: {message}") # ltp = message[0]['last_traded_price'] # print(ltp) def on_open(wsapp): logger.info("on open") some_error_condition = False if some_error_condition: error_message = "Simulated error" if hasattr(wsapp, 'on_error'): wsapp.on_error("Custom Error Type", error_message) else: sws.subscribe(correlation_id, mode, token_list) # sws.unsubscribe(correlation_id, mode, token_list1) def on_error(wsapp, error): logger.error(error) def on_close(wsapp): logger.info("Close") def close_connection(): sws.close_connection() # Assign the callbacks. sws.on_open = on_open sws.on_data = on_data sws.on_error = on_error sws.on_close = on_close sws.on_control_message = on_control_message threading.Thread(target = sws.connect).start() ORB_timeFrame =60 def createOHLC(): try: start = time.time() global df copydf = df.copy(deep=True).drop_duplicates() df = pd.DataFrame() ![alt text](``` image url ```) getOHLC_df(copydf) interval = ORB_timeFrame - (time.time() - start) print(f"Next check will start after {interval} sec : {datetime.now()}") threading.Timer(interval, createOHLC).start() except Exception as e: print(e) def getOHLC_df(df): df_final = pd.DataFrame() try: data ={} data['timestamp'] = df['timestamp'].iloc[0] data['open']= df['ltp'].iloc[0] data['low']= df['ltp'].min() data['high']= df['ltp'].max() data['close']= df['ltp'].iloc[-1] df_final = pd.concat([df_final, pd.DataFrame([data])], ignore_index=True) print(df_final) except Exception as e: print(e) while True: ns =datetime.now() interval = ORB_timeFrame - ns.second print("start in ", interval) time.sleep(interval) createOHLC() `![out put.png](/assets/uploads/files/1708411901588-out-put.png)
-
Hi @jdanidhar
Apologies for the delayed response.
Able to notice "Nifty Bank" index token hardcoded as "26009". Kindly update it to "99926009".
To differentiate if the issue with code or with API. Please share us the API request(Along with headers)/error message from postman(or)curl to analyze from our end.Regards,
SmartAPI team