fyers-apiv3 3.0.1__tar.gz → 3.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,484 @@
1
+ Metadata-Version: 2.1
2
+ Name: fyers_apiv3
3
+ Version: 3.0.3
4
+ Summary: Fyers trading APIs.
5
+ Home-page: https://github.com/FyersDev/fyers-api-py
6
+ Author: Fyers-Tech
7
+ Author-email: support@fyers.in
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE.txt
13
+
14
+ # The Fyers API Python client - v3
15
+
16
+ The official Python client for communicating with the [Fyers API](https://myapi.fyers.in)
17
+
18
+ ## Documentation
19
+
20
+ - [Python client documentation](https://myapi.fyers.in/docsv3)
21
+
22
+ ## Requirements
23
+
24
+ - Python v3
25
+
26
+ ## Installation
27
+
28
+ Install via [pip](https://pypi.org/project/fyers-apiv3/)
29
+
30
+ pip install fyers-apiv3
31
+
32
+ ## Breaking changes - v3
33
+
34
+ `v3` is a **breaking** major release with multiple internal modification to improve user experience.<br>
35
+
36
+ ### New Data Socket:
37
+ - Improved tick update speed, ensuring swift and efficient market data updates.
38
+ - Introducing Lite mode for targeted last traded price (LTP) change updates.
39
+ - SymbolUpdate: Real-time symbol-specific updates for instant parameter changes.
40
+ - DepthUpdate: Real-time market depth changes for selected symbols.
41
+ - Increased subscription capacity, accommodating tracking of 200 symbols.
42
+ - Strengthened error handling callbacks for seamless issue resolution.
43
+
44
+ ### New Order Socket:
45
+ - Real-time updates for orders.
46
+ - Real-time updates for positions.
47
+ - Real-time updates for trades.
48
+ - Real-time updates for eDIS.
49
+ - Real-time updates for price alerts.
50
+ - Improved error handling callbacks.
51
+
52
+
53
+ ## Getting started wih API
54
+
55
+ ```Python
56
+ from fyers_apiv3 import fyersModel
57
+ import webbrowser
58
+
59
+ """
60
+ In order to get started with Fyers API we would like you to do the following things first.
61
+ 1. Checkout our API docs : https://myapi.fyers.in/docsv3
62
+ 2. Create an APP using our API dashboard : https://myapi.fyers.in/dashboard/
63
+
64
+ Once you have created an APP you can start using the below SDK
65
+ """
66
+
67
+ #### Generate an authcode and then make a request to generate an accessToken (Login Flow)
68
+
69
+ """
70
+ 1. Input parameters
71
+ """
72
+ redirect_uri= "APP REDIRECT URI" ## redircet_uri you entered while creating APP.
73
+ client_id = "XCXXXXXxxM-100" ## Client_id here refers to APP_ID of the created app
74
+ secret_key = "MH*****TJ5" ## app_secret key which you got after creating the app
75
+ grant_type = "authorization_code" ## The grant_type always has to be "authorization_code"
76
+ response_type = "code" ## The response_type always has to be "code"
77
+ state = "sample" ## The state field here acts as a session manager. you will be sent with the state field after successfull generation of auth_code
78
+
79
+
80
+ ### Connect to the sessionModel object here with the required input parameters
81
+ appSession = fyersModel.SessionModel(client_id = client_id, redirect_uri = redirect_uri,response_type=response_type,state=state,secret_key=secret_key,grant_type=grant_type)
82
+
83
+ # ## Make a request to generate_authcode object this will return a login url which you need to open in your browser from where you can get the generated auth_code
84
+ generateTokenUrl = appSession.generate_authcode()
85
+
86
+ """There are two method to get the Login url if you are not automating the login flow
87
+ 1. Just by printing the variable name
88
+ 2. There is a library named as webbrowser which will then open the url for you without the hasel of copy pasting
89
+ both the methods are mentioned below"""
90
+ print((generateTokenUrl))
91
+ webbrowser.open(generateTokenUrl,new=1)
92
+
93
+ """
94
+ run the code firstly upto this after you generate the auth_code comment the above code and start executing the below code """
95
+ ##########################################################################################################################
96
+
97
+ ### After succesfull login the user can copy the generated auth_code over here and make the request to generate the accessToken
98
+ auth_code = "Paste the auth_code generated from the first request"
99
+ appSession.set_token(auth_code)
100
+ response = appSession.generate_token()
101
+
102
+ ## There can be two cases over here you can successfully get the acccessToken over the request or you might get some error over here. so to avoid that have this in try except block
103
+ try:
104
+ access_token = response["access_token"]
105
+ except Exception as e:
106
+ print(e,response) ## This will help you in debugging then and there itself like what was the error and also you would be able to see the value you got in response variable. instead of getting key_error for unsuccessfull response.
107
+
108
+
109
+
110
+ ## Once you have generated accessToken now we can call multiple trading related or data related apis after that in order to do so we need to first initialize the fyerModel object with all the requried params.
111
+ """
112
+ fyerModel object takes following values as arguments
113
+ 1. accessToken : this is the one which you received from above
114
+ 2. client_id : this is basically the app_id for the particular app you logged into
115
+ """
116
+ fyers = fyersModel.FyersModel(token=access_token,is_async=False,client_id=client_id,log_path="")
117
+
118
+
119
+ ## After this point you can call the relevant apis and get started with
120
+
121
+ ####################################################################################################################
122
+ """
123
+ 1. User Apis : This includes (Profile,Funds,Holdings)
124
+ """
125
+
126
+ print(fyers.get_profile()) ## This will provide us with the user related data
127
+
128
+ print(fyers.funds()) ## This will provide us with the funds the user has
129
+
130
+ print(fyers.holdings()) ## This will provide the available holdings the user has
131
+
132
+
133
+ ########################################################################################################################
134
+
135
+ """
136
+ 2. Transaction Apis : This includes (Tradebook,Orderbook,Positions)
137
+ """
138
+
139
+ print(fyers.tradebook()) ## This will provide all the trade related information
140
+
141
+ print(fyers.orderbook()) ## This will provide the user with all the order realted information
142
+
143
+ print(fyers.positions()) ## This will provide the user with all the positions the user has on his end
144
+
145
+
146
+ ######################################################################################################################
147
+
148
+ """
149
+ 3. Order Placement : This Apis helps to place order.
150
+ There are two ways to place order
151
+ a. single order : wherein you can fire one order at a time
152
+ b. multi order : this is used to place a basket of order but the basket size can max be 10 symbols
153
+ """
154
+
155
+ ## SINGLE ORDER
156
+
157
+ data = {
158
+ "symbol":"NSE:ONGC-EQ",
159
+ "qty":1,
160
+ "type":1,
161
+ "side":1,
162
+ "productType":"INTRADAY",
163
+ "limitPrice":0,
164
+ "stopPrice":0,
165
+ "validity":"DAY",
166
+ "disclosedQty":0,
167
+ "offlineOrder":False,
168
+ "stopLoss":0,
169
+ "takeProfit":0
170
+ } ## This is a sample example to place a limit order you can make the further changes based on your requriements
171
+
172
+ print(fyers.place_order(data))
173
+
174
+ ## MULTI ORDER
175
+
176
+ data = [{ "symbol":"NSE:SBIN-EQ",
177
+ "qty":1,
178
+ "type":1,
179
+ "side":1,
180
+ "productType":"INTRADAY",
181
+ "limitPrice":61050,
182
+ "stopPrice":0 ,
183
+ "disclosedQty":0,
184
+ "validity":"DAY",
185
+ "offlineOrder":False,
186
+ "stopLoss":0,
187
+ "takeProfit":0
188
+ },
189
+ {
190
+ "symbol":"NSE:HDFC-EQ",
191
+ "qty":1,
192
+ "type":2,
193
+ "side":1,
194
+ "productType":"INTRADAY",
195
+ "limitPrice":0,
196
+ "stopPrice":0 ,
197
+ "disclosedQty":0,
198
+ "validity":"DAY",
199
+ "offlineOrder":False,
200
+ "stopLoss":0,
201
+ "takeProfit":0
202
+ }] ### This takes input as a list containing multiple single order data into it and the execution of the orders goes in the same format as mentioned.
203
+
204
+ print(fyers.place_basket_orders(data))
205
+
206
+
207
+ ###################################################################################################################
208
+
209
+ """
210
+ 4. Other Transaction : This includes (modify_order,exit_position,cancel_order,convert_positions)
211
+ """
212
+
213
+ ## Modify_order request
214
+ data = {
215
+ "id":"7574657627567",
216
+ "type":1,
217
+ "limitPrice": 61049,
218
+ "qty":1
219
+ }
220
+
221
+ print(fyers.modify_order(data))
222
+
223
+ ## Modify Multi Order
224
+
225
+ data = [
226
+ { "id":"8102710298291",
227
+ "type":1,
228
+ "limitPrice": 61049,
229
+ "qty":0
230
+ },
231
+ {
232
+ "id":"8102710298292",
233
+ "type":1,
234
+ "limitPrice": 61049,
235
+ "qty":1
236
+ }]
237
+
238
+ print(fyers.modify_basket_orders(data))
239
+
240
+
241
+ ### Cancel_order
242
+ data = {"id":'808058117761'}
243
+ print(fyers.cancel_order(data))
244
+
245
+ ### cancel_multi_order
246
+ data = [
247
+ {
248
+ "id":'808058117761'
249
+ },
250
+ {
251
+ "id":'808058117762'
252
+ }]
253
+
254
+ print(fyers.cancel_basket_orders(data))
255
+
256
+
257
+ ### Exit Position
258
+ data = {
259
+ "id":"NSE:SBIN-EQ-INTRADAY"
260
+ }
261
+
262
+ print(fyers.exit_positions(data))
263
+
264
+
265
+ ### Convert Position
266
+
267
+ data = {
268
+ "symbol":"MCX:SILVERMIC20NOVFUT",
269
+ "positionSide":1,
270
+ "convertQty":1,
271
+ "convertFrom":"INTRADAY",
272
+ "convertTo":"CNC"
273
+ }
274
+
275
+ print(fyers.convert_position(data))
276
+
277
+
278
+ #################################################################################################################
279
+
280
+ """
281
+ DATA APIS : This includes following Apis(History,Quotes,MarketDepth)
282
+ """
283
+
284
+ ## Historical Data
285
+
286
+ data = {"symbol":"NSE:SBIN-EQ","resolution":"D","date_format":"0","range_from":"1622097600","range_to":"1622097685","cont_flag":"1"}
287
+
288
+ print(fyers.history(data))
289
+
290
+ ## Quotes
291
+
292
+ data = {"symbols":"NSE:SBIN-EQ"}
293
+ print(fyers.quotes(data))
294
+
295
+
296
+ ## Market Depth
297
+
298
+ data = {"symbol":"NSE:SBIN-EQ","ohlcv_flag":"1"}
299
+ print(fyers.depth(data))
300
+
301
+
302
+ ```
303
+
304
+ ## Getting started with Data Socket
305
+
306
+ ```python
307
+
308
+ from fyers_apiv3.FyersWebsocket import data_ws
309
+
310
+
311
+ def onmessage(message):
312
+ """
313
+ Callback function to handle incoming messages from the FyersDataSocket WebSocket.
314
+
315
+ Parameters:
316
+ message (dict): The received message from the WebSocket.
317
+
318
+ """
319
+ print("Response:", message)
320
+
321
+
322
+ def onerror(message):
323
+ """
324
+ Callback function to handle WebSocket errors.
325
+
326
+ Parameters:
327
+ message (dict): The error message received from the WebSocket.
328
+
329
+
330
+ """
331
+ print("Error:", message)
332
+
333
+
334
+ def onclose(message):
335
+ """
336
+ Callback function to handle WebSocket connection close events.
337
+ """
338
+ print("Connection closed:", message)
339
+
340
+
341
+ def onopen():
342
+ """
343
+ Callback function to subscribe to data type and symbols upon WebSocket connection.
344
+
345
+ """
346
+ # Specify the data type and symbols you want to subscribe to
347
+ data_type = "SymbolUpdate"
348
+ # data_type = "DepthUpdate"
349
+
350
+
351
+ # Subscribe to the specified symbols and data type
352
+ symbols = ['NSE:SBIN-EQ', 'NSE:ADANIENT-EQ']
353
+ fyers.subscribe(symbols=symbols, data_type=data_type)
354
+
355
+ # Keep the socket running to receive real-time data
356
+ fyers.keep_running()
357
+
358
+
359
+ # Replace the sample access token with your actual access token obtained from Fyers
360
+ access_token = "XCXXXXXXM-100:eyJ0tHfZNSBoLo"
361
+
362
+ # Create a FyersDataSocket instance with the provided parameters
363
+ fyers = data_ws.FyersDataSocket(
364
+ access_token=access_token, # Access token in the format "appid:accesstoken"
365
+ log_path="", # Path to save logs. Leave empty to auto-create logs in the current directory.
366
+ litemode=False, # Lite mode disabled. Set to True if you want a lite response.
367
+ write_to_file=False, # Save response in a log file instead of printing it.
368
+ reconnect=True, # Enable auto-reconnection to WebSocket on disconnection.
369
+ on_connect=onopen, # Callback function to subscribe to data upon connection.
370
+ on_close=onclose, # Callback function to handle WebSocket connection close events.
371
+ on_error=onerror, # Callback function to handle WebSocket errors.
372
+ on_message=onmessage # Callback function to handle incoming messages from the WebSocket.
373
+ )
374
+
375
+ # Establish a connection to the Fyers WebSocket
376
+ fyers.connect()
377
+
378
+ ```
379
+
380
+
381
+ ## Getting started with Order Socket
382
+
383
+ ```python
384
+ from fyers_apiv3.FyersWebsocket import order_ws
385
+
386
+
387
+ def onTrade(message):
388
+ """
389
+ Callback function to handle incoming messages from the FyersDataSocket WebSocket.
390
+
391
+ Parameters:
392
+ message (dict): The received message from the WebSocket.
393
+
394
+ """
395
+ print("Trade Response:", message)
396
+
397
+ def onOrder(message):
398
+ """
399
+ Callback function to handle incoming messages from the FyersDataSocket WebSocket.
400
+
401
+ Parameters:
402
+ message (dict): The received message from the WebSocket.
403
+
404
+ """
405
+ print("Order Response:", message)
406
+
407
+ def onPosition(message):
408
+ """
409
+ Callback function to handle incoming messages from the FyersDataSocket WebSocket.
410
+
411
+ Parameters:
412
+ message (dict): The received message from the WebSocket.
413
+
414
+ """
415
+ print("Position Response:", message)
416
+
417
+ def onGeneral(message):
418
+ """
419
+ Callback function to handle incoming messages from the FyersDataSocket WebSocket.
420
+
421
+ Parameters:
422
+ message (dict): The received message from the WebSocket.
423
+
424
+ """
425
+ print("General Response:", message)
426
+ def onerror(message):
427
+ """
428
+ Callback function to handle WebSocket errors.
429
+
430
+ Parameters:
431
+ message (dict): The error message received from the WebSocket.
432
+
433
+
434
+ """
435
+ print("Error:", message)
436
+
437
+
438
+ def onclose(message):
439
+ """
440
+ Callback function to handle WebSocket connection close events.
441
+ """
442
+ print("Connection closed:", message)
443
+
444
+
445
+ def onopen():
446
+ """
447
+ Callback function to subscribe to data type and symbols upon WebSocket connection.
448
+
449
+ """
450
+ # Specify the data type and symbols you want to subscribe to
451
+ # data_type = "OnOrders"
452
+ # data_type = "OnTrades"
453
+ # data_type = "OnPositions"
454
+ # data_type = "OnGeneral"
455
+ data_type = "OnOrders,OnTrades,OnPositions,OnGeneral"
456
+
457
+ fyers.subscribe(data_type=data_type)
458
+
459
+ # Keep the socket running to receive real-time data
460
+ fyers.keep_running()
461
+
462
+
463
+ # Replace the sample access token with your actual access token obtained from Fyers
464
+ access_token = "XCXXXXXXM-100:eyJ0tHfZNSBoLo"
465
+
466
+ # Create a FyersDataSocket instance with the provided parameters
467
+ fyers = order_ws.FyersOrderSocket(
468
+ access_token=access_token, # Your access token for authenticating with the Fyers API.
469
+ write_to_file=False, # A boolean flag indicating whether to write data to a log file or not.
470
+ log_path="", # The path to the log file if write_to_file is set to True (empty string means current directory).
471
+ on_connect=onopen, # Callback function to be executed upon successful WebSocket connection.
472
+ on_close=onclose, # Callback function to be executed when the WebSocket connection is closed.
473
+ on_error=onerror, # Callback function to handle any WebSocket errors that may occur.
474
+ on_general=onGeneral, # Callback function to handle general events from the WebSocket.
475
+ on_orders=onOrder, # Callback function to handle order-related events from the WebSocket.
476
+ on_positions=onPosition, # Callback function to handle position-related events from the WebSocket.
477
+ on_trades=onTrade # Callback function to handle trade-related events from the WebSocket.
478
+ )
479
+
480
+ # Establish a connection to the Fyers WebSocket
481
+ fyers.connect()
482
+
483
+
484
+ ```