72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
from oandapyV20 import API
|
|
from oandapyV20.endpoints import accounts, positions
|
|
|
|
from core.exchanges import BaseExchange
|
|
|
|
|
|
class OANDAExchange(BaseExchange):
|
|
def call_method(self, request):
|
|
self.client.request(request)
|
|
response = request.response
|
|
if isinstance(response, list):
|
|
response = {"itemlist": response}
|
|
return response
|
|
|
|
def connect(self):
|
|
self.client = API(access_token=self.account.api_secret)
|
|
self.account_id = self.account.api_key
|
|
|
|
def get_account(self):
|
|
r = accounts.AccountDetails(self.account_id)
|
|
self.client.request(r)
|
|
return r.response
|
|
|
|
def get_supported_assets(self):
|
|
return False
|
|
|
|
def get_balance(self):
|
|
raise NotImplementedError
|
|
|
|
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
|
|
|
|
def get_trade(self, trade_id):
|
|
r = accounts.TradeDetails(accountID=self.account_id, tradeID=trade_id)
|
|
self.client.request(r)
|
|
return r.response
|
|
|
|
def update_trade(self, trade):
|
|
raise NotImplementedError
|
|
# r = orders.OrderReplace(
|
|
# accountID=self.account_id, orderID=trade.order_id, data=data
|
|
# )
|
|
# self.client.request(r)
|
|
# return r.response
|
|
|
|
def cancel_trade(self, trade_id):
|
|
raise NotImplementedError
|
|
|
|
def get_position_info(self, symbol):
|
|
r = positions.PositionDetails(self.account_id, symbol)
|
|
self.client.request(r)
|
|
return r.response
|
|
|
|
def get_all_positions(self):
|
|
items = []
|
|
r = positions.OpenPositions(accountID=self.account_id)
|
|
response = self.call(r)
|
|
|
|
print("Positions", response)
|
|
for item in response["itemlist"]:
|
|
item["account"] = self.account.name
|
|
item["account_id"] = self.account.id
|
|
item["unrealized_pl"] = float(item["unrealized_pl"])
|
|
items.append(item)
|
|
return items
|