Kite Connect API and PiBridge For Beginners

Hi Traders,
This explanation is for Beginners who want to know the basic about PiBridge and Kite Connect API.
There are lot many confusion about Kite Connect API and Pi Bridge and many confusing the API with back-test/analysis.This 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 chennai based.
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 that 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 TV”
FRIEND : “Yaa, Sure”

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

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

YOU : “How many years 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?
What is the Price of TV?
Price?
Cost of TV?
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 output the price.

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 your 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 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(Exch TrdSym 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 Preceisly uses Comman language). Like HTTP API’s

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.

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 retreive information from kite Server.
Example:-
Margin API
Format/Function/Method/Procedure :- https://api.kite.trade/user/margins/equity?api_key=xxx&access_token=yyy"
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 that API.
So You need to refer the API doc thoroughly before using.

API’s are nothing to do with your back-test 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.

So Do not confuse this API’s with your back-test/Analysis/Algo etc…

Do I Need to use API…?

It’s all depends on your requirement. until unless you have compelling reason to use API, don’t use API.
Example:-
If you are a investor, You buy stocks based on TV news, 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 Tgt at 0.5% and SL 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 Window,
Fill Exch, Trd Sym, Qty, Order Type, Product etc and click ok.
Now Again open the Order window, Fill Exch, Trd Sym, Qty, Order Type, Product etc and click ok to place Tgt Order
Now Again open the Order window, Fill Exch, Trd Sym, Qty, Order Type, Product etc and click ok to place SL Order

Now keep on watching the Order status of Tgt and SL, If target hits,
Open the Order Window and cancel the Pending SL Order.

The above is some king 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 qty you put 5000 qty like this.So ultimately, you fail and you will think “what if some thing will do all this in one click or something like that”

Yes, “That something” is API only.
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 or Pain to do manually.

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

PiBridge Communication Layout:

Kite API Communication Layout:

25 Likes

Thanks for the description. In the slide, you can make a box to show which are the things running at users’ computer. eg., in the first slide, upto PI runs on the users machine. and In the second only upto KITE API. Further, if you can provide vendors name that provide Ninja to PI bridge connection, it would help.

thanks again.

Very Nicely explained. thanks for sharing such a useful information

thanks for info for beginner like me

Excellent explanation and easy to understand

so can i do fully automatically using API right?

Assume I have API Key, api secret and request token. Like above, please give me an example to get checksum,sha256 stuff… and hard http line to get access token and further save it as some variable in Amibroker…

Hats off to this guy. What an article/explanation. Thanks a millions.