1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
import os
import datetime
import calendar
from PIL import Image
from django.db import models
from django.db.models import Sum
from django.db.models.signals import post_save
from django.contrib.sitemaps import Sitemap
from django.dispatch import receiver
from django.urls import reverse
from django.apps import apps
from django.utils.html import format_html
from django.utils import timezone
from django.conf import settings
from django.template.defaultfilters import slugify
from media.models import LuxImage, LuxImageSize
from media.utils import resize_image
from utils.util import render_images, render_products, parse_video, markdown_to_html
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)
TRANSACTION_CODE = (
('Trade', 'Trade'),
)
transaction_code = models.CharField(choices=TRANSACTION_CODE, max_length=25)
TRANSACTION_SUBCODE = (
('Buy to Open', 'Buy to Open'),
('Sell to Open', 'Sell to Open'),
('Sell to Close', 'Sell to Close'),
)
transaction_subcode = models.CharField(choices=TRANSACTION_SUBCODE, max_length=25)
BUY_SELL = (
('Buy', 'Buy'),
('Sell', 'Sell'),
)
buy_sell = models.CharField(choices=BUY_SELL, max_length=4)
OPEN_CLOSE = (
('Open', 'Open'),
('Close', 'Close'),
)
open_close = models.CharField(choices=OPEN_CLOSE, max_length=5)
quantity = models.FloatField()
expiration_date = models.DateTimeField()
strike = models.FloatField()
CALL_PUT = (
('C', 'Call'),
('P', 'Put'),
)
call_put = models.CharField(choices=CALL_PUT, max_length=4)
price = models.FloatField()
fees = models.FloatField()
amount = models.FloatField()
description = models.TextField(blank=True)
def __str__(self):
return str(self.symbol)
def get_profit_by_symbol(self,t):
buy_amount = 0
sell_amount = 0
for o in OptionsTrade.objects.filter(symbol__symbol=t).filter(buy_sell="Sell"):
sell_amount+=o.amount+o.fees
for o in OptionsTrade.objects.filter(symbol__symbol=t).filter(buy_sell="Buy"):
buy_amount+=o.amount+o.fees
return buy_amount+sell_amount
def get_profit(self):
buy_amount = 0
sell_amount = 0
for o in OptionsTrade.objects.filter(buy_sell="Sell"):
sell_amount+=o.amount+o.fees
for o in OptionsTrade.objects.filter(buy_sell="Buy"):
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)
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)
close_date = models.DateTimeField(null=True, blank=True)
open_date = models.DateTimeField(null=True, blank=True)
entry_price = models.FloatField()
stop_price = models.FloatField()
target_price = models.FloatField()
close_price = models.FloatField(null=True, blank=True)
shares = models.FloatField()
STATUS = (
(0, 'Open'),
(1, 'Closed'),
(2, 'Watching'),
)
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)
class Meta:
ordering = ('-open_date',)
get_latest_by = 'open_date'
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})
@property
def risk_reward(self):
if self.stop_price > self.entry_price:
return "No risk"
else:
return round((self.entry_price - self.stop_price)/(self.target_price-self.entry_price),2)
@property
def goal_dollars(self):
return round((self.target_price*self.shares)-(self.entry_price*self.shares), 2)
@property
def goal_percent(self):
return round((((self.target_price*self.shares)-(self.entry_price*self.shares))/self.amount_invested)*100, 2)
@property
def risk_dollars(self):
if self.stop_price > self.entry_price:
return 0
else:
return round((self.entry_price-self.stop_price)*self.shares, 2)
@property
def amount_invested(self):
return round(self.entry_price * self.shares, 2)
@property
def realized_dollars(self):
return round((self.close_price*self.shares)-(self.entry_price*self.shares), 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()
if self.status == 1 and not self.close_date:
self.close_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 LuxOptionsTrade(models.Model):
symbol = models.CharField(max_length=256)
date = models.DateTimeField(auto_now_add=True)
close_date = models.DateTimeField(null=True, blank=True)
open_date = models.DateTimeField(null=True, blank=True)
entry_price = models.FloatField()
stop_price = models.FloatField()
target_price = models.FloatField()
strike_price = models.FloatField()
CALL_PUT = (
(0, 'Call'),
(1, 'Put'),
)
call_put = models.IntegerField(choices=CALL_PUT, default=2)
contract_price = models.FloatField()
number_contracts = models.FloatField()
delta = models.FloatField()
expiration_date = models.DateField()
fees = models.FloatField(blank=True)
STATUS = (
(0, 'Open'),
(1, 'Closed'),
(2, 'Watching'),
)
status = models.IntegerField(choices=STATUS, default=2)
notes = models.TextField(null=True, blank=True)
pl = models.FloatField(null=True)
class Meta:
ordering = ('-open_date',)
get_latest_by = 'open_date'
def __str__(self):
return str(self.symbol)
def get_absolute_url(self):
return reverse('luxtrade:optiondetail', kwargs={"pk": self.pk})
@property
def risk_per_contract(self):
return round(((self.entry_price-self.stop_price)*self.delta/self.contract_price)*self.contract_price*100, 2)
@property
def risk_total(self):
return round(self.risk_per_contract*self.number_contracts, 2)
@property
def risk_reward(self):
return round((self.target_price-self.entry_price)/(self.entry_price-self.stop_price), 2);
@property
def amount_invested(self):
return round(((self.contract_price * self.number_contracts)*100)+self.fees, 2)
@property
def profit_goal(self):
if self.call_put == 0:
return round((((self.target_price-(self.strike_price+self.contract_price))*100)*self.number_contracts)-self.fees, 3)
@property
def days_until_expiration(self):
td = self.expiration_date - datetime.date.today()
return td.days
def save(self, *args, **kwargs):
if self.status == 0 and not self.open_date:
self.open_date = timezone.now()
if self.number_contracts < 10:
self.fees = self.number_contracts + (.14*self.number_contracts)
else:
self.fees = 10 + (.14*self.number_contracts)
if self.status == 1 and not self.close_date:
self.close_date = timezone.now()
if self.status == 1 and not self.pl:
self.pl = 0 #round((self.close_price*self.shares)-(self.entry_price*self.shares), 2)
super(LuxOptionsTrade, self).save()
class TradeJrnl(models.Model):
date = models.DateTimeField(auto_now_add=True)
body_markdown = models.TextField(blank=True)
body_html = models.TextField(null=True, blank=True)
def __str__(self):
return str(self.date)
@property
def get_previous_admin_url(self):
n = self.get_previous_by_read_date()
return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=[n.id])
@property
def get_next_admin_url(self):
model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name)
try:
return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=[self.get_next_by_read_date().pk])
except model.DoesNotExist:
return ''
def save(self, *args, **kwargs):
md = render_images(self.body_markdown)
self.body_html = markdown_to_html(md)
super(TradeJrnl, self).save()
|