Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add methods for virtual contract note API #97

Merged
merged 6 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const (

URIOrderMargins string = "/margins/orders"
URIBasketMargins string = "/margins/basket"
URIOrderCharges string = "/charges/orders"

// MF endpoints
URIGetMFOrders string = "/mf/orders"
Expand Down
1 change: 1 addition & 0 deletions connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
53 changes: 53 additions & 0 deletions margins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
52 changes: 52 additions & 0 deletions margins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
2 changes: 1 addition & 1 deletion mock_responses
Loading