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 
28 #include <cstdint>
29 #include <string>
30 
31 #include "../utils.hpp"
32 #include "rapidcsv/src/rapidcsv.h"
33 #include "rapidjson/include/rapidjson/document.h"
34 #include "rapidjson/include/rapidjson/rapidjson.h"
35 
36 namespace kiteconnect {
37 
38 using std::string;
39 namespace rj = rapidjson;
40 namespace kc = kiteconnect;
41 namespace utils = kc::internal::utils;
42 
44 struct ohlc {
45  ohlc() = default;
46  explicit ohlc(const rj::Value::Object& val) { parse(val); };
47 
48  void parse(const rj::Value::Object& val) {
49  open = utils::json::get<double>(val, "open");
50  high = utils::json::get<double>(val, "high");
51  low = utils::json::get<double>(val, "low");
52  close = utils::json::get<double>(val, "close");
53  };
54 
55  double open = -1;
56  double high = -1;
57  double low = -1;
58  double close = -1;
59 };
60 
62 struct depth {
63  depth() = default;
64  explicit depth(const rj::Value::Object& val) { parse(val); };
65 
66  void parse(const rj::Value::Object& val) {
67  price = utils::json::get<double>(val, "price");
68  quantity = utils::json::get<int>(val, "quantity");
69  orders = utils::json::get<int>(val, "orders");
70  };
71 
72  int quantity = -1;
73  int orders = 0;
74  double price = -1;
75 };
76 
78 struct quote {
79  quote() = default;
80  explicit quote(const rj::Value::Object& val) { parse(val); };
81 
82  void parse(const rj::Value::Object& val) {
83  instrumentToken = utils::json::get<uint32_t>(val, "instrument_token");
84  timestamp = utils::json::get<string>(val, "timestamp");
85  lastPrice = utils::json::get<double>(val, "last_price");
86  lastQuantity = utils::json::get<int>(val, "last_quantity");
87  lastTradeTime = utils::json::get<string>(val, "last_trade_time");
88  averagePrice = utils::json::get<double>(val, "average_price");
89  volume = utils::json::get<int64_t>(val, "volume");
90  buyQuantity = utils::json::get<int>(val, "buy_quantity");
91  sellQuantity = utils::json::get<int>(val, "sell_quantity");
92  OHLC = utils::json::get<utils::json::JsonObject, ohlc>(val, "ohlc");
93  netChange = utils::json::get<double>(val, "net_change");
94  OI = utils::json::get<double>(val, "oi");
95  OIDayHigh = utils::json::get<double>(val, "oi_day_high");
96  OIDayLow = utils::json::get<double>(val, "oi_day_low");
97  lowerCircuitLimit =
98  utils::json::get<double>(val, "lower_circuit_limit");
99  upperCircuitLimit =
100  utils::json::get<double>(val, "upper_circuit_limit");
101  marketDepth =
102  utils::json::get<utils::json::JsonObject, mDepth>(val, "depth");
103  };
104 
105  uint32_t instrumentToken = 0;
106  int64_t volume = -1;
107  int buyQuantity = -1;
108  int sellQuantity = -1;
109  int lastQuantity = -1;
110  double lastPrice = -1;
111  double averagePrice = -1;
112  double netChange = -1;
113  double OI = -1;
114  double OIDayHigh = -1;
115  double OIDayLow = -1;
116  double lowerCircuitLimit = -1;
117  double upperCircuitLimit = -1;
118  string timestamp;
119  string lastTradeTime;
120  ohlc OHLC;
121  struct mDepth {
122  mDepth() = default;
123  explicit mDepth(const rj::Value::Object& val) { parse(val); };
124 
125  void parse(const rj::Value::Object& val) {
126  rj::Value buyDepthVal(rj::kArrayType);
127  utils::json::get<utils::json::JsonArray>(val, buyDepthVal, "buy");
128  for (auto& i : buyDepthVal.GetArray()) {
129  buy.emplace_back(i.GetObject());
130  };
131 
132  rj::Value sellDepthVal(rj::kArrayType);
133  utils::json::get<utils::json::JsonArray>(val, sellDepthVal, "sell");
134  for (auto& i : sellDepthVal.GetArray()) {
135  sell.emplace_back(i.GetObject());
136  };
137  }
138 
139  std::vector<depth> buy;
140  std::vector<depth> sell;
141  } marketDepth;
142 };
143 
145 struct ohlcQuote {
146  ohlcQuote() = default;
147  explicit ohlcQuote(const rj::Value::Object& val) { parse(val); };
148 
149  void parse(const rj::Value::Object& val) {
150  instrumentToken = utils::json::get<uint32_t>(val, "instrument_token");
151  lastPrice = utils::json::get<double>(val, "last_price");
152  OHLC = utils::json::get<utils::json::JsonObject, ohlc>(val, "ohlc");
153  };
154 
155  uint32_t instrumentToken = 0;
156  double lastPrice = -1;
157  ohlc OHLC;
158 };
159 
161 struct ltpQuote {
162  ltpQuote() = default;
163  explicit ltpQuote(const rj::Value::Object& val) { parse(val); };
164 
165  void parse(const rj::Value::Object& val) {
166  instrumentToken = utils::json::get<uint32_t>(val, "instrument_token");
167  lastPrice = utils::json::get<double>(val, "last_price");
168  };
169 
170  uint32_t instrumentToken = 0;
171  double lastPrice = -1;
172 };
173 
176  GENERATE_FLUENT_METHOD(
177  historicalDataParams, uint32_t, instrumentToken, InstrumentToken);
178  GENERATE_FLUENT_METHOD(historicalDataParams, bool, continuous, Continuous);
179  GENERATE_FLUENT_METHOD(historicalDataParams, bool, oi, Oi);
180  GENERATE_FLUENT_METHOD(historicalDataParams, const string&, from, From);
181  GENERATE_FLUENT_METHOD(historicalDataParams, const string&, to, To);
182  GENERATE_FLUENT_METHOD(
183  historicalDataParams, const string&, interval, Interval);
184 
185  uint32_t instrumentToken = 0;
186  bool continuous = false;
187  bool oi = false;
188  string from;
189  string to;
190  string interval;
191 };
192 
195  historicalData() = default;
196  explicit historicalData(const rj::Value::Array& val) { parse(val); };
197 
198  void parse(const rj::Value::Array& val) {
199  // if the sent value doesn't have a floating point (this time),
200  // GetDouble() will throw error
201  static auto getDouble = [](rj::Value& val) -> double {
202  if (val.IsDouble()) { return val.GetDouble(); };
203  if (val.IsInt()) { return val.GetInt(); };
204  throw libException("type isn't double");
205  };
206  datetime = val[DATETIME_IDX].GetString();
207  open = getDouble(val[OPEN_IDX]);
208  high = getDouble(val[HIGH_IDX]);
209  low = getDouble(val[LOW_IDX]);
210  close = getDouble(val[CLOSE_IDX]);
211  volume = val[VOLUME_IDX].GetInt64();
212  if (val.Size() > OI_IDX) { OI = val[OI_IDX].GetInt64(); };
213  };
214 
215  int64_t volume = -1;
216  int64_t OI = -1;
217  double open = -1;
218  double high = -1;
219  double low = -1;
220  double close = -1;
221  string datetime;
222  static constexpr uint8_t DATETIME_IDX = 0;
223  static constexpr uint8_t OPEN_IDX = 1;
224  static constexpr uint8_t HIGH_IDX = 2;
225  static constexpr uint8_t LOW_IDX = 3;
226  static constexpr uint8_t CLOSE_IDX = 4;
227  static constexpr uint8_t VOLUME_IDX = 5;
228  static constexpr uint8_t OI_IDX = 6;
229 };
230 
232 struct instrument {
233  instrument() = default;
234  explicit instrument(const std::vector<string>& row) { parse(row); };
235 
236  void parse(const std::vector<string>& tokens) {
237  static const auto toInt = [](const string& str) -> int {
238  return (str.empty()) ? 0 : std::stoi(str);
239  };
240  static const auto toUint32 = [](const string& str) -> uint32_t {
241  return (str.empty()) ? 0 : std::stoul(str);
242  };
243  static const auto toDouble = [](const string& str) -> double {
244  return (str.empty()) ? 0.0 : std::stod(str);
245  };
246 
247  instrumentToken = toUint32(tokens[INSTRUMENT_TOKEN_IDX]);
248  exchangeToken = toInt(tokens[EXCHANGE_TOKEN_IDX]);
249  tradingsymbol = tokens[TRADINGSYMBOL_IDX];
250  name = tokens[NAME_IDX];
251  lastPrice = toDouble(tokens[LAST_PRICE_IDX]);
252  expiry = tokens[EXPIRY_IDX];
253  strikePrice = toDouble(tokens[STRIKE_PRICE_IDX]);
254  tickSize = toDouble(tokens[TICK_SIZE_IDX]);
255  lotSize = toDouble(tokens[LOT_SIZE_IDX]);
256  instrumentType = tokens[INSTRUMENT_TYPE_IDX];
257  segment = tokens[SEGMENT_IDX];
258  exchange = tokens[EXCHANGE_IDX];
259  };
260 
261  uint32_t instrumentToken = 0;
262  int exchangeToken = -1;
263  double lastPrice = -1;
264  double strikePrice = -1;
265  double tickSize = -1;
266  double lotSize = -1;
267  string tradingsymbol;
268  string name;
269  string expiry;
270  string instrumentType;
271  string segment;
272  string exchange;
273 
274  static constexpr uint8_t INSTRUMENT_TOKEN_IDX = 0;
275  static constexpr uint8_t EXCHANGE_TOKEN_IDX = 1;
276  static constexpr uint8_t TRADINGSYMBOL_IDX = 2;
277  static constexpr uint8_t NAME_IDX = 3;
278  static constexpr uint8_t LAST_PRICE_IDX = 4;
279  static constexpr uint8_t EXPIRY_IDX = 5;
280  static constexpr uint8_t STRIKE_PRICE_IDX = 6;
281  static constexpr uint8_t TICK_SIZE_IDX = 7;
282  static constexpr uint8_t LOT_SIZE_IDX = 8;
283  static constexpr uint8_t INSTRUMENT_TYPE_IDX = 9;
284  static constexpr uint8_t SEGMENT_IDX = 10;
285  static constexpr uint8_t EXCHANGE_IDX = 11;
286 };
287 
288 } // namespace kiteconnect
kiteconnect::libException
This exception is thrown when an error occures at the library level.
Definition: exceptions.hpp:206
kiteconnect::quote::mDepth
Definition: market.hpp:121
kiteconnect::historicalData
Represents historical data of an instrument.
Definition: market.hpp:194
kiteconnect::ltpQuote
Represents a LTP quote information of an instrument.
Definition: market.hpp:161
kiteconnect::instrument
Represents information of an instrument.
Definition: market.hpp:232
kiteconnect::historicalDataParams
represents parameters required for the getHistoricalData function
Definition: market.hpp:175
kiteconnect::ohlcQuote
Represents a ohlc quote information of an instrument.
Definition: market.hpp:145
kiteconnect::ohlc
Represents OHLC information of an instrument.
Definition: market.hpp:44
kiteconnect::quote
Represents quote informating of an instrument.
Definition: market.hpp:78
kiteconnect::depth
Represents market depth of an instrument.
Definition: market.hpp:62