コード例 #1
0
ファイル: data_portal.py プロジェクト: zhuo2015/zipline
    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
コード例 #2
0
ファイル: data_portal.py プロジェクト: luca-s/zipline
    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
コード例 #3
0
ファイル: basic.py プロジェクト: t330883522/zipline
    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
コード例 #4
0
ファイル: technical.py プロジェクト: chrisvasquez/zipline
    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
コード例 #5
0
ファイル: technical.py プロジェクト: MattMing/zipline
 def compute(self, today, assets, out, close, volume):
     out[:] = nansum(close * volume, axis=0) / len(close)
コード例 #6
0
ファイル: technical.py プロジェクト: MattMing/zipline
 def compute(self, today, assets, out, base, weight):
     out[:] = nansum(base * weight, axis=0) / nansum(weight, axis=0)
コード例 #7
0
ファイル: basic.py プロジェクト: t330883522/zipline
 def compute(self, today, assets, out, base, weight):
     out[:] = nansum(base * weight, axis=0) / nansum(weight, axis=0)
コード例 #8
0
ファイル: basic.py プロジェクト: t330883522/zipline
 def compute(self, today, assets, out, close, volume):
     out[:] = nansum(close * volume, axis=0) / len(close)