feat: initial work for DCA tool

This commit is contained in:
Herculino Trotta
2024-11-12 00:13:27 -03:00
parent eab04ad298
commit 7ebe3b6b5b
13 changed files with 576 additions and 0 deletions

View File

@@ -71,6 +71,7 @@ INSTALLED_APPS = [
"django_cotton",
"apps.rules.apps.RulesConfig",
"apps.calendar_view.apps.CalendarViewConfig",
"apps.dca.apps.DcaConfig",
]
MIDDLEWARE = [

View File

@@ -45,4 +45,5 @@ urlpatterns = [
path("", include("apps.currencies.urls")),
path("", include("apps.rules.urls")),
path("", include("apps.calendar_view.urls")),
path("", include("apps.dca.urls")),
]

0
app/apps/dca/__init__.py Normal file
View File

7
app/apps/dca/admin.py Normal file
View File

@@ -0,0 +1,7 @@
from django.contrib import admin
from apps.dca.models import DCAStrategy, DCAEntry
# Register your models here.
admin.site.register(DCAStrategy)
admin.site.register(DCAEntry)

6
app/apps/dca/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class DcaConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.dca"

60
app/apps/dca/forms.py Normal file
View File

@@ -0,0 +1,60 @@
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Row, Column
from django.utils.translation import gettext_lazy as _
from .models import DCAStrategy, DCAEntry
from apps.common.widgets.decimal import ArbitraryDecimalDisplayNumberInput
class DCAStrategyForm(forms.ModelForm):
class Meta:
model = DCAStrategy
fields = ["name", "target_currency", "payment_currency", "notes"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
"name",
Row(
Column("target_currency", css_class="form-group col-md-6"),
Column("payment_currency", css_class="form-group col-md-6"),
),
"notes",
)
class DCAEntryForm(forms.ModelForm):
class Meta:
model = DCAEntry
fields = [
"date",
"amount_paid",
"amount_received",
"expense_transaction",
"income_transaction",
"notes",
]
widgets = {
"amount_paid": ArbitraryDecimalDisplayNumberInput(decimal_places=8),
"amount_received": ArbitraryDecimalDisplayNumberInput(decimal_places=8),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
"date",
Row(
Column("amount_paid", css_class="form-group col-md-6"),
Column("amount_received", css_class="form-group col-md-6"),
),
Row(
Column("expense_transaction", css_class="form-group col-md-6"),
Column("income_transaction", css_class="form-group col-md-6"),
),
"notes",
)

View File

@@ -0,0 +1,54 @@
# Generated by Django 5.1.2 on 2024-11-12 01:41
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('currencies', '0006_currency_exchange_currency'),
('transactions', '0022_rename_paused_recurringtransaction_is_paused'),
]
operations = [
migrations.CreateModel(
name='DCAStrategy',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, verbose_name='Name')),
('notes', models.TextField(blank=True, null=True, verbose_name='Notes')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('payment_currency', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='dca_payment_strategies', to='currencies.currency', verbose_name='Payment Currency')),
('target_currency', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='dca_target_strategies', to='currencies.currency', verbose_name='Target Currency')),
],
options={
'verbose_name': 'DCA Strategy',
'verbose_name_plural': 'DCA Strategies',
'ordering': ['-created_at'],
},
),
migrations.CreateModel(
name='DCAEntry',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateField(verbose_name='Date')),
('amount_paid', models.DecimalField(decimal_places=8, max_digits=20, verbose_name='Amount Paid')),
('amount_received', models.DecimalField(decimal_places=8, max_digits=20, verbose_name='Amount Received')),
('notes', models.TextField(blank=True, null=True, verbose_name='Notes')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('expense_transaction', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='dca_expense_entries', to='transactions.transaction', verbose_name='Expense Transaction')),
('income_transaction', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='dca_income_entries', to='transactions.transaction', verbose_name='Income Transaction')),
('strategy', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='entries', to='dca.dcastrategy', verbose_name='Strategy')),
],
options={
'verbose_name': 'DCA Entry',
'verbose_name_plural': 'DCA Entries',
'ordering': ['-date'],
},
),
]

View File

140
app/apps/dca/models.py Normal file
View File

