Ejemplo n.º 1
0
    def test__get_current_exposure__future_ticker(self):
        """
        Test the result of _get_current_exposure function for a future ticker in case of an empty portfolio and in case
        if a position for the given specific ticker exists.
        """
        expected_current_exposure_values = []
        # Set current specific ticker to ExampleZ00 Comdty and open position in the portfolio for the current ticker
        self.future_ticker.get_current_specific_ticker.return_value = BloombergTicker("ExampleZ00 Comdty")
        futures_alpha_model_strategy = AlphaModelStrategy(self.ts, {self.alpha_model: [self.future_ticker]},
                                                          use_stop_losses=False)
        # In case of empty portfolio get_signal function should have current exposure set to OUT
        futures_alpha_model_strategy.on_before_market_open()
        expected_current_exposure_values.append(Exposure.OUT)
        self.alpha_model.get_signal.assert_called_with(self.future_ticker, Exposure.OUT)

        self.positions_in_portfolio = [Mock(spec=BacktestPosition, **{
            'contract.return_value': Contract("ExampleZ00 Comdty", "FUT", "SIM_EXCHANGE"),
            'quantity.return_value': 10,
            'start_time': str_to_date("2000-01-01")
        })]
        futures_alpha_model_strategy.on_before_market_open()
        self.alpha_model.get_signal.assert_called_with(self.future_ticker, Exposure.LONG)

        self.positions_in_portfolio = [BacktestPositionFactory.create_position(c) for c in (
            Contract("ExampleZ00 Comdty", "STK", "SIM_EXCHANGE"), Contract("ExampleZ00 Comdty", "STK", "SIM_EXCHANGE"))]
        self.assertRaises(AssertionError, futures_alpha_model_strategy.on_before_market_open)
Ejemplo n.º 2
0
    def test__adjust_number_of_open_positions__multiple_models_3(self, generate_close_orders):
        """
        Test description:
        - max number of positions is 1
        - portfolio contains position with contract ExampleZ00 Comdty
        - ExampleZ00 Comdty ane Example Ticker are traded by two independent alpha models
        - there is signal with suggested exposure LONG for Example Ticker and OUT for ExampleN00 Comdty
        - Expected output: Example Ticker suggested exposure will be changed to OUT
        """
        self.future_ticker.get_current_specific_ticker.return_value = BloombergTicker("ExampleN00 Comdty")
        alpha_model_2 = MagicMock()

        alpha_model_strategy = AlphaModelStrategy(self.ts, {
            self.alpha_model: [self.future_ticker],
            alpha_model_2: [BloombergTicker("Example Ticker")]
        }, use_stop_losses=False, max_open_positions=1)

        self.alpha_model.get_signal.return_value = Signal(self.future_ticker,
                                                          Exposure.OUT, 1)
        alpha_model_2.get_signal.return_value = Signal(BloombergTicker("Example Ticker"), Exposure.LONG, 1)

        self.positions_in_portfolio = [Mock(spec=BacktestPosition, **{
            'contract.return_value': Contract("ExampleZ00 Comdty", "FUT", "SIM_EXCHANGE"),
            'quantity.return_value': -10,
            'start_time': str_to_date("2000-01-01")
        })]
        alpha_model_strategy.on_before_market_open()
        self.ts.position_sizer.size_signals.assert_called_once()
        args, kwargs = self.ts.position_sizer.size_signals.call_args_list[0]
        signals, _ = args
        expected_signals = [Signal(self.future_ticker, Exposure.OUT, 1),
                            Signal(BloombergTicker("Example Ticker"), Exposure.OUT, 1)]
        self.assertCountEqual(signals, expected_signals)
Ejemplo n.º 3
0
    def test__adjust_number_of_open_positions_4(self):
        """
        Test description:
        - max number of positions is 1
        - portfolio contains position with contract ExampleZ00 Comdty
        - there is signal for ExampleZ00 Comdty with suggested exposure OUT and for Example Ticker - LONG
        - Expected output: Example Ticker will be changed to OUT
        """
        self.future_ticker.get_current_specific_ticker.return_value = BloombergTicker("AN01 Index")

        alpha_model_strategy = AlphaModelStrategy(self.ts,
                                                  {self.alpha_model: [BloombergTicker("ExampleZ00 Comdty"),
                                                                      BloombergTicker("Example Ticker")]},
                                                  use_stop_losses=False, max_open_positions=1)

        exposures = {
            BloombergTicker("ExampleZ00 Comdty"): Exposure.OUT,
            BloombergTicker("Example Ticker"): Exposure.LONG,
        }
        self.alpha_model.get_signal.side_effect = lambda ticker, _: Signal(ticker, exposures[ticker], 1)

        self.positions_in_portfolio = [Mock(spec=BacktestPosition, **{
            'contract.return_value': Contract("ExampleZ00 Comdty", "FUT", "SIM_EXCHANGE"),
            'quantity.return_value': -10,
            'start_time': str_to_date("2000-01-01")
        })]
        alpha_model_strategy.on_before_market_open()
        self.ts.position_sizer.size_signals.assert_called_once()
        args, kwargs = self.ts.position_sizer.size_signals.call_args_list[0]
        signals, _ = args
        expected_signals = [Signal(BloombergTicker("ExampleZ00 Comdty"), Exposure.OUT, 1),
                            Signal(BloombergTicker("Example Ticker"), Exposure.OUT, 1)]
        self.assertCountEqual(signals, expected_signals)
