def findEvents(symbols, startday,endday,weeks, marketSymbol,verbose=False):

        # week calc
        week=dt.timedelta(weeks=weeks)
        time = startday.time()
        lookbackday=dt.datetime.combine(startday,time)-week
        # week cal

        # Reading the Data for the list of Symbols.
        timeofday=dt.timedelta(hours=16)

        closefield = "close"
        lookbackTimestamps = du.getNSEdays(lookbackday,startday,timeofday)
        analyzeTimestamps=du.getNSEdays(startday,endday,timeofday)
        timestamps=lookbackTimestamps+analyzeTimestamps

        np_eventmat=pandas.DataFrame(index=timestamps, columns=symbols)

        dataobj = da.DataAccess('NSEData')

        close = dataobj.get_data(timestamps, symbols, closefield)

        close = (close.fillna(method='ffill')).fillna(method='backfill')
##        close.to_csv('res.csv',sep=',')

        for i in range(0, len(analyzeTimestamps)):
            print 'Iteration : ',i+1
            currentDates = [close.index[j] for j in range(i,len(lookbackTimestamps)+1+i)]

            subDataframe=close.ix[currentDates]

            eventDate= currentDates[len(currentDates)-1]

            maxValue=subDataframe.idxmax()
            minValue=subDataframe.idxmin()

            for stockname, date in maxValue.T.iteritems():
                value=(close[stockname][eventDate]/close[stockname][date])-1
                if(value<=0.00 and value>-0.02):
                    np_eventmat[stockname][eventDate]='52wk+++,'+str(close[stockname][eventDate])+','+str(close[stockname][date])+','+str(date.date())+','+str(value*100)+','+str(currentDates[0].date())+' - '+str(eventDate.date())
                elif(value<-0.02 and value>-0.05):
                    np_eventmat[stockname][eventDate]='52wk++,'+str(close[stockname][eventDate])+','+str(close[stockname][date])+','+str(date.date())+','+str(value*100)+','+str(currentDates[0].date())+' - '+str(eventDate.date())
                elif(value<-0.05 and value>-0.07):
                    np_eventmat[stockname][eventDate]='52wk+,'+str(close[stockname][eventDate])+','+str(close[stockname][date])+','+str(date.date())+','+str(value*100)+','+str(currentDates[0].date())+' - '+str(eventDate.date())


            for stockname, date in minValue.T.iteritems():
                value=(close[stockname][eventDate]/close[stockname][date])-1
                if(value>=0.00 and value<0.02):
                    np_eventmat[stockname][eventDate]='52wk---,'+str(close[stockname][eventDate])+','+str(close[stockname][date])+','+str(date.date())+','+str(value*100)+','+str(currentDates[0].date())+' - '+str(eventDate.date())
                elif(value>0.02 and value<0.05):
                    np_eventmat[stockname][eventDate]='52wk--,'+str(close[stockname][eventDate])+','+str(close[stockname][date])+','+str(date.date())+','+str(value*100)+','+str(currentDates[0].date())+' - '+str(eventDate.date())
                elif(value>0.05 and value<0.07):
                    np_eventmat[stockname][eventDate]='52wk-,'+str(close[stockname][eventDate])+','+str(close[stockname][date])+','+str(date.date())+','+str(value*100)+','+str(currentDates[0].date())+' - '+str(eventDate.date())

        return np_eventmat
        def __init__(self,eventMatrix,startday,endday,\
            lookback_days = 20, lookforward_days =20,\
            verbose=False):

            """ Event Profiler class construtor 
                Parameters : evenMatrix
                           : startday
                           : endday
                (optional) : lookback_days ( default = 20)
                (optional) : lookforward_days( default = 20)

                eventMatrix is a pandas DataMatrix
                eventMatrix must have the following structure:
                    |IBM |GOOG|XOM |MSFT| GS | JP |
                (d1)|nan |nan | 1  |nan |nan | 1  |
                (d2)|nan | 1  |nan |nan |nan |nan |
                (d3)| 1  |nan | 1  |nan | 1  |nan |
                (d4)|nan |  1 |nan | 1  |nan |nan |
                ...................................
                ...................................
                Also, d1 = start date
                nan = no information about any event.
                 = status bit(positively confirms the event occurence)
            """

            self.eventMatrix = eventMatrix
            self.startday = startday
            self.endday = endday
            self.symbols = eventMatrix.columns
            self.lookback_days = lookback_days
            self.lookforward_days = lookforward_days
            self.total_days = lookback_days + lookforward_days + 1
     
#     original code
#           self.dataobj = da.DataAccess('Yahoo')
#     original code

#   modified code to access NSE data  
            self.dataobj = da.DataAccess('NSEData')         
#   modified code 
         
            self.timeofday = dt.timedelta(hours=16)
     
#     original code
#           self.timestamps = du.getNYSEdays(startday,endday,self.timeofday)
#     original code

#     modified code  
            self.timestamps = du.getNSEdays(startday,endday,self.timeofday)
