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

balance_locked & balance_free #249

Closed
jpmediadev opened this issue Mar 18, 2021 · 4 comments
Closed

balance_locked & balance_free #249

jpmediadev opened this issue Mar 18, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@jpmediadev
Copy link
Contributor

incorrectly calculated - always equal to the initial value of the account, regardless of positions and deals

class Testing(TradingStrategy):

....
self.free.append(self.execution.accounts()[0].balance_free(USDT))
self.locked.append(self.execution.accounts()[0].balance_locked(USDT))
self.balance.append(self.execution.accounts()[0].balance(USDT))

@cjdsellers
Copy link
Member

cjdsellers commented Mar 18, 2021

So works correctly for live trading. The free and locked balances are parsed from the live stream.

However for backtesting the SimulatedExchange isn't tracking margin. An Instrument object is able to calculation initial and maintenance margins, and so the calculation can be made at the exchange easily.

However how to track and report this info, as well as how it might relate to the AccountState event I'm still thinking about...

@jpmediadev
Copy link
Contributor Author

it's great that you do without a backtest and test on a real account, so you've already found your alpha)

SimulatedExchange must do some calculations as a broker, how is an order with a stop loss for a backtest considered now?

we need to calculate the current value of the position and its changes at the kernel level on every data tick

in my opinion, in the simplest case, is need to calculate:
position.unrealized_pnl(price_of_data_type) or current_order_position_cost for all open positions, at the level of cython core (if it is doing at the level of a user def strategy this will greatly slow down the speed of the entire system) and after: init equity - (positions + holded in orders)

@cjdsellers
Copy link
Member

So currently unrealized pnl isn't calculated on every tick as that would be quite inefficient. This also relates to your other issue about tracking a max and min unrealized pnl over a positions life.

My thoughts right now is to provide this functionality with an optional component which can subscribe to position events and also ticks, possibly it could update in time snapshots or when certain thresholds are crossed.

@cjdsellers cjdsellers added the bug Something isn't working label Mar 19, 2021
@cjdsellers
Copy link
Member

I'm going to close this and roll the discussion into #248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants