def findEvents(symbols, startday,endday, marketSymbol,verbose=False): # Reading the Data for the list of Symbols. timeofday=dt.timedelta(hours=16) timestamps = du.getSSEdays(startday,endday,timeofday) dataobj = da.DataAccess('Yahoo') if verbose: print __name__ + " reading data" # Reading the Data close = dataobj.get_data(timestamps, symbols, closefield) # Completing the Data - Removing the NaN values from the Matrix close = (close.fillna(method='ffill')).fillna(method='backfill') # Calculating Daily Returns for the Market #tsu.returnize0(close.values) SPYValues=close[marketSymbol] # Calculating the Returns of the Stock Relative to the Market # So if a Stock went up 5% and the Market rised 3%. The the return relative to market is 2% mktneutDM = close np_eventmat = copy.deepcopy(mktneutDM) for sym in symbols: for time in timestamps: np_eventmat[sym][time]=np.NAN if verbose: print __name__ + " finding events" # Generating the Event Matrix # Event described is : Market falls more than 3% plus the stock falls 5% more than the Market # Suppose : The market fell 3%, then the stock should fall more than 8% to mark the event. # And if the market falls 5%, then the stock should fall more than 10% to mark the event. output = open(output_flie, 'w') output_content = [] for i in range(1,len(timestamps)): for symbol in symbols: if close[symbol][i-1] >= 7 and close[symbol][i] < 7 : # When market fall is more than 3% and also the stock compared to market is also fell by more than 5%. np_eventmat[symbol][i] = 1.0 #overwriting by the bit, marking the event buy_date = timestamps[i] if i+5 < len(mktneutDM[symbol]): sell_date = timestamps[i+5]; else: sell_date = timestamps[-1]; output_line = [] output_line.append(buy_date); output_line.append(symbol) output_line.append("Buy") output_line.append(100) output_content.append(output_line) output_line = [] output_line.append(sell_date); output_line.append(symbol) output_line.append("Sell") output_line.append(100) output_content.append(output_line) output_content = sorted(output_content, key = lambda x: x[0]) for item in output_content: d = item[0]; line = str(d.year) + ","+ str(d.month) + "," + str(d.day) + "," line += item[1] + "," line += item[2] + "," + str(item[3])+"\n" output.write(line) output.close() return np_eventmat
def findEvents(symbols, startday, endday, marketSymbol, verbose=False): # Reading the Data for the list of Symbols. timeofday = dt.timedelta(hours=0) timestamps = du.getSSEdays(startday, endday, timeofday) dataobj = da.DataAccess('Yahoo') if verbose: print __name__ + " reading data" # Reading the Data close = dataobj.get_data(timestamps, symbols, closefield) for sym in symbols: print sym # Completing the Data - Removing the NaN values from the Matrix #close = (close.fillna(method='ffill')).fillna(method='backfill') #print close['SPY'][0], close['SPY'][1] # Calculating Daily Returns for the Market #tsu.returnize0(close.values) SPYValues = close[marketSymbol] # Calculating the Returns of the Stock Relative to the Market # So if a Stock went up 5% and the Market rised 3%. The the return relative to market is 2% mktneutDM = close np_eventmat = copy.deepcopy(mktneutDM) for sym in symbols: for time in timestamps: np_eventmat[sym][time] = np.NAN if verbose: print __name__ + " finding events" # Generating the Event Matrix # Event described is : Market falls more than 3% plus the stock falls 5% more than the Market # Suppose : The market fell 3%, then the stock should fall more than 8% to mark the event. # And if the market falls 5%, then the stock should fall more than 10% to mark the event. output = open(output_flie, 'w') output_content = [] for i in range(1, len(timestamps)): for symbol in symbols: if close[symbol][i - 1] >= 7 and close[symbol][ i] < 7: # When market fall is more than 3% and also the stock compared to market is also fell by more than 5%. np_eventmat[symbol][ i] = 1.0 #overwriting by the bit, marking the event buy_date = timestamps[i] if i + 5 < len(mktneutDM[symbol]): sell_date = timestamps[i + 5] else: sell_date = timestamps[-1] output_line = [] output_line.append(buy_date) output_line.append(symbol) output_line.append("Buy") output_line.append(100) output_content.append(output_line) output_line = [] output_line.append(sell_date) output_line.append(symbol) output_line.append("Sell") output_line.append(100) output_content.append(output_line) output_content = sorted(output_content, key=lambda x: x[0]) for item in output_content: d = item[0] line = str(d.year) + "," + str(d.month) + "," + str(d.day) + "," line += item[1] + "," line += item[2] + "," + str(item[3]) + "\n" output.write(line) output.close() return np_eventmat
item.append(date) item.append(trade_data[3]) item.append(trade_data[4]) item.append(int(trade_data[5])) trades.append(item) symbols.append(trade_data[3]) print trades #startday = dt.datetime(2011,1,1) #endday = dt.datetime(2012,12,26) startday = trades[0][0] endday = trades[-1][0] """step2 read history data""" symbols = set(symbols) dataobj = da.DataAccess('Yahoo') timeofday = dt.timedelta(hours=16) timestamps = du.getSSEdays(startday, endday, timeofday) close = dataobj.get_data(timestamps, symbols, closefield) close = (close.fillna(method='ffill')).fillna(method='backfill') """step 3 caculate value""" values = {} for item in timestamps: values[item] = cash stock = {} for sym in symbols: stock[sym] = 0 trade_t = startday for i in range(len(trades)): t = trades[i] t_next = t