def getPrices(startDate, endDate, symbols, fields, fillna=True, isSymbolsList=False, includeLastDay=True): """ reads stock prices from Yahoo the prices returned INCLUDE the endDate @param isSymbolsList: whether the symbols passed in is a stock symbol or a list symbol (e.g. sp5002012). If true, symbols can contain only one symbol. @return prices with NaNs filled (forward, backward, 1.0) """ assert not isSymbolsList or isinstance(symbols, str) or len(symbols) == 1, \ 'When isSymbolsList is true, symbols can only contain one symbol.' if includeLastDay: endDate += timedelta(days=1) dataReader = DataAccess('Yahoo') timeStamps = getNYSEdays(startDate, endDate, timedelta(hours=16)) if isSymbolsList: symbols = dataReader.get_symbols_from_list(symbols if isinstance(symbols, str) else symbols[0]) data = dataReader.get_data(timeStamps, symbols, fields) if fillna: data = fillNA(data) # data.index = pd.Series(data.index) - timedelta(hours=16) # remove 16 from the dates return data
def marketsim(cash, orders_file, data_item): # Read orders orders = defaultdict(list) symbols = set([]) for year, month, day, sym, action, num in csv.reader(open(orders_file, "rU")): orders[date(int(year), int(month), int(day))].append((sym, action, int(num))) symbols.add(sym) days = orders.keys() days.sort() day, end = days[0], days[-1] # Reading the Data for the list of Symbols. timestamps = getNYSEdays(datetime(day.year,day.month,day.day), datetime(end.year,end.month,end.day+1), timedelta(hours=16)) dataobj = DataAccess('Yahoo') close = dataobj.get_data(timestamps, symbols, data_item) values = [] portfolio = Portfolio(cash) for i, t in enumerate(timestamps): for sym, action, num in orders[date(t.year, t.month, t.day)]: if action == 'Sell': num *= -1 portfolio.update(sym, num, close[sym][i]) entry = (t.year, t.month, t.day, portfolio.value(close, i)) values.append(entry) return values
def question_3(): startDate = datetime(2008, 1, 1) endDate = datetime(2009, 12, 31) timeStamps = getNYSEdays(startDate, endDate, timedelta(hours=16)) dataReader = DataAccess('Yahoo') symbolsListName = 'sp5002012' symbols = dataReader.get_symbols_from_list(symbolsListName) symbols.append('SPY') data = fillNA(dataReader.get_data(timeStamps, symbols, 'actual_close')) # read data and filled na # create events and event-profile events = find_crossing_threshold_events(data, threshold=7) eventprofiler(events, {'close': data}, i_lookback=20, i_lookforward=20, s_filename = join(rootDir, symbolsListName + '_threshold7.pdf'), b_market_neutral=True, b_errorbars=True, s_market_sym='SPY')
def simulate(startDate, endDate, symbols, allocations, timeOfDay = timedelta(hours=16), verbose=True, cache=False): """ simulate and assess the performance of a buy-and-hold 4 stock portfolio @param startDate: the start date @param endDate: the end date @param symbols: symbols for the equities, a list @param allocations: allocations to the equities (buy and hold) in decimals. e.g. [0.2, 0.3, 0.5] @type startDate dt.datetime @type endDate dt.datetime @type allocations list @type symbols list @return: Standard deviation of daily returns of the total portfolio, Average daily return of the total portfolio, Sharpe ratio (Always assume you have 252 trading days in an year. And risk free rate = 0) of the total portfolio, Cumulative return of the total portfolio """ # make time stamps timeStamps = getNYSEdays(startDate, endDate, timeOfDay) # read data dataReader = DataAccess('Yahoo', cachestalltime = 12 if cache else 0) data = dataReader.get_data(timeStamps, symbols, ['close'])[0].values # get the values as a np array if verbose: print '-------- Data read ------------\n', data # allocate portPrices = combineStocks(data, allocations) if verbose: print '-------- Portfolio Prices ------------\n', portPrices # compute returns dailyReturns = copy(portPrices) returnize0(dailyReturns) if verbose: print '-------- Portfolio Returns ------------\n', dailyReturns # compute metrics stdDailyRets = np.std(dailyReturns) avgDailyRets = np.mean(dailyReturns) sharpeRatio = avgDailyRets / stdDailyRets * np.sqrt(252) cumRet = portPrices[-1] if verbose: print '--------- Metrics -------------' print 'STD of daily returns =', stdDailyRets print 'Average daily returns =', avgDailyRets print 'Sharpe Ratio =', sharpeRatio print 'Cumulative return =', cumRet return stdDailyRets, avgDailyRets, sharpeRatio, cumRet
def getPrices(startDate, endDate, symbols, fields, fillna=True, isSymbolsList=False, includeLastDay=True): """ reads stock prices from Yahoo the prices returned INCLUDE the endDate @param isSymbolsList: whether the symbols passed in is a stock symbol or a list symbol (e.g. sp5002012). If true, symbols can contain only one symbol. @return prices with NaNs filled (forward, backward, 1.0) """ assert not isSymbolsList or isinstance(symbols, str) or len(symbols) == 1, \ 'When isSymbolsList is true, symbols can only contain one symbol.' if includeLastDay: endDate += timedelta(days=1) dataReader = DataAccess('Yahoo') timeStamps = getNYSEdays(startDate, endDate, timedelta(hours=16)) if isSymbolsList: symbols = dataReader.get_symbols_from_list( symbols if isinstance(symbols, str) else symbols[0]) data = dataReader.get_data(timeStamps, symbols, fields) if fillna: data = fillNA(data) # data.index = pd.Series(data.index) - timedelta(hours=16) # remove 16 from the dates return data