Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    how to Convert LTP to OHLC

    Python SDK
    3
    10
    188
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      balabsm last edited by

      how to Convert LTP to OHLC

      Thanks and Regards
      Balasubramanian. M

      A 1 Reply Last reply Reply Quote 0
      • A
        admin @balabsm last edited by

        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}}

        L 1 Reply Last reply Reply Quote 0
        • L
          Learner @admin last edited by

          @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'}

          A 1 Reply Last reply Reply Quote 0
          • A
            admin @Learner last edited by

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

            L 1 Reply Last reply Reply Quote 0
            • L
              Learner @admin last edited by

              @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.

              A 1 Reply Last reply Reply Quote 0
              • A
                admin @Learner last edited by

                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.

                L 1 Reply Last reply Reply Quote 0
                • L
                  Learner @admin last edited by

                  @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

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    admin @Learner last edited by

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

                    L 1 Reply Last reply Reply Quote 0
                    • L
                      Learner @admin last edited by

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

                      A 1 Reply Last reply Reply Quote 0
                      • A
                        admin @Learner last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post