Skip to content

Commit

Permalink
feat: add charges,leverage and unit tests for order margins
Browse files Browse the repository at this point in the history
  • Loading branch information
ranjanrak committed Jan 7, 2023
1 parent 03ff18c commit c7d61dd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
22 changes: 22 additions & 0 deletions margins.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,31 @@ type OrderMargins struct {
Cash float64 `json:"cash"`
VAR float64 `json:"var"`
PNL PNL `json:"pnl"`
Leverage float64 `json:"leverage"`
Charges Charges `json:"charges"`
Total float64 `json:"total"`
}

// 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"`
Expand Down
56 changes: 49 additions & 7 deletions margins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,62 @@ func (ts *TestSuite) TestGetOrderMargins(t *testing.T) {
TriggerPrice: 0,
}

orderResponse, err := ts.KiteConnect.GetOrderMargins(GetMarginParams{
compactOrderResp, err := ts.KiteConnect.GetOrderMargins(GetMarginParams{
OrderParams: []OrderMarginParam{params},
Compact: true,
})
if err != nil {
t.Errorf("Error while getting order margins: %v", err)
t.Errorf("Error while getting compact order margins: %v", err)
}

if len(orderResponse) != 1 {
t.Errorf("Incorrect response, expected len(orderResponse) to be 0, got: %v", len(orderResponse))
if len(compactOrderResp) != 1 {
t.Errorf("Incorrect response length, expected len(compactOrderResp) to be 1, got: %v", len(compactOrderResp))
}

if orderResponse[0].Total != 961.45 {
t.Errorf("Incorrect total, expected 961.45, got: %v", orderResponse[0].Total)
if compactOrderResp[0].TradingSymbol != "INFY" {
t.Errorf("Incorrect tradingsymbol, expected INFY, got: %v", compactOrderResp[0].TradingSymbol)
}

if compactOrderResp[0].Total == 0 {
t.Errorf("Incorrect compact total margins, got: %v", compactOrderResp[0].Total)
}

// Detailed order margin tests include charges, leverage
detailOrderResp, err := ts.KiteConnect.GetOrderMargins(GetMarginParams{
OrderParams: []OrderMarginParam{params},
Compact: false,
})

if err != nil {
t.Errorf("Error while getting detailed order margins: %v", err)
}

if detailOrderResp[0].Leverage != 1 {
t.Errorf("Incorrect leverage multiplier, expected 1x, got: %v", detailOrderResp[0].TradingSymbol)
}

if len(detailOrderResp) != 1 {
t.Errorf("Incorrect response, expected len(detailOrderResp) to be 1, got: %v", len(detailOrderResp))
}

if detailOrderResp[0].Charges.TransactionTax == 0 {
t.Errorf("Incorrect TransactionTax in detailed order margins, got: %v", detailOrderResp[0].Charges.TransactionTax)
}

if detailOrderResp[0].Charges.StampDuty == 0 {
t.Errorf("Incorrect StampDuty in detailed order margins, got: %v", detailOrderResp[0].Charges.StampDuty)
}

if detailOrderResp[0].Charges.GST.Total == 0 {
t.Errorf("Incorrect GST in detailed order margins, got: %v", detailOrderResp[0].Charges.GST.Total)
}

if detailOrderResp[0].Charges.Total == 0 {
t.Errorf("Incorrect charges total in detailed order margins, got: %v", detailOrderResp[0].Charges.Total)
}

if detailOrderResp[0].Total == 0 {
t.Errorf("Incorrect total margin in detailed order margins, got: %v", detailOrderResp[0].Total)
}
}

Expand All @@ -55,7 +97,7 @@ func (ts *TestSuite) TestGetBasketMargins(t *testing.T) {
ConsiderPositions: true,
})
if err != nil {
t.Errorf("Error while getting basket order margins: %v", err)
t.Errorf("Error while getting compact basket order margins: %v", err)
}

if len(orderResponseBasket.Orders) != 2 {
Expand Down
2 changes: 1 addition & 1 deletion mock_responses

0 comments on commit c7d61dd

Please sign in to comment.