From 3a3918126154a3102f19682c22b1e429939cceb5 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 18 Dec 2022 15:10:28 +0000 Subject: [PATCH] Implement viewing and altering trends --- app/urls.py | 15 ++++ core/templates/base.html | 3 - core/templates/partials/strategy-list.html | 16 ++-- .../partials/trend-direction-list.html | 55 +++++++++++++ core/views/__init__.py | 2 + core/views/limits.py | 82 ++++++++++++++++++- 6 files changed, 160 insertions(+), 13 deletions(-) create mode 100644 core/templates/partials/trend-direction-list.html diff --git a/app/urls.py b/app/urls.py index 0b0ab0d..e82d7a5 100644 --- a/app/urls.py +++ b/app/urls.py @@ -193,4 +193,19 @@ urlpatterns = [ limits.TradingTimeDelete.as_view(), name="tradingtime_delete", ), + path( + "trend_directions//flip//", + limits.TrendDirectionFlip.as_view(), + name="trenddirection_flip", + ), + path( + "trend_directions//delete//", + limits.TrendDirectionDelete.as_view(), + name="trenddirection_delete", + ), + path( + "trend_directions//view//", + limits.TrendDirectionList.as_view(), + name="trenddirections", + ), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/core/templates/base.html b/core/templates/base.html index f513ded..506f9b3 100644 --- a/core/templates/base.html +++ b/core/templates/base.html @@ -267,9 +267,6 @@ diff --git a/core/templates/partials/trend-direction-list.html b/core/templates/partials/trend-direction-list.html new file mode 100644 index 0000000..010a6fe --- /dev/null +++ b/core/templates/partials/trend-direction-list.html @@ -0,0 +1,55 @@ +{% include 'partials/notify.html' %} + + + + + + + + {% for key, item in object_list.items %} + + + + + + {% endfor %} + +
symboldirectionactions
{{ key }}{{ item }} +
+ + +
+
diff --git a/core/views/__init__.py b/core/views/__init__.py index 9e62ace..94de40f 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -126,6 +126,8 @@ class ObjectList(RestrictedViewMixin, ObjectNameMixin, ListView): for arg in self.list_url_args: if arg in locals(): list_url_args[arg] = locals()[arg] + elif arg in kwargs: + list_url_args[arg] = kwargs[arg] orig_type = type if type == "page": diff --git a/core/views/limits.py b/core/views/limits.py index a23bb8f..689aaaf 100644 --- a/core/views/limits.py +++ b/core/views/limits.py @@ -1,13 +1,91 @@ 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 TradingTime +from core.models import Strategy, TradingTime from core.util import logs -from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate +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" + # model = TrendDirection + 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"] + + # submit_url_name = "trenddirection_create" + + 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