From 8be50e5db748537b1e28f4b3ebbfadee64be8188 Mon Sep 17 00:00:00 2001 From: cardosofede Date: Wed, 4 Oct 2023 22:46:44 -0300 Subject: [PATCH] (feat) modify strategy v2 launcher --- .../scripts/strategy_v2_launcher.py | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/hummingbot_files/templates/master_bot_conf/scripts/strategy_v2_launcher.py b/hummingbot_files/templates/master_bot_conf/scripts/strategy_v2_launcher.py index 2f67432..ca5f44a 100644 --- a/hummingbot_files/templates/master_bot_conf/scripts/strategy_v2_launcher.py +++ b/hummingbot_files/templates/master_bot_conf/scripts/strategy_v2_launcher.py @@ -2,7 +2,7 @@ import inspect import os import importlib.util -from hummingbot.core.data_type.common import OrderType, PositionMode, TradeType +from hummingbot.core.data_type.common import OrderType, PositionMode, TradeType, PositionSide, PositionAction from hummingbot.smart_components.strategy_frameworks.data_types import ( ExecutorHandlerStatus, ) @@ -59,9 +59,37 @@ class StrategyV2Launcher(ScriptStrategyBase): self.executor_handlers[controller_config] = DirectionalTradingExecutorHandler(strategy=self, controller=controller) def on_stop(self): + for connector in self.connectors.keys(): + if self.is_perpetual(connector): + self.close_open_positions(connector) for executor_handler in self.executor_handlers.values(): executor_handler.stop() + @staticmethod + def is_perpetual(exchange): + """ + Checks if the exchange is a perpetual market. + """ + return "perpetual" in exchange + + def close_open_positions(self, exchange): + connector = self.connectors[exchange] + for trading_pair, position in connector.account_positions.items(): + if position.position_side == PositionSide.LONG: + self.sell(connector_name=exchange, + trading_pair=position.trading_pair, + amount=abs(position.amount), + order_type=OrderType.MARKET, + price=connector.get_mid_price(position.trading_pair), + position_action=PositionAction.CLOSE) + elif position.position_side == PositionSide.SHORT: + self.buy(connector_name=exchange, + trading_pair=position.trading_pair, + amount=abs(position.amount), + order_type=OrderType.MARKET, + price=connector.get_mid_price(position.trading_pair), + position_action=PositionAction.CLOSE) + def on_tick(self): """ This shows you how you can start meta controllers. You can run more than one at the same time and based on the @@ -76,6 +104,7 @@ class StrategyV2Launcher(ScriptStrategyBase): return "Market connectors are not ready." lines = [] for controller_config, executor_handler in self.executor_handlers.items(): + lines.extend(["\n------------------------------------------------------------------------------------------"]) lines.extend([f"Strategy: {executor_handler.controller.config.strategy_name} | Config: {controller_config}", executor_handler.to_format_status()]) return "\n".join(lines)