Skip to content
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

Allow for cash to be deposited or withdrawn during a backtest #925

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ def _check_params(self, params):
"can be optimized or run with.")
setattr(self, k, v)
return params

def deposit_cash(self, amount):
return self._broker.deposit_cash(amount)

def withdraw_cash(self, amount):
return self._broker.withdraw_cash(amount)

def I(self, # noqa: E743
func: Callable, *args,
Expand Down Expand Up @@ -721,6 +727,16 @@ def __init__(self, *, data, cash, commission, margin,

def __repr__(self):
return f'<Broker: {self._cash:.0f}{self.position.pl:+.1f} ({len(self.trades)} trades)>'

def deposit_cash(self, amount: float):
self._cash += amount
return self._cash

def withdraw_cash(self, amount: float):
if amount > self._cash:
return self._cash
self.cash -= amount
return self._cash

def new_order(self,
size: float,
Expand Down