-
Notifications
You must be signed in to change notification settings - Fork 73
/
mutualfunds.go
272 lines (232 loc) · 9.6 KB
/
mutualfunds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package kiteconnect
import (
"fmt"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"github.com/zerodha/gokiteconnect/v4/models"
)
// MFHolding represents a individual mutualfund holding.
type MFHolding struct {
Folio string `json:"folio"`
Fund string `json:"fund"`
Tradingsymbol string `json:"tradingsymbol"`
AveragePrice float64 `json:"average_price"`
LastPrice float64 `json:"last_price"`
LastPriceDate string `json:"last_price_date"`
Pnl float64 `json:"pnl"`
Quantity float64 `json:"quantity"`
}
// MFTrade represents a individual trades of a mutualfund holding.
type MFTrade struct {
Fund string `json:"fund"`
Tradingsymbol string `json:"tradingsymbol"`
AveragePrice float64 `json:"average_price"`
Variety string `json:"variety"`
ExchangeTimestamp models.Time `json:"exchange_timestamp"`
Amount float64 `json:"amount"`
Folio string `json:"folio"`
Quantity float64 `json:"quantity"`
}
// MFHoldingBreakdown represents a list of mutualfund holdings.
type MFHoldingBreakdown []MFTrade
// MFHoldings represents a list of mutualfund holdings.
type MFHoldings []MFHolding
// MFOrder represents a individual mutualfund order response.
type MFOrder struct {
OrderID string `json:"order_id"`
ExchangeOrderID string `json:"exchange_order_id"`
Tradingsymbol string `json:"tradingsymbol"`
Status string `json:"status"`
StatusMessage string `json:"status_message"`
Folio string `json:"folio"`
Fund string `json:"fund"`
OrderTimestamp models.Time `json:"order_timestamp"`
ExchangeTimestamp models.Time `json:"exchange_timestamp"`
SettlementID string `json:"settlement_id"`
TransactionType string `json:"transaction_type"`
Variety string `json:"variety"`
PurchaseType string `json:"purchase_type"`
Quantity float64 `json:"quantity"`
Amount float64 `json:"amount"`
LastPrice float64 `json:"last_price"`
AveragePrice float64 `json:"average_price"`
PlacedBy string `json:"placed_by"`
Tag string `json:"tag"`
}
// MFOrders represents a list of mutualfund orders.
type MFOrders []MFOrder
// MFAllottedISINs represents a list of all ISINs in which atleast one allotment is present.
type MFAllottedISINs []string
// MFSIPStepUp represents stepup date and percentage for SIPs.
type MFSIPStepUp map[string]int
// MFSIP represents a individual mutualfund SIP response.
type MFSIP struct {
ID string `json:"sip_id"`
Tradingsymbol string `json:"tradingsymbol"`
FundName string `json:"fund"`
DividendType string `json:"dividend_type"`
TransactionType string `json:"transaction_type"`
Status string `json:"status"`
SipType string `json:"sip_type"`
Created models.Time `json:"created"`
Frequency string `json:"frequency"`
InstalmentAmount float64 `json:"instalment_amount"`
Instalments int `json:"instalments"`
LastInstalment models.Time `json:"last_instalment"`
PendingInstalments int `json:"pending_instalments"`
InstalmentDay int `json:"instalment_day"`
CompletedInstalments int `json:"completed_instalments"`
NextInstalment string `json:"next_instalment"`
TriggerPrice float64 `json:"trigger_price"`
StepUp MFSIPStepUp `json:"step_up"`
Tag string `json:"tag"`
}
// MFSIPs represents a list of mutualfund SIPs.
type MFSIPs []MFSIP
// MFOrderResponse represents the successful order place response.
type MFOrderResponse struct {
OrderID string `json:"order_id"`
}
// MFSIPResponse represents the successful SIP place response.
type MFSIPResponse struct {
OrderID *string `json:"order_id"`
SIPID string `json:"sip_id"`
}
// MFOrderParams represents parameters for placing an order.
type MFOrderParams struct {
Tradingsymbol string `json:"tradingsymbol" url:"tradingsymbol"`
TransactionType string `json:"transaction_type" url:"transaction_type"`
Quantity float64 `json:"quantity" url:"quantity,omitempty"`
Amount float64 `json:"amount" url:"amount,omitempty"`
Tag string `json:"tag" url:"tag,omitempty"`
}
// MFSIPParams represents parameters for placing a SIP.
type MFSIPParams struct {
Tradingsymbol string `json:"tradingsymbol" url:"tradingsymbol"`
Amount float64 `json:"amount" url:"amount"`
Instalments int `json:"instalments" url:"instalments"`
Frequency string `json:"frequency" url:"frequency"`
InstalmentDay int `json:"instalment_day" url:"instalment_day,omitempty"`
InitialAmount float64 `json:"initial_amount" url:"initial_amount,omitempty"`
TriggerPrice float64 `json:"trigger_price" url:"trigger_price,omitempty"`
StepUp string `json:"step_up" url:"step_up,omitempty"`
SipType string `json:"sip_type" url:"sip_type,omitempty"`
Tag string `json:"tag" url:"tag,omitempty"`
}
// MFSIPModifyParams represents parameters for modifying a SIP.
type MFSIPModifyParams struct {
Amount float64 `json:"amount" url:"amount,omitempty"`
Frequency string `json:"frequency" url:"frequency,omitempty"`
InstalmentDay int `json:"instalment_day" url:"instalment_day,omitempty"`
Instalments int `json:"instalments" url:"instalments,omitempty"`
StepUp string `json:"step_up" url:"step_up,omitempty"`
Status string `json:"status" url:"status,omitempty"`
}
// GetMFOrders gets list of mutualfund orders.
func (c *Client) GetMFOrders() (MFOrders, error) {
var orders MFOrders
err := c.doEnvelope(http.MethodGet, URIGetMFOrders, nil, nil, &orders)
return orders, err
}
// GetMFOrderInfo get individual mutualfund order info.
func (c *Client) GetMFOrderInfo(OrderID string) (MFOrder, error) {
var orderInfo MFOrder
err := c.doEnvelope(http.MethodGet, fmt.Sprintf(URIGetMFOrderInfo, OrderID), nil, nil, &orderInfo)
return orderInfo, err
}
// GetMFOrdersByDate gets list of mutualfund orders for a custom date range.
func (c *Client) GetMFOrdersByDate(fromDate, toDate string) (MFOrders, error) {
var (
orders MFOrders
)
params := make(url.Values)
// from and to dates from request
params.Add("from", fromDate)
params.Add("to", toDate)
err := c.doEnvelope(http.MethodGet, URIGetMFOrders, params, nil, &orders)
return orders, err
}
// PlaceMFOrder places an mutualfund order.
func (c *Client) PlaceMFOrder(orderParams MFOrderParams) (MFOrderResponse, error) {
var (
orderResponse MFOrderResponse
params url.Values
err error
)
if params, err = query.Values(orderParams); err != nil {
return orderResponse, NewError(InputError, fmt.Sprintf("Error decoding order params: %v", err), nil)
}
err = c.doEnvelope(http.MethodPost, URIPlaceMFOrder, params, nil, &orderResponse)
return orderResponse, err
}
// GetMFSIPs gets list of mutualfund SIPs.
func (c *Client) GetMFSIPs() (MFSIPs, error) {
var sips MFSIPs
err := c.doEnvelope(http.MethodGet, URIGetMFSIPs, nil, nil, &sips)
return sips, err
}
// GetMFSIPInfo get individual SIP info.
func (c *Client) GetMFSIPInfo(sipID string) (MFSIP, error) {
var sip MFSIP
err := c.doEnvelope(http.MethodGet, fmt.Sprintf(URIGetMFSIPInfo, sipID), nil, nil, &sip)
return sip, err
}
// PlaceMFSIP places an mutualfund SIP order.
func (c *Client) PlaceMFSIP(sipParams MFSIPParams) (MFSIPResponse, error) {
var (
sipResponse MFSIPResponse
params url.Values
err error
)
if params, err = query.Values(sipParams); err != nil {
return sipResponse, NewError(InputError, fmt.Sprintf("Error decoding order params: %v", err), nil)
}
err = c.doEnvelope(http.MethodPost, URIPlaceMFSIP, params, nil, &sipResponse)
return sipResponse, err
}
// ModifyMFSIP modifies an mutualfund SIP.
func (c *Client) ModifyMFSIP(sipID string, sipParams MFSIPModifyParams) (MFSIPResponse, error) {
var (
sipResponse MFSIPResponse
params url.Values
err error
)
if params, err = query.Values(sipParams); err != nil {
return sipResponse, NewError(InputError, fmt.Sprintf("Error decoding order params: %v", err), nil)
}
err = c.doEnvelope(http.MethodPut, fmt.Sprintf(URIModifyMFSIP, sipID), params, nil, &sipResponse)
return sipResponse, err
}
// CancelMFSIP cancels an mutualfund SIP.
func (c *Client) CancelMFSIP(sipID string) (MFSIPResponse, error) {
var (
sipResponse MFSIPResponse
)
err := c.doEnvelope(http.MethodDelete, fmt.Sprintf(URICancelMFSIP, sipID), nil, nil, &sipResponse)
return sipResponse, err
}
// CancelMFOrder cancels an mutualfund order.
func (c *Client) CancelMFOrder(orderID string) (MFOrderResponse, error) {
var orderResponse MFOrderResponse
err := c.doEnvelope(http.MethodDelete, fmt.Sprintf(URICancelMFOrder, orderID), nil, nil, &orderResponse)
return orderResponse, err
}
// GetMFHoldings gets list of user mutualfund holdings.
func (c *Client) GetMFHoldings() (MFHoldings, error) {
var holdings MFHoldings
err := c.doEnvelope(http.MethodGet, URIGetMFHoldings, nil, nil, &holdings)
return holdings, err
}
// GetMFHoldingInfo get individual Holding info.
func (c *Client) GetMFHoldingInfo(isin string) (MFHoldingBreakdown, error) {
var holdingBreakdown MFHoldingBreakdown
err := c.doEnvelope(http.MethodGet, fmt.Sprintf(URIGetMFHoldingInfo, isin), nil, nil, &holdingBreakdown)
return holdingBreakdown, err
}
// GetMFAllottedISINs gets list of user mutualfund holdings.
func (c *Client) GetMFAllottedISINs() (MFAllottedISINs, error) {
var isins MFAllottedISINs
err := c.doEnvelope(http.MethodGet, URIGetAllotedISINs, nil, nil, &isins)
return isins, err
}