Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IndexTrading] support historical config and fix chain rebalance #1389

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions Trading/Mode/index_trading_mode/index_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,43 @@ async def _rebalance_portfolio(self, details: dict):
return orders

async def _sell_indexed_coins_for_reference_market(self, details: dict) -> list:
removed_coins_to_sell_orders = []
if removed_coins_to_sell := list(details[RebalanceDetails.REMOVE.value]):
removed_coins_to_sell_orders = await trading_modes.convert_assets_to_target_asset(
self.trading_mode, removed_coins_to_sell,
self.exchange_manager.exchange_personal_data.portfolio_manager.reference_market, {}
)
if (
details[RebalanceDetails.REMOVE.value] and
not (
details[RebalanceDetails.BUY_MORE.value]
or details[RebalanceDetails.ADD.value]
or details[RebalanceDetails.SWAP.value]
)
):
# if rebalance is triggered by removed assets, make sure that the asset can actually be sold
# otherwise the whole rebalance is useless
sold_coins = [
symbol_util.parse_symbol(order.symbol).base
if order.side is trading_enums.TradeOrderSide.SELL
else symbol_util.parse_symbol(order.symbol).quote
for order in removed_coins_to_sell_orders
]
if not any(
asset in sold_coins
for asset in details[RebalanceDetails.REMOVE.value]
):
self.logger.info(
f"Cancelling rebalance: not enough {list(details[RebalanceDetails.REMOVE.value])} funds to sell"
)
raise trading_errors.MissingMinimalExchangeTradeVolume(
f"not enough {list(details[RebalanceDetails.REMOVE.value])} funds to sell"
)
order_coins_to_sell = self._get_coins_to_sell(details)
orders = await trading_modes.convert_assets_to_target_asset(
self.trading_mode, self._get_coins_to_sell(details),
self.trading_mode, order_coins_to_sell,
self.exchange_manager.exchange_personal_data.portfolio_manager.reference_market, {}
)
) + removed_coins_to_sell_orders
if orders:
# ensure orders are filled
await asyncio.gather(
Expand All @@ -106,7 +139,7 @@ async def _sell_indexed_coins_for_reference_market(self, details: dict) -> list:

def _get_coins_to_sell(self, details: dict) -> list:
return list(details[RebalanceDetails.SWAP.value]) or (
self.trading_mode.indexed_coins + list(details[RebalanceDetails.REMOVE.value])
self.trading_mode.indexed_coins
)

async def _ensure_enough_funds_to_buy_after_selling(self):
Expand Down Expand Up @@ -268,6 +301,7 @@ async def _check_index_if_necessary(self):
if (
current_time - self._last_trigger_time
) >= self.trading_mode.refresh_interval_days * commons_constants.DAYS_TO_SECONDS:
self.trading_mode.update_config_and_user_inputs_if_necessary()
if len(self.trading_mode.indexed_coins) < self.MIN_INDEXED_COINS:
self.logger.error(
f"At least {self.MIN_INDEXED_COINS} coin is required to maintain an index. Please "
Expand Down Expand Up @@ -489,6 +523,7 @@ def init_user_inputs(self, inputs: dict) -> None:
Called right before starting the tentacle, should define all the tentacle's user inputs unless
those are defined somewhere else.
"""
trading_config = self.trading_config
self.refresh_interval_days = float(self.UI.user_input(
IndexTradingModeProducer.REFRESH_INTERVAL, commons_enums.UserInputTypes.FLOAT,
self.refresh_interval_days, inputs,
Expand All @@ -503,14 +538,14 @@ def init_user_inputs(self, inputs: dict) -> None:
title="Rebalance cap: maximum allowed percent holding of a coin beyond initial ratios before "
"triggering a rebalance.",
))) / trading_constants.ONE_HUNDRED
self.sell_unindexed_traded_coins = self.trading_config.get(
self.sell_unindexed_traded_coins = trading_config.get(
IndexTradingModeProducer.SELL_UNINDEXED_TRADED_COINS,
self.sell_unindexed_traded_coins
)
if (not self.exchange_manager or not self.exchange_manager.is_backtesting) and \
authentication.Authenticator.instance().has_open_source_package():
self.UI.user_input(IndexTradingModeProducer.INDEX_CONTENT, commons_enums.UserInputTypes.OBJECT_ARRAY,
self.trading_config.get(IndexTradingModeProducer.INDEX_CONTENT, None), inputs,
trading_config.get(IndexTradingModeProducer.INDEX_CONTENT, None), inputs,
item_title="Coin",
other_schema_values={"minItems": 0, "uniqueItems": True},
title="Custom distribution: when used, only coins listed in this distribution and "
Expand Down Expand Up @@ -607,7 +642,7 @@ def get_removed_coins_from_config(self, available_traded_bases) -> list:
return removed_coins
current_coins = [
asset[index_distribution.DISTRIBUTION_NAME]
for asset in self.get_ideal_distribution()
for asset in (self.get_ideal_distribution() or [])
]
return list(set(removed_coins + [
asset[index_distribution.DISTRIBUTION_NAME]
Expand Down
64 changes: 57 additions & 7 deletions Trading/Mode/index_trading_mode/tests/test_index_trading_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ async def test_ohlcv_callback(tools):
await producer.ohlcv_callback("binance", "123", "BTC", "BTC/USDT", None, None)
ensure_index_mock.assert_not_called()
_notify_if_missing_too_many_coins_mock.assert_not_called()
get_exchange_current_time_mock.assert_called_once()
assert get_exchange_current_time_mock.call_count == 2
get_exchange_current_time_mock.reset_mock()
assert producer._last_trigger_time == current_time

Expand All @@ -361,7 +361,7 @@ async def test_ohlcv_callback(tools):
await producer.ohlcv_callback("binance", "123", "BTC", "BTC/USDT", None, None)
ensure_index_mock.assert_not_called()
_notify_if_missing_too_many_coins_mock.assert_not_called()
get_exchange_current_time_mock.assert_called_once()
assert get_exchange_current_time_mock.call_count == 1

assert producer._last_trigger_time == current_time
with mock.patch.object(
Expand All @@ -371,7 +371,7 @@ async def test_ohlcv_callback(tools):
await producer.ohlcv_callback("binance", "123", "BTC", "BTC/USDT", None, None)
ensure_index_mock.assert_called_once()
_notify_if_missing_too_many_coins_mock.assert_called_once()
get_exchange_current_time_mock.assert_called_once()
assert get_exchange_current_time_mock.call_count == 2
assert producer._last_trigger_time == current_time * 2


Expand Down Expand Up @@ -801,20 +801,70 @@ async def test_ensure_enough_funds_to_buy_after_selling(tools):
async def test_sell_indexed_coins_for_reference_market(tools):
update = {}
mode, producer, consumer, trader = await _init_mode(tools, _get_config(tools, update))
orders = [
mock.Mock(
symbol="BTC/USDT",
side=trading_enums.TradeOrderSide.SELL
),
mock.Mock(
symbol="ETH/USDT",
side=trading_enums.TradeOrderSide.SELL
)
]
with mock.patch.object(
octobot_trading.modes, "convert_assets_to_target_asset", mock.AsyncMock(return_value=["1", "2"])
octobot_trading.modes, "convert_assets_to_target_asset", mock.AsyncMock(return_value=orders)
) as convert_assets_to_target_asset_mock, mock.patch.object(
trading_personal_data, "wait_for_order_fill", mock.AsyncMock()
) as wait_for_order_fill_mock, mock.patch.object(
consumer, "_get_coins_to_sell", mock.Mock(return_value=[1, 2, 3])
) as _get_coins_to_sell_mock:
assert await consumer._sell_indexed_coins_for_reference_market("details") == ["1", "2"]
details = {
index_trading.RebalanceDetails.REMOVE.value: {}
}
assert await consumer._sell_indexed_coins_for_reference_market(details) == orders
convert_assets_to_target_asset_mock.assert_called_once_with(
mode, [1, 2, 3],
consumer.exchange_manager.exchange_personal_data.portfolio_manager.reference_market, {}
)
assert wait_for_order_fill_mock.call_count == 2
_get_coins_to_sell_mock.assert_called_once_with("details")
_get_coins_to_sell_mock.assert_called_once_with(details)
convert_assets_to_target_asset_mock.reset_mock()
wait_for_order_fill_mock.reset_mock()
_get_coins_to_sell_mock.reset_mock()

# with valid remove coins
details = {
index_trading.RebalanceDetails.REMOVE.value: {"BTC": 0.01},
index_trading.RebalanceDetails.BUY_MORE.value: {},
index_trading.RebalanceDetails.ADD.value: {},
index_trading.RebalanceDetails.SWAP.value: {},
}
assert await consumer._sell_indexed_coins_for_reference_market(details) == orders + orders
assert convert_assets_to_target_asset_mock.call_count == 2
assert wait_for_order_fill_mock.call_count == 4
_get_coins_to_sell_mock.assert_called_once_with(details)
convert_assets_to_target_asset_mock.reset_mock()
wait_for_order_fill_mock.reset_mock()
_get_coins_to_sell_mock.reset_mock()

with mock.patch.object(
octobot_trading.modes, "convert_assets_to_target_asset", mock.AsyncMock(return_value=[])
) as convert_assets_to_target_asset_mock_2:
# with remove coins that can't be sold
details = {
index_trading.RebalanceDetails.REMOVE.value: {"BTC": 0.01},
index_trading.RebalanceDetails.BUY_MORE.value: {},
index_trading.RebalanceDetails.ADD.value: {},
index_trading.RebalanceDetails.SWAP.value: {},
}
with pytest.raises(trading_errors.MissingMinimalExchangeTradeVolume):
assert await consumer._sell_indexed_coins_for_reference_market(details) == orders + orders
convert_assets_to_target_asset_mock_2.assert_called_once_with(
mode, ["BTC"],
consumer.exchange_manager.exchange_personal_data.portfolio_manager.reference_market, {}
)
wait_for_order_fill_mock.assert_not_called()
_get_coins_to_sell_mock.assert_not_called()


async def test_get_coins_to_sell(tools):
Expand Down Expand Up @@ -864,7 +914,7 @@ async def test_get_coins_to_sell(tools):
},
index_trading.RebalanceDetails.ADD.value: {},
index_trading.RebalanceDetails.SWAP.value: {},
}) == ["BTC", "ETH", "DOGE", "SHIB", "XRP"]
}) == ["BTC", "ETH", "DOGE", "SHIB"]


async def test_resolve_swaps(tools):
Expand Down
Loading