Skip to content

Cash withdrawals and reinvestment #124

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

Closed
uberdeveloper opened this issue Aug 3, 2020 · 2 comments
Closed

Cash withdrawals and reinvestment #124

uberdeveloper opened this issue Aug 3, 2020 · 2 comments
Labels
question Not a bug, but a FAQ entry

Comments

@uberdeveloper
Copy link

uberdeveloper commented Aug 3, 2020

I wanted to simulate some realistic scenarios relating to systematic investment plan, cash withdrawals, and partial reinvestment.
Presently, I am manipulating the self._broker._cash attribute to this effect.

Is there any suggested approach?
I am more concerned about the execution rather than the returns as I am using attributes inside the Strategy class to calculate customized returns.

@kernc
Copy link
Owner

kernc commented Aug 4, 2020

Manipulating self._broker._cash seems reasonable for the time being.

Watch out for "margin call" liquidations if equity ever becomes negative:

@property
def equity(self) -> float:
return self._cash + sum(trade.pl for trade in self.trades)

@kernc kernc added the question Not a bug, but a FAQ entry label Aug 4, 2020
@uberdeveloper
Copy link
Author

This is the present workaround and it works fine.

class MyStrategy(Strategy):
    def init(self):
        # Anything above this capital in cash would be withdrawn
        self._initial_capital = 10000 
        # Everything else goes here

    def next(self):
        # After processing all logic
        
        # Withdraw only when there is excess
        if (self.equity > self._initial_capital) and (self._broker._cash > self._initial_capital):
            # Withdraw cash only when all positions are closed and everything is in cash
            if (self.position.size == 0):
                withdrawal = self._broker._cash - self._initial_capital
                self._broker._cash -= withdrawal

One big caveat is withdrawing cash when a position is already open.
Would post after experimenting this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a bug, but a FAQ entry
Projects
None yet
Development

No branches or pull requests

2 participants