Skip to content

Commit e282f42

Browse files
authored
Merge pull request #12 from ranjanrak/order-margin
Add:Order Margin API
2 parents ed56748 + f71cba8 commit e282f42

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

examples/kiteconnect_sample.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,15 @@
4444
])["order_id"];
4545

4646
echo "Order id is ".$order_id;
47+
48+
// fetch order margin
49+
$order_param = array(array("exchange" => "NSE",
50+
"tradingsymbol" => "INFY",
51+
"transaction_type" => $kite::TRANSACTION_TYPE_BUY,
52+
"variety" => $kite::VARIETY_REGULAR,
53+
"product" => $kite::PRODUCT_CNC,
54+
"order_type" => $kite::ORDER_TYPE_MARKET,
55+
"quantity" => 1
56+
),);
57+
print_r($kite->orderMargins($order_param));
4758
?>

kiteconnect.php

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class KiteConnect {
122122
"order.modify" => "/orders/{variety}/{order_id}",
123123
"order.cancel" => "/orders/{variety}/{order_id}",
124124
"order.trades" => "/orders/{order_id}/trades",
125+
"order.margins" => "/margins/orders",
125126

126127
"portfolio.positions" => "/portfolio/positions",
127128
"portfolio.holdings" => "/portfolio/holdings",
@@ -497,6 +498,25 @@ public function getOrderHistory($order_id) {
497498
return $this->_format_response_array($this->_get("order.info", ["order_id" => $order_id]));
498499
}
499500

501+
/**
502+
* Fetch order margin
503+
*
504+
* @param array $params Order params to fetch margin detail
505+
* $params string "exchange" Name of the exchange(eg. NSE, BSE, NFO, CDS, MCX)
506+
* $params string "tradingsymbol" Trading symbol of the instrument
507+
* $params string "transaction_type" eg. BUY, SELL
508+
* $params string "variety" Order variety (regular, amo, bo, co etc.)
509+
* $params string "product" Margin product to use for the order
510+
* $params string "order_type" Order type (MARKET, LIMIT etc.)
511+
* $params int "quantity" Quantity of the order
512+
* $params float|null "price" Price at which the order is going to be placed (LIMIT orders)
513+
* $params float|null "trigger_price" Trigger price (for SL, SL-M, CO orders)
514+
* @return array
515+
*/
516+
public function orderMargins($params) {
517+
return $this->_post("order.margins", json_encode($params), 'Content-type: application/json');
518+
}
519+
500520
/**
501521
* Retrieve the list of trades executed.
502522
* @return array
@@ -650,12 +670,14 @@ public function getLTP($instruments) {
650670
* $params bool "continuous" is a bool flag to get continuous data for futures and options instruments. Defaults to false.
651671
* @return array
652672
*/
653-
public function getHistoricalData($instrument_token, $interval, $from, $to, $continuous = false) {
673+
public function getHistoricalData($instrument_token, $interval, $from, $to, $continuous = false, $oi = false) {
654674
$params = [
655675
"instrument_token" => $instrument_token,
656676
"interval" => $interval,
657677
"from" => $from,
658-
"to" => $to
678+
"to" => $to,
679+
"continuous" => $continuous,
680+
"oi" => $oi
659681
];
660682

661683
if ($from instanceof DateTime) {
@@ -666,12 +688,18 @@ public function getHistoricalData($instrument_token, $interval, $from, $to, $con
666688
$params["to"] = $to->format("Y-m-d H:i:s");
667689
}
668690

669-
if (empty($params["continuous"]) || $continuous == false) {
691+
if ($params["continuous"] == false) {
670692
$params["continuous"] = 0;
671693
} else {
672694
$params["continuous"] = 1;
673695
}
674696

697+
if ($params["oi"] == false) {
698+
$params["oi"] = 0;
699+
} else {
700+
$params["oi"] = 1;
701+
}
702+
675703
$data = $this->_get("market.historical", $params);
676704

677705
$records = [];
@@ -683,6 +711,9 @@ public function getHistoricalData($instrument_token, $interval, $from, $to, $con
683711
$r->low = $j[3];
684712
$r->close = $j[4];
685713
$r->volume = $j[5];
714+
if (!empty($j[6])) {
715+
$r->oi = $j[6];
716+
}
686717

687718
$records[] = $r;
688719
}
@@ -824,7 +855,7 @@ public function getGTTs() {
824855
}
825856

826857
/**
827-
* Get history of the individual order.
858+
* Get detail of individual GTT order.
828859
* @param string $trigger_id "trigger_id" Trigger ID
829860
* @return array
830861
*/
@@ -833,7 +864,7 @@ public function getGTT($trigger_id) {
833864
}
834865

835866
/**
836-
* Cancel an open order.
867+
* Delete an GTT order
837868
* @param string $trigger_id "trigger_id" Trigger ID
838869
* @return void
839870
*/
@@ -989,8 +1020,8 @@ private function _format_response_array($data) {
9891020
* @param array|null $params Request parameters.
9901021
* @return mixed Array or object (deserialised JSON).
9911022
*/
992-
private function _get($route, $params=null) {
993-
return $this->_request($route, "GET", $params);
1023+
private function _get($route, $params=null, $header_content=null) {
1024+
return $this->_request($route, "GET", $params, $header_content);
9941025
}
9951026

9961027
/**
@@ -1000,8 +1031,8 @@ private function _get($route, $params=null) {
10001031
* @param array|null $params Request parameters.
10011032
* @return mixed Array or object (deserialised JSON).
10021033
*/
1003-
private function _post($route, $params=null) {
1004-
return $this->_request($route, "POST", $params);
1034+
private function _post($route, $params=null, $header_content=null) {
1035+
return $this->_request($route, "POST", $params, $header_content);
10051036
}
10061037

10071038
/**
@@ -1011,8 +1042,8 @@ private function _post($route, $params=null) {
10111042
* @param array|null $params Request parameters.
10121043
* @return mixed Array or object (deserialised JSON).
10131044
*/
1014-
private function _put($route, $params=null) {
1015-
return $this->_request($route, "PUT", $params);
1045+
private function _put($route, $params=null, $header_content=null) {
1046+
return $this->_request($route, "PUT", $params, $header_content);
10161047
}
10171048

10181049
/**
@@ -1022,8 +1053,8 @@ private function _put($route, $params=null) {
10221053
* @param array|null $params Request parameters.
10231054
* @return mixed Array or object (deserialised JSON).
10241055
*/
1025-
private function _delete($route, $params=null) {
1026-
return $this->_request($route, "DELETE", $params);
1056+
private function _delete($route, $params=null, $header_content=null) {
1057+
return $this->_request($route, "DELETE", $params, $header_content);
10271058
}
10281059

10291060
/**
@@ -1034,7 +1065,7 @@ private function _delete($route, $params=null) {
10341065
* @param array|null $params Request parameters.
10351066
* @return mixed Array or object (deserialised JSON).
10361067
*/
1037-
private function _request($route, $method, $params=null) {
1068+
private function _request($route, $method, $params, $header_content) {
10381069
$uri = $this->_routes[$route];
10391070

10401071
// 'RESTful' URLs.
@@ -1051,17 +1082,21 @@ private function _request($route, $method, $params=null) {
10511082
var_dump($params);
10521083
}
10531084

1054-
// Prepare the payload.
1055-
$request_headers = ["Content-type: application/x-www-form-urlencoded",
1056-
"Accept-Encoding: gzip, deflate",
1057-
"Accept-Charset: UTF-8,*;q=0.5",
1085+
// Set the header content type, if not sent set it to default
1086+
if ($header_content){
1087+
$content_type = $header_content;
1088+
} else {
1089+
// default header content type of urlencoded
1090+
$content_type = "Content-type: application/x-www-form-urlencoded";
1091+
}
1092+
// Prepare the payload
1093+
$request_headers[] = [$content_type,
10581094
"User-Agent: phpkiteconnect/".self::_version,
10591095
"X-Kite-Version: 3"];
1060-
1096+
10611097
if ($this->api_key && $this->access_token) {
10621098
$request_headers[] = "Authorization: token " . $this->api_key . ":" . $this->access_token;
10631099
}
1064-
10651100
// Make the HTTP request.
10661101
if(function_exists("curl_init")) {
10671102
$resp = $this->_curl($url, $method, $request_headers, $params);
@@ -1075,7 +1110,6 @@ private function _request($route, $method, $params=null) {
10751110
if($this->debug) {
10761111
print("Response :" . $result . "\n");
10771112
}
1078-
10791113
if(empty($headers["content-type"])) {
10801114
throw new DataException("Unknown content-type in response");
10811115
} else if(strpos($headers["content-type"], "application/json") !== false) {
@@ -1092,7 +1126,6 @@ private function _request($route, $method, $params=null) {
10921126
return;
10931127
}
10941128
}
1095-
10961129
// Check if the exception class is defined.
10971130
if(class_exists($json->error_type)) {
10981131
throw new $json->error_type($json->message, $headers["status_code"]);
@@ -1183,6 +1216,9 @@ private function _curl($url, $method, $headers, $params=null) {
11831216
$payload = null;
11841217
if($payload = http_build_query($params && is_array($params) ? $params : [])) {
11851218
$payload = preg_replace("/%5B(\d+?)%5D/", "", $payload);
1219+
} else if(json_decode($params)){
1220+
// send json param payload
1221+
$payload = $params;
11861222
}
11871223

11881224
if($method == "POST" || $method == "PUT") {

0 commit comments

Comments
 (0)