Ejemplo n.º 4
0
    def test__get_current_exposure__future_ticker_rolling(self, generate_close_orders):
        """
        Test the result of _get_current_exposure function for a future ticker in case if a position for an expired
        contract exists in portfolio and the rolling should be performed.
        """
        # Set the future ticker to point to a new specific ticker, different from the one in the position from portfolio
        self.future_ticker.get_current_specific_ticker.return_value = BloombergTicker("ExampleN01 Comdty")
        futures_alpha_model_strategy = AlphaModelStrategy(self.ts, {self.alpha_model: [self.future_ticker]},
                                                          use_stop_losses=False)
        self.positions_in_portfolio = [Mock(spec=BacktestPosition, **{
            'contract.return_value': Contract("ExampleZ00 Comdty", "FUT", "SIM_EXCHANGE"),
            'quantity.return_value': 10,
            'start_time': str_to_date("2000-01-01")
        })]
        futures_alpha_model_strategy.on_before_market_open()

        self.alpha_model.get_signal.assert_called_once_with(self.future_ticker, Exposure.LONG)

        self.positions_in_portfolio = [Mock(spec=BacktestPosition, **{
            'contract.return_value': Contract("ExampleZ00 Comdty", "FUT", "SIM_EXCHANGE"),
            'quantity.return_value': 10,
            'start_time': str_to_date("2000-01-01")
        }), Mock(spec=BacktestPosition, **{
            'contract.return_value': Contract("ExampleZ00 Comdty", "FUT", "SIM_EXCHANGE"),
            'quantity.return_value': 20,
            'start_time': str_to_date("2000-01-02")
        })]
        self.assertRaises(AssertionError, futures_alpha_model_strategy.on_before_market_open)
Ejemplo n.º 5
0
    def test__adjust_number_of_open_positions_2(self, generate_close_orders):
        """
        Test description:
        - max number of positions is 1
        - portfolio contains position with contract ExampleZ00 Comdty
        - there is a signal with suggested exposure LONG for ExampleN01 Comdty
        - Expected output: ExampleN01 Comdty suggested exposure will be unchanged
        """
        self.future_ticker.get_current_specific_ticker.return_value = BloombergTicker(
            "ExampleN01 Comdty")
        alpha_model_strategy = AlphaModelStrategy(
            self.ts, {self.alpha_model: [self.future_ticker]},
            use_stop_losses=False,
            max_open_positions=1)
        self.alpha_model.get_signal.return_value = Signal(
            self.future_ticker, Exposure.LONG, 1)

        self.positions_in_portfolio = [
            Mock(spec=BacktestPosition,
                 **{
                     'contract.return_value':
                     Contract("ExampleZ00 Comdty", "FUT", "SIM_EXCHANGE"),
                     'quantity.return_value':
                     -10,
                     'start_time':
                     str_to_date("2000-01-01")
                 })
        ]
        alpha_model_strategy.on_before_market_open()
        self.ts.position_sizer.size_signals.assert_called_with(
            [Signal(self.future_ticker, Exposure.LONG, 1)], False)
Ejemplo n.º 6
0
    def test__get_current_exposure(self):
        """
        Test the result of _get_current_exposure function for a non-future ticker by inspecting the parameters passed to
        alpha models get_signal function.
        """

        alpha_model_strategy = AlphaModelStrategy(self.ts, {self.alpha_model: [self.ticker]},
                                                  use_stop_losses=False)
        # In case of empty portfolio get_signal function should have current exposure set to OUT
        alpha_model_strategy.on_before_market_open()
        self.alpha_model.get_signal.assert_called_with(self.ticker, Exposure.OUT)

        # Open long position in the portfolio
        self.positions_in_portfolio = [Mock(spec=BacktestPosition, **{
            'contract.return_value': Contract("Example Ticker", "STK", "SIM_EXCHANGE"),
            'quantity.return_value': 10,
            'start_time': str_to_date("2000-01-01")
        })]
        alpha_model_strategy.on_before_market_open()
        self.alpha_model.get_signal.assert_called_with(self.ticker, Exposure.LONG)

        # Open short position in the portfolio
        self.positions_in_portfolio = [Mock(spec=BacktestPosition, **{
            'contract.return_value': Contract("Example Ticker", "STK", "SIM_EXCHANGE"),
            'quantity.return_value': -10,
            'start_time': str_to_date("2000-01-01")
        })]
        alpha_model_strategy.on_before_market_open()
        self.alpha_model.get_signal.assert_called_with(self.ticker, Exposure.SHORT)

        # Verify if in case of two positions for the same contract an exception will be raised by the strategy
        self.positions_in_portfolio = [BacktestPositionFactory.create_position(c) for c in (
            Contract("Example Ticker", "STK", "SIM_EXCHANGE"), Contract("Example Ticker", "STK", "SIM_EXCHANGE"))]
        self.assertRaises(AssertionError, alpha_model_strategy.on_before_market_open)