Implement viewing open positions
This commit is contained in:
@@ -18,6 +18,7 @@ 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"]
|
||||
@@ -46,7 +47,9 @@ class AccountInfo(LoginRequiredMixin, View):
|
||||
|
||||
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}
|
||||
account_info = {
|
||||
k: v for k, v in account_info.items() if k in self.VIEWABLE_FIELDS_MODEL
|
||||
}
|
||||
|
||||
if type == "page":
|
||||
type = "modal"
|
||||
@@ -61,6 +64,7 @@ class AccountInfo(LoginRequiredMixin, View):
|
||||
|
||||
return render(request, template_name, context)
|
||||
|
||||
|
||||
class Accounts(LoginRequiredMixin, View):
|
||||
allowed_types = ["modal", "widget", "window", "page"]
|
||||
window_content = "window-content/accounts.html"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import uuid
|
||||
|
||||
import orjson
|
||||
from alpaca.trading.client import TradingClient
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponse, HttpResponseBadRequest
|
||||
from django.shortcuts import render
|
||||
@@ -8,13 +9,12 @@ 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.models import Account, Callback, Hook
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger(__name__)
|
||||
|
||||
|
||||
@@ -23,10 +23,17 @@ def get_positions(user, account_id=None):
|
||||
accounts = Account.objects.filter(user=user)
|
||||
for account in accounts:
|
||||
if account.exchange == "alpaca":
|
||||
cast = {"api-key": account.api_key, "secret-key": account.api_secret, "paper": account.sandbox}
|
||||
trading_client = TradingClient(**cast)
|
||||
trading_client = TradingClient(
|
||||
account.api_key, account.api_secret, paper=account.sandbox
|
||||
)
|
||||
positions = trading_client.get_all_positions()
|
||||
print("POSITIONS", positions)
|
||||
|
||||
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:
|
||||
@@ -34,6 +41,7 @@ def get_positions(user, account_id=None):
|
||||
# return False
|
||||
# self.status = parsed.status
|
||||
# self.response = order
|
||||
return items
|
||||
|
||||
|
||||
class Positions(LoginRequiredMixin, View):
|
||||
@@ -55,4 +63,4 @@ class Positions(LoginRequiredMixin, View):
|
||||
"items": items,
|
||||
"type": type,
|
||||
}
|
||||
return render(request, template_name, context)
|
||||
return render(request, template_name, context)
|
||||
|
||||
Reference in New Issue
Block a user