Kite Connect API and PiBridge for Beginners

Kite Connect API and PiBridge are wonderful products of Zerodha. Both have limitless capabilities and are boon for retail traders.This article is for Beginners who want to know the basic about PiBridge and Kite Connect API.There are a lot of confusions about Kite Connect API and Pi Bridge and many confusing the API with back-test / analysis.This article is an attempt to bring clarity on PiBridge and Kite Connect API, what are they and their purpose.

API – A Layman Example

Your Friend is located somewhere in Mumbai and You are sitting in Chennai.Your Friend bought a TV last week and you also planning to buy the same kind of TV.So You need certain information about the TV your friend bought.

How You get the information from Your friend who located in Mumbai.You will call him on mobile and ask the information You need, like below

YOU : “Hey Friend,How do You do?”
FRIEND : “Im Fine, what about you”

YOU : “Im good.BTW, I just need some information”
FRIEND : “What of Kind of Information?”

YOU : “Last Week You Bought a TV..?”
FRIEND : “Yes”

YOU : “Im also Planning to Buy the Same Kind of TV.Need info about that”
FRIEND : “Yaa, Sure”

YOU : “Which Company Brand, the TV is?”
FRIEND : “LG”

YOU : “What is the Price?”
FRIEND : “Rs26000/-“

YOU : “How many years of warranty?”
FRIEND : “3 Years”

YOU : “Is there any discount offered?”
FRIEND : “Yes. 5%”

YOU : “Is Installation Free?”
FRIEND : “Yes”

YOU : “Ok. Thanks for the Information. Bye TC”
FRIEND : “Bye. TC”

******Chat Ended*****

As you can see,You are requesting some information and your friend providing the same.This is the Way how API works.One Program requests data and other provides.So API communication involves Two or more programs or Computer.

As humans are intelligent,one can easily understand what the other person is requesting irrespective of how he asked or requested(Provided both are using same language).

Example:-
In the above conversation, For getting the price of the TV,
You can use What is the Price? or What is the Price of TV? or Price? or Cost of TV? or Cost? etc etc…

Your friend can easily understand what your are asking and will answer correctly since we are intelligent (This is the only things differentiate us from the machines).Unlike us, Computers are not intelligent (though they are fast, still need our guidance), they cannot understand everything on their own and they only understand things which are programmed in to them.

Say for example:-
A Computer is programmed to say the price of the TV when some one type “Price of TV“.

So if you type, “Price of TV” , the computer will output the price.If you type “Price ofTV” or “PriceofTV” or etc etc other than the specified format either in lowercase or upper case whatever,Computer will not understand your request and it will not give price information.

Same way if two computers or program communicates with each other, one has to send request in the format which can be understood by the other computer and the other computer will send the requested information in the format which can be understood by the first computer.

So Basically, API will have formats to request specific information.If you need information, you need to send your request in the specific format supported by the API, so that the other program can understand what you are requesting and provide the necessary information or do necessary action.

In a Nutshell:
To get information/data from another Program or computer(Pi or Kite Server),You first need to communicate with that Program or computer

To communicate with another program or computer,You first need to check whether the program or computer accepts/supports communication from external.

If the another Program or computer accepts/supports communication from external, then you need to find-out what type of communication is supported by the another program or computer and in which standard format the communication is to be made so that the another program or computer understands your communication and it is the API which tells you all the info about the communication like how to communicate and the format, what are the information retrieved from the program or computer and what type of request are processed/supported by the program or computer.

Here is the Wiki Technical Explanation

“In computer programming, an Application Programming Interface (API) is a set of subroutine definitions, protocols, and tools for building application software. In general terms, it is a set of clearly defined methods of communication between various software components. A good API makes it easier to develop a computer program by providing all the building blocks, which are then put together by the programmer. An API may be for a web-based system, operating system, database system, computer hardware or software library. An API specification can take many forms, but often includes specifications for routines, data structures, object classes, variables or remote calls. POSIX, Microsoft Windows API, the C++ Standard Template Library and Java APIs are examples of different forms of APIs. Documentation for the API is usually provided to facilitate usage”

Usage Example:Step-wise
Suppose You want to Place a Order in Pi from AmiBroker.

  1. Check whether any API is available to communicate with Pi (Yes, PiBridge. You can use PiBridge to have communication between AmiBroker and Pi)
  2. Check whether the API can be used from AmiBroker (Yes, PiBridge is a COM API which can be used from AmiBroker)
  3. Check whether the Order Placement Request is Supported by PiBridge (Yes.. It has PlaceOrder Method)
  4. Check what are the details needs to be passed to the method(Exchange, Trade Symbol etc)
  5. Now, Just call the PlaceOrder Function from amibroker by passing all required parameters correctly
  6. Now the Order Request, you sent from amibroker will reach Pi and Pi will analyse all the details and if everything is correct then it will place the order and return order status to AmiBroker.

