示例#1
0
    def test_add_transaction_for_known_crypto_asset(self, _):
        symbol = "ETH"
        na_exchange = stock_exchanges.ExchangeRepository().get_by_name(
            stock_exchanges.OTHER_OR_NA_EXCHANGE_NAME
        )
        asset_repository = assets.AssetRepository(exchange=na_exchange)

        asset = asset_repository.add_crypto(
            symbol=symbol,
            user=self.user,
        )

        response = self.client.post(
            reverse(self.VIEW_NAME),
            {
                "executed_at": "2021-03-04T00:00:00Z",
                "account": self.account.pk,
                "asset": asset.pk,
                "quantity": 10,
                "price": 3.15,
                "transaction_costs": 0,
                "local_value": 123.56,
                "value_in_account_currency": 123.33,
                "total_in_account_currency": 123.33,
                "currency": "EUR",
            },
        )
        self.assertEqual(response.status_code, 201)
    def test_assets_with_same_isin_multiple_currencies(self, mock):
        mock.return_value = SAME_ISIN_MULTIPLE_CURRENCIES_RESPONSE

        asset = stock_exchanges.get_or_create_asset(
            isin="IE00BK5BQT80",
            exchange=stock_exchanges.ExchangeRepository().get_by_code("LSE"),
            asset_defaults={"local_currency": "GBP"},
            add_untracked_if_not_found=False,
            user=None,
        )
        self.assertEqual(asset.currency, models.Currency.GBP)
示例#3
0
def import_transaction(
    account: models.Account,
    transaction_record: pd.Series,
    import_all_assets,
) -> Tuple[models.Transaction, bool]:
    executed_at = transaction_record["Datetime"]
    isin = transaction_record["ISIN"]
    local_value = transaction_record["Local value"]
    total_in_account_currency = transaction_record["Total"]
    value_in_account_currency = transaction_record["Value"]
    transaction_costs = transaction_record["Transaction costs"].astype(str)
    order_id = transaction_record["Order ID"]
    quantity = transaction_record["Quantity"]
    price = transaction_record["Price"]
    account_currency = transaction_record["Value currency"]
    local_currency = transaction_record["Local value currency"]
    product = transaction_record["Product"]

    if models.currency_enum_from_string(account_currency) != account.currency:
        raise CurrencyMismatch("Currency of import didn't match the account")
    exchange_mic = transaction_record["Venue"]
    exchange_ref = transaction_record["Reference"]
    try:
        exchange = stock_exchanges.ExchangeRepository().get(
            exchange_mic, exchange_ref)
    except Exception as e:
        logger.error(e)
        raise e

    def to_decimal(pd_f):
        return decimal.Decimal(pd_f.astype(str))

    transaction_costs = decimal.Decimal(transaction_costs)
    if transaction_costs.is_nan():
        transaction_costs = None
    return accounts.AccountRepository().add_transaction(
        account,
        isin=isin,
        exchange=exchange,
        executed_at=executed_at,
        quantity=to_decimal(quantity),
        price=to_decimal(price),
        transaction_costs=transaction_costs,
        local_value=to_decimal(local_value),
        value_in_account_currency=to_decimal(value_in_account_currency),
        total_in_account_currency=to_decimal(total_in_account_currency),
        order_id=order_id,
        asset_defaults={
            "local_currency": local_currency,
            "name": product
        },
        import_all_assets=import_all_assets,
    )
示例#4
0
    def add_transaction_custom_asset(
        self,
        account: models.Account,
        symbol: str,
        currency: models.Currency,
        exchange: str,
        asset_type: models.AssetType,
        executed_at: datetime.datetime,
        quantity: decimal.Decimal,
        price: decimal.Decimal,
        transaction_costs: decimal.Decimal,
        local_value: decimal.Decimal,
        value_in_account_currency: decimal.Decimal,
        total_in_account_currency: decimal.Decimal,
        order_id: Optional[str] = None,
    ) -> models.Transaction:

        exchange_entity = stock_exchanges.ExchangeRepository().get_by_name(
            exchange)
        tracked = False
        if asset_type == models.AssetType.CRYPTO:
            tracked = prices.are_crypto_prices_available(symbol)
        asset, created = models.Asset.objects.get_or_create(
            symbol=symbol,
            exchange=exchange_entity,
            currency=currency,
            asset_type=asset_type,
            tracked=tracked,
            added_by=account.user if not tracked else None,
        )
        if tracked and created:
            prices.collect_prices(asset)

        position = self._get_or_create_position_for_asset(account, asset.pk)

        transaction, _ = self._add_transaction(
            account,
            position,
            executed_at,
            quantity,
            price,
            transaction_costs,
            local_value,
            value_in_account_currency,
            total_in_account_currency,
            order_id,
            custom_asset=not tracked,
        )
        return transaction
示例#5
0
    def add_transaction_crypto_asset(
        self,
        account: models.Account,
        symbol: str,
        executed_at: datetime.datetime,
        quantity: decimal.Decimal,
        price: decimal.Decimal,
        local_value: decimal.Decimal,
        value_in_account_currency: decimal.Decimal,
        total_in_account_currency: decimal.Decimal,
        transaction_costs: Optional[decimal.Decimal] = None,
        order_id: Optional[str] = None,
    ):

        na_exchange = stock_exchanges.ExchangeRepository().get_by_name(
            stock_exchanges.OTHER_OR_NA_EXCHANGE_NAME)
        asset_repository = assets.AssetRepository(exchange=na_exchange)
        user = account.user
        asset = asset_repository.add_crypto(
            symbol=symbol,
            user=user,
        )
        position = self._get_or_create_position_for_asset(account, asset.pk)

        return self._add_transaction(
            account,
            position,
            executed_at,
            quantity,
            price,
            transaction_costs,
            local_value,
            value_in_account_currency,
            total_in_account_currency,
            order_id,
            custom_asset=not asset.tracked,
        )