ranaroussi/quantstats

Asset Wise Transaction List - Profit Loss Account

Opened this issue · 0 comments

To have a asset wise transaction history - summary where we can see profit/loss for transactions asset wise, something along these lines - where every portfolio change can be analyzed for asset wise summary

def record_transaction(self, old_weights, new_weights, current_date, current_prices):
    for asset in self.portfolio:
        old_weight = old_weights.get(asset, 0)
        new_weight = new_weights.get(asset, 0)
        quantity_change = (new_weight - old_weight) * current_prices[asset]
        transaction = {
            'date': current_date,
            'asset': asset,
            'type': 'buy' if new_weight > old_weight else 'sell',
            'quantity': abs(quantity_change),
            'price_per_unit': current_prices[asset],
            'total_value': abs(quantity_change) * current_prices[asset],
            # Add other elements like fees, profit/loss, etc.
        }
        self.transaction_history.append(transaction)