diff options
-rw-r--r-- | app/trading/models.py | 26 | ||||
-rw-r--r-- | app/trading/templates/trading/list.html | 29 | ||||
-rw-r--r-- | app/trading/views.py | 2 |
3 files changed, 57 insertions, 0 deletions
diff --git a/app/trading/models.py b/app/trading/models.py index 704f3bb..97c82a6 100644 --- a/app/trading/models.py +++ b/app/trading/models.py @@ -111,6 +111,7 @@ class LuxTradeStatsManager(models.Manager): end_date = datetime.date(year, 12, 31) return self.filter(close_date__range=(start_date, end_date)).aggregate(Sum('pl')) + class LuxTrade(models.Model): symbol = models.CharField(max_length=256) date = models.DateTimeField(auto_now_add=True) @@ -191,6 +192,20 @@ class LuxTrade(models.Model): super(LuxTrade, self).save() +class LuxOptionsTradeStatsManager(models.Manager): + + def get_month_pl(self, month=timezone.now().month): + last_day = calendar.monthrange(timezone.now().year, month)[1] + start_date = datetime.date(timezone.now().year, month, 1) + end_date = datetime.date(timezone.now().year, month, last_day) + return self.filter(close_date__range=(start_date, end_date)).aggregate(Sum('pl')) + + def get_year_pl(self, year=timezone.now().year): + start_date = datetime.date(year, 1, 1) + end_date = datetime.date(year, 12, 31) + return self.filter(close_date__range=(start_date, end_date)).aggregate(Sum('pl')) + + class LuxOptionsTrade(models.Model): symbol = models.CharField(max_length=256) date = models.DateTimeField(auto_now_add=True) @@ -225,6 +240,9 @@ class LuxOptionsTrade(models.Model): ordering = ('-open_date',) get_latest_by = 'open_date' + objects = models.Manager() # The default manager. + stats = LuxOptionsTradeStatsManager() + def __str__(self): return str(self.symbol) @@ -257,6 +275,14 @@ class LuxOptionsTrade(models.Model): td = self.expiration_date - datetime.date.today() return td.days + @property + def realized_dollars(self): + return round((((self.contract_close_price*self.number_contracts)*100) - ((self.contract_price*self.number_contracts)*100)-self.fees), 2) + + @property + def realized_percent(self): + return round((self.realized_dollars/self.amount_invested)*100, 2) + def save(self, *args, **kwargs): if self.status == 0 and not self.open_date: self.open_date = timezone.now() diff --git a/app/trading/templates/trading/list.html b/app/trading/templates/trading/list.html index a36c15c..d00d599 100644 --- a/app/trading/templates/trading/list.html +++ b/app/trading/templates/trading/list.html @@ -266,6 +266,35 @@ </td>{% endif %} </tr> {% endfor %} + <tr> + <td> </td> + </tr> + <tr> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td>{{month}} P/L:</td> + <td>{{options_monthly_pl.pl__sum}}</td> + </tr> + <tr> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td></td> + <td>YTD P/L:</td> + <td>{{options_year_pl.pl__sum}}</td> + </tr> </table> {% endblock %} diff --git a/app/trading/views.py b/app/trading/views.py index aa68192..9e19c05 100644 --- a/app/trading/views.py +++ b/app/trading/views.py @@ -18,6 +18,8 @@ class LuxTradeListView(PaginatedListView): context['options_trades_closed'] = LuxOptionsTrade.objects.filter(status=1) context['monthly_pl'] = LuxTrade.stats.get_month_pl() context['year_pl'] = LuxTrade.stats.get_year_pl() + context['options_monthly_pl'] = LuxOptionsTrade.stats.get_month_pl() + context['options_year_pl'] = LuxOptionsTrade.stats.get_year_pl() context['month'] = datetime.now().strftime('%h') return context |