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 #pragma clang diagnostic ignored "-Wundefined-inline"
28 
29 #include <vector>
30 
31 #include "../kite.hpp"
32 #include "../utils.hpp"
33 
34 namespace kiteconnect {
35 inline std::vector<orderMargins> kite::getOrderMargins(
36  const std::vector<marginsParams>& params) {
37  utils::json::json<utils::json::JsonArray> ordersJson;
38  ordersJson.array<marginsParams>(params, [&](const marginsParams& param,
39  rj::Value& buffer) {
40  ordersJson.field("exchange", param.exchange, &buffer);
41  ordersJson.field("tradingsymbol", param.tradingsymbol, &buffer);
42  ordersJson.field("transaction_type", param.transactionType, &buffer);
43  ordersJson.field("variety", param.variety, &buffer);
44  ordersJson.field("product", param.product, &buffer);
45  ordersJson.field("order_type", param.orderType, &buffer);
46  ordersJson.field("quantity", param.quantity, &buffer);
47  ordersJson.field("price", param.price, &buffer);
48  ordersJson.field("trigger_price", param.triggerPrice, &buffer);
49  });
50 
51  return callApi<std::vector<orderMargins>, utils::json::JsonArray, true>(
52  "margins.orders", { { "", ordersJson.serialize() } }, {},
53  [](utils::json::JsonArray& data) {
54  std::vector<orderMargins> margins;
55  for (auto& i : data) { margins.emplace_back(i.GetObject()); }
56  return margins;
57  });
58 };
59 
61  const std::vector<marginsParams>& params, bool considerPositions) {
62  utils::json::json<utils::json::JsonArray> ordersJson;
63  ordersJson.array<marginsParams>(params, [&](const marginsParams& param,
64  rj::Value& buffer) {
65  ordersJson.field("exchange", param.exchange, &buffer);
66  ordersJson.field("tradingsymbol", param.tradingsymbol, &buffer);
67  ordersJson.field("transaction_type", param.transactionType, &buffer);
68  ordersJson.field("variety", param.variety, &buffer);
69  ordersJson.field("product", param.product, &buffer);
70  ordersJson.field("order_type", param.orderType, &buffer);
71  ordersJson.field("quantity", param.quantity, &buffer);
72  ordersJson.field("price", param.price, &buffer);
73  ordersJson.field("trigger_price", param.triggerPrice, &buffer);
74  });
75 
76  return callApi<basketMargins, utils::json::JsonObject>("margins.basket",
77  { { "", ordersJson.serialize() } },
78  { considerPositions ? "true" : "false" });
79 };
80 } // namespace kiteconnect
kiteconnect::margins
Represents user margins for a segment.
Definition: user.hpp:166
kiteconnect::kite::getBasketMargins
basketMargins getBasketMargins(const std::vector< marginsParams > &params, bool considerPositions)
Get margins required for a basket.
Definition: margins.hpp:60
kiteconnect::basketMargins
Definition: margins.hpp:163
kiteconnect::marginsParams
Parameters required by the margin methods.
Definition: margins.hpp:87
kiteconnect::kite::getOrderMargins
std::vector< orderMargins > getOrderMargins(const std::vector< marginsParams > &params)
Get margins required for placing particular orders.
Definition: margins.hpp:35