Ejemplo n.º 1
0
    def __init__(self,
                 symbol,
                 start_date,
                 end_date=datetime.date.today().isoformat()):
        super(YahooQuote, self).__init__()
        self.symbol = symbol.upper()
        start_year, start_month, start_day = start_date.split('-')
        start_month = str(int(start_month) - 1)  #expected by yahoo
        end_year, end_month, end_day = end_date.split('-')
        end_month = str(int(end_month) - 1)
        url_string = "http://ichart.finance.yahoo.com/table.csv?s={0}".format(
            symbol)
        url_string += "&a={0}&b={1}&c={2}".format(start_month, start_day,
                                                  start_year)
        url_string += "&d={0}&e={1}&f={2}".format(end_month, end_day, end_year)

        csv = urllib.urlopen(url_string).readlines()
        csv.reverse()
        for bar in xrange(0, len(csv) - 1):
            ds, open_, high, low, close, volume, adjc = csv[bar].rstrip(
            ).split(',')
            open_, high, low, close, adjc = [
                float(x) for x in [open_, high, low, close, adjc]
            ]
            if close != adjc:
                factor = adjc / close
                open_, high, low, close = [
                    x * factor for x in [open_, high, low, close]
                ]
            dt = datetime.datetime.strptime(ds, '%Y-%m-%d')
            self.append(dt, open_, high, low, close, volume)
        self.write_csv("./stocks/{0}.csv".format(symbol))
Ejemplo n.º 2
0
 def __init__(self,symbol,start_date,end_date=datetime.date.today().isoformat()):
   super(GoogleQuote,self).__init__()
   self.symbol = symbol.upper()
   start = start_date
   end = end_date
   #start = datetime.date(int(start_date[0:4]),int(start_date[5:7]),int(start_date[8:10]))
   #end = datetime.date(int(end_date[0:4]),int(end_date[5:7]),int(end_date[8:10]))
   url_string = "http://www.google.com/finance/historical?q={0}".format(self.symbol)
   url_string += "&startdate={0}&enddate={1}&output=csv".format(
                     start.strftime('%b %d, %Y'),end.strftime('%b %d, %Y'))
   print url_string
   csv = urllib.urlopen(url_string).readlines()
   csv.reverse()
   for bar in xrange(0,len(csv)-1):
     ds,open_,high,low,close,volume = csv[bar].rstrip().split(',')
     try:
       open_,high,low,close = [float(x) for x in [open_,high,low,close]]
     except ValueError:
       continue
     dt = datetime.datetime.strptime(ds,'%d-%b-%y')
     self.append(dt,open_,high,low,close,volume)
Ejemplo n.º 3
0
    def __init__(self,symbol,start_date,end_date=datetime.date.today().isoformat()):
        super(YahooQuote,self).__init__()
        self.symbol = symbol.upper()
        start_year,start_month,start_day = start_date.split('-')
        start_month = str(int(start_month)-1) #expected by yahoo
        end_year,end_month,end_day = end_date.split('-')
        end_month = str(int(end_month)-1)
        url_string = "http://ichart.finance.yahoo.com/table.csv?s={0}".format(symbol)
        url_string += "&a={0}&b={1}&c={2}".format(start_month,start_day,start_year)
        url_string += "&d={0}&e={1}&f={2}".format(end_month,end_day,end_year)

        csv = urllib.urlopen(url_string).readlines()
        csv.reverse()
        for bar in xrange(0,len(csv)-1):
            ds,open_,high,low,close,volume,adjc = csv[bar].rstrip().split(',')
            open_,high,low,close,adjc = [float(x) for x in [open_,high,low,close,adjc]]
            if close != adjc:
                factor = adjc/close
                open_,high,low,close = [x*factor for x in [open_,high,low,close]]
            dt = datetime.datetime.strptime(ds,'%Y-%m-%d')
            self.append(dt,open_,high,low,close,volume)
        self.write_csv("./stocks/{0}.csv".format(symbol))
Ejemplo n.º 4
0
def get_hist_data(symbol,
                  start_date,
                  end_date=datetime.date.today().isoformat()):
    start = datetime.date(int(start_date[0:4]), int(start_date[5:7]),
                          int(start_date[8:10]))
    end = datetime.date(int(end_date[0:4]), int(end_date[5:7]),
                        int(end_date[8:10]))
    url = "http://www.google.com/finance/historical?q={0}".format(
        symbol.upper())
    url += "&startdate={0}&enddate={1}&output=csv".format(
        start.strftime('%b %d, %Y'), end.strftime('%b %d, %Y'))
    csv = urllib.urlopen(url).readlines()
    csv.reverse()

    filepath = "./input/{0}.csv".format(symbol)
    with open(filepath, 'wb') as csvfile:
        for bar in xrange(0, len(csv) - 1):
            ds, open_, high, low, close, volume = csv[bar].rstrip().split(',')
            # open_,high,low,close = [float(x) for x in [open_,high,low,close]]
            dt = datetime.datetime.strptime(ds, '%d-%b-%y')
            line = [close]
            csvfile.write(",".join(line))
            csvfile.write('\n')