From ee7f10f575a33ade59f85bcb056a922dfd6c4858 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 20 Jan 2023 00:07:32 +0100 Subject: [PATCH] [Exchange] fix open position check and set_symbol_position_mode tmp --- .../exchanges/connectors/ccxt/ccxt_connector.py | 10 ++++++---- octobot_trading/exchanges/traders/trader.pxd | 3 ++- octobot_trading/exchanges/traders/trader.py | 7 +++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/octobot_trading/exchanges/connectors/ccxt/ccxt_connector.py b/octobot_trading/exchanges/connectors/ccxt/ccxt_connector.py index 8bcfed2232..df8fff5b96 100644 --- a/octobot_trading/exchanges/connectors/ccxt/ccxt_connector.py +++ b/octobot_trading/exchanges/connectors/ccxt/ccxt_connector.py @@ -427,9 +427,11 @@ async def get_positions(self, symbols=None, **kwargs: dict) -> list: ] async def get_position(self, symbol: str, **kwargs: dict) -> dict: - return self.adapter.adapt_position( - await self.client.fetch_position(symbol=symbol, params=kwargs) - ) + if self.client.has.get("fetchPosition"): + return self.adapter.adapt_position( + await self.client.fetch_position(symbol=symbol, params=kwargs) + ) + raise NotImplementedError("get_position is not implemented") async def get_funding_rate(self, symbol: str, **kwargs: dict) -> dict: return self.adapter.adapt_funding_rate( @@ -449,7 +451,7 @@ async def set_symbol_margin_type(self, symbol: str, isolated: bool): marginType=self.CCXT_ISOLATED if isolated else self.CCXT_CROSSED) async def set_symbol_position_mode(self, symbol: str, one_way: bool): - return await self.client.set_position_mode(self, hedged=not one_way, symbol=symbol) + return await self.client.set_position_mode(hedged=not one_way, symbol=symbol) async def set_symbol_partial_take_profit_stop_loss(self, symbol: str, inverse: bool, tp_sl_mode: enums.TakeProfitStopLossMode): diff --git a/octobot_trading/exchanges/traders/trader.pxd b/octobot_trading/exchanges/traders/trader.pxd index b08001565f..7bd149e51b 100644 --- a/octobot_trading/exchanges/traders/trader.pxd +++ b/octobot_trading/exchanges/traders/trader.pxd @@ -40,4 +40,5 @@ cdef class Trader(util.Initializable): cpdef object set_risk(self, object risk) cpdef object convert_order_to_trade(self, object order) - cdef bint _has_open_position(self, str symbol) + # any() cant be cythonized + # cdef bool _has_open_position(self, str symbol) diff --git a/octobot_trading/exchanges/traders/trader.py b/octobot_trading/exchanges/traders/trader.py index 28702a19f9..bccb807422 100644 --- a/octobot_trading/exchanges/traders/trader.py +++ b/octobot_trading/exchanges/traders/trader.py @@ -670,5 +670,8 @@ def _has_open_position(self, symbol): :param symbol: the position symbol :return: True if open position for :symbol: exists """ - return len(self.exchange_manager.exchange_personal_data.positions_manager.get_symbol_positions( - symbol=symbol)) != 0 + return any( + position.size + for position in self.exchange_manager.exchange_personal_data.positions_manager.get_symbol_positions( + symbol=symbol + ))