Auto Trading With Zerodha

#21
Here are the steps to go about:

------------------------------------------Part 1 Begins------------------------------------------

1. You will need to signup for a Kit Connect Developers account.
https://developers.kite.trade/signup

2. Once signup, you will need to deposit Rs.2000/- to your developer account. Only when you deposit it, you would be able to create your auto trading application.

3. Whie signing up, provide your Zerodha Trading Account (If you dont have zerodha account, download the forms from Here. and send the courier to Zerodha banglore office.)

4. Once developer account is created, you will need to create an app for your autotrading. (This option is available once you open a developer account)

5. To create an app, select "Publisher + Kite Connect" app.

6. You will need your own https web url (I think you might not need this if you are placing orders from your local machine. in that case enter "127.0.0.1" in redirect url field.)

7. Once an app is created, you will get your api_key and api_secret. This two would be used when you create your login session for auto trading.

----------------------------------------------Part 1 Complete-----------------------------------------------

-----------------------------------------------Part 2 Begins ------------------------------------------------------------

1. Flow of login
a. You will first login to your account using your api_key using the below url
https://kite.trade/connect/login?api_key=xxx
ex: https://kite.zerodha.com/connect/login?api_key=uhogbq3fio9viw33
b. This will redirect you to the "redirect url" you mentioned when you created your App.
c. Now, in the redirect url, you would receive "request token".
d. Use this "request token " along with your "api_key" and "api_secret" to generate "checksum".
e. This checksum is then used to send another http request which will give you access_token.
2. The "api_key" got from while creating the App and the "access_token" from the above http url is used in all subsequent request to place orders.
3. The php file that does this is attached in this post with name "login-success.php". This file needs to be uploaded to your webserver address which you have specified in your app creation.
4. I will attach all the images of my app creation to give you better idea and help.

-----------------------------------------------Part 2 Ends-------------------------------------------------------

Contents of login-success.php file
PHP:
<?php

function customError($errno, $errstr) {
  echo "There has been some error. Please clear browser cache and relogin.<br/>$errstr";
}

//set error handler
set_error_handler("customError");

echo "The request token received is : ".$_GET["request_token"];

$api_key = "your api_key";
$secret_key = "your secret key";

// Incoming from the redirect.
    $request_token = $_GET["request_token"];

    $checksum = hash("sha256", $api_key . $request_token . $secret_key);
echo "<br/>".$checksum;

$url = 'https://api.kite.trade/session/token';
$data = array('api_key' => $api_key, 'request_token' => $request_token, 'checksum' => $checksum);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);

$result = file_get_contents($url, false, $context);
if ($result == FALSE) { 
echo "<h1 style='color:red;'>Error while logging in.</h1>";
}else{
echo "<h1>You are logged in succesfully.</h1>";
}

//var_dump($result);
$out = json_decode($result, true);
echo $out['data']['user_name'];
echo $out['data']['access_token'];

?>
<body>
<input type="hidden" id="zerodha_token" value="<?php echo $out['data']['access_token']; ?>">
<input type="hidden" id="zerodha_user" value="<?php echo $out['data']['user_name']; ?>">
</body>

Various images to show how to register for an app in Kite Connect Developer Console.
---------------------------------------------------------------------------------







----------------------------------------------------------------------------------

Here is the php code to place order directly to your Zerodha account once you know your api_key and access_token.

PHP:
<?php
$api_key = "uhogbq3fio9viw33";
$access_token = $_GET["access_token"];
$tradingSymbol = $_GET["tradingsymbol"];
$exchange = $_GET["exchange"];
$transactionType = $_GET["transactionType"];
$orderType = $_GET["orderType"];
$price = $_GET["price"];
$quantity = $_GET["quantity"];
$triggerPrice = $_GET["triggerPrice"];
//echo $access_token."-".$tradingSymbol."-".$exchange."-".$transactionType."-".$orderType."-".$price."-".$quantity;

$url = 'https://api.kite.trade/orders/regular';
$data = array('api_key' => $api_key, 'access_token' => $access_token, 'tradingsymbol' => $tradingSymbol, 'exchange' => $exchange, 'transaction_type' => $transactionType, 'order_type' => $orderType, 'quantity' => $quantity, 'price' => $price, 'trigger_price' => $triggerPrice, 'product' => 'MIS', 'validity' => 'DAY');

// use key 'http' even if you send the request to https://...
$content = http_build_query($data);
//var_dump($content);
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded",
        'method'  => 'POST',
        'content' => $content,
    ),
);

$context  = stream_context_create($options);
$result = file_GET_contents($url, false, $context);
if ($result === FALSE) { echo "Error"; }
echo $result;
?>
really very great to have such patience to explain to ur friend. thanks a lot sir
 
#22
Hi guys,

I am using Zerodha's auto trading API.

...

Sourabh, this is immensely useful. Thanks a lot. Just a few questions.

1. Is this fully automated, right from retrieval of quotes to order placement and execution? Or does it just generate the buy/sell trigger and we have to go in and manually punch in the order?

2. Here's an example of what I am hoping to do. Retrieve quotes for a scrip/set of scrips every minute, do some analysis locally on my computer, and then issue a buy/sell trigger. Can this be done with these APIs? On a related note,, would the APIs allow me to retrieve and store historical prices (tick data or 1min interval) on my computer?

3. Does the Rs.2000 pm include data feed charges or do we have to pay for it separately?

4. How reliable has this system been, and what technical problems have you faced so far with it? For ex. does it give you problems when trading volumes are high (during open/close etc.)?

