Implement viewing live and DB information from account

This commit is contained in:
2022-10-21 23:57:32 +01:00
parent 1bdd49ee6a
commit 572b839c2c
10 changed files with 125 additions and 20 deletions

View File

@@ -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)