Mismatch unrealized pnl in broker and in strategies, need help
-
Hello. I have two strategies and need to calculate unrealized pnl separately for each strategy. I made this functionality with Analyzers:
- One keeps strategies' positions:
class Positions(bt.Analyzer): def start(self): self._positions = defaultdict(Position) def notify_order(self, order): if order.status not in [Order.Partial, Order.Completed]: return # It's not an execution pos = self._positions[order.data] for exbit in order.executed.iterpending(): if exbit is None: break # end of pending reached pos.update(exbit.size, exbit.price)
- Second one calculates unrealized pnl for each strategy:
class Unrealized(bt.Analyzer): def start(self): self._positions = self.strategy.analyzers.positions._positions self.unrealized = OrderedDict() def next(self): unrealized = 0.0 for data in self._positions: comminfo = self.strategy.broker.getcommissioninfo(data) position = self._positions[data] dunrealized = comminfo.profitandloss(position.size, position.price, data.close[0]) unrealized += dunrealized dtkey = self.strategy.datetime.datetime() self.unrealized[dtkey] = unrealized def get_analysis(self): return self.unrealized
- Another one is for storing cerebro.broker._unrealized for all positions from all strategies:
class BrokerValue(bt.Analyzer): def start(self): self.unrealized = OrderedDict() def next(self): dtkey = self.strategy.datetime.datetime() self.unrealized[dtkey] = self.strategy.broker._unrealized def get_analysis(self): return self.unrealized
It works well for one strategy in cerebro: unrealized pnl from analyzer BrokerValue is the same as from Unrealized analyzer. But for two strategies results from strat1.analyzers.unrealized + (plus) strat2.analyzers.unrealized is different with unrealized from cerebro.broker.
Here is screen with difference (orders makes in the beginings of each month): link textCan you please advice what may be wrong in my approach? And how to make it work properly.
I was really already exhausted with it.p.s. There are no any commissions schemes in cerebro.broker and each strategy can make orders in same instrument.
Thanks!
-
It's a little bit mysterious approach above, and the reason is in the next:
Example
We have two strategies and looking for Positions for each separately (added analyzer above), and also looking for Positions in BackBroker that summarized it. Open a position in strat1: 10 size by price 100, and open in strat2: 20 size by price 200. Same instrument in both, but there are may be different opening dates.Broker calculated price (as in position.update() logic): (10 * 100 + 20 * 200) / (10 + 20) = 166.(6) and position size 10 + 20. i.e. broker has size 30 by price 166.(6)
Then if in strategy 1 we have to partial close position with size 5, then:
strat1 position have: size 5 by price 100
strat2 position have same: size 20 by price 200 (no updating position).
and broker after updating has: size 25 by same price 166.(6)And now if we find average price for strat1 and strat2: (5 * 100 + 20 * 200) / (5 + 20) = 180 that obviously not equal that broker position store. Thats it.
-
@oalexandere said in Mismatch unrealized pnl in broker and in strategies, need help:
Can you please advice what may be wrong in my approach?
Probably that you believe that each strategy operates with different funds. They don't. The broker is a single account broker.
@oalexandere said in Mismatch unrealized pnl in broker and in strategies, need help:
And how to make it work properly.
If the strategies operate on the same assets (i.e.: they are antagonistic) there is no way.
If they operate on different assets, you can track the assets or use specific
tradeids
for the orders generated by each strategy.@oalexandere said in Mismatch unrealized pnl in broker and in strategies, need help:
strat1 position have: size 5 by price 100
strat2 position have same: size 20 by price 200 (no updating position).Wrong. The positions are the same for all strategies for a single asset.
-
@backtrader said in Mismatch unrealized pnl in broker and in strategies, need help:
If the strategies operate on the same assets (i.e.: they are antagonistic) there is no way.
Bad news for me. But anyway thanks for reply.
@backtrader said in Mismatch unrealized pnl in broker and in strategies, need help:
Wrong. The positions are the same for all strategies for a single asset.
Yes for broker it's true, I understand. But example was for situation with separatly storing positions for each strategy (with analyzer <class Positions> above). And then comparing it with Positions in broker.