예제 #1
0
def fetch_stock_values(stock_code, start_date, end_date):
    """Fetches daily stock values from Yahoo Finance."""

    start_date = date_to_datetime(
        parse_date(start_date if start_date is not None else -30 * 3600 * 24))
    end_date = date_to_datetime(
        parse_date(end_date if end_date is not None else 0))

    if start_date > end_date:
        raise ValueError("start_date must be equal to or less than end_date")

    provider = Yahoo()
    rows = provider.asset_values(stock_code, start_date, end_date,
                                 Granularity.day)

    for row in rows:
        # TODO: Write a function to handle this for generic cases
        # TODO: Convert the timestamp to an ISO format
        # NOTE: The last column is data source. Not sure if this is an elegant
        # way to handle this.

        # FIXME: Think of a better way to handle this
        dt = row[0].isoformat()

        print(", ".join([dt] + [str(c) for c in row[1:]] + ["yahoo"]))
예제 #2
0
파일: __main__.py 프로젝트: suminb/finance
def fetch_stock_values(stock_code, start_date, end_date):
    """Fetches daily stock values from Yahoo Finance."""

    start_date = date_to_datetime(
        parse_date(start_date if start_date is not None else -30 * 3600 * 24))
    end_date = date_to_datetime(
        parse_date(end_date if end_date is not None else 0))

    if start_date > end_date:
        raise ValueError('start_date must be equal to or less than end_date')

    provider = Yahoo()
    rows = provider.asset_values(
        stock_code, start_date, end_date, Granularity.day)

    for row in rows:
        # TODO: Write a function to handle this for generic cases
        # TODO: Convert the timestamp to an ISO format
        # NOTE: The last column is data source. Not sure if this is an elegant
        # way to handle this.

        # FIXME: Think of a better way to handle this
        dt = row[0].isoformat()

        print(', '.join([dt] + [str(c) for c in row[1:]] + ['yahoo']))
예제 #3
0
def fetch_stock_values(stock_code, start_date, end_date,
                       granularity=Granularity.day):
    """Fetches stock prices from Yahoo Finance."""
    if start_date > end_date:
        raise ValueError('start_date must be equal to or less than end_date')

    provider = Yahoo()
    rows = provider.asset_values(
        stock_code, start_date, end_date, granularity)

    for row in rows:
        # NOTE: The last column is data source. Not sure if this is an elegant
        # way to handle this.
        yield row + (provider.name,)
예제 #4
0
def test_yahoo_fetch_data():
    provider = Yahoo()
    from_date, to_date = parse_date('2014-01-01'), parse_date('2015-12-31')
    data = provider.fetch_data('005380.KS', from_date, to_date)

    for date, open_, high, low, close_, volume, adj_close in data:
        assert isinstance(date, datetime)
        # assert from_date <= date <= to_date
        assert isinstance(open_, float)
        assert isinstance(high, float)
        assert isinstance(low, float)
        assert isinstance(close_, float)
        assert isinstance(volume, int)
        assert isinstance(adj_close, float)
예제 #5
0
def test_yahoo_fetch_data_with_invalid_code():
    provider = Yahoo()
    from_date, to_date = parse_date('2014-01-01'), parse_date('2015-12-31')
    with pytest.raises(HTTPError):
        data = provider.fetch_data('!@#$%', from_date, to_date)
        next(data)