From c2acb9c93081956725e33808764e35c61cc7b90b Mon Sep 17 00:00:00 2001 From: lxf Date: Thu, 6 Jan 2022 10:01:13 -0500 Subject: trad: started on simpler forms for updates and added profit to table --- app/trading/admin.py | 4 +- app/trading/forms.py | 23 ++++--- app/trading/migrations/0022_auto_20220104_1551.py | 22 +++++++ app/trading/models.py | 23 +++++-- app/trading/templates/trading/base.html | 2 +- .../templates/trading/create_luxoptions_form.html | 2 +- app/trading/templates/trading/list.html | 2 + .../templates/trading/luxoptions_update_form.html | 70 ++++++++++++++++++++++ app/trading/urls.py | 7 ++- app/trading/views.py | 48 +++++++++++++-- 10 files changed, 181 insertions(+), 22 deletions(-) create mode 100644 app/trading/migrations/0022_auto_20220104_1551.py create mode 100644 app/trading/templates/trading/luxoptions_update_form.html (limited to 'app/trading') diff --git a/app/trading/admin.py b/app/trading/admin.py index 3243f53..96e60ee 100644 --- a/app/trading/admin.py +++ b/app/trading/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin from django.db import models -from .models import TradeJrnl, LuxOptionsTrade, LuxTrade, LuxOptionPurchase, LuxOptionContact +from .models import TradeJrnl, LuxOptionsTrade, LuxTrade, LuxOptionPurchase, LuxOptionContract from utils.widgets import AdminImageWidget, LGEntryForm @@ -36,7 +36,7 @@ class LuxTradeAdmin(admin.ModelAdmin): class LuxOptionContactInline(admin.StackedInline): - model = LuxOptionContact + model = LuxOptionContract extra = 0 fieldsets = ( (None, { diff --git a/app/trading/forms.py b/app/trading/forms.py index 60c6de6..cd2128d 100644 --- a/app/trading/forms.py +++ b/app/trading/forms.py @@ -1,13 +1,13 @@ from django import forms from django.forms.utils import ValidationError -from .models import LuxOptionContact +from .models import LuxOptionContract class LuxOptionsForm(forms.ModelForm): contracts = forms.IntegerField() class Meta: - model = LuxOptionContact + model = LuxOptionContract fields = [ 'symbol', 'strike_price', @@ -17,9 +17,16 @@ class LuxOptionsForm(forms.ModelForm): 'contracts' ] - def save(self, commit=True): - i = 0 - while i < int(self.cleaned_data['contracts']): - print(i, "contracts") - i = i+1 - return super(LuxOptionsForm, self).save(commit=commit) + +class LuxOptionsUpdateForm(forms.ModelForm): + contracts_to_close = forms.IntegerField() + + class Meta: + model = LuxOptionContract + fields = [ + 'symbol', + 'strike_price', + 'expiration_date', + 'contracts_to_close', + 'contract_close_price', + ] diff --git a/app/trading/migrations/0022_auto_20220104_1551.py b/app/trading/migrations/0022_auto_20220104_1551.py new file mode 100644 index 0000000..f074f29 --- /dev/null +++ b/app/trading/migrations/0022_auto_20220104_1551.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.7 on 2022-01-04 15:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('trading', '0021_auto_20220104_1039'), + ] + + operations = [ + migrations.RenameModel( + old_name='LuxOptionContact', + new_name='LuxOptionContract', + ), + migrations.AlterField( + model_name='luxoptionpurchase', + name='status', + field=models.IntegerField(choices=[(0, 'Open'), (1, 'Closed')], default=0), + ), + ] diff --git a/app/trading/models.py b/app/trading/models.py index 720b834..d96970f 100644 --- a/app/trading/models.py +++ b/app/trading/models.py @@ -223,8 +223,21 @@ class LuxOptionPurchase(models.Model): def __str__(self): return str(self.symbol) + def get_absolute_url(self): + return reverse('luxtrade:option-update', kwargs={"pk": self.pk}) + def get_contract_count(self): - return self.luxoptioncontact_set.count() + return self.luxoptioncontract_set.count() + + def profit_loss(self): + total = 0 + for option in self.luxoptioncontract_set.all(): + if option.contract_close_price: + pl = option.contract_close_price - option.contract_open_price + else: + pl = 0 + total = total + pl + return total*100 @property def stop_price(self): @@ -232,7 +245,7 @@ class LuxOptionPurchase(models.Model): @property def total_invested(self): - return round(self.get_contract_count()*(self.luxoptioncontact_set.first().contract_open_price*100)) + return round(self.get_contract_count()*(self.luxoptioncontract_set.first().contract_open_price*100)) @property def trade_risk(self): @@ -244,11 +257,11 @@ class LuxOptionPurchase(models.Model): @property def sell_half_at(self): - return self.luxoptioncontact_set.first().contract_open_price*1.25 + return self.luxoptioncontract_set.first().contract_open_price*1.25 @property def contract_price(self): - return self.luxoptioncontact_set.first().contract_open_price + return self.luxoptioncontract_set.first().contract_open_price def save(self, *args, **kwargs): if self.status == 1 and not self.close_date: @@ -259,7 +272,7 @@ class LuxOptionPurchase(models.Model): super(LuxOptionPurchase, self).save() -class LuxOptionContact(models.Model): +class LuxOptionContract(models.Model): symbol = models.CharField(max_length=256) strike_price = models.FloatField() expiration_date = models.DateField() diff --git a/app/trading/templates/trading/base.html b/app/trading/templates/trading/base.html index 2d6e5da..9b9e6b0 100644 --- a/app/trading/templates/trading/base.html +++ b/app/trading/templates/trading/base.html @@ -15,7 +15,7 @@