CPPKiteConnect
kite.hpp
1 /*
2  * Licensed under the MIT License <http://opensource.org/licenses/MIT>.
3  * SPDX-License-Identifier: MIT
4  *
5  * Copyright (c) 2020-2022 Bhumit Attarde
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20  * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
22  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23  * USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #pragma once
26 
27 #include <cstdint>
28 #include <functional>
29 #include <string>
30 #include <unordered_map>
31 #include <vector>
32 
33 #include "cpp-httplib/httplib.h"
34 
35 #include "responses/responses.hpp"
36 #include "utils.hpp"
37 
38 namespace kiteconnect {
39 
40 using std::string;
41 namespace kc = kiteconnect;
42 namespace utils = kc::internal::utils;
43 
48 class kite {
49 
50  public:
56  explicit kite(string apikey);
57 
58  // api
59 
65  void setApiKey(const string& arg);
66 
72  string getApiKey() const;
73 
83  string loginURL() const;
84 
93  void setAccessToken(const string& arg);
94 
100  string getAccessToken() const;
101 
114  const string& requestToken, const string& apiSecret);
115 
131  bool invalidateSession();
132 
133  // user
134 
144 
153 
163  margins getMargins(const string& segment);
164 
165  // orders
166 
177  string placeOrder(const placeOrderParams& params);
178 
199  string modifyOrder(const modifyOrderParams& params);
210 
224  string cancelOrder(const string& variety, const string& orderId,
237  const string& parentOrderId = "");
238 
247  std::vector<order> orders();
248 
259  std::vector<order> orderHistory(const string& orderId);
260 
269  std::vector<trade> trades();
270 
281  std::vector<trade> orderTrades(const string& orderId);
282 
283  // gtt
284 
295  int placeGtt(const placeGttParams& params);
296 
305  std::vector<GTT> triggers();
306 
316  GTT getGtt(int triggerId);
317 
328  int modifyGtt(const kc::modifyGttParams& params);
329 
340  int deleteGtt(int triggerId);
341 
342  // portfolio
343 
352  std::vector<holding> holdings();
353 
363 
375  bool convertPosition(const convertPositionParams& params);
376 
377  // market
378 
392  std::vector<instrument> getInstruments(const string& exchange = "");
393 
409  std::unordered_map<string, quote> getQuote(
410  const std::vector<string>& symbols);
411 
427  std::unordered_map<string, ohlcQuote> getOhlc(
428  const std::vector<string>& symbols);
429 
445  std::unordered_map<string, ltpQuote> getLtp(
446  const std::vector<string>& symbols);
447 
458  std::vector<historicalData> getHistoricalData(
459  const historicalDataParams& params);
460 
461  // MF
462 
473  string placeMfOrder(const placeMfOrderParams& params);
474 
485  string cancelMfOrder(const string& orderId);
486 
495  std::vector<mfOrder> getMfOrders();
496 
507  mfOrder getMfOrder(const string& orderId);
508 
517  std::vector<mfHolding> getMfHoldings();
518 
530 
541  string modifyMfSip(const modifyMfSipParams& params);
542 
553  string cancelMfSip(const string& sipId);
554 
563  std::vector<mfSip> getSips();
564 
575  mfSip getSip(const string& sipId);
576 
585  std::vector<mfInstrument> getMfInstruments();
586 
587  // margins
588 
600  std::vector<orderMargins> getOrderMargins(
601  const std::vector<marginsParams>& params);
602 
616  const std::vector<marginsParams>& params, bool considerPositions);
617 
618  private:
619  static string encodeSymbolsList(const std::vector<string>& symbols);
620 
621  string getAuth() const;
622 
623  template <class Res, class Data, bool UseCustomParser = false>
624  inline Res callApi(const string& service,
625  const utils::http::Params& body = {},
626  const utils::FmtArgs& fmtArgs = {},
627  utils::json::CustomParser<Res, Data, UseCustomParser>
628  customParser = {});
629 
630  const string version = "3";
631  const string root = "https://api.kite.trade";
632  const string loginUrlFmt =
633  "https://kite.zerodha.com/connect/login?v=3&api_key={api_key}";
634  const std::unordered_map<string, utils::http::endpoint> endpoints {
635  // api
636  { "api.token", { utils::http::METHOD::POST, "/session/token" } },
637  { "api.token.invalidate",
638  { utils::http::METHOD::DEL,
639  "/session/token?api_key={0}&access_token={1}" } },
640  // user
641  { "user.profile", { utils::http::METHOD::GET, "/user/profile" } },
642  { "user.margins", { utils::http::METHOD::GET, "/user/margins" } },
643  { "user.margins.segment",
644  { utils::http::METHOD::GET, "/user/margins/{0}" } },
645  // order
646  { "order.info", { utils::http::METHOD::GET, "/orders/{0}" } },
647  { "order.place", { utils::http::METHOD::POST, "/orders/{0}" } },
648  { "order.modify", { utils::http::METHOD::PUT, "/orders/{0}/{1}" } },
649  { "order.cancel", { utils::http::METHOD::DEL, "/orders/{0}/{1}" } },
650  { "order.cancel.bo", { utils::http::METHOD::DEL,
651  "/orders/{0}/{1}?parent_order_id={1}" } },
652  { "order.trades", { utils::http::METHOD::GET, "/orders/{0}/trades" } },
653  { "orders", { utils::http::METHOD::GET, "/orders" } },
654  { "trades", { utils::http::METHOD::GET, "/trades" } },
655  // gtt
656  { "gtt", { utils::http::METHOD::GET, "/gtt/triggers" } },
657  { "gtt.place", { utils::http::METHOD::POST, "/gtt/triggers" } },
658  { "gtt.info", { utils::http::METHOD::GET, "/gtt/triggers/{0}" } },
659  { "gtt.modify", { utils::http::METHOD::PUT, "/gtt/triggers/{0}" } },
660  { "gtt.delete", { utils::http::METHOD::DEL, "/gtt/triggers/{0}" } },
661  // mf
662  { "mf.orders", { utils::http::METHOD::GET, "/mf/orders" } },
663  { "mf.order.info", { utils::http::METHOD::GET, "/mf/orders/{0}" } },
664  { "mf.order.place", { utils::http::METHOD::POST, "/mf/orders" } },
665  { "mf.order.cancel", { utils::http::METHOD::DEL, "/mf/orders/{0}" } },
666  { "mf.holdings", { utils::http::METHOD::GET, "/mf/holdings" } },
667  { "mf.instruments", { utils::http::METHOD::GET, "/mf/instruments",
668  utils::http::CONTENT_TYPE::NON_JSON,
669  utils::http::CONTENT_TYPE::NON_JSON } },
670  { "mf.sips", { utils::http::METHOD::GET, "/mf/sips" } },
671  { "mf.sip.info", { utils::http::METHOD::GET, "/mf/sips/{0}" } },
672  { "mf.sip.place", { utils::http::METHOD::POST, "/mf/sips" } },
673  { "mf.sip.modify", { utils::http::METHOD::PUT, "/mf/sips/{0}" } },
674  { "mf.sip.cancel", { utils::http::METHOD::DEL, "/mf/sips/{0}" } },
675  // portfolio
676  { "portfolio.holdings",
677  { utils::http::METHOD::GET, "/portfolio/holdings" } },
678  { "portfolio.positions",
679  { utils::http::METHOD::GET, "/portfolio/positions" } },
680  { "portfolio.positions.convert",
681  { utils::http::METHOD::PUT, "/portfolio/positions" } },
682  // market
683  { "market.instruments.all", { utils::http::METHOD::GET, "/instruments",
684  utils::http::CONTENT_TYPE::NON_JSON,
685  utils::http::CONTENT_TYPE::NON_JSON } },
686  { "market.instruments", { utils::http::METHOD::GET, "/instruments/{0}",
687  utils::http::CONTENT_TYPE::NON_JSON,
688  utils::http::CONTENT_TYPE::NON_JSON } },
689  { "market.margins", { utils::http::METHOD::GET, "/margins/{0}" } },
690  { "market.historical",
691  { utils::http::METHOD::GET,
692  "/instruments/historical/{0}/"
693  "{1}?from={2}&to={3}&continuous={4}&oi={5}" } },
694  { "market.trigger_range",
695  { utils::http::METHOD::GET, "/instruments/trigger_range/{9}" } },
696  { "market.quote", { utils::http::METHOD::GET, "/quote?{0}" } },
697  { "market.quote.ohlc",
698  { utils::http::METHOD::GET, "/quote/ohlc?{0}" } },
699  { "market.quote.ltp", { utils::http::METHOD::GET, "/quote/ltp?{0}" } },
700  // margins
701  { "margins.orders", { utils::http::METHOD::POST, "/margins/orders",
702  utils::http::CONTENT_TYPE::JSON } },
703  { "margins.basket", { utils::http::METHOD::POST,
704  "/margins/basket?consider_positions={0}",
705  utils::http::CONTENT_TYPE::JSON } },
706  };
707  string key;
708  string token;
709  string authorization;
710  httplib::Client client;
711 
712  protected:
713 #ifdef KITE_UNIT_TEST
714  virtual utils::http::response sendReq(const utils::http::endpoint& endpoint,
715  const utils::http::Params& body, const utils::FmtArgs& fmtArgs);
716 #else
717  utils::http::response sendReq(const utils::http::endpoint& endpoint,
726  const utils::http::Params& body, const utils::FmtArgs& fmtArgs);
727 #endif
728 };
729 
730 } // namespace kiteconnect
kiteconnect::kite::getMfInstruments
std::vector< mfInstrument > getMfInstruments()
Get the list of mutual fund instruments available for trading.
Definition: mf.hpp:135
kiteconnect::mfOrder
Represents information of a mutual fund order.
Definition: mf.hpp:113
kiteconnect::positions
Represents response of the getPositions() method.
Definition: portfolio.hpp:155
kiteconnect::kite::triggers
std::vector< GTT > triggers()
Get list of GTTs.
Definition: gtt.hpp:79
kiteconnect::kite::trades
std::vector< trade > trades()
Get list of trades.
Definition: order.hpp:117
kiteconnect::allMargins
Represents all margins (equity and commodity).
Definition: user.hpp:186
kiteconnect::kite::getMfOrder
mfOrder getMfOrder(const string &orderId)
Get details of a particular mutual fund order.
Definition: mf.hpp:68
kiteconnect::modifyGttParams
Parameters required for the modifyGtt method.
Definition: gtt.hpp:98
kiteconnect::kite::placeGtt
int placeGtt(const placeGttParams &params)
Place a GTT.
Definition: gtt.hpp:66
kiteconnect::kite::kite
kite(string apikey)
Construct a new kite object.
Definition: api.hpp:36
kiteconnect::kite::getGtt
GTT getGtt(int triggerId)
Get details of a particular GTT.
Definition: gtt.hpp:88
kiteconnect::placeMfOrderParams
Parameters required for the placeMfOrder method.
Definition: mf.hpp:42
kiteconnect::kite::getMfHoldings
std::vector< mfHolding > getMfHoldings()
Get mutual fund holdings.
Definition: mf.hpp:73
kiteconnect::kite::cancelMfSip
string cancelMfSip(const string &sipId)
Cancel a mutual fund SIP.
Definition: mf.hpp:114
kiteconnect::mfSip
Represents information of a mutual fund SIP.
Definition: mf.hpp:188
kiteconnect::kite::getInstruments
std::vector< instrument > getInstruments(const string &exchange="")
Retrieve the list of market instruments available to trade.
Definition: market.hpp:90
kiteconnect::kite::cancelOrder
string cancelOrder(const string &variety, const string &orderId, const string &parentOrderId="")
cancel an order
Definition: order.hpp:82
kiteconnect::kite::modifyGtt
int modifyGtt(const kc::modifyGttParams &params)
Modify a GTT.
Definition: gtt.hpp:93
kiteconnect::GTT
GTT represents a single GTT order.
Definition: gtt.hpp:120
kiteconnect::kite::getLtp
std::unordered_map< string, ltpQuote > getLtp(const std::vector< string > &symbols)
Retrieve Last Traded Price for a list of instruments.
Definition: market.hpp:61
kiteconnect::margins
Represents user margins for a segment.
Definition: user.hpp:166
kiteconnect::placeOrderParams
Parameters required for the placeOrder method.
Definition: order.hpp:43
kiteconnect::kite::getSip
mfSip getSip(const string &sipId)
Get details of a particular SIP.
Definition: mf.hpp:130
kiteconnect::kite::getApiKey
string getApiKey() const
Get current session's API key.
Definition: api.hpp:42
kiteconnect::kite::sendReq
utils::http::response sendReq(const utils::http::endpoint &endpoint, const utils::http::Params &body, const utils::FmtArgs &fmtArgs)
send a http request with the context used by kite
Definition: internal.hpp:61
kiteconnect::kite::getAccessToken
string getAccessToken() const
Get access token set for current session.
Definition: api.hpp:53
kiteconnect::kite::getOhlc
std::unordered_map< string, ohlcQuote > getOhlc(const std::vector< string > &symbols)
Retrieve OHLC info for a list of instruments.
Definition: market.hpp:48
kiteconnect::kite::holdings
std::vector< holding > holdings()
Get holdings.
Definition: portfolio.hpp:35
kiteconnect::kite::setApiKey
void setApiKey(const string &arg)
Set the API key for current session.
Definition: api.hpp:40
kiteconnect::kite::modifyMfSip
string modifyMfSip(const modifyMfSipParams &params)
Modify a mutual SIP order.
Definition: mf.hpp:99
kiteconnect::kite::getBasketMargins
basketMargins getBasketMargins(const std::vector< marginsParams > &params, bool considerPositions)
Get margins required for a basket.
Definition: margins.hpp:60
kiteconnect::kite::orderHistory
std::vector< order > orderHistory(const string &orderId)
Get history of an order.
Definition: order.hpp:108
kiteconnect::kite::getMfOrders
std::vector< mfOrder > getMfOrders()
Get mutual fund orders.
Definition: mf.hpp:59
kiteconnect::historicalDataParams
represents parameters required for the getHistoricalData function
Definition: market.hpp:175
kiteconnect::kite::getHistoricalData
std::vector< historicalData > getHistoricalData(const historicalDataParams &params)
Retrieve historical data of an instrument.
Definition: market.hpp:74
kiteconnect::kite::placeMfOrder
string placeMfOrder(const placeMfOrderParams &params)
Place a mutual fund order.
Definition: mf.hpp:35
kiteconnect::basketMargins
Definition: margins.hpp:163
kiteconnect::placeMfSipResponse
Represents response returned by the placeMfSip method.
Definition: mf.hpp:80
kiteconnect::kite::getSips
std::vector< mfSip > getSips()
Get list of SIPs.
Definition: mf.hpp:121
kiteconnect::kite::deleteGtt
int deleteGtt(int triggerId)
Delete a GTT.
Definition: gtt.hpp:107
kiteconnect::kite::getPositions
positions getPositions()
Get positions.
Definition: portfolio.hpp:44
kiteconnect::kite::cancelMfOrder
string cancelMfOrder(const string &orderId)
Cancel a mutual fund order.
Definition: mf.hpp:52
kiteconnect::modifyMfSipParams
Parameters required for the modifyMfSip method.
Definition: mf.hpp:94
kiteconnect::kite::orders
std::vector< order > orders()
Get list of orders.
Definition: order.hpp:99
kiteconnect::kite::getQuote
std::unordered_map< string, quote > getQuote(const std::vector< string > &symbols)
Retrieve quote for a list of instruments.
Definition: market.hpp:35
kiteconnect::kite
kite represents a KiteConnect session. It wraps around the KiteConnect REST API and provides a native...
Definition: kite.hpp:48
kiteconnect::kite::convertPosition
bool convertPosition(const convertPositionParams &params)
Convert an open position to a different product type.
Definition: portfolio.hpp:48
kiteconnect::kite::modifyOrder
string modifyOrder(const modifyOrderParams &params)
modify an order
Definition: order.hpp:64
kiteconnect::placeGttParams
Parameters required for the placeGtt method.
Definition: gtt.hpp:78
kiteconnect::kite::generateSession
userSession generateSession(const string &requestToken, const string &apiSecret)
Generate an user session. Use this method to generate an access token.
Definition: api.hpp:55
kiteconnect::kite::loginURL
string loginURL() const
Get the login URL to which the user should be redirected to initiate the login flow.
Definition: api.hpp:44
kiteconnect::kite::orderTrades
std::vector< trade > orderTrades(const string &orderId)
Get the list of trades executed for a particular order.
Definition: order.hpp:126
kiteconnect::placeMfSipParams
Parameters required for the placeMfSip method.
Definition: mf.hpp:58
kiteconnect::userProfile
Represents an user's profile.
Definition: user.hpp:42
kiteconnect::modifyOrderParams
Parameters required for the modifyOrder method.
Definition: order.hpp:90
kiteconnect::kite::placeOrder
string placeOrder(const placeOrderParams &params)
Place an order.
Definition: order.hpp:35
kiteconnect::kite::getOrderMargins
std::vector< orderMargins > getOrderMargins(const std::vector< marginsParams > &params)
Get margins required for placing particular orders.
Definition: margins.hpp:35
kiteconnect::kite::placeMfSip
placeMfSipResponse placeMfSip(const placeMfSipParams &params)
Place a mutual fund SIP order.
Definition: mf.hpp:82
kiteconnect::convertPositionParams
Parameters required for the convertPosition() method.
Definition: portfolio.hpp:174
kiteconnect::kite::invalidateSession
bool invalidateSession()
This method invalidates the access token and destroys current session.
Definition: api.hpp:66
kiteconnect::kite::getMargins
allMargins getMargins()
Get margins for all segments.
Definition: user.hpp:37
kiteconnect::userSession
Represents information of a user session.
Definition: user.hpp:100
kiteconnect::kite::profile
userProfile profile()
Get user's profile.
Definition: user.hpp:33
kiteconnect::kite::setAccessToken
void setAccessToken(const string &arg)
Set the access token current session.
Definition: api.hpp:48