how to Convert LTP to OHLC


  • how to Convert LTP to OHLC


  • HI @Learner As this is a user specific requirement you need to connect with your own software team who can code for you.


  • @admin how to get in touch with your software team? Please provide link.


  • HI @Learner This is complete user specific requirement please get in touch with software team.


  • @admin Sir, forgive me. I have a limited understanding of how python works. From whatever I have understood from your reply, I have searched online for datetime script and modified it accordingly.

    Below is the python script which fetches current date time.

    import time
    from datetime import datetime
    import pytz

    Asia time zone

    IST = pytz.timezone('Asia/Kolkata')
    datetime_ist = datetime.now(IST)
    print("Datetime:",
    datetime_ist.strftime('%Y/%m/%d %H:%M:%S'))

    I have merged the time script with LTP API and I'm getting the below response.

    Datetime: 2021/10/14 11:23:25
    {'data': {'close': 481.7,
    'exchange': 'NSE',
    'high': 491.9,
    'low': 482.0,
    'ltp': 489.25,
    'open': 482.0,
    'symboltoken': '3045',
    'tradingsymbol': 'SBIN-EQ'},
    'errorcode': '',
    'message': 'SUCCESS',
    'status': True}

    Can you please tell me how to create DataFrame that has datetime, open high low close values?

    Thank you


  • Hi @Learner This is a user specific requirement. But to achieve this you need to initiate a empty dataframe with defined column name as date time you need to log the timing everytime you call the ltp api and append it to the dataframe column datetime.


  • @admin Hello Sir, thanks for replying. I have searched the forum and found the below code:

    from smartapi import SmartConnect
    SmartApi =SmartConnect(api_key=apikey)
    login = SmartApi.generateSession(username, pwd)

    exchange = "NSE"
    tradingsymbol = "SBIN-EQ"
    symboltoken = 3045
    SmartApi.ltpData("NSE", "SBIN-EQ", "3045")

    I've used it and got below output:

    {'data': {'close': 469.25,
    'exchange': 'NSE',
    'high': 478.5,
    'low': 468.1,
    'ltp': 471.4,
    'open': 470.0,
    'symboltoken': '3045',
    'tradingsymbol': 'SBIN-EQ'},
    'errorcode': '',
    'message': 'SUCCESS',
    'status': True}

    Can you please tell me how to load the above output into a DataFrame along with date and time. Thank you.


  • Hi @Learner The above mentioned response is from the websocket if you want to get the ohlc you can use our ltp api.


  • @admin Hello Sir, I'm getting response like this. It is not showing open high low close values. Please tell me how to get ohlc values.

    __on_open################
    {'task': 'cn', 'channel': 'NONLM', 'token': '0972771668', 'user': 'V314538', 'acctid': 'V314538'}
    2021-10-11 08:12:27.072680 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': '0972771668', 'user': 'V314538', 'acctid': 'V314538'}
    on open
    Ticks: [{'ak': 'ok', 'msg': 'connected', 'task': 'cn'}]
    Ticks: [{'ak': 'ok', 'msg': 'heartbeat', 'task': 'hb'}]
    Ticks: [{'e': 'nse_cm', 'ltp': '2684.80', 'ltq': '13', 'ltt': 'NA', 'name': 'sf', 'tk': '2885'}]
    Ticks: [{'ap': '2694.39', 'bp': '2684.80', 'bq': '7', 'bs': '69', 'c': '2671.25', 'cng': '13.55', 'e': 'nse_cm', 'lo': '2676.55', 'ltp': '2684.80', 'ltq': '13', 'ltt': '11/10/2021 13:42:27', 'name': 'sf', 'nc': '00.5073', 'sp': '2684.95', 'tbq': '187430', 'tk': '2885', 'to': '11683209144.36', 'tsq': '526331', 'v': '4336124'}]
    Ticks: [{'name': 'tm', 'tvalue': '11/10/2021 13:42:28'}]
    2021-10-11 08:12:28.395460 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': '0972771668', 'user': 'V314538', 'acctid': 'V314538'}


  • HI @balabsm Belo is the response of ltp api we already provide ohlc.
    BAJAJ-AUTO-EQ : 16669
    {'status': True, 'message': 'SUCCESS', 'errorcode': '', 'data': {'exchange': 'NSE', 'tradingsymbol': 'BAJAJ-AUTO-EQ', 'symboltoken': '16669', 'open': 3929.0, 'high': 3944.8, 'low': 3875.4, 'close': 3913.4, 'ltp': 3900.0}}