コード例 #1
0
def parse_yahoo_historical(fh, asobject=False, adjusted=True):
    """
    Parse the historical data in file handle fh from yahoo finance and return
    results as a list of

    d, open, close, high, low, volume

    where d is a floating poing representation of date, as returned by date2num

    if adjust=True, use adjusted prices
    """
    results = []

    lines = fh.readlines()

    datefmt = None

    for line in lines[1:]:

        vals = line.split(',')

        if len(vals) != 7: continue
        datestr = vals[0]
        if datefmt is None:
            try:
                datefmt = '%Y-%m-%d'
                dt = datetime.date(*time.strptime(datestr, datefmt)[:3])
            except ValueError:
                datefmt = '%d-%b-%y'  # Old Yahoo--cached file?
        dt = datetime.date(*time.strptime(datestr, datefmt)[:3])
        d = date2num(dt)
        open, high, low, close = [float(val) for val in vals[1:5]]
        volume = int(vals[5])
        if adjusted:
            aclose = float(vals[6])
            m = aclose / close
            open *= m
            high *= m
            low *= m
            close = aclose

        results.append((d, open, close, high, low, volume))
    results.reverse()
    if asobject:
        if len(results) == 0: return None
        else:
            date, open, close, high, low, volume = map(npy.asarray,
                                                       zip(*results))
        return Bunch(date=date,
                     open=open,
                     close=close,
                     high=high,
                     low=low,
                     volume=volume)
    else:

        return results
コード例 #2
0
def parse_yahoo_historical(fh, asobject=False, adjusted=True):
    """
    Parse the historical data in file handle fh from yahoo finance and return
    results as a list of

    d, open, close, high, low, volume

    where d is a floating poing representation of date, as returned by date2num

    if adjust=True, use adjusted prices
    """
    results = []

    lines = fh.readlines()

    datefmt = None

    for line in lines[1:]:

        vals = line.split(",")

        if len(vals) != 7:
            continue
        datestr = vals[0]
        if datefmt is None:
            try:
                datefmt = "%Y-%m-%d"
                dt = datetime.date(*time.strptime(datestr, datefmt)[:3])
            except ValueError:
                datefmt = "%d-%b-%y"  # Old Yahoo--cached file?
        dt = datetime.date(*time.strptime(datestr, datefmt)[:3])
        d = date2num(dt)
        open, high, low, close = [float(val) for val in vals[1:5]]
        volume = int(vals[5])
        if adjusted:
            aclose = float(vals[6])
            m = aclose / close
            open *= m
            high *= m
            low *= m
            close = aclose

        results.append((d, open, close, high, low, volume))
    results.reverse()
    if asobject:
        if len(results) == 0:
            return None
        else:
            date, open, close, high, low, volume = map(np.asarray, zip(*results))
        return Bunch(date=date, open=open, close=close, high=high, low=low, volume=volume)
    else:

        return results
コード例 #3
0
ファイル: finance.py プロジェクト: pv/matplotlib-cvs
def parse_yahoo_historical(fh, asobject=False, adjusted=True):
    """
    Parse the historical data in file handle fh from yahoo finance and return
    results as a list of

    d, open, close, high, low, volume
    
    where d is a floating poing representation of date, as returned by date2num

    if adjust=True, use adjusted prices
    """
    results = []

    lines = fh.readlines()
    for line in lines[1:]:

        vals = line.split(',')
        if len(vals)!=7: continue
        datestr = vals[0]
        dt = datetime.date(*time.strptime(datestr, '%d-%b-%y')[:3])
        d = date2num(dt)
        open, high, low, close =  [float(val) for val in vals[1:5]]
        volume = int(vals[5])
        if adjusted:
            aclose = float(vals[6])
            m = aclose/close
            open *= m
            high *= m
            low *= m
            close = aclose

        results.append((d, open, close, high, low, volume))
    results.reverse()
    if asobject:
        date, open, close, high, low, volume = map(nx.asarray, zip(*results))
        return Bunch(date=date, open=open, close=close, high=high, low=low, volume=volume)
    else:

        return results
コード例 #4
0
    ticker = ticker.upper()

    results = []
    try:
        lines = urlopen(url).readlines()
    except IOError, exc:
        verbose.report_error('urlopen() failure\n' + url + '\n' + exc.strerror[1])
        return None
    for line in lines[1:]:

        vals = line.split(',')
        if len(vals)!=7: continue
        datestr = vals[0]
        dt = datetime.date(*time.strptime(datestr, '%d-%b-%y')[:3])
        d = date2num(dt)
        open, high, low, close =  [float(val) for val in vals[1:5]]
        volume = int(vals[5])

        results.append((d, open, close, high, low, volume))
    results.reverse()
    return results


        
def plot_day_summary(ax, quotes, ticksize=3,
                     colorup='k', colordown='r',
                     ):
    """
    quotes is a list of (time, open, close, high, low, ...) tuples
    
コード例 #5
0
ファイル: finance.py プロジェクト: jtomase/matplotlib
    ticker = ticker.upper()

    results = []
    try:
        lines = urlopen(url).readlines()
    except IOError, exc:
        verbose.report_error('urlopen() failure\n' + url + '\n' + exc.strerror[1])
        return None
    for line in lines[1:]:

        vals = line.split(',')
        if len(vals)!=7: continue
        datestr = vals[0]
        dt = datetime.date(*time.strptime(datestr, '%d-%b-%y')[:3])
        d = date2num(dt)
        open, high, low, close =  [float(val) for val in vals[1:5]]
        volume = int(vals[5])

        results.append((d, open, close, high, low, volume))
    results.reverse()
    return results


        
def plot_day_summary(ax, quotes, ticksize=3,
                     colorup='k', colordown='r',
                     ):
    """
    quotes is a list of (time, open, close, high, low, ...) tuples