Zerodha Algo Trading, Java, How to connect to kiteconnect?

 Here I am going to show you how to integrate java program to zerodha kite connect. Let us see each step in detail.

STEP 1: KITE CONNECT SIGN UP

  • Goto https://kite.trade/
  • Click Sign up
  • Enter the information and submit

Creating App Zerodha Kite

STEP 2: APP CREATION

  • Login into https://kite.trade/
  • Click create new APP
  • Enter any name in App name field
  • Insert your Zerodha client id in “Client ID” Field
  • Enter any url in Redirect URL Field. eg:-https://kite.zerodha.com/
  • Click create to create the app.
App Creation in Zerodha Kite

Activate the APP by paying 2000 rs.

Page that looks like below one is displayed

App Creation in Zerodha Kite

Three things that we need to integrate our code to Zerodha

  • API Key ( Found at the app page above)
  • API secret ( Found at the app page above)
  • Request Token ( We need to generate this separately)

Before generating request token we need to add TOTP in Zerodha

HOW TO ADD TOTP IN ZERODHA

  • Go to play store download Authy App.
  • Open the App add complete the mobile verification
  • Go to Zerodha profile
  • Click Password and Security
How to enable TOTP in Zerodha

Next, click on Enable 2-step OTP

TOTP Authentication in Zerodha
  • Click Add Account in your APP
  • Scan this QR Code and click Add Account.

So you have completed setting of totp in zerodha.

Please click here for more information about setting TOTP.

STEP 3: GENERATE REQUEST TOKEN

https://kite.zerodha.com/connect/login?api_key=dshjfsgdfgfd7852

In the above link replace yellow test by your API Key found at the app.

Copy the link into browser you will be redirected to an URL with request token.

Copy the request token from the above redirected URL.

Next we will see how to code in Java to connect to Zerodha.

STEP 4: CODING

Download kiteconnect from Official Site

Add the jar into our project libraries.

If you are using netbeans IDE Go to-> Project properties -> Select Libraries -> Add Jar/Folder -> select the jar and submit

How to Connect Java to Zerodha

PROGRAM TO CONNECT TO ZERODHA

import com.zerodhatech.kiteconnect.KiteConnect;
import com.zerodhatech.kiteconnect.kitehttp.exceptions.KiteException;
import com.zerodhatech.kiteconnect.utils.Constants;
import com.zerodhatech.models.*;
import com.zerodhatech.ticker.*;
import java.io.IOException;
public class mainFrame{
KiteConnect kiteSdk;
public void connectZerodha()
{
String req_token="CODE FROM PREVIOUS STEP" ;     
String api_key="dsklhsafiofsafo6";
String sec_key="fdjkjfdiiinfdijidjfidsjfij ifdjf";
kiteSdk = new KiteConnect("dsklhsafiofsafo6");
kiteSdk.setUserId("LP8865");
User users = null;
         try {
             users = kiteSdk.generateSession(req_token, sec_key);
         }  catch (IOException ex) {
          
        } catch (KiteException ex) { 
        Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JSONException ex) {
        Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
kiteSdk.setAccessToken(users.accessToken);
kiteSdk.setPublicToken(users.publicToken);
}
public static void main(String[] args) {
        new mainFrame().connectZerodha();
    }
}

So we have connected our code Zerodha kite.

Next we will see how to print the profile information

Profile profile = kiteSdk.getProfile();
System.out.println(profile.userName);

So we have learned how to integrate Java with Zerodha. If you have any queries please let me know in comments . Alog Trading Python

Thank you.

Mukesh Mohanan.


Comments

Popular posts from this blog

Create Option Chain using PYTHON.

How to Place Order in Zerodha using Python

How to place order in zerodha when RSI crosses below 30? Algo Trading Python