from datetime import datetime from django.http import HttpResponseRedirect from django.views.generic.edit import CreateView, UpdateView from utils.views import PaginatedListView from .models import LuxTrade, LuxOptionsTrade, LuxOptionContact, LuxOptionPurchase from .forms import LuxOptionsForm 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]) context['options_trades_closed'] = LuxOptionsTrade.objects.filter(status=1) 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['month'] = datetime.now().strftime('%h') context['luxoptions_purchases'] = LuxOptionPurchase.objects.filter(status=0) return context def get_queryset(self): queryset = super(LuxTradeListView, self).get_queryset() return queryset.filter(status=1) 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 = LuxOptionContact 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 = LuxOptionContact.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})