summaryrefslogtreecommitdiff
path: root/app/trading/models.py
blob: 0b79c9e992605e00adb23927d4abb230c487b082 (plain)
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
import os
from PIL import Image
from django.db import models
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)

    def __str__(self):
        return str(self.symbol)


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 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)
    
    def __str__(self):
        return str(self.symbol)

    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()
        super(LuxTrade, 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()