Implement viewing live and DB information from account
This commit is contained in:
@@ -18,6 +18,48 @@ def get_accounts(user):
|
||||
accounts = Account.objects.filter(user=user)
|
||||
return accounts
|
||||
|
||||
class AccountInfo(LoginRequiredMixin, View):
|
||||
VIEWABLE_FIELDS_MODEL = ["name", "exchange", "api_key", "sandbox"]
|
||||
allowed_types = ["modal", "widget", "window", "page"]
|
||||
window_content = "window-content/account-info.html"
|
||||
|
||||
def get(self, request, type, account_id):
|
||||
"""
|
||||
Get the account details.
|
||||
:param account_id: The id of the account.
|
||||
"""
|
||||
if type not in self.allowed_types:
|
||||
return HttpResponseBadRequest
|
||||
template_name = f"wm/{type}.html"
|
||||
unique = str(uuid.uuid4())[:8]
|
||||
try:
|
||||
account = Account.objects.get(id=account_id, user=request.user)
|
||||
except Account.DoesNotExist:
|
||||
message = "Account does not exist"
|
||||
message_class = "danger"
|
||||
context = {
|
||||
"message": message,
|
||||
"message_class": message_class,
|
||||
"window_content": self.window_content,
|
||||
}
|
||||
return render(request, template_name, context)
|
||||
|
||||
live_info = dict(account.get_account())
|
||||
account_info = account.__dict__
|
||||
account_info = {k:v for k,v in account_info.items() if k in self.VIEWABLE_FIELDS_MODEL}
|
||||
|
||||
if type == "page":
|
||||
type = "modal"
|
||||
context = {
|
||||
"db_info": account_info,
|
||||
"live_info": live_info,
|
||||
"account_id": account_id,
|
||||
"type": type,
|
||||
"unique": unique,
|
||||
"window_content": self.window_content,
|
||||
}
|
||||
|
||||
return render(request, template_name, context)
|
||||
|
||||
class Accounts(LoginRequiredMixin, View):
|
||||
allowed_types = ["modal", "widget", "window", "page"]
|
||||
|
||||
@@ -8,13 +8,13 @@ from django.views import View
|
||||
from rest_framework.parsers import FormParser, JSONParser
|
||||
from rest_framework.views import APIView
|
||||
from serde import ValidationError
|
||||
from alpaca.trading.client import TradingClient
|
||||
|
||||
|
||||
from core.forms import HookForm
|
||||
from core.lib.serde import drakdoo_s
|
||||
from core.models import Callback, Hook, Account
|
||||
from core.util import logs
|
||||
import ccxt
|
||||
from ccxt.base.errors import NotSupported
|
||||
log = logs.get_logger(__name__)
|
||||
|
||||
|
||||
@@ -22,14 +22,10 @@ def get_positions(user, account_id=None):
|
||||
items = []
|
||||
accounts = Account.objects.filter(user=user)
|
||||
for account in accounts:
|
||||
if hasattr(ccxt, account.exchange):
|
||||
instance = getattr(ccxt, account.exchange)({"apiKey": account.api_key, "secret": account.api_secret})
|
||||
if account.sandbox:
|
||||
instance.set_sandbox_mode(True)
|
||||
try:
|
||||
positions = instance.fetch_positions()
|
||||
except NotSupported:
|
||||
positions = [{"account": account.exchange, "error": "Not supported"}]
|
||||
if account.exchange == "alpaca":
|
||||
cast = {"api-key": account.api_key, "secret-key": account.api_secret, "paper": account.sandbox}
|
||||
trading_client = TradingClient(**cast)
|
||||
positions = trading_client.get_all_positions()
|
||||
print("POSITIONS", positions)
|
||||
# try:
|
||||
# parsed = ccxt_s.CCXTRoot.from_dict(order)
|
||||
|
||||
Reference in New Issue
Block a user