From afccd9c1d94484063ae1bcd5b9a6c80dd0e196a0 Mon Sep 17 00:00:00 2001 From: Lakshay Kalbhor Date: Thu, 27 Jul 2023 17:18:10 +0530 Subject: [PATCH 1/6] feat: APIs for order charges --- connect.go | 1 + connect_test.go | 1 + margins.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ margins_test.go | 28 ++++++++++++++++++++++++++ mock_responses | 2 +- 5 files changed, 83 insertions(+), 1 deletion(-) 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..e91adad 100644 --- a/margins.go +++ b/margins.go @@ -19,6 +19,19 @@ type OrderMarginParam struct { TriggerPrice float64 `json:"trigger_price,omitempty"` } +// OrderMarginParam represents an order in the Margin 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,18 @@ type OrderMargins struct { Total float64 `json:"total"` } +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 +107,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 +172,26 @@ func (c *Client) GetBasketMargins(baskparam GetBasketParams) (BasketMargins, err return out, nil } + +func (c *Client) GetOrdersCharges(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..28b8245 100644 --- a/margins_test.go +++ b/margins_test.go @@ -104,3 +104,31 @@ 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: "NSE", + Tradingsymbol: "INFY", + TransactionType: "BUY", + Variety: "regular", + Product: "CNC", + OrderType: "MARKET", + Quantity: 1, + AveragePrice: 10, + OrderID: "11111", + } + + orderResponseCharges, err := ts.KiteConnect.GetOrdersCharges(GetChargesParams{ + OrderParams: []OrderChargesParam{params}, + }) + if err != nil { + t.Errorf("Error while getting compact basket order margins: %v", err) + } + + if len(orderResponseCharges) != 1 { + t.Errorf("Incorrect response, expected len(orderResponseBasket.Orders) to be 2, 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 From 6bce21f36e84e3856c5ede0894b9f3fbed1d0ddf Mon Sep 17 00:00:00 2001 From: Lakshay Kalbhor Date: Thu, 27 Jul 2023 17:21:25 +0530 Subject: [PATCH 2/6] chore: lint changes --- margins.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/margins.go b/margins.go index e91adad..abc1578 100644 --- a/margins.go +++ b/margins.go @@ -19,7 +19,7 @@ type OrderMarginParam struct { TriggerPrice float64 `json:"trigger_price,omitempty"` } -// OrderMarginParam represents an order in the Margin Calculator API +// OrderChargesParam represents an order in the Charges Calculator API type OrderChargesParam struct { OrderID string `json:"order_id"` Exchange string `json:"exchange"` @@ -57,6 +57,7 @@ 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"` From 84ababc55b831dd6321365289d41d1ce42351e7c Mon Sep 17 00:00:00 2001 From: Lakshay Kalbhor Date: Thu, 27 Jul 2023 17:22:33 +0530 Subject: [PATCH 3/6] chore: lint changes --- margins.go | 2 +- margins_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/margins.go b/margins.go index abc1578..d070e27 100644 --- a/margins.go +++ b/margins.go @@ -174,7 +174,7 @@ func (c *Client) GetBasketMargins(baskparam GetBasketParams) (BasketMargins, err return out, nil } -func (c *Client) GetOrdersCharges(chargeParam GetChargesParams) ([]OrderCharges, error) { +func (c *Client) GetOrderCharges(chargeParam GetChargesParams) ([]OrderCharges, error) { body, err := json.Marshal(chargeParam.OrderParams) if err != nil { return []OrderCharges{}, err diff --git a/margins_test.go b/margins_test.go index 28b8245..f8c1a9e 100644 --- a/margins_test.go +++ b/margins_test.go @@ -121,7 +121,7 @@ func (ts *TestSuite) TestGetOrderCharges(t *testing.T) { OrderID: "11111", } - orderResponseCharges, err := ts.KiteConnect.GetOrdersCharges(GetChargesParams{ + orderResponseCharges, err := ts.KiteConnect.GetOrderCharges(GetChargesParams{ OrderParams: []OrderChargesParam{params}, }) if err != nil { From a881e5ccaa9f918cea9e9a1fbdf5821b0dbee685 Mon Sep 17 00:00:00 2001 From: Lakshay Kalbhor Date: Thu, 27 Jul 2023 17:57:25 +0530 Subject: [PATCH 4/6] fix: add input testcase corresponding to mock response --- margins_test.go | 50 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/margins_test.go b/margins_test.go index f8c1a9e..bede83c 100644 --- a/margins_test.go +++ b/margins_test.go @@ -109,26 +109,50 @@ func (ts *TestSuite) TestGetOrderCharges(t *testing.T) { t.Parallel() params := - OrderChargesParam{ - Exchange: "NSE", - Tradingsymbol: "INFY", - TransactionType: "BUY", - Variety: "regular", - Product: "CNC", - OrderType: "MARKET", - Quantity: 1, - AveragePrice: 10, - OrderID: "11111", + []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: "11111", + }, + { + Exchange: "NFO", + Tradingsymbol: "NIFTY2371317900PE", + TransactionType: "BUY", + Variety: "regular", + Product: "NRML", + OrderType: "LIMIT", + Quantity: 100, + AveragePrice: 1.5, + OrderID: "11111", + }, } orderResponseCharges, err := ts.KiteConnect.GetOrderCharges(GetChargesParams{ - OrderParams: []OrderChargesParam{params}, + OrderParams: params, }) if err != nil { - t.Errorf("Error while getting compact basket order margins: %v", err) + t.Errorf("Error while getting order charges: %v", err) } if len(orderResponseCharges) != 1 { - t.Errorf("Incorrect response, expected len(orderResponseBasket.Orders) to be 2, got: %v", len(orderResponseCharges)) + t.Errorf("Incorrect response, expected len(orderResponseCharges) to be 3, got: %v", len(orderResponseCharges)) } } From b10c58a7740455512f5d805056a6d3a9170af507 Mon Sep 17 00:00:00 2001 From: Lakshay Kalbhor Date: Thu, 27 Jul 2023 17:59:38 +0530 Subject: [PATCH 5/6] fix: add input testcase corresponding to mock response --- margins_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/margins_test.go b/margins_test.go index bede83c..38c8ba2 100644 --- a/margins_test.go +++ b/margins_test.go @@ -152,7 +152,7 @@ func (ts *TestSuite) TestGetOrderCharges(t *testing.T) { t.Errorf("Error while getting order charges: %v", err) } - if len(orderResponseCharges) != 1 { + if len(orderResponseCharges) != 3 { t.Errorf("Incorrect response, expected len(orderResponseCharges) to be 3, got: %v", len(orderResponseCharges)) } } From fb2f9fba09c285f8f4de8b3958179f84b02d7793 Mon Sep 17 00:00:00 2001 From: Lakshay Kalbhor Date: Fri, 28 Jul 2023 11:22:35 +0530 Subject: [PATCH 6/6] fix: pass unique order_id in test cases --- margins_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/margins_test.go b/margins_test.go index 38c8ba2..e0e2a87 100644 --- a/margins_test.go +++ b/margins_test.go @@ -130,7 +130,7 @@ func (ts *TestSuite) TestGetOrderCharges(t *testing.T) { OrderType: "LIMIT", Quantity: 1, AveragePrice: 5862, - OrderID: "11111", + OrderID: "22222", }, { Exchange: "NFO", @@ -141,7 +141,7 @@ func (ts *TestSuite) TestGetOrderCharges(t *testing.T) { OrderType: "LIMIT", Quantity: 100, AveragePrice: 1.5, - OrderID: "11111", + OrderID: "33333", }, }