Ejemplo n.º 1
0
    def filter(self, startdate, enddate=None, parent=None, **kwargs):
        if 'datetime_index' not in kwargs:
            kwargs.update({'datetime_index': self.datetime_index})
        kwargs.update({'reindex': self.reindex})
        if 'date_check' not in kwargs:
            kwargs.update({'date_check': self.date_check})
        date_check = kwargs.get('date_check', self.date_check)

        univ_window = dateutil.cut_window(
            DATES,
            dateutil.compliment_datestring(str(startdate), -1, date_check),
            dateutil.compliment_datestring(str(enddate), 1, date_check)
            if enddate is not None else None)
        startdate, enddate = univ_window[0], univ_window[-1]
        # at first, the universe is full, i.e. all sids are included
        parent = pd.DataFrame(True,
                              index=pd.to_datetime(univ_window)
                              if self.datetime_index else univ_window,
                              columns=SIDS)
        for elem in self._filters:
            # if the item is a tuple of filter, we first call their filter method and then intersect the results
            if isinstance(elem, tuple) or isinstance(elem, list):
                res = [
                    f.filter(startdate, enddate, parent=parent, **kwargs)
                    for f in elem
                ]
                for df in res:
                    parent = parent & df
            # simply call its filter method
            else:
                parent = elem.filter(startdate,
                                     enddate,
                                     parent=parent,
                                     **kwargs)
        return parent
Ejemplo n.º 2
0
Archivo: ops.py Proyecto: leeong05/orca
    def filter(self, startdate, enddate=None, parent=None, **kwargs):
        if 'datetime_index' not in kwargs:
            kwargs.update({'datetime_index': self.datetime_index})
        kwargs.update({'reindex': self.reindex})
        if 'date_check' not in kwargs:
            kwargs.update({'date_check': self.date_check})
        date_check = kwargs.get('date_check', self.date_check)

        univ_window = dateutil.cut_window(
                DATES,
                dateutil.compliment_datestring(str(startdate), -1, date_check),
                dateutil.compliment_datestring(str(enddate), 1, date_check) if enddate is not None else None)
        startdate, enddate = univ_window[0], univ_window[-1]
        # at first, the universe is full, i.e. all sids are included
        parent = pd.DataFrame(True,
                index=pd.to_datetime(univ_window) if self.datetime_index else univ_window,
                columns=SIDS)
        for elem in self._filters:
            # if the item is a tuple of filter, we first call their filter method and then intersect the results
            if isinstance(elem, tuple) or isinstance(elem, list):
                res = [f.filter(startdate, enddate, parent=parent, **kwargs) for f in elem]
                for df in res:
                    parent = parent & df
            # simply call its filter method
            else:
                parent = elem.filter(startdate, enddate, parent=parent, **kwargs)
        return parent
Ejemplo n.º 3
0
    def filter(self, startdate, enddate=None, parent=None, return_parent=False, **kwargs):
        datetime_index = kwargs.get('datetime_index', self.datetime_index)
        reindex = kwargs.get('reindex', self.reindex)
        date_check = kwargs.get('date_check', self.date_check)

        univ_window = dateutil.cut_window(
                DATES,
                dateutil.compliment_datestring(str(startdate), -1, date_check),
                dateutil.compliment_datestring(str(enddate), 1, date_check) if enddate is not None else None)
        si, ei = map(DATES.index, [univ_window[0], univ_window[-1]])
        data_window = DATES[si-self.delay-(self.window-1): ei-self.delay+1]

        dfs = []
        for fetcher, dname in self.datas:
            df = fetcher.fetch_window(dname, data_window)
            df.index = DATES[si-(self.window-1): ei+1]
            dfs.append(df)
        df = self.synth(*dfs)

        parent = self.comply(df, parent)

        df = self.rule(self.window)(df)
        self.comply(df, parent, False)
        df = df.iloc[self.window-1:]
        df.index = univ_window
        if return_parent is True:
            return (self.format(parent, datetime_index=datetime_index, reindex=reindex),
                    self.format(df, datetime_index=datetime_index, reindex=reindex))
        return self.format(df, datetime_index=datetime_index, reindex=reindex)
Ejemplo n.º 4
0
def generate_dates(startdate, enddate, num):
    if enddate is None:
        enddate = DATES[-1]
    dates = dateutil.cut_window(
        DATES, dateutil.compliment_datestring(str(startdate), -1, True),
        dateutil.compliment_datestring(str(enddate), 1, True))
    chksize = len(dates) / num + (len(dates) % num > 0)
    return [dates[i:i + chksize] for i in range(0, len(dates), chksize)]
Ejemplo n.º 5
0
 def interval_fetch(pl, times, startdate, enddate, backdays=0, date_check=False):
     window = dateutil.cut_window(
             DATES,
             dateutil.compliment_datestring(str(startdate), -1, date_check),
             dateutil.compliment_datestring(str(enddate), 1, date_check),
             backdays
             )
     return AlphaBase.interval_fetch_window(pl, times, window)
