Improve posting trades to OANDA and make everything more robust

This commit is contained in:
2022-11-10 19:27:46 +00:00
parent bf863f43b2
commit af9f874209
11 changed files with 267 additions and 30 deletions

View File

@@ -164,6 +164,18 @@ class BaseExchange(object):
def get_account(self):
raise NotImplementedError
def extract_instrument(self, instruments, instrument):
for x in instruments["itemlist"]:
if x["name"] == instrument:
return x
return None
def get_currencies(self, symbols):
raise NotImplementedError
def get_instruments(self):
raise NotImplementedError
def get_supported_assets(self):
raise NotImplementedError

View File

@@ -1,5 +1,5 @@
from oandapyV20 import API
from oandapyV20.endpoints import accounts, orders, positions
from oandapyV20.endpoints import accounts, orders, positions, pricing
from core.exchanges import BaseExchange
@@ -20,9 +20,20 @@ class OANDAExchange(BaseExchange):
r = accounts.AccountDetails(self.account_id)
return self.call(r)
def get_supported_assets(self):
def get_instruments(self):
r = accounts.AccountInstruments(accountID=self.account_id)
response = self.call(r)
return response
def get_currencies(self, currencies):
params = {"instruments": ",".join(currencies)}
r = pricing.PricingInfo(accountID=self.account_id, params=params)
response = self.call(r)
return response
def get_supported_assets(self, response=None):
if not response:
response = self.get_instruments()
return [x["name"] for x in response["itemlist"]]
def get_balance(self):
@@ -43,19 +54,22 @@ class OANDAExchange(BaseExchange):
# "price": "1.5000", - added later
"stopLossOnFill": {"timeInForce": "GTC", "price": str(trade.stop_loss)},
"takeProfitOnFill": {"price": str(trade.take_profit)},
"timeInForce": "IOC",
# "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)
trade.response = response
trade.status = "posted"
trade.order_id = response["id"]
trade.client_order_id = response["requestID"]
trade.save()
return response
def get_trade(self, trade_id):