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

Broker Cash, Strange Metric #493

Open
shaunpatterson opened this issue Oct 3, 2021 · 2 comments
Open

Broker Cash, Strange Metric #493

shaunpatterson opened this issue Oct 3, 2021 · 2 comments

Comments

@shaunpatterson
Copy link

Expected Behavior

Start with $1000
Buy $100 of stock
self._broker._cash should be $900.
Sell $150 of stock
self._broker._cash should be $1050

Actual Behavior

Start with $1000
Buy $100 of stock
self._broker._cash doesn't change
Sell $150 of stock
self._broker._cash is now $1050 (cash += closed_trade.pl)

I don't understand this behavior. The variable is named cash, but it's not really the current cash value.

If you want to make trades based on your actual available cash, self._broker.margin_available seems to be what you want... but that seems strange.

Am I missing something here?

@kernc
Copy link
Owner

kernc commented Oct 3, 2021

Can you rather show some code?

Note, everything prefixed with an underscore is, as customary, private API with no explicit guarantees.

@shaunpatterson
Copy link
Author

shaunpatterson commented Oct 4, 2021

   def _close_trade(self, trade: Trade, price: float, time_index: int):
        self.trades.remove(trade)
        if trade._sl_order:
            self.orders.remove(trade._sl_order)
        if trade._tp_order:
            self.orders.remove(trade._tp_order)

        self.closed_trades.append(trade._replace(exit_price=price, exit_bar=time_index))
        self._cash += trade.pl

    def _open_trade(self, price: float, size: int, sl: float, tp: float, time_index: int):
        trade = Trade(self, size, price, time_index)
        self.trades.append(trade)
        # Create SL/TP (bracket) orders.
        # Make sure SL order is created first so it gets adversarially processed before TP order
        # in case of an ambiguous tie (both hit within a single bar).
        # Note, sl/tp orders are inserted at the front of the list, thus order reversed.
        if tp:
            trade.tp = tp
        if sl:
            trade.sl = sl

Cash is only changed when the trade is closed.

I understand the underscore variables are private -- however, I found this because I was interested in creating an enhancement to plot the available cash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants