|
| 1 | +from datetime import timedelta |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from freqtrade.enums import CandleType |
| 7 | +from freqtrade.exceptions import RetryableOrderError |
| 8 | +from freqtrade.exchange.common import API_RETRY_COUNT |
| 9 | +from freqtrade.util import dt_now, dt_ts |
| 10 | +from tests.conftest import EXMS, get_patched_exchange |
| 11 | +from tests.exchange.test_exchange import ccxt_exceptionhandlers |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.usefixtures("init_persistence") |
| 15 | +def test_fetch_stoploss_order_bitget(default_conf, mocker): |
| 16 | + default_conf["dry_run"] = False |
| 17 | + mocker.patch("freqtrade.exchange.common.time.sleep") |
| 18 | + api_mock = MagicMock() |
| 19 | + |
| 20 | + exchange = get_patched_exchange(mocker, default_conf, api_mock, exchange="bitget") |
| 21 | + |
| 22 | + api_mock.fetch_open_orders = MagicMock(return_value=[]) |
| 23 | + api_mock.fetch_canceled_and_closed_orders = MagicMock(return_value=[]) |
| 24 | + |
| 25 | + with pytest.raises(RetryableOrderError): |
| 26 | + exchange.fetch_stoploss_order("1234", "ETH/BTC") |
| 27 | + assert api_mock.fetch_open_orders.call_count == API_RETRY_COUNT + 1 |
| 28 | + assert api_mock.fetch_canceled_and_closed_orders.call_count == API_RETRY_COUNT + 1 |
| 29 | + |
| 30 | + api_mock.fetch_open_orders.reset_mock() |
| 31 | + api_mock.fetch_canceled_and_closed_orders.reset_mock() |
| 32 | + |
| 33 | + api_mock.fetch_canceled_and_closed_orders = MagicMock( |
| 34 | + return_value=[{"id": "1234", "status": "closed", "clientOrderId": "123455"}] |
| 35 | + ) |
| 36 | + api_mock.fetch_open_orders = MagicMock(return_value=[{"id": "50110", "clientOrderId": "1234"}]) |
| 37 | + |
| 38 | + resp = exchange.fetch_stoploss_order("1234", "ETH/BTC") |
| 39 | + assert api_mock.fetch_open_orders.call_count == 2 |
| 40 | + assert api_mock.fetch_canceled_and_closed_orders.call_count == 2 |
| 41 | + |
| 42 | + assert resp["id"] == "1234" |
| 43 | + assert resp["id_stop"] == "50110" |
| 44 | + assert resp["type"] == "stoploss" |
| 45 | + |
| 46 | + default_conf["dry_run"] = True |
| 47 | + exchange = get_patched_exchange(mocker, default_conf, api_mock, exchange="bitget") |
| 48 | + dro_mock = mocker.patch(f"{EXMS}.fetch_dry_run_order", MagicMock(return_value={"id": "123455"})) |
| 49 | + |
| 50 | + api_mock.fetch_open_orders.reset_mock() |
| 51 | + api_mock.fetch_canceled_and_closed_orders.reset_mock() |
| 52 | + resp = exchange.fetch_stoploss_order("1234", "ETH/BTC") |
| 53 | + |
| 54 | + assert api_mock.fetch_open_orders.call_count == 0 |
| 55 | + assert api_mock.fetch_canceled_and_closed_orders.call_count == 0 |
| 56 | + assert dro_mock.call_count == 1 |
| 57 | + |
| 58 | + |
| 59 | +def test_fetch_stoploss_order_bitget_exceptions(default_conf_usdt, mocker): |
| 60 | + default_conf_usdt["dry_run"] = False |
| 61 | + api_mock = MagicMock() |
| 62 | + |
| 63 | + # Test emulation of the stoploss getters |
| 64 | + api_mock.fetch_canceled_and_closed_orders = MagicMock(return_value=[]) |
| 65 | + |
| 66 | + ccxt_exceptionhandlers( |
| 67 | + mocker, |
| 68 | + default_conf_usdt, |
| 69 | + api_mock, |
| 70 | + "bitget", |
| 71 | + "fetch_stoploss_order", |
| 72 | + "fetch_open_orders", |
| 73 | + retries=API_RETRY_COUNT + 1, |
| 74 | + order_id="12345", |
| 75 | + pair="ETH/USDT", |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def test_bitget_ohlcv_candle_limit(mocker, default_conf_usdt): |
| 80 | + # This test is also a live test - so we're sure our limits are correct. |
| 81 | + api_mock = MagicMock() |
| 82 | + api_mock.options = { |
| 83 | + "fetchOHLCV": { |
| 84 | + "maxRecentDaysPerTimeframe": { |
| 85 | + "1m": 30, |
| 86 | + "5m": 30, |
| 87 | + "15m": 30, |
| 88 | + "30m": 30, |
| 89 | + "1h": 60, |
| 90 | + "4h": 60, |
| 91 | + "1d": 60, |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + exch = get_patched_exchange(mocker, default_conf_usdt, api_mock, exchange="bitget") |
| 97 | + timeframes = ("1m", "5m", "1h") |
| 98 | + |
| 99 | + for timeframe in timeframes: |
| 100 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.SPOT) == 1000 |
| 101 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.FUTURES) == 1000 |
| 102 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.MARK) == 1000 |
| 103 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.FUNDING_RATE) == 200 |
| 104 | + |
| 105 | + start_time = dt_ts(dt_now() - timedelta(days=17)) |
| 106 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.SPOT, start_time) == 1000 |
| 107 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.FUTURES, start_time) == 1000 |
| 108 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.MARK, start_time) == 1000 |
| 109 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.FUNDING_RATE, start_time) == 200 |
| 110 | + start_time = dt_ts(dt_now() - timedelta(days=48)) |
| 111 | + length = 200 if timeframe in ("1m", "5m") else 1000 |
| 112 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.SPOT, start_time) == length |
| 113 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.FUTURES, start_time) == length |
| 114 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.MARK, start_time) == length |
| 115 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.FUNDING_RATE, start_time) == 200 |
| 116 | + |
| 117 | + start_time = dt_ts(dt_now() - timedelta(days=61)) |
| 118 | + length = 200 |
| 119 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.SPOT, start_time) == length |
| 120 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.FUTURES, start_time) == length |
| 121 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.MARK, start_time) == length |
| 122 | + assert exch.ohlcv_candle_limit(timeframe, CandleType.FUNDING_RATE, start_time) == 200 |
0 commit comments