Skip to content

Reduce memory footprint #884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,16 +1567,20 @@ def _optimize_sambo() -> Union[pd.Series,
else:
dimensions.append(values.tolist())

# Avoid recomputing re-evaluations:
memoized_run = lru_cache()(lambda tup: self.run(**dict(tup))) # XXX: Reeval if this needed?
# Avoid recomputing re-evaluations
@lru_cache()
def memoized_run(tup):
nonlocal maximize, self
stats = self.run(**dict(tup))
return -maximize(stats)

progress = iter(_tqdm(repeat(None), total=max_tries, leave=False, desc='Backtest.optimize'))
_names = tuple(kwargs.keys())

def objective_function(x):
nonlocal progress, memoized_run, constraint, _names
next(progress)
res = memoized_run(tuple(zip(_names, x)))
value = -maximize(res)
value = memoized_run(tuple(zip(_names, x)))
return 0 if np.isnan(value) else value

def cons(x):
Expand Down