feat: add current price and other info to DCA strategy detail

This commit is contained in:
Herculino Trotta
2024-11-15 22:24:07 -03:00
parent 7f6e6514d5
commit 2c15a284af
5 changed files with 98 additions and 37 deletions

View File

@@ -1,25 +1,16 @@
from django.db.models import Func, F, Value, DurationField, Case, When, DecimalField, Q
from django.db.models.functions import Abs, Extract
import datetime
from django.db.models import Func, F, Value, Case, When, DecimalField, Q
from django.db.models.functions import Extract
from django.utils import timezone
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from apps.currencies.models import ExchangeRate
from apps.currencies.models import Currency
from apps.currencies.models import ExchangeRate
def convert(amount, from_currency: Currency, to_currency: Currency, date=None):
if from_currency == to_currency:
return (
amount,
to_currency.prefix,
to_currency.suffix,
to_currency.decimal_places,
)
if date is None:
date = timezone.localtime(timezone.now())
def get_exchange_rate(
from_currency: Currency, to_currency: Currency, date: datetime.date
) -> ExchangeRate | None:
try:
exchange_rate = (
ExchangeRate.objects.filter(
@@ -41,13 +32,37 @@ def convert(amount, from_currency: Currency, to_currency: Currency, date=None):
)
if exchange_rate is None:
return None, None, None, None
return None
except ExchangeRate.DoesNotExist:
return None
else:
return exchange_rate
def convert(amount, from_currency: Currency, to_currency: Currency, date=None):
if from_currency == to_currency:
return (
amount * exchange_rate.effective_rate,
amount,
to_currency.prefix,
to_currency.suffix,
to_currency.decimal_places,
)
except ExchangeRate.DoesNotExist:
if date is None:
date = timezone.localtime(timezone.now())
exchange_rate = get_exchange_rate(
from_currency=from_currency, to_currency=to_currency, date=date
)
if exchange_rate is None:
return None, None, None, None
return (
amount * exchange_rate.effective_rate,
to_currency.prefix,
to_currency.suffix,
to_currency.decimal_places,
)

View File

@@ -7,7 +7,7 @@ from django.template.defaultfilters import date
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from apps.currencies.utils.convert import convert
from apps.currencies.utils.convert import convert, get_exchange_rate
class DCAStrategy(models.Model):
@@ -133,6 +133,18 @@ class DCAStrategy(models.Model):
"amounts_bought": amounts_bought,
}
def current_price(self):
exchange_rate = get_exchange_rate(
from_currency=self.payment_currency,
to_currency=self.target_currency,
date=timezone.localtime(timezone.now()),
)
if exchange_rate:
return exchange_rate.effective_rate, exchange_rate.date
else:
return None
class DCAEntry(models.Model):
strategy = models.ForeignKey(

View File

@@ -145,15 +145,6 @@ def strategy_detail(request, strategy_id):
"entries": entries,
"entries_data": entries_data,
"monthly_data": monthly_data,
"total_invested": strategy.total_invested(),
"total_received": strategy.total_received(),
"average_entry_price": strategy.average_entry_price(),
"total_entries": strategy.total_entries(),
"current_total_value": strategy.current_total_value(),
"total_profit_loss": strategy.total_profit_loss(),
"total_profit_loss_percentage": strategy.total_profit_loss_percentage(),
"investment_frequency": strategy.investment_frequency_data(),
"price_comparison_data": strategy.price_comparison_data(),
}
return render(request, "dca/fragments/strategy/details.html", context)

View File

@@ -1,7 +1,8 @@
{% load currency_display %}
<div class="{% if text_end %}text-end{% elif text_start %}text-start{% endif %}">
<div class="amount{% if color == 'grey' or color == "gray" %} tw-text-gray-500{% elif color == 'green' %} tw-text-green-400{% elif color == 'red' %} tw-text-red-400{% endif %}"
data-original-value="{% currency_display amount=amount prefix=prefix suffix=suffix decimal_places=decimal_places %}" data-amount="{{ amount|floatformat:"-40u" }}">
</div>
<span class="amount{% if color == 'grey' or color == "gray" %} tw-text-gray-500{% elif color == 'green' %} tw-text-green-400{% elif color == 'red' %} tw-text-red-400{% endif %}"
data-original-value="{% currency_display amount=amount prefix=prefix suffix=suffix decimal_places=decimal_places %}"
data-amount="{{ amount|floatformat:"-40u" }}">
</span><span>{{ slot }}</span>
</div>

View File

@@ -1,7 +1,27 @@
{% load i18n %}
<div class="container-fluid px-md-3 py-3 column-gap-5">
<div class="tw-text-3xl fw-bold font-monospace tw-w-full mb-3">
<div>{{ strategy.name }}</div>
<div class="d-lg-flex justify-content-between mb-3 w-100">
<div class="tw-text-3xl fw-bold font-monospace d-flex align-items-center">
{{ strategy.name }}
</div>
<div class="tw-text-sm text-lg-end mt-2 mt-lg-0">
<div class="mb-2">
<span class="badge rounded-pill text-bg-secondary">{{ strategy.payment_currency.name }}</span> x <span class="badge rounded-pill text-bg-secondary">{{ strategy.target_currency.name }}</span>
</div>
<div>
{% if strategy.current_price %}
<c-amount.display
:amount="strategy.current_price.0"
:prefix="strategy.payment_currency.prefix"
:suffix="strategy.payment_currency.suffix"
:decimal_places="strategy.payment_currency.decimal_places">
• {{ strategy.current_price.1|date:"SHORT_DATETIME_FORMAT" }}
</c-amount.display>
{% else %}
<div class="tw-text-red-400">{% trans "No exchange rate available" %}</div>
{% endif %}
</div>
</div>
</div>
<div class="row gy-3 gx-3">
@@ -108,6 +128,28 @@
</div>
<div class="col-xl-6 col">
<div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 gy-3 gx-3">
<div class="col">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">{% trans "Current value" %}</h5>
<div class="card-text">
{% if strategy.current_price %}
<c-amount.display
:amount="strategy.current_price.0"
:prefix="strategy.payment_currency.prefix"
:suffix="strategy.payment_currency.suffix"
:decimal_places="strategy.payment_currency.decimal_places">
</c-amount.display>
{% else %}
<div>{% trans "No exchange rate available" %}</div>
{% endif %}
</div>
</div>
<div class="card-footer text-body-secondary">
2 days ago
</div>
</div>
</div>
<div class="col">
<div class="card h-100">
<div class="card-body">
@@ -169,7 +211,7 @@
<div class="card-body">
<h5 class="card-title">{% trans "Total P/L" %}</h5>
<div
class="card-text {% if strategy.total_profit_loss >= 0 %}text-success{% else %}text-danger{% endif %}">
class="card-text {% if strategy.total_profit_loss >= 0 %}tw-text-green-400{% else %}tw-text-red-400{% endif %}">
<c-amount.display
:amount="strategy.total_profit_loss"
:prefix="strategy.payment_currency.prefix"
@@ -185,7 +227,7 @@
<div class="card-body">
<h5 class="card-title">{% trans "Total % P/L" %}</h5>
<div
class="card-text {% if strategy.total_profit_loss >= 0 %}text-success{% else %}text-danger{% endif %}">
class="card-text {% if strategy.total_profit_loss >= 0 %}tw-text-green-400{% else %}tw-text-red-400{% endif %}">
{{ strategy.total_profit_loss_percentage|floatformat:2 }}%
</div>
</div>