@@ -0,0 +1,140 @@
from decimal import Decimal
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from apps.currencies.utils.convert import convert
class DCAStrategy(models.Model):
name = models.CharField(max_length=255, verbose_name=_("Name"))
target_currency = models.ForeignKey(
"currencies.Currency",
verbose_name=_("Target Currency"),
on_delete=models.PROTECT,
related_name="dca_target_strategies",
)
payment_currency = models.ForeignKey(
"currencies.Currency",
verbose_name=_("Payment Currency"),
on_delete=models.PROTECT,
related_name="dca_payment_strategies",
)
notes = models.TextField(blank=True, null=True, verbose_name=_("Notes"))
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = _("DCA Strategy")
verbose_name_plural = _("DCA Strategies")
ordering = ["-created_at"]
def __str__(self):
return self.name
def total_invested(self):
return sum(entry.amount_paid for entry in self.entries.all())
def total_received(self):
return sum(entry.amount_received for entry in self.entries.all())
def average_entry_price(self):
total_invested = self.total_invested()
total_received = self.total_received()
if total_received:
return total_invested / total_received
return Decimal("0")
def total_entries(self):
return self.entries.count()
def current_total_value(self):
"""Calculate current total value of all entries"""
return sum(entry.current_value() for entry in self.entries.all())
def total_profit_loss(self):
"""Calculate total P/L in payment currency"""
return self.current_total_value() - self.total_invested()
def total_profit_loss_percentage(self):
"""Calculate total P/L percentage"""
total_invested = self.total_invested()
if total_invested:
return (self.total_profit_loss() / total_invested) * 100
return Decimal("0")
class DCAEntry(models.Model):
strategy = models.ForeignKey(
DCAStrategy,
on_delete=models.CASCADE,
related_name="entries",
verbose_name=_("Strategy"),
)
date = models.DateField(verbose_name=_("Date"))
amount_paid = models.DecimalField(
max_digits=42, decimal_places=30, verbose_name=_("Amount Paid")
)
amount_received = models.DecimalField(
max_digits=42, decimal_places=30, verbose_name=_("Amount Received")
)
expense_transaction = models.ForeignKey(
"transactions.Transaction",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="dca_expense_entries",
verbose_name=_("Expense Transaction"),
)
income_transaction = models.ForeignKey(
"transactions.Transaction",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="dca_income_entries",
verbose_name=_("Income Transaction"),
)
notes = models.TextField(blank=True, null=True, verbose_name=_("Notes"))
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = _("DCA Entry")
verbose_name_plural = _("DCA Entries")
ordering = ["-date"]
def __str__(self):
return f"{self.strategy.name} - {self.date}"
def entry_price(self):
if self.amount_received:
return self.amount_paid / self.amount_received
return 0
def current_value(self):
"""
Calculate current value of received amount in payment currency
using latest exchange rate
"""
if not self.amount_received:
return Decimal("0")
amount, _, _, _ = convert(
self.amount_received,
self.strategy.target_currency,
self.strategy.payment_currency,
timezone.now().date(),
)
return amount or Decimal("0")
def profit_loss(self):
"""Calculate P/L in payment currency"""
return self.current_value() - self.amount_paid
def profit_loss_percentage(self):
"""Calculate P/L percentage"""
if self.amount_paid:
return (self.profit_loss() / self.amount_paid) * Decimal("100")
return Decimal("0")

3
app/apps/dca/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

9
app/apps/dca/urls.py Normal file
View File

@@ -0,0 +1,9 @@
from django.urls import path
from . import views
urlpatterns = [
path("dca/", views.strategy_list, name="strategy_list"),
path("dca/<int:pk>/", views.strategy_detail, name="strategy_detail"),
# Add more URLs for CRUD operations
]

67
app/apps/dca/views.py Normal file
View File

@@ -0,0 +1,67 @@
# apps/dca_tracker/views.py
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from django.db.models import Sum, Avg
from django.db.models.functions import TruncMonth
from .models import DCAStrategy, DCAEntry
from .forms import DCAStrategyForm, DCAEntryForm
@login_required
def strategy_list(request):
strategies = DCAStrategy.objects.all()
return render(request, "dca/strategy_list.html", {"strategies": strategies})
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from django.db.models import Sum, Avg
from django.db.models.functions import TruncMonth
@login_required
def strategy_detail(request, pk):
strategy = get_object_or_404(DCAStrategy, id=pk)
entries = strategy.entries.all()
# Calculate monthly aggregates
monthly_data = (
entries.annotate(month=TruncMonth("date"))
.values("month")
.annotate(
total_paid=Sum("amount_paid"),
total_received=Sum("amount_received"),
avg_entry_price=Avg("amount_paid") / Avg("amount_received"),
)
.order_by("month")
)
# Prepare entries data with current values
entries_data = [
{
"entry": entry,
"current_value": entry.current_value(),
"profit_loss": entry.profit_loss(),
"profit_loss_percentage": entry.profit_loss_percentage(),
}
for entry in entries
]
context = {
"strategy": strategy,
"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(),
}
return render(request, "dca/strategy_detail.html", context)

View File

