forked from TradeViaPython/Kite_Zerodha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kite_trade.py
155 lines (133 loc) · 5.93 KB
/
kite_trade.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
try:
import requests
except ImportError:
os.system('python -m pip install requests')
try:
import dateutil
except ImportError:
os.system('python -m pip install python-dateutil')
import requests
import dateutil.parser
def get_enctoken(userid, password, twofa):
session = requests.Session()
response = session.post('https://kite.zerodha.com/api/login', data={
"user_id": userid,
"password": password
})
response = session.post('https://kite.zerodha.com/api/twofa', data={
"request_id": response.json()['data']['request_id'],
"twofa_value": twofa,
"user_id": response.json()['data']['user_id']
})
enctoken = response.cookies.get('enctoken')
if enctoken:
return enctoken
else:
raise Exception("Enter valid details !!!!")
class KiteApp:
# Products
PRODUCT_MIS = "MIS"
PRODUCT_CNC = "CNC"
PRODUCT_NRML = "NRML"
PRODUCT_CO = "CO"
# Order types
ORDER_TYPE_MARKET = "MARKET"
ORDER_TYPE_LIMIT = "LIMIT"
ORDER_TYPE_SLM = "SL-M"
ORDER_TYPE_SL = "SL"
# Varities
VARIETY_REGULAR = "regular"
VARIETY_CO = "co"
VARIETY_AMO = "amo"
# Transaction type
TRANSACTION_TYPE_BUY = "BUY"
TRANSACTION_TYPE_SELL = "SELL"
# Validity
VALIDITY_DAY = "DAY"
VALIDITY_IOC = "IOC"
# Exchanges
EXCHANGE_NSE = "NSE"
EXCHANGE_BSE = "BSE"
EXCHANGE_NFO = "NFO"
EXCHANGE_CDS = "CDS"
EXCHANGE_BFO = "BFO"
EXCHANGE_MCX = "MCX"
def __init__(self, enctoken):
self.headers = {"Authorization": f"enctoken {enctoken}"}
self.session = requests.session()
self.root_url = "https://api.kite.trade"
# self.root_url = "https://kite.zerodha.com/oms"
self.session.get(self.root_url, headers=self.headers)
def instruments(self, exchange=None):
data = self.session.get(f"{self.root_url}/instruments",headers=self.headers).text.split("\n")
Exchange = []
for i in data[1:-1]:
row = i.split(",")
if exchange is None or exchange == row[11]:
Exchange.append({'instrument_token': int(row[0]), 'exchange_token': row[1], 'tradingsymbol': row[2],
'name': row[3][1:-1], 'last_price': float(row[4]),
'expiry': dateutil.parser.parse(row[5]).date() if row[5] != "" else None,
'strike': float(row[6]), 'tick_size': float(row[7]), 'lot_size': int(row[8]),
'instrument_type': row[9], 'segment': row[10],
'exchange': row[11]})
return Exchange
def quote(self, instruments):
data = self.session.get(f"{self.root_url}/quote", params={"i": instruments}, headers=self.headers).json()["data"]
return data
def ltp(self, instruments):
data = self.session.get(f"{self.root_url}/quote/ltp", params={"i": instruments}, headers=self.headers).json()["data"]
return data
def historical_data(self, instrument_token, from_date, to_date, interval, continuous=False, oi=False):
params = {"from": from_date,
"to": to_date,
"interval": interval,
"continuous": 1 if continuous else 0,
"oi": 1 if oi else 0}
lst = self.session.get(
f"{self.root_url}/instruments/historical/{instrument_token}/{interval}", params=params,
headers=self.headers).json()["data"]["candles"]
records = []
for i in lst:
record = {"date": dateutil.parser.parse(i[0]), "open": i[1], "high": i[2], "low": i[3],
"close": i[4], "volume": i[5],}
if len(i) == 7:
record["oi"] = i[6]
records.append(record)
return records
def margins(self):
margins = self.session.get(f"{self.root_url}/user/margins", headers=self.headers).json()["data"]
return margins
def orders(self):
orders = self.session.get(f"{self.root_url}/orders", headers=self.headers).json()["data"]
return orders
def positions(self):
positions = self.session.get(f"{self.root_url}/portfolio/positions", headers=self.headers).json()["data"]
return positions
def place_order(self, variety, exchange, tradingsymbol, transaction_type, quantity, product, order_type, price=None,
validity=None, disclosed_quantity=None, trigger_price=None, squareoff=None, stoploss=None,
trailing_stoploss=None, tag=None):
params = locals()
del params["self"]
for k in list(params.keys()):
if params[k] is None:
del params[k]
order_id = self.session.post(f"{self.root_url}/orders/{variety}",
data=params, headers=self.headers).json()["data"]["order_id"]
return order_id
def modify_order(self, variety, order_id, parent_order_id=None, quantity=None, price=None, order_type=None,
trigger_price=None, validity=None, disclosed_quantity=None):
params = locals()
del params["self"]
for k in list(params.keys()):
if params[k] is None:
del params[k]
order_id = self.session.put(f"{self.root_url}/orders/{variety}/{order_id}",
data=params, headers=self.headers).json()["data"][
"order_id"]
return order_id
def cancel_order(self, variety, order_id, parent_order_id=None):
order_id = self.session.delete(f"{self.root_url}/orders/{variety}/{order_id}",
data={"parent_order_id": parent_order_id} if parent_order_id else {},
headers=self.headers).json()["data"]["order_id"]
return order_id