-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMFAPI.cs
2113 lines (1911 loc) · 112 KB
/
MFAPI.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI.WebControls;
//following gets all data for mf=53 from start to to date, if you dont give mf code then it will download data for all MF
//http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf=53&frmdt=01-Jul-2020&todt=25-Sep-2020
//http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf=53&frmdt=2020-07-01&todt=2020-09-25
//static string urlMFCompCodeHistoryURL = "http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf={0}&frmdt={1}&todt={2}";
namespace Analytics
{
public static class MFAPI
{
public static ListItem[] listFundHouseMaster = new[]
{
//Following list is created based on http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?
// use document.getElementById("ctl00_amfiHomeContent_cmbMutualFund")[0] gives following
//<option value="0">-- Select Mutual Fund --</option>
//document.getElementById("ctl00_amfiHomeContent_cmbMutualFund")[0].value gives following
//"0"
//ocument.getElementById("ctl00_amfiHomeContent_cmbMutualFund")[1].innerHTML gives following
//"All"
//DropDownSubCategories.Items.Clear();
//DropDownSubCategories.Items.AddRange(Electronics);
// OR
//DropDownSubCategories.Items.Add(new ListItem{ Value = "2", Text = "Home Audio"}); .......
new ListItem{ Value = "-1" , Text = "--Select Mutual Fund --"},
//new ListItem{ Value = "all" , Text = "All </ option >
new ListItem{ Value ="39" , Text = "ABN AMRO Mutual Fund"},
new ListItem{ Value = "3" , Text = "Aditya Birla Sun Life Mutual Fund"},
new ListItem{ Value = "50" , Text = "AEGON Mutual Fund"},
new ListItem{ Value = "1" , Text = "Alliance Capital Mutual Fund"},
new ListItem{ Value = "53" , Text = "Axis Mutual Fund"},
new ListItem{ Value = "4" , Text = "Baroda Mutual Fund"},
new ListItem{ Value = "36" , Text = "Benchmark Mutual Fund"},
new ListItem{ Value = "59" , Text = "BNP Paribas Mutual Fund"},
new ListItem{ Value = "46" , Text = "BOI AXA Mutual Fund"},
new ListItem{ Value = "32" , Text = "Canara Robeco Mutual Fund"},
new ListItem{ Value = "60" , Text = "Daiwa Mutual Fund"},
new ListItem{ Value = "31" , Text = "DBS Chola Mutual Fund"},
new ListItem{ Value = "38" , Text = "Deutsche Mutual Fund"},
new ListItem{ Value = "6" , Text = "DSP Mutual Fund"},
new ListItem{ Value = "47" , Text = "Edelweiss Mutual Fund"},
new ListItem{ Value = "54" , Text = "Essel Mutual Fund"},
new ListItem{ Value = "40" , Text = "Fidelity Mutual Fund"},
new ListItem{ Value = "51" , Text = "Fortis Mutual Fund"},
new ListItem{ Value = "27" , Text = "Franklin Templeton Mutual Fund"},
new ListItem{ Value = "8" , Text = "GIC Mutual Fund"},
new ListItem{ Value = "49" , Text = "Goldman Sachs Mutual Fund"},
new ListItem{ Value = "9" , Text = "HDFC Mutual Fund"},
new ListItem{ Value = "37" , Text = "HSBC Mutual Fund"},
new ListItem{ Value = "20" , Text = "ICICI Prudential Mutual Fund"},
new ListItem{ Value = "57" , Text = "IDBI Mutual Fund"},
new ListItem{ Value = "48" , Text = "IDFC Mutual Fund"},
new ListItem{ Value = "68" , Text = "IIFCL Mutual Fund (IDF)"},
new ListItem{ Value = "62" , Text = "IIFL Mutual Fund"},
////new ListItem{ Value = "11" , Text = "IL & amp; F S Mutual Fund"},
//new ListItem{ Value = "11" , Text = "IL&F S Mutual Fund"},
////new ListItem{ Value = "65" , Text = "IL & amp; FS Mutual Fund(IDF)"},
//new ListItem{ Value = "65" , Text = "IL&FS Mutual Fund(IDF)"},
new ListItem{ Value = "63" , Text = "Indiabulls Mutual Fund"},
new ListItem{ Value = "14" , Text = "ING Mutual Fund"},
new ListItem{ Value = "42" , Text = "Invesco Mutual Fund"},
new ListItem{ Value = "70" , Text = "ITI Mutual Fund"},
new ListItem{ Value = "16" , Text = "JM Financial Mutual Fund"},
new ListItem{ Value = "43" , Text = "JPMorgan Mutual Fund"},
new ListItem{ Value = "17" , Text = "Kotak Mahindra Mutual Fund"},
//new ListItem{ Value = "56" , Text = "L & amp; T Mutual Fund"},
new ListItem{ Value = "56" , Text = "L&T Mutual Fund"},
new ListItem{ Value = "18" , Text = "LIC Mutual Fund"},
new ListItem{ Value = "69" , Text = "Mahindra Manulife Mutual Fund"},
new ListItem{ Value = "45" , Text = "Mirae Asset Mutual Fund"},
new ListItem{ Value = "19" , Text = "Morgan Stanley Mutual Fund"},
new ListItem{ Value = "55" , Text = "Motilal Oswal Mutual Fund"},
new ListItem{ Value = "21" , Text = "Nippon India Mutual Fund"},
new ListItem{ Value = "58" , Text = "PGIM India Mutual Fund"},
new ListItem{ Value = "44" , Text = "PineBridge Mutual Fund"},
new ListItem{ Value = "34" , Text = "PNB Mutual Fund"},
new ListItem{ Value = "64" , Text = "PPFAS Mutual Fund"},
new ListItem{ Value = "10" , Text = "Principal Mutual Fund"},
new ListItem{ Value = "13" , Text = "quant Mutual Fund"},
new ListItem{ Value = "41" , Text = "Quantum Mutual Fund"},
new ListItem{ Value = "35" , Text = "Sahara Mutual Fund"},
new ListItem{ Value = "22" , Text = "SBI Mutual Fund"},
new ListItem{ Value = "52" , Text = "Shinsei Mutual Fund"},
new ListItem{ Value = "67" , Text = "Shriram Mutual Fund"},
new ListItem{ Value = "66" , Text = "SREI Mutual Fund (IDF)"},
new ListItem{ Value = "2" , Text = "Standard Chartered Mutual Fund"},
//new ListItem{ Value = "24" , Text = "SUN F&C Mutual Fund"},
new ListItem{ Value = "33" , Text = "Sundaram Mutual Fund"},
new ListItem{ Value = "25" , Text = "Tata Mutual Fund"},
new ListItem{ Value = "26" , Text = "Taurus Mutual Fund"},
new ListItem{ Value = "72" , Text = "Trust Mutual Fund"},
new ListItem{ Value = "61" , Text = "Union Mutual Fund"},
new ListItem{ Value = "28" , Text = "UTI Mutual Fund"},
new ListItem{ Value = "71" , Text = "YES Mutual Fund"},
new ListItem{ Value = "29" , Text = "Zurich India Mutual Fund"},
};
static string mfMasterFile = "MF_MASTER_CURRENT_NAV.txt";
//Following URL will fetch latest NAV for ALL MF
//webservice_url = "https://www.amfiindia.com/spages/NAVAll.txt?t=27092020012930"; //string.Format(mfCurrentNAVALL_URL);
//webservice_url = "https://www.amfiindia.com/spages/NAVAll.txt?t=27-09-2020"; //string.Format(mfCurrentNAVALL_URL);
//static string urlMF_MASTER_CURRENT = "https://www.amfiindia.com/spages/NAVAll.txt?t={0}";
static string urlMF_MASTER_CURRENT = "https://www.amfiindia.com/spages/NAVAll.txt";
//Use following URL to get specific date NAV for ALL MF. The format is same as urlMF_MASTER_CURRENT
//Output is:
//Scheme Code;Scheme Name;ISIN Div Payout/ISIN Growth;ISIN Div Reinvestment;Net Asset Value;Repurchase Price;Sale Price;Date
//http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?frmdt=01-Jan-2020
static string urlMF_NAV_FOR_DATE = "http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?frmdt={0}";
//Use following URL to get NAV history between from dt & to dt for specific MF code.
//Output is :
//Scheme Code;Scheme Name;ISIN Div Payout/ISIN Growth;ISIN Div Reinvestment;Net Asset Value;Repurchase Price;Sale Price;Date
//http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf=27&frmdt=27-Sep-2020&todt=05-Oct-2020
static string urlMF_NAV_HISTORY_FROM_TO = "http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf={0}&frmdt={1}&todt={2}";
static string urlMF_NAV_HISTORY_FROM = "http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf={0}&frmdt={1}";
//
//http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?frmdt=27-Sep-2020&todt=05-Oct-2020&mf=3&scm=119551
static string urlSCHEME_NAV_HISTORY_FROM_TO = "http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf={0}&frmdt={1}&todt={2}";
static string urlSCHEME_NAV_HISTORY_FROM = "http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf={0}&frmdt={1}";
//public static string readMFList()
//{
// //HTMLAgilityPack
// WebClient webClient = new WebClient();
// string html = webClient.DownloadString("http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx");
// HTMLDocument
//}
public static bool isFileWriteDateEqualsToday(string filename)
{
bool breturn = false;
try
{
if (File.Exists(filename))
{
if (filename.Contains("MF_MASTER_CURRENT_NAV.txt"))
{
DateTime dtFileWriteTime = File.GetLastWriteTime(filename);
DateTime dtToday = DateTime.Today;
if (dtFileWriteTime.Date == DateTime.Today)
{
breturn = true;
}
else
{
breturn = false;
}
}
else
{
breturn = true;
}
}
else
{
breturn = false;
}
}
catch (Exception ex)
{
breturn = false;
}
return breturn;
}
//Function to get ALL MF;s and latest available NAV
//"https://www.amfiindia.com/spages/NAVAll.txt"
//It will check if file was fetched today, if yes then it will not fetch it from AMFIINDIA.COM, but return DataTable from the existing file
//Else it will fetch file, save it locally and then return DataTable
//Format of the MF Master file & return table is as below
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
static public DataTable loadMFMasterWithCurrentNAV(string folderPath)
{
DataTable resultDataTable = null;
StringBuilder returnString = new StringBuilder("MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE");
string webservice_url;
Uri url;
WebResponse wr;
Stream receiveStream = null;
StreamReader reader = null;
string[] fields;
string record;
DataRow r;
string mfType = "", tmp1 = "";
string mfCompName = "";
double nav;
StringBuilder filename = new StringBuilder(folderPath + mfMasterFile); // "MF_MASTER_CURRENT_NAV.txt");
try
{
if (isFileWriteDateEqualsToday(filename.ToString()) == false)
{
//webservice_url = string.Format(mfCurrentNAVALL_URL);
//using (var webClient = new System.Net.WebClient())
//{
// record = webClient.DownloadString("https://www.amfiindia.com/spages/NAVAll.txt?t=27092020012930");
// webClient.Dispose();
//}
//https://www.amfiindia.com/spages/NAVAll.txt;
//webservice_url = string.Format(urlMF_MASTER_CURRENT, DateTime.Today.Date.ToShortDateString());
webservice_url = urlMF_MASTER_CURRENT;
url = new Uri(webservice_url);
var webRequest = WebRequest.Create(url);
webRequest.Method = WebRequestMethods.File.DownloadFile;
//webRequest.ContentType = "application/json";
wr = webRequest.GetResponseAsync().Result;
receiveStream = wr.GetResponseStream();
reader = new StreamReader(receiveStream);
if (reader != null)
{
//get first line where fields are mentioned
record = reader.ReadLine();
fields = record.Split(';');
resultDataTable = new DataTable();
resultDataTable.Columns.Add("MF_TYPE", typeof(string));
resultDataTable.Columns.Add("MF_COMP_NAME", typeof(string));
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
resultDataTable.Columns.Add("SCHEME_CODE", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Payout_ISIN_Growth", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Reinvestment", typeof(string));
resultDataTable.Columns.Add("SCHEME_NAME", typeof(string));
resultDataTable.Columns.Add("NET_ASSET_VALUE", typeof(decimal));
resultDataTable.Columns.Add("DATE", typeof(DateTime));
//Now we have table with following fields
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
//Now read each line and fill the data in table. We have to skip lines which do not have ';' and hence fields will be empty
while (!reader.EndOfStream)
{
record = reader.ReadLine();
record = record.Trim();
if (record.Length == 0)
{
continue;
}
else if (record.Contains(";") == false) //case of either MF type or MF House
{
tmp1 = record;
//lets read next few lines till we find a line with either ; or no ;
//if we find a line with ; then it's continuation of same MF Type but
while (!reader.EndOfStream)
{
record = reader.ReadLine();
record = record.Trim();
if (record.Length == 0)
{
continue;
}
else if (record.Contains(";") == false)
{
//we found a MF company name
mfType = tmp1;
mfCompName = record;
tmp1 = record;
}
else if (record.Contains(";") == true)
{
//we continue with same MF type
mfCompName = tmp1;
break;
}
}
}
fields = record.Split(';');
//Check if we have values for - Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
if (fields.Length == 6)
{
try
{
nav = System.Convert.ToDouble(fields[4]);
}
catch (Exception)
{
nav = 0.00;
}
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
returnString.AppendLine();
returnString.Append(mfType);
returnString.Append(";");
returnString.Append(mfCompName);
returnString.Append(";");
returnString.Append(fields[0]);
returnString.Append(";");
returnString.Append(fields[1]);
returnString.Append(";");
returnString.Append(fields[2]);
returnString.Append(";");
returnString.Append(fields[3]);
returnString.Append(";");
returnString.Append(string.Format("{0:0.0000}", nav));
//returnString.Append(fields[4]);
returnString.Append(";");
returnString.Append(System.Convert.ToDateTime(fields[5]).ToString("yyyy-MM-dd"));
resultDataTable.Rows.Add(new object[] {
mfType,
mfCompName,
fields[0],
fields[1],
fields[2],
fields[3],
System.Convert.ToDouble(string.Format("{0:0.0000}", nav)),
//fields[4],
System.Convert.ToDateTime(fields[5]).ToString("yyyy-MM-dd")
});
}
}
File.WriteAllText(filename.ToString(), returnString.ToString());
}
}
else
{
if (File.Exists(filename.ToString()))
reader = new StreamReader(filename.ToString());
if (reader != null)
{
record = reader.ReadLine();
fields = record.Split(';');
resultDataTable = new DataTable();
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
//foreach (string fieldname in fields)
//{
// resultDataTable.Columns.Add(fieldname, typeof(string));
//}
resultDataTable.Columns.Add("MF_TYPE", typeof(string));
resultDataTable.Columns.Add("MF_COMP_NAME", typeof(string));
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
resultDataTable.Columns.Add("SCHEME_CODE", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Payout_ISIN_Growth", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Reinvestment", typeof(string));
resultDataTable.Columns.Add("SCHEME_NAME", typeof(string));
resultDataTable.Columns.Add("NET_ASSET_VALUE", typeof(decimal));
resultDataTable.Columns.Add("DATE", typeof(DateTime));
while (!reader.EndOfStream)
{
record = reader.ReadLine();
fields = record.Split(';');
//r = resultDataTable.NewRow();
//r.ItemArray = fields;
//resultDataTable.Rows.Add(r);
//Fields in file
//0MF_TYPE;1MF_COMP_NAME;2SCHEME_CODE;3ISIN_Div_Payout_ISIN_Growth;4ISIN_Div_Reinvestment;5SCHEME_NAME;6NET_ASSET_VALUE;7DATE
resultDataTable.Rows.Add(new object[] {
fields[0],
fields[1],
fields[2],
fields[3],
fields[4],
fields[5],
System.Convert.ToDouble(string.Format("{0:0.0000}", fields[6])),
//fields[6],
System.Convert.ToDateTime(fields[7]).ToString("yyyy-MM-dd")
});
}
}
}
if (reader != null)
reader.Close();
if (receiveStream != null)
receiveStream.Close();
}
catch (Exception ex)
{
if (resultDataTable != null)
{
resultDataTable.Clear();
resultDataTable.Dispose();
}
resultDataTable = null;
}
return resultDataTable;
}
//This method will fetch MF data for specific date with following URL.
//http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?frmdt=01-Jan-2020
//The output is in different format than NAVALL for the current NAV
//The out put of this URL is as below
//Scheme Code;Scheme Name;ISIN Div Payout/ISIN Growth;ISIN Div Reinvestment;Net Asset Value;Repurchase Price;Sale Price;Date
//output of the method is table in following format
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
static public DataTable getMFNAVForDate(string folderPath, string fetchDate)
{
DataTable resultDataTable = null;
StringBuilder returnString = new StringBuilder("MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE");
string webservice_url;
Uri url;
WebResponse wr;
Stream receiveStream = null;
StreamReader reader = null;
string[] fields;
string record;
DataRow r;
string mfType = "", tmp1 = "";
string mfCompName = "";
string dateFetch = System.Convert.ToDateTime(fetchDate).ToString("yyyy-MM-dd");
StringBuilder filename = new StringBuilder(folderPath + dateFetch + ".txt"); // "MF_MASTER_CURRENT_NAV.txt");
try
{
//if (isFileWriteDateEqualsToday(filename.ToString()) == false)
if (File.Exists(filename.ToString()) == false)
{
//webservice_url = string.Format(urlMF_MASTER_CURRENT, DateTime.Today.Date.ToShortDateString());
webservice_url = string.Format(urlMF_NAV_FOR_DATE, dateFetch);
url = new Uri(webservice_url);
var webRequest = WebRequest.Create(url);
webRequest.Method = WebRequestMethods.File.DownloadFile;
//webRequest.ContentType = "application/json";
wr = webRequest.GetResponseAsync().Result;
receiveStream = wr.GetResponseStream();
reader = new StreamReader(receiveStream);
if (reader != null)
{
//get first line where fields are mentioned
record = reader.ReadLine();
fields = record.Split(';');
resultDataTable = new DataTable();
resultDataTable.Columns.Add("MF_TYPE", typeof(string));
resultDataTable.Columns.Add("MF_COMP_NAME", typeof(string));
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
resultDataTable.Columns.Add("SCHEME_CODE", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Payout_ISIN_Growth", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Reinvestment", typeof(string));
resultDataTable.Columns.Add("SCHEME_NAME", typeof(string));
resultDataTable.Columns.Add("NET_ASSET_VALUE", typeof(decimal));
resultDataTable.Columns.Add("DATE", typeof(DateTime));
//Now we have table with following fields
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
//Now read each line and fill the data in table. We have to skip lines which do not have ';' and hence fields will be empty
while (!reader.EndOfStream)
{
record = reader.ReadLine();
record = record.Trim();
if (record.Length == 0)
{
continue;
}
else if (record.Contains(";") == false) //case of either MF type or MF House
{
tmp1 = record;
//lets read next few lines till we find a line with either ; or no ;
//if we find a line with ; then it's continuation of same MF Type but
while (!reader.EndOfStream)
{
record = reader.ReadLine();
record = record.Trim();
if (record.Length == 0)
{
continue;
}
else if (record.Contains(";") == false)
{
//we found a MF company name
mfType = tmp1;
mfCompName = record;
tmp1 = record;
}
else if (record.Contains(";") == true)
{
//we continue with same MF type
mfCompName = tmp1;
break;
}
}
}
fields = record.Split(';');
//Following fields are in a record for this URL
//Scheme Code;Scheme Name;ISIN Div Payout/ISIN Growth;ISIN Div Reinvestment;Net Asset Value;Repurchase Price;Sale Price;Date
//Check if we have values for - Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
if (fields.Length >= 6)
{
//Our table is in following format
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
returnString.AppendLine();
returnString.Append(mfType);
returnString.Append(";");
returnString.Append(mfCompName);
returnString.Append(";");
returnString.Append(fields[0]); //Scheme Code
returnString.Append(";");
returnString.Append(fields[2]); //ISIN_Div_Payout_ISIN_Growth
returnString.Append(";");
returnString.Append(fields[3]); //ISIN_Div_Reinvestment
returnString.Append(";");
returnString.Append(fields[1]); //SCHEME_NAME
returnString.Append(";");
returnString.Append(string.Format("{0:0.0000}", System.Convert.ToDouble(fields[4])));
//returnString.Append(fields[4]); //NET_ASSET_VALUE
returnString.Append(";");
returnString.Append(System.Convert.ToDateTime(fields[7]).ToString("yyyy-MM-dd")); //DATE
resultDataTable.Rows.Add(new object[] {
mfType,
mfCompName,
fields[0],
fields[2],
fields[3],
fields[1],
string.Format("{0:0.0000}", System.Convert.ToDouble(fields[4])),
//fields[4],
System.Convert.ToDateTime(fields[7]).ToString("yyyy-MM-dd")
});
}
}
File.WriteAllText(filename.ToString(), returnString.ToString());
}
}
else
{
//if (File.Exists(filename.ToString()))
reader = new StreamReader(filename.ToString());
if (reader != null)
{
record = reader.ReadLine();
fields = record.Split(';');
resultDataTable = new DataTable();
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
//foreach (string fieldname in fields)
//{
// resultDataTable.Columns.Add(fieldname, typeof(string));
//}
resultDataTable.Columns.Add("MF_TYPE", typeof(string));
resultDataTable.Columns.Add("MF_COMP_NAME", typeof(string));
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
resultDataTable.Columns.Add("SCHEME_CODE", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Payout_ISIN_Growth", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Reinvestment", typeof(string));
resultDataTable.Columns.Add("SCHEME_NAME", typeof(string));
resultDataTable.Columns.Add("NET_ASSET_VALUE", typeof(decimal));
resultDataTable.Columns.Add("DATE", typeof(DateTime));
while (!reader.EndOfStream)
{
record = reader.ReadLine();
fields = record.Split(';');
//r = resultDataTable.NewRow();
//r.ItemArray = fields;
//resultDataTable.Rows.Add(r);
//FIle is in following format
//0MF_TYPE;1MF_COMP_NAME;2SCHEME_CODE;3ISIN_Div_Payout_ISIN_Growth;4ISIN_Div_Reinvestment;5SCHEME_NAME;6NET_ASSET_VALUE;7DATE
resultDataTable.Rows.Add(new object[] {
fields[0],
fields[1],
fields[2],
fields[3],
fields[4],
fields[5],
string.Format("{0:0.0000}", System.Convert.ToDouble(fields[6])),
//fields[4],
System.Convert.ToDateTime(fields[7]).ToString("yyyy-MM-dd")
});
}
}
}
if (reader != null)
reader.Close();
if (receiveStream != null)
receiveStream.Close();
}
catch (Exception ex)
{
if (resultDataTable != null)
{
resultDataTable.Clear();
resultDataTable.Dispose();
}
resultDataTable = null;
}
return resultDataTable;
}
//This method will fetch MF NAV history data for specific MF Code between from date = fromDt & To date < to date
//http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf=27&frmdt=2020-09-01&todt=2020-09-04
//The output is in different format than NAVALL for the current NAV
//The out put of this URL is as below
//Scheme Code;Scheme Name;ISIN Div Payout/ISIN Growth;ISIN Div Reinvestment;Net Asset Value;Repurchase Price;Sale Price;Date
//output of the method is table in following format
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
static public DataTable getHistoryNAV(string folderPath, string mfCode, string fromdt, string todt = null)
{
DataTable resultDataTable = null;
StringBuilder returnString = new StringBuilder("MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE");
string webservice_url;
Uri url;
WebResponse wr;
Stream receiveStream = null;
StreamReader reader = null;
string[] fields;
string record;
DataRow r;
string mfType = "", tmp1 = "";
string mfCompName = "";
string dateFrom = System.Convert.ToDateTime(fromdt).ToString("yyyy-MM-dd");
string dateTo = null;
StringBuilder filename;
if (todt != null)
{
dateTo = System.Convert.ToDateTime(todt).ToString("yyyy-MM-dd");
filename = new StringBuilder(folderPath + mfCode + "_" + dateFrom + "_" + dateTo + ".txt");
webservice_url = string.Format(urlMF_NAV_HISTORY_FROM_TO, mfCode, dateFrom, dateTo);
}
else
{
filename = new StringBuilder(folderPath + mfCode + "_" + dateFrom + ".txt");
webservice_url = string.Format(urlMF_NAV_HISTORY_FROM, mfCode, dateFrom);
}
try
{
//if (isFileWriteDateEqualsToday(filename.ToString()) == false)
if (File.Exists(filename.ToString()) == false)
{
//webservice_url = string.Format(urlMF_NAV_HISTORY, mfCode, dateFrom, dateTo);
url = new Uri(webservice_url);
var webRequest = WebRequest.Create(url);
webRequest.Method = WebRequestMethods.File.DownloadFile;
//webRequest.ContentType = "application/json";
wr = webRequest.GetResponseAsync().Result;
receiveStream = wr.GetResponseStream();
reader = new StreamReader(receiveStream);
if (reader != null)
{
//get first line where fields are mentioned
record = reader.ReadLine();
if (record.Length <= 0)
{
throw new Exception("No records found.");
}
fields = record.Split(';');
resultDataTable = new DataTable();
resultDataTable.Columns.Add("MF_TYPE", typeof(string));
resultDataTable.Columns.Add("MF_COMP_NAME", typeof(string));
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
resultDataTable.Columns.Add("SCHEME_CODE", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Payout_ISIN_Growth", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Reinvestment", typeof(string));
resultDataTable.Columns.Add("SCHEME_NAME", typeof(string));
resultDataTable.Columns.Add("NET_ASSET_VALUE", typeof(decimal));
//resultDataTable.Columns.Add("DATE", typeof(string));
resultDataTable.Columns.Add("DATE", typeof(DateTime));
//Now we have table with following fields
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
//Now read each line and fill the data in table. We have to skip lines which do not have ';' and hence fields will be empty
while (!reader.EndOfStream)
{
record = reader.ReadLine();
record = record.Trim();
if (record.Length == 0)
{
continue;
}
else if (record.Contains(";") == false) //case of either MF type or MF House
{
tmp1 = record;
//lets read next few lines till we find a line with either ; or no ;
//if we find a line with ; then it's continuation of same MF Type but
while (!reader.EndOfStream)
{
record = reader.ReadLine();
record = record.Trim();
if (record.Length == 0)
{
continue;
}
else if (record.Contains(";") == false)
{
//we found a MF company name
mfType = tmp1;
mfCompName = record;
tmp1 = record;
}
else if (record.Contains(";") == true)
{
//we continue with same MF type
mfCompName = tmp1;
break;
}
}
}
fields = record.Split(';');
//Following fields are in a record for this URL
//Scheme Code;Scheme Name;ISIN Div Payout/ISIN Growth;ISIN Div Reinvestment;Net Asset Value;Repurchase Price;Sale Price;Date
//Check if we have values for - Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
if (fields.Length >= 6)
{
//Our table is in following format
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
returnString.AppendLine();
returnString.Append(mfType);
returnString.Append(";");
returnString.Append(mfCompName);
returnString.Append(";");
returnString.Append(fields[0]); //Scheme Code
returnString.Append(";");
returnString.Append(fields[2]); //ISIN_Div_Payout_ISIN_Growth
returnString.Append(";");
returnString.Append(fields[3]); //ISIN_Div_Reinvestment
returnString.Append(";");
returnString.Append(fields[1]); //SCHEME_NAME
returnString.Append(";");
returnString.Append(string.Format("{0:0.0000}", System.Convert.ToDouble(fields[4])));
//returnString.Append(fields[4]); //NET_ASSET_VALUE
returnString.Append(";");
returnString.Append(System.Convert.ToDateTime(fields[7]).ToString("yyyy-MM-dd")); //DATE
resultDataTable.Rows.Add(new object[] {
mfType,
mfCompName,
fields[0],
fields[2],
fields[3],
fields[1],
System.Convert.ToDouble(string.Format("{0:0.0000}", System.Convert.ToDouble(fields[4]))),
//fields[4],
System.Convert.ToDateTime(fields[7]).ToString("yyyy-MM-dd")
});
}
}
File.WriteAllText(filename.ToString(), returnString.ToString());
}
}
else
{
//if (File.Exists(filename.ToString()))
reader = new StreamReader(filename.ToString());
if (reader != null)
{
record = reader.ReadLine();
fields = record.Split(';');
resultDataTable = new DataTable();
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
//foreach (string fieldname in fields)
//{
// resultDataTable.Columns.Add(fieldname, typeof(string));
//}
resultDataTable.Columns.Add("MF_TYPE", typeof(string));
resultDataTable.Columns.Add("MF_COMP_NAME", typeof(string));
//Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
resultDataTable.Columns.Add("SCHEME_CODE", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Payout_ISIN_Growth", typeof(string));
resultDataTable.Columns.Add("ISIN_Div_Reinvestment", typeof(string));
resultDataTable.Columns.Add("SCHEME_NAME", typeof(string));
resultDataTable.Columns.Add("NET_ASSET_VALUE", typeof(decimal));
//resultDataTable.Columns.Add("DATE", typeof(string));
resultDataTable.Columns.Add("DATE", typeof(DateTime));
while (!reader.EndOfStream)
{
record = reader.ReadLine();
fields = record.Split(';');
//r = resultDataTable.NewRow();
//r.ItemArray = fields;
//resultDataTable.Rows.Add(r);
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
resultDataTable.Rows.Add(new object[] {
fields[0],
fields[1],
fields[2],
fields[3],
fields[4],
fields[5],
System.Convert.ToDouble(string.Format("{0:0.0000}", System.Convert.ToDouble(fields[6]))),
//fields[4],
System.Convert.ToDateTime(fields[7]).ToString("yyyy-MM-dd")
});
}
}
}
}
catch (Exception ex)
{
if (resultDataTable != null)
{
resultDataTable.Clear();
resultDataTable.Dispose();
}
resultDataTable = null;
}
if (reader != null)
reader.Close();
if (receiveStream != null)
receiveStream.Close();
return resultDataTable;
}
//Function that will search a string in SCHEME_NAME column and return all rows from MF Master table that matches search string
//Format of the MF Master file & return table is as below
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
static public DataTable searchMFMaster(string folderPath, string searchString = null, bool bExactMatch = false,
DataTable mfMasterTable = null, int retryDays = 0, string searchDate = null)
{
DataTable resultDataTable = null;
DataTable localMFMaster = null;
int retryCount = 0;
string localSearchDate;
try
{
if (mfMasterTable == null)
{
//mfMasterTable = loadMFMasterWithCurrentNAV(folderPath);
localMFMaster = loadMFMasterWithCurrentNAV(folderPath);
}
else
{
localMFMaster = mfMasterTable.Copy();
}
if ((searchString == null) || (searchString.Length == 0))
{
resultDataTable = mfMasterTable.Copy();
}
else
{
do
{
if (bExactMatch == false)
{
localMFMaster.DefaultView.RowFilter = "SCHEME_NAME like '%" + searchString + "%'";
}
else
{
localMFMaster.DefaultView.RowFilter = "SCHEME_NAME = '" + searchString + "'";
}
resultDataTable = localMFMaster.DefaultView.ToTable();
if ((resultDataTable != null) && (resultDataTable.Rows.Count > 0))
{
break;
}
if (retryDays == 0)
{
break;
}
if (searchDate == null)
{
break;
}
retryCount++;
localSearchDate = System.Convert.ToDateTime(searchDate).AddDays(retryCount).ToShortDateString();
localMFMaster = getMFNAVForDate(folderPath, localSearchDate);
} while ((retryCount <= retryDays) && (searchDate != null) && (System.Convert.ToDateTime(searchDate) <= DateTime.Today));
}
}
catch (Exception ex)
{
if (resultDataTable != null)
{
resultDataTable.Clear();
resultDataTable.Dispose();
}
resultDataTable = null;
}
return resultDataTable;
}
//Function that will search a string in SCHEME_NAME column and return all rows from MF History table that matches search string
//Format of the MF Master file & return table is as below
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
static public DataTable searchMFHistoryForSchemeName(string folderPath, string mfCode, string fromDate,
string searchString = null, bool bExactMatch = false,
DataTable mfHistoryTable = null, string toDate = null)
{
DataTable resultDataTable = null;
DataTable localMFHistoryTable = null;
string dateFrom = System.Convert.ToDateTime(fromDate).ToString("yyyy-MM-dd");
string dateTo = null;
try
{
if (mfHistoryTable == null)
{
//mfMasterTable = loadMFMasterWithCurrentNAV(folderPath);
if (toDate != null)
{
dateTo = System.Convert.ToDateTime(toDate).ToString("yyyy-MM-dd");
}
localMFHistoryTable = getHistoryNAV(folderPath, mfCode, dateFrom, todt: dateTo);
}
else
{
localMFHistoryTable = mfHistoryTable.Copy();
}
if ((searchString == null) || (searchString.Length == 0))
{
resultDataTable = mfHistoryTable.Copy();
}
else
{
if (bExactMatch == false)
{
localMFHistoryTable.DefaultView.RowFilter = "SCHEME_NAME like '%" + searchString + "%'";
}
else
{
localMFHistoryTable.DefaultView.RowFilter = "SCHEME_NAME = '" + searchString + "'";
}
if (localMFHistoryTable.DefaultView.Count > 0)
{
resultDataTable = localMFHistoryTable.DefaultView.ToTable();
}
}
}
catch (Exception ex)
{
if (resultDataTable != null)
{
resultDataTable.Clear();
resultDataTable.Dispose();
}
resultDataTable = null;
}
return resultDataTable;
}
//Function that will search a string in MF_COMP_NAME column and return all rows from MF Master table that matches search string
//Format of the MF Master file & return table is as below
//MF_TYPE;MF_COMP_NAME;SCHEME_CODE;ISIN_Div_Payout_ISIN_Growth;ISIN_Div_Reinvestment;SCHEME_NAME;NET_ASSET_VALUE;DATE
static public DataTable getALLMFforFundHouse(string folderPath, string searchString = null, bool bExactMatch = false, DataTable mfMasterTable = null)
{
DataTable resultDataTable = null;
try
{
if (mfMasterTable == null)
{
mfMasterTable = loadMFMasterWithCurrentNAV(folderPath);
}
if ((searchString == null) || (searchString.Length == 0))
{
resultDataTable = mfMasterTable.Copy();
}
else
{
if (bExactMatch == false)
{
mfMasterTable.DefaultView.RowFilter = "MF_COMP_NAME like '%" + searchString + "%'";