Skip to content

Commit 58cd48a

Browse files
authored
Merge pull request #7 from zerodhatech/feat-gtt
feat: add GTT api calls
2 parents 39c1315 + b8c07b6 commit 58cd48a

File tree

1 file changed

+243
-44
lines changed

1 file changed

+243
-44
lines changed

kiteconnect.php

Lines changed: 243 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -107,50 +107,56 @@ class KiteConnect {
107107

108108
// API route map.
109109
private $_routes = [
110-
"api.token" => "/session/token",
111-
"api.token.invalidate" => "/session/token",
112-
"api.token.renew" => "/session/refresh_token",
113-
"user.profile" => "/user/profile",
114-
"user.margins" => "/user/margins",
115-
"user.margins.segment" => "/user/margins/{segment}",
116-
117-
"orders" => "/orders",
118-
"trades" => "/trades",
119-
120-
"order.info" => "/orders/{order_id}",
121-
"order.place" => "/orders/{variety}",
122-
"order.modify" => "/orders/{variety}/{order_id}",
123-
"order.cancel" => "/orders/{variety}/{order_id}",
124-
"order.trades" => "/orders/{order_id}/trades",
125-
126-
"portfolio.positions" => "/portfolio/positions",
127-
"portfolio.holdings" => "/portfolio/holdings",
128-
"portfolio.positions.convert" => "/portfolio/positions",
129-
130-
# MF api endpoints
131-
"mf.orders" => "/mf/orders",
132-
"mf.order.info" => "/mf/orders/{order_id}",
133-
"mf.order.place" => "/mf/orders",
134-
"mf.order.cancel" => "/mf/orders/{order_id}",
135-
136-
"mf.sips" => "/mf/sips",
137-
"mf.sip.info" => "/mf/sips/{sip_id}",
138-
"mf.sip.place" => "/mf/sips",
139-
"mf.sip.modify" => "/mf/sips/{sip_id}",
140-
"mf.sip.cancel" => "/mf/sips/{sip_id}",
141-
142-
"mf.holdings" => "/mf/holdings",
143-
"mf.instruments" => "/mf/instruments",
144-
145-
"market.instruments.all" => "/instruments",
146-
"market.instruments" => "/instruments/{exchange}",
147-
"market.margins" => "/margins/{segment}",
148-
"market.historical" => "/instruments/historical/{instrument_token}/{interval}",
149-
"market.trigger_range" => "/instruments/trigger_range/{transaction_type}",
150-
151-
"market.quote" => "/quote",
152-
"market.quote.ohlc" => "/quote/ohlc",
153-
"market.quote.ltp" => "/quote/ltp",
110+
"api.token" => "/session/token",
111+
"api.token.invalidate" => "/session/token",
112+
"api.token.renew" => "/session/refresh_token",
113+
"user.profile" => "/user/profile",
114+
"user.margins" => "/user/margins",
115+
"user.margins.segment" => "/user/margins/{segment}",
116+
117+
"orders" => "/orders",
118+
"trades" => "/trades",
119+
120+
"order.info" => "/orders/{order_id}",
121+
"order.place" => "/orders/{variety}",
122+
"order.modify" => "/orders/{variety}/{order_id}",
123+
"order.cancel" => "/orders/{variety}/{order_id}",
124+
"order.trades" => "/orders/{order_id}/trades",
125+
126+
"portfolio.positions" => "/portfolio/positions",
127+
"portfolio.holdings" => "/portfolio/holdings",
128+
"portfolio.positions.convert" => "/portfolio/positions",
129+
130+
# MF api endpoints
131+
"mf.orders" => "/mf/orders",
132+
"mf.order.info" => "/mf/orders/{order_id}",
133+
"mf.order.place" => "/mf/orders",
134+
"mf.order.cancel" => "/mf/orders/{order_id}",
135+
136+
"mf.sips" => "/mf/sips",
137+
"mf.sip.info" => "/mf/sips/{sip_id}",
138+
"mf.sip.place" => "/mf/sips",
139+
"mf.sip.modify" => "/mf/sips/{sip_id}",
140+
"mf.sip.cancel" => "/mf/sips/{sip_id}",
141+
142+
"mf.holdings" => "/mf/holdings",
143+
"mf.instruments" => "/mf/instruments",
144+
145+
"market.instruments.all" => "/instruments",
146+
"market.instruments" => "/instruments/{exchange}",
147+
"market.margins" => "/margins/{segment}",
148+
"market.historical" => "/instruments/historical/{instrument_token}/{interval}",
149+
"market.trigger_range" => "/instruments/trigger_range/{transaction_type}",
150+
151+
"market.quote" => "/quote",
152+
"market.quote.ohlc" => "/quote/ohlc",
153+
"market.quote.ltp" => "/quote/ltp",
154+
155+
"gtt.triggers" => "/gtt/triggers",
156+
"gtt.trigger_info" => "/gtt/triggers/{trigger_id}",
157+
"gtt.place" => "/gtt/triggers",
158+
"gtt.modify" => "/gtt/triggers/{trigger_id}",
159+
"gtt.delete" => "/gtt/triggers/{trigger_id}"
154160
];
155161

156162

@@ -162,6 +168,54 @@ class KiteConnect {
162168
public $session_hook = null;
163169
public $micro_cache = true;
164170

171+
// Constants
172+
// Products
173+
const PRODUCT_MIS = "MIS";
174+
const PRODUCT_CNC = "CNC";
175+
const PRODUCT_NRML = "NRML";
176+
const PRODUCT_CO = "CO";
177+
const PRODUCT_BO = "BO";
178+
179+
// Order types
180+
const ORDER_TYPE_MARKET = "MARKET";
181+
const ORDER_TYPE_LIMIT = "LIMIT";
182+
const ORDER_TYPE_SLM = "SL-M";
183+
const ORDER_TYPE_SL = "SL";
184+
185+
// Varities
186+
const VARIETY_REGULAR = "regular";
187+
const VARIETY_BO = "bo";
188+
const VARIETY_CO = "co";
189+
const VARIETY_AMO = "amo";
190+
191+
// Transaction type
192+
const TRANSACTION_TYPE_BUY = "BUY";
193+
const TRANSACTION_TYPE_SELL = "SELL";
194+
195+
// Validity
196+
const VALIDITY_DAY = "DAY";
197+
const VALIDITY_IOC = "IOC";
198+
199+
// Margins segments
200+
const MARGIN_EQUITY = "equity";
201+
const MARGIN_COMMODITY = "commodity";
202+
203+
const STATUS_CANCELLED = "CANCELLED";
204+
const STATUS_REJECTED = "REJECTED";
205+
const STATUS_COMPLETE = "COMPLETE";
206+
207+
// GTT Types
208+
const GTT_TYPE_OCO = "two-leg";
209+
const GTT_TYPE_SINGLE = "single";
210+
211+
// GTT Statuses
212+
const GTT_STATUS_ACTIVE = "active";
213+
const GTT_STATUS_TRIGGERED = "triggered";
214+
const GTT_STATUS_DISABLED = "disabled";
215+
const GTT_STATUS_EXPIRED = "expired";
216+
const GTT_STATUS_CANCELLED = "cancelled";
217+
const GTT_STATUS_REJECTED = "rejected";
218+
const GTT_STATUS_DELETED = "deleted";
165219

166220
/**
167221
* Initialise a new Kite Connect client instance.
@@ -434,6 +488,9 @@ public function getOrders() {
434488

435489
/**
436490
* Get history of the individual order.
491+
* @param string $order_id ID of the order (optional) whose trades
492+
* are to be retrieved. If no `order_id` is
493+
* specified, all trades for the day are returned.
437494
* @return array
438495
*/
439496
public function getOrderHistory($order_id) {
@@ -758,6 +815,148 @@ public function getMFInstruments() {
758815
}
759816

760817

818+
/**
819+
* Get the list of all orders placed for the day.
820+
* @return array
821+
*/
822+
public function getGTTs() {
823+
return $this->_format_response_array($this->_get("gtt.triggers"));
824+
}
825+
826+
/**
827+
* Get history of the individual order.
828+
* @param string $trigger_id "trigger_id" Trigger ID
829+
* @return array
830+
*/
831+
public function getGTT($trigger_id) {
832+
return $this->_format_response($this->_get("gtt.trigger_info", ["trigger_id" => $trigger_id]));
833+
}
834+
835+
/**
836+
* Cancel an open order.
837+
* @param string $trigger_id "trigger_id" Trigger ID
838+
* @return void
839+
*/
840+
public function deleteGTT($trigger_id) {
841+
return $this->_delete("gtt.delete", ["trigger_id" => $trigger_id]);
842+
}
843+
844+
private function getGTTPayload($params) {
845+
if ($params["trigger_type"] == self::GTT_TYPE_OCO && count($params["trigger_values"]) != 2) {
846+
throw new DataException("Invalid `trigger_values` for `OCO` order type");
847+
}
848+
if ($params["trigger_type"] == self::GTT_TYPE_SINGLE && count($params["trigger_values"]) != 1) {
849+
throw new DataException("Invalid `trigger_values` for `single` order type");
850+
}
851+
$condition = [
852+
"exchange" => $params["exchange"],
853+
"tradingsymbol" => $params["tradingsymbol"],
854+
"trigger_values" => $params["trigger_values"],
855+
"last_price" => (float)$params["last_price"]
856+
];
857+
$orders = array();
858+
foreach ($params["orders"] as &$o) {
859+
array_push($orders, [
860+
"transaction_type" => $o["transaction_type"],
861+
"order_type" => $o["order_type"],
862+
"product" => $o["product"],
863+
"quantity" => (int)$o["quantity"],
864+
"price" => (float)($o["price"]),
865+
"exchange" => $params["exchange"],
866+
"tradingsymbol" => $params["tradingsymbol"]
867+
]);
868+
}
869+
return [
870+
"condition" => $condition,
871+
"orders" => $orders
872+
];
873+
}
874+
875+
/**
876+
* Place a GTT. Check [GTT documentation](https://kite.trade/docs/connect/v3/gtt/#placing-orders) for details.
877+
* <code>
878+
* $params = [
879+
* // GTT type, its either `$kite::GTT_TYPE_OCO` or `$kite::GTT_TYPE_SINGLE`.
880+
* "trigger_type" => $kite::GTT_TYPE_OCO,
881+
* // Tradingsymbol of the instrument (ex. RELIANCE, INFY).
882+
* "tradingsymbol" => "SBIN",
883+
* // Exchange in which instrument is listed (NSE, BSE, NFO, BFO, CDS, MCX).
884+
* "exchange" => "NSE",
885+
* // List of trigger values, number of items depends on trigger type.
886+
* "trigger_values" => array(300, 400),
887+
* // Price at which trigger is created. This is usually the last price of the instrument.
888+
* "last_price" => 318,
889+
* // List of orders. Check [order params](https://kite.trade/docs/connect/v3/orders/#regular-order-parameters) for all available params.
890+
* "orders" => array([
891+
* "transaction_type" => $kite::TRANSACTION_TYPE_SELL,
892+
* "quantity" => 1,
893+
* "product" => $kite::PRODUCT_CNC,
894+
* "order_type" => $kite::ORDER_TYPE_LIMIT,
895+
* "price" => 300
896+
* ], [
897+
* "transaction_type" => $kite::TRANSACTION_TYPE_SELL,
898+
* "quantity" => 1,
899+
* "product" => $kite::PRODUCT_CNC,
900+
* "order_type" => $kite::ORDER_TYPE_LIMIT,
901+
* "price" => 400
902+
* ])
903+
* ]
904+
* </code>
905+
*
906+
* @param array $params GTT Params. Check above for required fields.
907+
*/
908+
public function placeGTT($params) {
909+
$payload = $this->getGTTPayload($params);
910+
return $this->_post("gtt.place", [
911+
"condition" => json_encode($payload["condition"]),
912+
"orders" => json_encode($payload["orders"]),
913+
"type" => $params["trigger_type"]
914+
]);
915+
}
916+
917+
/**
918+
* Modify GTT. Check [GTT documentation](https://kite.trade/docs/connect/v3/gtt/#modify-order) for details.
919+
* <code>
920+
* $params = [
921+
* // GTT type, its either `$kite::GTT_TYPE_OCO` or `$kite::GTT_TYPE_SINGLE`.
922+
* "trigger_type" => $kite::GTT_TYPE_OCO,
923+
* // Tradingsymbol of the instrument (ex. RELIANCE, INFY).
924+
* "tradingsymbol" => "SBIN",
925+
* // Exchange in which instrument is listed (NSE, BSE, NFO, BFO, CDS, MCX).
926+
* "exchange" => "NSE",
927+
* // List of trigger values, number of items depends on trigger type.
928+
* "trigger_values" => array(300, 400),
929+
* // Price at which trigger is created. This is usually the last price of the instrument.
930+
* "last_price" => 318,
931+
* // List of orders. Check [order params](https://kite.trade/docs/connect/v3/orders/#regular-order-parameters) for all available params.
932+
* "orders" => array([
933+
* "transaction_type" => $kite::TRANSACTION_TYPE_SELL,
934+
* "quantity" => 1,
935+
* "product" => $kite::PRODUCT_CNC,
936+
* "order_type" => $kite::ORDER_TYPE_LIMIT,
937+
* "price" => 300
938+
* ], [
939+
* "transaction_type" => $kite::TRANSACTION_TYPE_SELL,
940+
* "quantity" => 1,
941+
* "product" => $kite::PRODUCT_CNC,
942+
* "order_type" => $kite::ORDER_TYPE_LIMIT,
943+
* "price" => 400
944+
* ])
945+
* ]
946+
* </code>
947+
* @param int $trigger_id GTT Trigger ID
948+
* @param array $params GTT Params. Check above for required fields.
949+
*/
950+
public function modifyGTT($trigger_id, $params) {
951+
$payload = $this->getGTTPayload($params);
952+
return $this->_put("gtt.modify", [
953+
"condition" => json_encode($payload["condition"]),
954+
"orders" => json_encode($payload["orders"]),
955+
"type" => $params["trigger_type"],
956+
"trigger_id" => $trigger_id
957+
]);
958+
}
959+
761960
/**
762961
* Format response array, For example datetime string to DateTime object
763962
*/

0 commit comments

Comments
 (0)