Fix position list validation

This commit is contained in:
Mark Veidemanis 2022-11-29 07:20:39 +00:00
parent 6321fb9089
commit 4e1b574921
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 36 additions and 17 deletions

View File

@ -1,3 +1,5 @@
from abc import ABC, abstractmethod
from alpaca.common.exceptions import APIError from alpaca.common.exceptions import APIError
from glom import glom from glom import glom
from oandapyV20.exceptions import V20Error from oandapyV20.exceptions import V20Error
@ -61,7 +63,7 @@ def snake_to_camel(word):
return "".join(x.capitalize() or "_" for x in word.split("_")) return "".join(x.capitalize() or "_" for x in word.split("_"))
class BaseExchange(object): class BaseExchange(ABC):
def __init__(self, account): def __init__(self, account):
name = self.__class__.__name__ name = self.__class__.__name__
self.name = name.replace("Exchange", "").lower() self.name = name.replace("Exchange", "").lower()
@ -70,8 +72,9 @@ class BaseExchange(object):
self.connect() self.connect()
@abstractmethod
def connect(self): def connect(self):
raise NotImplementedError pass
@property @property
def schema(self): def schema(self):
@ -128,6 +131,7 @@ class BaseExchange(object):
def validate_response(self, response, method): def validate_response(self, response, method):
schema = self.get_schema(method) schema = self.get_schema(method)
# Return a dict of the validated response # Return a dict of the validated response
print("RESP", response)
response_valid = schema(**response).dict() response_valid = schema(**response).dict()
return response_valid return response_valid
@ -161,8 +165,9 @@ class BaseExchange(object):
# log.error(f"Error calling method: {e}") # log.error(f"Error calling method: {e}")
# raise GenericAPIError(e) # raise GenericAPIError(e)
@abstractmethod
def get_account(self): def get_account(self):
raise NotImplementedError pass
def extract_instrument(self, instruments, instrument): def extract_instrument(self, instruments, instrument):
for x in instruments["itemlist"]: for x in instruments["itemlist"]:
@ -170,38 +175,50 @@ class BaseExchange(object):
return x return x
return None return None
@abstractmethod
def get_currencies(self, symbols): def get_currencies(self, symbols):
raise NotImplementedError pass
@abstractmethod
def get_instruments(self): def get_instruments(self):
raise NotImplementedError pass
@abstractmethod
def get_supported_assets(self): def get_supported_assets(self):
raise NotImplementedError pass
@abstractmethod
def get_balance(self): def get_balance(self):
raise NotImplementedError pass
@abstractmethod
def get_market_value(self, symbol): def get_market_value(self, symbol):
raise NotImplementedError pass
@abstractmethod
def post_trade(self, trade): def post_trade(self, trade):
raise NotImplementedError pass
@abstractmethod
def get_trade(self, trade_id): def get_trade(self, trade_id):
raise NotImplementedError pass
@abstractmethod
def update_trade(self, trade): def update_trade(self, trade):
raise NotImplementedError pass
@abstractmethod
def cancel_trade(self, trade_id): def cancel_trade(self, trade_id):
raise NotImplementedError pass
@abstractmethod
def get_position_info(self, symbol): def get_position_info(self, symbol):
raise NotImplementedError pass
@abstractmethod
def get_all_positions(self): def get_all_positions(self):
raise NotImplementedError pass
@abstractmethod
def close_all_positions(self): def close_all_positions(self):
raise NotImplementedError pass

View File

@ -5,23 +5,25 @@ from pydantic import BaseModel
class PositionLong(BaseModel): class PositionLong(BaseModel):
units: str units: str
averagePrice: str averagePrice: str | None
pl: str pl: str
resettablePL: str resettablePL: str
financing: str financing: str
dividendAdjustment: str dividendAdjustment: str
guaranteedExecutionFees: str guaranteedExecutionFees: str
tradeIDs: list[str] tradeIDs: list[str] | None
unrealizedPL: str unrealizedPL: str
class PositionShort(BaseModel): class PositionShort(BaseModel):
units: str units: str
averagePrice: str | None
pl: str pl: str
resettablePL: str resettablePL: str
financing: str financing: str
dividendAdjustment: str dividendAdjustment: str
guaranteedExecutionFees: str guaranteedExecutionFees: str
tradeIDs: list[str] | None
unrealizedPL: str unrealizedPL: str