Tool to download months old NSE BhavCopy in one go

#11
Hi buddy,
I have got Amibroker now. Kindly help me in managing it.
How to get historical data and how to update data ?
Eagerly awaiting your friendly reply. ;)
Sorry for the late reply. was kindda busy..

I uploaded data for the metastock. You can use same data for metastock also. Search for my old posts. its uploaded at my esnips account.
For updating data you will have to regularly download and convert the data and import into metastock or amibroker.
 
#12
When I refer NSE bhavcopy there is a mix of EQ and BE series how do you people handle this.For example several entries of same date's data exist in NSE Bhavcopy what should I do to solve this problem.
I was facing the same stupid problem with my EODCoverter as well.. and it screwed up my data big time. Anyways I created a new utility to convert the NSE Bhav Copy to Metastock format text file.
Im attaching the code for the same here. You can modify it as you wish.
Dont have time to write a beautiful application so I apologize for that.

You can use this program in conjuction with my earlier posted BhavCopyDownloder.

Code:
static void Main(string[] args)
        {
            StreamReader reader = null;
            StreamWriter writer = null;
            string fileName = @"C:\Program Files\EODdlc\DATA\NSE\EQUITIES\20070924.csv";
            try
            {
                reader = new StreamReader(fileName);
                writer = new StreamWriter(fileName + ".txt");
                
                string str = reader.ReadLine();
                Console.WriteLine(str);
                writer.WriteLine("<ticker>,<Date>,<open>,<high>,<low>,<close>,<vol>,");
                int i = 0;
                StringBuilder strBuilder = new StringBuilder();
                while (!reader.EndOfStream)
                {
                    str = reader.ReadLine();
                    if (!str.Contains(",EQ,"))
                        Console.WriteLine(str);
                    else
                    {
                        strBuilder.Remove(0, strBuilder.Length);
                        ++i;
                        string[] tempStr = str.Split(new char[] { ',' });
                        DateTime dateTime = DateTime.Parse(tempStr[10]);

                        strBuilder.Append(tempStr[0] + ",");
                        string dateStr = dateTime.Year
                            + (dateTime.Month < 10 ?
                            "0" + dateTime.Month.ToString() : dateTime.Month.ToString()) +
                            (dateTime.Day < 10 ?
                            "0" + dateTime.Day.ToString() : dateTime.Day.ToString());
                        strBuilder.Append(dateStr + ",");
                        strBuilder.Append(tempStr[2] + ",");
                        strBuilder.Append(tempStr[3] + ",");
                        strBuilder.Append(tempStr[4] + ",");
                        strBuilder.Append(tempStr[5] + ",");
                        strBuilder.Append(tempStr[8] + ",");
                        writer.WriteLine(strBuilder.ToString());
                    }
                }
                Console.WriteLine("Total " + i + " Scrips written to the file");
                writer.Flush();
                Console.ReadLine();
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (writer != null)
                    writer.Close();
            }
        }
 

pokrate

Active Member
#14
Awesome, fantastic, brilliant.
I will definitely use it.
Please upload NIFTY INDEX historical data in METASTOCK format.
 
#15
This is the Code and executable for downloading daily NSE Bhavcopy data in Metastock format.

Code:
static void Main(string[] args)
        {
            StreamReader reader = null;
            StreamWriter writer = null;
            //string fileName = @"C:\Program Files\EODdlc\DATA\NSE\EQUITIES\20070924.csv";
            string fileName = DownloadBhavCopy();
            if (String.IsNullOrEmpty(fileName))
            {
                Console.WriteLine("Couldn't download BhavCopy");
                return;
            }
            try
            {
                Console.WriteLine("Converting " + fileName);
                reader = new StreamReader(fileName);
                writer = new StreamWriter(fileName + ".txt");

                string str = reader.ReadLine();
                Console.WriteLine(str);
                writer.WriteLine("<ticker>,<Date>,<open>,<high>,<low>,<close>,<vol>,");
                int i = 0;
                StringBuilder strBuilder = new StringBuilder();
                while (!reader.EndOfStream)
                {
                    str = reader.ReadLine();
                    if (!str.Contains(",EQ,"))
                        Console.WriteLine(str);
                    else
                    {
                        strBuilder.Remove(0, strBuilder.Length);
                        ++i;
                        string[] tempStr = str.Split(new char[] { ',' });
                        DateTime dateTime = DateTime.Parse(tempStr[10]);

                        strBuilder.Append(tempStr[0] + ",");
                        string dateStr = dateTime.Year
                            + (dateTime.Month < 10 ?
                            "0" + dateTime.Month.ToString() : dateTime.Month.ToString()) +
                            (dateTime.Day < 10 ?
                            "0" + dateTime.Day.ToString() : dateTime.Day.ToString());
                        strBuilder.Append(dateStr + ",");
                        strBuilder.Append(tempStr[2] + ",");
                        strBuilder.Append(tempStr[3] + ",");
                        strBuilder.Append(tempStr[4] + ",");
                        strBuilder.Append(tempStr[5] + ",");
                        strBuilder.Append(tempStr[8] + ",");
                        writer.WriteLine(strBuilder.ToString());
                    }
                }
                Console.WriteLine("Total " + i + " Scrips written to the file");
                writer.Flush();
                Console.ReadLine();
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (writer != null)
                    writer.Close();
            }
        }
        static string DownloadBhavCopy()
        {
            WebClient client = new WebClient();
            string dlFile = String.Empty;
            FileStream writer;
            try
            {
                string month = monthList[ DateTime.Today.Month - 1];
                string day = (DateTime.Today.Day <= 9) ? "0" + DateTime.Today.Day.ToString() :
                    DateTime.Today.Day.ToString(); ;
                string year = DateTime.Today.Year.ToString();
                Uri uri = new Uri(@"http://www.nse-india.com/content/historical/EQUITIES/2007/");
                Uri tempUri = new Uri(uri, month + "/cm" + day + month + year + "bhav.csv");
                Debug.WriteLine("Downloading " + tempUri.ToString());
                Console.WriteLine("Downloading " + tempUri.ToString());
                byte[] data = client.DownloadData(tempUri);
                dlFile = Path.Combine(Path.GetTempPath()/*@"C:\Program Files\EODdlc\DATA\NSE\EQUITIES"*/, "cm" + day + month + year + "bhav.csv");
                using (writer = File.Create(dlFile))
                {
                    try
                    {
                        writer.Write(data, 0, data.Length);
                    }
                    finally
                    {
                        if (writer != null)
                            writer.Close();
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Message.Contains("404"))
                    Debug.WriteLine("FileNotFound");
                dlFile = string.Empty;
            }
            return dlFile;
        }
    }
@PokRate
Check the following link. I guess it contains NSE Index till 21st August. Look for the ^NSEI or S&N Nifty index

http://www.esnips.com/doc/f08d537c-48f7-4581-b043-8b4092cac547/NSE_MetaStock
 

Similar threads