From b8c07b68273be309d02e8cc61e9e82dc49e4a6a2 Mon Sep 17 00:00:00 2001 From: Vivek R Date: Fri, 8 Nov 2019 14:54:06 +0530 Subject: [PATCH] feat: add GTT api calls --- kiteconnect.php | 287 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 243 insertions(+), 44 deletions(-) diff --git a/kiteconnect.php b/kiteconnect.php index 7754e91..7303ba6 100644 --- a/kiteconnect.php +++ b/kiteconnect.php @@ -107,50 +107,56 @@ class KiteConnect { // API route map. private $_routes = [ - "api.token" => "/session/token", - "api.token.invalidate" => "/session/token", - "api.token.renew" => "/session/refresh_token", - "user.profile" => "/user/profile", - "user.margins" => "/user/margins", - "user.margins.segment" => "/user/margins/{segment}", - - "orders" => "/orders", - "trades" => "/trades", - - "order.info" => "/orders/{order_id}", - "order.place" => "/orders/{variety}", - "order.modify" => "/orders/{variety}/{order_id}", - "order.cancel" => "/orders/{variety}/{order_id}", - "order.trades" => "/orders/{order_id}/trades", - - "portfolio.positions" => "/portfolio/positions", - "portfolio.holdings" => "/portfolio/holdings", - "portfolio.positions.convert" => "/portfolio/positions", - - # MF api endpoints - "mf.orders" => "/mf/orders", - "mf.order.info" => "/mf/orders/{order_id}", - "mf.order.place" => "/mf/orders", - "mf.order.cancel" => "/mf/orders/{order_id}", - - "mf.sips" => "/mf/sips", - "mf.sip.info" => "/mf/sips/{sip_id}", - "mf.sip.place" => "/mf/sips", - "mf.sip.modify" => "/mf/sips/{sip_id}", - "mf.sip.cancel" => "/mf/sips/{sip_id}", - - "mf.holdings" => "/mf/holdings", - "mf.instruments" => "/mf/instruments", - - "market.instruments.all" => "/instruments", - "market.instruments" => "/instruments/{exchange}", - "market.margins" => "/margins/{segment}", - "market.historical" => "/instruments/historical/{instrument_token}/{interval}", - "market.trigger_range" => "/instruments/trigger_range/{transaction_type}", - - "market.quote" => "/quote", - "market.quote.ohlc" => "/quote/ohlc", - "market.quote.ltp" => "/quote/ltp", + "api.token" => "/session/token", + "api.token.invalidate" => "/session/token", + "api.token.renew" => "/session/refresh_token", + "user.profile" => "/user/profile", + "user.margins" => "/user/margins", + "user.margins.segment" => "/user/margins/{segment}", + + "orders" => "/orders", + "trades" => "/trades", + + "order.info" => "/orders/{order_id}", + "order.place" => "/orders/{variety}", + "order.modify" => "/orders/{variety}/{order_id}", + "order.cancel" => "/orders/{variety}/{order_id}", + "order.trades" => "/orders/{order_id}/trades", + + "portfolio.positions" => "/portfolio/positions", + "portfolio.holdings" => "/portfolio/holdings", + "portfolio.positions.convert" => "/portfolio/positions", + + # MF api endpoints + "mf.orders" => "/mf/orders", + "mf.order.info" => "/mf/orders/{order_id}", + "mf.order.place" => "/mf/orders", + "mf.order.cancel" => "/mf/orders/{order_id}", + + "mf.sips" => "/mf/sips", + "mf.sip.info" => "/mf/sips/{sip_id}", + "mf.sip.place" => "/mf/sips", + "mf.sip.modify" => "/mf/sips/{sip_id}", + "mf.sip.cancel" => "/mf/sips/{sip_id}", + + "mf.holdings" => "/mf/holdings", + "mf.instruments" => "/mf/instruments", + + "market.instruments.all" => "/instruments", + "market.instruments" => "/instruments/{exchange}", + "market.margins" => "/margins/{segment}", + "market.historical" => "/instruments/historical/{instrument_token}/{interval}", + "market.trigger_range" => "/instruments/trigger_range/{transaction_type}", + + "market.quote" => "/quote", + "market.quote.ohlc" => "/quote/ohlc", + "market.quote.ltp" => "/quote/ltp", + + "gtt.triggers" => "/gtt/triggers", + "gtt.trigger_info" => "/gtt/triggers/{trigger_id}", + "gtt.place" => "/gtt/triggers", + "gtt.modify" => "/gtt/triggers/{trigger_id}", + "gtt.delete" => "/gtt/triggers/{trigger_id}" ]; @@ -162,6 +168,54 @@ class KiteConnect { public $session_hook = null; public $micro_cache = true; + // Constants + // Products + const PRODUCT_MIS = "MIS"; + const PRODUCT_CNC = "CNC"; + const PRODUCT_NRML = "NRML"; + const PRODUCT_CO = "CO"; + const PRODUCT_BO = "BO"; + + // Order types + const ORDER_TYPE_MARKET = "MARKET"; + const ORDER_TYPE_LIMIT = "LIMIT"; + const ORDER_TYPE_SLM = "SL-M"; + const ORDER_TYPE_SL = "SL"; + + // Varities + const VARIETY_REGULAR = "regular"; + const VARIETY_BO = "bo"; + const VARIETY_CO = "co"; + const VARIETY_AMO = "amo"; + + // Transaction type + const TRANSACTION_TYPE_BUY = "BUY"; + const TRANSACTION_TYPE_SELL = "SELL"; + + // Validity + const VALIDITY_DAY = "DAY"; + const VALIDITY_IOC = "IOC"; + + // Margins segments + const MARGIN_EQUITY = "equity"; + const MARGIN_COMMODITY = "commodity"; + + const STATUS_CANCELLED = "CANCELLED"; + const STATUS_REJECTED = "REJECTED"; + const STATUS_COMPLETE = "COMPLETE"; + + // GTT Types + const GTT_TYPE_OCO = "two-leg"; + const GTT_TYPE_SINGLE = "single"; + + // GTT Statuses + const GTT_STATUS_ACTIVE = "active"; + const GTT_STATUS_TRIGGERED = "triggered"; + const GTT_STATUS_DISABLED = "disabled"; + const GTT_STATUS_EXPIRED = "expired"; + const GTT_STATUS_CANCELLED = "cancelled"; + const GTT_STATUS_REJECTED = "rejected"; + const GTT_STATUS_DELETED = "deleted"; /** * Initialise a new Kite Connect client instance. @@ -434,6 +488,9 @@ public function getOrders() { /** * Get history of the individual order. + * @param string $order_id ID of the order (optional) whose trades + * are to be retrieved. If no `order_id` is + * specified, all trades for the day are returned. * @return array */ public function getOrderHistory($order_id) { @@ -758,6 +815,148 @@ public function getMFInstruments() { } + /** + * Get the list of all orders placed for the day. + * @return array + */ + public function getGTTs() { + return $this->_format_response_array($this->_get("gtt.triggers")); + } + + /** + * Get history of the individual order. + * @param string $trigger_id "trigger_id" Trigger ID + * @return array + */ + public function getGTT($trigger_id) { + return $this->_format_response($this->_get("gtt.trigger_info", ["trigger_id" => $trigger_id])); + } + + /** + * Cancel an open order. + * @param string $trigger_id "trigger_id" Trigger ID + * @return void + */ + public function deleteGTT($trigger_id) { + return $this->_delete("gtt.delete", ["trigger_id" => $trigger_id]); + } + + private function getGTTPayload($params) { + if ($params["trigger_type"] == self::GTT_TYPE_OCO && count($params["trigger_values"]) != 2) { + throw new DataException("Invalid `trigger_values` for `OCO` order type"); + } + if ($params["trigger_type"] == self::GTT_TYPE_SINGLE && count($params["trigger_values"]) != 1) { + throw new DataException("Invalid `trigger_values` for `single` order type"); + } + $condition = [ + "exchange" => $params["exchange"], + "tradingsymbol" => $params["tradingsymbol"], + "trigger_values" => $params["trigger_values"], + "last_price" => (float)$params["last_price"] + ]; + $orders = array(); + foreach ($params["orders"] as &$o) { + array_push($orders, [ + "transaction_type" => $o["transaction_type"], + "order_type" => $o["order_type"], + "product" => $o["product"], + "quantity" => (int)$o["quantity"], + "price" => (float)($o["price"]), + "exchange" => $params["exchange"], + "tradingsymbol" => $params["tradingsymbol"] + ]); + } + return [ + "condition" => $condition, + "orders" => $orders + ]; + } + + /** + * Place a GTT. Check [GTT documentation](https://kite.trade/docs/connect/v3/gtt/#placing-orders) for details. + * + * $params = [ + * // GTT type, its either `$kite::GTT_TYPE_OCO` or `$kite::GTT_TYPE_SINGLE`. + * "trigger_type" => $kite::GTT_TYPE_OCO, + * // Tradingsymbol of the instrument (ex. RELIANCE, INFY). + * "tradingsymbol" => "SBIN", + * // Exchange in which instrument is listed (NSE, BSE, NFO, BFO, CDS, MCX). + * "exchange" => "NSE", + * // List of trigger values, number of items depends on trigger type. + * "trigger_values" => array(300, 400), + * // Price at which trigger is created. This is usually the last price of the instrument. + * "last_price" => 318, + * // List of orders. Check [order params](https://kite.trade/docs/connect/v3/orders/#regular-order-parameters) for all available params. + * "orders" => array([ + * "transaction_type" => $kite::TRANSACTION_TYPE_SELL, + * "quantity" => 1, + * "product" => $kite::PRODUCT_CNC, + * "order_type" => $kite::ORDER_TYPE_LIMIT, + * "price" => 300 + * ], [ + * "transaction_type" => $kite::TRANSACTION_TYPE_SELL, + * "quantity" => 1, + * "product" => $kite::PRODUCT_CNC, + * "order_type" => $kite::ORDER_TYPE_LIMIT, + * "price" => 400 + * ]) + * ] + * + * + * @param array $params GTT Params. Check above for required fields. + */ + public function placeGTT($params) { + $payload = $this->getGTTPayload($params); + return $this->_post("gtt.place", [ + "condition" => json_encode($payload["condition"]), + "orders" => json_encode($payload["orders"]), + "type" => $params["trigger_type"] + ]); + } + + /** + * Modify GTT. Check [GTT documentation](https://kite.trade/docs/connect/v3/gtt/#modify-order) for details. + * + * $params = [ + * // GTT type, its either `$kite::GTT_TYPE_OCO` or `$kite::GTT_TYPE_SINGLE`. + * "trigger_type" => $kite::GTT_TYPE_OCO, + * // Tradingsymbol of the instrument (ex. RELIANCE, INFY). + * "tradingsymbol" => "SBIN", + * // Exchange in which instrument is listed (NSE, BSE, NFO, BFO, CDS, MCX). + * "exchange" => "NSE", + * // List of trigger values, number of items depends on trigger type. + * "trigger_values" => array(300, 400), + * // Price at which trigger is created. This is usually the last price of the instrument. + * "last_price" => 318, + * // List of orders. Check [order params](https://kite.trade/docs/connect/v3/orders/#regular-order-parameters) for all available params. + * "orders" => array([ + * "transaction_type" => $kite::TRANSACTION_TYPE_SELL, + * "quantity" => 1, + * "product" => $kite::PRODUCT_CNC, + * "order_type" => $kite::ORDER_TYPE_LIMIT, + * "price" => 300 + * ], [ + * "transaction_type" => $kite::TRANSACTION_TYPE_SELL, + * "quantity" => 1, + * "product" => $kite::PRODUCT_CNC, + * "order_type" => $kite::ORDER_TYPE_LIMIT, + * "price" => 400 + * ]) + * ] + * + * @param int $trigger_id GTT Trigger ID + * @param array $params GTT Params. Check above for required fields. + */ + public function modifyGTT($trigger_id, $params) { + $payload = $this->getGTTPayload($params); + return $this->_put("gtt.modify", [ + "condition" => json_encode($payload["condition"]), + "orders" => json_encode($payload["orders"]), + "type" => $params["trigger_type"], + "trigger_id" => $trigger_id + ]); + } + /** * Format response array, For example datetime string to DateTime object */