Begin implementing pydantic validation for OANDA

This commit is contained in:
2022-10-31 08:58:08 +00:00
parent c15ae379f5
commit 8ee56b0e37
4 changed files with 106 additions and 4 deletions

View File

@@ -57,7 +57,7 @@ class AlpacaExchange(BaseExchange):
return (True, balance)
def get_market_value(self, symbol):
def get_market_value(self, symbol): # TODO: pydantic
try:
position = self.client.get_position(symbol)
except APIError as e:
@@ -65,7 +65,7 @@ class AlpacaExchange(BaseExchange):
return False
return float(position["market_value"])
def post_trade(self, trade):
def post_trade(self, trade): # TODO: pydantic
# the trade is not placed yet
if trade.direction == "buy":
direction = OrderSide.BUY

View File

@@ -4,10 +4,25 @@ from oandapyV20.endpoints import accounts, orders, positions, trades
from core.exchanges import BaseExchange
from core.lib.schemas import oanda_s
OANDA_SCHEMA_MAPPING = {}
OANDA_SCHEMA_MAPPING = {"OpenPositions": oanda_s.OpenPositions}
class OANDAExchange(BaseExchange):
def call(self, method, request):
self.client.request(request)
response = request.response
if isinstance(response, list):
response = {"itemlist": response}
if method not in self.schema:
self.log.error(f"Method cannot be validated: {method}")
self.log.debug(f"Response: {response}")
return (False, f"Method cannot be validated: {method}")
try:
return (True, self.schema[method](**response).dict())
except ValidationError as e:
self.log.error(f"Could not validate response: {e}")
return (False, e)
def set_schema(self):
self.schema = OANDA_SCHEMA_MAPPING
@@ -58,5 +73,9 @@ class OANDAExchange(BaseExchange):
def get_all_positions(self):
r = positions.OpenPositions(accountID=self.account_id)
self.client.request(r)
success, response = self.call("OpenPositions", r)
if not success:
return (success, response)
print("Positions", response)
return (True, [])