CPPKiteConnect
gtt.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 namespace internal {
36 using std::string;
37 
38 template <class T>
39 string getConditionJson(const T& params) {
40  utils::json::json<utils::json::JsonObject> conditionJson;
41  conditionJson.field("exchange", params.exchange);
42  conditionJson.field("tradingsymbol", params.symbol);
43  conditionJson.field("trigger_values", params.triggerValues);
44  conditionJson.field("last_price", params.lastPrice);
45  return conditionJson.serialize();
46 }
47 
48 template <class T>
49 string getOrdersJson(const T& params) {
50  utils::json::json<utils::json::JsonArray> ordersJson;
51  ordersJson.array<gttParams>(
52  params.gttParamsList, [&](const gttParams& param, rj::Value& buffer) {
53  ordersJson.field("exchange", params.exchange, &buffer);
54  ordersJson.field("tradingsymbol", params.symbol, &buffer);
55  ordersJson.field(
56  "transaction_type", param.transactionType, &buffer);
57  ordersJson.field("quantity", param.quantity, &buffer);
58  ordersJson.field("order_type", param.orderType, &buffer);
59  ordersJson.field("product", param.product, &buffer);
60  ordersJson.field("price", param.price, &buffer);
61  });
62  return ordersJson.serialize();
63 }
64 }; // namespace internal
65 
66 inline int kite::placeGtt(const placeGttParams& params) {
67  utils::http::Params reqParams = {
68  { "type", params.triggerType },
69  { "condition", internal::getConditionJson(params) },
70  { "orders", internal::getOrdersJson(params) },
71  };
72 
73  return callApi<int, utils::json::JsonObject, true>(
74  "gtt.place", reqParams, {}, [](utils::json::JsonObject& data) {
75  return utils::json::get<int>(data, "trigger_id");
76  });
77 };
78 
79 inline std::vector<GTT> kite::triggers() {
80  return callApi<std::vector<GTT>, utils::json::JsonArray, true>(
81  "gtt", {}, {}, [](utils::json::JsonArray& data) {
82  std::vector<GTT> Triggers;
83  for (auto& i : data) { Triggers.emplace_back(i.GetObject()); }
84  return Triggers;
85  });
86 };
87 
88 inline GTT kite::getGtt(int triggerId) {
89  return callApi<GTT, utils::json::JsonObject>(
90  "gtt.info", {}, { std::to_string(triggerId) });
91 };
92 
93 inline int kite::modifyGtt(const kc::modifyGttParams& params) {
94  utils::http::Params reqParams = {
95  { "type", params.triggerType },
96  { "condition", internal::getConditionJson(params) },
97  { "orders", internal::getOrdersJson(params) },
98  };
99 
100  return callApi<int, utils::json::JsonObject, true>("gtt.modify", reqParams,
101  { std::to_string(params.triggerId) },
102  [](utils::json::JsonObject& data) {
103  return utils::json::get<int>(data, "trigger_id");
104  });
105 };
106 
107 inline int kite::deleteGtt(int triggerId) {
108  return callApi<int, utils::json::JsonObject, true>("gtt.delete", {},
109  { std::to_string(triggerId) }, [](utils::json::JsonObject& data) {
110  return utils::json::get<int>(data, "trigger_id");
111  });
112 };
113 
114 } // namespace kiteconnect
kiteconnect::kite::triggers
std::vector< GTT > triggers()
Get list of GTTs.
Definition: gtt.hpp:79
kiteconnect::modifyGttParams
Parameters required for the modifyGtt method.
Definition: gtt.hpp:98
kiteconnect::kite::placeGtt
int placeGtt(const placeGttParams &params)
Place a GTT.
Definition: gtt.hpp:66
kiteconnect::kite::getGtt
GTT getGtt(int triggerId)
Get details of a particular GTT.
Definition: gtt.hpp:88
kiteconnect::kite::modifyGtt
int modifyGtt(const kc::modifyGttParams &params)
Modify a GTT.
Definition: gtt.hpp:93
kiteconnect::GTT
GTT represents a single GTT order.
Definition: gtt.hpp:120
kiteconnect::kite::deleteGtt
int deleteGtt(int triggerId)
Delete a GTT.
Definition: gtt.hpp:107
kiteconnect::placeGttParams
Parameters required for the placeGtt method.
Definition: gtt.hpp:78