Tuesday 22 May 2012

Real Time Data From Google Finanace


We can fetch real time intraday data from google(1min) finanace.
Following is the procedure for geeting the data.
Following is the Url for getting the realtime backfill data which most of the trading softwares are using as a source for realtime datafeed
q= stock symbol on Google finance
x= exchange symbol
i= interval (in secs)
p= no of period(here 5d denotes 5 days of data)
      year  -- > 5Y
      month --> 5M
     Minute --> 5m

f= parameters (day, close, open, high,low& Volume)
df= difference (cpct is may be in % change )
auto =1,
ts = time start… if you cut the last 4 digits…the rest gives the start day in seconds
you can choose your own set of value depending upon your need.
The following link gives the 1 min Realtime data for Nifty
Using the above procdure we can make any formats(1min,5min, 15min,hourly) of real time data and can be use as a backfill
to feed charting softwares like Amibroker, Metastock, Ninja Trader etc
Following is the C# code for make the above URL.
private const string BASE_URL = "http://www.google.com/finance/getprices";
public enum Interval { OneMin = 60, FiveMin = 300, OneDay = 3600 }
/// <summary>
        /// Returns a URI to invoke the getPrices method.
        /// </summary>
        public string getGetPricesUri(string StockExchange,string Symbol,Interval interval, string period)
        {
            if (String.IsNullOrEmpty(period))
                throw new ArgumentException("No 'period'.");
            string formatURI = BASE_URL + "?q={0}{1}&i={2}&p={3}&f=d,c,h,l,o,v";
            string exchangeString = string.Empty;
            if (!String.IsNullOrEmpty(StockExchange))
            {
                exchangeString = "&x=" + StockExchange;
            }
            int _interval = 3600;
            switch (interval)
            {
                case Interval.OneDay:
                    _interval = 3600;
                    break;
                case Interval.OneMin:
                    _interval = 60;
                    break;
                case Interval.FiveMin:
                    _interval = 300;
                    break;
            }
            return String.Format(formatURI, Symbol, exchangeString, _interval, period);
        }
        /// <summary>
        /// Method invoked by getGetPricesUriForRecentData().
        /// </summary>
        public string getPeriod(DateTime startDate, DateTime endDate)
        {
            if (endDate.Date < startDate.Date)
            {
                throw new ArgumentException("The ending date can't be lower than the starting date.");
            }
            if (endDate.Year > startDate.Year)
            { //More than a year.
                return (endDate.Year - startDate.Year + 1) + "Y";
            }
            TimeSpan span = endDate - startDate;
            if (span.Days > 50)
            { //Return months.
                return ((span.Days % 25) + 1) + "M";
            }
            else if(span.TotalHours > 20)
            { //Return days.
                return (span.Days + 1) + "d"; //+1 because, depending on the hour, no data might be retrieved (1d is for today).
            }
            else
            { //Return days.
                return (span.Minutes + 1) + "m"; //+1 because, depending on the hour, no data might be retrieved (1d is for today).
            }
        }
// call the function
getGetPricesUri("NSE","NSEI", Interval.OneMin,getPeriod(DateTime.Now.AddDays(-15), DateTime.Now));

6 comments:

Kartik Shah said...

Can you please tell me, The step by step procedure to have this code in my Amibroker and getting free realtime data from Google. You can also mail me at kartiksmail@gmail.com

Regards,
Kartik Shah

eeasy baby said...

sir,

please help me to get the real timdata from google finance for amibroker and metastock

thank you
shiva=========
datashiva@gmail.com

Unknown said...

what is the interval value we need to give to take Weekly and Montly data .in your case daily is 3600 likewise monthly and Weekly ? and reply back to me

arumugam said...






Thanks for sharing this list. Very grateful. I work at mobile app testing company where i think it will be definitely useful.

NSE ACCOUNT

user3srsoftwaresolurtions said...
This comment has been removed by the author.
Acumentrade said...

Useful