def get_clean_prices(symbols=None, dataobj=dataobj, start=None, end=None, market_sym='$SPX', reset_cache=True): start = util.normalize_date(start or datetime.date(2008, 1, 1)) end = util.normalize_date(end or datetime.date(2009, 12, 31)) symbols = normalize_symbols(symbols) symbols += [market_sym] print "Calculating timestamps for {0} SP500 symbols".format(len(symbols)) ldt_timestamps = du.getNYSEdays(start, end, datetime.timedelta(hours=16)) ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] print "Retrieving data for {0} SP500 symbols between {1} and {2}.".format( len(symbols), start, end) ldf_data = dataobj.get_data( ldt_timestamps, symbols, ls_keys, ) d_data = dict(zip(ls_keys, ldf_data)) for s_key in ls_keys: print 'cleaning nans from the column {0}'.format(repr(s_key)) d_data[s_key] = d_data[s_key].fillna(method='ffill') d_data[s_key] = d_data[s_key].fillna(method='bfill') d_data[s_key] = d_data[s_key].fillna(1.0) return d_data
def get_clean_prices(symbols=None, dataobj=dataobj, start=None, end=None, market_sym='$SPX', reset_cache=True): start = util.normalize_date(start or datetime.date(2008, 1, 1)) end = util.normalize_date(end or datetime.date(2009, 12, 31)) symbols = normalize_symbols(symbols) symbols += [market_sym] print "Calculating timestamps for {0} SP500 symbols".format(len(symbols)) ldt_timestamps = du.getNYSEdays(start, end, datetime.timedelta(hours=16)) ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] print "Retrieving data for {0} SP500 symbols between {1} and {2}.".format(len(symbols), start, end) ldf_data = dataobj.get_data(ldt_timestamps, symbols, ls_keys, ) d_data = dict(zip(ls_keys, ldf_data)) for s_key in ls_keys: print 'cleaning nans from the column {0}'.format(repr(s_key)) d_data[s_key] = d_data[s_key].fillna(method='ffill') d_data[s_key] = d_data[s_key].fillna(method='bfill') d_data[s_key] = d_data[s_key].fillna(1.0) return d_data
def price_dataframe( symbols='sp5002012', start=datetime.datetime(2008, 1, 1), end=datetime.datetime(2009, 12, 31), price_type='actual_close', cleaner=clean_dataframe, ): """Retrieve the prices of a list of equities as a DataFrame (columns = symbols) Arguments: symbols (list of str): Ticker symbols like "GOOG", "AAPL", etc e.g. ["AAPL", " slv ", GLD", "GOOG", "$SPX", "XOM", "msft"] start (datetime): The date at the start of the period being analyzed. end (datetime): The date at the end of the period being analyzed. Yahoo data stops at 2013/1/1 """ if isinstance(price_type, basestring): price_type = [price_type] start = util.normalize_date(start or datetime.date(2008, 1, 1)) end = util.normalize_date(end or datetime.date(2009, 12, 31)) symbols = normalize_symbols(symbols) t = du.getNYSEdays(start, end, datetime.timedelta(hours=16)) df = clean_dataframes(dataobj.get_data(t, symbols, price_type)) if not df or len(df) > 1: return cleaner(df) else: return cleaner(df[0])
def price_dataframe(symbols='sp5002012', start=datetime.datetime(2008, 1, 1), end=datetime.datetime(2009, 12, 31), price_type='actual_close', cleaner=clean_dataframe, ): """Retrieve the prices of a list of equities as a DataFrame (columns = symbols) Arguments: symbols (list of str): Ticker symbols like "GOOG", "AAPL", etc e.g. ["AAPL", " slv ", GLD", "GOOG", "$SPX", "XOM", "msft"] start (datetime): The date at the start of the period being analyzed. end (datetime): The date at the end of the period being analyzed. Yahoo data stops at 2013/1/1 """ if isinstance(price_type, basestring): price_type = [price_type] start = util.normalize_date(start or datetime.date(2008, 1, 1)) end = util.normalize_date(end or datetime.date(2009, 12, 31)) symbols = normalize_symbols(symbols) t = du.getNYSEdays(start, end, datetime.timedelta(hours=16)) df = clean_dataframes(dataobj.get_data(t, symbols, price_type)) if not df or len(df) > 1: return cleaner(df) else: return cleaner(df[0])
def prices(symbol='$DJI', start=datetime.datetime(2008,1,1), end=datetime.datetime(2009,12,31)): start = util.normalize_date(start or datetime.date(2008, 1, 1)) end = util.normalize_date(end or datetime.date(2009, 12, 31)) symbol = symbol.upper() timeofday = datetime.timedelta(hours=16) timestamps = du.getNYSEdays(start, end, timeofday) ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] ldf_data = da.get_data(timestamps, [symbol], ls_keys) d_data = dict(zip(ls_keys, ldf_data)) na_price = d_data['close'].values return na_price[:,0]
def prices(symbol='$DJI', start=datetime.datetime(2008, 1, 1), end=datetime.datetime(2009, 12, 31)): start = util.normalize_date(start or datetime.date(2008, 1, 1)) end = util.normalize_date(end or datetime.date(2009, 12, 31)) symbol = symbol.upper() timeofday = datetime.timedelta(hours=16) timestamps = du.getNYSEdays(start, end, timeofday) ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] ldf_data = da.get_data(timestamps, [symbol], ls_keys) d_data = dict(zip(ls_keys, ldf_data)) na_price = d_data['close'].values return na_price[:, 0]
def chart_series(series, market_sym='$SPX', price='actual_close', normalize=True): """Display a graph of the price history for the list of ticker symbols provided Arguments: series (dataframe, list of str, or list of tuples): datafram (Timestamp or Datetime for index) other columns are float y-axis values to be plotted list of str: 1st 3 comma or slash-separated integers are the year, month, day others are float y-axis values list of tuples: 1st 3 integers are year, month, day others are float y-axis values market_sym (str): ticker symbol of equity or comodity to plot along side the series price (str): which market data value ('close', 'actual_close', 'volume', etc) to use for the market symbol for comparison to the series normalize (bool): Whether to normalize prices to 1 at the start of the time series. """ series = util.make_dataframe(series) start = util.normalize_date(series.index[0] or datetime.datetime(2008, 1, 1)) end = util.normalize_date(series.index[-1] or datetime.datetime(2009, 12, 28)) timestamps = du.getNYSEdays(start, end, datetime.timedelta(hours=16)) if market_sym: if isinstance(market_sym, basestring): market_sym = [market_sym.upper().strip()] reference_prices = da.get_data(timestamps, market_sym, [price])[0] reference_dict = dict(zip(market_sym, reference_prices)) for sym, market_data in reference_dict.iteritems(): series[sym] = pd.Series(market_data, index=timestamps) # na_price = reference_dict[price].values # if normalize: # na_price /= na_price[0, :] series.plot() # plt.clf() # plt.plot(timestamps, na_price) # plt.legend(symbols) # plt.ylabel(price.title()) # plt.xlabel('Date') # # plt.savefig('portfolio.chart_series.pdf', format='pdf') plt.grid(True) plt.show() return series
def portfolio_prices( symbols=("AAPL", "GLD", "GOOG", "$SPX", "XOM", "msft"), start=datetime.datetime(2005, 1, 1), end=datetime.datetime(2011, 12, 31), # data stops at 2013/1/1 normalize=True, allocation=None, price_type='actual_close', ): """Calculate the Sharpe Ratio and other performance metrics for a portfolio Arguments: symbols (list of str): Ticker symbols like "GOOG", "AAPL", etc start (datetime): The date at the start of the period being analyzed. end (datetime): The date at the end of the period being analyzed. normalize (bool): Whether to normalize prices to 1 at the start of the time series. allocation (list of float): The portion of the portfolio allocated to each equity. """ symbols = normalize_symbols(symbols) start = util.normalize_date(start) end = util.normalize_date(end) if allocation is None: allocation = [1. / len(symbols)] * len(symbols) if len(allocation) < len(symbols): allocation = list(allocation) + [1. / len(symbols) ] * (len(symbols) - len(allocation)) total = np.sum(allocation.sum) allocation = np.array([(float(a) / total) for a in allocation]) timestamps = du.getNYSEdays(start, end, datetime.timedelta(hours=16)) ls_keys = [price_type] ldf_data = da.get_data(timestamps, symbols, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) na_price = d_data[price_type].values if normalize: na_price /= na_price[0, :] na_price *= allocation return np.sum(na_price, axis=1)
def chart( symbols=("AAPL", "GLD", "GOOG", "$SPX", "XOM", "msft"), start=datetime.datetime(2008, 1, 1), end=datetime.datetime(2009, 12, 31), # data stops at 2013/1/1 normalize=True, ): """Display a graph of the price history for the list of ticker symbols provided Arguments: symbols (list of str): Ticker symbols like "GOOG", "AAPL", etc start (datetime): The date at the start of the period being analyzed. end (datetime): The date at the end of the period being analyzed. normalize (bool): Whether to normalize prices to 1 at the start of the time series. """ start = util.normalize_date(start or datetime.date(2008, 1, 1)) end = util.normalize_date(end or datetime.date(2009, 12, 31)) symbols = [s.upper() for s in symbols] timeofday = datetime.timedelta(hours=16) timestamps = du.getNYSEdays(start, end, timeofday) ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] ldf_data = da.get_data(timestamps, symbols, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) na_price = d_data['close'].values if normalize: na_price /= na_price[0, :] plt.clf() plt.plot(timestamps, na_price) plt.legend(symbols) plt.ylabel('Adjusted Close') plt.xlabel('Date') plt.savefig('chart.pdf', format='pdf') plt.grid(True) plt.show() return na_price
def portfolio_prices( symbols=("AAPL", "GLD", "GOOG", "$SPX", "XOM", "msft"), start=datetime.datetime(2005, 1, 1), end=datetime.datetime(2011, 12, 31), # data stops at 2013/1/1 normalize=True, allocation=None, price_type='actual_close', ): """Calculate the Sharpe Ratio and other performance metrics for a portfolio Arguments: symbols (list of str): Ticker symbols like "GOOG", "AAPL", etc start (datetime): The date at the start of the period being analyzed. end (datetime): The date at the end of the period being analyzed. normalize (bool): Whether to normalize prices to 1 at the start of the time series. allocation (list of float): The portion of the portfolio allocated to each equity. """ symbols = normalize_symbols(symbols) start = util.normalize_date(start) end = util.normalize_date(end) if allocation is None: allocation = [1. / len(symbols)] * len(symbols) if len(allocation) < len(symbols): allocation = list(allocation) + [1. / len(symbols)] * (len(symbols) - len(allocation)) total = np.sum(allocation.sum) allocation = np.array([(float(a) / total) for a in allocation]) timestamps = du.getNYSEdays(start, end, datetime.timedelta(hours=16)) ls_keys = [price_type] ldf_data = da.get_data(timestamps, symbols, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) na_price = d_data[price_type].values if normalize: na_price /= na_price[0, :] na_price *= allocation return np.sum(na_price, axis=1)