summaryrefslogtreecommitdiff
path: root/app/trading/views.py
blob: 82904ca7dd3a6ed6790a56d7df226a1c4b78673a (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
import datetime
import calendar
from django.http import HttpResponseRedirect
from django.utils import timezone
from django.views.generic.edit import CreateView, UpdateView
from utils.views import PaginatedListView

from .models import LuxTrade, LuxOptionsTrade, LuxOptionContract, LuxOptionPurchase
from .forms import LuxOptionsForm, LuxOptionsUpdateForm


class LuxTradeListView(PaginatedListView):
    model = LuxTrade
    template_name = 'trading/list.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(LuxTradeListView, self).get_context_data(**kwargs)
        context['open_trades'] = LuxTrade.objects.filter(status=0)
        context['watch_trades'] = LuxTrade.objects.filter(status=2)
        context['options_trades'] = LuxOptionsTrade.objects.filter(status__in=[0,2])
        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)
        context['options_trades_closed'] = LuxOptionsTrade.objects.filter(status=1).filter(close_date__range=(start_date, end_date))
        context['monthly_pl'] = LuxTrade.stats.get_month_pl()
        context['year_pl'] = LuxTrade.stats.get_year_pl()
        #context['options_monthly_pl'] = LuxOptionsTrade.stats.get_month_pl()
        #context['options_year_pl'] = LuxOptionsTrade.stats.get_year_pl()
        context['options_monthly_pl'] = LuxOptionPurchase.stats.get_month_pl()
        context['options_year_pl'] = LuxOptionPurchase.stats.get_year_pl()
        context['month'] = timezone.now().strftime('%h')
        context['luxoptions_purchases'] = LuxOptionPurchase.objects.filter(close_date__range=(start_date, end_date))
        return context

    def get_queryset(self):
        queryset = super(LuxTradeListView, self).get_queryset()
        queryset = queryset.filter(status=1)
        year=timezone.now().year
        start_date = datetime.date(year, 1, 1)
        end_date = datetime.date(year, 12, 31)
        return queryset.filter(close_date__range=(start_date, end_date))


class LuxTradeDetailView(UpdateView):
    model = LuxTrade
    fields = ['symbol', 'status', 'entry_price', 'stop_price', 'target_price', 'shares', 'close_price', 'notes', 'is_wanderer']
    template_name = 'trading/update_form.html'
    success_url = '/trading/'


class TradeModelFormView(CreateView):
    model = LuxTrade
    fields = ['symbol', 'status', 'entry_price', 'stop_price', 'target_price', 'shares', 'is_wanderer']
    success_url = '/trading/'
    template_name = 'trading/create_form.html'


class LuxOptionsTradeDetailView(UpdateView):
    model = LuxOptionsTrade
    fields = [
        'symbol', 
        'status',
        'close_date',
        'entry_price',
        'stop_price',
        'target_price',
        'call_put',
        'expiration_date',
        'strike_price',
        'contract_price',
        'contract_close_price',
        'number_contracts',
        'delta',
        'notes',
        'pl'
    ]
    template_name = 'trading/update_options_form.html'
    success_url = '/trading/'


class OptionsModelFormView(CreateView):
    model = LuxOptionsTrade
    fields = [
        'symbol', 
        'status',
        'entry_price',
        'stop_price',
        'target_price',
        'call_put',
        'expiration_date',
        'strike_price',
        'contract_price',
        'number_contracts',
        'delta'
    ]
    success_url = '/trading/'
    template_name = 'trading/create_options_form.html'


class LuxOptionPurchaseCreateView(CreateView):
    model = LuxOptionContract
    fields = ['symbol']
    success_url = '/trading/'
    template_name = 'trading/create_luxoptions_form.html'


    def get_context_data(self, **kwargs):
        context = super(LuxOptionPurchaseCreateView, self).get_context_data(**kwargs)
        context = {'form': LuxOptionsForm()}
        return context

    def post(self, request, *args, **kwargs):
        form = LuxOptionsForm(request.POST)
        if form.is_valid():
            i = 0
            p = LuxOptionPurchase.objects.create(
                symbol = form.cleaned_data['symbol'],
            )
            print(form.cleaned_data['contracts'])
            while i < int(form.cleaned_data['contracts']):
                c = LuxOptionContract.objects.create(
                    symbol = form.cleaned_data['symbol'],
                    strike_price = form.cleaned_data['strike_price'],
                    expiration_date = form.cleaned_data['expiration_date'],
                    contract_open_price = form.cleaned_data['contract_open_price'],
                    call_put = form.cleaned_data['call_put'],
                    options_purchase = p
                )
                i = i+1
            return HttpResponseRedirect('/trading/')
        return render(request, 'trading/create_luxoptions_form.html', {'form': form})

class LuxOptionsPurchaseEditView(UpdateView):
    model = LuxOptionPurchase
    fields = [
        'symbol',
        'close_date',
        'status'
    ]
    success_url = '/trading/'
    template_name = 'trading/luxoptions_update_form.html'


    def get_context_data(self, **kwargs):
        context = super(LuxOptionsPurchaseEditView, self).get_context_data(**kwargs)
        obj = LuxOptionPurchase.objects.get(pk=self.object.pk)
        context['form'] = LuxOptionsUpdateForm()
        context['object'] = self.object
        return context

    def post(self, request, *args, **kwargs):
        form = LuxOptionsForm(request.POST)
        if form.is_valid():
            i = 0
            p = LuxOptionPurchase.objects.get(
                symbol = form.cleaned_data['symbol'],
                open_date = form.cleaned_data['open_date'],
            )
            print(form.cleaned_data['contracts_to_close'])
            while i < int(form.cleaned_data['contracts_to_close']):
                c = LuxOptionContract.objects.get(
                    symbol = form.cleaned_data['symbol'],
                    strike_price = form.cleaned_data['strike_price'],
                    expiration_date = form.cleaned_data['expiration_date'],
                    contract_open_price = form.cleaned_data['contract_open_price'],
                    call_put = form.cleaned_data['call_put'],
                )
                c.contract_close_price = form.cleaned_data['contract_close_price']
                c.save()
            return HttpResponseRedirect('/trading/')
        return render(request, 'trading/create_luxoptions_form.html', {'form': form})