CPPKiteConnect
portfolio.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 <string>
29 
30 #include "../utils.hpp"
31 #include "rapidjson/include/rapidjson/document.h"
32 #include "rapidjson/include/rapidjson/rapidjson.h"
33 
34 namespace kiteconnect {
35 
36 using std::string;
37 namespace rj = rapidjson;
38 namespace kc = kiteconnect;
39 namespace utils = kc::internal::utils;
40 
42 struct holding {
43  holding() = default;
44  explicit holding(const rj::Value::Object& val) { parse(val); };
45 
46  void parse(const rj::Value::Object& val) {
47  tradingsymbol = utils::json::get<string>(val, "tradingsymbol");
48  exchange = utils::json::get<string>(val, "exchange");
49  instrumentToken = utils::json::get<uint32_t>(val, "instrument_token");
50  ISIN = utils::json::get<string>(val, "isin");
51  product = utils::json::get<string>(val, "product");
52  price = utils::json::get<double>(val, "price");
53  quantity = utils::json::get<int>(val, "quantity");
54  t1Quantity = utils::json::get<int>(val, "t1_quantity");
55  realisedQuantity = utils::json::get<int>(val, "realised_quantity");
56  collateralQuantity = utils::json::get<int>(val, "collateral_quantity");
57  collateralType = utils::json::get<string>(val, "collateral_type");
58  averagePrice = utils::json::get<double>(val, "average_price");
59  lastPrice = utils::json::get<double>(val, "last_price");
60  closePrice = utils::json::get<double>(val, "close_price");
61  PnL = utils::json::get<double>(val, "pnl");
62  dayChange = utils::json::get<double>(val, "day_change");
63  dayChangePercentage =
64  utils::json::get<double>(val, "day_change_percentage");
65  };
66 
67  uint32_t instrumentToken = 0;
68  int quantity = -1;
69  int t1Quantity = -1;
70  int realisedQuantity = -1;
71  int collateralQuantity = -1;
72  double price = -1;
73  double averagePrice = -1;
74  double lastPrice = -1;
75  double closePrice = -1;
76  double PnL = -1;
77  double dayChange = -1;
78  double dayChangePercentage = -1;
79  string tradingsymbol;
80  string exchange;
81  string ISIN;
82  string product;
83  string collateralType;
84 };
85 
87 struct position {
88  position() = default;
89  explicit position(const rj::Value::Object& val) { parse(val); };
90 
91  void parse(const rj::Value::Object& val) {
92  tradingsymbol = utils::json::get<string>(val, "tradingsymbol");
93  exchange = utils::json::get<string>(val, "exchange");
94  instrumentToken = utils::json::get<uint32_t>(val, "instrument_token");
95  product = utils::json::get<string>(val, "product");
96  quantity = utils::json::get<int>(val, "quantity");
97  overnightQuantity = utils::json::get<int>(val, "overnight_quantity");
98  multiplier = utils::json::get<double>(val, "multiplier");
99  averagePrice = utils::json::get<double>(val, "average_price");
100  closePrice = utils::json::get<double>(val, "close_price");
101  lastPrice = utils::json::get<double>(val, "last_price");
102  value = utils::json::get<double>(val, "value");
103  PnL = utils::json::get<double>(val, "pnl");
104  M2M = utils::json::get<double>(val, "m2m");
105  unrealised = utils::json::get<double>(val, "unrealised");
106  realised = utils::json::get<double>(val, "realised");
107  buyQuantity = utils::json::get<int>(val, "buy_quantity");
108  buyPrice = utils::json::get<double>(val, "buy_price");
109  buyValue = utils::json::get<double>(val, "buy_value");
110  buyM2MValue = utils::json::get<double>(val, "buy_m2m");
111  sellQuantity = utils::json::get<int>(val, "sell_quantity");
112  sellPrice = utils::json::get<double>(val, "sell_price");
113  sellValue = utils::json::get<double>(val, "sell_value");
114  sellM2MValue = utils::json::get<double>(val, "sell_m2m");
115  dayBuyQuantity = utils::json::get<int>(val, "day_buy_quantity");
116  dayBuyPrice = utils::json::get<double>(val, "day_buy_price");
117  dayBuyValue = utils::json::get<double>(val, "day_buy_value");
118  daySellQuantity = utils::json::get<int>(val, "day_sell_quantity");
119  daySellPrice = utils::json::get<double>(val, "day_sell_price");
120  daySellValue = utils::json::get<double>(val, "day_sell_value");
121  };
122 
123  uint32_t instrumentToken = 0;
124  int quantity = -1;
125  int buyQuantity = -1;
126  int overnightQuantity = -1;
127  int sellQuantity = -1;
128  int dayBuyQuantity = -1;
129  int daySellQuantity = -1;
130  double multiplier = -1;
131  double averagePrice = -1;
132  double closePrice = -1;
133  double lastPrice = -1;
134  double value = -1;
135  double PnL = -1;
136  double M2M = -1;
137  double unrealised = -1;
138  double realised = -1;
139  double buyPrice = -1;
140  double buyValue = -1;
141  double buyM2MValue = -1;
142  double sellPrice = -1;
143  double sellValue = -1;
144  double sellM2MValue = -1;
145  double dayBuyPrice = -1;
146  double dayBuyValue = -1;
147  double daySellPrice = -1;
148  double daySellValue = -1;
149  string tradingsymbol;
150  string exchange;
151  string product;
152 };
153 
155 struct positions {
156  positions() = default;
157  explicit positions(const rj::Value::Object& val) { parse(val); };
158 
159  void parse(const rj::Value::Object& val) {
160  rj::Value netVal(rj::kArrayType);
161  utils::json::get<utils::json::JsonArray>(val, netVal, "net");
162  for (auto& i : netVal.GetArray()) { net.emplace_back(i.GetObject()); };
163 
164  rj::Value dayVal(rj::kArrayType);
165  utils::json::get<utils::json::JsonArray>(val, dayVal, "day");
166  for (auto& i : dayVal.GetArray()) { day.emplace_back(i.GetObject()); };
167  };
168 
169  std::vector<position> net;
170  std::vector<position> day;
171 };
172 
175  GENERATE_FLUENT_METHOD(convertPositionParams, int, quantity, Quantity);
176  GENERATE_FLUENT_METHOD(
177  convertPositionParams, const string&, exchange, Exchange);
178  GENERATE_FLUENT_METHOD(
179  convertPositionParams, const string&, symbol, Symbol);
180  GENERATE_FLUENT_METHOD(
181  convertPositionParams, const string&, transactionType, TransactionType);
182  GENERATE_FLUENT_METHOD(
183  convertPositionParams, const string&, positionType, PositionType);
184  GENERATE_FLUENT_METHOD(
185  convertPositionParams, const string&, oldProduct, OldProduct);
186  GENERATE_FLUENT_METHOD(
187  convertPositionParams, const string&, newProduct, NewProduct);
188 
189  int quantity = -1;
190  string exchange;
191  string symbol;
192  string transactionType;
193  string positionType;
194  string oldProduct;
195  string newProduct;
196 };
197 
198 } // namespace kiteconnect
kiteconnect::positions
Represents response of the getPositions() method.
Definition: portfolio.hpp:155
kiteconnect::position
Represents an individual position.
Definition: portfolio.hpp:87
kiteconnect::holding
Respresents an individual holding.
Definition: portfolio.hpp:42
kiteconnect::convertPositionParams
Parameters required for the convertPosition() method.
Definition: portfolio.hpp:174