There are basically two kinds of API’s
API’s which are specific to certain programming language.Like C++, Visual Basic, Python.
API’s which are programming language independent(Most Precisely uses Common language) like HTTP API.

For Example:-
If your friend is Telugu and You are Tamil,then if you ask in Tamil, he will not able to understand what you are asking, but if both of you are Tamil, then both can understand (This is Language Specific API).

Suppose, You Tamil & Your Friend Telugu, But both can understand English (Common language like HTTP),So both can communicate using English.(This is language independent API or Common Language API).

Most of the Computers/Programs/Languages will understand the HTTP language.So any API built on HTTP, most probably work on all computer/Program/Languages.

Kite Connect is a HTTP API and is independent of Programming Language.So you can use this Kite Connect API to communicate with Kite Server from virtually any programming language/any OS/Program and request/retrieve any kind of information that Kite Connect Supports like Place Order, Check Order Status etc.

PiBridge is language specific API, that can be called from windows and windows supported languages only.

In a nutshell:
Kite Connect is a HTTP API and provides methods/Procedures/Protocols to communicate with Kite Server and request or retrieve information from kite Server.
Example:- Margin API
Format/Function/Method/Procedure:-https://api.kite.trade/user/margins/equity?api_key=xxx&access_token=yyy4"
Provides :- Margins available

PiBridge is a COM API and provides methods/Procedures/Protocols to communicate with Pi and request or retrieve information from Pi.
Example:- Funds API
Format/Function/Method/Procedure :- GetFunds()
Provides :- Funds available

Normally, the API Provider will also provide the documentation of the API, without doc, its almost not possible to know what information can be requested or retrieved through the API. So You need to refer the API doc thoroughly before using.

API’s are nothing to do with your back-tested result or technical analysis.These are just meant for communicating with other program(Kite Server for Kite API, Pi for PiBridge) and request/retrieve information like Placing Order or checking Order Status etc.So Do not confuse this API’s with  back-test/Analysis/Algo etc.

Do I Need to use API…?

It’s all depends on your requirement.
Example:-
If you are a investor, You occasionally buy stocks on fundamental analysis, then no need to use API.

Another:-
Suppose, You are calculating certain Buy/Sell level using some formula, only want to trade using when this levels break and place a target at 0.5% and stoploss at 0.5%.If You want to execute the above,You need to keep on watching the Market watch for Breakout of Price or Set a Price Alert.Once Price Breaks level or Price alert is displayed,Open a Buy Order Window,Fill Exchange, Trade Symbol, Quantity, Order Type, Product  Type etc and press submit button to place order.Now Again open the Order window, Fill Exchange, Trade Symbol, Quantity, Order Type, Product  Type etc and press submit button to place the target order and repeat the step again to place the stoploss order.

Now keep on watching the order status of target and stoploss. If target hits then, open the order book and cancel the pending stoploss order or vice versa.

The above is some kind of irritating or you feel stressed and you are always tied to the computer / Trading Platform.If you want to do same kind of thing for multiple symbols say 15 or 20, suddenly you get bored, irritated, stressed and tired.That will also lead to some fat fingering like instead of buying you sold or instead of 50 quantity you put 5000 quantity like this.So ultimately, you fail and you will think “what if some thing will do all this in one click or with similar functions”

Yes, That’s where API is coming to the picture.
You can do the above repeated things in a single click or automatically for you with the help of API and all will happen in matter of milliseconds (Unlike Manual, where You might have took 3-10 mins to place Order in 10 or 15 symbols)

The above is just an example to understand.

With API, You can automate repeated works, works which requires fast execution and any other complex things that is impossible to do manually.

With API, You are no longer tied to Computer/Trading Platform.That doesn’t mean that you never need to check the computer, Periodic Supervision is must. Because Machines are still machines, they are not humans.

ChokS

ChokS

.Net Programmer. AFL Coder. Author of KiteDotNet & KiteXL.

4 Comments

  • yogesh k says:

    Dear sir
    can we connect from amibroker to kiteconnect .will you please elborate on the process or is this info available anywhere.

    • Admin Admin says:

      yes, you can connect amibroker with kite connect api, also you can get live data feeds into amibroker and can place orders as well.
      you can contact hello@howutrade for more info.

  • Ganesh says:

    Hi
    Please tell is my underatdning is correct.
    Once i write any code in Kite Connect then the orders will placed directly through zeordha Kite ?

Leave a Reply

Your email address will not be published.