fisk/core/views/positions.py

170 lines
5.9 KiB
Python
Raw Normal View History

2022-10-17 06:20:30 +00:00
import uuid
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseBadRequest
2022-10-17 06:20:30 +00:00
from django.shortcuts import render
2022-12-02 07:20:37 +00:00
from django.urls import reverse
2022-10-17 06:20:30 +00:00
from django.views import View
from rest_framework.parsers import FormParser
2022-11-28 19:45:22 +00:00
from two_factor.views.mixins import OTPRequiredMixin
2022-10-17 06:20:30 +00:00
from core.exchanges import GenericAPIError
from core.models import Account, Trade
2022-10-17 06:20:30 +00:00
from core.util import logs
2022-10-21 23:15:27 +00:00
2022-10-17 06:20:30 +00:00
log = logs.get_logger(__name__)
def get_positions(user, account_id=None):
items = []
accounts = Account.objects.filter(user=user)
for account in accounts:
try:
positions = account.client.get_all_positions()
except GenericAPIError:
continue
2022-10-21 23:15:27 +00:00
for item in positions:
items.append(item)
2022-10-21 23:15:27 +00:00
return items
2022-10-17 06:20:30 +00:00
def annotate_positions(positions, user, return_order_ids=False):
"""
Annotate positions with trade information.
If return_order_ids is True, yield a list of order_ids instead of a list of trades.
:param positions: list of positions
:param user: user
:param return_order_ids: whether to return a generator of order_ids
:return: None or list of order_ids
:rtype: None or generator
"""
for item in positions:
try:
if "trade_ids" in item:
if return_order_ids:
for trade_id in Trade.objects.filter(
user=user, order_id__in=item["trade_ids"]
):
yield trade_id.order_id
else:
item["trades"] = Trade.objects.filter(
user=user, order_id__in=item["trade_ids"]
)
except Trade.DoesNotExist:
pass
2022-11-28 19:45:22 +00:00
class Positions(LoginRequiredMixin, OTPRequiredMixin, View):
2022-10-17 06:20:30 +00:00
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/objects.html"
list_template = "partials/position-list.html"
page_title = "Live positions from all exchanges"
page_subtitle = "Manual trades are editable under 'Bot Trades' tab."
context_object_name_singular = "position"
context_object_name = "positions"
2022-10-17 06:20:30 +00:00
2022-11-28 19:45:22 +00:00
def get(self, request, type, account_id=None):
2022-10-17 06:20:30 +00:00
if type not in self.allowed_types:
return HttpResponseBadRequest
2022-12-02 07:20:37 +00:00
self.template_name = f"wm/{type}.html"
2022-10-17 06:20:30 +00:00
unique = str(uuid.uuid4())[:8]
items = get_positions(request.user, account_id)
annotate_positions(items, request.user, return_order_ids=False)
2022-12-02 07:20:37 +00:00
orig_type = type
2022-10-17 06:20:30 +00:00
if type == "page":
type = "modal"
2022-12-02 07:20:37 +00:00
cast = {
"type": orig_type,
}
if account_id:
cast["account_id"] = account_id
list_url = reverse("positions", kwargs={**cast})
2022-10-17 06:20:30 +00:00
context = {
"title": f"Positions ({type})",
2022-10-17 06:20:30 +00:00
"unique": unique,
"window_content": self.window_content,
"list_template": self.list_template,
2022-10-17 06:20:30 +00:00
"items": items,
"type": type,
"page_title": self.page_title,
"page_subtitle": self.page_subtitle,
2022-12-02 07:20:37 +00:00
"list_url": list_url,
"context_object_name_singular": self.context_object_name_singular,
"context_object_name": self.context_object_name,
2022-11-29 07:20:39 +00:00
"widget_options": 'gs-w="12" gs-h="1" gs-y="0" gs-x="0"',
2022-10-17 06:20:30 +00:00
}
2022-12-02 07:20:37 +00:00
# Return partials for HTMX
if self.request.htmx:
if request.headers["HX-Target"] == self.context_object_name + "-table":
self.template_name = self.list_template
elif orig_type == "page":
2022-12-02 07:20:37 +00:00
self.template_name = self.list_template
else:
context["window_content"] = self.list_template
return render(request, self.template_name, context)
2022-11-28 19:45:22 +00:00
class PositionAction(LoginRequiredMixin, OTPRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/view-position.html"
parser_classes = [FormParser]
2022-11-28 19:45:22 +00:00
def get(self, request, type, account_id, symbol):
"""
Get live information for a trade.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest()
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
account = Account.get_by_id(account_id, request.user)
2022-11-04 07:20:55 +00:00
info = account.client.get_position_info(symbol)
valid_trade_ids = list(
annotate_positions([info], request.user, return_order_ids=True)
)
2022-12-02 07:20:37 +00:00
# Remove some fields from the info dict
del info["long"]
del info["short"]
if type == "page":
type = "modal"
context = {
"title": f"Position info ({type})",
"unique": unique,
"window_content": self.window_content,
"type": type,
"items": info,
"valid_trade_ids": valid_trade_ids,
}
2022-11-04 07:20:55 +00:00
return render(request, template_name, context)
2022-12-02 07:20:37 +00:00
def delete(self, request, account_id, side, symbol):
"""
Close a position.
"""
template_name = "partials/notify.html"
account = Account.get_by_id(account_id, request.user)
try:
api_response = account.client.close_position(side, symbol)
except GenericAPIError as e:
context = {"message": e, "class": "danger"}
return render(request, template_name, context)
if "longOrderCreateTransaction" in api_response:
context = {
"message": f"Long position closed on {symbol}",
"class": "success",
}
elif "shortOrderCreateTransaction" in api_response:
context = {
"message": f"Short position closed on {symbol}",
"class": "success",
}
response = render(request, template_name, context)
response["HX-Trigger"] = "positionEvent"
return response