How to organize Websockets data into OHLC?

Dear All,

I am a novice user on the Fyers API platform and would like to seek your assistance in organizing market data obtained via WebSocket into the structure of open, high, low, close, and volume.

Enclosed herewith is a sample dataset for your reference:
[{‘symbol’: ‘NSE:BANKNIFTY2381744200CE’, ‘timestamp’: 1692179079, ‘fyCode’: 7208, ‘fyFlag’: 2, ‘pktLen’: 200, ‘ltp’: 36.0, ‘open_price’: 60.0, ‘high_price’: 64.0, ‘low_price’: 23.5, ‘close_price’: 144.6, ‘min_open_price’: 35.7, ‘min_high_price’: 36.45, ‘min_low_price’: 35.05, ‘min_close_price’: 36.0, ‘min_volume’: 142635, ‘last_traded_qty’: 120, ‘last_traded_time’: 1692179079, ‘avg_trade_price’: 3931, ‘vol_traded_today’: 71572695, ‘tot_buy_qty’: 223665, ‘tot_sell_qty’: 341685, ‘market_pic’: [{‘price’: 36.0, ‘qty’: 660, ‘num_orders’: 6}, {‘price’: 35.95, ‘qty’: 840, ‘num_orders’: 13}, {‘price’: 35.9, ‘qty’: 1335, ‘num_orders’: 12}, {‘price’: 35.85, ‘qty’: 1275, ‘num_orders’: 12}, {‘price’: 35.79, ‘qty’: 795, ‘num_orders’: 9}, {‘price’: 36.1, ‘qty’: 465, ‘num_orders’: 5}, {‘price’: 36.15, ‘qty’: 975, ‘num_orders’: 11}, {‘price’: 36.2, ‘qty’: 1950, ‘num_orders’: 15}, {‘price’: 36.25, ‘qty’: 1440, ‘num_orders’: 14}, {‘price’: 36.29, ‘qty’: 1515, ‘num_orders’: 10}]}]

By the way, I had tried re-sampling the data(timestamp,ltp,min_volume), however while testing the data with 1min candle’s data I found discrepancies for volume aggregation.
Note: If the similar query was addressed in the past, help me with the code. Thanks in advance.
Regards

hi @tejaswini.alu50pcz

Do you want only OHLC for a particular symbol ?

Is it ? Please confirm for same so that I can help you

I am struggling with an error:
tried finding solutions but none worked. Can you help?

I tried changing Python version. I also created a separate environment. Doesn’t help.

Here is my code simplified for testing:
from fyers_api.Websocket import ws

import os

access_token = “eyJ0eXAiOi*****”

app_id= “9y**-100”

access_token = f’{app_id}:{access_token}’

def custom_message(msg):

symbol = msg[0][‘symbol’]

ltp = msg[0][‘ltp’]

high = msg[0][‘high_price’]

low = msg[0][‘low_price’]

print(f"Symbol: {symbol}, LTP: {ltp}, High: {high}, Low: {low}")

def create_watchlist(access_token):

data_type = “symbolData”

symbols = [“NSE:HDFC-EQ”]

fs = ws.FyersSocket(access_token=access_token,run_background=False, log_path=“/Users/Documents/Algo_trading/”)

fs.websocket_data = custom_message

fs.subscribe(symbol=symbols, data_type=data_type)

fs.keep_running()

create_watchlist(access_token)

{‘s’: ‘error’, ‘code’: -300, ‘message’: ‘Please provide a valid symbol’}

Hi Pankaj,

I extend my gratitude for your timely response.

Indeed, at present, the scope pertains to data associated with a single symbol.

My objective is to perform a re-sampling of this data, condensing it into a OHLCV format that aligns with intervals of 30 seconds or shorter.

Best Regards

Hi @praveen3 ,
You are getting this error because this symbol does not exist.
You can find more details on google related to HDFC demerger

Try with this new symbol : NSE:HDFCAMC-EQ

And let me know if you are still facing issue

Hi @tejaswini.alu50pcz ,

you can use the praveen custom message code. if this is not working fine I am attaching mine also.

def custom_message(msg):
    print (f"Custom:{msg}")
    resp = {
        "symbol":msg[0]['symbol'],
        "open_price":msg[0]['open_price'],
        "high_price":msg[0]['high_price'],
        "low_price":msg[0]['low_price'],
        "close_price":msg[0]['close_price'],
        "ltp":msg[0]['ltp']
        
    }
    print (f"OHLC data :  {resp}")

using this You can get easily OHLC data and you can perform your own analysis.

Hope you got it.
Thank you