Repository: quantopian/zipline
File: zipline/finance/controls.py (Python)

Code before:
                 _algo_datetime,
                 _algo_current_data):
        """
        Fail if the leverage is greater than the allowed leverage.
        """
        if _account.leverage > self.max_leverage:
            self.fail()


class MinLeverage(AccountControl):
    """
    AccountControl representing a limit on the minimum leverage allowed
    by the algorithm after a threshold period of time.
    """

    def __init__(self, min_leverage, deadline):
        """
        max_leverage is the gross leverage in decimal form. For example,
        2, limits an algorithm to trading at most double the account value.
        """
        super(MinLeverage, self).__init__(min_leverage=min_leverage,
                                          deadline=deadline)
        self.min_leverage = min_leverage
        self.deadline = deadline

        if min_leverage is None:
            raise ValueError(
                "Must supply min_leverage"
            )

        if min_leverage < 0:
            raise ValueError(
                "min_leverage must be positive"
            )

        if deadline is None:
            raise ValueError(
                "Must supply deadline"
            )

    def validate(self,
                 _portfolio,
                 _account,
                 _algo_datetime,
                 _algo_current_data):
        """
        Make validation checks if we are after the deadline.
        Fail if the leverage is less than the min leverage.
        """
        if _algo_datetime > self.deadline and \
           _account.leverage < self.min_leverage:

Review comment (suggestion):
You might want to look at using the decorators in `zipline.utils.input_validation` to implement these checks. I think `expect_types(min_leverage=(int, float), deadline=datetime)` and `expect_strictly_bounded(min_leverage=(0, None))` would capture the behavior we want here.  If you do end up using those functions, you probably want to pass `__funcname='MinLeverage'` so that the generated error messages reference `MinLeverage` instead of `__init__`.

Code after:
                "max_leverage must be positive"
            )

    def validate(self,
                 _portfolio,
                 _account,
                 _algo_datetime,
                 _algo_current_data):
        """
        Fail if the leverage is greater than the allowed leverage.
        """
        if _account.leverage > self.max_leverage:
            self.fail()


class MinLeverage(AccountControl):
    """AccountControl representing a limit on the minimum leverage allowed
    by the algorithm after a threshold period of time.

    Parameters
    ----------
    min_leverage : float
        The gross leverage in decimal form.
    deadline : datetime
        The date the min leverage must be achieved by.

    For example, min_leverage=2 limits an algorithm to trading at minimum
    double the account value by the deadline date.
    """

    @expect_types(
        __funcname='MinLeverage',
        min_leverage=(int, float),
        deadline=datetime
    )
    @expect_bounded(__funcname='MinLeverage', min_leverage=(0, None))
    def __init__(self, min_leverage, deadline):
        super(MinLeverage, self).__init__(min_leverage=min_leverage,
                                          deadline=deadline)
        self.min_leverage = min_leverage
        self.deadline = deadline

    def validate(self,
                 _portfolio,
                 account,
                 algo_datetime,
                 _algo_current_data):
        """
        Make validation checks if we are after the deadline.
        Fail if the leverage is less than the min leverage.
        """