Skip to content

Commit

Permalink
Merge pull request #12 from ranjanrak/order-margin
Browse files Browse the repository at this point in the history
Add:Order Margin API
  • Loading branch information
vividvilla authored Feb 11, 2021
2 parents ed56748 + f71cba8 commit e282f42
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
11 changes: 11 additions & 0 deletions examples/kiteconnect_sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,15 @@
])["order_id"];

echo "Order id is ".$order_id;

// fetch order margin
$order_param = array(array("exchange" => "NSE",
"tradingsymbol" => "INFY",
"transaction_type" => $kite::TRANSACTION_TYPE_BUY,
"variety" => $kite::VARIETY_REGULAR,
"product" => $kite::PRODUCT_CNC,
"order_type" => $kite::ORDER_TYPE_MARKET,
"quantity" => 1
),);
print_r($kite->orderMargins($order_param));
?>
80 changes: 58 additions & 22 deletions kiteconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class KiteConnect {
"order.modify" => "/orders/{variety}/{order_id}",
"order.cancel" => "/orders/{variety}/{order_id}",
"order.trades" => "/orders/{order_id}/trades",
"order.margins" => "/margins/orders",

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

/**
* Fetch order margin
*
* @param array $params Order params to fetch margin detail
* $params string "exchange" Name of the exchange(eg. NSE, BSE, NFO, CDS, MCX)
* $params string "tradingsymbol" Trading symbol of the instrument
* $params string "transaction_type" eg. BUY, SELL
* $params string "variety" Order variety (regular, amo, bo, co etc.)
* $params string "product" Margin product to use for the order
* $params string "order_type" Order type (MARKET, LIMIT etc.)
* $params int "quantity" Quantity of the order
* $params float|null "price" Price at which the order is going to be placed (LIMIT orders)
* $params float|null "trigger_price" Trigger price (for SL, SL-M, CO orders)
* @return array
*/
public function orderMargins($params) {
return $this->_post("order.margins", json_encode($params), 'Content-type: application/json');
}

/**
* Retrieve the list of trades executed.
* @return array
Expand Down Expand Up @@ -650,12 +670,14 @@ public function getLTP($instruments) {
* $params bool "continuous" is a bool flag to get continuous data for futures and options instruments. Defaults to false.
* @return array
*/
public function getHistoricalData($instrument_token, $interval, $from, $to, $continuous = false) {
public function getHistoricalData($instrument_token, $interval, $from, $to, $continuous = false, $oi = false) {
$params = [
"instrument_token" => $instrument_token,
"interval" => $interval,
"from" => $from,
"to" => $to
"to" => $to,
"continuous" => $continuous,
"oi" => $oi
];

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

if (empty($params["continuous"]) || $continuous == false) {
if ($params["continuous"] == false) {
$params["continuous"] = 0;
} else {
$params["continuous"] = 1;
}

if ($params["oi"] == false) {
$params["oi"] = 0;
} else {
$params["oi"] = 1;
}

$data = $this->_get("market.historical", $params);

$records = [];
Expand All @@ -683,6 +711,9 @@ public function getHistoricalData($instrument_token, $interval, $from, $to, $con
$r->low = $j[3];
$r->close = $j[4];
$r->volume = $j[5];
if (!empty($j[6])) {
$r->oi = $j[6];
}

$records[] = $r;
}
Expand Down Expand Up @@ -824,7 +855,7 @@ public function getGTTs() {
}

/**
* Get history of the individual order.
* Get detail of individual GTT order.
* @param string $trigger_id "trigger_id" Trigger ID
* @return array
*/
Expand All @@ -833,7 +864,7 @@ public function getGTT($trigger_id) {
}

/**
* Cancel an open order.
* Delete an GTT order
* @param string $trigger_id "trigger_id" Trigger ID
* @return void
*/
Expand Down Expand Up @@ -989,8 +1020,8 @@ private function _format_response_array($data) {
* @param array|null $params Request parameters.
* @return mixed Array or object (deserialised JSON).
*/
private function _get($route, $params=null) {
return $this->_request($route, "GET", $params);
private function _get($route, $params=null, $header_content=null) {
return $this->_request($route, "GET", $params, $header_content);
}

/**
Expand All @@ -1000,8 +1031,8 @@ private function _get($route, $params=null) {
* @param array|null $params Request parameters.
* @return mixed Array or object (deserialised JSON).
*/
private function _post($route, $params=null) {
return $this->_request($route, "POST", $params);
private function _post($route, $params=null, $header_content=null) {
return $this->_request($route, "POST", $params, $header_content);
}

/**
Expand All @@ -1011,8 +1042,8 @@ private function _post($route, $params=null) {
* @param array|null $params Request parameters.
* @return mixed Array or object (deserialised JSON).
*/
private function _put($route, $params=null) {
return $this->_request($route, "PUT", $params);
private function _put($route, $params=null, $header_content=null) {
return $this->_request($route, "PUT", $params, $header_content);
}

/**
Expand All @@ -1022,8 +1053,8 @@ private function _put($route, $params=null) {
* @param array|null $params Request parameters.
* @return mixed Array or object (deserialised JSON).
*/
private function _delete($route, $params=null) {
return $this->_request($route, "DELETE", $params);
private function _delete($route, $params=null, $header_content=null) {
return $this->_request($route, "DELETE", $params, $header_content);
}

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

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

// Prepare the payload.
$request_headers = ["Content-type: application/x-www-form-urlencoded",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: UTF-8,*;q=0.5",
// Set the header content type, if not sent set it to default
if ($header_content){
$content_type = $header_content;
} else {
// default header content type of urlencoded
$content_type = "Content-type: application/x-www-form-urlencoded";
}
// Prepare the payload
$request_headers[] = [$content_type,
"User-Agent: phpkiteconnect/".self::_version,
"X-Kite-Version: 3"];

if ($this->api_key && $this->access_token) {
$request_headers[] = "Authorization: token " . $this->api_key . ":" . $this->access_token;
}

// Make the HTTP request.
if(function_exists("curl_init")) {
$resp = $this->_curl($url, $method, $request_headers, $params);
Expand All @@ -1075,7 +1110,6 @@ private function _request($route, $method, $params=null) {
if($this->debug) {
print("Response :" . $result . "\n");
}

if(empty($headers["content-type"])) {
throw new DataException("Unknown content-type in response");
} else if(strpos($headers["content-type"], "application/json") !== false) {
Expand All @@ -1092,7 +1126,6 @@ private function _request($route, $method, $params=null) {
return;
}
}

// Check if the exception class is defined.
if(class_exists($json->error_type)) {
throw new $json->error_type($json->message, $headers["status_code"]);
Expand Down Expand Up @@ -1183,6 +1216,9 @@ private function _curl($url, $method, $headers, $params=null) {
$payload = null;
if($payload = http_build_query($params && is_array($params) ? $params : [])) {
$payload = preg_replace("/%5B(\d+?)%5D/", "", $payload);
} else if(json_decode($params)){
// send json param payload
$payload = $params;
}

if($method == "POST" || $method == "PUT") {
Expand Down

0 comments on commit e282f42

Please sign in to comment.