Ejemplo n.º 1
0
    def __init__(self):
        self.data_access = DataAccess()

        self.symbols = []
        self.start_date = None
        self.end_date = None
        self.field = 'Adj Close'

        self.condition = Condition()
        self.matrix = None
        self.num_events = 0

        self.oneEventPerEquity = True
Ejemplo n.º 2
0
    def __init__(self):
        self.data_access = DataAccess()

        self.symbols = []
        self.start_date = None
        self.end_date = None
        self.field = 'adjusted_close'

        self.condition = Condition()
        self.matrix = None
        self.num_events = 0

        self.oneEventPerEquity = True
Ejemplo n.º 3
0
class EventFinder(object):
    def __init__(self):
        self.data_access = DataAccess()

        self.symbols = []
        self.start_date = None
        self.end_date = None
        self.field = 'adjusted_close'

        self.condition = Condition()
        self.matrix = None
        self.num_events = 0

        self.oneEventPerEquity = True

    def generate_filename(self):
        return '%s%s%s%s%s%s' % (''.join(self.symbols), self.start_date.strftime('%Y-%m-%d'),
                self.end_date.strftime('%Y-%m-%d'), self.field, self.condition.id,
                str(self.oneEventPerEquity))

    def search(self, oneEventPerEquity=True, useCache=True, save=True):
        self.oneEventPerEquity = oneEventPerEquity

        # 1. Load the data if requested and available
        self.matrix = self.data_access.load(self.generate_filename(), '.evt_matrix')
        if useCache and self.matrix is not None:
            pass
        else:
            # 2. Data was not loaded
            # 2.1 Get the dates, and Download/Import the data
            nyse_dates = DateUtils.nyse_dates(start=self.start_date, end=self.end_date)
            data = self.data_access.get_data(self.symbols, nyse_dates[0], nyse_dates[-1], self.field)
            # Special case
            if len(data.columns) == 1:
                data.columns = self.symbols

            # 2.2 Create and fill the matrix of events
            data = data[self.start_date:self.end_date]
            self.matrix = pd.DataFrame(index=data.index, columns=self.symbols)

            for symbol in self.symbols:
                i = 0
                for item in data[symbol][1:]:
                    e = self.condition.function(i, item, data[symbol][1:])
                    if e:
                        self.matrix[symbol][i+1] = 1
                        if oneEventPerEquity == True:
                            break
                    i = i + 1


        # 3. Calculate other results and save if requested
        # Reduce Matrix: Sum each row and columns: if is greater than 0 there is an event
        self.matrix = self.matrix[self.matrix.fillna(value=0).sum(axis=1) > 0]
        valid_cols = self.matrix.columns[self.matrix.fillna(value=0).sum(axis=0) > 0].values
        self.matrix = self.matrix[valid_cols]
        # 3.2 Create list of events
        self.list = pd.Series(index=self.matrix.index, name='Equity')
        for idx, row in self.matrix.iterrows():
            equity = row[row == 1].index[0]
            self.list.loc[idx] = equity
        # 3.3 Save
        self.num_events = len(self.list)
        if save:
            self.data_access.save(self.matrix, self.generate_filename(), '.evt_matrix')
Ejemplo n.º 4
0
def decrease(decrease):
    condition = Condition()
    condition.id = 'decrease' + str(decrease)
    condition.function = lambda i, item, data: (data[i - 1] - item > decrease)
    return condition
Ejemplo n.º 5
0
class EventFinder(object):
    def __init__(self):
        self.data_access = DataAccess()

        self.symbols = []
        self.start_date = None
        self.end_date = None
        self.field = 'Adj Close'

        self.condition = Condition()
        self.matrix = None
        self.num_events = 0

        self.oneEventPerEquity = True

    def generate_filename(self):
        return '%s%s%s%s%s%s' % (
            ''.join(self.symbols), self.start_date.strftime('%Y-%m-%d'),
            self.end_date.strftime('%Y-%m-%d'), self.field, self.condition.id,
            str(self.oneEventPerEquity))

    def search(self, oneEventPerEquity=True, useCache=True, save=True):
        self.oneEventPerEquity = oneEventPerEquity

        # 1. Load the data if requested and available
        self.matrix = self.data_access.load(self.generate_filename(),
                                            '.evt_matrix')
        if useCache and self.matrix is not None:
            pass
        else:
            # 2. Data was not loaded
            # 2.1 Get the dates, and Download/Import the data
            nyse_dates = DateUtils.nyse_dates(start=self.start_date,
                                              end=self.end_date)
            data = self.data_access.get_data(self.symbols, nyse_dates[0],
                                             nyse_dates[-1], self.field)
            # Special case
            if len(data.columns) == 1:
                data.columns = self.symbols

            # 2.2 Create and fill the matrix of events
            data = data[self.start_date:self.end_date]
            self.matrix = pd.DataFrame(index=data.index, columns=self.symbols)

            for symbol in self.symbols:
                i = 0
                for item in data[symbol][1:]:
                    e = self.condition.function(i, item, data[symbol][1:])
                    if e:
                        self.matrix[symbol][i + 1] = 1
                        if oneEventPerEquity == True:
                            break
                    i = i + 1

        # 3. Calculate other results and save if requested
        # Reduce Matrix: Sum each row and columns: if is greater than 0 there is an event
        self.matrix = self.matrix[self.matrix.fillna(value=0).sum(axis=1) > 0]
        valid_cols = self.matrix.columns[self.matrix.fillna(value=0).sum(
            axis=0) > 0].values
        self.matrix = self.matrix[valid_cols]
        # 3.2 Create list of events
        self.list = pd.Series(index=self.matrix.index,
                              name='Equity',
                              dtype=str)
        for idx, row in self.matrix.iterrows():
            equity = row[row == 1].index[0]
            self.list.ix[idx] = equity
        # 3.3 Save
        self.num_events = len(self.list)
        if save:
            self.data_access.save(self.matrix, self.generate_filename(),
                                  '.evt_matrix')
Ejemplo n.º 6
0
def went_below(below):
    condition = Condition()
    condition.id = 'went_below' + str(below)
    condition.function = lambda i, item, data: (data[i - 1] >= below and item <
                                                below)
    return condition
Ejemplo n.º 7
0
def went_above(above):
    condition = Condition()
    condition.id = 'went_above' + str(above)
    condition.function = lambda i, item, data: (data[i - 1] <= above and item >
                                                above)
    return condition
Ejemplo n.º 8
0
def increase(increase):
    condition = Condition()
    condition.id = 'increase' + str(increase)
    condition.function = lambda i, item, data: (item - data[i - 1] > increase)
    return condition
Ejemplo n.º 9
0
def increase(increase):
    condition = Condition()
    condition.id = 'increase' + str(increase)
    condition.function =  lambda i, item, data: (item - data[i-1] > increase)
    return condition
Ejemplo n.º 10
0
def went_above(above):
    condition = Condition()
    condition.id = 'went_above' + str(above)
    condition.function = lambda i, item, data: (data[i-1] <= above and item > above)
    return condition
Ejemplo n.º 11
0
def decrease(decrease):
    condition = Condition()
    condition.id = 'decrease' + str(decrease)
    condition.function = lambda i, item, data: (data[i-1] - item > decrease)
    return condition
Ejemplo n.º 12
0
def went_below(below):
    condition = Condition()
    condition.id = 'went_below' + str(below)
    condition.function = lambda i, item, data: (data[i-1] >= below and item < below)
    return condition