Compare commits

...

28 Commits

Author SHA1 Message Date
Axel CH
4baaa84ef2
Merge 6b8ca7217b into af422c7cd4 2024-09-14 21:55:36 +02:00
Matthias
af422c7cd4
Merge pull request #10645 from dxbstyle/develop
added check for Kraken exchange
2024-09-14 10:44:23 +02:00
Matthias
51bdecea53 Improve check to cover more potential api oddities 2024-09-14 10:09:15 +02:00
Matthias
0f505c6d7b Improve check to cover more potential api oddities 2024-09-14 10:04:28 +02:00
Matthias
ae72f10448
Merge pull request #10619 from KingND/pixel/feat/freqai_labels_are_okay_in_lookahead_analysis
feat: include lookahead-analysis table caption when biased_indicator is likely from FreqAI target
2024-09-14 09:51:38 +02:00
Axel-CH
6b8ca7217b flake8 fix and cleanup 2024-09-11 21:48:48 -04:00
Axel-CH
2811a470aa handle pre existing open order cancelation on trade exit 2024-09-11 21:11:20 -04:00
Axel-CH
1fccdd8cda fix test_exit_positions 2024-09-11 18:59:29 -04:00
Axel-CH
33b421014d remove irrelevant trade.has_open_orders conditions 2024-09-11 18:42:51 -04:00
Axel-CH
730bef2920 add comments and logs for failing test, create untied_assets propertyto Trade 2024-09-09 19:28:04 -04:00
dxbstyle
ae155c78c2 added check 2024-09-09 21:29:49 +02:00
Axel-CH
79ce1ddaef rollback on process_open_trade_positions. Adjust position only if there is no open order, change will be made on other PR 2024-09-09 13:30:07 -04:00
Axel-CH
714822c93c add ETC/BTC pair to conftest get_markets 2024-09-09 13:15:17 -04:00
Axel-CH
fb3787173f fix test_exit_positions_exception 2024-09-09 12:07:50 -04:00
KingND
f970454cb4 chore: ruff format 2024-09-08 13:57:48 -04:00
KingND
69678574d4 fix: support python 3.9 union type hinting 2024-09-08 12:04:58 -04:00
KingND
53cab5074b chore: refactor and cleanup tests 2024-09-08 12:04:58 -04:00
KingND
c6c65b1799 chore: flake8 2024-09-08 12:04:58 -04:00
KingND
bb9f64027a chore: improve language in docs 2024-09-08 12:04:58 -04:00
KingND
5f52fc4338 feat: update lookahead-analysis doc caveats to include info regarding the false positive on FreqAI targets 2024-09-08 12:04:58 -04:00
KingND
82e30c8519 feat: if a biased_indicator starting with & appears in a lookahead-analysis, caption the table with a note that freqai targets appearing here can be ignored 2024-09-08 12:04:58 -04:00
Axel-CH
e9ba0d2ce8 Merge remote-tracking branch 'origin/develop' into feature/proceed-exit-while-open-order 2024-09-06 17:29:07 -04:00
Axel-CH
6a580176ea Merge branch 'develop' into feature/proceed-exit-while-open-order 2024-07-01 17:49:40 -04:00
Axel-CH
910b3ad536 allow adjust trade position, even if there is an open order 2024-04-15 15:40:57 -04:00
Axel-CH
05cf4cab8e add has_open_entry_orders property to trade 2024-04-15 15:18:39 -04:00
Axel-CH
6752c3e288 add has_untied_assets, replace one has_open_orders condition by has_untied_assets in exit_positions 2024-04-15 14:43:40 -04:00
Axel-CH
faeda2a166 fix mypy error on has_open_position function 2024-04-14 00:40:12 -04:00
Axel-CH
3d67e0893a edit backtest_loop to check exit if trade has open position 2024-04-14 00:25:40 -04:00
10 changed files with 274 additions and 34 deletions

View File

