Skip to content

Backtest Optimiser Constraint Function - syntax & structure query #335

Answered by kernc
KitHaywood asked this question in Q&A
Discussion options

You must be logged in to vote

With constraint= parameter, you merely constrain the input variables' value search space. You don't know anything about the number of trades in advance.

What you're looking for is a customized maximize= function. To optimize the strategy in such a way that only between three and ten trades are made, something like this:

def maximize_func(stats):
    return stats['Equity Final [$]'] if 3 <= stats['# Trades'] <= 10 else 0

bt = Backtest(...)
bt.optimize(..., maximize=maximize_func)

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@KitHaywood
Comment options

Answer selected by KitHaywood
Comment options

You must be logged in to vote
3 replies
@kernc
Comment options

kernc Jan 3, 2023
Maintainer

@kiann00
Comment options

@kiann00
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants