MichaelJMath
10/5/2018 - 4:39 PM

Get Pricing from Y Charts API

Get OHLCV Pricing from Y Charts API and return as a dataframe.

def get_ohlcv(symbol, company_client, start_date, end_date, dropna=True):
    """Get OHLCV data from YCharts API
    
    Parameters
    ----------
    symbol: string
    company_client: pycharts.CompanyClient
    start_date: pd.datetime
    end_date:pd.datetime
    dropna: boolean
    
    Returns
    -------
    price_df: pd.DataFrame
    """
    prices = cc.get_series(symbol, ['open_price', 'high_price', 'low_price',  'close_price', 'volume'], 
              start_date, end_date )
    prices = prices['response'][symbol]['results']
    result = {}
    for field in prices:
        result[field] = dict(prices[field]['data'])
        
    price_df = pd.DataFrame.from_dict(result)
    price_df.index = pd.to_datetime(price_df.index)
    price_df.sort_index(inplace=True)
    
    null_rows = price_df[price_df.isnull().any(axis=1)].index.strftime('%Y-%m-%d').tolist()
    print "Dates with NaNs: {}".format(null_rows)
    
    if dropna:
        nrows = len(price_df)

        price_df.dropna(inplace=True)
        print "{} NaN Rows Dropped".format(nrows - len(price_df) )
        
    return price_df