2022-10-17 06:20:30 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.shortcuts import render
|
2023-02-10 20:49:35 +00:00
|
|
|
from mixins.views import ObjectList, ObjectRead
|
2022-11-28 19:45:22 +00:00
|
|
|
from two_factor.views.mixins import OTPRequiredMixin
|
2022-10-17 06:20:30 +00:00
|
|
|
|
2022-11-10 07:20:14 +00:00
|
|
|
from core.exchanges import GenericAPIError
|
2022-11-29 07:20:39 +00:00
|
|
|
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__)
|
|
|
|
|
|
|
|
|
2022-11-29 07:20:39 +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-12-08 07:20:07 +00:00
|
|
|
class Positions(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
|
2022-10-29 11:43:13 +00:00
|
|
|
list_template = "partials/position-list.html"
|
|
|
|
page_title = "Live positions from all exchanges"
|
|
|
|
page_subtitle = "Manual trades are editable under 'Bot Trades' tab."
|
2022-11-29 07:20:39 +00:00
|
|
|
context_object_name_singular = "position"
|
|
|
|
context_object_name = "positions"
|
2022-12-08 07:20:07 +00:00
|
|
|
|
|
|
|
list_url_name = "positions"
|
|
|
|
list_url_args = ["type", "account_id"]
|
|
|
|
|
2022-12-08 07:20:07 +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-08 07:20:07 +00:00
|
|
|
def get_queryset(self, **kwargs):
|
|
|
|
account_id = kwargs.get("account_id", None)
|
2022-12-02 07:20:37 +00:00
|
|
|
if account_id:
|
2022-12-08 07:20:07 +00:00
|
|
|
self.extra_context["account_id"] = account_id
|
|
|
|
items = []
|
2023-01-01 15:46:40 +00:00
|
|
|
# Only get enabled accounts for positions
|
|
|
|
accounts = Account.objects.filter(user=self.request.user, enabled=True)
|
2022-12-08 07:20:07 +00:00
|
|
|
for account in accounts:
|
|
|
|
try:
|
|
|
|
positions = account.client.get_all_positions()
|
|
|
|
except GenericAPIError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for item in positions:
|
|
|
|
items.append(item)
|
|
|
|
annotate_positions(items, self.request.user, return_order_ids=False)
|
|
|
|
return items
|
|
|
|
|
2022-10-27 17:08:40 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
class PositionAction(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
|
|
|
|
detail_template = "partials/position-detail.html"
|
2022-10-27 17:08:40 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
context_object_name_singular = "position"
|
2022-12-08 07:20:46 +00:00
|
|
|
context_object_name = "position info"
|
2022-10-27 17:08:40 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
detail_url_name = "position_action"
|
|
|
|
detail_url_args = ["type", "account_id", "symbol"]
|
2022-10-27 17:08:40 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
def get_object(self, **kwargs):
|
|
|
|
account_id = kwargs.get("account_id")
|
|
|
|
symbol = kwargs.get("symbol")
|
|
|
|
account = Account.get_by_id(account_id, self.request.user)
|
|
|
|
info = account.client.get_position_info(symbol)
|
2022-12-02 07:20:37 +00:00
|
|
|
del info["long"]
|
|
|
|
del info["short"]
|
2022-12-08 07:20:46 +00:00
|
|
|
valid_trade_ids = list(
|
|
|
|
annotate_positions([info], self.request.user, return_order_ids=True)
|
|
|
|
)
|
|
|
|
self.extra_context = {"valid_trade_ids": valid_trade_ids}
|
|
|
|
return info
|
2022-12-02 07:20:37 +00:00
|
|
|
|
|
|
|
def delete(self, request, account_id, side, symbol):
|
|
|
|
"""
|
|
|
|
Close a position.
|
|
|
|
"""
|
2023-02-10 20:49:35 +00:00
|
|
|
template_name = "mixins/partials/notify.html"
|
2022-12-02 07:20:37 +00:00
|
|
|
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
|