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

@@ -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)