Exemple #1
0
    def add_ledger_action(self, action: LedgerAction) -> None:
        # should never happen, should be stopped at the main loop
        assert action.timestamp <= self.query_end_ts, (
            'Ledger action time > query_end_ts found in processing')
        # calculate the profit currency rate
        if action.rate is None or action.rate_asset is None:
            rate = self.get_rate_in_profit_currency(action.asset,
                                                    action.timestamp)
        else:
            if action.rate_asset == self.profit_currency:
                rate = action.rate
            else:
                quote_rate = self.get_rate_in_profit_currency(
                    action.rate_asset, action.timestamp)
                rate = action.rate * quote_rate

        profit_loss = action.amount * rate
        account_for_action = (action.timestamp > self.query_start_ts
                              and action.action_type
                              in self.taxable_ledger_actions)
        log.debug(
            'Processing LedgerAction',
            sensitive_log=True,
            action=action,
            rate_used=rate,
            account_for_action=account_for_action,
        )
        if account_for_action is False:
            profit_loss = ZERO

        if action.is_profitable():
            self.ledger_actions_profit_loss += profit_loss
            self.cost_basis.obtain_asset(
                location=action.location,
                timestamp=action.timestamp,
                description=f'{str(action.action_type)}',
                asset=action.asset,
                amount=action.amount,
                rate=rate,
                fee_in_profit_currency=ZERO,
            )
        else:
            self.ledger_actions_profit_loss -= profit_loss

            result = self.cost_basis.reduce_asset_amount(
                asset=action.asset,
                amount=action.amount,
            )
            if not result:
                log.critical(
                    f'No documented buy found for {action.asset} before '
                    f'{self.csv_exporter.timestamp_to_date(action.timestamp)}',
                )

        if action.timestamp > self.query_start_ts:
            self.csv_exporter.add_ledger_action(
                action=action,
                profit_loss_in_profit_currency=profit_loss,
            )
Exemple #2
0
    def add_ledger_action(
        self,
        action: LedgerAction,
        profit_loss_in_profit_currency: FVal,
    ) -> None:
        if not self.create_csv:
            return

        self.ledger_actions_csv.append({
            'time':
            self.timestamp_to_date(action.timestamp),
            'type':
            str(action.action_type),
            'location':
            str(action.location),
            'asset':
            str(action.asset),
            'amount':
            str(action.amount),
            f'profit_loss_in_{self.profit_currency.symbol}':
            profit_loss_in_profit_currency,
        })

        paid_asset: Optional[Asset]
        received_asset: Optional[Asset]
        if action.is_profitable():
            paid_in_profit_currency = ZERO
            paid_in_asset = ZERO
            paid_asset = None
            received_asset = action.asset
            received_in_asset = action.amount
            received_in_profit_currency = profit_loss_in_profit_currency
        else:
            paid_in_profit_currency = profit_loss_in_profit_currency
            paid_in_asset = action.amount
            paid_asset = action.asset
            received_asset = None
            received_in_asset = AssetAmount(ZERO)
            received_in_profit_currency = ZERO

        self.add_to_allevents(
            event_type=EV_LEDGER_ACTION,
            location=action.location,
            paid_in_profit_currency=paid_in_profit_currency,
            paid_asset=paid_asset,
            paid_in_asset=paid_in_asset,
            received_asset=received_asset,
            received_in_asset=received_in_asset,
            taxable_received_in_profit_currency=received_in_profit_currency,
            total_received_in_profit_currency=received_in_profit_currency,
            timestamp=action.timestamp,
            link=action.link,
            notes=action.notes,
        )