forked from zerodha/gokiteconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
margins.go
198 lines (167 loc) · 5.4 KB
/
margins.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
package kiteconnect
import (
"encoding/json"
"net/http"
"net/url"
)
// OrderMarginParam represents an order in the Margin Calculator API
type OrderMarginParam struct {
Exchange string `json:"exchange"`
Tradingsymbol string `json:"tradingsymbol"`
TransactionType string `json:"transaction_type"`
Variety string `json:"variety"`
Product string `json:"product"`
OrderType string `json:"order_type"`
Quantity float64 `json:"quantity"`
Price float64 `json:"price,omitempty"`
TriggerPrice float64 `json:"trigger_price,omitempty"`
}
// OrderChargesParam represents an order in the Charges Calculator API
type OrderChargesParam struct {
OrderID string `json:"order_id"`
Exchange string `json:"exchange"`
Tradingsymbol string `json:"tradingsymbol"`
TransactionType string `json:"transaction_type"`
Variety string `json:"variety"`
Product string `json:"product"`
OrderType string `json:"order_type"`
Quantity float64 `json:"quantity"`
AveragePrice float64 `json:"average_price"`
}
// PNL represents the PNL
type PNL struct {
Realised float64 `json:"realised"`
Unrealised float64 `json:"unrealised"`
}
// OrdersMargins represents response from the Margin Calculator API.
type OrderMargins struct {
Type string `json:"type"`
TradingSymbol string `json:"tradingsymbol"`
Exchange string `json:"exchange"`
SPAN float64 `json:"span"`
Exposure float64 `json:"exposure"`
OptionPremium float64 `json:"option_premium"`
Additional float64 `json:"additional"`
BO float64 `json:"bo"`
Cash float64 `json:"cash"`
VAR float64 `json:"var"`
PNL PNL `json:"pnl"`
Leverage float64 `json:"leverage"`
Charges Charges `json:"charges"`
Total float64 `json:"total"`
}
// OrderCharges represent an item's response from the Charges calculator API
type OrderCharges struct {
Exchange string `json:"exchange"`
Tradingsymbol string `json:"tradingsymbol"`
TransactionType string `json:"transaction_type"`
Variety string `json:"variety"`
Product string `json:"product"`
OrderType string `json:"order_type"`
Quantity float64 `json:"quantity"`
Price float64 `json:"price"`
Charges Charges `json:"charges"`
}
// Charges represents breakdown of various charges that are applied to an order
type Charges struct {
TransactionTax float64 `json:"transaction_tax"`
TransactionTaxType string `json:"transaction_tax_type"`
ExchangeTurnoverCharge float64 `json:"exchange_turnover_charge"`
SEBITurnoverCharge float64 `json:"sebi_turnover_charge"`
Brokerage float64 `json:"brokerage"`
StampDuty float64 `json:"stamp_duty"`
GST GST `json:"gst"`
Total float64 `json:"total"`
}
// GST represents the various GST charges
type GST struct {
IGST float64 `json:"igst"`
CGST float64 `json:"cgst"`
SGST float64 `json:"sgst"`
Total float64 `json:"total"`
}
// BasketMargins represents response from the Margin Calculator API for Basket orders
type BasketMargins struct {
Initial OrderMargins `json:"initial"`
Final OrderMargins `json:"final"`
Orders []OrderMargins `json:"orders"`
}
type GetMarginParams struct {
OrderParams []OrderMarginParam
Compact bool
}
type GetBasketParams struct {
OrderParams []OrderMarginParam
Compact bool
ConsiderPositions bool
}
type GetChargesParams struct {
OrderParams []OrderChargesParam
}
func (c *Client) GetOrderMargins(marparam GetMarginParams) ([]OrderMargins, error) {
body, err := json.Marshal(marparam.OrderParams)
if err != nil {
return []OrderMargins{}, err
}
var headers http.Header = map[string][]string{}
headers.Add("Content-Type", "application/json")
uri := URIOrderMargins
if marparam.Compact {
uri += "?mode=compact"
}
resp, err := c.doRaw(http.MethodPost, uri, body, headers)
if err != nil {
return []OrderMargins{}, err
}
var out []OrderMargins
if err := readEnvelope(resp, &out); err != nil {
return []OrderMargins{}, err
}
return out, nil
}
func (c *Client) GetBasketMargins(baskparam GetBasketParams) (BasketMargins, error) {
body, err := json.Marshal(baskparam.OrderParams)
if err != nil {
return BasketMargins{}, err
}
var headers http.Header = map[string][]string{}
headers.Add("Content-Type", "application/json")
uri := URIBasketMargins
v := url.Values{}
if baskparam.Compact {
v.Set("mode", "compact")
}
if baskparam.ConsiderPositions {
v.Set("consider_positions", "true")
}
if qp := v.Encode(); qp != "" {
uri += "?" + qp
}
resp, err := c.doRaw(http.MethodPost, uri, body, headers)
if err != nil {
return BasketMargins{}, err
}
var out BasketMargins
if err := readEnvelope(resp, &out); err != nil {
return BasketMargins{}, err
}
return out, nil
}
func (c *Client) GetOrderCharges(chargeParam GetChargesParams) ([]OrderCharges, error) {
body, err := json.Marshal(chargeParam.OrderParams)
if err != nil {
return []OrderCharges{}, err
}
var headers http.Header = map[string][]string{}
headers.Add("Content-Type", "application/json")
uri := URIOrderCharges
resp, err := c.doRaw(http.MethodPost, uri, body, headers)
if err != nil {
return []OrderCharges{}, err
}
var out []OrderCharges
if err := readEnvelope(resp, &out); err != nil {
return []OrderCharges{}, err
}
return out, nil
}