I have to migrate to web-socket from quotes:
How do I calculate the volume of last 1 minute? Is it
message['tot_sell_qty'] + message['tot_buy_qty']
Does web-socket count towards API rate limit?
What does
avg_trade_price
mean? What is the timeframe of average?What is the difference between
last_traded_time
andexch_feed_time
?Where should I call the function to calculate the candle generation function? Not sure if
After fetching the data, I am getting this error once:
Error processing message: 'tot_sell_qty'
Also, a feedback: the documentation is not exhaustive and it should be improved.
Pankaj Your help would be appreciated!
Code I am using:
def on_message(message):
"""Function on_message is used to process a message and extract relevant data from it.
Parameters:
- message (dict): A dictionary containing the message data.
Returns:
- None: This function does not return any value.
Processing Logic:
- Check if the message contains a "type" key.
- Check if the value of "type" key is either 'sf' or 'if'.
- Extract the symbol, ltp, exch_feed_time, and vol from the message.
- Create a data row using the extracted data.
- Pass the data row to the buffer_message function.
- If any error occurs, log the error."""
try:
if "type" in message and message["type"] in ('sf', 'if'):
symbol = message['symbol']
ltp = message['ltp']
exch_feed_time = message['exch_feed_time']
vol = message['tot_sell_qty'] + message['tot_buy_qty']
data_row = f"{symbol},{ltp},{ltp},{ltp},{ltp},{vol},{exch_feed_time}\n"
buffer_message(data_row)
except Exception as e:
logger.error(f"Error processing message: {e}")
def buffer_message(data_row):
"""Parameters:
- data_row (list): A list of data to be appended to the message buffer.
Returns:
- None: This function does not return any value.
Processing Logic:
- Append data_row to message_buffer.
- Check if message_buffer is full.
- If full, flush buffer to file."""
message_buffer.append(data_row)
if len(message_buffer) >= BUFFER_SIZE:
flush_buffer_to_file()
def flush_buffer_to_file():
"""Parameters:
- None
Returns:
- None
Processing Logic:
- If message_buffer is not empty.
- Open file in append mode.
- Write message_buffer to file.
- Clear message_buffer."""
if message_buffer:
with open(FILE_RAW_DATA, 'a') as f:
f.writelines(message_buffer)
message_buffer.clear()