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 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 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