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)
def findEvents(symbols_year, startday, endday, event, data_item="close"): dataobj = DataAccess('Yahoo') symbols = dataobj.get_symbols_from_list("sp500%d" % symbols_year) symbols.append('SPY') # Reading the Data for the list of Symbols. timestamps = getNYSEdays(startday, endday, timedelta(hours=16)) # Reading the Data print "# reading data" close = dataobj.get_data(timestamps, symbols, data_item) # Generating the Event Matrix print "# finding events" eventmat = copy.deepcopy(close) for sym in symbols: for time in timestamps: eventmat[sym][time] = NAN for symbol in symbols: event(eventmat, symbol, close[symbol], timestamps) return eventmat