Beispiel #1
0
    def get_simple_transform(self,
                             asset,
                             transform_name,
                             dt,
                             data_frequency,
                             bars=None):
        if transform_name == "returns":
            # returns is always calculated over the last 2 days, regardless
            # of the simulation's data frequency.
            hst = self.get_history_window([asset],
                                          dt,
                                          2,
                                          "1d",
                                          "price",
                                          ffill=True)[asset]

            return (hst.iloc[-1] - hst.iloc[0]) / hst.iloc[0]

        if bars is None:
            raise ValueError("bars cannot be None!")

        if data_frequency == "minute":
            freq_str = "1m"
            calculated_bar_count = int(
                self._get_minute_count_for_transform(dt, bars))
        else:
            freq_str = "1d"
            calculated_bar_count = bars

        price_arr = self.get_history_window([asset],
                                            dt,
                                            calculated_bar_count,
                                            freq_str,
                                            "price",
                                            ffill=True)[asset]

        if transform_name == "mavg":
            return nanmean(price_arr)
        elif transform_name == "stddev":
            return nanstd(price_arr, ddof=1)
        elif transform_name == "vwap":
            volume_arr = self.get_history_window([asset],
                                                 dt,
                                                 calculated_bar_count,
                                                 freq_str,
                                                 "volume",
                                                 ffill=True)[asset]

            vol_sum = nansum(volume_arr)

            try:
                ret = nansum(price_arr * volume_arr) / vol_sum
            except ZeroDivisionError:
                ret = np.nan

            return ret
Beispiel #2
0
    def get_simple_transform(self, asset, transform_name, dt, data_frequency,
                             bars=None):
        if transform_name == "returns":
            # returns is always calculated over the last 2 days, regardless
            # of the simulation's data frequency.
            hst = self.get_history_window(
                [asset], dt, 2, "1d", "price", ffill=True
            )[asset]

            return (hst.iloc[-1] - hst.iloc[0]) / hst.iloc[0]

        if bars is None:
            raise ValueError("bars cannot be None!")

        if data_frequency == "minute":
            freq_str = "1m"
            calculated_bar_count = int(self._get_minute_count_for_transform(
                dt, bars
            ))
        else:
            freq_str = "1d"
            calculated_bar_count = bars

        price_arr = self.get_history_window(
            [asset], dt, calculated_bar_count, freq_str, "price", ffill=True
        )[asset]

        if transform_name == "mavg":
            return nanmean(price_arr)
        elif transform_name == "stddev":
            return nanstd(price_arr, ddof=1)
        elif transform_name == "vwap":
            volume_arr = self.get_history_window(
                [asset], dt, calculated_bar_count, freq_str, "volume",
                ffill=True
            )[asset]

            vol_sum = nansum(volume_arr)

            try:
                ret = nansum(price_arr * volume_arr) / vol_sum
            except ZeroDivisionError:
                ret = np.nan

            return ret
Beispiel #3
0
    def compute(self, today, assets, out, data):
        ndays = data.shape[0]

        # Initialize weights array
        weights = arange(1, ndays + 1, dtype=float64_dtype).reshape(ndays, 1)

        # Compute normalizer
        normalizer = (ndays * (ndays + 1)) / 2

        # Weight the data
        weighted_data = data * weights

        # Compute weighted averages
        out[:] = nansum(weighted_data, axis=0) / normalizer
Beispiel #4
0
    def compute(self, today, assets, out, data):
        ndays = data.shape[0]

        # Initialize weights array
        weights = arange(1, ndays + 1, dtype=float64_dtype).reshape(ndays, 1)

        # Compute normalizer
        normalizer = (ndays * (ndays + 1)) / 2

        # Weight the data
        weighted_data = data * weights

        # Compute weighted averages
        out[:] = nansum(weighted_data, axis=0) / normalizer
Beispiel #5
0
 def compute(self, today, assets, out, close, volume):
     out[:] = nansum(close * volume, axis=0) / len(close)
Beispiel #6
0
 def compute(self, today, assets, out, base, weight):
     out[:] = nansum(base * weight, axis=0) / nansum(weight, axis=0)
Beispiel #7
0
 def compute(self, today, assets, out, base, weight):
     out[:] = nansum(base * weight, axis=0) / nansum(weight, axis=0)
Beispiel #8
0
 def compute(self, today, assets, out, close, volume):
     out[:] = nansum(close * volume, axis=0) / len(close)