@@ -122,6 +122,7 @@ class KiteConnect {
122
122
"order.modify " => "/orders/{variety}/{order_id} " ,
123
123
"order.cancel " => "/orders/{variety}/{order_id} " ,
124
124
"order.trades " => "/orders/{order_id}/trades " ,
125
+ "order.margins " => "/margins/orders " ,
125
126
126
127
"portfolio.positions " => "/portfolio/positions " ,
127
128
"portfolio.holdings " => "/portfolio/holdings " ,
@@ -497,6 +498,25 @@ public function getOrderHistory($order_id) {
497
498
return $ this ->_format_response_array ($ this ->_get ("order.info " , ["order_id " => $ order_id ]));
498
499
}
499
500
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
+
500
520
/**
501
521
* Retrieve the list of trades executed.
502
522
* @return array
@@ -650,12 +670,14 @@ public function getLTP($instruments) {
650
670
* $params bool "continuous" is a bool flag to get continuous data for futures and options instruments. Defaults to false.
651
671
* @return array
652
672
*/
653
- public function getHistoricalData ($ instrument_token , $ interval , $ from , $ to , $ continuous = false ) {
673
+ public function getHistoricalData ($ instrument_token , $ interval , $ from , $ to , $ continuous = false , $ oi = false ) {
654
674
$ params = [
655
675
"instrument_token " => $ instrument_token ,
656
676
"interval " => $ interval ,
657
677
"from " => $ from ,
658
- "to " => $ to
678
+ "to " => $ to ,
679
+ "continuous " => $ continuous ,
680
+ "oi " => $ oi
659
681
];
660
682
661
683
if ($ from instanceof DateTime) {
@@ -666,12 +688,18 @@ public function getHistoricalData($instrument_token, $interval, $from, $to, $con
666
688
$ params ["to " ] = $ to ->format ("Y-m-d H:i:s " );
667
689
}
668
690
669
- if (empty ( $ params ["continuous " ]) || $ continuous == false ) {
691
+ if ($ params ["continuous " ] == false ) {
670
692
$ params ["continuous " ] = 0 ;
671
693
} else {
672
694
$ params ["continuous " ] = 1 ;
673
695
}
674
696
697
+ if ($ params ["oi " ] == false ) {
698
+ $ params ["oi " ] = 0 ;
699
+ } else {
700
+ $ params ["oi " ] = 1 ;
701
+ }
702
+
675
703
$ data = $ this ->_get ("market.historical " , $ params );
676
704
677
705
$ records = [];
@@ -683,6 +711,9 @@ public function getHistoricalData($instrument_token, $interval, $from, $to, $con
683
711
$ r ->low = $ j [3 ];
684
712
$ r ->close = $ j [4 ];
685
713
$ r ->volume = $ j [5 ];
714
+ if (!empty ($ j [6 ])) {
715
+ $ r ->oi = $ j [6 ];
716
+ }
686
717
687
718
$ records [] = $ r ;
688
719
}
@@ -824,7 +855,7 @@ public function getGTTs() {
824
855
}
825
856
826
857
/**
827
- * Get history of the individual order.
858
+ * Get detail of individual GTT order.
828
859
* @param string $trigger_id "trigger_id" Trigger ID
829
860
* @return array
830
861
*/
@@ -833,7 +864,7 @@ public function getGTT($trigger_id) {
833
864
}
834
865
835
866
/**
836
- * Cancel an open order.
867
+ * Delete an GTT order
837
868
* @param string $trigger_id "trigger_id" Trigger ID
838
869
* @return void
839
870
*/
@@ -989,8 +1020,8 @@ private function _format_response_array($data) {
989
1020
* @param array|null $params Request parameters.
990
1021
* @return mixed Array or object (deserialised JSON).
991
1022
*/
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 );
994
1025
}
995
1026
996
1027
/**
@@ -1000,8 +1031,8 @@ private function _get($route, $params=null) {
1000
1031
* @param array|null $params Request parameters.
1001
1032
* @return mixed Array or object (deserialised JSON).
1002
1033
*/
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 );
1005
1036
}
1006
1037
1007
1038
/**
@@ -1011,8 +1042,8 @@ private function _post($route, $params=null) {
1011
1042
* @param array|null $params Request parameters.
1012
1043
* @return mixed Array or object (deserialised JSON).
1013
1044
*/
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 );
1016
1047
}
1017
1048
1018
1049
/**
@@ -1022,8 +1053,8 @@ private function _put($route, $params=null) {
1022
1053
* @param array|null $params Request parameters.
1023
1054
* @return mixed Array or object (deserialised JSON).
1024
1055
*/
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 );
1027
1058
}
1028
1059
1029
1060
/**
@@ -1034,7 +1065,7 @@ private function _delete($route, $params=null) {
1034
1065
* @param array|null $params Request parameters.
1035
1066
* @return mixed Array or object (deserialised JSON).
1036
1067
*/
1037
- private function _request ($ route , $ method , $ params= null ) {
1068
+ private function _request ($ route , $ method , $ params, $ header_content ) {
1038
1069
$ uri = $ this ->_routes [$ route ];
1039
1070
1040
1071
// 'RESTful' URLs.
@@ -1051,17 +1082,21 @@ private function _request($route, $method, $params=null) {
1051
1082
var_dump ($ params );
1052
1083
}
1053
1084
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 ,
1058
1094
"User-Agent: phpkiteconnect/ " .self ::_version,
1059
1095
"X-Kite-Version: 3 " ];
1060
-
1096
+
1061
1097
if ($ this ->api_key && $ this ->access_token ) {
1062
1098
$ request_headers [] = "Authorization: token " . $ this ->api_key . ": " . $ this ->access_token ;
1063
1099
}
1064
-
1065
1100
// Make the HTTP request.
1066
1101
if (function_exists ("curl_init " )) {
1067
1102
$ resp = $ this ->_curl ($ url , $ method , $ request_headers , $ params );
@@ -1075,7 +1110,6 @@ private function _request($route, $method, $params=null) {
1075
1110
if ($ this ->debug ) {
1076
1111
print ("Response : " . $ result . "\n" );
1077
1112
}
1078
-
1079
1113
if (empty ($ headers ["content-type " ])) {
1080
1114
throw new DataException ("Unknown content-type in response " );
1081
1115
} else if (strpos ($ headers ["content-type " ], "application/json " ) !== false ) {
@@ -1092,7 +1126,6 @@ private function _request($route, $method, $params=null) {
1092
1126
return ;
1093
1127
}
1094
1128
}
1095
-
1096
1129
// Check if the exception class is defined.
1097
1130
if (class_exists ($ json ->error_type )) {
1098
1131
throw new $ json ->error_type ($ json ->message , $ headers ["status_code " ]);
@@ -1183,6 +1216,9 @@ private function _curl($url, $method, $headers, $params=null) {
1183
1216
$ payload = null ;
1184
1217
if ($ payload = http_build_query ($ params && is_array ($ params ) ? $ params : [])) {
1185
1218
$ payload = preg_replace ("/%5B(\d+?)%5D/ " , "" , $ payload );
1219
+ } else if (json_decode ($ params )){
1220
+ // send json param payload
1221
+ $ payload = $ params ;
1186
1222
}
1187
1223
1188
1224
if ($ method == "POST " || $ method == "PUT " ) {
0 commit comments