@@ -0,0 +1,228 @@
{% extends "layouts/base.html" %}
{% load i18n %}
{% block content %}
<div class="container">
<h1>{{ strategy.name }}</h1>
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">{% trans "Total Invested" %}</h5>
{# <p class="card-text">{{ strategy.total_invested }} {{ strategy.payment_currency }}</p>#}
<div class="card-text">
<c-amount.display
:amount="strategy.total_invested"
:prefix="strategy.payment_currency.prefix"
:suffix="strategy.payment_currency.suffix"
:decimal_places="strategy.payment_currency.decimal_places"></c-amount.display>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">{% trans "Total Received" %}</h5>
<div class="card-text">
<c-amount.display
:amount="strategy.total_received"
:prefix="strategy.target_currency.prefix"
:suffix="strategy.target_currency.suffix"
:decimal_places="strategy.target_currency.decimal_places"></c-amount.display>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">{% trans "Average Entry Price" %}</h5>
<div class="card-text">
<c-amount.display
:amount="strategy.average_entry_price"
:prefix="strategy.payment_currency.prefix"
:suffix="strategy.payment_currency.suffix"
:decimal_places="strategy.payment_currency.decimal_places"></c-amount.display>
</div>
</div>
</div>
</div>
</div>
<div class="row mt-4">
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">{% trans "Current Total Value" %}</h5>
<div class="card-text">
<c-amount.display
:amount="strategy.current_total_value"
:prefix="strategy.payment_currency.prefix"
:suffix="strategy.payment_currency.suffix"
:decimal_places="strategy.payment_currency.decimal_places"></c-amount.display>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<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 %}">
<c-amount.display
:amount="strategy.total_profit_loss"
:prefix="strategy.payment_currency.prefix"
:suffix="strategy.payment_currency.suffix"
:decimal_places="strategy.payment_currency.decimal_places">
</c-amount.display>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<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 %}">
{{ strategy.total_profit_loss_percentage|floatformat:2 }}%
</div>
</div>
</div>
</div>
</div>
<!-- Monthly Chart -->
<div class="row mt-4">
<div class="col-12">
<div class="card">
<div class="card-body">
<canvas id="monthlyChart"></canvas>
</div>
</div>
</div>
</div>
<!-- Entries Table -->
<div class="row mt-4">
<div class="col-12">
<div class="card">
<div class="card-body">
<h5 class="card-title">{% trans "Entries" %}</h5>
<div class="table-responsive">
<table class="table table-hover text-nowrap">
<thead>
<tr>
<th>{% trans "Date" %}</th>
<th>{% trans "Amount Paid" %}</th>
<th>{% trans "Amount Received" %}</th>
<th>{% trans "Current Value" %}</th>
<th>{% trans "P/L" %}</th>
<th>{% trans "Actions" %}</th>
</tr>
</thead>
<tbody>
{% for entry in entries %}
<tr>
<td>{{ entry.date|date:"SHORT_DATE_FORMAT" }}</td>
<td><c-amount.display
:amount="entry.amount_paid"
:prefix="entry.strategy.payment_currency.prefix"
:suffix="entry.strategy.payment_currency.suffix"
:decimal_places="entry.strategy.payment_currency.decimal_places"></c-amount.display></td>
<td><c-amount.display
:amount="entry.amount_received"
:prefix="entry.strategy.target_currency.prefix"
:suffix="entry.strategy.target_currency.suffix"
:decimal_places="entry.strategy.target_currency.decimal_places"></c-amount.display></td>
<td><c-amount.display
:amount="entry.current_value"
:prefix="entry.strategy.payment_currency.prefix"
:suffix="entry.strategy.payment_currency.suffix"
:decimal_places="entry.strategy.payment_currency.decimal_places"></c-amount.display></td>
<td>
{% if entry.profit_loss_percentage > 0 %}
<span class="badge text-bg-success"><i class="fa-solid fa-up-long me-2"></i>{{ entry.profit_loss_percentage|floatformat:"2g" }}%</span>
{% elif entry.profit_loss_percentage < 0 %}
<span class="badge text-bg-danger"><i class="fa-solid fa-down-long me-2"></i>{{ entry.profit_loss_percentage|floatformat:"2g" }}%</span>
{% endif %}
</td>
<td>
<!-- Add action buttons -->
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="row mt-4">
<div class="col-12">
<div class="card">
<div class="card-body">
<h5 class="card-title">{% trans "Performance Over Time" %}</h5>
<canvas id="performanceChart"></canvas>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block extra_js_body %}
<script>
// Add Chart.js initialization for performance visualization
const perfomancectx = document.getElementById('performanceChart').getContext('2d');
const performanceChart = new Chart(perfomancectx, {
type: 'line',
data: {
labels: [{% for entry in entries_data %}'{{ entry.entry.date|date:"Y-m-d" }}'{% if not forloop.last %}, {% endif %}{% endfor %}],
datasets: [{
label: '{% trans "P/L %" %}',
data: [{% for entry in entries_data %}{{ entry.profit_loss_percentage|floatformat:2 }}{% if not forloop.last %}, {% endif %}{% endfor %}],
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
</script>
<script>
// Add Chart.js initialization for performance visualization
const ctx = document.getElementById('monthlyChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: [{% for entry in monthly_data %}'{{ entry.date|date:"Y-m-d" }}'{% if not forloop.last %}, {% endif %}{% endfor %}],
datasets: [{
label: '{% trans "P/L %" %}',
data: [{% for entry in monthly_data %}{{ entry.total_paid|floatformat:2 }}{% if not forloop.last %}, {% endif %}{% endfor %}],
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
</script>
{% endblock %}