CPPKiteConnect
margins.hpp
1 /*
2  * Licensed under the MIT License <http://opensource.org/licenses/MIT>.
3  * SPDX-License-Identifier: MIT
4  *
5  * Copyright (c) 2020-2023 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 
43 struct orderCharges {
44  orderCharges() = default;
45  explicit orderCharges(const rj::Value::Object& val) { parse(val); };
46 
47  void parse(const rj::Value::Object& val) {
48  transactionTax = utils::json::get<double>(val, "transaction_tax");
49  exchangeTurnoverCharge =
50  utils::json::get<double>(val, "exchange_turnover_charge");
51  sebiTurnoverCharge =
52  utils::json::get<double>(val, "sebi_turnover_charge");
53  brokerage = utils::json::get<double>(val, "brokerage");
54  stampDuty = utils::json::get<double>(val, "stamp_duty");
55  total = utils::json::get<double>(val, "total");
56  transactionTaxType =
57  utils::json::get<string>(val, "transaction_tax_type");
58  gst = utils::json::get<utils::json::JsonObject, Gst>(val, "gst");
59  };
60 
61  double transactionTax = -1;
62  double exchangeTurnoverCharge = -1;
63  double sebiTurnoverCharge = -1;
64  double brokerage = -1;
65  double stampDuty = -1;
66  double total = -1;
67  string transactionTaxType;
68  struct Gst {
69  Gst() = default;
70  explicit Gst(const rj::Value::Object& val) { parse(val); };
71 
72  void parse(const rj::Value::Object& val) {
73  igst = utils::json::get<double>(val, "igst");
74  cgst = utils::json::get<double>(val, "cgst");
75  sgst = utils::json::get<double>(val, "sgst");
76  total = utils::json::get<double>(val, "total");
77  };
78 
79  double igst = -1;
80  double cgst = -1;
81  double sgst = -1;
82  double total = -1;
83  } gst;
84 };
85 
87 struct marginsParams {
88  marginsParams() = default;
89 
90  GENERATE_FLUENT_METHOD(marginsParams, double, quantity, Quantity);
91  GENERATE_FLUENT_METHOD(marginsParams, double, price, Price);
92  GENERATE_FLUENT_METHOD(marginsParams, double, triggerPrice, TriggerPrice);
93  GENERATE_FLUENT_METHOD(marginsParams, const string&, exchange, Exchange);
94  GENERATE_FLUENT_METHOD(
95  marginsParams, const string&, tradingsymbol, Tradingsymbol);
96  GENERATE_FLUENT_METHOD(
97  marginsParams, const string&, transactionType, TransactionType);
98  GENERATE_FLUENT_METHOD(marginsParams, const string&, variety, Variety);
99  GENERATE_FLUENT_METHOD(marginsParams, const string&, product, Product);
100  GENERATE_FLUENT_METHOD(marginsParams, const string&, orderType, OrderType);
101 
102  double quantity = 0;
103  double price = 0;
104  double triggerPrice = 0;
105  string exchange;
106  string tradingsymbol;
107  string transactionType;
108  string variety;
109  string product;
110  string orderType;
111 };
112 
114 struct orderMargins {
115  orderMargins() = default;
116  explicit orderMargins(const rj::Value::Object& val) { parse(val); };
117 
118  void parse(const rj::Value::Object& val) {
119  type = utils::json::get<string>(val, "type");
120  tradingSymbol = utils::json::get<string>(val, "tradingsymbol");
121  exchange = utils::json::get<string>(val, "exchange");
122  span = utils::json::get<double>(val, "span");
123  exposure = utils::json::get<double>(val, "exposure");
124  optionPremium = utils::json::get<double>(val, "option_premium");
125  additional = utils::json::get<double>(val, "additional");
126  bo = utils::json::get<double>(val, "bo");
127  cash = utils::json::get<double>(val, "cash");
128  var = utils::json::get<double>(val, "var");
129  pnl = utils::json::get<utils::json::JsonObject, PNL>(val, "pnl");
130  total = utils::json::get<double>(val, "total");
131  leverage = utils::json::get<double>(val, "leverage");
132  charges = utils::json::get<utils::json::JsonObject, orderCharges>(
133  val, "charges");
134  };
135 
136  double span = -1;
137  double exposure = -1;
138  double optionPremium = -1;
139  double additional = -1;
140  double bo = -1;
141  double cash = -1;
142  double var = -1;
143  double total = -1;
144  double leverage = -1;
145  string type;
146  string tradingSymbol;
147  string exchange;
148  struct PNL {
149  PNL() = default;
150  explicit PNL(const rj::Value::Object& val) { parse(val); };
151 
152  void parse(const rj::Value::Object& val) {
153  realised = utils::json::get<double>(val, "realised");
154  unrealised = utils::json::get<double>(val, "unrealised");
155  };
156 
157  double realised = -1;
158  double unrealised = -1;
159  } pnl;
160  orderCharges charges;
161 };
162 
164  basketMargins() = default;
165  explicit basketMargins(const rj::Value::Object& val) { parse(val); };
166 
167  void parse(const rj::Value::Object& val) {
168  initial = utils::json::get<utils::json::JsonObject, orderMargins>(
169  val, "initial");
170  final = utils::json::get<utils::json::JsonObject, orderMargins>(
171  val, "final");
172  charges = utils::json::get<utils::json::JsonObject, orderCharges>(
173  val, "charges");
174 
175  rj::Value ordersVal(rj::kArrayType);
176  utils::json::get<utils::json::JsonArray>(val, ordersVal, "orders");
177  for (auto& i : ordersVal.GetArray()) {
178  orders.emplace_back(i.GetObject());
179  };
180  };
181 
182  orderMargins initial;
183  orderMargins final;
184  std::vector<orderMargins> orders;
185  orderCharges charges;
186 };
187 } // namespace kiteconnect
kiteconnect::basketMargins
Definition: margins.hpp:163
kiteconnect::orderMargins
Represents margins required for placing an order.
Definition: margins.hpp:114
kiteconnect::orderCharges
Definition: margins.hpp:43
kiteconnect::orderCharges::Gst
Definition: margins.hpp:68
kiteconnect::marginsParams
Parameters required by the margin methods.
Definition: margins.hpp:87
kiteconnect::orderMargins::PNL
Definition: margins.hpp:148