2022-10-17 06:20:30 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2022-10-27 17:08:40 +00:00
|
|
|
from django.http import HttpResponseBadRequest
|
2022-10-17 06:20:30 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views import View
|
2022-10-27 17:08:40 +00:00
|
|
|
from rest_framework.parsers import FormParser
|
2022-10-17 06:20:30 +00:00
|
|
|
|
2022-10-27 17:08:40 +00:00
|
|
|
from core.lib import trades
|
|
|
|
from core.models import Account
|
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:
|
2022-10-27 17:08:40 +00:00
|
|
|
positions = account.client.get_all_positions()
|
|
|
|
print("POSITIONS", positions)
|
2022-10-21 23:15:27 +00:00
|
|
|
|
2022-10-27 17:08:40 +00:00
|
|
|
for item in positions:
|
|
|
|
item = dict(item)
|
|
|
|
item["account_id"] = account.id
|
|
|
|
item["unrealized_pl"] = float(item["unrealized_pl"])
|
|
|
|
items.append(item)
|
|
|
|
# try:
|
|
|
|
# parsed = ccxt_s.CCXTRoot.from_dict(order)
|
|
|
|
# except ValidationError as e:
|
|
|
|
# log.error(f"Error creating trade: {e}")
|
|
|
|
# return False
|
|
|
|
# self.status = parsed.status
|
|
|
|
# self.response = order
|
2022-10-21 23:15:27 +00:00
|
|
|
return items
|
2022-10-17 06:20:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Positions(LoginRequiredMixin, View):
|
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/positions.html"
|
|
|
|
|
|
|
|
async def get(self, request, type, account_id=None):
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest
|
|
|
|
template_name = f"wm/{type}.html"
|
|
|
|
unique = str(uuid.uuid4())[:8]
|
|
|
|
items = get_positions(request.user, account_id)
|
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
context = {
|
|
|
|
"title": f"Hooks ({type})",
|
|
|
|
"unique": unique,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
"items": items,
|
|
|
|
"type": type,
|
|
|
|
}
|
2022-10-21 23:15:27 +00:00
|
|
|
return render(request, template_name, context)
|
2022-10-27 17:08:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PositionAction(LoginRequiredMixin, View):
|
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/view-position.html"
|
|
|
|
parser_classes = [FormParser]
|
|
|
|
|
|
|
|
async def get(self, request, type, account_id, asset_id):
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
success, info = trades.get_position_info(account, asset_id)
|
|
|
|
print("INFO", info)
|
|
|
|
if not success:
|
|
|
|
message = "Position does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
|
|
|
|
items = get_positions(request.user, account_id)
|
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
context = {
|
|
|
|
"title": f"Hooks ({type})",
|
|
|
|
"unique": unique,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
"items": items,
|
|
|
|
"type": type,
|
|
|
|
}
|
|
|
|
if success:
|
|
|
|
context["items"] = info
|
|
|
|
else:
|
|
|
|
context["message"] = message
|
|
|
|
context["class"] = message_class
|
|
|
|
return render(request, template_name, context)
|