Skip to main content

How to Fetch Data Tick from Kite Connect API (Nifty50)


Get Nifty50 data from Zerodha API

In Last blog we discuss about how to generate request token and now we are going to look how to get data from Kite Connect. Here we will subscribe Nifty50. We will go step by step
If you have code from zerodha official  git hub we can start further if not you get it from my previous blog "Getting Started with Zerodha "

1) We need list of Nifty50 stock which below Can verify this list on NSE India site. Below is json which you can use you code. if need we can key value pair using it.

var stocks=
[{"instrumenttoken" :3861249, "tradingsymbol" : "ADANIPORTS"},
{"instrumenttoken" :60417, "tradingsymbol" : "ASIANPAINT"},
{"instrumenttoken" :1510401, "tradingsymbol" : "AXISBANK"},
{"instrumenttoken" :4267265, "tradingsymbol" : "BAJAJ-AUTO"},
{"instrumenttoken" :4268801, "tradingsymbol" : "BAJAJFINSV"},
{"instrumenttoken" :81153, "tradingsymbol" : "BAJFINANCE"},
{"instrumenttoken" :2714625, "tradingsymbol" : "BHARTIARTL"},
{"instrumenttoken" :134657, "tradingsymbol" : "BPCL"},
{"instrumenttoken" :177665, "tradingsymbol" : "CIPLA"},
{"instrumenttoken" :5215745, "tradingsymbol" : "COALINDIA"},
{"instrumenttoken" :225537, "tradingsymbol" : "DRREDDY"},
{"instrumenttoken" :232961, "tradingsymbol" : "EICHERMOT"},
{"instrumenttoken" :1207553, "tradingsymbol" : "GAIL"},
{"instrumenttoken" :315393, "tradingsymbol" : "GRASIM"},
{"instrumenttoken" :1850625, "tradingsymbol" : "HCLTECH"},
{"instrumenttoken" :340481, "tradingsymbol" : "HDFC"},
{"instrumenttoken" :341249, "tradingsymbol" : "HDFCBANK"},
{"instrumenttoken" :345089, "tradingsymbol" : "HEROMOTOCO"},
{"instrumenttoken" :348929, "tradingsymbol" : "HINDALCO"},
{"instrumenttoken" :359937, "tradingsymbol" : "HINDPETRO"},
{"instrumenttoken" :356865, "tradingsymbol" : "HINDUNILVR"},
{"instrumenttoken" :7712001, "tradingsymbol" : "IBULHSGFIN"},
{"instrumenttoken" :1270529, "tradingsymbol" : "ICICIBANK"},
{"instrumenttoken" :1346049, "tradingsymbol" : "INDUSINDBK"},
{"instrumenttoken" :7458561, "tradingsymbol" : "INFRATEL"},
{"instrumenttoken" :408065, "tradingsymbol" : "INFY"},
{"instrumenttoken" :415745, "tradingsymbol" : "IOC"},
{"instrumenttoken" :424961, "tradingsymbol" : "ITC"},
{"instrumenttoken" :492033, "tradingsymbol" : "KOTAKBANK"},
{"instrumenttoken" :2939649, "tradingsymbol" : "LT"},
{"instrumenttoken" :2672641, "tradingsymbol" : "LUPIN"},
{"instrumenttoken" :519937, "tradingsymbol" : "M&M"},
{"instrumenttoken" :2815745, "tradingsymbol" : "MARUTI"},
{"instrumenttoken" :2977281, "tradingsymbol" : "NTPC"},
{"instrumenttoken" :633601, "tradingsymbol" : "ONGC"},
{"instrumenttoken" :3834113, "tradingsymbol" : "POWERGRID"},
{"instrumenttoken" :738561, "tradingsymbol" : "RELIANCE"},
{"instrumenttoken" :779521, "tradingsymbol" : "SBIN"},
{"instrumenttoken" :857857, "tradingsymbol" : "SUNPHARMA"},
{"instrumenttoken" :884737, "tradingsymbol" : "TATAMOTORS"},
{"instrumenttoken" :895745, "tradingsymbol" : "TATASTEEL"},
{"instrumenttoken" :2953217, "tradingsymbol" : "TCS"},
{"instrumenttoken" :3465729, "tradingsymbol" : "TECHM"},
{"instrumenttoken" :897537, "tradingsymbol" : "TITAN"},
{"instrumenttoken" :2952193, "tradingsymbol" : "ULTRACEMCO"},
{"instrumenttoken" :2889473, "tradingsymbol" : "UPL"},
{"instrumenttoken" :784129, "tradingsymbol" : "VEDL"},
{"instrumenttoken" :969473, "tradingsymbol" : "WIPRO"},
{"instrumenttoken" :3050241, "tradingsymbol" : "YESBANK"},
{"instrumenttoken" :975873, "tradingsymbol" : "ZEEL"}];

You can keep this json into some other location to make you stock list configurable. if you wish more stock to subscribe then you can add it.


Here we need only array of instrument number  which as below.
 
static UInt32[] intrument_tokent = new UInt32[] { 897537, 857857, 2953217, 895745, 884737, 633601, 779521, 415745, 2815745, 408065, 7458561, 1270529, 7712001, 341249, 1207553, 232961, 225537, 81153, 4267265, 1510401, 60417, 3861249, 519937, 2939649, 975873, 3050241, 969473, 784129, 2889473, 3465729, 345089, 492033, 2952193, 2714625, 4268801, 356865, 359937, 348929, 315393, 2977281, 177665, 5215745, 738561, 2672641, 3834113, 340481, 1850625, 424961, 1346049, 134657 };

