Auto Trading With Zerodha

#41
Dear Sourabh, I'm sure you have great influence with Zerodha. To hasten the process, I would suggest that Zerodha should open up to voluntary developers as Zerodha faces resource crunch and at the same time has huge pending backlogs of promises :)

Bangalore being an IT city could easily find approx 100 developers (voluntary) who could build on pending projects over short periods of time.

Just a small suggestion that Zerodha should seriously consider.
Hi Raj,

Yes, I am an official Zerodha Partner for Mumbai. I do algo programming on my strategies, auto trading, account opening and similar things.

I was looking for such API since last 5-6 years and zerodha made that happen.

Now, the only thing is strategy, if we have something like sure shot, we can build an ATM machine :)

Regarding your query on voluntery programmers, I dont think Zerodha would ever do. ITs not and open source company. Even for API, they are charging 2000/- per month.

Thanks,
Sourabh.
 
Last edited:
#42
Dear Sourabh,

Thanks for your valuable post. I have the PHP engine be installed on my local machine already.

I'm trying to connect Kite to Amibroker for the data feed. Can you figure out where to get started.

Thanks in advance.
Hi Raj,

Read my first post, i have given my full PHP code for login flow.
Now, You need a server and domain name. With local machine, you might just be able to do testing, but not live trading. As they need some valid domain url in their flow.
 

Raj232

Well-Known Member
#43
Hi Raj,

Yes, I am an official Zerodha Partner for Mumbai. I do algo programming on my strategies, auto trading, account opening and similar things.

I was looking for such API since last 5-6 years and zerodha made that happen.

Now, the only thing is strategy, if we have something like sure shot, we can build an ATM machine

Regarding your query on voluntery programmers, I dont think Zerodha would ever do. ITs not and open source company. Even for API, they are charging 2000/- per month.

Thanks,
Sourabh.
Hi Raj,

Read my first post, i have given my full PHP code for login flow.
Now, You need a server and domain name. With local machine, you might just be able to do testing, but not live trading. As they need some valid domain url in their flow.

Thanks Saurabh, I read your first post about login, however login itself is of no use as the main task is trading the correct BUY & SELL signals. So what essentially you are saying is 1 step completed but there are approx 19 still to go.:thumb:

Apart from login, there is, selection of correct scrips, choosing an appropriate strategy, order management, position sizing, money management, etc., each of which needs a lot of programming to make it work .. :)

For it to become and ATM ... probably a lot more needs to be done, what you are mentioning is already available with PI + GDFL + NEST plus + strategy + etc.. however no one yet has got the ATM machine :D:D

.. and every step forward seems to be more of a programming task :p:p than a trading strategy... :clap:

.. but a good start anyways.. thanks:clapping:
 

Raj232

Well-Known Member
#44
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;
?>
SOURABHji .. any update to your APIs ?


I'm sure there are many developers here who are traders as well:

How about working on a piece of software which can place orders in Zerodha KITE (web based) (using FULL Automation ..i.e no manual intervention, except the 1st dummy order for the 1st transaction of the day) from Amibroker AFL generated signals:

1. Kite connects to Amibroker ver 6 via HTML5 technology for feeds

2. Amibroker has its own AFL (Amibroker Formula Language) generating buy/sell signals.

3. The Buy/Sell orders are placed into Zerodha's KITE automatically (web login would already be done) from Amibroker as and when signals are generated.
 
#46
No idea buddy. I dont use Amibroker, nor do i know its linking with Kite.

Dear Sourabh,

Thanks for your valuable post. I have the PHP engine be installed on my local machine already.

I'm trying to connect Kite to Amibroker for the data feed. Can you figure out where to get started.

Thanks in advance.
 
#47
Thanks Saurabh, I read your first post about login, however login itself is of no use as the main task is trading the correct BUY & SELL signals. So what essentially you are saying is 1 step completed but there are approx 19 still to go.:thumb:

Apart from login, there is, selection of correct scrips, choosing an appropriate strategy, order management, position sizing, money management, etc., each of which needs a lot of programming to make it work .. :)

For it to become and ATM ... probably a lot more needs to be done, what you are mentioning is already available with PI + GDFL + NEST plus + strategy + etc.. however no one yet has got the ATM machine :D:D

.. and every step forward seems to be more of a programming task :p:p than a trading strategy... :clap:

.. but a good start anyways.. thanks:clapping:
Raj, my post was more aimed to help programmers looking for auto trade programming.

Also, I have my auto trading running on my server everyday where all filteration, buy/sell logic is programmed.
 
#48
Its about quality of program that your write and the strategy.

After auto trading program, i hardly trade manually.
I make my server run all trades on my behalf.
To test any new program or strategy, we can always do auto trades in small quantities.

Auto trading if done properly is a great tool for programmer.
But, true as you mentined it needs real courage.
 
#50
Its about quality of program that your write and the strategy.

After auto trading program, i hardly trade manually.
I make my server run all trades on my behalf.
To test any new program or strategy, we can always do auto trades in small quantities.

Auto trading if done properly is a great tool for programmer.
But, true as you mentined it needs real courage.
Would you mind sharing your PnL? And are you using your own computers for this or cloud etc.?

...
But, true as you mentined it needs real courage.
And deep pockets. Zerodha charges 4k per month for providing the auto trading facility. No idea why it is so expensive.