Begin implementing OANDA
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import stripe
|
||||
from alpaca.common.exceptions import APIError
|
||||
from alpaca.trading.client import TradingClient
|
||||
from alpaca.trading.requests import GetAssetsRequest
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
|
||||
from core.exchanges.alpaca import AlpacaExchange
|
||||
from core.exchanges.oanda import OANDAExchange
|
||||
from core.lib import trades
|
||||
from core.lib.customers import get_or_create, update_customer_fields
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger(__name__)
|
||||
EXCHANGE_MAP = {"alpaca": AlpacaExchange, "oanda": OANDAExchange}
|
||||
|
||||
|
||||
class Plan(models.Model):
|
||||
@@ -70,7 +70,7 @@ class User(AbstractUser):
|
||||
|
||||
|
||||
class Account(models.Model):
|
||||
EXCHANGE_CHOICES = (("alpaca", "Alpaca"),)
|
||||
EXCHANGE_CHOICES = (("alpaca", "Alpaca"), ("oanda", "OANDA"))
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
name = models.CharField(max_length=255)
|
||||
exchange = models.CharField(choices=EXCHANGE_CHOICES, max_length=255)
|
||||
@@ -89,23 +89,18 @@ class Account(models.Model):
|
||||
"""
|
||||
Override the save function to update supported symbols.
|
||||
"""
|
||||
try:
|
||||
request = GetAssetsRequest(status="active", asset_class="crypto")
|
||||
assets = self.client.get_all_assets(filter=request)
|
||||
asset_list = [x["symbol"] for x in assets if "symbol" in x]
|
||||
self.supported_symbols = asset_list
|
||||
print("Supported symbols", self.supported_symbols)
|
||||
except APIError as e:
|
||||
log.error(f"Could not get asset list: {e}")
|
||||
# return False
|
||||
|
||||
client = self.get_client()
|
||||
if client:
|
||||
supported_symbols = client.get_supported_assets()
|
||||
if supported_symbols:
|
||||
self.supported_symbols = supported_symbols
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_client(self):
|
||||
trading_client = TradingClient(
|
||||
self.api_key, self.api_secret, paper=self.sandbox, raw_data=True
|
||||
)
|
||||
return trading_client
|
||||
if self.exchange in EXCHANGE_MAP:
|
||||
return EXCHANGE_MAP[self.exchange](self)
|
||||
else:
|
||||
raise Exception("Exchange not supported")
|
||||
|
||||
@property
|
||||
def client(self):
|
||||
@@ -114,6 +109,13 @@ class Account(models.Model):
|
||||
"""
|
||||
return self.get_client()
|
||||
|
||||
@property
|
||||
def rawclient(self):
|
||||
"""
|
||||
Convenience property for one-off API calls.
|
||||
"""
|
||||
return self.get_client().client
|
||||
|
||||
@classmethod
|
||||
def get_by_id(cls, account_id, user):
|
||||
return cls.objects.get(id=account_id, user=user)
|
||||
@@ -174,7 +176,7 @@ class Trade(models.Model):
|
||||
self._original = self
|
||||
|
||||
def post(self):
|
||||
return trades.post_trade(self)
|
||||
return self.account.client.post_trade(self)
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
# close the trade
|
||||
|
||||
Reference in New Issue
Block a user