Thanks again.
 
#23
My answers inline

Sourabh, this is immensely useful. Thanks a lot. Just a few questions.

1. Is this fully automated, right from retrieval of quotes to order placement and execution? Or does it just generate the buy/sell trigger and we have to go in and manually punch in the order?
Yes fully automated

2. Here's an example of what I am hoping to do. Retrieve quotes for a scrip/set of scrips every minute, do some analysis locally on my computer, and then issue a buy/sell trigger. Can this be done with these APIs? On a related note,, would the APIs allow me to retrieve and store historical prices (tick data or 1min interval) on my computer?
Yes very much possible. I personally do this and place orders every minutes when my conditions are fulfilled.

3. Does the Rs.2000 pm include data feed charges or do we have to pay for it separately?
There is another 2000 charge per month for feed data

4. How reliable has this system been, and what technical problems have you faced so far with it? For ex. does it give you problems when trading volumes are high (during open/close etc.)?
Not much issue. Just that their bracket order sometimes have issue with tick value.

Thanks again.
 
#25

Raj232

Well-Known Member
#26
What I'm not clear about with Zerodha is ... "Why Zerodha requires its users to know programming ?"
Why is it that they do not provide a completed user interface instead of half-baked interface where trading tasks can be done rather than investors do programming tasks to get some functionality out of the software that Zerodha provides ?

Seems very strange.. traders need to study programming...
 

mastermind007

Well-Known Member
#27
What I'm not clear about with Zerodha is ... "Why Zerodha requires its users to know programming ?"
Why is it that they do not provide a completed user interface instead of half-baked interface where trading tasks can be done rather than investors do programming tasks to get some functionality out of the software that Zerodha provides ?

Seems very strange.. traders need to study programming...
Every strategy at its inception is the biggest ever million dollar idea which ironically leaves many strategy inventors in pain because
(a) they cannot write on their on (b) they cannot bring themselves up to level ot telling some programmer their secrets,
because secrets once shared will no longer be a secret.

Platform like Amibroker fit into that sweet spot where theoretically one can write own strategy and test it.

Although the amibroker platform by itself is excellent, its market place is innundated with a very unique kind of entropy/noise.

Every :confused: rookie in amibroker gets bombarded by thousands of malicious, bogus and untested (but good looking :) ) AFLs available for FREE!!! :D
 
Last edited:

mastermind007

Well-Known Member
#28
This is a loophole in the system. "Traders are not trading automatically but they are 'testing' their own developed trading platforms for real trades."

Basically auto trading for retailers is not allowed in india.. If one wishes to do the same then he has to take exchange approvals and also strategy needs to be approved from CA,exchanges and he needs to get dealer terminal to place auto trades and what not.
part of testing he needs to test how platform behaves for real trades. So zerodha give order firing access to only developer's zerodha account and tells
So zerodha first requires you to sign up for 'developer account' before using kite connect APIs. then you need to create an app in their system and then you can use their APIs. Zerodha people show it as you are a platform developer(Like Pi,Kite,Nest Etc) who is developing trading platform and as a exchange that once you are OK with your platform, You(developer) will go to exchanges to take all approvals before releasing this to public.

And as there is no time limit for testing phase, You can remain in testing mode for anytime you want firing real orders from your account.
Berry Berry Clever
 
#29
Perfectly explained.

This is a loophole in the system. "Traders are not trading automatically but they are 'testing' their own developed trading platforms for real trades."

Basically auto trading for retailers is not allowed in india.. If one wishes to do the same then he has to take exchange approvals and also strategy needs to be approved from CA,exchanges and he needs to get dealer terminal to place auto trades and what not.

So zerodha first requires you to sign up for 'developer account' before using kite connect APIs. then you need to create an app in their system and then you can use their APIs. Zerodha people show it as you are a platform developer(Like Pi,Kite,Nest Etc) who is developing trading platform and as a part of testing he needs to test how platform behaves for real trades. So zerodha give order firing access to only developer's zerodha account and tells exchange that once you are OK with your platform, You(developer) will go to exchanges to take all approvals before releasing this to public.

And as there is no time limit for testing phase, You can remain in testing mode for anytime you want firing real orders from your account.
 

Raj232

Well-Known Member
#30
This is a loophole in the system. "Traders are not trading automatically but they are 'testing' their own developed trading platforms for real trades."

Basically auto trading for retailers is not allowed in india.. If one wishes to do the same then he has to take exchange approvals and also strategy needs to be approved from CA,exchanges and he needs to get dealer terminal to place auto trades and what not.

So zerodha first requires you to sign up for 'developer account' before using kite connect APIs. then you need to create an app in their system and then you can use their APIs. Zerodha people show it as you are a platform developer(Like Pi,Kite,Nest Etc) who is developing trading platform and as a part of testing he needs to test how platform behaves for real trades. So zerodha give order firing access to only developer's zerodha account and tells exchange that once you are OK with your platform, You(developer) will go to exchanges to take all approvals before releasing this to public.

And as there is no time limit for testing phase, You can remain in testing mode for anytime you want firing real orders from your account.
Dear Denzo, Thank you very much for the detailed explanation.

Still however, i'm unable to see any meaningful posts on step-by-step integration of Nest/Pi/Kite with AmiB eg. Feeding quotes to AmiB without 3rd party involvement and firing orders from AmiB to Nest/Pi/Kite.

Probably, even after a couple of years, this seems to be called stages of infancy.