diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/trading/migrations/0010_auto_20210716_1344.py | 45 | ||||
-rw-r--r-- | app/trading/models.py | 32 |
2 files changed, 76 insertions, 1 deletions
diff --git a/app/trading/migrations/0010_auto_20210716_1344.py b/app/trading/migrations/0010_auto_20210716_1344.py new file mode 100644 index 0000000..27b3a6f --- /dev/null +++ b/app/trading/migrations/0010_auto_20210716_1344.py @@ -0,0 +1,45 @@ +# Generated by Django 3.2.4 on 2021-07-16 13:44 + +from django.db import migrations, models +import django.db.models.deletion +import django.db.models.manager + + +class Migration(migrations.Migration): + + dependencies = [ + ('trading', '0009_luxtrade_is_wanderer'), + ] + + operations = [ + migrations.AlterModelManagers( + name='luxtrade', + managers=[ + ('stats', django.db.models.manager.Manager()), + ], + ), + migrations.AddField( + model_name='luxtrade', + name='pl', + field=models.FloatField(null=True), + ), + migrations.AddField( + model_name='ticker', + name='current_price', + field=models.CharField(blank=True, max_length=243, null=True), + ), + migrations.AddField( + model_name='ticker', + name='price_date', + field=models.DateTimeField(null=True), + ), + migrations.CreateModel( + name='Price', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('price', models.CharField(blank=True, max_length=243, null=True)), + ('price_date', models.DateTimeField()), + ('symbol', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='trading.ticker')), + ], + ), + ] 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) |