-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrader.py
1376 lines (1083 loc) · 50.3 KB
/
trader.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Copyright 2013-2024 Aditya <code.aditya@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
**trader** is a Python 3 module which provides functions to download and
process the data for Stock Markets.
This is expected to be used as a back-end module for a GUI which
utilizes the work of this module. However, users are free to find their
own creative uses.
Exchange/Segments Supported
---------------------------
- NSE/Indices (India)
- NSE/Equities (India)
- NSE/Futures (India)
Functions Available
-------------------
Public Functions
++++++++++++++++
This module provides the following public functions:
``process_nse_indices()``:
download and process NSE Indices data to save as csv
``process_nse_equities()``:
download and process NSE Equities data to save as csv
``process_nse_futures()``:
download and process NSE Futures data to save as csv
``download_file()``:
download the files off internet
``apply_server_timestamp()``:
apply the last-modified timestamp to files as received from headers
``to_datetime_date()``:
return datetime.date object where possible else None
``create_folder()``:
create the folder in a safe manner
``get_request_headers()``:
return "request headers" dict for `download_file()`
``unzip()``:
extract the contents of zip file in present working directory
``write_csv()``:
generate and save the file given necessary arguments
Private Functions
+++++++++++++++++
See the docstrings of individual private functions for details:
- ``_read_input_as_list()``
- ``_convert_dash_to_zero()``
- ``_convert_blank_to_zero()``
- ``_sanitize_ohlc()``
- ``_pop_unnecessary_keys()``
- ``_format_output_data()``
- ``_finalize_output()``
- ``_download_nse_indices()``
- ``_download_nse_equities()``
- ``_download_nse_futures()``
- ``_get_nse_indices_fieldnames()``
- ``_get_nse_equities_fieldnames()``
- ``_get_nse_futures_fieldnames()``
- ``_get_nse_indices_filenames()``
- ``_get_nse_equities_filenames()``
- ``_get_nse_futures_filenames()``
- ``_manipulate_nse_indices()``
- ``_manipulate_nse_equities()``
- ``_manipulate_nse_futures()``
- ``_parse_nse_indices()``
- ``_parse_nse_equities()``
- ``_parse_nse_futures()``
- ``_output_nse_indices()``
- ``_output_nse_equities()``
- ``_output_nse_futures()``
Global Variables
----------------
- ``DEBUGGING = False``
When it is ``True``, no attempt is made to download the files
How To Use This Module
----------------------
1. Import the module using:
import trader
2. The user is expected to primarily make use of the following functions:
- ``process_nse_indices()``
- ``process_nse_equities()``
- ``process_nse_futures()``
Just pass the required arguments to them to download and process the
data for required Exchange/Segments.
3. ``download_file()`` function can be used to download anything off the
internet.
------------------------------------------------------------------------
"""
# Import Standard modules
import csv
import datetime
import logging
import os
import socket
import urllib.error
import urllib.request
import zipfile
import zlib
###############################################################################
#### Add logging functionality ################################################
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Console handler
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# Output formatting
formatter = logging.Formatter("{asctime} - {module:15s} - {process} - "
"{levelname:8s} - {message}", style="{")
ch.setFormatter(formatter)
# Add console handler to logger object
logger.addHandler(ch)
###############################################################################
###############################################################################
#### Other Global Variables ###################################################
# When DEBUGGING is set as True, it doesn't attempt to download anything
DEBUGGING = False
###############################################################################
def process_nse_indices(start_date,
end_date=None,
download_location=os.path.join(os.getcwd(),
'downloads'),
output_location=os.path.join(os.getcwd(),
'processed_data'),
ignore_weekend=True):
"""Processes the data for NSE Indices.
:param start_date: date from when the files should be downloaded
:param end_date: date upto which the files should be downloaded
:param download_location: where to save the downloaded files
:param output_location: where to save the processed output files
:param ignore_weekend: when set as True, it doesn't attempt to
process the data for Saturdays and Sundays
:type start_date: datetime.datetime or datetime.date or str
:type end_date: None or datetime.datetime or datetime.date or str
:type download_location: str
:type output_location: str
:type ignore_weekend: bool
When passing `start_date` and `end_date` arguments as str, it should
be in the form of "YYYY-MM-DD" like "2014-01-01".
`download_location` is used to pass as `download_location` argument
to `_download_nse_indices()` and `input_location` argument to
`_output_nse_indices()`.
`output_location` is used to pass as `output_location` argument to
`_output_nse_indices()`.
Pass `ignore_weekend` as False if any trades happen on a Saturday or
a Sunday. This would be specially useful in case of Muhurat Trading.
Examples:
>>> process_nse_indices("2014-01-01")
>>> process_nse_indices("2014-01-01", "2014-01-01")
>>> process_nse_indices("2014-01-01", "2014-01-31")
>>> process_nse_indices(datetime.datetime(2014,1,1), datetime.date(2014,1,1))
>>> process_nse_indices("2014-01-01", "2014-01-31", ignore_weekend=False)
>>> process_nse_indices("2014-01-01", "2014-01-31", download_location=os.getcwd(), output_location=os.getcwd())
"""
logger.info("Processing NSE Indices...")
if end_date is None:
end_date = start_date
# Ensure date inputs are datetime.date objects
start_date = to_datetime_date(start_date)
end_date = to_datetime_date(end_date)
# Other initialization stuff
current_date = start_date
time_delta = datetime.timedelta(days=1)
# Loop through dates to download files and output csv files
while current_date <= end_date:
if ignore_weekend and datetime.date.isoweekday(current_date) in (6, 7):
logger.debug("Skipped NSE Indices for {0}.".format(current_date))
current_date += time_delta
continue
_download_nse_indices(current_date,
download_location=download_location)
_output_nse_indices(current_date,
input_location=download_location,
output_location=output_location)
current_date += time_delta
def process_nse_equities(start_date,
end_date=None,
download_location=os.path.join(os.getcwd(),
'downloads'),
output_location=os.path.join(os.getcwd(),
'processed_data'),
ignore_weekend=True):
"""Processes the data for NSE Equities.
:param start_date: date from when the files should be downloaded
:param end_date: date upto which the files should be downloaded
:param download_location: where to save the downloaded files
:param output_location: where to save the processed output files
:param ignore_weekend: when set as True, it doesn't attempt to
process the data for Saturdays and Sundays
:type start_date: datetime.datetime or datetime.date or str
:type end_date: None or datetime.datetime or datetime.date or str
:type download_location: str
:type output_location: str
:type ignore_weekend: bool
When passing `start_date` and `end_date` arguments as str, it should
be in the form of "YYYY-MM-DD" like "2014-01-01".
`download_location` is used to pass as `download_location` argument
to `_download_nse_equities()` and `input_location` argument to
`_output_nse_equities()`.
`output_location` is used to pass as `output_location` argument to
`_output_nse_equities()`.
Pass `ignore_weekend` as False if any trades happen on a Saturday or
a Sunday. This would be specially useful in case of Muhurat Trading.
Examples:
>>> process_nse_equities("2014-01-01")
>>> process_nse_equities("2014-01-01", "2014-01-01")
>>> process_nse_equities("2014-01-01", "2014-01-31")
>>> process_nse_equities(datetime.datetime(2014,1,1), datetime.date(2014,1,1))
>>> process_nse_equities("2014-01-01", "2014-01-31", ignore_weekend=False)
>>> process_nse_equities("2014-01-01", "2014-01-31", download_location=os.getcwd(), output_location=os.getcwd())
"""
logger.info("Processing NSE Equities...")
if end_date is None:
end_date = start_date
# Ensure date inputs are datetime.date objects
start_date = to_datetime_date(start_date)
end_date = to_datetime_date(end_date)
# Misc variables
current_date = start_date
time_delta = datetime.timedelta(days=1)
# Loop through dates to download files and output csv files
while current_date <= end_date:
if ignore_weekend and datetime.date.isoweekday(current_date) in (6, 7):
logger.debug("Skipped NSE Equities for {0}.".format(current_date))
current_date += time_delta
continue
_download_nse_equities(current_date,
download_location=download_location)
_output_nse_equities(current_date,
input_location=download_location,
output_location=output_location)
current_date += time_delta
def process_nse_futures(start_date,
end_date=None,
download_location=os.path.join(os.getcwd(),
'downloads'),
output_location=os.path.join(os.getcwd(),
'processed_data'),
ignore_weekend=True):
"""Processes the data for NSE Futures.
:param start_date: date from when the files should be downloaded
:param end_date: date upto which the files should be downloaded
:param download_location: where to save the downloaded files
:param output_location: where to save the processed output files
:param ignore_weekend: when set as True, it doesn't attempt to
process the data for Saturdays and Sundays
:type start_date: datetime.datetime or datetime.date or str
:type end_date: None or datetime.datetime or datetime.date or str
:type download_location: str
:type output_location: str
:type ignore_weekend: bool
When passing `start_date` and `end_date` arguments as str, it should
be in the form of "YYYY-MM-DD" like "2014-01-01".
`download_location` is used to pass as `download_location` argument
to `_download_nse_futures()` and `input_location` argument to
`_output_nse_futures()`.
`output_location` is used to pass as `output_location` argument to
`_output_nse_futures()`.
Pass `ignore_weekend` as False if any trades happen on a Saturday or
a Sunday. This would be specially useful in case of Muhurat Trading.
Examples:
>>> process_nse_futures("2014-01-01")
>>> process_nse_futures("2014-01-01", "2014-01-01")
>>> process_nse_futures("2014-01-01", "2014-01-31")
>>> process_nse_futures(datetime.datetime(2014,1,1), datetime.date(2014,1,1))
>>> process_nse_futures("2014-01-01", "2014-01-31", ignore_weekend=False)
>>> process_nse_futures("2014-01-01", "2014-01-31", download_location=os.getcwd(), output_location=os.getcwd())
"""
logger.info("Processing NSE Futures...")
if end_date is None:
end_date = start_date
# Ensure date inputs are datetime.date objects
start_date = to_datetime_date(start_date)
end_date = to_datetime_date(end_date)
# Other initialization stuff
current_date = start_date
time_delta = datetime.timedelta(days=1)
# Loop through dates to download files and output csv files
while current_date <= end_date:
if ignore_weekend and datetime.date.isoweekday(current_date) in (6, 7):
logger.debug("Skipped NSE Futures for {0}.".format(current_date))
current_date += time_delta
continue
_download_nse_futures(current_date,
download_location=download_location)
_output_nse_futures(current_date,
input_location=download_location,
output_location=output_location)
current_date += time_delta
def download_file(*urls,
download_location=os.path.join(os.getcwd(), 'downloads')):
"""Downloads the files provided as multiple url arguments.
:param urls: the url for files to be downloaded, separated by commas
:param download_location: where to save the downloaded files
:type urls: str
:type download_location: str
The function would download the files and save it in the folder
provided as keyword-argument for `download_location`. If
`download_location` is not provided, then the file would be saved in
directory called as "downloads" created in the current working
directory. Folder for `download_location` would be created if it
doesn't already exist.
If the download encounters an error it would alert about it and
provide the information about the Error Code and Error Reason where
possible.
It preserves the Last Modification time as received from the server.
It uses `DEBUGGING` global variable to activate the Debug Mode.
In Debug Mode, files are not downloaded, neither there is any
attempt to establish the connection with the server. It just prints
out the filename and its url that would have been attempted to be
downloaded in Normal Mode.
Examples:
>>> download_file('http://localhost/index.html', 'http://localhost/info.php')
>>> download_file('http://localhost/index.html', download_location='/home/aditya/Download/test')
>>> download_file('http://localhost/index.html', download_location='/home/aditya/Download/test/')
"""
# Some initialization stuff
create_folder(download_location)
headers = get_request_headers()
# Loop through all the files to be downloaded
for url in urls:
if not url:
logger.debug("None url: Nothing to download.")
continue
filename = os.path.basename(url)
if not DEBUGGING:
logger.info("Downloading {0}.".format(url))
try:
request_sent = urllib.request.Request(url, headers=headers)
response_received = urllib.request.urlopen(request_sent, timeout=10)
logger.debug("Response received from {0}.".format(url))
except urllib.error.URLError as e:
logger.warning("Error {code} : {reason} : {filename}".format(
code=e.code, reason=e.reason, filename=filename
))
except socket.timeout as e:
logger.error("Request Timed Out. Couldn't download {0}".format(url))
else:
output_file = os.path.join(download_location, filename)
read_response = response_received.read()
# Since we "Accept-Encoding" as "gzip", we need to
# decompress the response if received as such
# See: https://stackoverflow.com/q/16084117/1928540
if response_received.headers.get('Content-Encoding') == 'gzip':
logger.debug("Response received with 'gzip' header. "
"Decompressing it.")
read_response = zlib.decompress(read_response,
16 + zlib.MAX_WBITS)
with open(output_file, 'wb') as downloaded_file:
downloaded_file.write(read_response)
last_modified = response_received.headers.get('Last-Modified')
apply_server_timestamp(output_file, last_modified)
logger.info("{0}: Downloaded successfully.".format(filename))
else:
logger.info("DEBUG MODE: {0} would be downloaded from {1}."
"".format(filename, url))
def apply_server_timestamp(output_file, last_modified):
"""Changes the last modified and last access time of the output file
to the one as received from Last-Modified response header.
:param output_file: the file whose timestamp is to be altered
:param last_modified: time string as received from Response headers
:type output_file: str
:type last_modified: str
"""
server_datetime = datetime.datetime.strptime(last_modified,
"%a, %d %b %Y %H:%M:%S GMT")
server_utc_time = server_datetime.replace(tzinfo=datetime.timezone.utc)
server_timestamp = server_utc_time.timestamp()
os.utime(output_file, times=(server_timestamp, server_timestamp))
def to_datetime_date(input_date):
"""Checks the type of `input_date` and returns a datetime.date
object where possible, else returns None.
:param input_date: date to be converted to datetime.date object
:type input_date: datetime.datetime or datetime.date or str
:rtype: datetime.date or None
:raises: ValueError when `input_date` is provided as str not in
format of "%Y-%m-%d"
"""
date = None
if type(input_date) is datetime.date:
date = input_date
elif type(input_date) is datetime.datetime:
date = input_date.date()
elif type(input_date) is str:
date = datetime.datetime.strptime(input_date, '%Y-%m-%d').date()
return date
def create_folder(folder):
"""Creates the folder, no problem if the folder already exists.
:param folder: the location to be created
:type folder: str
"""
try:
os.makedirs(folder, exist_ok=True)
logger.debug("{0} folder exists. Ready to process further."
"".format(folder))
except FileExistsError:
# This is raised on Linux when a folder already exists however
# its mode (ie; Permissions) do not match with the default mode
logger.warning("{0}: Make sure the folder already exists and you have "
"write permissions for it.".format(folder))
def get_request_headers():
"""Returns "Request Headers" information as a dictionary.
:rtype: dict
"""
return {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive',
'Referer': 'https://www.nseindia.com/',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0',
}
def unzip(input_zipfile):
"""Unzips all the contents of the file in present working directory.
:param input_zipfile: the file to be extracted
:type input_zipfile: str
"""
try:
with zipfile.ZipFile(input_zipfile, 'r') as input_file:
input_file.extractall()
logger.debug("{0}: Zip file extracted.".format(input_zipfile))
except FileNotFoundError:
logger.warning("{0}: File not found. Zip file could not be extracted."
"".format(input_zipfile))
def write_csv(output_file, header, fieldnames, output_data):
"""Outputs the processed data to a file for final consumption.
:param output_file: the file to be generated
:param header: the header line to be written to `output_file`
:param fieldnames: fieldnames present in each element of
`output_data`
:param output_data: the data to be written
:type output_file: str
:type header: tuple
:type fieldnames: tuple
:type output_data: list containing each element as a dict
"""
if len(output_data) < 1:
return
with open(output_file, 'w', newline='') as file:
csv.writer(file, delimiter=',').writerow(header)
csv.DictWriter(file, fieldnames, delimiter=',').writerows(output_data)
logger.info("{0}: File generated.".format(output_file))
def _read_input_as_list(input_file, input_fieldnames):
"""Reads the input_file and returns a list, each element of which is
a dictionary containing the record for a particular Symbol.
:param input_file: file to be read
:param input_fieldnames: fieldnames present in `input_file`
:type input_file: str
:type input_fieldnames: tuple
:returns: data read with csv.DictReader
:rtype: list containing each element as a dict
"""
with open(input_file, 'r') as file:
input_data = list(csv.DictReader(file,
delimiter=',',
fieldnames=input_fieldnames,
restkey='Skip',
restval='Skip'))
return input_data
def _convert_dash_to_zero(data):
"""Takes a dictionary and if any of the values is "-", it converts
it to "0".
:param data: dictionary of record
:type data: dict
"""
for element in data:
if data[element] == '-':
data[element] = '0'
def _convert_blank_to_zero(data):
"""Takes a dictionary and if any of the values is blank i.e. "", it
converts it to "0".
:param data: dictionary of record
:type data: dict
"""
for element in data:
if data[element] == '':
data[element] = '0'
def _sanitize_ohlc(input_dict):
"""Takes a dictionary and modifies the values of Open, High and Low
to be the value of Close; when the values of all of them is equal to
zero.
:param input_dict: dictionary of record to be sanitized
:type input_dict: dict
"""
keys = ['Open', 'High', 'Low']
if input_dict['Close'] != '0' and all(input_dict[x] == '0' for x in keys):
for field in keys:
input_dict[field] = input_dict['Close']
def _pop_unnecessary_keys(data):
"""Takes a list containing each element as a dictionary and removes
unnecessary keys for final output.
:param data: list of output data
:type data: list containing each element as a dict
"""
unnecessary_keys = [
'Change', 'Change_pct', 'Turnover', 'PE', 'PB', 'ISIN', 'Div_yield',
'Prev_Close', 'Skip', 'Series', 'LTP', 'Total_Trades', 'Instrument',
'Expiry_Date', 'Strike_Price', 'Option_Type', 'Settlement_Price',
'Contracts', 'Turnover_lakh', 'OI_Change', 'BizDt', 'Sgmt', 'Src',
'FinInstrmTp', 'FinInstrmId', 'ISIN', 'XpryDt', 'FininstrmActlXpryDt',
'StrkPric', 'OptnTp', 'FinInstrmNm', 'UndrlygPric', 'SttlmPric',
'OpnIntrst', 'ChngInOpnIntrst', 'SsnId', 'NewBrdLotQty', 'Rmks',
'Rsvd1', 'Rsvd2', 'Rsvd3', 'Rsvd4',
]
for element in data:
for key in unnecessary_keys:
element.pop(key, None)
def _format_output_data(data):
"""Takes a list containing each element as a dictionary and formats
the keys for final output.
:param data: list of output data
:type data: list containing each element as a dict
"""
strip_spaces = ['Symbol', 'Open', 'High', 'Low', 'Close', 'Volume', 'OI']
format_floats = ['Open', 'High', 'Low', 'Close']
for element in data:
# element['Date'] is to be formatted in _manipulate_*() functions
for key in strip_spaces:
element[key] = element[key].strip()
for key in format_floats:
element[key] = "{0:.2f}".format(float(element[key]))
def _finalize_output(data):
"""Takes a list containing each element as a dictionary and
finalizes it for final consumption.
:param data: list of output data
:type data: list containing each element as a dict
"""
if len(data) < 1:
logger.warning("No data available.")
return
_pop_unnecessary_keys(data)
_format_output_data(data)
def _download_nse_indices(date,
download_location=os.path.join(os.getcwd(),
'downloads')):
"""Downloads the files for NSE Indices.
:param date: date for which files to be downloaded
:param download_location: where to save the downloaded files
:type date: datetime.date
:type download_location: str
The files downloaded are:
- **Bhavcopy File:** This is the main file which contains most of
the required data.
- **Vix File:** This contains the data for INDIAVIX Index. Data for
INDIAVIX is not present in the Bhavcopy File.
"""
# Generate download URLs
bhavcopy, vix = None, None
bhavcopy = r'https://archives.nseindia.com/content/indices/ind_close_all_{full_date}.csv'.format(
full_date=date.strftime('%d%m%Y'))
# Since 14th May 2014, data for INDIAVIX is included in bhavcopy
# itself, so no need to download it separately
if date < datetime.date(2014, 5, 14):
vix = r'https://www1.nseindia.com/content/vix/histdata/hist_india_vix_{full_date}_{full_date}.csv'.format(
full_date=date.strftime('%d-%b-%Y'))
# Download the files
download_file(bhavcopy, vix, download_location=download_location)
def _download_nse_equities(date,
download_location=os.path.join(os.getcwd(),
'downloads')):
"""Downloads the files for NSE Equities.
:param date: date for which files to be downloaded
:param download_location: where to save the downloaded files
:type date: datetime.date
:type download_location: str
The files downloaded are:
- **Bhavcopy File:** This is the main file which contains most of
the required data.
- **Delivery File:** This file is downloaded because it contains the
data for Deliverable Quantity ie; how much quantity was traded for
Delivery ie; (Total Volume - Intraday Volume).
The Deliverable Quantity data would be inserted into Open Interest
field in the `_output_nse_equities()` function.
"""
# Generate download URLs
delivery = r'https://archives.nseindia.com/archives/equities/mto/MTO_{full_date}.DAT'.format(
full_date=date.strftime('%d%m%Y'))
if date > datetime.date(2024, 7, 7):
# CM-UDiFF Common Bhavcopy Final (zip)
bhavcopy = r'https://archives.nseindia.com/content/cm/BhavCopy_NSE_CM_0_0_0_{date}_F_0000.csv.zip'.format(
date=date.strftime('%Y%m%d'))
else:
bhavcopy = r'https://archives.nseindia.com/content/historical/EQUITIES/{year}/{mon}/cm{date}{mon}{year}bhav.csv.zip'.format(
year=date.strftime('%Y'),
mon=(date.strftime('%b')).upper(),
date=date.strftime('%d'))
# Download the files
download_file(bhavcopy, delivery, download_location=download_location)
def _download_nse_futures(date,
download_location=os.path.join(os.getcwd(),
'downloads')):
"""Downloads the files for NSE Futures.
:param date: date for which files to be downloaded
:param download_location: where to save the downloaded files
:type date: datetime.date
:type download_location: str
The files downloaded are:
- **Bhavcopy File:** This is the main file which contains most of
the required data.
"""
# Generate download URLs
if date > datetime.date(2024, 7, 7):
# F&O-UDiFF Common Bhavcopy Final (zip)
bhavcopy = r'https://archives.nseindia.com/content/fo/BhavCopy_NSE_FO_0_0_0_{date}_F_0000.csv.zip'.format(
date=date.strftime('%Y%m%d'))
else:
bhavcopy = r'https://archives.nseindia.com/content/historical/DERIVATIVES/{year}/{mon}/fo{date}{mon}{year}bhav.csv.zip'.format(
year=date.strftime('%Y'),
mon=(date.strftime('%b')).upper(),
date=date.strftime('%d'))
# Download the files
download_file(bhavcopy, download_location=download_location)
def _get_nse_indices_fieldnames():
"""Returns the fieldnames present in bhavcopy file, vix file and the
output file for NSE Indices that we would generate.
:rtype: tuple of tuples
"""
bhav_fieldnames = ('Symbol', 'Date', 'Open', 'High', 'Low', 'Close',
'Change', 'Change_pct', 'Volume', 'Turnover', 'PE',
'PB', 'Div_yield')
vix_fieldnames = ('Date', 'Open', 'High', 'Low', 'Close', 'Prev_Close',
'Change', 'Change_pct')
ind_fieldnames = ('Symbol', 'Date', 'Open', 'High', 'Low', 'Close',
'Volume', 'OI')
return bhav_fieldnames, vix_fieldnames, ind_fieldnames
def _get_nse_equities_fieldnames(date):
"""Returns the fieldnames present in bhavcopy file, delivery file
and the output file for NSE Equities that we would generate.
:param date: date for which files are to be processed
:type date: datetime.date
:rtype: tuple of tuples
"""
delv_fieldnames = ('Type', 'Sl_No', 'Symbol', 'Series', 'Volume', 'OI',
'OI_%')
eq_fieldnames = ('Symbol', 'Date', 'Open', 'High', 'Low', 'Close',
'Volume', 'OI')
if date > datetime.date(2024, 7, 7):
bhav_fieldnames = (
'Date', 'BizDt', 'Sgmt', 'Src', 'FinInstrmTp', 'FinInstrmId',
'ISIN', 'Symbol', 'Series', 'XpryDt', 'FininstrmActlXpryDt',
'StrkPric', 'OptnTp', 'FinInstrmNm', 'Open', 'High', 'Low',
'Close', 'LTP', 'Prev_Close', 'UndrlygPric', 'SttlmPric',
'OpnIntrst', 'ChngInOpnIntrst', 'Volume', 'Turnover',
'Total_Trades', 'SsnId', 'NewBrdLotQty', 'Rmks', 'Rsvd1', 'Rsvd2',
'Rsvd3', 'Rsvd4',
)
else:
bhav_fieldnames = (
'Symbol', 'Series', 'Open', 'High', 'Low', 'Close', 'LTP',
'Prev_Close', 'Volume', 'Turnover', 'Date', 'Total_Trades', 'ISIN',
)
return bhav_fieldnames, delv_fieldnames, eq_fieldnames
def _get_nse_futures_fieldnames(date):
"""Returns the fieldnames present in bhavcopy file and the output
file for NSE Futures that we would generate.
:param date: date for which files are to be processed
:type date: datetime.date
:rtype: tuple of tuples
"""
fut_fieldnames = ('Symbol', 'Date', 'Open', 'High', 'Low', 'Close',
'Volume', 'OI')
if date > datetime.date(2024, 7, 7):
bhav_fieldnames = (
'Date', 'BizDt', 'Sgmt', 'Src', 'Instrument', 'FinInstrmId',
'ISIN', 'Symbol', 'Series', 'Expiry_Date', 'FininstrmActlXpryDt',
'Strike_Price', 'Option_Type', 'FinInstrmNm', 'Open', 'High', 'Low',
'Close', 'LTP', 'Prev_Close', 'UndrlygPric', 'Settlement_Price',
'OI', 'OI_Change', 'Contracts', 'Turnover_lakh',
'Total_Trades', 'SsnId', 'NewBrdLotQty', 'Rmks', 'Rsvd1', 'Rsvd2',
'Rsvd3', 'Rsvd4',
)
else:
bhav_fieldnames = (
'Instrument', 'Symbol', 'Expiry_Date', 'Strike_Price',
'Option_Type', 'Open', 'High', 'Low', 'Close', 'Settlement_Price',
'Contracts', 'Turnover_lakh', 'OI', 'OI_Change', 'Date',
)
return bhav_fieldnames, fut_fieldnames
def _get_nse_indices_filenames(date):
"""Generates filenames for bhavcopy csv file, vix csv file and the
output csv file for NSE Indices.
:param date: date for which filenames are required
:type date: datetime.date
:rtype: tuple of str
"""
bhav_filename = r'ind_close_all_{current_date}.csv'.format(
current_date=date.strftime('%d%m%Y'))
vix_filename = r'hist_india_vix_{current_date}_{current_date}.csv'.format(
current_date=date.strftime('%d-%b-%Y'))
ind_filename = r'NSE-Indices-{current_date}.csv'.format(
current_date=date.strftime('%Y-%m-%d'))
return bhav_filename, vix_filename, ind_filename
def _get_nse_equities_filenames(date):
"""Generates filenames for bhavcopy csv.zip file, delivery DAT file
and the output csv file for NSE Equities.
:param date: date for which filenames are required
:type date: datetime.date
:rtype: tuple of str
"""
delv_filename = r'MTO_{current_date}.DAT'.format(
current_date=date.strftime('%d%m%Y'))
eq_filename = r'NSE-Equities-{current_date}.csv'.format(
current_date=date.strftime('%Y-%m-%d'))
if date > datetime.date(2024, 7, 7):
bhav_filename = r'BhavCopy_NSE_CM_0_0_0_{date}_F_0000.csv.zip'.format(
date=date.strftime('%Y%m%d'))
else:
bhav_filename = r'cm{current_date}bhav.csv.zip'.format(
current_date=(date.strftime('%d%b%Y')).upper())
return bhav_filename, delv_filename, eq_filename
def _get_nse_futures_filenames(date):
"""Generates filenames for bhavcopy csv.zip file and the output csv
file for NSE Futures.
:param date: date for which filenames are required
:type date: datetime.date
:rtype: tuple of str
"""
fut_filename = r'NSE-Futures-{current_date}.csv'.format(
current_date=date.strftime('%Y-%m-%d'))
if date > datetime.date(2024, 7, 7):
bhav_filename = r'BhavCopy_NSE_FO_0_0_0_{date}_F_0000.csv.zip'.format(
date=date.strftime('%Y%m%d'))
else:
bhav_filename = r'fo{current_date}bhav.csv.zip'.format(
current_date=(date.strftime('%d%b%Y')).upper())
return bhav_filename, fut_filename
def _manipulate_nse_indices(date, input_data, output_data):
"""Manipulates the data for NSE Indices.
:param date: date the data relates to
:param input_data: data to be manipulated
:param output_data: data after manipulation
:type date: datetime.date
:type input_data: list containing each element as a dict
:type output_data: list containing each element as a dict
:returns: `output_data`
:rtype: list containing each element as a dict
The function takes `input_data`, manipulates and appends it to
`output_data` and returns `output_data` at the end.
"""
for foo in input_data:
record = foo.copy()
record['OI'] = '0'
if 'Symbol' in record:
record['Symbol'] = record['Symbol'].upper().replace(" ", "")
try:
# Turnover is in Rs. Crore, convert it to Rs. Lakh and
# use it as Volume which is more useful than sum of
# volume of all the scrips in case of Indices
volume = float(record['Turnover']) * 100
record['Volume'] = "{0:.0f}".format(volume)
except ValueError:
# This is raised because of the header line
pass
else:
record['Symbol'] = 'INDIAVIX'
record['Volume'] = '0'
date_str = record.get('Date')
# Ignore header row containing the word "Date"
if date_str.find("Date") != -1:
continue
record['Date'] = date.isoformat()
_convert_dash_to_zero(record)
_convert_blank_to_zero(record)
_sanitize_ohlc(record)
output_data.append(record)
return output_data
def _manipulate_nse_equities(date, input_bhav, input_delv, output_data):
"""Manipulates the data for NSE Equities.
:param date: date for which files to be processed
:param input_bhav: primary data to be manipulated