diff --git a/connect.go b/connect.go index f130e9f..eeeea5e 100644 --- a/connect.go +++ b/connect.go @@ -113,6 +113,7 @@ const ( URIOrderMargins string = "/margins/orders" URIBasketMargins string = "/margins/basket" + URIOrderCharges string = "/charges/orders" // MF endpoints URIGetMFOrders string = "/mf/orders" diff --git a/connect_test.go b/connect_test.go index c32f0c7..af89948 100644 --- a/connect_test.go +++ b/connect_test.go @@ -157,6 +157,7 @@ var MockResponders = [][]string{ {http.MethodPost, URIOrderMargins, "order_margins.json"}, {http.MethodPost, URIBasketMargins, "basket_margins.json"}, {http.MethodPost, URIInitHoldingsAuth, "holdings_auth.json"}, + {http.MethodPost, URIOrderCharges, "virtual_contract_note.json"}, // DELETE endpoints {http.MethodDelete, URICancelOrder, "order_response.json"}, diff --git a/margins.go b/margins.go index 859cade..d070e27 100644 --- a/margins.go +++ b/margins.go @@ -19,6 +19,19 @@ type OrderMarginParam struct { 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"` @@ -44,6 +57,19 @@ type OrderMargins struct { 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"` @@ -82,6 +108,10 @@ type GetBasketParams struct { ConsiderPositions bool } +type GetChargesParams struct { + OrderParams []OrderChargesParam +} + func (c *Client) GetOrderMargins(marparam GetMarginParams) ([]OrderMargins, error) { body, err := json.Marshal(marparam.OrderParams) if err != nil { @@ -143,3 +173,26 @@ func (c *Client) GetBasketMargins(baskparam GetBasketParams) (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 +} diff --git a/margins_test.go b/margins_test.go index 43aea1c..e0e2a87 100644 --- a/margins_test.go +++ b/margins_test.go @@ -104,3 +104,55 @@ func (ts *TestSuite) TestGetBasketMargins(t *testing.T) { t.Errorf("Incorrect response, expected len(orderResponseBasket.Orders) to be 2, got: %v", len(orderResponseBasket.Orders)) } } + +func (ts *TestSuite) TestGetOrderCharges(t *testing.T) { + t.Parallel() + + params := + []OrderChargesParam{ + { + Exchange: "SBIN", + Tradingsymbol: "INFY", + TransactionType: "BUY", + Variety: "regular", + Product: "CNC", + OrderType: "MARKET", + Quantity: 1, + AveragePrice: 560, + OrderID: "11111", + }, + { + Exchange: "MCX", + Tradingsymbol: "GOLDPETAL23JULFUT", + TransactionType: "SELL", + Variety: "regular", + Product: "NRML", + OrderType: "LIMIT", + Quantity: 1, + AveragePrice: 5862, + OrderID: "22222", + }, + { + Exchange: "NFO", + Tradingsymbol: "NIFTY2371317900PE", + TransactionType: "BUY", + Variety: "regular", + Product: "NRML", + OrderType: "LIMIT", + Quantity: 100, + AveragePrice: 1.5, + OrderID: "33333", + }, + } + + orderResponseCharges, err := ts.KiteConnect.GetOrderCharges(GetChargesParams{ + OrderParams: params, + }) + if err != nil { + t.Errorf("Error while getting order charges: %v", err) + } + + if len(orderResponseCharges) != 3 { + t.Errorf("Incorrect response, expected len(orderResponseCharges) to be 3, got: %v", len(orderResponseCharges)) + } +} diff --git a/mock_responses b/mock_responses index 4a069d1..0dd520a 160000 --- a/mock_responses +++ b/mock_responses @@ -1 +1 @@ -Subproject commit 4a069d12778c226a6afaca205928dd391bde6ff5 +Subproject commit 0dd520a4b2d871d599920b0fbb7ba2c158499b93