Implement strategies and posting trades
This commit is contained in:
@@ -1,18 +1,13 @@
|
||||
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.http import HttpResponseBadRequest
|
||||
from django.shortcuts import render
|
||||
from django.views import View
|
||||
from rest_framework.parsers import FormParser, JSONParser
|
||||
from rest_framework.views import APIView
|
||||
from serde import ValidationError
|
||||
from rest_framework.parsers import FormParser
|
||||
|
||||
from core.forms import HookForm
|
||||
from core.lib.serde import drakdoo_s
|
||||
from core.models import Account, Callback, Hook
|
||||
from core.lib import trades
|
||||
from core.models import Account
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger(__name__)
|
||||
@@ -22,25 +17,21 @@ def get_positions(user, account_id=None):
|
||||
items = []
|
||||
accounts = Account.objects.filter(user=user)
|
||||
for account in accounts:
|
||||
if account.exchange == "alpaca":
|
||||
trading_client = TradingClient(
|
||||
account.api_key, account.api_secret, paper=account.sandbox
|
||||
)
|
||||
positions = trading_client.get_all_positions()
|
||||
print("POSITIONS", positions)
|
||||
positions = account.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:
|
||||
# log.error(f"Error creating trade: {e}")
|
||||
# return False
|
||||
# self.status = parsed.status
|
||||
# self.response = order
|
||||
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
|
||||
return items
|
||||
|
||||
|
||||
@@ -64,3 +55,42 @@ class Positions(LoginRequiredMixin, View):
|
||||
"type": type,
|
||||
}
|
||||
return render(request, template_name, context)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user