CPPKiteConnect
order.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 <vector>
30 
31 #include "../kite.hpp"
32 #include "../utils.hpp"
33 
34 namespace kiteconnect {
35 inline string kite::placeOrder(const placeOrderParams& params) {
36  // required parameters
37  utils::http::Params bodyParams = {
38  { "exchange", params.exchange },
39  { "tradingsymbol", params.symbol },
40  { "transaction_type", params.transactionType },
41  { "quantity", std::to_string(params.quantity) },
42  { "product", params.product },
43  { "order_type", params.orderType },
44  };
45  // optional parameters
46  utils::addParam(bodyParams, params.price, "price");
47  utils::addParam(bodyParams, params.validity, "validity");
48  utils::addParam(bodyParams, params.disclosedQuantity, "disclosed_quantity");
49  utils::addParam(bodyParams, params.triggerPrice, "trigger_price");
50  utils::addParam(bodyParams, params.squareOff, "squareoff");
51  utils::addParam(bodyParams, params.stopLoss, "stoploss");
52  utils::addParam(bodyParams, params.trailingStopLoss, "trailing_stoploss");
53  utils::addParam(bodyParams, params.icebergLegs, "iceberg_legs");
54  utils::addParam(bodyParams, params.icebergQuantity, "iceberg_quantity");
55  utils::addParam(bodyParams, params.validityTtl, "validity_ttl");
56  utils::addParam(bodyParams, params.tag, "tag");
57 
58  return callApi<string, utils::json::JsonObject, true>("order.place",
59  bodyParams, { params.variety }, [](utils::json::JsonObject& data) {
60  return utils::json::get<string>(data, "order_id");
61  });
62 };
63 
64 inline string kite::modifyOrder(const modifyOrderParams& params) {
65  utils::http::Params bodyParams = {};
66  // optional parameters
67  utils::addParam(bodyParams, params.parentOrderId, "parent_order_id");
68  utils::addParam(bodyParams, params.quantity, "quantity");
69  utils::addParam(bodyParams, params.price, "price");
70  utils::addParam(bodyParams, params.orderType, "order_type");
71  utils::addParam(bodyParams, params.triggerPrice, "trigger_price");
72  utils::addParam(bodyParams, params.validity, "validity");
73  utils::addParam(bodyParams, params.disclosedQuantity, "disclosed_quantity");
74 
75  return callApi<string, utils::json::JsonObject, true>("order.modify",
76  bodyParams, { params.variety, params.orderId },
77  [](utils::json::JsonObject& data) {
78  return utils::json::get<string>(data, "order_id");
79  });
80 };
81 
82 inline string kite::cancelOrder(
83  const string& variety, const string& orderId, const string& parentOrderID) {
84  string endpoint;
85  utils::FmtArgs fmtArgs;
86  if (variety == "bo") {
87  endpoint = "order.cancel.bo";
88  fmtArgs = { variety, orderId, parentOrderID };
89  } else {
90  endpoint = "order.cancel";
91  fmtArgs = { variety, orderId };
92  };
93  return callApi<string, utils::json::JsonObject, true>(
94  endpoint, {}, fmtArgs, [](utils::json::JsonObject& data) {
95  return utils::json::get<string>(data, "order_id");
96  });
97 };
98 
99 inline std::vector<order> kite::orders() {
100  return callApi<std::vector<order>, utils::json::JsonArray, true>(
101  "orders", {}, {}, [](utils::json::JsonArray& data) {
102  std::vector<order> Orders;
103  for (auto& i : data) { Orders.emplace_back(i.GetObject()); }
104  return Orders;
105  });
106 };
107 
108 inline std::vector<order> kite::orderHistory(const string& orderId) {
109  return callApi<std::vector<order>, utils::json::JsonArray, true>(
110  "order.info", {}, { orderId }, [](utils::json::JsonArray& data) {
111  std::vector<order> history;
112  for (auto& i : data) { history.emplace_back(i.GetObject()); }
113  return history;
114  });
115 };
116 
117 inline std::vector<trade> kite::trades() {
118  return callApi<std::vector<trade>, utils::json::JsonArray, true>(
119  "trades", {}, {}, [](utils::json::JsonArray& data) {
120  std::vector<trade> trades;
121  for (auto& i : data) { trades.emplace_back(i.GetObject()); }
122  return trades;
123  });
124 };
125 
126 inline std::vector<trade> kite::orderTrades(const string& orderId) {
127  return callApi<std::vector<trade>, utils::json::JsonArray, true>(
128  "order.trades", {}, { orderId }, [](utils::json::JsonArray& data) {
129  std::vector<trade> trades;
130  for (auto& i : data) { trades.emplace_back(i.GetObject()); }
131  return trades;
132  });
133 };
134 } // namespace kiteconnect
kiteconnect::kite::trades
std::vector< trade > trades()
Get list of trades.
Definition: order.hpp:117
kiteconnect::kite::cancelOrder
string cancelOrder(const string &variety, const string &orderId, const string &parentOrderId="")
cancel an order
Definition: order.hpp:82
kiteconnect::placeOrderParams
Parameters required for the placeOrder method.
Definition: order.hpp:43
kiteconnect::kite::orderHistory
std::vector< order > orderHistory(const string &orderId)
Get history of an order.
Definition: order.hpp:108
kiteconnect::kite::orders
std::vector< order > orders()
Get list of orders.
Definition: order.hpp:99
kiteconnect::kite::modifyOrder
string modifyOrder(const modifyOrderParams &params)
modify an order
Definition: order.hpp:64
kiteconnect::kite::orderTrades
std::vector< trade > orderTrades(const string &orderId)
Get the list of trades executed for a particular order.
Definition: order.hpp:126
kiteconnect::modifyOrderParams
Parameters required for the modifyOrder method.
Definition: order.hpp:90
kiteconnect::kite::placeOrder
string placeOrder(const placeOrderParams &params)
Place an order.
Definition: order.hpp:35