2) Now we need to store each tick for above list. We are going store data in-memory so we can use Dictionary to make it simple.

// Store tick using key value pair
 static Dictionary<UInt32, List<Tick>> instrumentTickRaw = new Dictionary<UInt32, List<Tick>>();


public static void AddTickToInstrumentTicks(Tick TickData)
{
   try{
               // check here if already key exist then keep add data to list   
              if (instrumentTickRaw.ContainsKey(TickData.InstrumentToken))
                {
                    instrumentTickRaw[TickData.InstrumentToken].Add(TickData);
                }
              // else we will create new list and then add new key as below
              else
                {
                    var list = new List<Tick>();
                    list.Add(TickData);
                    instrumentTickRaw.Add(TickData.InstrumentToken, list);
                }
          }
          catch(Exception ex) 
         {
              Logger.Log(LogType.Error, ex.Message);
         }
          // TODO:->Logic here to process 1 min candle
}

As we are going to subscribe multiple stock we need to create such collection which will easy to maintain. So we used Dictionary. We check if already exits key then we do keep add data into it.
other wise we will make new entry only once then keep adding. So each key is going to make collection of Ticks respectively.
Using above collection we are able to access any stock data without taking much effort.
We need to call above method onTick() Event.


3) Subscribe Nifty50 stocks.

Here we already having below method with reference to Github code. We are going to mention our array of stocks.
We need pass our array to Subscribe Method and need set mode to Full.

 private static void initTicker()
        {
            ticker = new Ticker(MyAPIKey, MyAccessToken);

            ticker.OnTick += OnTick;
            ticker.OnReconnect += OnReconnect;
            ticker.OnNoReconnect += OnNoReconnect;
            ticker.OnError += OnError;
            ticker.OnClose += OnClose;
            ticker.OnConnect += OnConnect;
            ticker.OnOrderUpdate += OnOrderUpdate;

            ticker.EnableReconnect(Interval: 5, Retries: 50);
            ticker.Connect();

            // Subscribing to NIFTY50 and setting mode to FULL
            ticker.Subscribe(Tokens: intrument_tokent);
            ticker.SetMode(Tokens: intrument_tokent, Mode: Constants.MODE_FULL);
        }




4) Now we need to call our newly written method onTick Event.

  private static void OnTick(Tick TickData)
 {
     AddTickToInstrumentTicks(TickData);
 }


Using above code we can keep listen data from API. So we have basic structure to watch data.
We can apply different algorithms which are nothing but our strategies ge. OBR, MeanReversion etc.

Till now we have got raw data fron API we need to create 1 min candle using our Dictionary.
That we will see in our next blog.




Please share you thoughts on this, if you have any suggestion please add comment. so i will improve it while making next blog. This just educational blog. You can learn here how use kite api.







Comments

  1. Hi Sunil

    You are doing a wonderful job of helping people. Sincerely appreciate it. Is it possible to get N500 stocks data all at once? But need notbe tick data; Say 15m data.?

    thanks

    ReplyDelete
  2. Hi Sar Nifty,

    Thanks for appreciation.
    If we need to get data from zerodha for N500 of time frame 15 min. then its directly not possible but you can use historical api but it having some limitations.
    1) Historical api only provide data for maximum number of day is 60.
    2) It will only process 3 request/ sec. Otherwise it will throw exception “Too many requests”.
    This option will very time consuming.
    Instead of I would suggest using Tick data. Ticker service can easily handle 500 stocks. You can write background worker to convert your tick data to 15 min time.

    ReplyDelete
  3. I'm hoping you keep writing like this. I love how careful and in depth you go on this topic. Keep up the great work Best communication

    ReplyDelete
  4. This is also a primarily fantastic distribute which I really specialized confirming out Fashion

    ReplyDelete
  5. This is a good post. This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much. Keep up the good works Best prices for scrap gold

    ReplyDelete

  6. Informative blog keep sharing such a great information with us.
    Upcoming IPO

    ReplyDelete

Post a Comment

Popular posts from this blog

Getting Started with Zerodha API using C#

Getting Started with Zerodha API    In today's era, every thing is automated. now day's technology changing things rapidly. Many of us might have started imagination of automating the trades. This will be very interesting if you automate your trading action using your computer. If you planing to automate your trading, then here is some important information you may get.   Zerodha offering API for trading purpose, with the help of  API you can place order, cancel, modify. Zerodha Kite API V3 is very useful for automating trades. API is available in two forms one is Ticker which will give you tick data in market hours, another is historical API which will allow you to get old data.      To subscribe Kite API you must have Zerodha Dmat Account. Once you got your account with Zerodha login/Signup to  Kite Connect . You need to create app for API . Refer below image to create app. Fill the details. It will ask you to login your Kite Dmat account to link. So you can see this

How to generate the request_token for zerodha api using C#

Zerodha Kite Api How login Zerodha api using C# application? Hi Friends, recently i have started create application for zerodha api to make my own client for trading. Many of us have question how to login and trade using our custom C# application. I also face same problem i was looking solution for the same but  i was not able to fine exact way of doing it. I have gone through zerodha api document  According to them we need web view to make us to work this. Please follow be steps to get login token for API. Steps : 1) Creaet your login URL which is kite login + API key  eg.   var kiteURL="https://kite.trade/connect/login?v=3&api_key="    var apiKey="xpma3rmciorheitb"     2) Add web Browser to you web form set below properties             webBrowser1.AllowNavigation = true;             webBrowser1.ScriptErrorsSuppressed = true;             webBrowser1.Navigate(new Uri(kiteURL+apiKey)); 3) Now you need to register event "webBrowser1_