Ejemplo n.º 6
0
 def record_fetch(df, startdate, enddate, backdays, date_check=False):
     window = dateutil.cut_window(
             DATES,
             dateutil.compliment_datestring(str(startdate), -1, date_check),
             dateutil.compliment_datestring(str(enddate), 1, date_check),
             backdays
             )
     return AlphaBase.record_fetch_window(df, window)
Ejemplo n.º 7
0
 def fetch(self, indicator, startdate, enddate=None, backdays=0, **kwargs):
     date_check = kwargs.get('date_check', self.date_check)
     window = dateutil.cut_window(
             DATES,
             dateutil.compliment_datestring(str(startdate), -1, date_check),
             dateutil.compliment_datestring(str(enddate), 1, date_check) if enddate is not None else None,
             backdays=backdays)
     return self.fetch_window(indicator, window, **kwargs)
Ejemplo n.º 8
0
 def fetch(self, indicator, startdate, enddate=None, backdays=0, **kwargs):
     date_check = kwargs.get('date_check', self.date_check)
     window = dateutil.cut_window(
         DATES,
         dateutil.compliment_datestring(str(startdate), -1, date_check),
         dateutil.compliment_datestring(str(enddate), 1, date_check)
         if enddate is not None else None,
         backdays=backdays)
     return self.fetch_window(indicator, window, **kwargs)
Ejemplo n.º 9
0
    def fetch(self, dname, times, startdate, enddate=None, backdays=0, **kwargs):
        """Use :py:meth:`fetch_window` behind the scene."""
        date_check = kwargs.get('date_check', self.date_check)

        window = dateutil.cut_window(
                DATES,
                dateutil.compliment_datestring(str(startdate), -1, date_check),
                dateutil.compliment_datestring(str(enddate), 1, date_check) if enddate is not None else None,
                backdays=backdays)
        return self.fetch_window(dname, times, window, **kwargs)
Ejemplo n.º 10
0
Archivo: run.py Proyecto: leeong05/orca
def generate_dates(startdate, enddate, num):
    if enddate is None:
        enddate = DATES[-1]
    dates = dateutil.cut_window(
            DATES,
            dateutil.compliment_datestring(str(startdate), -1, True),
            dateutil.compliment_datestring(str(enddate), 1, True)
            )
    chksize = len(dates) / num + (len(dates) % num > 0)
    return [dates[i: i+chksize] for i in range(0, len(dates), chksize)]
Ejemplo n.º 11
0
    def fetch(self, dname, startdate, enddate=None, backdays=0, **kwargs):
        """Use :py:meth:`fetch_window` behind the scene."""
        date_check = kwargs.get('date_check', self.date_check)

        window = dateutil.cut_window(
            DATES,
            dateutil.compliment_datestring(str(startdate), -1, date_check),
            dateutil.compliment_datestring(str(enddate), 1, date_check)
            if enddate is not None else None,
            backdays=backdays)
        return self.fetch_window(dname, window, **kwargs)
Ejemplo n.º 12
0
    def filter(self, startdate, enddate=None, parent=None, **kwargs):
        datetime_index = kwargs.get('datetime_index', self.datetime_index)
        reindex = kwargs.get('reindex', self.reindex)
        date_check = kwargs.get('date_check', self.date_check)

        univ_window = dateutil.cut_window(
                DATES,
                dateutil.compliment_datestring(str(startdate), -1, date_check),
                dateutil.compliment_datestring(str(enddate), 1, date_check) if enddate is not None else None)
        index, columns = univ_window, SIDS
        df = pd.DataFrame(0, index, columns)
        df = df.add(self.series.astype(int), axis=1)

        self.comply(df, parent, False)
        return self.format(df, datetime_index, reindex)
Ejemplo n.º 13
0
    def filter(self, startdate, enddate=None, parent=None, **kwargs):
        datetime_index = kwargs.get('datetime_index', self.datetime_index)
        reindex = kwargs.get('reindex', self.reindex)
        date_check = kwargs.get('date_check', self.date_check)

        univ_window = dateutil.cut_window(
            DATES,
            dateutil.compliment_datestring(str(startdate), -1, date_check),
            dateutil.compliment_datestring(str(enddate), 1, date_check)
            if enddate is not None else None)
        index, columns = univ_window, SIDS
        df = pd.DataFrame(0, index, columns)
        df = df.add(self.series.astype(int), axis=1)

        self.comply(df, parent, False)
        return self.format(df, datetime_index, reindex)
Ejemplo n.º 14
0
    def generate_dates(startdate, enddate, parts=None):
        startdate, enddate = str(startdate), str(enddate)
        if enddate[:5].lower() == 'today':
            enddate = DATES[-1-int(enddate[6:])]

        dates = dateutil.cut_window(
                DATES,
                dateutil.compliment_datestring(str(startdate), -1, True),
                dateutil.compliment_datestring(str(enddate), 1, True)
                )
        if parts is None:
            return dates
        chksize = len(dates)/parts
        if len(dates) > chksize * parts:
            chksize += 1
        return [dates[i: i+chksize] for i in range(0, len(dates), chksize)]
