Implement viewing open positions

This commit is contained in:
2022-10-22 00:15:27 +01:00
parent 572b839c2c
commit 30d516ebf9
8 changed files with 81 additions and 72 deletions

View File

@@ -1,5 +1,5 @@
from alpaca.trading.client import TradingClient
import stripe
from alpaca.trading.client import TradingClient
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
@@ -69,9 +69,7 @@ class User(AbstractUser):
class Account(models.Model):
EXCHANGE_CHOICES = (
("alpaca", "Alpaca"),
)
EXCHANGE_CHOICES = (("alpaca", "Alpaca"),)
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
exchange = models.CharField(choices=EXCHANGE_CHOICES, max_length=255)
@@ -80,7 +78,9 @@ class Account(models.Model):
sandbox = models.BooleanField(default=False)
def get_account(self):
trading_client = TradingClient(self.api_key, self.api_secret, paper=self.sandbox)
trading_client = TradingClient(
self.api_key, self.api_secret, paper=self.sandbox
)
return trading_client.get_account()
@@ -121,9 +121,7 @@ class Trade(models.Model):
stop_loss = models.FloatField(null=True, blank=True)
take_profit = models.FloatField(null=True, blank=True)
status = models.CharField(max_length=255, null=True, blank=True)
direction = models.CharField(
choices=DIRECTION_CHOICES, max_length=255
)
direction = models.CharField(choices=DIRECTION_CHOICES, max_length=255)
# To populate from the trade
order_id = models.CharField(max_length=255, null=True, blank=True)
@@ -141,7 +139,9 @@ class Trade(models.Model):
if self.response is None:
# the trade is not placed yet
if self.account.exchange == "alpaca":
trading_client = TradingClient(self.account.api_key, self.account.api_secret, paper=self.sandbox)
trading_client = TradingClient(
self.account.api_key, self.account.api_secret, paper=self.sandbox
)
if self.type == "market":
order = account.create_order(
self.symbol, self.type, self.direction, self.amount