Git Product home page Git Product logo

cppkiteconnect's Introduction

cppkiteconnect

Unofficial headers only c++ client for communicating with the Kite Connect API.

It is based on official zerodha dotnetkiteconnect.

Kite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (WebSockets), and more, with the simple HTTP API collection.

Licensed under the MIT License.

Documentation

Requirements

  • GCC 4.9.x (std::regex supported) or Visual Studio 2017 / 2015 update 3 (on windows): c++11 compliant compiler.

Dependent Libraries

All these can be added (sourced & compiled ) wrt your platform by using Microsoft vcpkg. You must give it a try. Note i have build this code only on windows machine, but it should compile on Linux

Installation Using vcpkg

On Windows:

  • Install git
  • Install Visual Studio 2017
  • Install vcpkg Remember that in this (vcpkg) dir all the libraries will be built, so this drive should have sufficient space.
  • Open command prompt or powershell prompt and change dir to vcpkg dir
  • Install dependent libraries : vcpkg.exe install cpr fmt nlohmann-json boost
  • Git clone or download cppKiteConnect.
  • Start a new C++ project in Visual Studio
  • Open example directory and build

On Linux:

API usage

#include <string>
#include <map>
#include <vector>
#include <iostream>
// Import library
#include <KiteConnect.h>
#include <utils.h>
#include <cpr/cpr.h>

int main()
{
   // define a proxy
   const cpr::Proxies localProxy{
                           {"http", "http://192.168.100.6:8080"},
                           {"https", "http://192.168.100.6:8080"}
   };
   /*
   Connection failure error looks like
   -------------------------------------------------------------------------
   Error occurred: Session libCurl error
   error code :1
   error message :Failed to connect to api.kite.trade.com port 443: Timed out
   -------------------------------------------------------------------------
   */

   std::string  myApiKey      /* = user filled data */;
   std::string  RequestToken  /* = user filled data */;
   std::string  MySecret      /* = user filled data */;


   try {
      // Create a client instance
      // using apiKey. Enabling Debug will give logs of requests and responses
      kite::KiteConnect kite (myApiKey, true,
                              "/*empty access token*/", "/*empty url*/"
                              /*localProxy if required*/);

      // Collect login url to authenticate user. Load this URL in browser or WebView.
      // After successful authentication this will redirect to your redirect url with request token.
      std::string url = kite.GetLoginURL();

      // Collect tokens and user details using the request token
      json user = kite.GenerateSession(RequestToken, MySecret);

      std::string MyAccessToken = user.at("result").at("data").at("access_token");
      std::string MyPublicToken = user.at("result").at("data").at("public
      token");

      // Initialize Kite APIs with access token
      kite.SetAccessToken(MyAccessToken);

      // Set session expiry callback. Method can be separate function also.
      kite.SetSessionExpiryHook( [](){ std::cerr << "\n Need to login again \n"; } );

      // Example call for functions like "GetHoldings"
      json holdings = kite.GetHoldings();
      std::cout << holdings.dump(4) << std::endl; // json dump with 4 space width

      // Example call for functions like "PlaceOrder"
      json response = kite.PlaceOrder(
         kite::EXCHANGE_CDS,
         "USDINR17AUGFUT",
         kite::TRANSACTION_TYPE_SELL,
         1,
         64.0000,
         kite::ORDER_TYPE_MARKET,
         kite::PRODUCT_MIS
      );
      std::cout << std::endl << response.dump(4) << std::endl;
      std::cout << "Order Id: " + response.at("result").at("data").at("order_id").get<std::string>();
   }
   catch (const std::runtime_error& re)
   {
      // speciffic handling for runtime_error
      std::cerr << "Runtime error: " << re.what() << std::endl;
   }
   catch (const std::exception& ex)
   {
      // speciffic handling for all exceptions extending std::exception, except
      // std::runtime_error which is handled explicitly
      std::cerr << "Error occurred: " << ex.what() << std::endl;
   }
   catch (...)
   {
      std::cerr << "\nGot Exception\n";
   }
   return 0;
}

Kite ticker usage - websocket [ not yet implemented ]

Examples

Check examples folder for more examples.

Run unit tests

cppkiteconnect's People

Contributors

mastro-geppetto avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

vikchopde

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.