Ejemplo n.º 1
0
 def _agged(self, times, data=None):
     """
     Get the aggregated data for the given times
     data
         intraday dataframe. If None, self.datas[0] is picked
     times
         list of 2-tuples with start and end times
     Note
     -----
     1) For each time tuple given in list, data is aggregated
     in OHLC form and added to a dataframe
     2) Columns are numbered based on the order of the times 
     list so that the first tuple is named 0, the next 1 and so on 
     3) Expect a date and timestamp column in the list
     """
     from fastbt.utils import recursive_merge
     agged = {'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last'}
     dfs = []
     if not (data):
         data = self.datas[0]
     for i, (s, e) in enumerate(times):
         temp = data.set_index('timestamp').between_time(
             s, e).groupby('date').agg(agged)
         columns = {
             x: '{x}{i}'.format(x=x, i=i)
             for x in ['open', 'high', 'low', 'close']
         }
         temp.rename(columns=columns, inplace=True)
         dfs.append(temp)
     return recursive_merge(dfs, on=['date']).reset_index()
Ejemplo n.º 2
0
 def corr(self, names=[], column='pnl'):
     """
     Create a correlation matrix
     names
         names are the names of data sources to be merged
         by default, all data sources are used
     column
         column name to merge
     """ 
     keys = self._sources.keys()
     if not(names):
         names = keys
     # Rename columns for better reporting
     collect = [] 
     for name in names:
         src = self._sources.get(name)
         if src is not None:
            cols = ['date', column]
            tmp = src[cols].rename(columns={column:name})
            collect.append(tmp)
     if len(collect) > 0:
         frame = recursive_merge(collect, on=['date'], how='outer').fillna(0)
         return frame.corr()
     else:
         return []
Ejemplo n.º 3
0
    def _agged(self, times, data=None):
        """
        Get the aggregated data for the given times
        data
            intraday dataframe. If None, self.datas[0] is picked
        times
            list of 2-tuples with start and end times
        Note
        -----
        1) For each time tuple given in list, data is aggregated
        in OHLC form and added to a dataframe
        2) Columns are numbered based on the order of the times
        list so that the first tuple is named 0, the next 1 and so on
        3) Expect a date and timestamp column in the list
        """
        from fastbt.utils import recursive_merge

        agged = {"open": "first", "high": "max", "low": "min", "close": "last"}
        dfs = []
        if not (data):
            data = self.datas[0]
        for i, (s, e) in enumerate(times):
            temp = (
                data.set_index("timestamp")
                .between_time(s, e)
                .groupby("date")
                .agg(agged)
            )
            columns = {
                x: "{x}{i}".format(x=x, i=i) for x in ["open", "high", "low", "close"]
            }
            temp.rename(columns=columns, inplace=True)
            dfs.append(temp)
        return recursive_merge(dfs, on=["date"]).reset_index()
Ejemplo n.º 4
0
 def get_column(self, column='pnl', on='date', how='outer'):
     """
     Get a single column from all the dataframes and merge
     them into a single dataframe
     """
     names = self._sources.keys()
     # Rename columns for better reporting
     collect = [] 
     for name in names:
         src = self._sources.get(name)
         if src is not None:
            cols = ['date', column]
            tmp = src[cols].rename(columns={column:name})
            collect.append(tmp)
     if len(collect) > 0:
         frame = recursive_merge(collect, on=['date'], how='outer').fillna(0)
         return frame