CPPKiteConnect
mf.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 inline string kite::placeMfOrder(const placeMfOrderParams& params) {
36  // required parameters
37  utils::http::Params bodyParams = {
38  { "tradingsymbol", params.symbol },
39  { "transaction_type", params.transactionType },
40  };
41  // optional parameters
42  utils::addParam(bodyParams, params.quantity, "quantity");
43  utils::addParam(bodyParams, params.amount, "amount");
44  utils::addParam(bodyParams, params.tag, "tag");
45 
46  return callApi<string, utils::json::JsonObject, true>(
47  "mf.order.place", bodyParams, {}, [](utils::json::JsonObject& data) {
48  return utils::json::get<string>(data, "order_id");
49  });
50 };
51 
52 inline string kite::cancelMfOrder(const string& orderId) {
53  return callApi<string, utils::json::JsonObject, true>(
54  "mf.order.cancel", {}, { orderId }, [](utils::json::JsonObject& data) {
55  return utils::json::get<string>(data, "order_id");
56  });
57 };
58 
59 inline std::vector<mfOrder> kite::getMfOrders() {
60  return callApi<std::vector<mfOrder>, utils::json::JsonArray, true>(
61  "mf.orders", {}, {}, [](utils::json::JsonArray& data) {
62  std::vector<mfOrder> Orders;
63  for (auto& i : data) { Orders.emplace_back(i.GetObject()); }
64  return Orders;
65  });
66 };
67 
68 inline mfOrder kite::getMfOrder(const string& orderId) {
69  return callApi<mfOrder, utils::json::JsonObject>(
70  "mf.order.info", {}, { orderId });
71 };
72 
73 inline std::vector<mfHolding> kite::getMfHoldings() {
74  return callApi<std::vector<mfHolding>, utils::json::JsonArray, true>(
75  "mf.holdings", {}, {}, [](utils::json::JsonArray& data) {
76  std::vector<mfHolding> holdings;
77  for (auto& i : data) { holdings.emplace_back(i.GetObject()); }
78  return holdings;
79  });
80 };
81 
83  // required parameters
84  utils::http::Params bodyParams = {
85  { "tradingsymbol", params.symbol },
86  { "amount", std::to_string(params.amount) },
87  { "instalments", std::to_string(params.installments) },
88  { "frequency", params.frequency },
89  };
90  // optional parameters
91  utils::addParam(bodyParams, params.initialAmount, "initial_amount");
92  utils::addParam(bodyParams, params.installmentDay, "instalment_day");
93  utils::addParam(bodyParams, params.tag, "tag");
94 
95  return callApi<placeMfSipResponse, utils::json::JsonObject>(
96  "mf.sip.place", bodyParams);
97 };
98 
99 inline string kite::modifyMfSip(const modifyMfSipParams& params) {
100  utils::http::Params bodyParams = {};
101  // optional parameters
102  utils::addParam(bodyParams, params.amount, "amount");
103  utils::addParam(bodyParams, params.status, "status");
104  utils::addParam(bodyParams, params.installments, "instalments");
105  utils::addParam(bodyParams, params.frequency, "frequency");
106  utils::addParam(bodyParams, params.installmentDay, "instalment_day");
107 
108  return callApi<string, utils::json::JsonObject, true>("mf.sip.modify",
109  bodyParams, { params.sipId }, [](utils::json::JsonObject& data) {
110  return utils::json::get<string>(data, "order_id");
111  });
112 };
113 
114 inline string kite::cancelMfSip(const string& sipId) {
115  return callApi<string, utils::json::JsonObject, true>(
116  "mf.sip.cancel", {}, { sipId }, [](utils::json::JsonObject& data) {
117  return utils::json::get<string>(data, "sip_id");
118  });
119 };
120 
121 inline std::vector<mfSip> kite::getSips() {
122  return callApi<std::vector<mfSip>, utils::json::JsonArray, true>(
123  "mf.sips", {}, {}, [](utils::json::JsonArray& data) {
124  std::vector<mfSip> sips;
125  for (auto& i : data) { sips.emplace_back(i.GetObject()); }
126  return sips;
127  });
128 };
129 
130 inline mfSip kite::getSip(const string& sipId) {
131  return callApi<mfSip, utils::json::JsonObject>(
132  "mf.sip.info", {}, { sipId });
133 };
134 
135 inline std::vector<mfInstrument> kite::getMfInstruments() {
136  const auto response = sendReq(endpoints.at("mf.instruments"), {}, {});
137  if (!response) { return {}; };
138 
139  return utils::parseInstruments<mfInstrument>(response.rawBody);
140 };
141 
142 } // namespace kiteconnect
kiteconnect::kite::getMfInstruments
std::vector< mfInstrument > getMfInstruments()
Get the list of mutual fund instruments available for trading.
Definition: mf.hpp:135
kiteconnect::mfOrder
Represents information of a mutual fund order.
Definition: mf.hpp:113
kiteconnect::kite::getMfOrder
mfOrder getMfOrder(const string &orderId)
Get details of a particular mutual fund order.
Definition: mf.hpp:68
kiteconnect::placeMfOrderParams
Parameters required for the placeMfOrder method.
Definition: mf.hpp:42
kiteconnect::kite::getMfHoldings
std::vector< mfHolding > getMfHoldings()
Get mutual fund holdings.
Definition: mf.hpp:73
kiteconnect::kite::cancelMfSip
string cancelMfSip(const string &sipId)
Cancel a mutual fund SIP.
Definition: mf.hpp:114
kiteconnect::mfSip
Represents information of a mutual fund SIP.
Definition: mf.hpp:188
kiteconnect::kite::getSip
mfSip getSip(const string &sipId)
Get details of a particular SIP.
Definition: mf.hpp:130
kiteconnect::kite::sendReq
utils::http::response sendReq(const utils::http::endpoint &endpoint, const utils::http::Params &body, const utils::FmtArgs &fmtArgs)
send a http request with the context used by kite
Definition: internal.hpp:61
kiteconnect::kite::holdings
std::vector< holding > holdings()
Get holdings.
Definition: portfolio.hpp:35
kiteconnect::kite::modifyMfSip
string modifyMfSip(const modifyMfSipParams &params)
Modify a mutual SIP order.
Definition: mf.hpp:99
kiteconnect::kite::getMfOrders
std::vector< mfOrder > getMfOrders()
Get mutual fund orders.
Definition: mf.hpp:59
kiteconnect::kite::placeMfOrder
string placeMfOrder(const placeMfOrderParams &params)
Place a mutual fund order.
Definition: mf.hpp:35
kiteconnect::placeMfSipResponse
Represents response returned by the placeMfSip method.
Definition: mf.hpp:80
kiteconnect::kite::getSips
std::vector< mfSip > getSips()
Get list of SIPs.
Definition: mf.hpp:121
kiteconnect::kite::cancelMfOrder
string cancelMfOrder(const string &orderId)
Cancel a mutual fund order.
Definition: mf.hpp:52
kiteconnect::modifyMfSipParams
Parameters required for the modifyMfSip method.
Definition: mf.hpp:94
kiteconnect::placeMfSipParams
Parameters required for the placeMfSip method.
Definition: mf.hpp:58
kiteconnect::kite::placeMfSip
placeMfSipResponse placeMfSip(const placeMfSipParams &params)
Place a mutual fund SIP order.
Definition: mf.hpp:82