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

@@ -1,4 +1,4 @@
import ccxt
from alpaca.trading.client import TradingClient
import stripe
from django.conf import settings
from django.contrib.auth.models import AbstractUser
@@ -70,7 +70,6 @@ class User(AbstractUser):
class Account(models.Model):
EXCHANGE_CHOICES = (
("binance", "Binance"),
("alpaca", "Alpaca"),
)
user = models.ForeignKey(User, on_delete=models.CASCADE)
@@ -80,6 +79,10 @@ class Account(models.Model):
api_secret = models.CharField(max_length=255)
sandbox = models.BooleanField(default=False)
def get_account(self):
trading_client = TradingClient(self.api_key, self.api_secret, paper=self.sandbox)
return trading_client.get_account()
class Session(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
@@ -138,11 +141,7 @@ class Trade(models.Model):
if self.response is None:
# the trade is not placed yet
if self.account.exchange == "alpaca":
account = ccxt.alpaca(
{"apiKey": self.account.api_key, "secret": self.account.api_secret}
)
if self.account.sandbox:
account.set_sandbox_mode(True)
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