CPPKiteConnect
market.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 
26 #pragma once
27 #pragma clang diagnostic ignored "-Wundefined-inline"
28 
29 #include <string>
30 
31 #include "../kite.hpp"
32 #include "../utils.hpp"
33 
34 namespace kiteconnect {
35 inline std::unordered_map<string, quote> kite::getQuote(
36  const std::vector<string>& symbols) {
37  return callApi<std::unordered_map<string, quote>, utils::json::JsonObject,
38  true>("market.quote", {}, { encodeSymbolsList(symbols) },
39  [](utils::json::JsonObject& data) {
40  std::unordered_map<string, quote> Quotes;
41  for (auto& i : data) {
42  Quotes.emplace(i.name.GetString(), i.value.GetObject());
43  };
44  return Quotes;
45  });
46 };
47 
48 inline std::unordered_map<string, ohlcQuote> kite::getOhlc(
49  const std::vector<string>& symbols) {
50  return callApi<std::unordered_map<string, ohlcQuote>,
51  utils::json::JsonObject, true>("market.quote.ohlc", {},
52  { encodeSymbolsList(symbols) }, [](utils::json::JsonObject& data) {
53  std::unordered_map<string, ohlcQuote> Quotes;
54  for (auto& i : data) {
55  Quotes.emplace(i.name.GetString(), i.value.GetObject());
56  };
57  return Quotes;
58  });
59 };
60 
61 inline std::unordered_map<string, ltpQuote> kite::getLtp(
62  const std::vector<string>& symbols) {
63  return callApi<std::unordered_map<string, ltpQuote>,
64  utils::json::JsonObject, true>("market.quote.ltp", {},
65  { encodeSymbolsList(symbols) }, [](utils::json::JsonObject& data) {
66  std::unordered_map<string, ltpQuote> Quotes;
67  for (auto& i : data) {
68  Quotes.emplace(i.name.GetString(), i.value.GetObject());
69  };
70  return Quotes;
71  });
72 };
73 
74 inline std::vector<historicalData> kite::getHistoricalData(
75  const historicalDataParams& params) {
76  static const auto toString = [](bool val) { return val ? "1" : "0"; };
77  return callApi<std::vector<historicalData>, utils::json::JsonObject, true>(
78  "market.historical", {},
79  { std::to_string(params.instrumentToken), params.interval, params.from,
80  params.to, toString(params.continuous), toString(params.oi) },
81  [](utils::json::JsonObject& data) {
82  std::vector<historicalData> candles;
83  for (auto& i : data["candles"].GetArray()) {
84  candles.emplace_back(i.GetArray());
85  }
86  return candles;
87  });
88 };
89 
90 inline std::vector<instrument> kite::getInstruments(const string& exchange) {
91  utils::FmtArgs fmtArgs = {};
92  utils::http::endpoint endpoint;
93  if (exchange.empty()) {
94  endpoint = endpoints.at("market.instruments.all");
95  } else {
96  endpoint = endpoints.at("market.instruments");
97  fmtArgs.emplace_back(exchange);
98  }
99  const auto response = sendReq(endpoint, {}, fmtArgs);
100  if (!response) { return {}; };
101 
102  return utils::parseInstruments<instrument>(response.rawBody);
103 };
104 
105 }; // namespace kiteconnect
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::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::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::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::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::getQuote
std::unordered_map< string, quote > getQuote(const std::vector< string > &symbols)
Retrieve quote for a list of instruments.
Definition: market.hpp:35