Auto Trading With Zerodha

#1
Hi guys,

I am using Zerodha's auto trading API.

I can successfully place the orders in my zerodha account.

You would need the following things to do so.

1. An account with Zerodha.
2. An active API programming / developer account. (costs 2000 rs per month)
3. An https connection to your website or local machine. (i am using https connection to my web server.

I am leaving next 1-3 post blank to update the code in detail how to do so.
I will update the same in next few hours/days.

I would try to help answer your queries here if any related to programming for Zerodha Auto Trading.

I have tried to explain in best way i can about the process in the below posts. The process might look little complex at beginning, but once you are used to it, you will find it simple.
So decide your strategy, start placing order directly and automatically into your zerodha account.

And If you would like to open Zerodha account, enter your details in the below form and someone from Zerodha would call to assist you.
Zerodha Form

Direct Form Downloads

Youtube video to help you open your Zerodha account.
https://www.youtube.com/watch?v=yEqz0ak6eJs
 
Last edited:
#2
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;
?>
 
Last edited:

headstrong007

----- Full-Time ----- Day-Trader
#3
It'll be great help to us..if u can guide us..how to automate simple strategies like ema cross over.. stoch cut.. macd cut or combination of any of two..
Also mark the exact code area, where we can change the value of indicators.. when required..like stochastic 5-3-3 to 15-3-3 and how to apply it on diff time frames easily, like 3/5/15 min according to demand of market.
We need dynamic automation..where we can change indicator value and time fame easily and apply them quickly...
********
Note u can't edit a post after 24 hrs, so u have to fill it within that time limit..
Can use big post header with serial #01, #02 for related post.. we can find it easily.. IMO..
It is going to be one of the most interesting thread in TJ..
 

cloudTrader

Well-Known Member
#4
As Headstrong mentioned the blank post will not be available for editing after 24 hours you will need to edit it within that period of time.

Thanks for your contribution. :thumb:
 
#7
Hi,

I dont think you can use it with any other software.

This is basically for programmers who want to code their own strategy and place orders through their programs.

If you are not a programmer, then this might not be of that help to you.

You can instead try Pi bridge plugin from zerodha for what you want to try.
 

Aman1

Well-Known Member
#8
Saurabh I think its possible. Here are the replies from Nitin on their website

Q.
Zerodha/Nithin
Can this API get integrated with a charting s/w? or it is a stand alone API to trade from a pc or website?
Please excuse my ignorance, I don’t know any alphabet of programming
Reply:
"You can integrate with a charting s/w, u can also link excel/python etc to Kite our web based platform".

Q. It’s good for a programmer only, no where is shared any code for Ninjatrader to kite or Pi.

Nitin Reply:

"Using NT requires programming skills, any average programmer should be very easily able to connect NT using Kite connect. Currently we don’t have the bandwidth to work on third party tools, but we are soon going to start inviting programmers to start building these plugins"

http://zerodha.com/z-connect/tradezerodha/kite/kite-connect-apis-for-programmatic-access
 

lemondew

Well-Known Member
#10
@ saurabh. Thanks for php code

Also a small code where you can Stream/read scripts values will be good.

I also have 1 concern. If I wish to track 20 scripts. lets say I wish to track the option values of 20 scripts. I wish to read them continuosly. Would I have to open 20 handles or I will get them in 1 query.