fisk/core/views/limits.py

114 lines
3.8 KiB
Python

from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.views import View
from core.forms import TradingTimeForm
from core.models import Strategy, TradingTime
from core.util import logs
from core.views import (
ObjectCreate,
ObjectDelete,
ObjectList,
ObjectNameMixin,
ObjectUpdate,
)
log = logs.get_logger(__name__)
# Trend directions
class TrendDirectionList(LoginRequiredMixin, ObjectList):
list_template = "partials/trend-direction-list.html"
page_title = "List of trend directions for a strategy."
page_subtitle = None
context_object_name_singular = "trend direction"
context_object_name = "trend directions"
list_url_name = "trenddirections"
list_url_args = ["type", "strategy_id"]
def get_queryset(self, **kwargs):
strategy_id = kwargs.get("strategy_id", None)
self.extra_context = {"strategy_id": strategy_id}
try:
strategy = Strategy.objects.get(id=strategy_id, user=self.request.user)
except Strategy.DoesNotExist:
context = {"message": "Strategy does not exist", "class": "danger"}
return self.render_to_response(context)
return strategy.trends
class TrendDirectionFlip(LoginRequiredMixin, ObjectNameMixin, View):
template_name = "partials/notify.html"
model = Strategy
def get(self, request, strategy_id, symbol):
try:
strategy = Strategy.objects.get(id=strategy_id, user=request.user)
except Strategy.DoesNotExist:
context = {"message": "Strategy does not exist", "class": "danger"}
return render(request, self.template_name, context)
if symbol not in strategy.trends:
context = {"message": "Trend direction does not exist", "class": "danger"}
return render(request, self.template_name, context)
if strategy.trends[symbol] == "buy":
strategy.trends[symbol] = "sell"
elif strategy.trends[symbol] == "sell":
strategy.trends[symbol] = "buy"
strategy.save()
context = {"message": "Trend direction updated", "class": "success"}
return render(request, self.template_name, context)
class TrendDirectionDelete(LoginRequiredMixin, ObjectNameMixin, View):
template_name = "partials/notify.html"
model = Strategy
def delete(self, request, strategy_id, symbol):
try:
strategy = Strategy.objects.get(id=strategy_id, user=request.user)
except Strategy.DoesNotExist:
context = {"message": "Strategy does not exist", "class": "danger"}
return render(request, self.template_name, context)
if symbol not in strategy.trends:
context = {"message": "Trend direction does not exist", "class": "danger"}
return render(request, self.template_name, context)
del strategy.trends[symbol]
strategy.save()
context = {"message": "Trend direction deleted", "class": "success"}
return render(request, self.template_name, context)
# Trading times
class TradingTimeList(LoginRequiredMixin, ObjectList):
list_template = "partials/trading-time-list.html"
model = TradingTime
page_title = "List of allowed trading times. Used as options for a strategy."
page_subtitle = "Add times here in order to permit trading."
list_url_name = "tradingtimes"
list_url_args = ["type"]
submit_url_name = "tradingtime_create"
class TradingTimeCreate(LoginRequiredMixin, ObjectCreate):
model = TradingTime
form_class = TradingTimeForm
submit_url_name = "tradingtime_create"
class TradingTimeUpdate(LoginRequiredMixin, ObjectUpdate):
model = TradingTime
form_class = TradingTimeForm
submit_url_name = "tradingtime_update"
class TradingTimeDelete(LoginRequiredMixin, ObjectDelete):
model = TradingTime