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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ docs/_build/
# PyBuilder
target/

# Ruff cache
.ruff_cache/

# Jupyter Notebook
.ipynb_checkpoints

Expand Down
6 changes: 0 additions & 6 deletions dev_requirements.txt

This file was deleted.

23 changes: 11 additions & 12 deletions examples/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
HOST = "127.0.0.1"


def serializer(obj): return isinstance(obj, (date, datetime, Decimal)) and str(obj) # noqa
def serializer(obj):
return isinstance(obj, (date, datetime, Decimal)) and str(obj) # noqa


# Kite Connect App settings. Go to https://developers.kite.trade/apps/
Expand All @@ -42,10 +43,14 @@ def serializer(obj): return isinstance(obj, (date, datetime, Decimal)) and str(o
redirect_url = "http://{host}:{port}/login".format(host=HOST, port=PORT)

# Login url
login_url = "https://kite.zerodha.com/connect/login?api_key={api_key}".format(api_key=kite_api_key)
login_url = "https://kite.zerodha.com/connect/login?api_key={api_key}".format(
api_key=kite_api_key
)

# Kite connect console url
console_url = "https://developers.kite.trade/apps/{api_key}".format(api_key=kite_api_key)
console_url = "https://developers.kite.trade/apps/{api_key}".format(
api_key=kite_api_key
)

# App
app = Flask(__name__)
Expand All @@ -68,8 +73,7 @@ def serializer(obj): return isinstance(obj, (date, datetime, Decimal)) and str(o


def get_kite_client():
"""Returns a kite client object
"""
"""Returns a kite client object"""
kite = KiteConnect(api_key=kite_api_key)
if "access_token" in session:
kite.set_access_token(session["access_token"])
Expand All @@ -82,7 +86,7 @@ def index():
api_key=kite_api_key,
redirect_url=redirect_url,
console_url=console_url,
login_url=login_url
login_url=login_url,
)


Expand All @@ -103,12 +107,7 @@ def login():

return login_template.format(
access_token=data["access_token"],
user_data=json.dumps(
data,
indent=4,
sort_keys=True,
default=serializer
)
user_data=json.dumps(data, indent=4, sort_keys=True, default=serializer),
)


Expand Down
85 changes: 52 additions & 33 deletions examples/gtt_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,65 @@
# Once you have the request_token, obtain the access_token
# as follows.

data = kite.generate_session("request_token_here", secret="your_secret")
data = kite.generate_session("request_token_here", api_secret="your_secret")
kite.set_access_token(data["access_token"])

# Place single-leg gtt order - https://kite.trade/docs/connect/v3/gtt/#single
try:
order_single = [{
"exchange":"NSE",
"tradingsymbol": "SBIN",
"transaction_type": kite.TRANSACTION_TYPE_BUY,
"quantity": 1,
"order_type": "LIMIT",
"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)
logging.info("single leg gtt order trigger_id : {}".format(single_gtt['trigger_id']))
order_single = [
{
"exchange": "NSE",
"tradingsymbol": "SBIN",
"transaction_type": kite.TRANSACTION_TYPE_BUY,
"quantity": 1,
"order_type": "LIMIT",
"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,
)
logging.info(f"single leg gtt order trigger_id : {single_gtt['trigger_id']}")
except Exception as e:
logging.info("Error placing single leg gtt order: {}".format(e))
logging.info(f"Error placing single leg gtt order: {e}")


# Place two-leg(OCO) gtt order - https://kite.trade/docs/connect/v3/gtt/#two-leg
try:
order_oco = [{
"exchange":"NSE",
"tradingsymbol": "SBIN",
"transaction_type": kite.TRANSACTION_TYPE_SELL,
"quantity": 1,
"order_type": "LIMIT",
"product": "CNC",
"price": 470
},{
"exchange":"NSE",
"tradingsymbol": "SBIN",
"transaction_type": kite.TRANSACTION_TYPE_SELL,
"quantity": 1,
"order_type": "LIMIT",
"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)
logging.info("GTT OCO trigger_id : {}".format(gtt_oco['trigger_id']))
order_oco = [
{
"exchange": "NSE",
"tradingsymbol": "SBIN",
"transaction_type": kite.TRANSACTION_TYPE_SELL,
"quantity": 1,
"order_type": "LIMIT",
"product": "CNC",
"price": 470,
},
{
"exchange": "NSE",
"tradingsymbol": "SBIN",
"transaction_type": kite.TRANSACTION_TYPE_SELL,
"quantity": 1,
"order_type": "LIMIT",
"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,
)
logging.info(f"GTT OCO trigger_id : {gtt_oco['trigger_id']}")
except Exception as e:
logging.info("Error placing gtt oco order: {}".format(e))
logging.info("Error placing gtt oco order: {}".format(e))
132 changes: 71 additions & 61 deletions examples/order_margins.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,78 +17,87 @@
# Fetch margin detail for order/orders
try:
# Fetch margin detail for single order
order_param_single = [{
"exchange": "NSE",
"tradingsymbol": "INFY",
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": 2
}]
order_param_single = [
{
"exchange": "NSE",
"tradingsymbol": "INFY",
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": 2,
}
]

margin_detail = kite.order_margins(order_param_single)
logging.info("Required margin for single order: {}".format(margin_detail))

# Fetch margin detail for list of orders
order_param_multi = [{
"exchange": "NSE",
"tradingsymbol": "SBIN",
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": 10
logging.info("Required margin for single order: {}".format(margin_detail))

# Fetch margin detail for list of orders
order_param_multi = [
{
"exchange": "NSE",
"tradingsymbol": "SBIN",
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": 10,
},
{
"exchange": "NFO",
"tradingsymbol": "TCS20DECFUT",
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "LIMIT",
"quantity": 5,
"price":2725.30
"exchange": "NFO",
"tradingsymbol": "TCS20DECFUT",
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "LIMIT",
"quantity": 5,
"price": 2725.30,
},
{
"exchange": "NFO",
"tradingsymbol": "NIFTY20DECFUT",
"transaction_type": "BUY",
"variety": "bo",
"product": "MIS",
"order_type": "MARKET",
"quantity": 5
}]
"exchange": "NFO",
"tradingsymbol": "NIFTY20DECFUT",
"transaction_type": "BUY",
"variety": "bo",
"product": "MIS",
"order_type": "MARKET",
"quantity": 5,
},
]

margin_detail = kite.order_margins(order_param_multi)
logging.info("Required margin for order_list: {}".format(margin_detail))

# Basket orders
order_param_basket = [
{
"exchange": "NFO",
"tradingsymbol": "NIFTY21JUN15400PE",
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": 75
},
{
"exchange": "NFO",
"tradingsymbol": "NIFTY21JUN14450PE",
"transaction_type": "SELL",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": 150
}]
{
"exchange": "NFO",
"tradingsymbol": "NIFTY21JUN15400PE",
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": 75,
},
{
"exchange": "NFO",
"tradingsymbol": "NIFTY21JUN14450PE",
"transaction_type": "SELL",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": 150,
},
]

margin_amount = kite.basket_order_margins(order_param_basket)
logging.info("Required margin for basket order: {}".format(margin_amount))
# Compact margin response
margin_amount_comt = kite.basket_order_margins(order_param_basket, mode='compact')
logging.info("Required margin for basket order in compact form: {}".format(margin_amount_comt))
margin_amount_comt = kite.basket_order_margins(order_param_basket, mode="compact")
logging.info(
"Required margin for basket order in compact form: {}".format(
margin_amount_comt
)
)

except Exception as e:
logging.info("Error fetching order margin: {}".format(e))
Expand All @@ -106,7 +115,7 @@
"product": "CNC",
"order_type": "MARKET",
"quantity": 1,
"average_price": 560
"average_price": 560,
},
{
"order_id": "2222222222",
Expand All @@ -117,7 +126,7 @@
"product": "NRML",
"order_type": "LIMIT",
"quantity": 1,
"average_price": 5862
"average_price": 5862,
},
{
"order_id": "3333333333",
Expand All @@ -128,10 +137,11 @@
"product": "NRML",
"order_type": "LIMIT",
"quantity": 100,
"average_price": 1.5
}]
"average_price": 1.5,
},
]

order_book_charges = kite.get_virtual_contract_note(order_book_params)
logging.info("Virtual contract note charges: {}".format(order_book_charges))
except Exception as e:
logging.info("Error fetching virtual contract note charges: {}".format(e))
logging.info("Error fetching virtual contract note charges: {}".format(e))
4 changes: 2 additions & 2 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
transaction_type=kite.TRANSACTION_TYPE_BUY,
quantity=1,
product=kite.PRODUCT_CNC,
order_type=kite.ORDER_TYPE_MARKET
order_type=kite.ORDER_TYPE_MARKET,
)

logging.info("Order placed. ID is: {}".format(order_id))
Expand All @@ -41,7 +41,7 @@
tradingsymbol="INF090I01239",
transaction_type=kite.TRANSACTION_TYPE_BUY,
amount=5000,
tag="mytag"
tag="mytag",
)

# Cancel a mutual fund order
Expand Down
4 changes: 3 additions & 1 deletion examples/threaded_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def on_connect(ws, response):

# Callback when current connection is closed.
def on_close(ws, code, reason):
logging.info("Connection closed: {code} - {reason}".format(code=code, reason=reason))
logging.info(
"Connection closed: {code} - {reason}".format(code=code, reason=reason)
)


# Callback when connection closed with error.
Expand Down
Loading