Add Elasticsearch support

This commit is contained in:
2022-12-13 07:20:49 +00:00
parent 50820172b1
commit 8afe638f0d
7 changed files with 78 additions and 7 deletions

View File

@@ -7,7 +7,7 @@ from alpaca.trading.requests import (
MarketOrderRequest,
)
from core.exchanges import BaseExchange, ExchangeError, GenericAPIError
from core.exchanges import BaseExchange, ExchangeError, GenericAPIError, common
class AlpacaExchange(BaseExchange):
@@ -38,6 +38,13 @@ class AlpacaExchange(BaseExchange):
except ValueError:
raise GenericAPIError(f"Balance is not a float: {equity}")
common.get_balance_hook(
self.account.user.id,
self.account.user.username,
self.account.id,
self.account.name,
balance,
)
return balance
def get_market_value(self, symbol): # TODO: pydantic

18
core/exchanges/common.py Normal file
View File

@@ -0,0 +1,18 @@
from core.lib.elastic import store_msg
def get_balance_hook(user_id, user_name, account_id, account_name, balance):
"""
Called every time the balance is fetched on an account.
Store this into Elasticsearch.
"""
store_msg(
"balances",
{
"user_id": user_id,
"user_name": user_name,
"account_id": account_id,
"account_name": account_name,
"balance": balance,
},
)

View File

@@ -1,7 +1,7 @@
from oandapyV20 import API
from oandapyV20.endpoints import accounts, orders, positions, pricing, trades
from core.exchanges import BaseExchange
from core.exchanges import BaseExchange, common
class OANDAExchange(BaseExchange):
@@ -39,7 +39,16 @@ class OANDAExchange(BaseExchange):
def get_balance(self):
r = accounts.AccountSummary(self.account_id)
response = self.call(r)
return float(response["balance"])
balance = float(response["balance"])
common.get_balance_hook(
self.account.user.id,
self.account.user.username,
self.account.id,
self.account.name,
balance,
)
return balance
def get_market_value(self, symbol):
raise NotImplementedError