-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmy_fyers_model.py
executable file
·314 lines (280 loc) · 9.47 KB
/
my_fyers_model.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import os
import pytz
import pandas as pd
import configparser
from constants import *
from datetime import datetime
from fyers_apiv3 import fyersModel
from fyers_apiv3.fyersModel import FyersModel
config = configparser.ConfigParser()
config.read('credentials.ini')
client_id = config['fyers']['client_id']
secret_key = config['fyers']['secret_key']
redirect_url = config['fyers']['redirect_url']
response_type = config['fyers']['response_type']
state = config['fyers']['state']
grant_type = config['fyers']['grant_type']
log_dir = config['fyers']['log_dir']
file_name = config['fyers']['file_name']
time_zone = config['fyers']['time_zone']
verbose = config['fyers']['verbose']
def get_access_token():
if not os.path.exists(file_name):
session = fyersModel.SessionModel(
client_id=client_id,
secret_key=secret_key,
redirect_uri=redirect_url,
response_type=response_type,
grant_type=grant_type
)
response = session.generate_authcode()
print("Login Url : ", response)
auth_code = input("Enter Auth Code : ")
session.set_token(auth_code)
access_token = session.generate_token()['access_token']
with open(file_name, 'w') as f:
f.write(access_token)
else:
with open(file_name, 'r') as f:
access_token = f.read()
return access_token
class MyFyersModel(object):
def __init__(self):
self.token = get_access_token()
self.fyers_model = FyersModel(client_id=client_id, token=get_access_token(), log_path=log_dir, is_async=False)
def get_fyre_model(self):
return self.fyers_model
def get_token(self):
return self.token
# User Details
def get_profile(self):
"""This allows you to fetch basic details of the client.
:return: json object
"""
return self.fyers_model.get_profile()
def get_fund(self, data=None):
"""Shows the balance available for the user for capital as well as the commodity market.
:return: json object
"""
return self.fyers_model.funds()
def get_holdings(self, data=None):
"""Fetches the equity and mutual fund holdings which the user has in this demate account.
This will include T1 and demat holdings.
:return: json object
"""
return self.fyers_model.holdings()
# Transaction Info
def get_order_book(self, data=None):
"""Fetches all the orders placed by the user across all platforms and exchanges in the current trading day.
"""
return self.fyers_model.orderbook(data)
def get_positions(self):
"""
Fetches the current open and closed positions for the current trading day.
Note that previous trading day’s closed positions will not be shown here.
:return:
"""
return self.fyers_model.positions()
def get_tradebook(self, data=None):
"""Fetches all the trades for the current day across all platforms and exchanges in the current trading day."""
return self.fyers_model.tradebook()
# Order Placement
def get_placeorder(self, data):
"""
Single Order
This allows the user to place an order to any exchange via Fyers
:param data:
data = {
"symbol":"NSE:SBIN-EQ",
"qty":1,
"type":2,
"side":1,
"productType":"INTRADAY",
"limitPrice":0,
"stopPrice":0,
"validity":"DAY",
"disclosedQty":0,
"offlineOrder":"False",
}
"""
return self.fyers_model.place_order(data=data)
def get_place_basket_orders(self, data):
"""
Multi Order
You can place upto 10 orders simultaneously via the API.
While Placing Multi orders you need to pass an ARRAY containing the orders request attributes
:param data:
data = [{
"symbol":"NSE:SBIN-EQ",
"qty":1,
"type":2,
"side":1,
"productType":"INTRADAY",
"limitPrice":0,
"stopPrice":0,
"validity":"DAY",
"disclosedQty":0,
"offlineOrder":"False",
},
{
"symbol":"NSE:HDFC-EQ",
"qty":1,
"type":2,
"side":1,
"productType":"INTRADAY",
"limitPrice":0,
"stopPrice":0,
"validity":"DAY",
"disclosedQty":0,
"offlineOrder":"False",
}]
"""
# Other Transactions
def get_modify_order(self, data):
"""
Modify Order
This allows the user to modify a pending order. User can provide parameters which needs to be modified.
In case a particular parameter has not been provided, the original value will be considered.
:param data:
orderId = "8102710298291"
data = {
"id":orderId,
"type":1,
"limitPrice": 61049,
"qty":1
}
:return:
"""
return self.fyers_model.modify_order(data=data)
def get_modify_basket_orders(self, data):
"""
Modify Multi Orders
You can modify upto 10 orders simultaneously via the API.
While Modifying Multi orders you need to pass an ARRAY containing the orders request attributes
:param data:
orderId = "8102710298291"
data = [{
"id": 8102710298291,
"type": 1,
"limitPrice": 61049,
"qty": 1
},
{
"id": 8102710298292,
"type": 1,
"limitPrice": 61049,
"qty": 1
}]
:return:
"""
return self.fyers_model.modify_basket_orders(data)
# Cancel Order
def get_cancel_order(self, data):
"""
Cancel Order
Cancel pending orders
:param data
data = {"id":'808058117761'}
:return:
"""
return self.fyers_model.cancel_order(data)
# Cancel Multi Order
def get_cancel_basket_orders(self, data):
"""
Cancel Multi Order
You can cancel upto 10 orders simultaneously via the API.
While cancelling Multi orders you need to pass an ARRAY containing the orders request attributes
:param data:
data = [{
"id": '808058117761'
},
{
"id": '808058117762'
}]
:return:
"""
return self.fyers_model.cancel_basket_orders(data=data)
# Exit Position
def get_exit_position(self, data):
"""
Exit Position
This allows the user to either exit all open positions or any specific open position.
:param data:
data = {}
:return:
"""
return self.fyers_model.exit_positions(data=data)
# Exit Position - By Id
def get_exit_position_by_id(self, data):
"""
Exit Position - By Id
This will only exit the open positions for a particular position id
:param data:
data = {
"id":"NSE:SBIN-EQ-BO"
}
:return:
"""
return self.fyers_model.exit_positions(data=data)
# Pending Order Cancel
def pending_order_cancel(self):
pass
# Convert Position
def get_convert_position(self, data):
"""
Convert Position
This allows the user to convert an open position from one product type to another.
:param data:
data = {
"symbol":"MCX:SILVERMIC20NOVFUT",
"positionSide":1,
"convertQty":1,
"convertFrom":"INTRADAY",
"convertTo":"CNC"
}
:return:
"""
return self.fyers_model.convert_position(data=data)
# Broker Config
def get_market_status(self):
"""
Market Status
Fetches the current market status of all the exchanges and their segments
:return:
"""
return self.fyers_model.market_status()
# Data API
def get_history(self, data):
"""
History
The historical API provides archived data (up to date) for the symbols.
across various exchanges within the given range. A historical record is presented in the form of a candle
and the data is available in different resolutions
like - minute, 10 minutes, 60 minutes...240 minutes and daily.
:return: Candels data
"""
return self.fyers_model.history(data=data)
def get_quotes(self, data):
"""
Quotes
Quotes API retrieves the full market quotes for one or more symbols provided by the user.
:param data:
data = {
"symbols":"NSE:SBIN-EQ,NSE:HDFC-EQ"
}
:return: json
"""
return self.fyers_model.quotes(data=data)
def get_market_depth(self, data):
"""
Market Depth
The Market Depth API returns the complete market data of the symbol provided.
It will include the quantity, OHLC values, and Open Interest fields, and bid/ask prices.
:param data:
data = {
"symbol":"NSE:SBIN-EQ",
"ohlcv_flag":"1"
}
:return: json
"""
return self.fyers_model.depth(data=data)