diff options
Diffstat (limited to 'app/trading/models.py')
-rw-r--r-- | app/trading/models.py | 26 |
1 files changed, 26 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() |