Remove asset filter and begin implementing posting trades

This commit is contained in:
2022-11-10 07:20:14 +00:00
parent 47384aed5f
commit 40f6330a13
7 changed files with 164 additions and 86 deletions

View File

@@ -1,4 +1,6 @@
from alpaca.common.exceptions import APIError
from glom import glom
from oandapyV20.exceptions import V20Error
from core.lib import schemas
from core.util import logs
@@ -135,7 +137,11 @@ class BaseExchange(object):
:raises NoSchema: If the method is not in the schema mapping
:raises ValidationError: If the response cannot be validated
"""
response = self.call_method(method, *args, **kwargs)
try:
response = self.call_method(method, *args, **kwargs)
except (APIError, V20Error) as e:
log.error(f"Error calling method {method}: {e}")
raise GenericAPIError(e)
try:
response_valid = self.validate_response(response, method)
except NoSchema as e:
@@ -147,6 +153,7 @@ class BaseExchange(object):
except NoSchema as e:
log.error(f"{e} - {response}")
response_converted = response_valid
# return (True, response_converted)
return response_converted

View File

@@ -1,5 +1,5 @@
from oandapyV20 import API
from oandapyV20.endpoints import accounts, positions
from oandapyV20.endpoints import accounts, orders, positions
from core.exchanges import BaseExchange
@@ -26,16 +26,37 @@ class OANDAExchange(BaseExchange):
return [x["name"] for x in response["itemlist"]]
def get_balance(self):
raise NotImplementedError
r = accounts.AccountSummary(self.account_id)
response = self.call(r)
return float(response["balance"])
def get_market_value(self, symbol):
raise NotImplementedError
def post_trade(self, trade):
raise NotImplementedError
# r = orders.OrderCreate(accountID, data=data)
# self.client.request(r)
# return r.response
if trade.direction == "sell":
amount = -trade.amount
else:
amount = trade.amount
data = {
"order": {
# "price": "1.5000", - added later
"stopLossOnFill": {"timeInForce": "GTC", "price": str(trade.stop_loss)},
"takeProfitOnFill": {"price": str(trade.take_profit)},
"timeInForce": "GTC",
"instrument": trade.symbol,
"units": str(amount),
"type": trade.type.upper(),
"positionFill": "DEFAULT",
}
}
print("SENDINGF ORDER", data)
if trade.type == "limit":
data["order"]["price"] = str(trade.price)
r = orders.OrderCreate(self.account_id, data=data)
response = self.call(r)
print("POSTED TRADE", response)
return response
def get_trade(self, trade_id):
r = accounts.TradeDetails(accountID=self.account_id, tradeID=trade_id)