Definitely not efficient, but I manually do it by saving all the trades and then iterating through them in post processing:
def notify_trade(self,trade):
self.trades.setdefault(self.data._name, [] ).append(trade)
trades_dict:dict = {}
for trade in result.trades.get(symbol, []):
if trade.status == 1:
trades_dict[trade.baropen] = {'open': {'size': trade.size, 'price': trade.price, 'value': trade.value } }
elif trade.status == 2:
size = trades_dict.get(trade.baropen).get('open').get('size')
open_price = trades_dict.get(trade.baropen).get('open').get('price')
open_value = trades_dict.get(trade.baropen).get('open').get('value')
close_value = open_value + trade.pnl
close_price = close_value / size
open_commission = min ( max (min_commision, (size * commission_rate_per_share) ), ( max_commision_pct * size * open_price ) )
close_commission = min ( max (min_commision, (size * commission_rate_per_share) ), ( max_commision_pct * close_value ) )
close_fee = close_value * trns_fee_pct
close_fee += min ( (size * finra_sell_fee_per_share) , finra_max_fee_amount)
Interactive Brokers has a size based commission with a min and max limit. There is also a value based fee assessed on closing a position. This only works with opening and closing a position in its entirety.