@ -101,3 +101,4 @@ This could lead to a false-negative (the strategy will then be reported as non-b
- `lookahead-analysis` has access to everything that backtesting has too.
Please don't provoke any configs like enabling position stacking.
If you decide to do so, then make doubly sure that you won't ever run out of `max_open_trades` amount and neither leftover money in your wallet.
- In the results table, the `biased_indicators` column will falsely flag FreqAI target indicators defined in `set_freqai_targets()` as biased. These are not biased and can safely be ignored.

View File

@ -78,6 +78,7 @@ class Kraken(Exchange):
# x["side"], x["amount"],
)
for x in orders
if x["remaining"] is not None and (x["side"] == "sell" or x["price"] is not None)
]
for bal in balances:
if not isinstance(balances[bal], dict):

View File

@ -715,8 +715,6 @@ class FreqtradeBot(LoggingMixin):
"""
# Walk through each pair and check if it needs changes
for trade in Trade.get_open_trades():
# If there is any open orders, wait for them to finish.
# TODO Remove to allow mul open orders
if not trade.has_open_orders:
# Do a wallets update (will be ratelimited to once per hour)
self.wallets.update(False)
@ -724,8 +722,7 @@ class FreqtradeBot(LoggingMixin):
self.check_and_call_adjust_trade_position(trade)
except DependencyException as exception:
logger.warning(
f"Unable to adjust position of trade for {trade.pair}: {exception}"
)
f"Unable to adjust position of trade for {trade.pair}: {exception}")
def check_and_call_adjust_trade_position(self, trade: Trade):
"""
@ -1251,8 +1248,7 @@ class FreqtradeBot(LoggingMixin):
trades_closed = 0
for trade in trades:
if (
not trade.has_open_orders
and not trade.has_open_sl_orders
not trade.has_open_sl_orders
and not self.wallets.check_exit_amount(trade)
):
logger.warning(
@ -1277,7 +1273,7 @@ class FreqtradeBot(LoggingMixin):
f"Unable to handle stoploss on exchange for {trade.pair}: {exception}"
)
# Check if we can sell our current pair
if not trade.has_open_orders and trade.is_open and self.handle_trade(trade):
if trade.is_open and self.handle_trade(trade):
trades_closed += 1
except DependencyException as exception:
@ -1421,7 +1417,7 @@ class FreqtradeBot(LoggingMixin):
self.handle_protections(trade.pair, trade.trade_direction)
return True
if trade.has_open_orders or not trade.is_open:
if not trade.is_open:
# Trade has an open order, Stoploss-handling can't happen in this case
# as the Amount on the exchange is tied up in another trade.
# The trade can be closed already (sell-order fill confirmation came in this iteration)
@ -1689,13 +1685,15 @@ class FreqtradeBot(LoggingMixin):
logger.warning(f"Unable to replace order for {trade.pair}: {exception}")
self.replace_order_failed(trade, f"Could not replace order for {trade}.")
def cancel_all_open_orders(self) -> None:
def cancel_open_orders_of_trade(self, trade: Trade, reason: str, sides: List[str]) -> None:
"""
Cancel all orders that are currently open
Cancel trade orders of specified sides that are currently open
:param trade: Trade object of the trade we're analyzing
:param reason: The reason for that cancelation
:param sides: The sides where cancellation should take place
:return: None
"""
for trade in Trade.get_open_trades():
for open_order in trade.open_orders:
try:
order = self.exchange.fetch_order(open_order.order_id, trade.pair)
@ -1703,15 +1701,30 @@ class FreqtradeBot(LoggingMixin):
logger.info("Can't query order for %s due to %s", trade, traceback.format_exc())
continue
for side in sides:
if (order["side"] == side):
if order["side"] == trade.entry_side:
self.handle_cancel_enter(
trade, order, open_order, constants.CANCEL_REASON["ALL_CANCELLED"]
trade, order, open_order, reason
)
elif order["side"] == trade.exit_side:
self.handle_cancel_exit(
trade, order, open_order, constants.CANCEL_REASON["ALL_CANCELLED"]
trade, order, open_order, reason
)
def cancel_all_open_orders(self) -> None:
"""
Cancel all orders that are currently open
:return: None
"""
for trade in Trade.get_open_trades():
self.cancel_open_orders_of_trade(
trade, constants.CANCEL_REASON["ALL_CANCELLED"],
[trade.entry_side, trade.exit_side]
)
Trade.commit()
def handle_cancel_enter(
@ -1957,6 +1970,14 @@ class FreqtradeBot(LoggingMixin):
limit = self.get_valid_price(custom_exit_price, proposed_limit_rate)
if trade.has_open_orders:
# cancel any open order of this trade
self.cancel_open_orders_of_trade(
trade, constants.CANCEL_REASON["REPLACE"],
[trade.exit_side]
)
Trade.commit()
# First cancelling stoploss on exchange ...
trade = self.cancel_stoploss_on_exchange(trade)

View File

@ -1,7 +1,7 @@
import logging
import time
from pathlib import Path
from typing import Any, Dict, List
from typing import Any, Dict, List, Union
import pandas as pd
from rich.text import Text
@ -19,7 +19,9 @@ logger = logging.getLogger(__name__)
class LookaheadAnalysisSubFunctions:
@staticmethod
def text_table_lookahead_analysis_instances(
config: Dict[str, Any], lookahead_instances: List[LookaheadAnalysis]
config: Dict[str, Any],
lookahead_instances: List[LookaheadAnalysis],
caption: Union[str, None] = None,
):
headers = [
"filename",
@ -65,7 +67,9 @@ class LookaheadAnalysisSubFunctions:
]
)
print_rich_table(data, headers, summary="Lookahead Analysis")
print_rich_table(
data, headers, summary="Lookahead Analysis", table_kwargs={"caption": caption}
)
return data
@staticmethod
@ -239,8 +243,24 @@ class LookaheadAnalysisSubFunctions:
# report the results
if lookaheadAnalysis_instances:
caption: Union[str, None] = None
if any(
[
any(
[
indicator.startswith("&")
for indicator in inst.current_analysis.false_indicators
]
)
for inst in lookaheadAnalysis_instances
]
):
caption = (
"Any indicators in 'biased_indicators' which are used within "
"set_freqai_targets() can be ignored."
)
LookaheadAnalysisSubFunctions.text_table_lookahead_analysis_instances(
config, lookaheadAnalysis_instances
config, lookaheadAnalysis_instances, caption=caption
)
if config.get("lookahead_analysis_exportfilename") is not None:
LookaheadAnalysisSubFunctions.export_to_csv(config, lookaheadAnalysis_instances)

