qstrader icon indicating copy to clipboard operation
qstrader copied to clipboard

How to get total portfolio value on one bar?

Open easytrader opened this issue 8 years ago • 1 comments

It always faces the situation. If today is 2016/9/29 and I want to sell 100 shares of goog(goog price is 781). So my total portfolio value is 100*781=78100. If I want to use all my money to buy apple(appl price is 113). I should sell 100 shares of goog and I can buy 78100/113 shares of appl.

So could it add a api for get this bar's total portfolio value? Or it can add order_percent api that can oder the percentage of total portfolio value.

Thank you.

while self.price_handler.continue_backtest:
    try:
        event = self.events_queue.get(False)
    except queue.Empty:
        self.price_handler.stream_next()
    else:
        if event is not None:
            if event.type == EventType.TICK or event.type == EventType.BAR:
                self.cur_time = event.time
                self.strategy.calculate_signals(event)
                self.portfolio_handler.update_portfolio_value()
                self.statistics.update(event.time, self.portfolio_handler)
            elif event.type == EventType.SIGNAL:
                self.portfolio_handler.on_signal(event)
            elif event.type == EventType.ORDER:
                self.execution_handler.execute_order(event)
            elif event.type == EventType.FILL:
                self.portfolio_handler.on_fill(event)
            else:
                raise NotImplemented("Unsupported event.type '%s'" % event.type)

easytrader avatar Sep 29 '16 03:09 easytrader

My impression is that self.equity holds the total portfolio value. Take a look at update_portfolio() method. The equity field is calculated by adding:

  • initial cash value,
  • realised P&L of all closed positions,
  • realised P&L of all open positions,
  • unrealiesd P&L of all open positions.

nme01 avatar Oct 16 '17 09:10 nme01