Ejemplo n.º 15
0
Archivo: ops.py Proyecto: leeong05/orca
    def filter(self, startdate, enddate=None, parent=None, **kwargs):
        if 'datetime_index' not in kwargs:
            kwargs.update({'datetime_index': self.datetime_index})
        kwargs.update({'reindex': self.reindex})
        if 'date_check' not in kwargs:
            kwargs.update({'date_check': self.date_check})
        date_check = kwargs.get('date_check', self.date_check)

        univ_window = dateutil.cut_window(
                DATES,
                dateutil.compliment_datestring(str(startdate), -1, date_check),
                dateutil.compliment_datestring(str(enddate), 1, date_check) if enddate is not None else None)
        startdate, enddate = univ_window[0], univ_window[-1]
        res = [elem.filter(startdate, enddate=enddate, parent=parent, **kwargs) for elem in self._filters]
        seed = res.pop()
        for df in res:
            seed = seed | df
        return seed
Ejemplo n.º 16
0
 def fetch(self, *args, **kwargs):
     date_check = kwargs.get('date_check', self.date_check)
     enddate = kwargs.get('enddate', None)
     backdays = kwargs.get('backdays', 0)
     try:
         startdate = dateutil.compliment_datestring(str(args[0]), -1, date_check)
         dname = self._dname
         args = args[1:]
     except ValueError:
         startdate = dateutil.compliment_datestring(str(args[1]), -1, date_check)
         dname = args[0]
         args = args[2:]
     if args:
         enddate = args[0]
     window = dateutil.cut_window(
             DATES,
             startdate,
             dateutil.compliment_datestring(str(enddate), 1, date_check) if enddate is not None else None,
             backdays=backdays)
     return self.fetch_window(dname, window, **kwargs)
Ejemplo n.º 17
0
    def filter(self,
               startdate,
               enddate=None,
               parent=None,
               return_parent=False,
               **kwargs):
        datetime_index = kwargs.get('datetime_index', self.datetime_index)
        reindex = kwargs.get('reindex', self.reindex)
        date_check = kwargs.get('date_check', self.date_check)

        univ_window = dateutil.cut_window(
            DATES,
            dateutil.compliment_datestring(str(startdate), -1, date_check),
            dateutil.compliment_datestring(str(enddate), 1, date_check)
            if enddate is not None else None)
        si, ei = map(DATES.index, [univ_window[0], univ_window[-1]])
        data_window = DATES[si - self.delay - (self.window - 1):ei -
                            self.delay + 1]

        dfs = []
        for fetcher, dname in self.datas:
            df = fetcher.fetch_window(dname, data_window)
            df.index = DATES[si - (self.window - 1):ei + 1]
            dfs.append(df)
        df = self.synth(*dfs)

        parent = self.comply(df, parent)

        df = self.rule(self.window)(df)
        self.comply(df, parent, False)
        df = df.iloc[self.window - 1:]
        df.index = univ_window
        if return_parent is True:
            return (self.format(parent,
                                datetime_index=datetime_index,
                                reindex=reindex),
                    self.format(df,
                                datetime_index=datetime_index,
                                reindex=reindex))
        return self.format(df, datetime_index=datetime_index, reindex=reindex)
Ejemplo n.º 18
0
    def filter(self, startdate, enddate=None, parent=None, **kwargs):
        if 'datetime_index' not in kwargs:
            kwargs.update({'datetime_index': self.datetime_index})
        kwargs.update({'reindex': self.reindex})
        if 'date_check' not in kwargs:
            kwargs.update({'date_check': self.date_check})
        date_check = kwargs.get('date_check', self.date_check)

        univ_window = dateutil.cut_window(
            DATES,
            dateutil.compliment_datestring(str(startdate), -1, date_check),
            dateutil.compliment_datestring(str(enddate), 1, date_check)
            if enddate is not None else None)
        startdate, enddate = univ_window[0], univ_window[-1]
        res = [
            elem.filter(startdate, enddate=enddate, parent=parent, **kwargs)
            for elem in self._filters
        ]
        seed = res.pop()
        for df in res:
            seed = seed | df
        return seed
Ejemplo n.º 19
0
 def fetch(self, *args, **kwargs):
     date_check = kwargs.get('date_check', self.date_check)
     enddate = kwargs.get('enddate', None)
     backdays = kwargs.get('backdays', 0)
     try:
         startdate = dateutil.compliment_datestring(str(args[0]), -1,
                                                    date_check)
         dname = self._dname
         args = args[1:]
     except ValueError:
         startdate = dateutil.compliment_datestring(str(args[1]), -1,
                                                    date_check)
         dname = args[0]
         args = args[2:]
     if args:
         enddate = args[0]
     window = dateutil.cut_window(
         DATES,
         startdate,
         dateutil.compliment_datestring(str(enddate), 1, date_check)
         if enddate is not None else None,
         backdays=backdays)
     return self.fetch_window(dname, window, **kwargs)
Ejemplo n.º 20
0
 def test_cut_window_2(self):
     l = [2, 3, 5, 7, 11, 13, 17, 19]
     self.assertEqual(dateutil.cut_window(l, 9, None, 2),
                      [5, 7, 11, 13, 17, 19])