Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions examples/gtt_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@
"product": "CNC",
"price": 470,
}]
single_gtt = kite.place_gtt(trigger_type=kite.GTT_TYPE_SINGLE, tradingsymbol="SBIN", exchange="NSE", trigger_values=[470], last_price=473, orders=order_single)
single_gtt = kite.place_gtt(
trigger_type=kite.GTT_TYPE_SINGLE,
tradingsymbol="SBIN",
exchange="NSE",
trigger_values=[470],
last_price=473,
orders=order_single,
tag="my_strategy_1" # Optional tag to identify this GTT order
)
logging.info("single leg gtt order trigger_id : {}".format(single_gtt['trigger_id']))
except Exception as e:
logging.info("Error placing single leg gtt order: {}".format(e))
Expand All @@ -50,7 +58,15 @@
"product": "CNC",
"price": 480
}]
gtt_oco = kite.place_gtt(trigger_type=kite.GTT_TYPE_OCO, tradingsymbol="SBIN", exchange="NSE", trigger_values=[470,480], last_price=473, orders=order_oco)
gtt_oco = kite.place_gtt(
trigger_type=kite.GTT_TYPE_OCO,
tradingsymbol="SBIN",
exchange="NSE",
trigger_values=[470,480],
last_price=473,
orders=order_oco,
tag="my_strategy_2" # Optional tag to identify this GTT order
)
logging.info("GTT OCO trigger_id : {}".format(gtt_oco['trigger_id']))
except Exception as e:
logging.info("Error placing gtt oco order: {}".format(e))
20 changes: 13 additions & 7 deletions kiteconnect/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def get_gtt(self, trigger_id):
"""Fetch details of a GTT"""
return self._get("gtt.info", url_args={"trigger_id": trigger_id})

def _get_gtt_payload(self, trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders):
def _get_gtt_payload(self, trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders, tag=None):
"""Get GTT payload"""
if type(trigger_values) != list:
raise ex.InputException("invalid type for `trigger_values`")
Expand All @@ -700,20 +700,24 @@ def _get_gtt_payload(self, trigger_type, tradingsymbol, exchange, trigger_values
for req in ["transaction_type", "quantity", "order_type", "product", "price"]:
if req not in o:
raise ex.InputException("`{req}` missing inside orders".format(req=req))
gtt_orders.append({
order_data = {
"exchange": exchange,
"tradingsymbol": tradingsymbol,
"transaction_type": o["transaction_type"],
"quantity": int(o["quantity"]),
"order_type": o["order_type"],
"product": o["product"],
"price": float(o["price"]),
})
}
# Add tag to order if provided
if tag:
order_data["tag"] = tag
gtt_orders.append(order_data)

return condition, gtt_orders

def place_gtt(
self, trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders
self, trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders, tag=None
):
"""
Place GTT order
Expand All @@ -727,18 +731,19 @@ def place_gtt(
- `transaction_type` BUY or SELL
- `quantity` Quantity to transact
- `price` The min or max price to execute the order at (for LIMIT orders)
- `tag` An optional tag to identify the order.
"""
# Validations.
assert trigger_type in [self.GTT_TYPE_OCO, self.GTT_TYPE_SINGLE]
condition, gtt_orders = self._get_gtt_payload(trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders)
condition, gtt_orders = self._get_gtt_payload(trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders, tag)

return self._post("gtt.place", params={
"condition": json.dumps(condition),
"orders": json.dumps(gtt_orders),
"type": trigger_type})

def modify_gtt(
self, trigger_id, trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders
self, trigger_id, trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders, tag=None
):
"""
Modify GTT order
Expand All @@ -752,8 +757,9 @@ def modify_gtt(
- `transaction_type` BUY or SELL
- `quantity` Quantity to transact
- `price` The min or max price to execute the order at (for LIMIT orders)
- `tag` An optional tag to identify the order.
"""
condition, gtt_orders = self._get_gtt_payload(trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders)
condition, gtt_orders = self._get_gtt_payload(trigger_type, tradingsymbol, exchange, trigger_values, last_price, orders, tag)

return self._put("gtt.modify",
url_args={"trigger_id": trigger_id},
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,33 @@ def test_place_gtt(kiteconnect):
assert gtts["trigger_id"] == 123


@responses.activate
def test_place_gtt_with_tag(kiteconnect):
"""Test place gtt order with tag parameter."""
responses.add(
responses.POST,
"{0}{1}".format(kiteconnect.root, kiteconnect._routes["gtt.place"]),
body=utils.get_response("gtt.place"),
content_type="application/json"
)
gtts = kiteconnect.place_gtt(
trigger_type=kiteconnect.GTT_TYPE_SINGLE,
tradingsymbol="INFY",
exchange="NSE",
trigger_values=[1],
last_price=800,
orders=[{
"transaction_type": kiteconnect.TRANSACTION_TYPE_BUY,
"quantity": 1,
"order_type": kiteconnect.ORDER_TYPE_LIMIT,
"product": kiteconnect.PRODUCT_CNC,
"price": 1,
}],
tag="test_strategy"
)
assert gtts["trigger_id"] == 123


@responses.activate
def test_modify_gtt(kiteconnect):
"""Test modify gtt order."""
Expand Down Expand Up @@ -342,6 +369,34 @@ def test_modify_gtt(kiteconnect):
assert gtts["trigger_id"] == 123


@responses.activate
def test_modify_gtt_with_tag(kiteconnect):
"""Test modify gtt order with tag parameter."""
responses.add(
responses.PUT,
"{0}{1}".format(kiteconnect.root, kiteconnect._routes["gtt.modify"].format(trigger_id=123)),
body=utils.get_response("gtt.modify"),
content_type="application/json"
)
gtts = kiteconnect.modify_gtt(
trigger_id=123,
trigger_type=kiteconnect.GTT_TYPE_SINGLE,
tradingsymbol="INFY",
exchange="NSE",
trigger_values=[1],
last_price=800,
orders=[{
"transaction_type": kiteconnect.TRANSACTION_TYPE_BUY,
"quantity": 1,
"order_type": kiteconnect.ORDER_TYPE_LIMIT,
"product": kiteconnect.PRODUCT_CNC,
"price": 1,
}],
tag="modified_strategy"
)
assert gtts["trigger_id"] == 123


@responses.activate
def test_delete_gtt(kiteconnect):
"""Test delete gtt order."""
Expand Down