summaryrefslogtreecommitdiff
path: root/app/trading/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/trading/models.py')
-rw-r--r--app/trading/models.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/app/trading/models.py b/app/trading/models.py
index edfc0c2..c1339e0 100644
--- a/app/trading/models.py
+++ b/app/trading/models.py
@@ -1,4 +1,6 @@
import os
+import datetime
+import calendar
from PIL import Image
from django.db import models
from django.db.models.signals import post_save
@@ -20,14 +22,23 @@ def get_upload_path(self, filename):
return "images/products/%s" % (filename)
+
class Ticker(models.Model):
symbol = models.CharField(max_length=9)
name = models.CharField(max_length=243, blank=True, null=True)
+ current_price = models.CharField(max_length=243, blank=True, null=True)
+ price_date = models.DateTimeField(null=True)
def __str__(self):
return str(self.symbol)
+class Price(models.Model):
+ symbol = models.ForeignKey(Ticker, null=True, on_delete=models.SET_NULL)
+ price = models.CharField(max_length=243, blank=True, null=True)
+ price_date = models.DateTimeField()
+
+
class OptionsTrade(models.Model):
date = models.DateTimeField()
symbol = models.ForeignKey(Ticker, null=True, on_delete=models.SET_NULL)
@@ -85,6 +96,17 @@ class OptionsTrade(models.Model):
buy_amount+=o.amount+o.fees
return buy_amount+sell_amount
+
+class LuxTradeStatsManager(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)
+ print("date info: ", last_day, start_date, end_date)
+ 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)
@@ -103,10 +125,14 @@ class LuxTrade(models.Model):
status = models.IntegerField(choices=STATUS, default=2)
notes = models.TextField(null=True, blank=True)
is_wanderer = models.BooleanField(default=True)
+ pl = models.FloatField(null=True)
def __str__(self):
return str(self.symbol)
+ objects = models.Manager() # The default manager.
+ stats = LuxTradeStatsManager()
+
def get_absolute_url(self):
return reverse('luxtrade:detail', kwargs={"pk": self.pk})
@@ -143,12 +169,15 @@ class LuxTrade(models.Model):
@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()
+ if self.status == 1 and not self.pl:
+ self.pl = round((self.close_price*self.shares)-(self.entry_price*self.shares), 2)
super(LuxTrade, self).save()
+
class TradeJrnl(models.Model):
date = models.DateTimeField(auto_now_add=True)
body_markdown = models.TextField(blank=True)
@@ -170,6 +199,7 @@ class TradeJrnl(models.Model):
except model.DoesNotExist:
return ''
+
def save(self, *args, **kwargs):
md = render_images(self.body_markdown)
self.body_html = markdown_to_html(md)