Example #1
0
    def test_bad_inputs(self):
        template = (
            "MACDSignal() expected a value greater than or equal to 1"
            " for argument %r, but got 0 instead."
        )
        with self.assertRaises(ValueError) as e:
            MovingAverageConvergenceDivergenceSignal(fast_period=0)
        self.assertEqual(template % 'fast_period', str(e.exception))

        with self.assertRaises(ValueError) as e:
            MovingAverageConvergenceDivergenceSignal(slow_period=0)
        self.assertEqual(template % 'slow_period', str(e.exception))

        with self.assertRaises(ValueError) as e:
            MovingAverageConvergenceDivergenceSignal(signal_period=0)
        self.assertEqual(template % 'signal_period', str(e.exception))

        with self.assertRaises(ValueError) as e:
            MovingAverageConvergenceDivergenceSignal(
                fast_period=5,
                slow_period=4,
            )

        expected = (
            "'slow_period' must be greater than 'fast_period', but got\n"
            "slow_period=4, fast_period=5"
        )
        self.assertEqual(expected, str(e.exception))
Example #2
0
    def test_moving_average_convergence_divergence(self,
                                                   seed,
                                                   fast_period,
                                                   slow_period,
                                                   signal_period):
        rng = RandomState(seed)

        nassets = 3

        macd = MovingAverageConvergenceDivergenceSignal(
            fast_period=fast_period,
            slow_period=slow_period,
            signal_period=signal_period,
        )

        today = pd.Timestamp('2016', tz='utc')
        assets = pd.Index(np.arange(nassets))
        out = np.empty(shape=(nassets,), dtype=np.float64)
        close = rng.rand(macd.window_length, nassets)

        macd.compute(
            today,
            assets,
            out,
            close,
            fast_period,
            slow_period,
            signal_period,
        )

        close_df = pd.DataFrame(close)
        fast_ewma = self.expected_ewma(
            close_df,
            fast_period,
        )
        slow_ewma = self.expected_ewma(
            close_df,
            slow_period,
        )
        signal_ewma = self.expected_ewma(
            fast_ewma - slow_ewma,
            signal_period
        )

        # Everything but the last row should be NaN.
        self.assertTrue(signal_ewma.iloc[:-1].isnull().all().all())

        # We're testing a single compute call, which we expect to be equivalent
        # to the last row of the frame we calculated with pandas.
        expected_signal = signal_ewma.values[-1]

        np.testing.assert_almost_equal(
            out,
            expected_signal,
            decimal=8
        )
Example #3
0
def make_pipeline():
    all_assets_filter = (USEquityPricing.close.latest > 0)
    returns_5 = Returns(window_length=5)
    rsi = RSI(inputs=[USEquityPricing.close])
    macd = MovingAverageConvergenceDivergenceSignal(mask=all_assets_filter)
    ema = ExponentialWeightedMovingAverage(mask=all_assets_filter,
                                           inputs=[USEquityPricing.close],
                                           window_length=30,
                                           decay_rate=(1 - (2.0 / (1 + 15.0))))
    mean_5 = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                 window_length=5,
                                 mask=all_assets_filter)
    mean_10 = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                  window_length=10,
                                  mask=all_assets_filter)
    bb = BollingerBands(inputs=[USEquityPricing.close], window_length=20, k=2)
    return Pipeline(
        columns={
            'returns_5': returns_5,
            'RSI': rsi,
            'MACD': macd,
            'EMA': ema,
            'SMA_5': mean_5,
            'SMA_10': mean_10,
            'bb_upper': bb.upper,
            'bb_middle': bb.middle,
            'bb_lower': bb.lower
        },
        screen=all_assets_filter,
    )
Example #4
0
    def test_bad_inputs(self):
        template = ("MACDSignal() expected a value greater than or equal to 1"
                    " for argument %r, but got 0 instead.")
        with pytest.raises(ValueError,
                           match=re.escape(template % "fast_period")):
            MovingAverageConvergenceDivergenceSignal(fast_period=0)

        with pytest.raises(ValueError,
                           match=re.escape(template % "slow_period")):
            MovingAverageConvergenceDivergenceSignal(slow_period=0)

        with pytest.raises(ValueError,
                           match=re.escape(template % "signal_period")):
            MovingAverageConvergenceDivergenceSignal(signal_period=0)

        err_msg = (
            "'slow_period' must be greater than 'fast_period', but got\n"
            "slow_period=4, fast_period=5")
        with pytest.raises(ValueError, match=err_msg):
            MovingAverageConvergenceDivergenceSignal(
                fast_period=5,
                slow_period=4,
            )
Example #5
0
    def test_MACD_window_length_generation(self, seed):
        rng = RandomState(seed)

        signal_period = rng.randint(1, 90)
        fast_period = rng.randint(signal_period + 1, signal_period + 100)
        slow_period = rng.randint(fast_period + 1, fast_period + 100)
        ewma = MovingAverageConvergenceDivergenceSignal(
            fast_period=fast_period,
            slow_period=slow_period,
            signal_period=signal_period,
        )
        assert_equal(
            ewma.window_length,
            slow_period + signal_period - 1,
        )