| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- from decimal import Decimal
- from django.db import models
- class Valor(models.Model):
- """Un valor bursátil (acción, ETF...) de la cartera."""
- ticker = models.CharField(
- max_length=20, unique=True,
- help_text='Ticker según Yahoo Finance; en Bolsa española con sufijo .MC',
- )
- nombre = models.CharField(max_length=100)
- mercado = models.CharField(max_length=30, blank=True, help_text='p. ej. SIBE, NASDAQ')
- isin = models.CharField(max_length=12, blank=True)
- precio_actual = models.DecimalField(
- max_digits=12, decimal_places=4, null=True, blank=True,
- editable=False,
- )
- actualizado_en = models.DateTimeField(null=True, blank=True, editable=False)
- class Meta:
- verbose_name = 'valor'
- verbose_name_plural = 'valores'
- ordering = ['ticker']
- def __str__(self):
- return f'{self.ticker} - {self.nombre}'
- class Operacion(models.Model):
- COMPRA = 'C'
- VENTA = 'V'
- TIPOS = [
- (COMPRA, 'Compra'),
- (VENTA, 'Venta'),
- ]
- valor = models.ForeignKey(Valor, on_delete=models.CASCADE, related_name='operaciones')
- tipo = models.CharField('tipo de operación', max_length=1, choices=TIPOS)
- fecha = models.DateField()
- cantidad = models.DecimalField(max_digits=14, decimal_places=4)
- precio = models.DecimalField(
- max_digits=12, decimal_places=4,
- help_text='Precio unitario por acción',
- )
- comisiones = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0'))
- class Meta:
- ordering = ['-fecha', '-pk']
- verbose_name = 'operación'
- def __str__(self):
- return f'{self.get_tipo_display()} {self.cantidad} {self.valor.ticker} ({self.fecha})'
- @property
- def importe(self):
- """Importe total de la operación, comisiones incluidas."""
- return (self.cantidad * self.precio).quantize(Decimal('0.01')) + self.comisiones
- class Dividendo(models.Model):
- valor = models.ForeignKey(Valor, on_delete=models.CASCADE, related_name='dividendos')
- fecha = models.DateField()
- bruto = models.DecimalField(max_digits=10, decimal_places=2)
- retencion = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0'))
- class Meta:
- ordering = ['-fecha', '-pk']
- def __str__(self):
- return f'Dividendo {self.bruto} € {self.valor.ticker} ({self.fecha})'
- @property
- def neto(self):
- return self.bruto - self.retencion
|