View File

@ -1376,7 +1376,6 @@ class Backtesting:
self.wallets.update()
# 4. Create exit orders (if any)
if not trade.has_open_orders:
self._check_trade_exit(trade, row, current_time) # Place exit order if necessary
# 5. Process exit orders.

View File

@ -178,6 +178,7 @@ class Order(ModelBase):
return (
f"Order(id={self.id}, trade={self.ft_trade_id}, order_id={self.order_id}, "
f"side={self.side}, filled={self.safe_filled}, price={self.safe_price}, "
f"amount={self.amount}, "
f"status={self.status}, date={self.order_date_utc:{DATETIME_PRINT_FORMAT}})"
)
@ -585,6 +586,67 @@ class LocalTrade:
]
return len(open_orders_wo_sl) > 0
@property
def has_open_entry_orders(self) -> bool:
"""
True if there are open entry orders for this trade
"""
open_entry_orders = [
o for o in self.orders
if o.ft_order_side == self.entry_side and o.ft_is_open
]
return len(open_entry_orders) > 0
@property
def has_open_position(self) -> bool:
"""
True if there is an open position for this trade
"""
entry_orders = [
o for o in self.orders
if o.ft_order_side == self.entry_side
]
entry_orders_filled_qty = sum(eno.safe_filled for eno in entry_orders)
exit_orders = [
o for o in self.orders
if o.ft_order_side == self.exit_side
]
exit_orders_filled_qty = sum(exo.safe_filled for exo in exit_orders)
return (entry_orders_filled_qty - exit_orders_filled_qty) > 0
@property
def untied_assets(self) -> float:
entry_orders = [
o for o in self.orders
if o.ft_order_side == self.entry_side
]
entry_orders_filled_qty = sum(eno.safe_filled for eno in entry_orders)
exit_orders = [
o for o in self.orders
if o.ft_order_side == self.exit_side
]
exit_orders_remaining_qty = sum(exo.safe_remaining for exo in exit_orders)
untied_remaining = entry_orders_filled_qty - exit_orders_remaining_qty
logger.info(f"entry_orders: {entry_orders}")
logger.info(f"exit_orders: {exit_orders}")
logger.info(f"entry_orders_filled_qty: {entry_orders_filled_qty}")
logger.info(f"exit_orders_remaining_qty: {exit_orders_remaining_qty}")
logger.info(f"untied_remaining: {untied_remaining}")
return untied_remaining
@property
def has_untied_assets(self) -> bool:
"""
True if there is still remaining position not yet tied up to exit order
"""
return self.untied_assets > 0
@property
def open_sl_orders(self) -> List[Order]:
"""

View File

@ -964,6 +964,29 @@ def get_markets():
},
"info": {},
},
"ETC/BTC": {
"id": "ETCBTC",
"symbol": "ETC/BTC",
"base": "ETC",
"quote": "BTC",
"active": True,
"spot": True,
"swap": False,
"linear": None,
"type": "spot",
"contractSize": None,
"precision": {"base": 8, "quote": 8, "amount": 2, "price": 7},
"limits": {
"amount": {"min": 0.01, "max": 90000000.0},
"price": {"min": 1e-07, "max": 1000.0},
"cost": {"min": 0.0001, "max": 9000000.0},
"leverage": {
"min": None,
"max": None,
},
},
"info": {},
},
"ETH/USDT": {
"id": "USDT-ETH",
"symbol": "ETH/USDT",

View File

@ -1265,14 +1265,14 @@ def test_exit_positions(mocker, default_conf_usdt, limit_order, is_short, caplog
trades = [trade]
freqtrade.wallets.update()
n = freqtrade.exit_positions(trades)
assert n == 0
assert n == 1
# Test amount not modified by fee-logic
assert not log_has_re(r"Applying fee to amount for Trade .*", caplog)
gra = mocker.patch("freqtrade.freqtradebot.FreqtradeBot.get_real_amount", return_value=0.0)
# test amount modified by fee-logic
n = freqtrade.exit_positions(trades)
assert n == 0
assert n == 1
assert gra.call_count == 0
@ -1305,6 +1305,7 @@ def test_exit_positions_exception(mocker, default_conf_usdt, limit_order, caplog
ft_price=trade.open_rate,
order_id=order_id,
ft_is_open=False,
filled=11
)
)
Trade.session.add(trade)

View File

@ -1,3 +1,4 @@
import logging
import time
from unittest.mock import MagicMock
@ -347,8 +348,8 @@ def test_dca_short(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
assert trade.nr_of_successful_exits == 1
@pytest.mark.parametrize("leverage", [1, 2])
def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) -> None:
@pytest.mark.parametrize("leverage", [1])
def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker, caplog) -> None:
default_conf_usdt["position_adjustment_enable"] = True
default_conf_usdt["trading_mode"] = "futures"
default_conf_usdt["margin_mode"] = "isolated"
@ -478,10 +479,16 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker)
assert pytest.approx(trade.amount) == 91.689215 * leverage
assert pytest.approx(trade.orders[-1].amount) == 91.689215 * leverage
assert freqtrade.strategy.adjust_entry_price.call_count == 0
caplog.clear()
caplog.set_level(logging.DEBUG)
# Process again, should not adjust entry price
freqtrade.process()
trade = Trade.get_trades().first()
assert len(trade.orders) == 5
assert trade.orders[-2].status == "canceled"
assert len(trade.orders) == 6
assert trade.orders[-1].side == trade.exit_side
assert trade.orders[-1].status == "open"
assert trade.orders[-1].price == 2.02
# Adjust entry price cannot be called - this is an exit order

View File

@ -13,6 +13,12 @@ from freqtrade.optimize.analysis.lookahead_helpers import LookaheadAnalysisSubFu
from tests.conftest import EXMS, get_args, log_has_re, patch_exchange
IGNORE_BIASED_INDICATORS_CAPTION = (
"Any indicators in 'biased_indicators' which are used within "
"set_freqai_targets() can be ignored."
)
@pytest.fixture
def lookahead_conf(default_conf_usdt, tmp_path):
default_conf_usdt["user_data_dir"] = tmp_path
@ -133,6 +139,58 @@ def test_lookahead_helper_start(lookahead_conf, mocker) -> None:
text_table_mock.reset_mock()
@pytest.mark.parametrize(
"indicators, expected_caption_text",
[
(
["&indicator1", "indicator2"],
IGNORE_BIASED_INDICATORS_CAPTION,
),
(
["indicator1", "&indicator2"],
IGNORE_BIASED_INDICATORS_CAPTION,
),
(
["&indicator1", "&indicator2"],
IGNORE_BIASED_INDICATORS_CAPTION,
),
(["indicator1", "indicator2"], None),
([], None),
],
ids=(
"First of two biased indicators starts with '&'",
"Second of two biased indicators starts with '&'",
"Both biased indicators start with '&'",
"No biased indicators start with '&'",
"Empty biased indicators list",
),
)
def test_lookahead_helper_start__caption_based_on_indicators(
indicators, expected_caption_text, lookahead_conf, mocker
):
"""Test that the table caption is only populated if a biased_indicator starts with '&'."""
single_mock = MagicMock()
lookahead_analysis = LookaheadAnalysis(
lookahead_conf,
{"name": "strategy_test_v3_with_lookahead_bias"},
)
lookahead_analysis.current_analysis.false_indicators = indicators
single_mock.return_value = lookahead_analysis
text_table_mock = MagicMock()
mocker.patch.multiple(
"freqtrade.optimize.analysis.lookahead_helpers.LookaheadAnalysisSubFunctions",
initialize_single_lookahead_analysis=single_mock,
text_table_lookahead_analysis_instances=text_table_mock,
)
LookaheadAnalysisSubFunctions.start(lookahead_conf)
text_table_mock.assert_called_once_with(
lookahead_conf, [lookahead_analysis], caption=expected_caption_text
)
def test_lookahead_helper_text_table_lookahead_analysis_instances(lookahead_conf):
analysis = Analysis()
analysis.has_bias = True
@ -199,6 +257,53 @@ def test_lookahead_helper_text_table_lookahead_analysis_instances(lookahead_conf
assert len(data) == 3
@pytest.mark.parametrize(
"caption",
[
"",
"A test caption",
None,
False,
],
ids=(
"Pass empty string",
"Pass non-empty string",
"Pass None",
"Don't pass caption",
),
)
def test_lookahead_helper_text_table_lookahead_analysis_instances__caption(
caption,
lookahead_conf,
mocker,
):
"""Test that the caption is passed in the table kwargs when calling print_rich_table()."""
print_rich_table_mock = MagicMock()
mocker.patch(
"freqtrade.optimize.analysis.lookahead_helpers.print_rich_table",
print_rich_table_mock,
)
lookahead_analysis = LookaheadAnalysis(
lookahead_conf,
{
"name": "strategy_test_v3_with_lookahead_bias",
"location": Path(lookahead_conf["strategy_path"], f"{lookahead_conf['strategy']}.py"),
},
)
kwargs = {}
if caption is not False:
kwargs["caption"] = caption
LookaheadAnalysisSubFunctions.text_table_lookahead_analysis_instances(
lookahead_conf, [lookahead_analysis], **kwargs
)
assert print_rich_table_mock.call_args[-1]["table_kwargs"]["caption"] == (
caption if caption is not False else None
)
def test_lookahead_helper_export_to_csv(lookahead_conf):
import pandas as pd