#     modified code

            self.verbose = verbose
            if verbose:
                print __name__ + " reading historical data"
            self.close = self.dataobj.get_data(self.timestamps,\
                self.symbols, "close", verbose=self.verbose)
            self.close = (self.close.fillna()).fillna(method='backfill')
def findEvents(symbols, startday,endday, marketSymbol,verbose=False):

        # Reading the Data for the list of Symbols.     
        timeofday=dt.timedelta(hours=16)
        timestamps = du.getNSEdays(startday,endday,timeofday)


        endOfQuarter=getQuarterEndDates(timestamps)
         
     
        dataobj = da.DataAccess('NSEData')
        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)


        # 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 - close[marketSymbol]

        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 : Analysis Quarter End prices of stocks

        for symbol in symbols:
                for i in endOfQuarter:                       

                        np_eventmat[symbol][i] = 1.0  #overwriting by the bit, marking the event
     
                        
        return np_eventmat
def findEvents(data, startday,endday, marketSymbol,verbose=False):

        # Reading the Data for the list of Symbols.     
        timeofday=dt.timedelta(hours=16)
        timestamps = du.getNSEdays(startday,endday,timeofday)
        
        dataobj = da.DataAccess('NSEData')
        if verbose:
            print __name__ + " reading data"
        # Reading the Data
        symbols=getSymbols(data, marketSymbol)
        
        eventDetails=getDates(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)

        # 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 - close[marketSymbol]
        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 : Analysing Stock Prices before and after the occurence of an event.
        # Stocks are analysed on specific dates as per data provided in csv files.

        for stock in eventDetails:      
            stockName=stock[0]
                        
            for i in range(1,len(stock)):
                np_eventmat[stockName][stock[i]] = 1.0  #overwriting by the bit, marking the event
     
                        
        return np_eventmat
def findEvents(symbols, startday,endday, marketSymbol,verbose=False):

	# Reading the Data for the list of Symbols.	
	timeofday=dt.timedelta(hours=16)
	timestamps = du.getNSEdays(startday,endday,timeofday)
	dataobj = da.DataAccess('NSEData')
	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 - close[marketSymbol] 

	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 : Analysis of events occuring when stock price falls to a certain value

	for symbol in symbols:
		
	    for i in range(1,len(mktneutDM[symbol])):
	        if SPYValues[i]<-0.01 and mktneutDM[symbol][i] < -0.02 : # 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
			
	return np_eventmat
import qstkutil.DataAccess as da
import datetime as dt
import matplotlib.pyplot as plt
from pylab import *
import pandas

print pandas.__version__

#
# Prepare to read the data
#
symbols = ["NSE","ABB.NS","ACC.NS","TCS.NS"]
startday = dt.datetime(2008,1,1)
endday = dt.datetime(2011,12,31)
timeofday=dt.timedelta(hours=16)
timestamps = du.getNSEdays(startday,endday,timeofday)

#dataobj = da.DataAccess('Yahoo')

dataobj = da.DataAccess('NSEData')

voldata = dataobj.get_data(timestamps, symbols, "volume",verbose=True)
close = dataobj.get_data(timestamps, symbols, "close",verbose=True)
actualclose = dataobj.get_data(timestamps, symbols, "actual_close",verbose=True)
#
# Plot the adjusted close data
#
plt.clf()
newtimestamps = close.index
pricedat = close.values # pull the 2D ndarray out of the pandas object
plt.plot(newtimestamps,pricedat)
    def findEvents(self, symbols, columnIndexes, verbose=False):

        eventDetails=self.eventDetailsData

        # Reading the Data for the list of Symbols.
        timeofday=dt.timedelta(hours=16)

        timestamps = du.getNSEdays(self.startday,self.endday,timeofday)

        stockDates=self.getDates(eventDetails, timestamps, columnIndexes)

        dataobj = da.DataAccess('NSEData')
        if verbose:
            print __name__ + " reading data"
        # Reading the Data

        close = dataobj.get_data(timestamps, symbols, self.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)
        NSEValues=close[self.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 - close[self.marketSymbol]
        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 : Analysing Stock Prices before and after the occurence of an event.
        # Stocks are analysed on specific dates as per data provided in csv files accessed from Google Docs.

        if(self.analyzeLookbackDays):
            for stock in stockDates:
                stockName=stock[0]
                for i in range(1,len(stock)):
                    if(self.analyzeStock(stockName, stock[i], mktneutDM)):
                        np_eventmat[stockName][stock[i]] = 1.0  #overwriting by the bit, marking the event

        else:

            if((self.marketChangeMin!='NA' and self.marketChangeMax!='NA') or (self.stockChangeMin!='NA' and self.stockChangeMax!='NA')):
                np_eventmat=self.getRangeChangeMatrix(stockDates, NSEValues, mktneutDM, np_eventmat)

            elif(self.marketValueChange!='NA' or self.stockValueChange!='NA'):
                np_eventmat=self.getValueChangeMatrix(stockDates, NSEValues, mktneutDM, np_eventmat)

            else:
                for stock in stockDates:
                    stockName=stock[0]
                    for i in range(1,len(stock)):
                        np_eventmat[stockName][stock[i]] = 1.0  #overwriting by the bit, marking the event

        return np_eventmat