diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 63a0b7468..904387fb2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,15 +16,16 @@ on: concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true - +permissions: + repository-projects: read jobs: build_linux: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ ubuntu-18.04, ubuntu-20.04, ubuntu-22.04 ] - python-version: ["3.8", "3.9", "3.10"] + os: [ ubuntu-20.04, ubuntu-22.04 ] + python-version: ["3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v3 @@ -90,14 +91,14 @@ jobs: freqtrade create-userdir --userdir user_data freqtrade hyperopt --datadir tests/testdata -e 6 --strategy SampleStrategy --hyperopt-loss SharpeHyperOptLossDaily --print-all - - name: Flake8 - run: | - flake8 - - name: Sort imports (isort) run: | isort --check . + - name: Run Ruff + run: | + ruff check --format=github . + - name: Mypy run: | mypy freqtrade scripts tests @@ -115,7 +116,7 @@ jobs: strategy: matrix: os: [ macos-latest ] - python-version: ["3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v3 @@ -186,14 +187,14 @@ jobs: freqtrade create-userdir --userdir user_data freqtrade hyperopt --datadir tests/testdata -e 5 --strategy SampleStrategy --hyperopt-loss SharpeHyperOptLossDaily --print-all - - name: Flake8 - run: | - flake8 - - name: Sort imports (isort) run: | isort --check . + - name: Run Ruff + run: | + ruff check --format=github . + - name: Mypy run: | mypy freqtrade scripts @@ -212,7 +213,7 @@ jobs: strategy: matrix: os: [ windows-latest ] - python-version: ["3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v3 @@ -248,9 +249,9 @@ jobs: freqtrade create-userdir --userdir user_data freqtrade hyperopt --datadir tests/testdata -e 5 --strategy SampleStrategy --hyperopt-loss SharpeHyperOptLossDaily --print-all - - name: Flake8 + - name: Run Ruff run: | - flake8 + ruff check --format=github . - name: Mypy run: | @@ -321,7 +322,6 @@ jobs: build_linux_online: # Run pytest with "live" checks runs-on: ubuntu-22.04 - # permissions: steps: - uses: actions/checkout@v3 @@ -425,7 +425,7 @@ jobs: python setup.py sdist bdist_wheel - name: Publish to PyPI (Test) - uses: pypa/gh-action-pypi-publish@v1.6.4 + uses: pypa/gh-action-pypi-publish@v1.8.1 if: (github.event_name == 'release') with: user: __token__ @@ -433,7 +433,7 @@ jobs: repository_url: https://test.pypi.org/legacy/ - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@v1.6.4 + uses: pypa/gh-action-pypi-publish@v1.8.1 if: (github.event_name == 'release') with: user: __token__ @@ -466,12 +466,13 @@ jobs: - name: Build and test and push docker images env: - IMAGE_NAME: freqtradeorg/freqtrade BRANCH_NAME: ${{ steps.extract_branch.outputs.branch }} run: | build_helpers/publish_docker_multi.sh deploy_arm: + permissions: + packages: write needs: [ deploy ] # Only run on 64bit machines runs-on: [self-hosted, linux, ARM64] @@ -494,8 +495,9 @@ jobs: - name: Build and test and push docker images env: - IMAGE_NAME: freqtradeorg/freqtrade BRANCH_NAME: ${{ steps.extract_branch.outputs.branch }} + GHCR_USERNAME: ${{ github.actor }} + GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | build_helpers/publish_docker_arm64.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 306e4bbda..ca3da8e90 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,33 +2,40 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pycqa/flake8 - rev: "4.0.1" + rev: "6.0.0" hooks: - id: flake8 # stages: [push] - repo: https://github.com/pre-commit/mirrors-mypy - rev: "v0.942" + rev: "v1.0.1" hooks: - id: mypy exclude: build_helpers additional_dependencies: - - types-cachetools==5.2.1 + - types-cachetools==5.3.0.4 - types-filelock==3.2.7 - - types-requests==2.28.11.7 - - types-tabulate==0.9.0.0 - - types-python-dateutil==2.8.19.5 + - types-requests==2.28.11.15 + - types-tabulate==0.9.0.1 + - types-python-dateutil==2.8.19.10 + - SQLAlchemy==2.0.7 # stages: [push] - repo: https://github.com/pycqa/isort - rev: "5.10.1" + rev: "5.12.0" hooks: - id: isort name: isort (python) # stages: [push] + - repo: https://github.com/charliermarsh/ruff-pre-commit + # Ruff version. + rev: 'v0.0.255' + hooks: + - id: ruff + - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.4.0 + rev: v4.4.0 hooks: - id: end-of-file-fixer exclude: | diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b4e0bc024..040aae39c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -45,16 +45,17 @@ pytest tests/test_.py::test_ ### 2. Test if your code is PEP8 compliant -#### Run Flake8 +#### Run Ruff ```bash -flake8 freqtrade tests scripts +ruff . ``` -We receive a lot of code that fails the `flake8` checks. +We receive a lot of code that fails the `ruff` checks. To help with that, we encourage you to install the git pre-commit -hook that will warn you when you try to commit code that fails these checks. -Guide for installing them is [here](http://flake8.pycqa.org/en/latest/user/using-hooks.html). +hook that will warn you when you try to commit code that fails these checks. + +you can manually run pre-commit with `pre-commit run -a`. ##### Additional styles applied diff --git a/Dockerfile b/Dockerfile index b3e5d5e88..6a4a168c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.10.7-slim-bullseye as base +FROM python:3.10.10-slim-bullseye as base # Setup env ENV LANG C.UTF-8 diff --git a/README.md b/README.md index 2ab62793d..c8bc50dac 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Please read the [exchange specific notes](docs/exchanges.md) to learn about even - [X] [Binance](https://www.binance.com/) - [X] [Gate.io](https://www.gate.io/ref/6266643) - [X] [OKX](https://okx.com/) +- [X] [Bybit](https://bybit.com/) Please make sure to read the [exchange specific notes](docs/exchanges.md), as well as the [trading with leverage](docs/leverage.md) documentation before diving in. @@ -164,6 +165,10 @@ first. If it hasn't been reported, please ensure you follow the template guide so that the team can assist you as quickly as possible. +For every [issue](https://github.com/freqtrade/freqtrade/issues/new/choose) created, kindly follow up and mark satisfaction or reminder to close issue when equilibrium ground is reached. + +--Maintain github's [community policy](https://docs.github.com/en/site-policy/github-terms/github-community-code-of-conduct)-- + ### [Feature Requests](https://github.com/freqtrade/freqtrade/labels/enhancement) Have you a great idea to improve the bot you want to share? Please, diff --git a/build_helpers/TA_Lib-0.4.25-cp311-cp311-win_amd64.whl b/build_helpers/TA_Lib-0.4.25-cp311-cp311-win_amd64.whl new file mode 100644 index 000000000..4d55d18c8 Binary files /dev/null and b/build_helpers/TA_Lib-0.4.25-cp311-cp311-win_amd64.whl differ diff --git a/build_helpers/install_windows.ps1 b/build_helpers/install_windows.ps1 index 461726a03..36606cc3a 100644 --- a/build_helpers/install_windows.ps1 +++ b/build_helpers/install_windows.ps1 @@ -14,5 +14,8 @@ if ($pyv -eq '3.9') { if ($pyv -eq '3.10') { pip install build_helpers\TA_Lib-0.4.25-cp310-cp310-win_amd64.whl } +if ($pyv -eq '3.11') { + pip install build_helpers\TA_Lib-0.4.25-cp311-cp311-win_amd64.whl +} pip install -r requirements-dev.txt pip install -e . diff --git a/build_helpers/pre_commit_update.py b/build_helpers/pre_commit_update.py index 8724d8ade..e6b47d100 100644 --- a/build_helpers/pre_commit_update.py +++ b/build_helpers/pre_commit_update.py @@ -8,12 +8,17 @@ import yaml pre_commit_file = Path('.pre-commit-config.yaml') require_dev = Path('requirements-dev.txt') +require = Path('requirements.txt') with require_dev.open('r') as rfile: requirements = rfile.readlines() +with require.open('r') as rfile: + requirements.extend(rfile.readlines()) + # Extract types only -type_reqs = [r.strip('\n') for r in requirements if r.startswith('types-')] +type_reqs = [r.strip('\n') for r in requirements if r.startswith( + 'types-') or r.startswith('SQLAlchemy')] with pre_commit_file.open('r') as file: f = yaml.load(file, Loader=yaml.FullLoader) diff --git a/build_helpers/publish_docker_arm64.sh b/build_helpers/publish_docker_arm64.sh index 071eb0fa2..a6ecdbee6 100755 --- a/build_helpers/publish_docker_arm64.sh +++ b/build_helpers/publish_docker_arm64.sh @@ -3,6 +3,10 @@ # Use BuildKit, otherwise building on ARM fails export DOCKER_BUILDKIT=1 +IMAGE_NAME=freqtradeorg/freqtrade +CACHE_IMAGE=freqtradeorg/freqtrade_cache +GHCR_IMAGE_NAME=ghcr.io/freqtrade/freqtrade + # Replace / with _ to create a valid tag TAG=$(echo "${BRANCH_NAME}" | sed -e "s/\//_/g") TAG_PLOT=${TAG}_plot @@ -14,7 +18,6 @@ TAG_ARM=${TAG}_arm TAG_PLOT_ARM=${TAG_PLOT}_arm TAG_FREQAI_ARM=${TAG_FREQAI}_arm TAG_FREQAI_RL_ARM=${TAG_FREQAI_RL}_arm -CACHE_IMAGE=freqtradeorg/freqtrade_cache echo "Running for ${TAG}" @@ -38,13 +41,13 @@ if [ $? -ne 0 ]; then echo "failed building multiarch images" return 1 fi -# Tag image for upload and next build step -docker tag freqtrade:$TAG_ARM ${CACHE_IMAGE}:$TAG_ARM docker build --cache-from freqtrade:${TAG_ARM} --build-arg sourceimage=${CACHE_IMAGE} --build-arg sourcetag=${TAG_ARM} -t freqtrade:${TAG_PLOT_ARM} -f docker/Dockerfile.plot . docker build --cache-from freqtrade:${TAG_ARM} --build-arg sourceimage=${CACHE_IMAGE} --build-arg sourcetag=${TAG_ARM} -t freqtrade:${TAG_FREQAI_ARM} -f docker/Dockerfile.freqai . docker build --cache-from freqtrade:${TAG_ARM} --build-arg sourceimage=${CACHE_IMAGE} --build-arg sourcetag=${TAG_ARM} -t freqtrade:${TAG_FREQAI_RL_ARM} -f docker/Dockerfile.freqai_rl . +# Tag image for upload and next build step +docker tag freqtrade:$TAG_ARM ${CACHE_IMAGE}:$TAG_ARM docker tag freqtrade:$TAG_PLOT_ARM ${CACHE_IMAGE}:$TAG_PLOT_ARM docker tag freqtrade:$TAG_FREQAI_ARM ${CACHE_IMAGE}:$TAG_FREQAI_ARM docker tag freqtrade:$TAG_FREQAI_RL_ARM ${CACHE_IMAGE}:$TAG_FREQAI_RL_ARM @@ -59,7 +62,6 @@ fi docker images -# docker push ${IMAGE_NAME} docker push ${CACHE_IMAGE}:$TAG_PLOT_ARM docker push ${CACHE_IMAGE}:$TAG_FREQAI_ARM docker push ${CACHE_IMAGE}:$TAG_FREQAI_RL_ARM @@ -70,25 +72,42 @@ docker push ${CACHE_IMAGE}:$TAG_ARM # Otherwise installation might fail. echo "create manifests" -docker manifest create --amend ${IMAGE_NAME}:${TAG} ${CACHE_IMAGE}:${TAG_ARM} ${IMAGE_NAME}:${TAG_PI} ${CACHE_IMAGE}:${TAG} +docker manifest create ${IMAGE_NAME}:${TAG} ${CACHE_IMAGE}:${TAG} ${CACHE_IMAGE}:${TAG_ARM} ${IMAGE_NAME}:${TAG_PI} docker manifest push -p ${IMAGE_NAME}:${TAG} -docker manifest create ${IMAGE_NAME}:${TAG_PLOT} ${CACHE_IMAGE}:${TAG_PLOT_ARM} ${CACHE_IMAGE}:${TAG_PLOT} +docker manifest create ${IMAGE_NAME}:${TAG_PLOT} ${CACHE_IMAGE}:${TAG_PLOT} ${CACHE_IMAGE}:${TAG_PLOT_ARM} docker manifest push -p ${IMAGE_NAME}:${TAG_PLOT} -docker manifest create ${IMAGE_NAME}:${TAG_FREQAI} ${CACHE_IMAGE}:${TAG_FREQAI_ARM} ${CACHE_IMAGE}:${TAG_FREQAI} +docker manifest create ${IMAGE_NAME}:${TAG_FREQAI} ${CACHE_IMAGE}:${TAG_FREQAI} ${CACHE_IMAGE}:${TAG_FREQAI_ARM} docker manifest push -p ${IMAGE_NAME}:${TAG_FREQAI} -docker manifest create ${IMAGE_NAME}:${TAG_FREQAI_RL} ${CACHE_IMAGE}:${TAG_FREQAI_RL_ARM} ${CACHE_IMAGE}:${TAG_FREQAI_RL} +docker manifest create ${IMAGE_NAME}:${TAG_FREQAI_RL} ${CACHE_IMAGE}:${TAG_FREQAI_RL} ${CACHE_IMAGE}:${TAG_FREQAI_RL_ARM} docker manifest push -p ${IMAGE_NAME}:${TAG_FREQAI_RL} +# copy images to ghcr.io + +alias crane="docker run --rm -i -v $(pwd)/.crane:/home/nonroot/.docker/ gcr.io/go-containerregistry/crane" +mkdir .crane +chmod a+rwx .crane + +echo "${GHCR_TOKEN}" | crane auth login ghcr.io -u "${GHCR_USERNAME}" --password-stdin + +crane copy ${IMAGE_NAME}:${TAG_FREQAI_RL} ${GHCR_IMAGE_NAME}:${TAG_FREQAI_RL} +crane copy ${IMAGE_NAME}:${TAG_FREQAI} ${GHCR_IMAGE_NAME}:${TAG_FREQAI} +crane copy ${IMAGE_NAME}:${TAG_PLOT} ${GHCR_IMAGE_NAME}:${TAG_PLOT} +crane copy ${IMAGE_NAME}:${TAG} ${GHCR_IMAGE_NAME}:${TAG} + # Tag as latest for develop builds if [ "${TAG}" = "develop" ]; then + echo 'Tagging image as latest' docker manifest create ${IMAGE_NAME}:latest ${CACHE_IMAGE}:${TAG_ARM} ${IMAGE_NAME}:${TAG_PI} ${CACHE_IMAGE}:${TAG} docker manifest push -p ${IMAGE_NAME}:latest + + crane copy ${IMAGE_NAME}:latest ${GHCR_IMAGE_NAME}:latest fi docker images +rm -rf .crane # Cleanup old images from arm64 node. docker image prune -a --force --filter "until=24h" diff --git a/build_helpers/publish_docker_multi.sh b/build_helpers/publish_docker_multi.sh index a608c1282..27fa06b95 100755 --- a/build_helpers/publish_docker_multi.sh +++ b/build_helpers/publish_docker_multi.sh @@ -2,6 +2,8 @@ # The below assumes a correctly setup docker buildx environment +IMAGE_NAME=freqtradeorg/freqtrade +CACHE_IMAGE=freqtradeorg/freqtrade_cache # Replace / with _ to create a valid tag TAG=$(echo "${BRANCH_NAME}" | sed -e "s/\//_/g") TAG_PLOT=${TAG}_plot @@ -11,7 +13,6 @@ TAG_PI="${TAG}_pi" PI_PLATFORM="linux/arm/v7" echo "Running for ${TAG}" -CACHE_IMAGE=freqtradeorg/freqtrade_cache CACHE_TAG=${CACHE_IMAGE}:${TAG_PI}_cache # Add commit and commit_message to docker container @@ -26,7 +27,10 @@ if [ "${GITHUB_EVENT_NAME}" = "schedule" ]; then --cache-to=type=registry,ref=${CACHE_TAG} \ -f docker/Dockerfile.armhf \ --platform ${PI_PLATFORM} \ - -t ${IMAGE_NAME}:${TAG_PI} --push . + -t ${IMAGE_NAME}:${TAG_PI} \ + --push \ + --provenance=false \ + . else echo "event ${GITHUB_EVENT_NAME}: building with cache" # Build regular image @@ -35,12 +39,16 @@ else # Pull last build to avoid rebuilding the whole image # docker pull --platform ${PI_PLATFORM} ${IMAGE_NAME}:${TAG} + # disable provenance due to https://github.com/docker/buildx/issues/1509 docker buildx build \ --cache-from=type=registry,ref=${CACHE_TAG} \ --cache-to=type=registry,ref=${CACHE_TAG} \ -f docker/Dockerfile.armhf \ --platform ${PI_PLATFORM} \ - -t ${IMAGE_NAME}:${TAG_PI} --push . + -t ${IMAGE_NAME}:${TAG_PI} \ + --push \ + --provenance=false \ + . fi if [ $? -ne 0 ]; then @@ -68,12 +76,10 @@ fi docker images -docker push ${CACHE_IMAGE} +docker push ${CACHE_IMAGE}:$TAG docker push ${CACHE_IMAGE}:$TAG_PLOT docker push ${CACHE_IMAGE}:$TAG_FREQAI docker push ${CACHE_IMAGE}:$TAG_FREQAI_RL -docker push ${CACHE_IMAGE}:$TAG - docker images diff --git a/build_helpers/pyarrow-10.0.0-cp39-cp39-linux_armv7l.whl b/build_helpers/pyarrow-11.0.0-cp39-cp39-linux_armv7l.whl similarity index 62% rename from build_helpers/pyarrow-10.0.0-cp39-cp39-linux_armv7l.whl rename to build_helpers/pyarrow-11.0.0-cp39-cp39-linux_armv7l.whl index a6c879cf5..a7ad80bdf 100644 Binary files a/build_helpers/pyarrow-10.0.0-cp39-cp39-linux_armv7l.whl and b/build_helpers/pyarrow-11.0.0-cp39-cp39-linux_armv7l.whl differ diff --git a/config_examples/config_binance.example.json b/config_examples/config_binance.example.json index 3e99bd114..7968bdedc 100644 --- a/config_examples/config_binance.example.json +++ b/config_examples/config_binance.example.json @@ -59,20 +59,6 @@ "pairlists": [ {"method": "StaticPairList"} ], - "edge": { - "enabled": false, - "process_throttle_secs": 3600, - "calculate_since_number_of_days": 7, - "allowed_risk": 0.01, - "stoploss_range_min": -0.01, - "stoploss_range_max": -0.1, - "stoploss_range_step": -0.01, - "minimum_winrate": 0.60, - "minimum_expectancy": 0.20, - "min_trade_number": 10, - "max_trade_duration_minute": 1440, - "remove_pumps": false - }, "telegram": { "enabled": false, "token": "your_telegram_token", diff --git a/config_examples/config_bittrex.example.json b/config_examples/config_bittrex.example.json index a0a5071dd..3be5ba092 100644 --- a/config_examples/config_bittrex.example.json +++ b/config_examples/config_bittrex.example.json @@ -56,20 +56,6 @@ "pairlists": [ {"method": "StaticPairList"} ], - "edge": { - "enabled": false, - "process_throttle_secs": 3600, - "calculate_since_number_of_days": 7, - "allowed_risk": 0.01, - "stoploss_range_min": -0.01, - "stoploss_range_max": -0.1, - "stoploss_range_step": -0.01, - "minimum_winrate": 0.60, - "minimum_expectancy": 0.20, - "min_trade_number": 10, - "max_trade_duration_minute": 1440, - "remove_pumps": false - }, "telegram": { "enabled": false, "token": "your_telegram_token", diff --git a/config_examples/config_freqai.example.json b/config_examples/config_freqai.example.json index dfd54b3d9..65a93379e 100644 --- a/config_examples/config_freqai.example.json +++ b/config_examples/config_freqai.example.json @@ -21,8 +21,8 @@ "ccxt_config": {}, "ccxt_async_config": {}, "pair_whitelist": [ - "1INCH/USDT", - "ALGO/USDT" + "1INCH/USDT:USDT", + "ALGO/USDT:USDT" ], "pair_blacklist": [] }, @@ -48,7 +48,7 @@ ], "freqai": { "enabled": true, - "purge_old_models": true, + "purge_old_models": 2, "train_period_days": 15, "backtest_period_days": 7, "live_retrain_hours": 0, @@ -60,8 +60,8 @@ "1h" ], "include_corr_pairlist": [ - "BTC/USDT", - "ETH/USDT" + "BTC/USDT:USDT", + "ETH/USDT:USDT" ], "label_period_candles": 20, "include_shifted_candles": 2, diff --git a/config_examples/config_full.example.json b/config_examples/config_full.example.json index b60957b58..64e5b76ea 100644 --- a/config_examples/config_full.example.json +++ b/config_examples/config_full.example.json @@ -60,6 +60,7 @@ "force_entry": "market", "stoploss": "market", "stoploss_on_exchange": false, + "stoploss_price_type": "last", "stoploss_on_exchange_interval": 60, "stoploss_on_exchange_limit_ratio": 0.99 }, diff --git a/config_examples/config_kraken.example.json b/config_examples/config_kraken.example.json index c55dea6ba..420047627 100644 --- a/config_examples/config_kraken.example.json +++ b/config_examples/config_kraken.example.json @@ -64,20 +64,6 @@ "pairlists": [ {"method": "StaticPairList"} ], - "edge": { - "enabled": false, - "process_throttle_secs": 3600, - "calculate_since_number_of_days": 7, - "allowed_risk": 0.01, - "stoploss_range_min": -0.01, - "stoploss_range_max": -0.1, - "stoploss_range_step": -0.01, - "minimum_winrate": 0.60, - "minimum_expectancy": 0.20, - "min_trade_number": 10, - "max_trade_duration_minute": 1440, - "remove_pumps": false - }, "telegram": { "enabled": false, "token": "your_telegram_token", diff --git a/docker/Dockerfile.armhf b/docker/Dockerfile.armhf index 7b663ae6c..4972e7109 100644 --- a/docker/Dockerfile.armhf +++ b/docker/Dockerfile.armhf @@ -1,4 +1,4 @@ -FROM python:3.9.12-slim-bullseye as base +FROM python:3.9.16-slim-bullseye as base # Setup env ENV LANG C.UTF-8 diff --git a/docs/advanced-hyperopt.md b/docs/advanced-hyperopt.md index 0dace9985..ff0521f4f 100644 --- a/docs/advanced-hyperopt.md +++ b/docs/advanced-hyperopt.md @@ -75,7 +75,7 @@ This function needs to return a floating point number (`float`). Smaller numbers ## Overriding pre-defined spaces -To override a pre-defined space (`roi_space`, `generate_roi_table`, `stoploss_space`, `trailing_space`), define a nested class called Hyperopt and define the required spaces as follows: +To override a pre-defined space (`roi_space`, `generate_roi_table`, `stoploss_space`, `trailing_space`, `max_open_trades_space`), define a nested class called Hyperopt and define the required spaces as follows: ```python from freqtrade.optimize.space import Categorical, Dimension, Integer, SKDecimal @@ -123,6 +123,12 @@ class MyAwesomeStrategy(IStrategy): Categorical([True, False], name='trailing_only_offset_is_reached'), ] + + # Define a custom max_open_trades space + def max_open_trades_space(self) -> List[Dimension]: + return [ + Integer(-1, 10, name='max_open_trades'), + ] ``` !!! Note diff --git a/docs/advanced-setup.md b/docs/advanced-setup.md index 93a2025ed..54d489aa1 100644 --- a/docs/advanced-setup.md +++ b/docs/advanced-setup.md @@ -192,7 +192,7 @@ $RepeatedMsgReduction on ### Logging to journald -This needs the `systemd` python package installed as the dependency, which is not available on Windows. Hence, the whole journald logging functionality is not available for a bot running on Windows. +This needs the `cysystemd` python package installed as dependency (`pip install cysystemd`), which is not available on Windows. Hence, the whole journald logging functionality is not available for a bot running on Windows. To send Freqtrade log messages to `journald` system service use the `--logfile` command line option with the value in the following format: diff --git a/docs/bot-basics.md b/docs/bot-basics.md index 3df926371..1aa8f3085 100644 --- a/docs/bot-basics.md +++ b/docs/bot-basics.md @@ -12,6 +12,9 @@ This page provides you some basic concepts on how Freqtrade works and operates. * **Indicators**: Technical indicators (SMA, EMA, RSI, ...). * **Limit order**: Limit orders which execute at the defined limit price or better. * **Market order**: Guaranteed to fill, may move price depending on the order size. +* **Current Profit**: Currently pending (unrealized) profit for this trade. This is mainly used throughout the bot and UI. +* **Realized Profit**: Already realized profit. Only relevant in combination with [partial exits](strategy-callbacks.md#adjust-trade-position) - which also explains the calculation logic for this. +* **Total Profit**: Combined realized and unrealized profit. The relative number (%) is calculated against the total investment in this trade. ## Fee handling @@ -75,3 +78,7 @@ This loop will be repeated again and again until the bot is stopped. !!! Note Both Backtesting and Hyperopt include exchange default Fees in the calculation. Custom fees can be passed to backtesting / hyperopt by specifying the `--fee` argument. + +!!! Warning "Callback call frequency" + Backtesting will call each callback at max. once per candle (`--timeframe-detail` modifies this behavior to once per detailed candle). + Most callbacks will be called once per iteration in live (usually every ~5s) - which can cause backtesting mismatches. diff --git a/docs/configuration.md b/docs/configuration.md index 2113be692..8a1aeb40e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -134,7 +134,7 @@ Mandatory parameters are marked as **Required**, which means that they are requi | Parameter | Description | |------------|-------------| -| `max_open_trades` | **Required.** Number of open trades your bot is allowed to have. Only one open trade per pair is possible, so the length of your pairlist is another limitation that can apply. If -1 then it is ignored (i.e. potentially unlimited open trades, limited by the pairlist). [More information below](#configuring-amount-per-trade).
**Datatype:** Positive integer or -1. +| `max_open_trades` | **Required.** Number of open trades your bot is allowed to have. Only one open trade per pair is possible, so the length of your pairlist is another limitation that can apply. If -1 then it is ignored (i.e. potentially unlimited open trades, limited by the pairlist). [More information below](#configuring-amount-per-trade). [Strategy Override](#parameters-in-the-strategy).
**Datatype:** Positive integer or -1. | `stake_currency` | **Required.** Crypto-currency used for trading.
**Datatype:** String | `stake_amount` | **Required.** Amount of crypto-currency your bot will use for each trade. Set it to `"unlimited"` to allow the bot to use all available balance. [More information below](#configuring-amount-per-trade).
**Datatype:** Positive float or `"unlimited"`. | `tradable_balance_ratio` | Ratio of the total account balance the bot is allowed to trade. [More information below](#configuring-amount-per-trade).
*Defaults to `0.99` 99%).*
**Datatype:** Positive float between `0.1` and `1.0`. @@ -263,6 +263,7 @@ Values set in the configuration file always overwrite values set in the strategy * `minimal_roi` * `timeframe` * `stoploss` +* `max_open_trades` * `trailing_stop` * `trailing_stop_positive` * `trailing_stop_positive_offset` @@ -665,7 +666,7 @@ You should also make sure to read the [Exchanges](exchanges.md) section of the d ### Using proxy with Freqtrade To use a proxy with freqtrade, export your proxy settings using the variables `"HTTP_PROXY"` and `"HTTPS_PROXY"` set to the appropriate values. -This will have the proxy settings applied to everything (telegram, coingecko, ...) except exchange requests. +This will have the proxy settings applied to everything (telegram, coingecko, ...) **except** for exchange requests. ``` bash export HTTP_PROXY="http://addr:port" @@ -681,11 +682,12 @@ To use a proxy for exchange connections - you will have to define the proxies as { "exchange": { "ccxt_config": { - "aiohttp_proxy": "http://addr:port", - "proxies": { - "http": "http://addr:port", - "https": "http://addr:port" - }, + "aiohttp_proxy": "http://addr:port", + "proxies": { + "http": "http://addr:port", + "https": "http://addr:port" + }, + } } } ``` diff --git a/docs/deprecated.md b/docs/deprecated.md index 3b5b28b81..6719ce56d 100644 --- a/docs/deprecated.md +++ b/docs/deprecated.md @@ -74,3 +74,8 @@ Webhook terminology changed from "sell" to "exit", and from "buy" to "entry", re * `webhooksell`, `webhookexit` -> `exit` * `webhooksellfill`, `webhookexitfill` -> `exit_fill` * `webhooksellcancel`, `webhookexitcancel` -> `exit_cancel` + + +## Removal of `populate_any_indicators` + +version 2023.3 saw the removal of `populate_any_indicators` in favor of split methods for feature engineering and targets. Please read the [migration document](strategy_migration.md#freqai-strategy) for full details. diff --git a/docs/developer.md b/docs/developer.md index ea2e36ce1..1bc75551f 100644 --- a/docs/developer.md +++ b/docs/developer.md @@ -24,7 +24,7 @@ This will spin up a local server (usually on port 8000) so you can see if everyt To configure a development environment, you can either use the provided [DevContainer](#devcontainer-setup), or use the `setup.sh` script and answer "y" when asked "Do you want to install dependencies for dev [y/N]? ". Alternatively (e.g. if your system is not supported by the setup.sh script), follow the manual installation process and run `pip3 install -e .[all]`. -This will install all required tools for development, including `pytest`, `flake8`, `mypy`, and `coveralls`. +This will install all required tools for development, including `pytest`, `ruff`, `mypy`, and `coveralls`. Then install the git hook scripts by running `pre-commit install`, so your changes will be verified locally before committing. This avoids a lot of waiting for CI already, as some basic formatting checks are done locally on your machine. @@ -363,7 +363,7 @@ from pathlib import Path exchange = ccxt.binance({ 'apiKey': '', 'secret': '' - 'options': {'defaultType': 'future'} + 'options': {'defaultType': 'swap'} }) _ = exchange.load_markets() diff --git a/docs/exchanges.md b/docs/exchanges.md index 7070fc690..997d012e1 100644 --- a/docs/exchanges.md +++ b/docs/exchanges.md @@ -75,6 +75,25 @@ Binance has been split into 2, and users must use the correct ccxt exchange ID f * [binance.com](https://www.binance.com/) - International users. Use exchange id: `binance`. * [binance.us](https://www.binance.us/) - US based users. Use exchange id: `binanceus`. +### Binance RSA keys + +Freqtrade supports binance RSA API keys. + +We recommend to use them as environment variable. + +``` bash +export FREQTRADE__EXCHANGE__SECRET="$(cat ./rsa_binance.private)" +``` + +They can however also be configured via configuration file. Since json doesn't support multi-line strings, you'll have to replace all newlines with `\n` to have a valid json file. + +``` json +// ... + "key": "", + "secret": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBABACAFQA<...>s8KX8=\n-----END PRIVATE KEY-----" +// ... +``` + ### Binance Futures Binance has specific (unfortunately complex) [Futures Trading Quantitative Rules](https://www.binance.com/en/support/faq/4f462ebe6ff445d4a170be7d9e897272) which need to be followed, and which prohibit a too low stake-amount (among others) for too many orders. @@ -224,8 +243,8 @@ OKX requires a passphrase for each api key, you will therefore need to add this OKX only provides 100 candles per api call. Therefore, the strategy will only have a pretty low amount of data available in backtesting mode. !!! Warning "Futures" - OKX Futures has the concept of "position mode" - which can be Net or long/short (hedge mode). - Freqtrade supports both modes (we recommend to use net mode) - but changing the mode mid-trading is not supported and will lead to exceptions and failures to place trades. + OKX Futures has the concept of "position mode" - which can be "Buy/Sell" or long/short (hedge mode). + Freqtrade supports both modes (we recommend to use Buy/Sell mode) - but changing the mode mid-trading is not supported and will lead to exceptions and failures to place trades. OKX also only provides MARK candles for the past ~3 months. Backtesting futures prior to that date will therefore lead to slight deviations, as funding-fees cannot be calculated correctly without this data. ## Gate.io @@ -236,6 +255,18 @@ OKX requires a passphrase for each api key, you will therefore need to add this Gate.io allows the use of `POINT` to pay for fees. As this is not a tradable currency (no regular market available), automatic fee calculations will fail (and default to a fee of 0). The configuration parameter `exchange.unknown_fee_rate` can be used to specify the exchange rate between Point and the stake currency. Obviously, changing the stake-currency will also require changes to this value. +## Bybit + +Futures trading on bybit is currently supported for USDT markets, and will use isolated futures mode. +Users with unified accounts (there's no way back) can create a Sub-account which will start as "non-unified", and can therefore use isolated futures. +On startup, freqtrade will set the position mode to "One-way Mode" for the whole (sub)account. This avoids making this call over and over again (slowing down bot operations), but means that changes to this setting may result in exceptions and errors. + +As bybit doesn't provide funding rate history, the dry-run calculation is used for live trades as well. + +!!! Tip "Stoploss on Exchange" + Bybit (futures only) supports `stoploss_on_exchange` and uses `stop-loss-limit` orders. It provides great advantages, so we recommend to benefit from it by enabling stoploss on exchange. + On futures, Bybit supports both `stop-limit` as well as `stop-market` orders. You can use either `"limit"` or `"market"` in the `order_types.stoploss` configuration setting to decide which type to use. + ## All exchanges Should you experience constant errors with Nonce (like `InvalidNonce`), it is best to regenerate the API keys. Resetting Nonce is difficult and it's usually easier to regenerate the API keys. diff --git a/docs/faq.md b/docs/faq.md index bcceaf898..b52a77c6b 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -2,7 +2,7 @@ ## Supported Markets -Freqtrade supports spot trading only. +Freqtrade supports spot trading, as well as (isolated) futures trading for some selected exchanges. Please refer to the [documentation start page](index.md#supported-futures-exchanges-experimental) for an uptodate list of supported exchanges. ### Can my bot open short positions? @@ -248,8 +248,26 @@ The Edge module is mostly a result of brainstorming of [@mishaker](https://githu You can find further info on expectancy, win rate, risk management and position size in the following sources: - https://www.tradeciety.com/ultimate-math-guide-for-traders/ -- http://www.vantharp.com/tharp-concepts/expectancy.asp - https://samuraitradingacademy.com/trading-expectancy/ - https://www.learningmarkets.com/determining-expectancy-in-your-trading/ -- http://www.lonestocktrader.com/make-money-trading-positive-expectancy/ +- https://www.lonestocktrader.com/make-money-trading-positive-expectancy/ - https://www.babypips.com/trading/trade-expectancy-matter + +## Official channels + +Freqtrade is using exclusively the following official channels: + +* [Freqtrade discord server](https://discord.gg/p7nuUNVfP7) +* [Freqtrade documentation (https://freqtrade.io)](https://freqtrade.io) +* [Freqtrade github organization](https://github.com/freqtrade) + +Nobody affiliated with the freqtrade project will ask you about your exchange keys or anything else exposing your funds to exploitation. +Should you be asked to expose your exchange keys or send funds to some random wallet, then please don't follow these instructions. + +Failing to follow these guidelines will not be responsibility of freqtrade. + +## "Freqtrade token" + +Freqtrade does not have a Crypto token offering. + +Token offerings you find on the internet referring Freqtrade, FreqAI or freqUI must be considered to be a scam, trying to exploit freqtrade's popularity for their own, nefarious gains. diff --git a/docs/freqai-configuration.md b/docs/freqai-configuration.md index 9d89800be..886dc2338 100644 --- a/docs/freqai-configuration.md +++ b/docs/freqai-configuration.md @@ -9,7 +9,7 @@ FreqAI is configured through the typical [Freqtrade config file](configuration.m ```json "freqai": { "enabled": true, - "purge_old_models": true, + "purge_old_models": 2, "train_period_days": 30, "backtest_period_days": 7, "identifier" : "unique-id", @@ -165,10 +165,10 @@ Below are the values you can expect to include/use inside a typical strategy dat ## Setting the `startup_candle_count` -The `startup_candle_count` in the FreqAI strategy needs to be set up in the same way as in the standard Freqtrade strategy (see details [here](strategy-customization.md#strategy-startup-period)). This value is used by Freqtrade to ensure that a sufficient amount of data is provided when calling the `dataprovider`, to avoid any NaNs at the beginning of the first training. You can easily set this value by identifying the longest period (in candle units) which is passed to the indicator creation functions (e.g., Ta-Lib functions). In the presented example, `startup_candle_count` is 20 since this is the maximum value in `indicators_periods_candles`. +The `startup_candle_count` in the FreqAI strategy needs to be set up in the same way as in the standard Freqtrade strategy (see details [here](strategy-customization.md#strategy-startup-period)). This value is used by Freqtrade to ensure that a sufficient amount of data is provided when calling the `dataprovider`, to avoid any NaNs at the beginning of the first training. You can easily set this value by identifying the longest period (in candle units) which is passed to the indicator creation functions (e.g., TA-Lib functions). In the presented example, `startup_candle_count` is 20 since this is the maximum value in `indicators_periods_candles`. !!! Note - There are instances where the Ta-Lib functions actually require more data than just the passed `period` or else the feature dataset gets populated with NaNs. Anecdotally, multiplying the `startup_candle_count` by 2 always leads to a fully NaN free training dataset. Hence, it is typically safest to multiply the expected `startup_candle_count` by 2. Look out for this log message to confirm that the data is clean: + There are instances where the TA-Lib functions actually require more data than just the passed `period` or else the feature dataset gets populated with NaNs. Anecdotally, multiplying the `startup_candle_count` by 2 always leads to a fully NaN free training dataset. Hence, it is typically safest to multiply the expected `startup_candle_count` by 2. Look out for this log message to confirm that the data is clean: ``` 2022-08-31 15:14:04 - freqtrade.freqai.data_kitchen - INFO - dropped 0 training points due to NaNs in populated dataset 4319. @@ -205,7 +205,7 @@ All of the aforementioned model libraries implement gradient boosted decision tr * LightGBM: https://lightgbm.readthedocs.io/en/v3.3.2/# * XGBoost: https://xgboost.readthedocs.io/en/stable/# -There are also numerous online articles describing and comparing the algorithms. Some relatively light-weight examples would be [CatBoost vs. LightGBM vs. XGBoost — Which is the best algorithm?](https://towardsdatascience.com/catboost-vs-lightgbm-vs-xgboost-c80f40662924#:~:text=In%20CatBoost%2C%20symmetric%20trees%2C%20or,the%20same%20depth%20can%20differ.) and [XGBoost, LightGBM or CatBoost — which boosting algorithm should I use?](https://medium.com/riskified-technology/xgboost-lightgbm-or-catboost-which-boosting-algorithm-should-i-use-e7fda7bb36bc). Keep in mind that the performance of each model is highly dependent on the application and so any reported metrics might not be true for your particular use of the model. +There are also numerous online articles describing and comparing the algorithms. Some relatively lightweight examples would be [CatBoost vs. LightGBM vs. XGBoost — Which is the best algorithm?](https://towardsdatascience.com/catboost-vs-lightgbm-vs-xgboost-c80f40662924#:~:text=In%20CatBoost%2C%20symmetric%20trees%2C%20or,the%20same%20depth%20can%20differ.) and [XGBoost, LightGBM or CatBoost — which boosting algorithm should I use?](https://medium.com/riskified-technology/xgboost-lightgbm-or-catboost-which-boosting-algorithm-should-i-use-e7fda7bb36bc). Keep in mind that the performance of each model is highly dependent on the application and so any reported metrics might not be true for your particular use of the model. Apart from the models already available in FreqAI, it is also possible to customize and create your own prediction models using the `IFreqaiModel` class. You are encouraged to inherit `fit()`, `train()`, and `predict()` to customize various aspects of the training procedures. You can place custom FreqAI models in `user_data/freqaimodels` - and freqtrade will pick them up from there based on the provided `--freqaimodel` name - which has to correspond to the class name of your custom model. Make sure to use unique names to avoid overriding built-in models. diff --git a/docs/freqai-feature-engineering.md b/docs/freqai-feature-engineering.md index 6c8c5bb46..6389bd9e5 100644 --- a/docs/freqai-feature-engineering.md +++ b/docs/freqai-feature-engineering.md @@ -8,7 +8,7 @@ Low level feature engineering is performed in the user strategy within a set of |---------------|-------------| | `feature_engineering__expand_all()` | This optional function will automatically expand the defined features on the config defined `indicator_periods_candles`, `include_timeframes`, `include_shifted_candles`, and `include_corr_pairs`. | `feature_engineering__expand_basic()` | This optional function will automatically expand the defined features on the config defined `include_timeframes`, `include_shifted_candles`, and `include_corr_pairs`. Note: this function does *not* expand across `include_periods_candles`. -| `feature_engineering_standard()` | This optional function will be called once with the dataframe of the base timeframe. This is the final function to be called, which means that the dataframe entering this function will contain all the features and columns from the base asset created by the other `feature_engineering_expand` functions. This function is a good place to do custom exotic feature extractions (e.g. tsfresh). This function is also a good place for any feature that should not be auto-expanded upon (e.g. day of the week). +| `feature_engineering_standard()` | This optional function will be called once with the dataframe of the base timeframe. This is the final function to be called, which means that the dataframe entering this function will contain all the features and columns from the base asset created by the other `feature_engineering_expand` functions. This function is a good place to do custom exotic feature extractions (e.g. tsfresh). This function is also a good place for any feature that should not be auto-expanded upon (e.g., day of the week). | `set_freqai_targets()` | Required function to set the targets for the model. All targets must be prepended with `&` to be recognized by the FreqAI internals. Meanwhile, high level feature engineering is handled within `"feature_parameters":{}` in the FreqAI config. Within this file, it is possible to decide large scale feature expansions on top of the `base_features` such as "including correlated pairs" or "including informative timeframes" or even "including recent candles." @@ -16,7 +16,7 @@ Meanwhile, high level feature engineering is handled within `"feature_parameters It is advisable to start from the template `feature_engineering_*` functions in the source provided example strategy (found in `templates/FreqaiExampleStrategy.py`) to ensure that the feature definitions are following the correct conventions. Here is an example of how to set the indicators and labels in the strategy: ```python - def feature_engineering_expand_all(self, dataframe, period, **kwargs): + def feature_engineering_expand_all(self, dataframe, period, metadata, **kwargs): """ *Only functional with FreqAI enabled strategies* This function will automatically expand the defined features on the config defined @@ -28,8 +28,13 @@ It is advisable to start from the template `feature_engineering_*` functions in All features must be prepended with `%` to be recognized by FreqAI internals. + Access metadata such as the current pair/timeframe/period with: + + `metadata["pair"]` `metadata["tf"]` `metadata["period"]` + :param df: strategy dataframe which will receive the features :param period: period of the indicator - usage example: + :param metadata: metadata of current pair dataframe["%-ema-period"] = ta.EMA(dataframe, timeperiod=period) """ @@ -62,7 +67,7 @@ It is advisable to start from the template `feature_engineering_*` functions in return dataframe - def feature_engineering_expand_basic(self, dataframe, **kwargs): + def feature_engineering_expand_basic(self, dataframe, metadata, **kwargs): """ *Only functional with FreqAI enabled strategies* This function will automatically expand the defined features on the config defined @@ -75,9 +80,14 @@ It is advisable to start from the template `feature_engineering_*` functions in Features defined here will *not* be automatically duplicated on user defined `indicator_periods_candles` + Access metadata such as the current pair/timeframe with: + + `metadata["pair"]` `metadata["tf"]` + All features must be prepended with `%` to be recognized by FreqAI internals. :param df: strategy dataframe which will receive the features + :param metadata: metadata of current pair dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-ema-200"] = ta.EMA(dataframe, timeperiod=200) """ @@ -86,7 +96,7 @@ It is advisable to start from the template `feature_engineering_*` functions in dataframe["%-raw_price"] = dataframe["close"] return dataframe - def feature_engineering_standard(self, dataframe, **kwargs): + def feature_engineering_standard(self, dataframe, metadata, **kwargs): """ *Only functional with FreqAI enabled strategies* This optional function will be called once with the dataframe of the base timeframe. @@ -98,22 +108,32 @@ It is advisable to start from the template `feature_engineering_*` functions in This function is a good place for any feature that should not be auto-expanded upon (e.g. day of the week). + Access metadata such as the current pair with: + + `metadata["pair"]` + All features must be prepended with `%` to be recognized by FreqAI internals. :param df: strategy dataframe which will receive the features + :param metadata: metadata of current pair usage example: dataframe["%-day_of_week"] = (dataframe["date"].dt.dayofweek + 1) / 7 """ dataframe["%-day_of_week"] = (dataframe["date"].dt.dayofweek + 1) / 7 dataframe["%-hour_of_day"] = (dataframe["date"].dt.hour + 1) / 25 return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe, metadata, **kwargs): """ *Only functional with FreqAI enabled strategies* Required function to set the targets for the model. All targets must be prepended with `&` to be recognized by the FreqAI internals. + Access metadata such as the current pair with: + + `metadata["pair"]` + :param df: strategy dataframe which will receive the targets + :param metadata: metadata of current pair usage example: dataframe["&-target"] = dataframe["close"].shift(-1) / dataframe["close"] """ dataframe["&-s_close"] = ( @@ -161,6 +181,19 @@ You can ask for each of the defined features to be included also for informative In total, the number of features the user of the presented example strat has created is: length of `include_timeframes` * no. features in `feature_engineering_expand_*()` * length of `include_corr_pairlist` * no. `include_shifted_candles` * length of `indicator_periods_candles` $= 3 * 3 * 3 * 2 * 2 = 108$. + + ### Gain finer control over `feature_engineering_*` functions with `metadata` + + All `feature_engineering_*` and `set_freqai_targets()` functions are passed a `metadata` dictionary which contains information about the `pair`, `tf` (timeframe), and `period` that FreqAI is automating for feature building. As such, a user can use `metadata` inside `feature_engineering_*` functions as criteria for blocking/reserving features for certain timeframes, periods, pairs etc. + + ```py +def feature_engineering_expand_all(self, dataframe, period, metadata, **kwargs): + if metadata["tf"] == "1h": + dataframe["%-roc-period"] = ta.ROC(dataframe, timeperiod=period) +``` + +This will block `ta.ROC()` from being added to any timeframes other than `"1h"`. + ### Returning additional info from training Important metrics can be returned to the strategy at the end of each model training by assigning them to `dk.data['extra_returns_per_train']['my_new_value'] = XYZ` inside the custom prediction model class. @@ -201,7 +234,7 @@ This will perform PCA on the features and reduce their dimensionality so that th ## Inlier metric -The `inlier_metric` is a metric aimed at quantifying how similar a the features of a data point are to the most recent historic data points. +The `inlier_metric` is a metric aimed at quantifying how similar the features of a data point are to the most recent historical data points. You define the lookback window by setting `inlier_metric_window` and FreqAI computes the distance between the present time point and each of the previous `inlier_metric_window` lookback points. A Weibull function is fit to each of the lookback distributions and its cumulative distribution function (CDF) is used to produce a quantile for each lookback point. The `inlier_metric` is then computed for each time point as the average of the corresponding lookback quantiles. The figure below explains the concept for an `inlier_metric_window` of 5. diff --git a/docs/freqai-parameter-table.md b/docs/freqai-parameter-table.md index 046fa8008..f67ea8541 100644 --- a/docs/freqai-parameter-table.md +++ b/docs/freqai-parameter-table.md @@ -15,10 +15,9 @@ Mandatory parameters are marked as **Required** and have to be set in one of the | `identifier` | **Required.**
A unique ID for the current model. If models are saved to disk, the `identifier` allows for reloading specific pre-trained models/data.
**Datatype:** String. | `live_retrain_hours` | Frequency of retraining during dry/live runs.
**Datatype:** Float > 0.
Default: `0` (models retrain as often as possible). | `expiration_hours` | Avoid making predictions if a model is more than `expiration_hours` old.
**Datatype:** Positive integer.
Default: `0` (models never expire). -| `purge_old_models` | Delete all unused models during live runs (not relevant to backtesting). If set to false (not default), dry/live runs will accumulate all unused models to disk. If
**Datatype:** Boolean.
Default: `True`. +| `purge_old_models` | Number of models to keep on disk (not relevant to backtesting). Default is 2, which means that dry/live runs will keep the latest 2 models on disk. Setting to 0 keeps all models. This parameter also accepts a boolean to maintain backwards compatibility.
**Datatype:** Integer.
Default: `2`. | `save_backtest_models` | Save models to disk when running backtesting. Backtesting operates most efficiently by saving the prediction data and reusing them directly for subsequent runs (when you wish to tune entry/exit parameters). Saving backtesting models to disk also allows to use the same model files for starting a dry/live instance with the same model `identifier`.
**Datatype:** Boolean.
Default: `False` (no models are saved). | `fit_live_predictions_candles` | Number of historical candles to use for computing target (label) statistics from prediction data, instead of from the training dataset (more information can be found [here](freqai-configuration.md#creating-a-dynamic-target-threshold)).
**Datatype:** Positive integer. -| `follow_mode` | Use a `follower` that will look for models associated with a specific `identifier` and load those for inferencing. A `follower` will **not** train new models.
**Datatype:** Boolean.
Default: `False`. | `continual_learning` | Use the final state of the most recently trained model as starting point for the new model, allowing for incremental learning (more information can be found [here](freqai-running.md#continual-learning)).
**Datatype:** Boolean.
Default: `False`. | `write_metrics_to_disk` | Collect train timings, inference timings and cpu usage in json file.
**Datatype:** Boolean.
Default: `False` | `data_kitchen_thread_count` |
Designate the number of threads you want to use for data processing (outlier methods, normalization, etc.). This has no impact on the number of threads used for training. If user does not set it (default), FreqAI will use max number of threads - 2 (leaving 1 physical core available for Freqtrade bot and FreqUI)
**Datatype:** Positive integer. @@ -46,13 +45,15 @@ Mandatory parameters are marked as **Required** and have to be set in one of the | `noise_standard_deviation` | If set, FreqAI adds noise to the training features with the aim of preventing overfitting. FreqAI generates random deviates from a gaussian distribution with a standard deviation of `noise_standard_deviation` and adds them to all data points. `noise_standard_deviation` should be kept relative to the normalized space, i.e., between -1 and 1. In other words, since data in FreqAI is always normalized to be between -1 and 1, `noise_standard_deviation: 0.05` would result in 32% of the data being randomly increased/decreased by more than 2.5% (i.e., the percent of data falling within the first standard deviation).
**Datatype:** Integer.
Default: `0`. | `outlier_protection_percentage` | Enable to prevent outlier detection methods from discarding too much data. If more than `outlier_protection_percentage` % of points are detected as outliers by the SVM or DBSCAN, FreqAI will log a warning message and ignore outlier detection, i.e., the original dataset will be kept intact. If the outlier protection is triggered, no predictions will be made based on the training dataset.
**Datatype:** Float.
Default: `30`. | `reverse_train_test_order` | Split the feature dataset (see below) and use the latest data split for training and test on historical split of the data. This allows the model to be trained up to the most recent data point, while avoiding overfitting. However, you should be careful to understand the unorthodox nature of this parameter before employing it.
**Datatype:** Boolean.
Default: `False` (no reversal). +| `shuffle_after_split` | Split the data into train and test sets, and then shuffle both sets individually.
**Datatype:** Boolean.
Default: `False`. +| `buffer_train_data_candles` | Cut `buffer_train_data_candles` off the beginning and end of the training data *after* the indicators were populated. The main example use is when predicting maxima and minima, the argrelextrema function cannot know the maxima/minima at the edges of the timerange. To improve model accuracy, it is best to compute argrelextrema on the full timerange and then use this function to cut off the edges (buffer) by the kernel. In another case, if the targets are set to a shifted price movement, this buffer is unnecessary because the shifted candles at the end of the timerange will be NaN and FreqAI will automatically cut those off of the training dataset.
**Datatype:** Boolean.
Default: `False`. ### Data split parameters | Parameter | Description | |------------|-------------| | | **Data split parameters within the `freqai.data_split_parameters` sub dictionary** -| `data_split_parameters` | Include any additional parameters available from Scikit-learn `test_train_split()`, which are shown [here](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html) (external website).
**Datatype:** Dictionary. +| `data_split_parameters` | Include any additional parameters available from scikit-learn `test_train_split()`, which are shown [here](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html) (external website).
**Datatype:** Dictionary. | `test_size` | The fraction of data that should be used for testing instead of training.
**Datatype:** Positive float < 1. | `shuffle` | Shuffle the training data points during training. Typically, to not remove the chronological order of data in time-series forecasting, this is set to `False`.
**Datatype:** Boolean.
Defaut: `False`. @@ -83,12 +84,13 @@ Mandatory parameters are marked as **Required** and have to be set in one of the | `add_state_info` | Tell FreqAI to include state information in the feature set for training and inferencing. The current state variables include trade duration, current profit, trade position. This is only available in dry/live runs, and is automatically switched to false for backtesting.
**Datatype:** bool.
Default: `False`. | `net_arch` | Network architecture which is well described in [`stable_baselines3` doc](https://stable-baselines3.readthedocs.io/en/master/guide/custom_policy.html#examples). In summary: `[, dict(vf=[], pi=[])]`. By default this is set to `[128, 128]`, which defines 2 shared hidden layers with 128 units each. | `randomize_starting_position` | Randomize the starting point of each episode to avoid overfitting.
**Datatype:** bool.
Default: `False`. +| `drop_ohlc_from_features` | Do not include the normalized ohlc data in the feature set passed to the agent during training (ohlc will still be used for driving the environment in all cases)
**Datatype:** Boolean.
**Default:** `False` ### Additional parameters | Parameter | Description | |------------|-------------| | | **Extraneous parameters** -| `freqai.keras` | If the selected model makes use of Keras (typical for Tensorflow-based prediction models), this flag needs to be activated so that the model save/loading follows Keras standards.
**Datatype:** Boolean.
Default: `False`. +| `freqai.keras` | If the selected model makes use of Keras (typical for TensorFlow-based prediction models), this flag needs to be activated so that the model save/loading follows Keras standards.
**Datatype:** Boolean.
Default: `False`. | `freqai.conv_width` | The width of a convolutional neural network input tensor. This replaces the need for shifting candles (`include_shifted_candles`) by feeding in historical data points as the second dimension of the tensor. Technically, this parameter can also be used for regressors, but it only adds computational overhead and does not change the model training/prediction.
**Datatype:** Integer.
Default: `2`. | `freqai.reduce_df_footprint` | Recast all numeric columns to float32/int32, with the objective of reducing ram/disk usage and decreasing train/inference timing. This parameter is set in the main level of the Freqtrade configuration file (not inside FreqAI).
**Datatype:** Boolean.
Default: `False`. diff --git a/docs/freqai-reinforcement-learning.md b/docs/freqai-reinforcement-learning.md index 4442a2f4f..ed6a41825 100644 --- a/docs/freqai-reinforcement-learning.md +++ b/docs/freqai-reinforcement-learning.md @@ -24,7 +24,7 @@ The framework is built on stable_baselines3 (torch) and OpenAI gym for the base ### Important considerations -As explained above, the agent is "trained" in an artificial trading "environment". In our case, that environment may seem quite similar to a real Freqtrade backtesting environment, but it is *NOT*. In fact, the RL training environment is much more simplified. It does not incorporate any of the complicated strategy logic, such as callbacks like `custom_exit`, `custom_stoploss`, leverage controls, etc. The RL environment is instead a very "raw" representation of the true market, where the agent has free-will to learn the policy (read: stoploss, take profit, etc.) which is enforced by the `calculate_reward()`. Thus, it is important to consider that the agent training environment is not identical to the real world. +As explained above, the agent is "trained" in an artificial trading "environment". In our case, that environment may seem quite similar to a real Freqtrade backtesting environment, but it is *NOT*. In fact, the RL training environment is much more simplified. It does not incorporate any of the complicated strategy logic, such as callbacks like `custom_exit`, `custom_stoploss`, leverage controls, etc. The RL environment is instead a very "raw" representation of the true market, where the agent has free will to learn the policy (read: stoploss, take profit, etc.) which is enforced by the `calculate_reward()`. Thus, it is important to consider that the agent training environment is not identical to the real world. ## Running Reinforcement Learning @@ -34,7 +34,7 @@ Setting up and running a Reinforcement Learning model is the same as running a R freqtrade trade --freqaimodel ReinforcementLearner --strategy MyRLStrategy --config config.json ``` -where `ReinforcementLearner` will use the templated `ReinforcementLearner` from `freqai/prediction_models/ReinforcementLearner` (or a custom user defined one located in `user_data/freqaimodels`). The strategy, on the other hand, follows the same base [feature engineering](freqai-feature-engineering.md) with `feature_engineering_*` as a typical Regressor. The difference lies in the creation of the targets, Reinforcement Learning doesnt require them. However, FreqAI requires a default (neutral) value to be set in the action column: +where `ReinforcementLearner` will use the templated `ReinforcementLearner` from `freqai/prediction_models/ReinforcementLearner` (or a custom user defined one located in `user_data/freqaimodels`). The strategy, on the other hand, follows the same base [feature engineering](freqai-feature-engineering.md) with `feature_engineering_*` as a typical Regressor. The difference lies in the creation of the targets, Reinforcement Learning doesn't require them. However, FreqAI requires a default (neutral) value to be set in the action column: ```python def set_freqai_targets(self, dataframe, **kwargs): @@ -52,18 +52,18 @@ where `ReinforcementLearner` will use the templated `ReinforcementLearner` from """ # For RL, there are no direct targets to set. This is filler (neutral) # until the agent sends an action. - df["&-action"] = 0 + dataframe["&-action"] = 0 ``` Most of the function remains the same as for typical Regressors, however, the function above shows how the strategy must pass the raw price data to the agent so that it has access to raw OHLCV in the training environment: ```python - def feature_engineering_standard(): + def feature_engineering_standard(self, dataframe, **kwargs): # The following features are necessary for RL models - informative[f"%-raw_close"] = informative["close"] - informative[f"%-raw_open"] = informative["open"] - informative[f"%-raw_high"] = informative["high"] - informative[f"%-raw_low"] = informative["low"] + dataframe[f"%-raw_close"] = dataframe["close"] + dataframe[f"%-raw_open"] = dataframe["open"] + dataframe[f"%-raw_high"] = dataframe["high"] + dataframe[f"%-raw_low"] = dataframe["low"] ``` Finally, there is no explicit "label" to make - instead it is necessary to assign the `&-action` column which will contain the agent's actions when accessed in `populate_entry/exit_trends()`. In the present example, the neutral action to 0. This value should align with the environment used. FreqAI provides two environments, both use 0 as the neutral action. @@ -175,10 +175,23 @@ As you begin to modify the strategy and the prediction model, you will quickly r pnl = self.get_unrealized_profit() factor = 100 + + pair = self.pair.replace(':', '') + + # you can use feature values from dataframe + # Assumes the shifted RSI indicator has been generated in the strategy. + rsi_now = self.raw_features[f"%-rsi-period-10_shift-1_{pair}_" + f"{self.config['timeframe']}"].iloc[self._current_tick] + # reward agent for entering trades - if action in (Actions.Long_enter.value, Actions.Short_enter.value) \ - and self._position == Positions.Neutral: - return 25 + if (action in (Actions.Long_enter.value, Actions.Short_enter.value) + and self._position == Positions.Neutral): + if rsi_now < 40: + factor = 40 / rsi_now + else: + factor = 1 + return 25 * factor + # discourage agent from not entering trades if action == Actions.Neutral.value and self._position == Positions.Neutral: return -1 @@ -235,14 +248,13 @@ FreqAI also provides a built in episodic summary logger called `self.tensorboard """ def calculate_reward(self, action: int) -> float: if not self._is_valid(action): - self.tensorboard_log("is_valid") + self.tensorboard_log("invalid") return -2 ``` !!! Note - The `self.tensorboard_log()` function is designed for tracking incremented objects only i.e. events, actions inside the training environment. If the event of interest is a float, the float can be passed as the second argument e.g. `self.tensorboard_log("float_metric1", 0.23)` would add 0.23 to `float_metric`. In this case you can also disable incrementing using `inc=False` parameter. - + The `self.tensorboard_log()` function is designed for tracking incremented objects only i.e. events, actions inside the training environment. If the event of interest is a float, the float can be passed as the second argument e.g. `self.tensorboard_log("float_metric1", 0.23)`. In this case the metric values are not incremented. ### Choosing a base environment diff --git a/docs/freqai-running.md b/docs/freqai-running.md index a75e30e83..1eaee1bf2 100644 --- a/docs/freqai-running.md +++ b/docs/freqai-running.md @@ -120,7 +120,7 @@ In the presented example config, the user will only allow predictions on models Model training parameters are unique to the selected machine learning library. FreqAI allows you to set any parameter for any library using the `model_training_parameters` dictionary in the config. The example config (found in `config_examples/config_freqai.example.json`) shows some of the example parameters associated with `Catboost` and `LightGBM`, but you can add any parameters available in those libraries or any other machine learning library you choose to implement. -Data split parameters are defined in `data_split_parameters` which can be any parameters associated with Scikit-learn's `train_test_split()` function. `train_test_split()` has a parameters called `shuffle` which allows to shuffle the data or keep it unshuffled. This is particularly useful to avoid biasing training with temporally auto-correlated data. More details about these parameters can be found the [Scikit-learn website](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html) (external website). +Data split parameters are defined in `data_split_parameters` which can be any parameters associated with scikit-learn's `train_test_split()` function. `train_test_split()` has a parameters called `shuffle` which allows to shuffle the data or keep it unshuffled. This is particularly useful to avoid biasing training with temporally auto-correlated data. More details about these parameters can be found the [scikit-learn website](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html) (external website). The FreqAI specific parameter `label_period_candles` defines the offset (number of candles into the future) used for the `labels`. In the presented [example config](freqai-configuration.md#setting-up-the-configuration-file), the user is asking for `labels` that are 24 candles in the future. @@ -165,20 +165,3 @@ tensorboard --logdir user_data/models/unique-id where `unique-id` is the `identifier` set in the `freqai` configuration file. This command must be run in a separate shell if you wish to view the output in your browser at 127.0.0.1:6060 (6060 is the default port used by Tensorboard). ![tensorboard](assets/tensorboard.jpg) - -## Setting up a follower - -You can indicate to the bot that it should not train models, but instead should look for models trained by a leader with a specific `identifier` by defining: - -```json - "freqai": { - "enabled": true, - "follow_mode": true, - "identifier": "example", - "feature_parameters": { - // leader bots feature_parameters inserted here - }, - } -``` - -In this example, the user has a leader bot with the `"identifier": "example"`. The leader bot is already running or is launched simultaneously with the follower. The follower will load models created by the leader and inference them to obtain predictions instead of training its own models. The user will also need to duplicate the `feature_parameters` parameters from from the leaders freqai configuration file into the freqai section of the followers config. diff --git a/docs/freqai.md b/docs/freqai.md index d13d43f66..ef8efb840 100644 --- a/docs/freqai.md +++ b/docs/freqai.md @@ -4,7 +4,10 @@ ## Introduction -FreqAI is a software designed to automate a variety of tasks associated with training a predictive machine learning model to generate market forecasts given a set of input signals. In general, the FreqAI aims to be a sand-box for easily deploying robust machine-learning libraries on real-time data ([details])(#freqai-position-in-open-source-machine-learning-landscape). +FreqAI is a software designed to automate a variety of tasks associated with training a predictive machine learning model to generate market forecasts given a set of input signals. In general, FreqAI aims to be a sandbox for easily deploying robust machine learning libraries on real-time data ([details](#freqai-position-in-open-source-machine-learning-landscape)). + +!!! Note + FreqAI is, and always will be, a not-for-profit, open-source project. FreqAI does *not* have a crypto token, FreqAI does *not* sell signals, and FreqAI does not have a domain besides the present [freqtrade documentation](https://www.freqtrade.io/en/latest/freqai/). Features include: @@ -19,7 +22,7 @@ Features include: * **Automatic data download** - Compute timeranges for data downloads and update historic data (in live deployments) * **Cleaning of incoming data** - Handle NaNs safely before training and model inferencing * **Dimensionality reduction** - Reduce the size of the training data via [Principal Component Analysis](freqai-feature-engineering.md#data-dimensionality-reduction-with-principal-component-analysis) -* **Deploying bot fleets** - Set one bot to train models while a fleet of [follower bots](freqai-running.md#setting-up-a-follower) inference the models and handle trades +* **Deploying bot fleets** - Set one bot to train models while a fleet of [consumers](producer-consumer.md) use signals. ## Quick start @@ -68,13 +71,17 @@ pip install -r requirements-freqai.txt !!! Note Catboost will not be installed on arm devices (raspberry, Mac M1, ARM based VPS, ...), since it does not provide wheels for this platform. +!!! Note "python 3.11" + Some dependencies (Catboost, Torch) currently don't support python 3.11. Freqtrade therefore only supports python 3.10 for these models/dependencies. + Tests involving these dependencies are skipped on 3.11. + ### Usage with docker -If you are using docker, a dedicated tag with FreqAI dependencies is available as `:freqai`. As such - you can replace the image line in your docker-compose file with `image: freqtradeorg/freqtrade:develop_freqai`. This image contains the regular FreqAI dependencies. Similar to native installs, Catboost will not be available on ARM based devices. +If you are using docker, a dedicated tag with FreqAI dependencies is available as `:freqai`. As such - you can replace the image line in your docker compose file with `image: freqtradeorg/freqtrade:develop_freqai`. This image contains the regular FreqAI dependencies. Similar to native installs, Catboost will not be available on ARM based devices. ### FreqAI position in open-source machine learning landscape -Forecasting chaotic time-series based systems, such as equity/cryptocurrency markets, requires a broad set of tools geared toward testing a wide range of hypotheses. Fortunately, a recent maturation of robust machine learning libraries (e.g. `scikit-learn`) has opened up a wide range of research possibilities. Scientists from a diverse range of fields can now easily prototype their studies on an abundance of established machine learning algorithms. Similarly, these user-friendly libraries enable "citzen scientists" to use their basic Python skills for data-exploration. However, leveraging these machine learning libraries on historical and live chaotic data sources can be logistically difficult and expensive. Additionally, robust data-collection, storage, and handling presents a disparate challenge. [`FreqAI`](#freqai) aims to provide a generalized and extensible open-sourced framework geared toward live deployments of adaptive modeling for market forecasting. The `FreqAI` framework is effectively a sandbox for the rich world of open-source machine learning libraries. Inside the `FreqAI` sandbox, users find they can combine a wide variety of third-party libraries to test creative hypotheses on a free live 24/7 chaotic data source - cryptocurrency exchange data. +Forecasting chaotic time-series based systems, such as equity/cryptocurrency markets, requires a broad set of tools geared toward testing a wide range of hypotheses. Fortunately, a recent maturation of robust machine learning libraries (e.g. `scikit-learn`) has opened up a wide range of research possibilities. Scientists from a diverse range of fields can now easily prototype their studies on an abundance of established machine learning algorithms. Similarly, these user-friendly libraries enable "citzen scientists" to use their basic Python skills for data exploration. However, leveraging these machine learning libraries on historical and live chaotic data sources can be logistically difficult and expensive. Additionally, robust data collection, storage, and handling presents a disparate challenge. [`FreqAI`](#freqai) aims to provide a generalized and extensible open-sourced framework geared toward live deployments of adaptive modeling for market forecasting. The `FreqAI` framework is effectively a sandbox for the rich world of open-source machine learning libraries. Inside the `FreqAI` sandbox, users find they can combine a wide variety of third-party libraries to test creative hypotheses on a free live 24/7 chaotic data source - cryptocurrency exchange data. ### Citing FreqAI diff --git a/docs/hyperopt.md b/docs/hyperopt.md index e72b850ca..19bffd742 100644 --- a/docs/hyperopt.md +++ b/docs/hyperopt.md @@ -50,7 +50,7 @@ usage: freqtrade hyperopt [-h] [-v] [--logfile FILE] [-V] [-c PATH] [-d PATH] [--eps] [--dmmp] [--enable-protections] [--dry-run-wallet DRY_RUN_WALLET] [--timeframe-detail TIMEFRAME_DETAIL] [-e INT] - [--spaces {all,buy,sell,roi,stoploss,trailing,protection,default} [{all,buy,sell,roi,stoploss,trailing,protection,default} ...]] + [--spaces {all,buy,sell,roi,stoploss,trailing,protection,trades,default} [{all,buy,sell,roi,stoploss,trailing,protection,trades,default} ...]] [--print-all] [--no-color] [--print-json] [-j JOBS] [--random-state INT] [--min-trades INT] [--hyperopt-loss NAME] [--disable-param-export] @@ -96,7 +96,7 @@ optional arguments: Specify detail timeframe for backtesting (`1m`, `5m`, `30m`, `1h`, `1d`). -e INT, --epochs INT Specify number of epochs (default: 100). - --spaces {all,buy,sell,roi,stoploss,trailing,protection,default} [{all,buy,sell,roi,stoploss,trailing,protection,default} ...] + --spaces {all,buy,sell,roi,stoploss,trailing,protection,trades,default} [{all,buy,sell,roi,stoploss,trailing,protection,trades,default} ...] Specify which parameters to hyperopt. Space-separated list. --print-all Print all results, not only the best ones. @@ -180,6 +180,7 @@ Rarely you may also need to create a [nested class](advanced-hyperopt.md#overrid * `generate_roi_table` - for custom ROI optimization (if you need the ranges for the values in the ROI table that differ from default or the number of entries (steps) in the ROI table which differs from the default 4 steps) * `stoploss_space` - for custom stoploss optimization (if you need the range for the stoploss parameter in the optimization hyperspace that differs from default) * `trailing_space` - for custom trailing stop optimization (if you need the ranges for the trailing stop parameters in the optimization hyperspace that differ from default) +* `max_open_trades_space` - for custom max_open_trades optimization (if you need the ranges for the max_open_trades parameter in the optimization hyperspace that differ from default) !!! Tip "Quickly optimize ROI, stoploss and trailing stoploss" You can quickly optimize the spaces `roi`, `stoploss` and `trailing` without changing anything in your strategy. @@ -643,6 +644,7 @@ Legal values are: * `roi`: just optimize the minimal profit table for your strategy * `stoploss`: search for the best stoploss value * `trailing`: search for the best trailing stop values +* `trades`: search for the best max open trades values * `protection`: search for the best protection parameters (read the [protections section](#optimizing-protections) on how to properly define these) * `default`: `all` except `trailing` and `protection` * space-separated list of any of the above values for example `--spaces roi stoploss` @@ -916,5 +918,5 @@ Once the optimized strategy has been implemented into your strategy, you should To achieve same the results (number of trades, their durations, profit, etc.) as during Hyperopt, please use the same configuration and parameters (timerange, timeframe, ...) used for hyperopt `--dmmp`/`--disable-max-market-positions` and `--eps`/`--enable-position-stacking` for Backtesting. Should results not match, please double-check to make sure you transferred all conditions correctly. -Pay special care to the stoploss (and trailing stoploss) parameters, as these are often set in configuration files, which override changes to the strategy. -You should also carefully review the log of your backtest to ensure that there were no parameters inadvertently set by the configuration (like `stoploss` or `trailing_stop`). +Pay special care to the stoploss, max_open_trades and trailing stoploss parameters, as these are often set in configuration files, which override changes to the strategy. +You should also carefully review the log of your backtest to ensure that there were no parameters inadvertently set by the configuration (like `stoploss`, `max_open_trades` or `trailing_stop`). diff --git a/docs/index.md b/docs/index.md index 40b9e98ad..c24d1f36b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -52,6 +52,7 @@ Please read the [exchange specific notes](exchanges.md) to learn about eventual, - [X] [Binance](https://www.binance.com/) - [X] [Gate.io](https://www.gate.io/ref/6266643) - [X] [OKX](https://okx.com/) +- [X] [Bybit](https://bybit.com/) Please make sure to read the [exchange specific notes](exchanges.md), as well as the [trading with leverage](leverage.md) documentation before diving in. diff --git a/docs/installation.md b/docs/installation.md index 9dd14274a..6e8488b9f 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -30,6 +30,12 @@ The easiest way to install and run Freqtrade is to clone the bot Github reposito !!! Warning "Up-to-date clock" The clock on the system running the bot must be accurate, synchronized to a NTP server frequently enough to avoid problems with communication to the exchanges. +!!! Error "Running setup.py install for gym did not run successfully." + If you get an error related with gym we suggest you to downgrade setuptools it to version 65.5.0 you can do it with the following command: + ```bash + pip install setuptools==65.5.0 + ``` + ------ ## Requirements @@ -284,10 +290,8 @@ cd freqtrade #### Freqtrade install: Conda Environment -Prepare conda-freqtrade environment, using file `environment.yml`, which exist in main freqtrade directory - ```bash -conda env create -n freqtrade-conda -f environment.yml +conda create --name freqtrade python=3.10 ``` !!! Note "Creating Conda Environment" @@ -296,12 +300,9 @@ conda env create -n freqtrade-conda -f environment.yml ```bash # choose your own packages conda env create -n [name of the environment] [python version] [packages] - - # point to file with packages - conda env create -n [name of the environment] -f [file] ``` -#### Enter/exit freqtrade-conda environment +#### Enter/exit freqtrade environment To check available environments, type @@ -313,7 +314,7 @@ Enter installed environment ```bash # enter conda environment -conda activate freqtrade-conda +conda activate freqtrade # exit conda environment - don't do it now conda deactivate @@ -323,6 +324,7 @@ Install last python dependencies with pip ```bash python3 -m pip install --upgrade pip +python3 -m pip install -r requirements.txt python3 -m pip install -e . ``` @@ -330,7 +332,7 @@ Patch conda libta-lib (Linux only) ```bash # Ensure that the environment is active! -conda activate freqtrade-conda +conda activate freqtrade cd build_helpers bash install_ta-lib.sh ${CONDA_PREFIX} nosudo @@ -349,8 +351,8 @@ conda env list # activate base environment conda activate -# activate freqtrade-conda environment -conda activate freqtrade-conda +# activate freqtrade environment +conda activate freqtrade #deactivate any conda environments conda deactivate diff --git a/docs/requirements-docs.txt b/docs/requirements-docs.txt index 774f40114..110373844 100644 --- a/docs/requirements-docs.txt +++ b/docs/requirements-docs.txt @@ -1,6 +1,6 @@ markdown==3.3.7 mkdocs==1.4.2 -mkdocs-material==9.0.5 +mkdocs-material==9.1.3 mdx_truly_sane_lists==1.3 -pymdown-extensions==9.9.1 +pymdown-extensions==9.10 jinja2==3.1.2 diff --git a/docs/rest-api.md b/docs/rest-api.md index 62ad586dd..5f604ef43 100644 --- a/docs/rest-api.md +++ b/docs/rest-api.md @@ -163,7 +163,7 @@ python3 scripts/rest_client.py --config rest_config.json [optional par | `strategy ` | Get specific Strategy content. **Alpha** | `available_pairs` | List available backtest data. **Alpha** | `version` | Show version. -| `sysinfo` | Show informations about the system load. +| `sysinfo` | Show information about the system load. | `health` | Show bot health (last bot loop). !!! Warning "Alpha status" @@ -192,6 +192,11 @@ blacklist :param add: List of coins to add (example: "BNB/BTC") +cancel_open_order + Cancel open order for trade. + + :param trade_id: Cancels open orders for this trade. + count Return the amount of open trades. @@ -274,7 +279,6 @@ reload_config Reload configuration. show_config - Returns part of the configuration, relevant for trading operations. start @@ -320,6 +324,7 @@ version whitelist Show the current whitelist. + ``` ### Message WebSocket diff --git a/docs/stoploss.md b/docs/stoploss.md index 20e53d8f5..7af717955 100644 --- a/docs/stoploss.md +++ b/docs/stoploss.md @@ -24,7 +24,7 @@ These modes can be configured with these values: ``` !!! Note - Stoploss on exchange is only supported for Binance (stop-loss-limit), Huobi (stop-limit), Kraken (stop-loss-market, stop-loss-limit), Gateio (stop-limit), and Kucoin (stop-limit and stop-market) as of now. + Stoploss on exchange is only supported for Binance (stop-loss-limit), Huobi (stop-limit), Kraken (stop-loss-market, stop-loss-limit), Gate (stop-limit), and Kucoin (stop-limit and stop-market) as of now. Do not set too low/tight stoploss value if using stop loss on exchange! If set to low/tight then you have greater risk of missing fill on the order and stoploss will not work. @@ -52,6 +52,18 @@ The bot cannot do these every 5 seconds (at each iteration), otherwise it would So this parameter will tell the bot how often it should update the stoploss order. The default value is 60 (1 minute). This same logic will reapply a stoploss order on the exchange should you cancel it accidentally. +### stoploss_price_type + +!!! Warning "Only applies to futures" + `stoploss_price_type` only applies to futures markets (on exchanges where it's available). + Freqtrade will perform a validation of this setting on startup, failing to start if an invalid setting for your exchange has been selected. + Supported price types are gonna differs between each exchanges. Please check with your exchange on which price types it supports. + +Stoploss on exchange on futures markets can trigger on different price types. +The naming for these prices in exchange terminology often varies, but is usually something around "last" (or "contract price" ), "mark" and "index". + +Acceptable values for this setting are `"last"`, `"mark"` and `"index"` - which freqtrade will transfer automatically to the corresponding API type, and place the [stoploss on exchange](#stoploss_on_exchange-and-stoploss_on_exchange_limit_ratio) order correspondingly. + ### force_exit `force_exit` is an optional value, which defaults to the same value as `exit` and is used when sending a `/forceexit` command from Telegram or from the Rest API. diff --git a/docs/strategy-advanced.md b/docs/strategy-advanced.md index f55cda5e2..cbb71e810 100644 --- a/docs/strategy-advanced.md +++ b/docs/strategy-advanced.md @@ -80,7 +80,7 @@ class AwesomeStrategy(IStrategy): ## Enter Tag When your strategy has multiple buy signals, you can name the signal that triggered. -Then you can access you buy signal on `custom_exit` +Then you can access your buy signal on `custom_exit` ```python def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: diff --git a/docs/strategy-callbacks.md b/docs/strategy-callbacks.md index 19bd26a04..f1cdc9f3b 100644 --- a/docs/strategy-callbacks.md +++ b/docs/strategy-callbacks.md @@ -316,11 +316,11 @@ class AwesomeStrategy(IStrategy): # evaluate highest to lowest, so that highest possible stop is used if current_profit > 0.40: - return stoploss_from_open(0.25, current_profit, is_short=trade.is_short) + return stoploss_from_open(0.25, current_profit, is_short=trade.is_short, leverage=trade.leverage) elif current_profit > 0.25: - return stoploss_from_open(0.15, current_profit, is_short=trade.is_short) + return stoploss_from_open(0.15, current_profit, is_short=trade.is_short, leverage=trade.leverage) elif current_profit > 0.20: - return stoploss_from_open(0.07, current_profit, is_short=trade.is_short) + return stoploss_from_open(0.07, current_profit, is_short=trade.is_short, leverage=trade.leverage) # return maximum stoploss value, keeping current stoploss price unchanged return 1 @@ -659,6 +659,7 @@ Position adjustments will always be applied in the direction of the trade, so a !!! Warning "Backtesting" During backtesting this callback is called for each candle in `timeframe` or `timeframe_detail`, so run-time performance will be affected. + This can also cause deviating results between live and backtesting, since backtesting can adjust the trade only once per candle, whereas live could adjust the trade multiple times per candle. ``` python from freqtrade.persistence import Trade @@ -827,7 +828,7 @@ class AwesomeStrategy(IStrategy): """ # Limit orders to use and follow SMA200 as price target for the first 10 minutes since entry trigger for BTC/USDT pair. - if pair == 'BTC/USDT' and entry_tag == 'long_sma200' and side == 'long' and (current_time - timedelta(minutes=10) > trade.open_date_utc: + if pair == 'BTC/USDT' and entry_tag == 'long_sma200' and side == 'long' and (current_time - timedelta(minutes=10)) > trade.open_date_utc: # just cancel the order if it has been filled more than half of the amount if order.filled > order.remaining: return None diff --git a/docs/strategy-customization.md b/docs/strategy-customization.md index 462f20402..8b6654c6c 100644 --- a/docs/strategy-customization.md +++ b/docs/strategy-customization.md @@ -881,7 +881,7 @@ All columns of the informative dataframe will be available on the returning data ### *stoploss_from_open()* -Stoploss values returned from `custom_stoploss` must specify a percentage relative to `current_rate`, but sometimes you may want to specify a stoploss relative to the open price instead. `stoploss_from_open()` is a helper function to calculate a stoploss value that can be returned from `custom_stoploss` which will be equivalent to the desired percentage above the open price. +Stoploss values returned from `custom_stoploss` must specify a percentage relative to `current_rate`, but sometimes you may want to specify a stoploss relative to the entry point instead. `stoploss_from_open()` is a helper function to calculate a stoploss value that can be returned from `custom_stoploss` which will be equivalent to the desired trade profit above the entry point. ??? Example "Returning a stoploss relative to the open price from the custom stoploss function" @@ -889,6 +889,8 @@ Stoploss values returned from `custom_stoploss` must specify a percentage relati If we want a stop price at 7% above the open price we can call `stoploss_from_open(0.07, current_profit, False)` which will return `0.1157024793`. 11.57% below $121 is $107, which is the same as 7% above $100. + This function will consider leverage - so at 10x leverage, the actual stoploss would be 0.7% above $100 (0.7% * 10x = 7%). + ``` python @@ -907,7 +909,7 @@ Stoploss values returned from `custom_stoploss` must specify a percentage relati # once the profit has risen above 10%, keep the stoploss at 7% above the open price if current_profit > 0.10: - return stoploss_from_open(0.07, current_profit, is_short=trade.is_short) + return stoploss_from_open(0.07, current_profit, is_short=trade.is_short, leverage=trade.leverage) return 1 @@ -954,12 +956,14 @@ In some situations it may be confusing to deal with stops relative to current ra ## Additional data (Wallets) -The strategy provides access to the `Wallets` object. This contains the current balances on the exchange. +The strategy provides access to the `wallets` object. This contains the current balances on the exchange. -!!! Note - Wallets is not available during backtesting / hyperopt. +!!! Note "Backtesting / Hyperopt" + Wallets behaves differently depending on the function it's called. + Within `populate_*()` methods, it'll return the full wallet as configured. + Within [callbacks](strategy-callbacks.md), you'll get the wallet state corresponding to the actual simulated wallet at that point in the simulation process. -Please always check if `Wallets` is available to avoid failures during backtesting. +Please always check if `wallets` is available to avoid failures during backtesting. ``` python if self.wallets: @@ -1036,11 +1040,10 @@ from datetime import timedelta, datetime, timezone # Within populate indicators (or populate_buy): if self.config['runmode'].value in ('live', 'dry_run'): - # fetch closed trades for the last 2 days - trades = Trade.get_trades([Trade.pair == metadata['pair'], - Trade.open_date > datetime.utcnow() - timedelta(days=2), - Trade.is_open.is_(False), - ]).all() + # fetch closed trades for the last 2 days + trades = Trade.get_trades_proxy( + pair=metadata['pair'], is_open=False, + open_date=datetime.now(timezone.utc) - timedelta(days=2)) # Analyze the conditions you'd like to lock the pair .... will probably be different for every strategy sumprofit = sum(trade.close_profit for trade in trades) if sumprofit < 0: diff --git a/docs/strategy_analysis_example.md b/docs/strategy_analysis_example.md index e3d2870e2..06dd33bc2 100644 --- a/docs/strategy_analysis_example.md +++ b/docs/strategy_analysis_example.md @@ -80,6 +80,7 @@ from freqtrade.resolvers import StrategyResolver from freqtrade.data.dataprovider import DataProvider strategy = StrategyResolver.load_strategy(config) strategy.dp = DataProvider(config, None, None) +strategy.ft_bot_start() # Generate buy/sell signals using strategy df = strategy.analyze_ticker(candles, {'pair': pair}) diff --git a/docs/telegram-usage.md b/docs/telegram-usage.md index db4a309d0..dc0ab0976 100644 --- a/docs/telegram-usage.md +++ b/docs/telegram-usage.md @@ -152,7 +152,7 @@ You can create your own keyboard in `config.json`: !!! Note "Supported Commands" Only the following commands are allowed. Command arguments are not supported! - `/start`, `/stop`, `/status`, `/status table`, `/trades`, `/profit`, `/performance`, `/daily`, `/stats`, `/count`, `/locks`, `/balance`, `/stopentry`, `/reload_config`, `/show_config`, `/logs`, `/whitelist`, `/blacklist`, `/edge`, `/help`, `/version` + `/start`, `/stop`, `/status`, `/status table`, `/trades`, `/profit`, `/performance`, `/daily`, `/stats`, `/count`, `/locks`, `/balance`, `/stopentry`, `/reload_config`, `/show_config`, `/logs`, `/whitelist`, `/blacklist`, `/edge`, `/help`, `/version`, `/marketdir` ## Telegram commands @@ -162,26 +162,34 @@ official commands. You can ask at any moment for help with `/help`. | Command | Description | |----------|-------------| +| **System commands** | `/start` | Starts the trader | `/stop` | Stops the trader | `/stopbuy | /stopentry` | Stops the trader from opening new trades. Gracefully closes open trades according to their rules. | `/reload_config` | Reloads the configuration file | `/show_config` | Shows part of the current configuration with relevant settings to operation | `/logs [limit]` | Show last log messages. +| `/help` | Show help message +| `/version` | Show version +| **Status** | | `/status` | Lists all open trades | `/status ` | Lists one or more specific trade. Separate multiple with a blank space. | `/status table` | List all open trades in a table format. Pending buy orders are marked with an asterisk (*) Pending sell orders are marked with a double asterisk (**) | `/trades [limit]` | List all recently closed trades in a table format. -| `/delete ` | Delete a specific trade from the Database. Tries to close open orders. Requires manual handling of this trade on the exchange. | `/count` | Displays number of trades used and available | `/locks` | Show currently locked pairs. | `/unlock ` | Remove the lock for this pair (or for this lock id). -| `/profit []` | Display a summary of your profit/loss from close trades and some stats about your performance, over the last n days (all trades by default) +| `/marketdir [long | short | even | none]` | Updates the user managed variable that represents the current market direction. If no direction is provided, the currently set direction will be displayed. +| **Modify Trade states** | | `/forceexit | /fx ` | Instantly exits the given trade (Ignoring `minimum_roi`). | `/forceexit all | /fx all` | Instantly exits all open trades (Ignoring `minimum_roi`). | `/fx` | alias for `/forceexit` | `/forcelong [rate]` | Instantly buys the given pair. Rate is optional and only applies to limit orders. (`force_entry_enable` must be set to True) | `/forceshort [rate]` | Instantly shorts the given pair. Rate is optional and only applies to limit orders. This will only work on non-spot markets. (`force_entry_enable` must be set to True) +| `/delete ` | Delete a specific trade from the Database. Tries to close open orders. Requires manual handling of this trade on the exchange. +| `/cancel_open_order | /coo ` | Cancel an open order for a trade. +| **Metrics** | +| `/profit []` | Display a summary of your profit/loss from close trades and some stats about your performance, over the last n days (all trades by default) | `/performance` | Show performance of each finished trade grouped by pair | `/balance` | Show account balance per currency | `/daily ` | Shows profit or loss per day, over the last n days (n defaults to 7) @@ -193,8 +201,7 @@ official commands. You can ask at any moment for help with `/help`. | `/whitelist [sorted] [baseonly]` | Show the current whitelist. Optionally display in alphabetical order and/or with just the base currency of each pairing. | `/blacklist [pair]` | Show the current blacklist, or adds a pair to the blacklist. | `/edge` | Show validated pairs by Edge if it is enabled. -| `/help` | Show help message -| `/version` | Show version + ## Telegram commands in action @@ -236,7 +243,7 @@ Enter Tag is configurable via Strategy. > **Enter Tag:** Awesome Long Signal > **Open Rate:** `0.00007489` > **Current Rate:** `0.00007489` -> **Current Profit:** `12.95%` +> **Unrealized Profit:** `12.95%` > **Stoploss:** `0.00007389 (-0.02%)` ### /status table @@ -410,3 +417,27 @@ ARDR/ETH 0.366667 0.143059 -0.01 ### /version > **Version:** `0.14.3` + +### /marketdir + +If a market direction is provided the command updates the user managed variable that represents the current market direction. +This variable is not set to any valid market direction on bot startup and must be set by the user. The example below is for `/marketdir long`: + +``` +Successfully updated marketdirection from none to long. +``` + +If no market direction is provided the command outputs the currently set market directions. The example below is for `/marketdir`: + +``` +Currently set marketdirection: even +``` + +You can use the market direction in your strategy via `self.market_direction`. + +!!! Warning "Bot restarts" + Please note that the market direction is not persisted, and will be reset after a bot restart/reload. + +!!! Danger "Backtesting" + As this value/variable is intended to be changed manually in dry/live trading. + Strategies using `market_direction` will probably not produce reliable, reproducible results (changes to this variable will not be reflected for backtesting). Use at your own risk. diff --git a/docs/utils.md b/docs/utils.md index 87c7f6aa6..eb675442f 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -955,3 +955,47 @@ Print trades with id 2 and 3 as json ``` bash freqtrade show-trades --db-url sqlite:///tradesv3.sqlite --trade-ids 2 3 --print-json ``` + +### Strategy-Updater + +Updates listed strategies or all strategies within the strategies folder to be v3 compliant. +If the command runs without --strategy-list then all strategies inside the strategies folder will be converted. +Your original strategy will remain available in the `user_data/strategies_orig_updater/` directory. + +!!! Warning "Conversion results" + Strategy updater will work on a "best effort" approach. Please do your due diligence and verify the results of the conversion. + We also recommend to run a python formatter (e.g. `black`) to format results in a sane manner. + +``` +usage: freqtrade strategy-updater [-h] [-v] [--logfile FILE] [-V] [-c PATH] + [-d PATH] [--userdir PATH] + [--strategy-list STRATEGY_LIST [STRATEGY_LIST ...]] + +options: + -h, --help show this help message and exit + --strategy-list STRATEGY_LIST [STRATEGY_LIST ...] + Provide a space-separated list of strategies to + backtest. Please note that timeframe needs to be set + either in config or via command line. When using this + together with `--export trades`, the strategy-name is + injected into the filename (so `backtest-data.json` + becomes `backtest-data-SampleStrategy.json` + +Common arguments: + -v, --verbose Verbose mode (-vv for more, -vvv to get all messages). + --logfile FILE, --log-file FILE + Log to the file specified. Special values are: + 'syslog', 'journald'. See the documentation for more + details. + -V, --version show program's version number and exit + -c PATH, --config PATH + Specify configuration file (default: + `userdir/config.json` or `config.json` whichever + exists). Multiple --config options may be used. Can be + set to `-` to read config from stdin. + -d PATH, --datadir PATH, --data-dir PATH + Path to directory with historical backtesting data. + --userdir PATH, --user-data-dir PATH + Path to userdata directory. + +``` diff --git a/docs/windows_installation.md b/docs/windows_installation.md index 1b0d9d724..43d6728ee 100644 --- a/docs/windows_installation.md +++ b/docs/windows_installation.md @@ -26,7 +26,7 @@ Install ta-lib according to the [ta-lib documentation](https://github.com/mrjbq7 As compiling from source on windows has heavy dependencies (requires a partial visual studio installation), there is also a repository of unofficial pre-compiled windows Wheels [here](https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib), which need to be downloaded and installed using `pip install TA_Lib-0.4.25-cp38-cp38-win_amd64.whl` (make sure to use the version matching your python version). -Freqtrade provides these dependencies for the latest 3 Python versions (3.8, 3.9 and 3.10) and for 64bit Windows. +Freqtrade provides these dependencies for the latest 3 Python versions (3.8, 3.9, 3.10 and 3.11) and for 64bit Windows. Other versions must be downloaded from the above link. ``` powershell diff --git a/environment.yml b/environment.yml index 5298b2baa..e69de29bb 100644 --- a/environment.yml +++ b/environment.yml @@ -1,75 +0,0 @@ -name: freqtrade -channels: - - conda-forge -# - defaults -dependencies: -# 1/4 req main - - python>=3.8,<=3.10 - - numpy - - pandas - - pip - - - py-find-1st - - aiohttp - - SQLAlchemy - - python-telegram-bot - - arrow - - cachetools - - requests - - urllib3 - - jsonschema - - TA-Lib - - tabulate - - jinja2 - - blosc - - sdnotify - - fastapi - - uvicorn - - pyjwt - - aiofiles - - psutil - - colorama - - questionary - - prompt-toolkit - - schedule - - python-dateutil - - joblib - - pyarrow - - - # ============================ - # 2/4 req dev - - - coveralls - - flake8 - - mypy - - pytest - - pytest-asyncio - - pytest-cov - - pytest-mock - - isort - - nbconvert - - # ============================ - # 3/4 req hyperopt - - - scipy - - scikit-learn - - filelock - - scikit-optimize - - progressbar2 - # ============================ - # 4/4 req plot - - - plotly - - jupyter - - - pip: - - pycoingecko - # - py_find_1st - - tables - - pytest-random-order - - ccxt - - flake8-tidy-imports - - -e . - # - python-rapidjso diff --git a/freqtrade/__init__.py b/freqtrade/__init__.py index 18b6c9130..6ba045adf 100644 --- a/freqtrade/__init__.py +++ b/freqtrade/__init__.py @@ -1,5 +1,5 @@ """ Freqtrade bot """ -__version__ = '2023.1.dev' +__version__ = '2023.3.dev' if 'dev' in __version__: from pathlib import Path diff --git a/freqtrade/__main__.py b/freqtrade/__main__.py old mode 100644 new mode 100755 diff --git a/freqtrade/commands/__init__.py b/freqtrade/commands/__init__.py index 788657cc8..66a9c995b 100644 --- a/freqtrade/commands/__init__.py +++ b/freqtrade/commands/__init__.py @@ -22,5 +22,6 @@ from freqtrade.commands.optimize_commands import (start_backtesting, start_backt start_edge, start_hyperopt) from freqtrade.commands.pairlist_commands import start_test_pairlist from freqtrade.commands.plot_commands import start_plot_dataframe, start_plot_profit +from freqtrade.commands.strategy_utils_commands import start_strategy_update from freqtrade.commands.trade_commands import start_trading from freqtrade.commands.webserver_commands import start_webserver diff --git a/freqtrade/commands/analyze_commands.py b/freqtrade/commands/analyze_commands.py old mode 100755 new mode 100644 index 20afa7ffd..e928ccad7 --- a/freqtrade/commands/analyze_commands.py +++ b/freqtrade/commands/analyze_commands.py @@ -40,8 +40,8 @@ def setup_analyze_configuration(args: Dict[str, Any], method: RunMode) -> Dict[s if (not Path(signals_file).exists()): raise OperationalException( - (f"Cannot find latest backtest signals file: {signals_file}." - "Run backtesting with `--export signals`.") + f"Cannot find latest backtest signals file: {signals_file}." + "Run backtesting with `--export signals`." ) return config diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index b53a1022d..47aa37fdf 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -111,10 +111,13 @@ ARGS_ANALYZE_ENTRIES_EXITS = ["exportfilename", "analysis_groups", "enter_reason NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes", "list-markets", "list-pairs", "list-strategies", "list-freqaimodels", "list-data", "hyperopt-list", "hyperopt-show", "backtest-filter", - "plot-dataframe", "plot-profit", "show-trades", "trades-to-ohlcv"] + "plot-dataframe", "plot-profit", "show-trades", "trades-to-ohlcv", + "strategy-updater"] NO_CONF_ALLOWED = ["create-userdir", "list-exchanges", "new-strategy"] +ARGS_STRATEGY_UTILS = ["strategy_list", "strategy_path", "recursive_strategy_search"] + class Arguments: """ @@ -198,8 +201,8 @@ class Arguments: start_list_freqAI_models, start_list_markets, start_list_strategies, start_list_timeframes, start_new_config, start_new_strategy, start_plot_dataframe, - start_plot_profit, start_show_trades, start_test_pairlist, - start_trading, start_webserver) + start_plot_profit, start_show_trades, start_strategy_update, + start_test_pairlist, start_trading, start_webserver) subparsers = self.parser.add_subparsers(dest='command', # Use custom message when no subhandler is added @@ -440,3 +443,11 @@ class Arguments: parents=[_common_parser]) webserver_cmd.set_defaults(func=start_webserver) self._build_args(optionlist=ARGS_WEBSERVER, parser=webserver_cmd) + + # Add strategy_updater subcommand + strategy_updater_cmd = subparsers.add_parser('strategy-updater', + help='updates outdated strategy' + 'files to the current version', + parents=[_common_parser]) + strategy_updater_cmd.set_defaults(func=start_strategy_update) + self._build_args(optionlist=ARGS_STRATEGY_UTILS, parser=strategy_updater_cmd) diff --git a/freqtrade/commands/build_config_commands.py b/freqtrade/commands/build_config_commands.py index f95a08ba5..63bb5c211 100644 --- a/freqtrade/commands/build_config_commands.py +++ b/freqtrade/commands/build_config_commands.py @@ -108,7 +108,7 @@ def ask_user_config() -> Dict[str, Any]: "binance", "binanceus", "bittrex", - "gateio", + "gate", "huobi", "kraken", "kucoin", @@ -123,7 +123,7 @@ def ask_user_config() -> Dict[str, Any]: "message": "Do you want to trade Perpetual Swaps (perpetual futures)?", "default": False, "filter": lambda val: 'futures' if val else 'spot', - "when": lambda x: x["exchange_name"] in ['binance', 'gateio', 'okx'], + "when": lambda x: x["exchange_name"] in ['binance', 'gate', 'okx'], }, { "type": "autocomplete", diff --git a/freqtrade/commands/cli_options.py b/freqtrade/commands/cli_options.py index 50fdb1aa2..f1474ec69 100644 --- a/freqtrade/commands/cli_options.py +++ b/freqtrade/commands/cli_options.py @@ -251,7 +251,8 @@ AVAILABLE_CLI_OPTIONS = { "spaces": Arg( '--spaces', help='Specify which parameters to hyperopt. Space-separated list.', - choices=['all', 'buy', 'sell', 'roi', 'stoploss', 'trailing', 'protection', 'default'], + choices=['all', 'buy', 'sell', 'roi', 'stoploss', + 'trailing', 'protection', 'trades', 'default'], nargs='+', default='default', ), diff --git a/freqtrade/commands/data_commands.py b/freqtrade/commands/data_commands.py index 2cd736b3e..1e74e1036 100644 --- a/freqtrade/commands/data_commands.py +++ b/freqtrade/commands/data_commands.py @@ -5,7 +5,7 @@ from datetime import datetime, timedelta from typing import Any, Dict, List from freqtrade.configuration import TimeRange, setup_utils_configuration -from freqtrade.constants import DATETIME_PRINT_FORMAT +from freqtrade.constants import DATETIME_PRINT_FORMAT, Config from freqtrade.data.converter import convert_ohlcv_format, convert_trades_format from freqtrade.data.history import (convert_trades_to_ohlcv, refresh_backtest_ohlcv_data, refresh_backtest_trades_data) @@ -20,15 +20,24 @@ from freqtrade.util.binance_mig import migrate_binance_futures_data logger = logging.getLogger(__name__) +def _data_download_sanity(config: Config) -> None: + if 'days' in config and 'timerange' in config: + raise OperationalException("--days and --timerange are mutually exclusive. " + "You can only specify one or the other.") + + if 'pairs' not in config: + raise OperationalException( + "Downloading data requires a list of pairs. " + "Please check the documentation on how to configure this.") + + def start_download_data(args: Dict[str, Any]) -> None: """ Download data (former download_backtest_data.py script) """ config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE) - if 'days' in config and 'timerange' in config: - raise OperationalException("--days and --timerange are mutually exclusive. " - "You can only specify one or the other.") + _data_download_sanity(config) timerange = TimeRange() if 'days' in config: time_since = (datetime.now() - timedelta(days=config['days'])).strftime("%Y%m%d") @@ -40,11 +49,6 @@ def start_download_data(args: Dict[str, Any]) -> None: # Remove stake-currency to skip checks which are not relevant for datadownload config['stake_currency'] = '' - if 'pairs' not in config: - raise OperationalException( - "Downloading data requires a list of pairs. " - "Please check the documentation on how to configure this.") - pairs_not_available: List[str] = [] # Init exchange diff --git a/freqtrade/commands/db_commands.py b/freqtrade/commands/db_commands.py index c424016b1..d83605c6f 100644 --- a/freqtrade/commands/db_commands.py +++ b/freqtrade/commands/db_commands.py @@ -1,7 +1,7 @@ import logging from typing import Any, Dict -from sqlalchemy import func +from sqlalchemy import func, select from freqtrade.configuration.config_setup import setup_utils_configuration from freqtrade.enums import RunMode @@ -20,7 +20,7 @@ def start_convert_db(args: Dict[str, Any]) -> None: config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) init_db(config['db_url']) - session_target = Trade._session + session_target = Trade.session init_db(config['db_url_from']) logger.info("Starting db migration.") @@ -36,16 +36,16 @@ def start_convert_db(args: Dict[str, Any]) -> None: session_target.commit() - for pairlock in PairLock.query: + for pairlock in PairLock.get_all_locks(): pairlock_count += 1 make_transient(pairlock) session_target.add(pairlock) session_target.commit() # Update sequences - max_trade_id = session_target.query(func.max(Trade.id)).scalar() - max_order_id = session_target.query(func.max(Order.id)).scalar() - max_pairlock_id = session_target.query(func.max(PairLock.id)).scalar() + max_trade_id = session_target.scalar(select(func.max(Trade.id))) + max_order_id = session_target.scalar(select(func.max(Order.id))) + max_pairlock_id = session_target.scalar(select(func.max(PairLock.id))) set_sequence_ids(session_target.get_bind(), trade_id=max_trade_id, diff --git a/freqtrade/commands/hyperopt_commands.py b/freqtrade/commands/hyperopt_commands.py old mode 100755 new mode 100644 diff --git a/freqtrade/commands/strategy_utils_commands.py b/freqtrade/commands/strategy_utils_commands.py new file mode 100644 index 000000000..e579ec475 --- /dev/null +++ b/freqtrade/commands/strategy_utils_commands.py @@ -0,0 +1,55 @@ +import logging +import sys +import time +from pathlib import Path +from typing import Any, Dict + +from freqtrade.configuration import setup_utils_configuration +from freqtrade.enums import RunMode +from freqtrade.resolvers import StrategyResolver +from freqtrade.strategy.strategyupdater import StrategyUpdater + + +logger = logging.getLogger(__name__) + + +def start_strategy_update(args: Dict[str, Any]) -> None: + """ + Start the strategy updating script + :param args: Cli args from Arguments() + :return: None + """ + + if sys.version_info == (3, 8): # pragma: no cover + sys.exit("Freqtrade strategy updater requires Python version >= 3.9") + + config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) + + strategy_objs = StrategyResolver.search_all_objects( + config, enum_failed=False, recursive=config.get('recursive_strategy_search', False)) + + filtered_strategy_objs = [] + if args['strategy_list']: + filtered_strategy_objs = [ + strategy_obj for strategy_obj in strategy_objs + if strategy_obj['name'] in args['strategy_list'] + ] + + else: + # Use all available entries. + filtered_strategy_objs = strategy_objs + + processed_locations = set() + for strategy_obj in filtered_strategy_objs: + if strategy_obj['location'] not in processed_locations: + processed_locations.add(strategy_obj['location']) + start_conversion(strategy_obj, config) + + +def start_conversion(strategy_obj, config): + print(f"Conversion of {Path(strategy_obj['location']).name} started.") + instance_strategy_updater = StrategyUpdater() + start = time.perf_counter() + instance_strategy_updater.start(config, strategy_obj) + elapsed = time.perf_counter() - start + print(f"Conversion of {Path(strategy_obj['location']).name} took {elapsed:.1f} seconds.") diff --git a/freqtrade/commands/trade_commands.py b/freqtrade/commands/trade_commands.py index 535844844..0707cc803 100644 --- a/freqtrade/commands/trade_commands.py +++ b/freqtrade/commands/trade_commands.py @@ -1,4 +1,5 @@ import logging +import signal from typing import Any, Dict @@ -12,15 +13,20 @@ def start_trading(args: Dict[str, Any]) -> int: # Import here to avoid loading worker module when it's not used from freqtrade.worker import Worker + def term_handler(signum, frame): + # Raise KeyboardInterrupt - so we can handle it in the same way as Ctrl-C + raise KeyboardInterrupt() + # Create and run worker worker = None try: + signal.signal(signal.SIGTERM, term_handler) worker = Worker(args) worker.run() except Exception as e: logger.error(str(e)) logger.exception("Fatal exception!") - except KeyboardInterrupt: + except (KeyboardInterrupt): logger.info('SIGINT received, aborting ...') finally: if worker: diff --git a/freqtrade/configuration/config_validation.py b/freqtrade/configuration/config_validation.py index 606f081ef..0ee48cf91 100644 --- a/freqtrade/configuration/config_validation.py +++ b/freqtrade/configuration/config_validation.py @@ -27,10 +27,7 @@ def _extend_validator(validator_class): if 'default' in subschema: instance.setdefault(prop, subschema['default']) - for error in validate_properties( - validator, properties, instance, schema, - ): - yield error + yield from validate_properties(validator, properties, instance, schema) return validators.extend( validator_class, {'properties': set_defaults} diff --git a/freqtrade/configuration/configuration.py b/freqtrade/configuration/configuration.py index 664610f33..862976eb1 100644 --- a/freqtrade/configuration/configuration.py +++ b/freqtrade/configuration/configuration.py @@ -28,7 +28,7 @@ class Configuration: Reuse this class for the bot, backtesting, hyperopt and every script that required configuration """ - def __init__(self, args: Dict[str, Any], runmode: RunMode = None) -> None: + def __init__(self, args: Dict[str, Any], runmode: Optional[RunMode] = None) -> None: self.args = args self.config: Optional[Config] = None self.runmode = runmode diff --git a/freqtrade/configuration/environment_vars.py b/freqtrade/configuration/environment_vars.py index 473758fe1..d59d4bd23 100644 --- a/freqtrade/configuration/environment_vars.py +++ b/freqtrade/configuration/environment_vars.py @@ -32,7 +32,7 @@ def flat_vars_to_nested_dict(env_dict: Dict[str, Any], prefix: str) -> Dict[str, :param prefix: Prefix to consider (usually FREQTRADE__) :return: Nested dict based on available and relevant variables. """ - no_convert = ['CHAT_ID'] + no_convert = ['CHAT_ID', 'PASSWORD'] relevant_vars: Dict[str, Any] = {} for env_var, val in sorted(env_dict.items()): diff --git a/freqtrade/configuration/load_config.py b/freqtrade/configuration/load_config.py index 6d0321ba0..57424468d 100644 --- a/freqtrade/configuration/load_config.py +++ b/freqtrade/configuration/load_config.py @@ -6,7 +6,7 @@ import re import sys from copy import deepcopy from pathlib import Path -from typing import Any, Dict, List +from typing import Any, Dict, List, Optional import rapidjson @@ -58,7 +58,7 @@ def load_config_file(path: str) -> Dict[str, Any]: """ try: # Read config from stdin if requested in the options - with open(path) if path != '-' else sys.stdin as file: + with Path(path).open() if path != '-' else sys.stdin as file: config = rapidjson.load(file, parse_mode=CONFIG_PARSE_MODE) except FileNotFoundError: raise OperationalException( @@ -75,7 +75,8 @@ def load_config_file(path: str) -> Dict[str, Any]: return config -def load_from_files(files: List[str], base_path: Path = None, level: int = 0) -> Dict[str, Any]: +def load_from_files( + files: List[str], base_path: Optional[Path] = None, level: int = 0) -> Dict[str, Any]: """ Recursively load configuration files if specified. Sub-files are assumed to be relative to the initial config. diff --git a/freqtrade/constants.py b/freqtrade/constants.py index 397367216..46e9b5cd4 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -5,7 +5,7 @@ bot constants """ from typing import Any, Dict, List, Literal, Tuple -from freqtrade.enums import CandleType, RPCMessageType +from freqtrade.enums import CandleType, PriceType, RPCMessageType DEFAULT_CONFIG = 'config.json' @@ -25,6 +25,7 @@ PRICING_SIDES = ['ask', 'bid', 'same', 'other'] ORDERTYPE_POSSIBILITIES = ['limit', 'market'] _ORDERTIF_POSSIBILITIES = ['GTC', 'FOK', 'IOC', 'PO'] ORDERTIF_POSSIBILITIES = _ORDERTIF_POSSIBILITIES + [t.lower() for t in _ORDERTIF_POSSIBILITIES] +STOPLOSS_PRICE_TYPES = [p for p in PriceType] HYPEROPT_LOSS_BUILTIN = ['ShortTradeDurHyperOptLoss', 'OnlyProfitHyperOptLoss', 'SharpeHyperOptLoss', 'SharpeHyperOptLossDaily', 'SortinoHyperOptLoss', 'SortinoHyperOptLossDaily', @@ -229,6 +230,7 @@ CONF_SCHEMA = { 'default': 'market'}, 'stoploss': {'type': 'string', 'enum': ORDERTYPE_POSSIBILITIES}, 'stoploss_on_exchange': {'type': 'boolean'}, + 'stoploss_price_type': {'type': 'string', 'enum': STOPLOSS_PRICE_TYPES}, 'stoploss_on_exchange_interval': {'type': 'number'}, 'stoploss_on_exchange_limit_ratio': {'type': 'number', 'minimum': 0.0, 'maximum': 1.0} @@ -544,7 +546,7 @@ CONF_SCHEMA = { "enabled": {"type": "boolean", "default": False}, "keras": {"type": "boolean", "default": False}, "write_metrics_to_disk": {"type": "boolean", "default": False}, - "purge_old_models": {"type": "boolean", "default": True}, + "purge_old_models": {"type": ["boolean", "number"], "default": 2}, "conv_width": {"type": "integer", "default": 1}, "train_period_days": {"type": "integer", "default": 0}, "backtest_period_days": {"type": "number", "default": 7}, @@ -566,7 +568,9 @@ CONF_SCHEMA = { "shuffle": {"type": "boolean", "default": False}, "nu": {"type": "number", "default": 0.1} }, - } + }, + "shuffle_after_split": {"type": "boolean", "default": False}, + "buffer_train_data_candles": {"type": "integer", "default": 0} }, "required": ["include_timeframes", "include_corr_pairlist", ] }, @@ -584,6 +588,7 @@ CONF_SCHEMA = { "rl_config": { "type": "object", "properties": { + "drop_ohlc_from_features": {"type": "boolean", "default": False}, "train_cycles": {"type": "integer"}, "max_trade_duration_candles": {"type": "integer"}, "add_state_info": {"type": "boolean", "default": False}, @@ -636,7 +641,6 @@ SCHEMA_TRADE_REQUIRED = [ SCHEMA_BACKTEST_REQUIRED = [ 'exchange', - 'max_open_trades', 'stake_currency', 'stake_amount', 'dry_run_wallet', @@ -646,6 +650,7 @@ SCHEMA_BACKTEST_REQUIRED = [ SCHEMA_BACKTEST_REQUIRED_FINAL = SCHEMA_BACKTEST_REQUIRED + [ 'stoploss', 'minimal_roi', + 'max_open_trades' ] SCHEMA_MINIMAL_REQUIRED = [ @@ -679,5 +684,7 @@ EntryExit = Literal['entry', 'exit'] BuySell = Literal['buy', 'sell'] MakerTaker = Literal['maker', 'taker'] BidAsk = Literal['bid', 'ask'] +OBLiteral = Literal['asks', 'bids'] Config = Dict[str, Any] +IntOrInf = float diff --git a/freqtrade/data/btanalysis.py b/freqtrade/data/btanalysis.py index 3102683b2..3567f4112 100644 --- a/freqtrade/data/btanalysis.py +++ b/freqtrade/data/btanalysis.py @@ -10,7 +10,7 @@ from typing import Any, Dict, List, Optional, Union import numpy as np import pandas as pd -from freqtrade.constants import LAST_BT_RESULT_FN +from freqtrade.constants import LAST_BT_RESULT_FN, IntOrInf from freqtrade.exceptions import OperationalException from freqtrade.misc import json_load from freqtrade.optimize.backtest_caching import get_backtest_metadata_filename @@ -90,7 +90,8 @@ def get_latest_hyperopt_filename(directory: Union[Path, str]) -> str: return 'hyperopt_results.pickle' -def get_latest_hyperopt_file(directory: Union[Path, str], predef_filename: str = None) -> Path: +def get_latest_hyperopt_file( + directory: Union[Path, str], predef_filename: Optional[str] = None) -> Path: """ Get latest hyperopt export based on '.last_result.json'. :param directory: Directory to search for last result @@ -193,7 +194,7 @@ def get_backtest_resultlist(dirname: Path): def find_existing_backtest_stats(dirname: Union[Path, str], run_ids: Dict[str, str], - min_backtest_date: datetime = None) -> Dict[str, Any]: + min_backtest_date: Optional[datetime] = None) -> Dict[str, Any]: """ Find existing backtest stats that match specified run IDs and load them. :param dirname: pathlib.Path object, or string pointing to the file. @@ -332,7 +333,7 @@ def analyze_trade_parallelism(results: pd.DataFrame, timeframe: str) -> pd.DataF def evaluate_result_multi(results: pd.DataFrame, timeframe: str, - max_open_trades: int) -> pd.DataFrame: + max_open_trades: IntOrInf) -> pd.DataFrame: """ Find overlapping trades by expanding each trade once per period it was open and then counting overlaps @@ -345,7 +346,7 @@ def evaluate_result_multi(results: pd.DataFrame, timeframe: str, return df_final[df_final['open_trades'] > max_open_trades] -def trade_list_to_dataframe(trades: List[LocalTrade]) -> pd.DataFrame: +def trade_list_to_dataframe(trades: Union[List[Trade], List[LocalTrade]]) -> pd.DataFrame: """ Convert list of Trade objects to pandas Dataframe :param trades: List of trade objects @@ -372,7 +373,7 @@ def load_trades_from_db(db_url: str, strategy: Optional[str] = None) -> pd.DataF filters = [] if strategy: filters.append(Trade.strategy == strategy) - trades = trade_list_to_dataframe(Trade.get_trades(filters).all()) + trades = trade_list_to_dataframe(list(Trade.get_trades(filters).all())) return trades diff --git a/freqtrade/data/dataprovider.py b/freqtrade/data/dataprovider.py index df4a4c898..3991432a4 100644 --- a/freqtrade/data/dataprovider.py +++ b/freqtrade/data/dataprovider.py @@ -9,7 +9,7 @@ from collections import deque from datetime import datetime, timezone from typing import Any, Dict, List, Optional, Tuple -from pandas import DataFrame, to_timedelta +from pandas import DataFrame, Timedelta, Timestamp, to_timedelta from freqtrade.configuration import TimeRange from freqtrade.constants import (FULL_DATAFRAME_THRESHOLD, Config, ListPairsWithTimeframes, @@ -18,6 +18,7 @@ from freqtrade.data.history import load_pair_history from freqtrade.enums import CandleType, RPCMessageType, RunMode from freqtrade.exceptions import ExchangeError, OperationalException from freqtrade.exchange import Exchange, timeframe_to_seconds +from freqtrade.exchange.types import OrderBook from freqtrade.misc import append_candles_to_dataframe from freqtrade.rpc import RPCManager from freqtrade.util import PeriodicCache @@ -206,9 +207,11 @@ class DataProvider: existing_df, _ = self.__producer_pairs_df[producer_name][pair_key] # CHECK FOR MISSING CANDLES - timeframe_delta = to_timedelta(timeframe) # Convert the timeframe to a timedelta for pandas - local_last = existing_df.iloc[-1]['date'] # We want the last date from our copy - incoming_first = dataframe.iloc[0]['date'] # We want the first date from the incoming + # Convert the timeframe to a timedelta for pandas + timeframe_delta: Timedelta = to_timedelta(timeframe) + local_last: Timestamp = existing_df.iloc[-1]['date'] # We want the last date from our copy + # We want the first date from the incoming + incoming_first: Timestamp = dataframe.iloc[0]['date'] # Remove existing candles that are newer than the incoming first candle existing_df1 = existing_df[existing_df['date'] < incoming_first] @@ -221,7 +224,7 @@ class DataProvider: # we missed some candles between our data and the incoming # so return False and candle_difference. if candle_difference > 1: - return (False, candle_difference) + return (False, int(candle_difference)) if existing_df1.empty: appended_df = dataframe else: @@ -281,7 +284,7 @@ class DataProvider: def historic_ohlcv( self, pair: str, - timeframe: str = None, + timeframe: Optional[str] = None, candle_type: str = '' ) -> DataFrame: """ @@ -333,7 +336,7 @@ class DataProvider: def get_pair_dataframe( self, pair: str, - timeframe: str = None, + timeframe: Optional[str] = None, candle_type: str = '' ) -> DataFrame: """ @@ -415,16 +418,14 @@ class DataProvider: def refresh(self, pairlist: ListPairsWithTimeframes, - helping_pairs: ListPairsWithTimeframes = None) -> None: + helping_pairs: Optional[ListPairsWithTimeframes] = None) -> None: """ Refresh data, called with each cycle """ if self._exchange is None: raise OperationalException(NO_EXCHANGE_EXCEPTION) - if helping_pairs: - self._exchange.refresh_latest_ohlcv(pairlist + helping_pairs) - else: - self._exchange.refresh_latest_ohlcv(pairlist) + final_pairs = (pairlist + helping_pairs) if helping_pairs else pairlist + self._exchange.refresh_latest_ohlcv(final_pairs) @property def available_pairs(self) -> ListPairsWithTimeframes: @@ -439,7 +440,7 @@ class DataProvider: def ohlcv( self, pair: str, - timeframe: str = None, + timeframe: Optional[str] = None, copy: bool = True, candle_type: str = '' ) -> DataFrame: @@ -487,7 +488,7 @@ class DataProvider: except ExchangeError: return {} - def orderbook(self, pair: str, maximum: int) -> Dict[str, List]: + def orderbook(self, pair: str, maximum: int) -> OrderBook: """ Fetch latest l2 orderbook data Warning: Does a network request - so use with common sense. diff --git a/freqtrade/data/entryexitanalysis.py b/freqtrade/data/entryexitanalysis.py old mode 100755 new mode 100644 index b2679bcea..5d67655cd --- a/freqtrade/data/entryexitanalysis.py +++ b/freqtrade/data/entryexitanalysis.py @@ -24,9 +24,9 @@ def _load_signal_candles(backtest_dir: Path): scpf = Path(backtest_dir.parent / f"{backtest_dir.stem}_signals.pkl") try: - scp = open(scpf, "rb") - signal_candles = joblib.load(scp) - logger.info(f"Loaded signal candles: {str(scpf)}") + with scpf.open("rb") as scp: + signal_candles = joblib.load(scp) + logger.info(f"Loaded signal candles: {str(scpf)}") except Exception as e: logger.error("Cannot load signal candles from pickled results: ", e) diff --git a/freqtrade/data/history/history_utils.py b/freqtrade/data/history/history_utils.py index 9a206baa4..b567b58bf 100644 --- a/freqtrade/data/history/history_utils.py +++ b/freqtrade/data/history/history_utils.py @@ -28,8 +28,8 @@ def load_pair_history(pair: str, fill_up_missing: bool = True, drop_incomplete: bool = False, startup_candles: int = 0, - data_format: str = None, - data_handler: IDataHandler = None, + data_format: Optional[str] = None, + data_handler: Optional[IDataHandler] = None, candle_type: CandleType = CandleType.SPOT ) -> DataFrame: """ @@ -69,7 +69,7 @@ def load_data(datadir: Path, fail_without_data: bool = False, data_format: str = 'json', candle_type: CandleType = CandleType.SPOT, - user_futures_funding_rate: int = None, + user_futures_funding_rate: Optional[int] = None, ) -> Dict[str, DataFrame]: """ Load ohlcv history data for a list of pairs. @@ -116,7 +116,7 @@ def refresh_data(*, datadir: Path, timeframe: str, pairs: List[str], exchange: Exchange, - data_format: str = None, + data_format: Optional[str] = None, timerange: Optional[TimeRange] = None, candle_type: CandleType, ) -> None: @@ -189,7 +189,7 @@ def _download_pair_history(pair: str, *, timeframe: str = '5m', process: str = '', new_pairs_days: int = 30, - data_handler: IDataHandler = None, + data_handler: Optional[IDataHandler] = None, timerange: Optional[TimeRange] = None, candle_type: CandleType, erase: bool = False, @@ -272,7 +272,7 @@ def refresh_backtest_ohlcv_data(exchange: Exchange, pairs: List[str], timeframes datadir: Path, trading_mode: str, timerange: Optional[TimeRange] = None, new_pairs_days: int = 30, erase: bool = False, - data_format: str = None, + data_format: Optional[str] = None, prepend: bool = False, ) -> List[str]: """ diff --git a/freqtrade/data/history/idatahandler.py b/freqtrade/data/history/idatahandler.py index be265ca34..6637663ff 100644 --- a/freqtrade/data/history/idatahandler.py +++ b/freqtrade/data/history/idatahandler.py @@ -308,7 +308,7 @@ class IDataHandler(ABC): timerange=timerange_startup, candle_type=candle_type ) - if self._check_empty_df(pairdf, pair, timeframe, candle_type, warn_no_data, True): + if self._check_empty_df(pairdf, pair, timeframe, candle_type, warn_no_data): return pairdf else: enddate = pairdf.iloc[-1]['date'] @@ -316,7 +316,7 @@ class IDataHandler(ABC): if timerange_startup: self._validate_pairdata(pair, pairdf, timeframe, candle_type, timerange_startup) pairdf = trim_dataframe(pairdf, timerange_startup) - if self._check_empty_df(pairdf, pair, timeframe, candle_type, warn_no_data): + if self._check_empty_df(pairdf, pair, timeframe, candle_type, warn_no_data, True): return pairdf # incomplete candles should only be dropped if we didn't trim the end beforehand. @@ -418,8 +418,8 @@ def get_datahandlerclass(datatype: str) -> Type[IDataHandler]: raise ValueError(f"No datahandler for datatype {datatype} available.") -def get_datahandler(datadir: Path, data_format: str = None, - data_handler: IDataHandler = None) -> IDataHandler: +def get_datahandler(datadir: Path, data_format: Optional[str] = None, + data_handler: Optional[IDataHandler] = None) -> IDataHandler: """ :param datadir: Folder to save data :param data_format: dataformat to use diff --git a/freqtrade/edge/edge_positioning.py b/freqtrade/edge/edge_positioning.py index 4656b7c93..73820ecbe 100644 --- a/freqtrade/edge/edge_positioning.py +++ b/freqtrade/edge/edge_positioning.py @@ -195,7 +195,7 @@ class Edge: def stake_amount(self, pair: str, free_capital: float, total_capital: float, capital_in_trade: float) -> float: - stoploss = self.stoploss(pair) + stoploss = self.get_stoploss(pair) available_capital = (total_capital + capital_in_trade) * self._capital_ratio allowed_capital_at_risk = available_capital * self._allowed_risk max_position_size = abs(allowed_capital_at_risk / stoploss) @@ -214,7 +214,7 @@ class Edge: ) return round(position_size, 15) - def stoploss(self, pair: str) -> float: + def get_stoploss(self, pair: str) -> float: if pair in self._cached_pairs: return self._cached_pairs[pair].stoploss else: diff --git a/freqtrade/enums/__init__.py b/freqtrade/enums/__init__.py index eb70a2894..69ef345e8 100644 --- a/freqtrade/enums/__init__.py +++ b/freqtrade/enums/__init__.py @@ -5,7 +5,9 @@ from freqtrade.enums.exitchecktuple import ExitCheckTuple from freqtrade.enums.exittype import ExitType from freqtrade.enums.hyperoptstate import HyperoptState from freqtrade.enums.marginmode import MarginMode +from freqtrade.enums.marketstatetype import MarketDirection from freqtrade.enums.ordertypevalue import OrderTypeValues +from freqtrade.enums.pricetype import PriceType from freqtrade.enums.rpcmessagetype import NO_ECHO_MESSAGES, RPCMessageType, RPCRequestType from freqtrade.enums.runmode import NON_UTIL_MODES, OPTIMIZE_MODES, TRADING_MODES, RunMode from freqtrade.enums.signaltype import SignalDirection, SignalTagType, SignalType diff --git a/freqtrade/enums/candletype.py b/freqtrade/enums/candletype.py index 9d05ff6d7..dcb9f1448 100644 --- a/freqtrade/enums/candletype.py +++ b/freqtrade/enums/candletype.py @@ -13,6 +13,9 @@ class CandleType(str, Enum): FUNDING_RATE = "funding_rate" # BORROW_RATE = "borrow_rate" # * unimplemented + def __str__(self): + return f"{self.name.lower()}" + @staticmethod def from_string(value: str) -> 'CandleType': if not value: diff --git a/freqtrade/enums/marketstatetype.py b/freqtrade/enums/marketstatetype.py new file mode 100644 index 000000000..5cede32c2 --- /dev/null +++ b/freqtrade/enums/marketstatetype.py @@ -0,0 +1,15 @@ +from enum import Enum + + +class MarketDirection(Enum): + """ + Enum for various market directions. + """ + LONG = "long" + SHORT = "short" + EVEN = "even" + NONE = "none" + + def __str__(self): + # convert to string + return self.value diff --git a/freqtrade/enums/pricetype.py b/freqtrade/enums/pricetype.py new file mode 100644 index 000000000..bf0922b9f --- /dev/null +++ b/freqtrade/enums/pricetype.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class PriceType(str, Enum): + """Enum to distinguish possible trigger prices for stoplosses""" + LAST = "last" + MARK = "mark" + INDEX = "index" diff --git a/freqtrade/enums/rpcmessagetype.py b/freqtrade/enums/rpcmessagetype.py index 2453d16d9..16d81b1d8 100644 --- a/freqtrade/enums/rpcmessagetype.py +++ b/freqtrade/enums/rpcmessagetype.py @@ -4,6 +4,7 @@ from enum import Enum class RPCMessageType(str, Enum): STATUS = 'status' WARNING = 'warning' + EXCEPTION = 'exception' STARTUP = 'startup' ENTRY = 'entry' @@ -37,5 +38,8 @@ class RPCRequestType(str, Enum): WHITELIST = 'whitelist' ANALYZED_DF = 'analyzed_df' + def __str__(self): + return self.value + NO_ECHO_MESSAGES = (RPCMessageType.ANALYZED_DF, RPCMessageType.WHITELIST, RPCMessageType.NEW_CANDLE) diff --git a/freqtrade/enums/signaltype.py b/freqtrade/enums/signaltype.py index f706fd4dc..b5af1f1b2 100644 --- a/freqtrade/enums/signaltype.py +++ b/freqtrade/enums/signaltype.py @@ -10,6 +10,9 @@ class SignalType(Enum): ENTER_SHORT = "enter_short" EXIT_SHORT = "exit_short" + def __str__(self): + return f"{self.name.lower()}" + class SignalTagType(Enum): """ @@ -18,7 +21,13 @@ class SignalTagType(Enum): ENTER_TAG = "enter_tag" EXIT_TAG = "exit_tag" + def __str__(self): + return f"{self.name.lower()}" + class SignalDirection(str, Enum): LONG = 'long' SHORT = 'short' + + def __str__(self): + return f"{self.name.lower()}" diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 973ed499b..b815fb3ee 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -17,7 +17,7 @@ from freqtrade.exchange.exchange_utils import (amount_to_contract_precision, amo timeframe_to_next_date, timeframe_to_prev_date, timeframe_to_seconds, validate_exchange, validate_exchanges) -from freqtrade.exchange.gateio import Gateio +from freqtrade.exchange.gate import Gate from freqtrade.exchange.hitbtc import Hitbtc from freqtrade.exchange.huobi import Huobi from freqtrade.exchange.kraken import Kraken diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index d85b2fb28..a89c02631 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -7,7 +7,8 @@ from typing import Dict, List, Optional, Tuple import arrow import ccxt -from freqtrade.enums import CandleType, MarginMode, TradingMode +from freqtrade.constants import BuySell +from freqtrade.enums import CandleType, MarginMode, PriceType, TradingMode from freqtrade.exceptions import DDosProtection, OperationalException, TemporaryError from freqtrade.exchange import Exchange from freqtrade.exchange.common import retrier @@ -23,16 +24,22 @@ class Binance(Exchange): _ft_has: Dict = { "stoploss_on_exchange": True, "stoploss_order_types": {"limit": "stop_loss_limit"}, - "order_time_in_force": ['GTC', 'FOK', 'IOC'], + "order_time_in_force": ["GTC", "FOK", "IOC", "PO"], "ohlcv_candle_limit": 1000, "trades_pagination": "id", "trades_pagination_arg": "fromId", "l2_limit_range": [5, 10, 20, 50, 100, 500, 1000], - "ccxt_futures_name": "swap" } _ft_has_futures: Dict = { "stoploss_order_types": {"limit": "stop", "market": "stop_market"}, + "order_time_in_force": ["GTC", "FOK", "IOC"], "tickers_have_price": False, + "floor_leverage": True, + "stop_price_type_field": "workingType", + "stop_price_type_value_mapping": { + PriceType.LAST: "CONTRACT_PRICE", + PriceType.MARK: "MARK_PRICE", + }, } _supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [ @@ -42,6 +49,26 @@ class Binance(Exchange): (TradingMode.FUTURES, MarginMode.ISOLATED) ] + def _get_params( + self, + side: BuySell, + ordertype: str, + leverage: float, + reduceOnly: bool, + time_in_force: str = 'GTC', + ) -> Dict: + params = super()._get_params(side, ordertype, leverage, reduceOnly, time_in_force) + if ( + time_in_force == 'PO' + and ordertype != 'market' + and self.trading_mode == TradingMode.SPOT + # Only spot can do post only orders + ): + params.pop('timeInForce') + params['postOnly'] = True + + return params + def get_tickers(self, symbols: Optional[List[str]] = None, cached: bool = False) -> Tickers: tickers = super().get_tickers(symbols=symbols, cached=cached) if self.trading_mode == TradingMode.FUTURES: @@ -78,33 +105,9 @@ class Binance(Exchange): raise DDosProtection(e) from e except (ccxt.NetworkError, ccxt.ExchangeError) as e: raise TemporaryError( - f'Could not set leverage due to {e.__class__.__name__}. Message: {e}') from e - except ccxt.BaseError as e: - raise OperationalException(e) from e + f'Error in additional_exchange_init due to {e.__class__.__name__}. Message: {e}' + ) from e - @retrier - def _set_leverage( - self, - leverage: float, - pair: Optional[str] = None, - trading_mode: Optional[TradingMode] = None - ): - """ - Set's the leverage before making a trade, in order to not - have the same leverage on every trade - """ - trading_mode = trading_mode or self.trading_mode - - if self._config['dry_run'] or trading_mode != TradingMode.FUTURES: - return - - try: - self._api.set_leverage(symbol=pair, leverage=round(leverage)) - except ccxt.DDoSProtection as e: - raise DDosProtection(e) from e - except (ccxt.NetworkError, ccxt.ExchangeError) as e: - raise TemporaryError( - f'Could not set leverage due to {e.__class__.__name__}. Message: {e}') from e except ccxt.BaseError as e: raise OperationalException(e) from e @@ -150,6 +153,7 @@ class Binance(Exchange): is_short: bool, amount: float, stake_amount: float, + leverage: float, wallet_balance: float, # Or margin balance mm_ex_1: float = 0.0, # (Binance) Cross only upnl_ex_1: float = 0.0, # (Binance) Cross only @@ -159,11 +163,12 @@ class Binance(Exchange): MARGIN: https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed PERPETUAL: https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 - :param exchange_name: + :param pair: Pair to calculate liquidation price for :param open_rate: Entry price of position :param is_short: True if the trade is a short, false otherwise :param amount: Absolute value of position size incl. leverage (in base currency) :param stake_amount: Stake amount - Collateral in settle currency. + :param leverage: Leverage used for this position. :param trading_mode: SPOT, MARGIN, FUTURES, etc. :param margin_mode: Either ISOLATED or CROSS :param wallet_balance: Amount of margin_mode in the wallet being used to trade @@ -212,7 +217,7 @@ class Binance(Exchange): leverage_tiers_path = ( Path(__file__).parent / 'binance_leverage_tiers.json' ) - with open(leverage_tiers_path) as json_file: + with leverage_tiers_path.open() as json_file: return json_load(json_file) else: try: diff --git a/freqtrade/exchange/binance_leverage_tiers.json b/freqtrade/exchange/binance_leverage_tiers.json index 908088cda..07fdcb5a4 100644 --- a/freqtrade/exchange/binance_leverage_tiers.json +++ b/freqtrade/exchange/binance_leverage_tiers.json @@ -104,10 +104,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.01", @@ -120,10 +120,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -134,13 +134,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "400000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "700.0" @@ -149,49 +149,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 400000.0, + "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "1000000", + "notionalFloor": "400000", "maintMarginRatio": "0.1", - "cum": "5700.0" + "cum": "20700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, + "minNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.125", - "cum": "11950.0" + "cum": "45700.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295700.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "1795700.0" } } ], @@ -202,10 +218,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -218,10 +234,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -234,10 +250,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -264,13 +280,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -279,17 +295,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -298,112 +330,144 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "25", - "notionalCap": "50000", + "initialLeverage": "50", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.0065", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.0075, + "maxLeverage": 40.0, + "info": { + "bracket": "2", + "initialLeverage": "40", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0075", + "cum": "5.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "3", + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "25000", + "maintMarginRatio": "0.01", + "cum": "67.5" + } + }, + { + "tier": 4.0, + "currency": "USDT", "minNotional": 50000.0, "maxNotional": 150000.0, "maintenanceMarginRate": 0.025, "maxLeverage": 20.0, "info": { - "bracket": "2", + "bracket": "4", "initialLeverage": "20", "notionalCap": "150000", "notionalFloor": "50000", "maintMarginRatio": "0.025", - "cum": "750.0" + "cum": "817.5" } }, { - "tier": 3.0, + "tier": 5.0, "currency": "USDT", "minNotional": 150000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "3", + "bracket": "5", "initialLeverage": "10", "notionalCap": "250000", "notionalFloor": "150000", "maintMarginRatio": "0.05", - "cum": "4500.0" + "cum": "4567.5" } }, { - "tier": 4.0, + "tier": 6.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "4", + "bracket": "6", "initialLeverage": "5", "notionalCap": "500000", "notionalFloor": "250000", "maintMarginRatio": "0.1", - "cum": "17000.0" + "cum": "17067.5" } }, { - "tier": 5.0, + "tier": 7.0, "currency": "USDT", "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "5", + "bracket": "7", "initialLeverage": "4", "notionalCap": "1000000", "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "29500.0" + "cum": "29567.5" } }, { - "tier": 6.0, + "tier": 8.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "2", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.25", - "cum": "154500.0" + "cum": "154567.5" } }, { - "tier": 7.0, + "tier": 9.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 30000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "9", "initialLeverage": "1", "notionalCap": "30000000", "notionalFloor": "2000000", "maintMarginRatio": "0.5", - "cum": "654500.0" + "cum": "654567.5" } } ], @@ -610,10 +674,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.01", @@ -626,10 +690,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -640,18 +704,132 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "400000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "700.0" } }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 400000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "1000000", + "notionalFloor": "400000", + "maintMarginRatio": "0.1", + "cum": "20700.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.125", + "cum": "45700.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295700.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "10000000", + "notionalFloor": "6000000", + "maintMarginRatio": "0.5", + "cum": "1795700.0" + } + } + ], + "ACH/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, { "tier": 4.0, "currency": "USDT", @@ -665,7 +843,7 @@ "notionalCap": "250000", "notionalFloor": "100000", "maintMarginRatio": "0.1", - "cum": "5700.0" + "cum": "5650.0" } }, { @@ -681,7 +859,7 @@ "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.125", - "cum": "11950.0" + "cum": "11900.0" } }, { @@ -697,7 +875,7 @@ "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "386900.0" } } ], @@ -804,144 +982,372 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 10000.0, - "maintenanceMarginRate": 0.0065, - "maxLeverage": 50.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", - "notionalCap": "10000", + "initialLeverage": "75", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.0065", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, + "info": { + "bracket": "2", + "initialLeverage": "50", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" + } + }, + { + "tier": 3.0, + "currency": "USDT", "minNotional": 10000.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 40.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "40", "notionalCap": "50000", "notionalFloor": "10000", "maintMarginRatio": "0.01", - "cum": "35.0" + "cum": "42.5" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 25.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "25", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.02", - "cum": "535.0" + "cum": "542.5" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "8042.5" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "58042.5" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "108042.5" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "233042.5" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1233042.5" } }, { - "tier": 9.0, + "tier": 10.0, "currency": "USDT", "minNotional": 20000000.0, "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "9", + "bracket": "10", "initialLeverage": "1", "notionalCap": "50000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6233042.5" + } + } + ], + "AGIX/BUSD:BUSD": [ + { + "tier": 1.0, + "currency": "BUSD", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 15.0, + "info": { + "bracket": "1", + "initialLeverage": "15", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "BUSD", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 10.0, + "info": { + "bracket": "2", + "initialLeverage": "10", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "BUSD", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 8.0, + "info": { + "bracket": "3", + "initialLeverage": "8", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "BUSD", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" + } + }, + { + "tier": 5.0, + "currency": "BUSD", + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" + } + }, + { + "tier": 6.0, + "currency": "BUSD", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "386900.0" + } + } + ], + "AGIX/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "200000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 200000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "200000", + "maintMarginRatio": "0.1", + "cum": "10650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "23150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "898150.0" } } ], @@ -1148,10 +1554,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 21.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "21", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -1178,13 +1584,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "100000", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -1193,23 +1599,23 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 2.0, @@ -1217,25 +1623,25 @@ "bracket": "5", "initialLeverage": "2", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "6", "initialLeverage": "1", - "notionalCap": "3000000", + "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "398150.0" } } ], @@ -1441,14 +1847,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, - "maintenanceMarginRate": 0.012, - "maxLeverage": 20.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.012", + "maintMarginRatio": "0.01", "cum": "0.0" } }, @@ -1458,78 +1864,94 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", - "cum": "65.0" + "cum": "75.0" } }, { "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "400000", "notionalFloor": "25000", "maintMarginRatio": "0.05", - "cum": "690.0" + "cum": "700.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 400000.0, + "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "1000000", + "notionalFloor": "400000", "maintMarginRatio": "0.1", - "cum": "5690.0" + "cum": "20700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, + "minNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.125", - "cum": "11940.0" + "cum": "45700.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295700.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386940.0" + "cum": "1795700.0" } } ], @@ -1734,112 +2156,144 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "25", - "notionalCap": "50000", + "initialLeverage": "50", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.0065", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.0075, + "maxLeverage": 40.0, + "info": { + "bracket": "2", + "initialLeverage": "40", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0075", + "cum": "5.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "3", + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "25000", + "maintMarginRatio": "0.01", + "cum": "67.5" + } + }, + { + "tier": 4.0, + "currency": "USDT", "minNotional": 50000.0, "maxNotional": 150000.0, "maintenanceMarginRate": 0.025, "maxLeverage": 20.0, "info": { - "bracket": "2", + "bracket": "4", "initialLeverage": "20", "notionalCap": "150000", "notionalFloor": "50000", "maintMarginRatio": "0.025", - "cum": "750.0" + "cum": "817.5" } }, { - "tier": 3.0, + "tier": 5.0, "currency": "USDT", "minNotional": 150000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "3", + "bracket": "5", "initialLeverage": "10", "notionalCap": "250000", "notionalFloor": "150000", "maintMarginRatio": "0.05", - "cum": "4500.0" + "cum": "4567.5" } }, { - "tier": 4.0, + "tier": 6.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "4", + "bracket": "6", "initialLeverage": "5", "notionalCap": "500000", "notionalFloor": "250000", "maintMarginRatio": "0.1", - "cum": "17000.0" + "cum": "17067.5" } }, { - "tier": 5.0, + "tier": 7.0, "currency": "USDT", "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "5", + "bracket": "7", "initialLeverage": "4", "notionalCap": "1000000", "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "29500.0" + "cum": "29567.5" } }, { - "tier": 6.0, + "tier": 8.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 2000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "2", - "notionalCap": "2000000", + "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.25", - "cum": "154500.0" + "cum": "154567.5" } }, { - "tier": 7.0, + "tier": 9.0, "currency": "USDT", - "minNotional": 2000000.0, - "maxNotional": 5000000.0, + "minNotional": 5000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "9", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "2000000", + "notionalCap": "10000000", + "notionalFloor": "5000000", "maintMarginRatio": "0.5", - "cum": "654500.0" + "cum": "1404567.5" } } ], @@ -1948,10 +2402,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -1964,10 +2418,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -1980,10 +2434,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -2010,13 +2464,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -2025,17 +2479,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -2044,112 +2514,128 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 150000.0, - "maintenanceMarginRate": 0.02, - "maxLeverage": 21.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "21", - "notionalCap": "150000", + "initialLeverage": "50", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.02", + "maintMarginRatio": "0.01", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", - "minNotional": 150000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 20.0, + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.015, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "20", - "notionalCap": "250000", - "notionalFloor": "150000", - "maintMarginRatio": "0.025", - "cum": "750.0" + "initialLeverage": "25", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.015", + "cum": "25.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 10.0, + "minNotional": 25000.0, + "maxNotional": 300000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "10", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.05", - "cum": "7000.0" + "initialLeverage": "20", + "notionalCap": "300000", + "notionalFloor": "25000", + "maintMarginRatio": "0.02", + "cum": "150.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 2000000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 300000.0, + "maxNotional": 1200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "2000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.1", - "cum": "57000.0" + "initialLeverage": "10", + "notionalCap": "1200000", + "notionalFloor": "300000", + "maintMarginRatio": "0.05", + "cum": "9150.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 2000000.0, - "maxNotional": 5000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 4.0, + "minNotional": 1200000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "4", - "notionalCap": "5000000", - "notionalFloor": "2000000", - "maintMarginRatio": "0.125", - "cum": "107000.0" + "initialLeverage": "5", + "notionalCap": "3000000", + "notionalFloor": "1200000", + "maintMarginRatio": "0.1", + "cum": "69150.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 5000000.0, - "maxNotional": 10000000.0, - "maintenanceMarginRate": 0.25, - "maxLeverage": 2.0, + "minNotional": 3000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, "info": { "bracket": "6", - "initialLeverage": "2", - "notionalCap": "10000000", - "notionalFloor": "5000000", - "maintMarginRatio": "0.25", - "cum": "732000.0" + "initialLeverage": "4", + "notionalCap": "6000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.125", + "cum": "144150.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 10000000.0, - "maxNotional": 11000000.0, + "minNotional": 6000000.0, + "maxNotional": 18000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "18000000", + "notionalFloor": "6000000", + "maintMarginRatio": "0.25", + "cum": "894150.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 18000000.0, + "maxNotional": 30000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "11000000", - "notionalFloor": "10000000", + "notionalCap": "30000000", + "notionalFloor": "18000000", "maintMarginRatio": "0.5", - "cum": "3232000.0" + "cum": "5394150.0" } } ], @@ -2349,6 +2835,104 @@ } } ], + "ASTR/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "386900.0" + } + } + ], "ATA/USDT:USDT": [ { "tier": 1.0, @@ -2453,14 +3037,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "50", "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, @@ -2468,80 +3052,112 @@ "tier": 2.0, "currency": "USDT", "minNotional": 5000.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "10", - "notionalCap": "25000", + "initialLeverage": "25", + "notionalCap": "50000", "notionalFloor": "5000", - "maintMarginRatio": "0.025", - "cum": "75.0" + "maintMarginRatio": "0.01", + "cum": "20.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "minNotional": 50000.0, + "maxNotional": 600000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "700.0" + "initialLeverage": "20", + "notionalCap": "600000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "770.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 600000.0, + "maxNotional": 1200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5700.0" + "initialLeverage": "10", + "notionalCap": "1200000", + "notionalFloor": "600000", + "maintMarginRatio": "0.05", + "cum": "15770.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "minNotional": 1200000.0, + "maxNotional": 3200000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11950.0" + "initialLeverage": "5", + "notionalCap": "3200000", + "notionalFloor": "1200000", + "maintMarginRatio": "0.1", + "cum": "75770.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, + "minNotional": 3200000.0, "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "3200000", + "maintMarginRatio": "0.125", + "cum": "155770.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "780770.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "20000000", + "notionalFloor": "12000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "3780770.0" } } ], @@ -2748,10 +3364,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -2764,10 +3380,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -2780,10 +3396,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -2810,13 +3426,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -2825,17 +3441,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -2844,112 +3476,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "25", - "notionalCap": "50000", + "initialLeverage": "50", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 40.0, + "info": { + "bracket": "2", + "initialLeverage": "40", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 10000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.0075, + "maxLeverage": 30.0, + "info": { + "bracket": "3", + "initialLeverage": "30", + "notionalCap": "25000", + "notionalFloor": "10000", + "maintMarginRatio": "0.0075", + "cum": "17.5" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "4", + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "25000", + "maintMarginRatio": "0.01", + "cum": "80.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.025, "maxLeverage": 20.0, "info": { - "bracket": "2", + "bracket": "5", "initialLeverage": "20", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.025", - "cum": "750.0" + "cum": "830.0" } }, { - "tier": 3.0, + "tier": 6.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "3", + "bracket": "6", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "7000.0" + "cum": "7080.0" } }, { - "tier": 4.0, + "tier": 7.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "4", + "bracket": "7", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "57000.0" + "cum": "57080.0" } }, { - "tier": 5.0, + "tier": 8.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "5", + "bracket": "8", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "107000.0" + "cum": "107080.0" } }, { - "tier": 6.0, + "tier": 9.0, "currency": "USDT", "minNotional": 5000000.0, - "maxNotional": 10000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "6", + "bracket": "9", "initialLeverage": "2", - "notionalCap": "10000000", + "notionalCap": "20000000", "notionalFloor": "5000000", "maintMarginRatio": "0.25", - "cum": "732000.0" + "cum": "732080.0" } }, { - "tier": 7.0, + "tier": 10.0, "currency": "USDT", - "minNotional": 10000000.0, - "maxNotional": 20000000.0, + "minNotional": 20000000.0, + "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "10", "initialLeverage": "1", - "notionalCap": "20000000", - "notionalFloor": "10000000", + "notionalCap": "50000000", + "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "3232000.0" + "cum": "5732080.0" } } ], @@ -2958,128 +3638,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.0065, "maxLeverage": 25.0, "info": { "bracket": "1", "initialLeverage": "25", - "notionalCap": "50000", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.0065", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", - "minNotional": 50000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.02, + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.0075, "maxLeverage": 20.0, "info": { "bracket": "2", "initialLeverage": "20", - "notionalCap": "250000", - "notionalFloor": "50000", - "maintMarginRatio": "0.02", - "cum": "500.0" + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0075", + "cum": "5.0" } }, { "tier": 3.0, "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 18.0, + "info": { + "bracket": "3", + "initialLeverage": "18", + "notionalCap": "50000", + "notionalFloor": "25000", + "maintMarginRatio": "0.01", + "cum": "67.5" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 50000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 15.0, + "info": { + "bracket": "4", + "initialLeverage": "15", + "notionalCap": "250000", + "notionalFloor": "50000", + "maintMarginRatio": "0.02", + "cum": "567.5" + } + }, + { + "tier": 5.0, + "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "3", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8000.0" + "cum": "8067.5" } }, { - "tier": 4.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "4", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58000.0" + "cum": "58067.5" } }, { - "tier": 5.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "5", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108000.0" + "cum": "108067.5" } }, { - "tier": 6.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.1665, "maxLeverage": 3.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.1665", - "cum": "315500.0" + "cum": "315567.5" } }, { - "tier": 7.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 15000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "7", + "bracket": "9", "initialLeverage": "2", "notionalCap": "15000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1150500.0" + "cum": "1150567.5" } }, { - "tier": 8.0, + "tier": 10.0, "currency": "USDT", "minNotional": 15000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "8", + "bracket": "10", "initialLeverage": "1", "notionalCap": "20000000", "notionalFloor": "15000000", "maintMarginRatio": "0.5", - "cum": "4900500.0" + "cum": "4900567.5" } } ], @@ -3480,144 +4192,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 10000.0, - "maintenanceMarginRate": 0.0065, - "maxLeverage": 50.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", - "notionalCap": "10000", + "initialLeverage": "75", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.0065", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, + "info": { + "bracket": "2", + "initialLeverage": "50", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" + } + }, + { + "tier": 3.0, + "currency": "USDT", "minNotional": 10000.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 40.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "40", "notionalCap": "50000", "notionalFloor": "10000", "maintMarginRatio": "0.01", - "cum": "35.0" + "cum": "42.5" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 25.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "25", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.02", - "cum": "535.0" + "cum": "542.5" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "8042.5" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "58042.5" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "108042.5" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "233042.5" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1233042.5" } }, { - "tier": 9.0, + "tier": 10.0, "currency": "USDT", "minNotional": 20000000.0, "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "9", + "bracket": "10", "initialLeverage": "1", "notionalCap": "50000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6233042.5" } } ], @@ -4018,144 +4746,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 10000.0, - "maintenanceMarginRate": 0.0065, - "maxLeverage": 50.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", - "notionalCap": "10000", + "initialLeverage": "75", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.0065", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, + "info": { + "bracket": "2", + "initialLeverage": "50", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" + } + }, + { + "tier": 3.0, + "currency": "USDT", "minNotional": 10000.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 40.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "40", "notionalCap": "50000", "notionalFloor": "10000", "maintMarginRatio": "0.01", - "cum": "35.0" + "cum": "42.5" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 25.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "25", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.02", - "cum": "535.0" + "cum": "542.5" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "8042.5" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "58042.5" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "108042.5" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "233042.5" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1233042.5" } }, { - "tier": 9.0, + "tier": 10.0, "currency": "USDT", "minNotional": 20000000.0, "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "9", + "bracket": "10", "initialLeverage": "1", "notionalCap": "50000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6233042.5" } } ], @@ -4164,96 +4908,96 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 8.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, "info": { "bracket": "1", - "initialLeverage": "8", - "notionalCap": "25000", + "initialLeverage": "20", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.025", + "maintMarginRatio": "0.02", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 6.0, + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "6", - "notionalCap": "250000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "625.0" + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 500000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "5", - "notionalCap": "500000", - "notionalFloor": "250000", - "maintMarginRatio": "0.1", - "cum": "13125.0" + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 500000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 4.0, + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "4", - "initialLeverage": "4", - "notionalCap": "1000000", - "notionalFloor": "500000", - "maintMarginRatio": "0.125", - "cum": "25625.0" + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 1500000.0, - "maintenanceMarginRate": 0.25, + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, "maxLeverage": 2.0, "info": { "bracket": "5", "initialLeverage": "2", - "notionalCap": "1500000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.25", - "cum": "150625.0" + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1500000.0, - "maxNotional": 2000000.0, + "minNotional": 1000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "6", "initialLeverage": "1", - "notionalCap": "2000000", - "notionalFloor": "1500000", + "notionalCap": "5000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.5", - "cum": "525625.0" + "cum": "386900.0" } } ], @@ -4264,10 +5008,10 @@ "minNotional": 0.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.004, - "maxLeverage": 50.0, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", + "initialLeverage": "75", "notionalCap": "50000", "notionalFloor": "0", "maintMarginRatio": "0.004", @@ -4280,10 +5024,10 @@ "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.005, - "maxLeverage": 25.0, + "maxLeverage": 50.0, "info": { "bracket": "2", - "initialLeverage": "25", + "initialLeverage": "50", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.005", @@ -4296,10 +5040,10 @@ "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "3", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.01", @@ -4312,10 +5056,10 @@ "minNotional": 1000000.0, "maxNotional": 7500000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "4", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "7500000", "notionalFloor": "1000000", "maintMarginRatio": "0.025", @@ -4328,10 +5072,10 @@ "minNotional": 7500000.0, "maxNotional": 40000000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 6.0, + "maxLeverage": 10.0, "info": { "bracket": "5", - "initialLeverage": "6", + "initialLeverage": "10", "notionalCap": "40000000", "notionalFloor": "7500000", "maintMarginRatio": "0.05", @@ -4456,13 +5200,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 3000000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maxLeverage": 50.0, "info": { "bracket": "3", - "initialLeverage": "25", - "notionalCap": "1000000", + "initialLeverage": "50", + "notionalCap": "3000000", "notionalFloor": "250000", "maintMarginRatio": "0.01", "cum": "1300.0" @@ -4471,55 +5215,55 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 10000000.0, + "minNotional": 3000000.0, + "maxNotional": 15000000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 15.0, + "maxLeverage": 20.0, "info": { "bracket": "4", - "initialLeverage": "15", - "notionalCap": "10000000", - "notionalFloor": "1000000", + "initialLeverage": "20", + "notionalCap": "15000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.025", - "cum": "16300.0" + "cum": "46300.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 10000000.0, - "maxNotional": 20000000.0, + "minNotional": 15000000.0, + "maxNotional": 30000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "5", "initialLeverage": "10", - "notionalCap": "20000000", - "notionalFloor": "10000000", + "notionalCap": "30000000", + "notionalFloor": "15000000", "maintMarginRatio": "0.05", - "cum": "266300.0" + "cum": "421300.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 20000000.0, - "maxNotional": 50000000.0, + "minNotional": 30000000.0, + "maxNotional": 80000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "6", "initialLeverage": "5", - "notionalCap": "50000000", - "notionalFloor": "20000000", + "notionalCap": "80000000", + "notionalFloor": "30000000", "maintMarginRatio": "0.1", - "cum": "1266300.0" + "cum": "1921300.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 50000000.0, + "minNotional": 80000000.0, "maxNotional": 100000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, @@ -4527,9 +5271,9 @@ "bracket": "7", "initialLeverage": "4", "notionalCap": "100000000", - "notionalFloor": "50000000", + "notionalFloor": "80000000", "maintMarginRatio": "0.125", - "cum": "2516300.0" + "cum": "3921300.0" } }, { @@ -4545,7 +5289,7 @@ "notionalCap": "200000000", "notionalFloor": "100000000", "maintMarginRatio": "0.15", - "cum": "5016300.0" + "cum": "6421300.0" } }, { @@ -4561,7 +5305,7 @@ "notionalCap": "300000000", "notionalFloor": "200000000", "maintMarginRatio": "0.25", - "cum": "2.50163E7" + "cum": "2.64213E7" } }, { @@ -4577,7 +5321,7 @@ "notionalCap": "500000000", "notionalFloor": "300000000", "maintMarginRatio": "0.5", - "cum": "1.000163E8" + "cum": "1.014213E8" } } ], @@ -5283,6 +6027,136 @@ } } ], + "CFX/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 50.0, + "info": { + "bracket": "1", + "initialLeverage": "50", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.01", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.015, + "maxLeverage": 25.0, + "info": { + "bracket": "2", + "initialLeverage": "25", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.015", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 300000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "3", + "initialLeverage": "20", + "notionalCap": "300000", + "notionalFloor": "25000", + "maintMarginRatio": "0.02", + "cum": "150.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 300000.0, + "maxNotional": 1200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "4", + "initialLeverage": "10", + "notionalCap": "1200000", + "notionalFloor": "300000", + "maintMarginRatio": "0.05", + "cum": "9150.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 1200000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "5", + "initialLeverage": "5", + "notionalCap": "3000000", + "notionalFloor": "1200000", + "maintMarginRatio": "0.1", + "cum": "69150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "6000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.125", + "cum": "144150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 18000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "18000000", + "notionalFloor": "6000000", + "maintMarginRatio": "0.25", + "cum": "894150.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 18000000.0, + "maxNotional": 30000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "8", + "initialLeverage": "1", + "notionalCap": "30000000", + "notionalFloor": "18000000", + "maintMarginRatio": "0.5", + "cum": "5394150.0" + } + } + ], "CHR/USDT:USDT": [ { "tier": 1.0, @@ -5290,10 +6164,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 21.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "21", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -5320,13 +6194,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "100000", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -5335,23 +6209,23 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 2.0, @@ -5359,25 +6233,25 @@ "bracket": "5", "initialLeverage": "2", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "6", "initialLeverage": "1", - "notionalCap": "3000000", + "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "398150.0" } } ], @@ -5479,6 +6353,218 @@ } } ], + "CKB/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "200000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 200000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "200000", + "maintMarginRatio": "0.1", + "cum": "10650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "23150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "898150.0" + } + } + ], + "COCOS/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "386900.0" + } + } + ], "COMP/USDT:USDT": [ { "tier": 1.0, @@ -5584,10 +6670,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 21.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "21", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -5614,13 +6700,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "100000", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -5629,23 +6715,23 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 2.0, @@ -5653,25 +6739,25 @@ "bracket": "5", "initialLeverage": "2", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "6", "initialLeverage": "1", - "notionalCap": "3000000", + "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "398150.0" } } ], @@ -5682,10 +6768,10 @@ "minNotional": 0.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 21.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "21", + "initialLeverage": "25", "notionalCap": "50000", "notionalFloor": "0", "maintMarginRatio": "0.01", @@ -5760,13 +6846,13 @@ "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 2000000.0, + "maxNotional": 3000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { "bracket": "6", "initialLeverage": "2", - "notionalCap": "2000000", + "notionalCap": "3000000", "notionalFloor": "1000000", "maintMarginRatio": "0.25", "cum": "154500.0" @@ -5775,17 +6861,17 @@ { "tier": 7.0, "currency": "USDT", - "minNotional": 2000000.0, - "maxNotional": 3000000.0, + "minNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "7", "initialLeverage": "1", - "notionalCap": "3000000", - "notionalFloor": "2000000", + "notionalCap": "5000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "654500.0" + "cum": "904500.0" } } ], @@ -6254,10 +7340,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "1", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -6270,10 +7356,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "2", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -6286,10 +7372,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 6.0, + "maxLeverage": 8.0, "info": { "bracket": "3", - "initialLeverage": "6", + "initialLeverage": "8", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -6332,13 +7418,13 @@ "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 2000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "6", "initialLeverage": "1", - "notionalCap": "2000000", + "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.5", "cum": "386900.0" @@ -6548,10 +7634,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "1", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -6564,10 +7650,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "2", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -6578,13 +7664,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 6.0, + "maxLeverage": 8.0, "info": { "bracket": "3", - "initialLeverage": "6", - "notionalCap": "100000", + "initialLeverage": "8", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -6593,23 +7679,23 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 2.0, @@ -6617,9 +7703,9 @@ "bracket": "5", "initialLeverage": "2", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { @@ -6635,7 +7721,7 @@ "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "398150.0" } } ], @@ -6938,112 +8024,144 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "25", - "notionalCap": "50000", + "initialLeverage": "50", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.0065", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.0075, + "maxLeverage": 40.0, + "info": { + "bracket": "2", + "initialLeverage": "40", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0075", + "cum": "5.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "3", + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "25000", + "maintMarginRatio": "0.01", + "cum": "67.5" + } + }, + { + "tier": 4.0, + "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.025, "maxLeverage": 20.0, "info": { - "bracket": "2", + "bracket": "4", "initialLeverage": "20", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.025", - "cum": "750.0" + "cum": "817.5" } }, { - "tier": 3.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "3", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "7000.0" + "cum": "7067.5" } }, { - "tier": 4.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "4", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "57000.0" + "cum": "57067.5" } }, { - "tier": 5.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "5", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "107000.0" + "cum": "107067.5" } }, { - "tier": 6.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "2", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.25", - "cum": "732000.0" + "cum": "732067.5" } }, { - "tier": 7.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "9", "initialLeverage": "1", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.5", - "cum": "3232000.0" + "cum": "3232067.5" } } ], @@ -7054,10 +8172,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -7070,10 +8188,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -7086,10 +8204,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -7116,13 +8234,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -7131,17 +8249,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -7298,10 +8432,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -7314,10 +8448,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -7328,13 +8462,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -7343,105 +8477,7 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, - "info": { - "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5650.0" - } - }, - { - "tier": 5.0, - "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, - "info": { - "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11900.0" - } - }, - { - "tier": 6.0, - "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 3000000.0, - "maintenanceMarginRate": 0.5, - "maxLeverage": 1.0, - "info": { - "bracket": "6", - "initialLeverage": "1", - "notionalCap": "3000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.5", - "cum": "386900.0" - } - } - ], - "DYDX/USDT:USDT": [ - { - "tier": 1.0, - "currency": "USDT", - "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, - "info": { - "bracket": "1", - "initialLeverage": "20", - "notionalCap": "50000", - "notionalFloor": "0", - "maintMarginRatio": "0.02", - "cum": "0.0" - } - }, - { - "tier": 2.0, - "currency": "USDT", - "minNotional": 50000.0, - "maxNotional": 150000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, - "info": { - "bracket": "2", - "initialLeverage": "10", - "notionalCap": "150000", - "notionalFloor": "50000", - "maintMarginRatio": "0.025", - "cum": "250.0" - } - }, - { - "tier": 3.0, - "currency": "USDT", - "minNotional": 150000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, - "info": { - "bracket": "3", - "initialLeverage": "8", - "notionalCap": "250000", - "notionalFloor": "150000", - "maintMarginRatio": "0.05", - "cum": "4000.0" - } - }, - { - "tier": 4.0, - "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 200000.0, "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, @@ -7449,9 +8485,9 @@ "bracket": "4", "initialLeverage": "5", "notionalCap": "500000", - "notionalFloor": "250000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "16500.0" + "cum": "10650.0" } }, { @@ -7467,29 +8503,29 @@ "notionalCap": "1000000", "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "29000.0" + "cum": "23150.0" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 4000000.0, + "maxNotional": 3000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { "bracket": "6", "initialLeverage": "2", - "notionalCap": "4000000", + "notionalCap": "3000000", "notionalFloor": "1000000", "maintMarginRatio": "0.25", - "cum": "154000.0" + "cum": "148150.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 4000000.0, + "minNotional": 3000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, @@ -7497,9 +8533,139 @@ "bracket": "7", "initialLeverage": "1", "notionalCap": "5000000", - "notionalFloor": "4000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "1154000.0" + "cum": "898150.0" + } + } + ], + "DYDX/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 50.0, + "info": { + "bracket": "1", + "initialLeverage": "50", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.01", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "2", + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "5000", + "maintMarginRatio": "0.02", + "cum": "50.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 50000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "3", + "initialLeverage": "20", + "notionalCap": "400000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "300.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 400000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "4", + "initialLeverage": "10", + "notionalCap": "800000", + "notionalFloor": "400000", + "maintMarginRatio": "0.05", + "cum": "10300.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 800000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "5", + "initialLeverage": "5", + "notionalCap": "2000000", + "notionalFloor": "800000", + "maintMarginRatio": "0.1", + "cum": "50300.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 2000000.0, + "maxNotional": 4000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "4000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.125", + "cum": "100300.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 4000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "4000000", + "maintMarginRatio": "0.25", + "cum": "600300.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "8", + "initialLeverage": "1", + "notionalCap": "20000000", + "notionalFloor": "12000000", + "maintMarginRatio": "0.5", + "cum": "3600300.0" } } ], @@ -7608,10 +8774,10 @@ "minNotional": 0.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "50000", "notionalFloor": "0", "maintMarginRatio": "0.01", @@ -7622,96 +8788,112 @@ "tier": 2.0, "currency": "USDT", "minNotional": 50000.0, - "maxNotional": 150000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", - "notionalCap": "150000", + "initialLeverage": "20", + "notionalCap": "100000", "notionalFloor": "50000", - "maintMarginRatio": "0.025", - "cum": "750.0" + "maintMarginRatio": "0.02", + "cum": "500.0" } }, { "tier": 3.0, "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 150000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "150000", + "notionalFloor": "100000", + "maintMarginRatio": "0.025", + "cum": "1000.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", "minNotional": 150000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 8.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "8", "notionalCap": "250000", "notionalFloor": "150000", "maintMarginRatio": "0.05", - "cum": "4500.0" + "cum": "4750.0" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "5", "notionalCap": "500000", "notionalFloor": "250000", "maintMarginRatio": "0.1", - "cum": "17000.0" + "cum": "17250.0" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "4", "notionalCap": "1000000", "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "29500.0" - } - }, - { - "tier": 6.0, - "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 2000000.0, - "maintenanceMarginRate": 0.25, - "maxLeverage": 2.0, - "info": { - "bracket": "6", - "initialLeverage": "2", - "notionalCap": "2000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.25", - "cum": "154500.0" + "cum": "29750.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 2000000.0, - "maxNotional": 3000000.0, + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "154750.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "3000000", - "notionalFloor": "2000000", + "notionalCap": "10000000", + "notionalFloor": "5000000", "maintMarginRatio": "0.5", - "cum": "654500.0" + "cum": "1404750.0" } } ], @@ -7818,144 +9000,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 10000.0, - "maintenanceMarginRate": 0.0065, - "maxLeverage": 50.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", - "notionalCap": "10000", + "initialLeverage": "75", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.0065", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, + "info": { + "bracket": "2", + "initialLeverage": "50", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" + } + }, + { + "tier": 3.0, + "currency": "USDT", "minNotional": 10000.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 40.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "40", "notionalCap": "50000", "notionalFloor": "10000", "maintMarginRatio": "0.01", - "cum": "35.0" + "cum": "42.5" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 25.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "25", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.02", - "cum": "535.0" + "cum": "542.5" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "8042.5" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "58042.5" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "108042.5" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "233042.5" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1233042.5" } }, { - "tier": 9.0, + "tier": 10.0, "currency": "USDT", "minNotional": 20000000.0, "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "9", + "bracket": "10", "initialLeverage": "1", "notionalCap": "50000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6233042.5" } } ], @@ -8062,144 +9260,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 10000.0, - "maintenanceMarginRate": 0.0065, - "maxLeverage": 50.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", - "notionalCap": "10000", + "initialLeverage": "75", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.0065", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, + "info": { + "bracket": "2", + "initialLeverage": "50", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" + } + }, + { + "tier": 3.0, + "currency": "USDT", "minNotional": 10000.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 40.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "40", "notionalCap": "50000", "notionalFloor": "10000", "maintMarginRatio": "0.01", - "cum": "35.0" + "cum": "42.5" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 25.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "25", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.02", - "cum": "535.0" + "cum": "542.5" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "8042.5" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "58042.5" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "108042.5" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "233042.5" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1233042.5" } }, { - "tier": 9.0, + "tier": 10.0, "currency": "USDT", "minNotional": 20000000.0, "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "9", + "bracket": "10", "initialLeverage": "1", "notionalCap": "50000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6233042.5" } } ], @@ -8210,10 +9424,10 @@ "minNotional": 0.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.004, - "maxLeverage": 50.0, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", + "initialLeverage": "75", "notionalCap": "50000", "notionalFloor": "0", "maintMarginRatio": "0.004", @@ -8226,10 +9440,10 @@ "minNotional": 50000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.005, - "maxLeverage": 25.0, + "maxLeverage": 50.0, "info": { "bracket": "2", - "initialLeverage": "25", + "initialLeverage": "50", "notionalCap": "100000", "notionalFloor": "50000", "maintMarginRatio": "0.005", @@ -8242,10 +9456,10 @@ "minNotional": 100000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "3", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "1000000", "notionalFloor": "100000", "maintMarginRatio": "0.01", @@ -8258,10 +9472,10 @@ "minNotional": 1000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "4", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.025", @@ -8274,10 +9488,10 @@ "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 6.0, + "maxLeverage": 10.0, "info": { "bracket": "5", - "initialLeverage": "6", + "initialLeverage": "10", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.05", @@ -8402,13 +9616,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maxLeverage": 50.0, "info": { "bracket": "3", - "initialLeverage": "25", - "notionalCap": "1000000", + "initialLeverage": "50", + "notionalCap": "2000000", "notionalFloor": "250000", "maintMarginRatio": "0.01", "cum": "1025.0" @@ -8417,71 +9631,71 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 2000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 15.0, + "maxLeverage": 20.0, "info": { "bracket": "4", - "initialLeverage": "15", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "initialLeverage": "20", + "notionalCap": "10000000", + "notionalFloor": "2000000", "maintMarginRatio": "0.02", - "cum": "11025.0" + "cum": "21025.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 5000000.0, - "maxNotional": 10000000.0, + "minNotional": 10000000.0, + "maxNotional": 25000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "5", "initialLeverage": "10", - "notionalCap": "10000000", - "notionalFloor": "5000000", + "notionalCap": "25000000", + "notionalFloor": "10000000", "maintMarginRatio": "0.05", - "cum": "161025.0" + "cum": "321025.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 10000000.0, - "maxNotional": 20000000.0, + "minNotional": 25000000.0, + "maxNotional": 50000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "6", "initialLeverage": "5", - "notionalCap": "20000000", - "notionalFloor": "10000000", + "notionalCap": "50000000", + "notionalFloor": "25000000", "maintMarginRatio": "0.1", - "cum": "661025.0" + "cum": "1571025.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 20000000.0, - "maxNotional": 40000000.0, + "minNotional": 50000000.0, + "maxNotional": 60000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { "bracket": "7", "initialLeverage": "4", - "notionalCap": "40000000", - "notionalFloor": "20000000", + "notionalCap": "60000000", + "notionalFloor": "50000000", "maintMarginRatio": "0.125", - "cum": "1161025.0" + "cum": "2821025.0" } }, { "tier": 8.0, "currency": "USDT", - "minNotional": 40000000.0, + "minNotional": 60000000.0, "maxNotional": 80000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, @@ -8489,9 +9703,9 @@ "bracket": "8", "initialLeverage": "3", "notionalCap": "80000000", - "notionalFloor": "40000000", + "notionalFloor": "60000000", "maintMarginRatio": "0.15", - "cum": "2161025.0" + "cum": "4321025.0" } }, { @@ -8507,7 +9721,7 @@ "notionalCap": "150000000", "notionalFloor": "80000000", "maintMarginRatio": "0.25", - "cum": "1.0161025E7" + "cum": "1.2321025E7" } }, { @@ -8523,7 +9737,7 @@ "notionalCap": "300000000", "notionalFloor": "150000000", "maintMarginRatio": "0.5", - "cum": "4.7661025E7" + "cum": "4.9821025E7" } } ], @@ -8641,6 +9855,120 @@ } } ], + "FET/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "200000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 200000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "200000", + "maintMarginRatio": "0.1", + "cum": "10650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "23150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "898150.0" + } + } + ], "FIL/BUSD:BUSD": [ { "tier": 1.0, @@ -8745,14 +10073,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "25", + "initialLeverage": "50", "notionalCap": "50000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, @@ -8761,111 +10089,127 @@ "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, - "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "250000", "notionalFloor": "50000", - "maintMarginRatio": "0.02", - "cum": "500.0" + "maintMarginRatio": "0.01", + "cum": "200.0" } }, { "tier": 3.0, "currency": "USDT", "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 10.0, + "maxNotional": 600000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "10", - "notionalCap": "1000000", + "initialLeverage": "20", + "notionalCap": "600000", "notionalFloor": "250000", - "maintMarginRatio": "0.05", - "cum": "8000.0" + "maintMarginRatio": "0.02", + "cum": "2700.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 2000000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 600000.0, + "maxNotional": 1200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "2000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.1", - "cum": "58000.0" + "initialLeverage": "10", + "notionalCap": "1200000", + "notionalFloor": "600000", + "maintMarginRatio": "0.05", + "cum": "20700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 2000000.0, - "maxNotional": 5000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 4.0, + "minNotional": 1200000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "4", - "notionalCap": "5000000", - "notionalFloor": "2000000", - "maintMarginRatio": "0.125", - "cum": "108000.0" + "initialLeverage": "5", + "notionalCap": "3000000", + "notionalFloor": "1200000", + "maintMarginRatio": "0.1", + "cum": "80700.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 5000000.0, - "maxNotional": 10000000.0, - "maintenanceMarginRate": 0.1665, - "maxLeverage": 3.0, + "minNotional": 3000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, "info": { "bracket": "6", - "initialLeverage": "3", - "notionalCap": "10000000", - "notionalFloor": "5000000", - "maintMarginRatio": "0.1665", - "cum": "315500.0" + "initialLeverage": "4", + "notionalCap": "6000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.125", + "cum": "155700.0" } }, { "tier": 7.0, "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, + "maintenanceMarginRate": 0.165, + "maxLeverage": 3.0, + "info": { + "bracket": "7", + "initialLeverage": "3", + "notionalCap": "10000000", + "notionalFloor": "6000000", + "maintMarginRatio": "0.165", + "cum": "395700.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1150500.0" + "cum": "1245700.0" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 20000000.0, "maxNotional": 30000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "1", "notionalCap": "30000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6150500.0" + "cum": "6245700.0" } } ], @@ -9170,10 +10514,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -9186,10 +10530,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -9202,10 +10546,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -9232,13 +10576,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -9247,17 +10591,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -9266,112 +10626,128 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "25", - "notionalCap": "50000", + "initialLeverage": "50", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", - "minNotional": 50000.0, - "maxNotional": 150000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 20.0, + "minNotional": 5000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "20", - "notionalCap": "150000", - "notionalFloor": "50000", - "maintMarginRatio": "0.025", - "cum": "750.0" + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "5000", + "maintMarginRatio": "0.01", + "cum": "20.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 150000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 10.0, + "minNotional": 50000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "10", - "notionalCap": "250000", - "notionalFloor": "150000", - "maintMarginRatio": "0.05", - "cum": "4500.0" + "initialLeverage": "20", + "notionalCap": "400000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "770.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 500000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 400000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "500000", - "notionalFloor": "250000", - "maintMarginRatio": "0.1", - "cum": "17000.0" + "initialLeverage": "10", + "notionalCap": "800000", + "notionalFloor": "400000", + "maintMarginRatio": "0.05", + "cum": "10770.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 500000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 4.0, + "minNotional": 800000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "4", - "notionalCap": "1000000", - "notionalFloor": "500000", - "maintMarginRatio": "0.125", - "cum": "29500.0" + "initialLeverage": "5", + "notionalCap": "2000000", + "notionalFloor": "800000", + "maintMarginRatio": "0.1", + "cum": "50770.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 4000000.0, - "maintenanceMarginRate": 0.25, - "maxLeverage": 2.0, + "minNotional": 2000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, "info": { "bracket": "6", - "initialLeverage": "2", - "notionalCap": "4000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.25", - "cum": "154500.0" + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.125", + "cum": "100770.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 4000000.0, - "maxNotional": 8000000.0, + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "725770.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "8000000", - "notionalFloor": "4000000", + "notionalCap": "20000000", + "notionalFloor": "12000000", "maintMarginRatio": "0.5", - "cum": "1154500.0" + "cum": "3725770.0" } } ], @@ -9555,6 +10931,104 @@ } } ], + "FXS/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "386900.0" + } + } + ], "GAL/BUSD:BUSD": [ { "tier": 1.0, @@ -9758,10 +11232,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 11.0, + "maxLeverage": 20.0, "info": { "bracket": "1", - "initialLeverage": "11", + "initialLeverage": "20", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -9855,14 +11329,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 11.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "11", + "initialLeverage": "50", "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.0065", "cum": "0.0" } }, @@ -9870,80 +11344,112 @@ "tier": 2.0, "currency": "USDT", "minNotional": 5000.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "10", - "notionalCap": "25000", + "initialLeverage": "25", + "notionalCap": "50000", "notionalFloor": "5000", - "maintMarginRatio": "0.025", - "cum": "75.0" + "maintMarginRatio": "0.01", + "cum": "17.5" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "minNotional": 50000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "700.0" + "initialLeverage": "20", + "notionalCap": "200000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "767.5" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 200000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5700.0" + "initialLeverage": "10", + "notionalCap": "400000", + "notionalFloor": "200000", + "maintMarginRatio": "0.05", + "cum": "5767.5" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 400000.0, "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "2", + "initialLeverage": "5", "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11950.0" + "notionalFloor": "400000", + "maintMarginRatio": "0.1", + "cum": "25767.5" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.125", + "cum": "50767.5" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "675767.5" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "3000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "2175767.5" } } ], @@ -10159,6 +11665,104 @@ } } ], + "GMX/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "386900.0" + } + } + ], "GRT/USDT:USDT": [ { "tier": 1.0, @@ -10196,13 +11800,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "100000", + "notionalCap": "400000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "700.0" @@ -10211,49 +11815,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 400000.0, + "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "1000000", + "notionalFloor": "400000", "maintMarginRatio": "0.1", - "cum": "5700.0" + "cum": "20700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, + "minNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.125", - "cum": "11950.0" + "cum": "45700.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295700.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "1795700.0" } } ], @@ -10453,6 +12073,120 @@ } } ], + "HIGH/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "400000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 400000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "1000000", + "notionalFloor": "400000", + "maintMarginRatio": "0.1", + "cum": "20650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.125", + "cum": "45650.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295650.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "10000000", + "notionalFloor": "6000000", + "maintMarginRatio": "0.5", + "cum": "1795650.0" + } + } + ], "HNT/USDT:USDT": [ { "tier": 1.0, @@ -10460,10 +12194,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 11.0, + "maxLeverage": 15.0, "info": { "bracket": "1", - "initialLeverage": "11", + "initialLeverage": "15", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -10490,12 +12224,110 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 120000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 8.0, "info": { "bracket": "3", "initialLeverage": "8", + "notionalCap": "120000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 120000.0, + "maxNotional": 300000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "300000", + "notionalFloor": "120000", + "maintMarginRatio": "0.1", + "cum": "6650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 300000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "300000", + "maintMarginRatio": "0.125", + "cum": "14150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "389150.0" + } + } + ], + "HOOK/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -10538,13 +12370,13 @@ "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "6", "initialLeverage": "1", - "notionalCap": "3000000", + "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.5", "cum": "386900.0" @@ -11146,10 +12978,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -11162,10 +12994,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -11176,13 +13008,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -11191,33 +13023,33 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", + "initialLeverage": "4", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { @@ -11225,15 +13057,31 @@ "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "3000000", - "notionalFloor": "1000000", + "notionalCap": "5000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "898150.0" } } ], @@ -11538,10 +13386,124 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.01", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "2", "initialLeverage": "20", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.02", + "cum": "50.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 10000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "25000", + "notionalFloor": "10000", + "maintMarginRatio": "0.025", + "cum": "100.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 8.0, + "info": { + "bracket": "4", + "initialLeverage": "8", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "725.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "5", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5725.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11975.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "8000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "1136975.0" + } + } + ], + "KLAY/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.01", @@ -11554,10 +13516,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -11568,13 +13530,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "400000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "700.0" @@ -11583,131 +13545,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 400000.0, + "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "1000000", + "notionalFloor": "400000", "maintMarginRatio": "0.1", - "cum": "5700.0" + "cum": "20700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, + "minNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.125", - "cum": "11950.0" + "cum": "45700.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, - "maintenanceMarginRate": 0.5, - "maxLeverage": 1.0, - "info": { - "bracket": "6", - "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.5", - "cum": "386950.0" - } - } - ], - "KLAY/USDT:USDT": [ - { - "tier": 1.0, - "currency": "USDT", - "minNotional": 0.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 20.0, - "info": { - "bracket": "1", - "initialLeverage": "20", - "notionalCap": "25000", - "notionalFloor": "0", - "maintMarginRatio": "0.025", - "cum": "0.0" - } - }, - { - "tier": 2.0, - "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 10.0, - "info": { - "bracket": "2", - "initialLeverage": "10", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "625.0" - } - }, - { - "tier": 3.0, - "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, - "info": { - "bracket": "3", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5625.0" - } - }, - { - "tier": 4.0, - "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "4", + "bracket": "6", "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11875.0" + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295700.0" } }, { - "tier": 5.0, + "tier": 7.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "5", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386875.0" + "cum": "1795700.0" } } ], @@ -11914,10 +13810,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 11.0, + "maxLeverage": 20.0, "info": { "bracket": "1", - "initialLeverage": "11", + "initialLeverage": "20", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -12011,14 +13907,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 11.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "11", + "initialLeverage": "50", "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, @@ -12026,80 +13922,112 @@ "tier": 2.0, "currency": "USDT", "minNotional": 5000.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "10", - "notionalCap": "25000", + "initialLeverage": "25", + "notionalCap": "50000", "notionalFloor": "5000", - "maintMarginRatio": "0.025", - "cum": "75.0" + "maintMarginRatio": "0.01", + "cum": "20.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "minNotional": 50000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "700.0" + "initialLeverage": "20", + "notionalCap": "400000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "770.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 400000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5700.0" + "initialLeverage": "10", + "notionalCap": "800000", + "notionalFloor": "400000", + "maintMarginRatio": "0.05", + "cum": "10770.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "minNotional": 800000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11950.0" + "initialLeverage": "5", + "notionalCap": "2000000", + "notionalFloor": "800000", + "maintMarginRatio": "0.1", + "cum": "50770.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, + "minNotional": 2000000.0, "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.125", + "cum": "100770.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "725770.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "20000000", + "notionalFloor": "12000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "3725770.0" } } ], @@ -12306,10 +14234,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -12322,10 +14250,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -12338,10 +14266,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -12368,13 +14296,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -12383,17 +14311,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -12402,144 +14346,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 10000.0, - "maintenanceMarginRate": 0.0065, - "maxLeverage": 50.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", - "notionalCap": "10000", + "initialLeverage": "75", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.0065", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, + "info": { + "bracket": "2", + "initialLeverage": "50", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" + } + }, + { + "tier": 3.0, + "currency": "USDT", "minNotional": 10000.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 40.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "40", "notionalCap": "50000", "notionalFloor": "10000", "maintMarginRatio": "0.01", - "cum": "35.0" + "cum": "42.5" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 25.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "25", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.02", - "cum": "535.0" + "cum": "542.5" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "8042.5" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "58042.5" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "108042.5" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "233042.5" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1233042.5" } }, { - "tier": 9.0, + "tier": 10.0, "currency": "USDT", "minNotional": 20000000.0, "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "9", + "bracket": "10", "initialLeverage": "1", "notionalCap": "50000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6233042.5" } } ], @@ -12723,6 +14683,120 @@ } } ], + "LQTY/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "200000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 200000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "200000", + "maintMarginRatio": "0.1", + "cum": "10650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "23150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "898150.0" + } + } + ], "LRC/USDT:USDT": [ { "tier": 1.0, @@ -12828,10 +14902,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -12844,10 +14918,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -12860,10 +14934,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -12890,13 +14964,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -12905,17 +14979,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -12924,144 +15014,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 10000.0, - "maintenanceMarginRate": 0.0065, - "maxLeverage": 25.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "25", - "notionalCap": "10000", + "initialLeverage": "75", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.0065", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", - "minNotional": 10000.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, "info": { "bracket": "2", - "initialLeverage": "20", - "notionalCap": "50000", - "notionalFloor": "10000", - "maintMarginRatio": "0.01", - "cum": "35.0" + "initialLeverage": "50", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 50000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.02, - "maxLeverage": 15.0, + "minNotional": 10000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 40.0, "info": { "bracket": "3", - "initialLeverage": "15", - "notionalCap": "250000", - "notionalFloor": "50000", - "maintMarginRatio": "0.02", - "cum": "535.0" + "initialLeverage": "40", + "notionalCap": "50000", + "notionalFloor": "10000", + "maintMarginRatio": "0.01", + "cum": "42.5" } }, { "tier": 4.0, "currency": "USDT", + "minNotional": 50000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "4", + "initialLeverage": "25", + "notionalCap": "250000", + "notionalFloor": "50000", + "maintMarginRatio": "0.02", + "cum": "542.5" + } + }, + { + "tier": 5.0, + "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "8042.5" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "58042.5" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "108042.5" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "233042.5" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1233042.5" } }, { - "tier": 9.0, + "tier": 10.0, "currency": "USDT", "minNotional": 20000000.0, - "maxNotional": 32000000.0, + "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "9", + "bracket": "10", "initialLeverage": "1", - "notionalCap": "32000000", + "notionalCap": "50000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6233042.5" } } ], @@ -13261,207 +15367,11 @@ } } ], - "MANA/USDT:USDT": [ + "MAGIC/USDT:USDT": [ { "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, - "info": { - "bracket": "1", - "initialLeverage": "25", - "notionalCap": "50000", - "notionalFloor": "0", - "maintMarginRatio": "0.01", - "cum": "0.0" - } - }, - { - "tier": 2.0, - "currency": "USDT", - "minNotional": 50000.0, - "maxNotional": 150000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 20.0, - "info": { - "bracket": "2", - "initialLeverage": "20", - "notionalCap": "150000", - "notionalFloor": "50000", - "maintMarginRatio": "0.025", - "cum": "750.0" - } - }, - { - "tier": 3.0, - "currency": "USDT", - "minNotional": 150000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 10.0, - "info": { - "bracket": "3", - "initialLeverage": "10", - "notionalCap": "250000", - "notionalFloor": "150000", - "maintMarginRatio": "0.05", - "cum": "4500.0" - } - }, - { - "tier": 4.0, - "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 500000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, - "info": { - "bracket": "4", - "initialLeverage": "5", - "notionalCap": "500000", - "notionalFloor": "250000", - "maintMarginRatio": "0.1", - "cum": "17000.0" - } - }, - { - "tier": 5.0, - "currency": "USDT", - "minNotional": 500000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 4.0, - "info": { - "bracket": "5", - "initialLeverage": "4", - "notionalCap": "1000000", - "notionalFloor": "500000", - "maintMarginRatio": "0.125", - "cum": "29500.0" - } - }, - { - "tier": 6.0, - "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 2000000.0, - "maintenanceMarginRate": 0.25, - "maxLeverage": 2.0, - "info": { - "bracket": "6", - "initialLeverage": "2", - "notionalCap": "2000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.25", - "cum": "154500.0" - } - }, - { - "tier": 7.0, - "currency": "USDT", - "minNotional": 2000000.0, - "maxNotional": 5000000.0, - "maintenanceMarginRate": 0.5, - "maxLeverage": 1.0, - "info": { - "bracket": "7", - "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "2000000", - "maintMarginRatio": "0.5", - "cum": "654500.0" - } - } - ], - "MASK/USDT:USDT": [ - { - "tier": 1.0, - "currency": "USDT", - "minNotional": 0.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 20.0, - "info": { - "bracket": "1", - "initialLeverage": "20", - "notionalCap": "25000", - "notionalFloor": "0", - "maintMarginRatio": "0.025", - "cum": "0.0" - } - }, - { - "tier": 2.0, - "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 10.0, - "info": { - "bracket": "2", - "initialLeverage": "10", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "625.0" - } - }, - { - "tier": 3.0, - "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, - "info": { - "bracket": "3", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5625.0" - } - }, - { - "tier": 4.0, - "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, - "info": { - "bracket": "4", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11875.0" - } - }, - { - "tier": 5.0, - "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, - "maintenanceMarginRate": 0.5, - "maxLeverage": 1.0, - "info": { - "bracket": "5", - "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.5", - "cum": "386875.0" - } - } - ], - "MATIC/BUSD:BUSD": [ - { - "tier": 1.0, - "currency": "BUSD", - "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 20.0, @@ -13474,16 +15384,374 @@ "cum": "0.0" } }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "386900.0" + } + } + ], + "MANA/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, + "info": { + "bracket": "1", + "initialLeverage": "50", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.0065", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.0075, + "maxLeverage": 40.0, + "info": { + "bracket": "2", + "initialLeverage": "40", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0075", + "cum": "5.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "3", + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "25000", + "maintMarginRatio": "0.01", + "cum": "67.5" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 50000.0, + "maxNotional": 150000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "4", + "initialLeverage": "20", + "notionalCap": "150000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "817.5" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 150000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "5", + "initialLeverage": "10", + "notionalCap": "250000", + "notionalFloor": "150000", + "maintMarginRatio": "0.05", + "cum": "4567.5" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "6", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "250000", + "maintMarginRatio": "0.1", + "cum": "17067.5" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "7", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "29567.5" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "8", + "initialLeverage": "2", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "154567.5" + } + }, + { + "tier": 9.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 10000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "9", + "initialLeverage": "1", + "notionalCap": "10000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.5", + "cum": "1404567.5" + } + } + ], + "MASK/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 600000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "600000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 600000.0, + "maxNotional": 1600000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "1600000", + "notionalFloor": "600000", + "maintMarginRatio": "0.1", + "cum": "30650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 1600000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "3000000", + "notionalFloor": "1600000", + "maintMarginRatio": "0.125", + "cum": "70650.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.25", + "cum": "445650.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "10000000", + "notionalFloor": "6000000", + "maintMarginRatio": "0.5", + "cum": "1945650.0" + } + } + ], + "MATIC/BUSD:BUSD": [ + { + "tier": 1.0, + "currency": "BUSD", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, { "tier": 2.0, "currency": "BUSD", "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -13496,10 +15764,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -13526,13 +15794,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -13541,17 +15809,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -13560,13 +15844,159 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, + "info": { + "bracket": "1", + "initialLeverage": "50", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.006", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.007, + "maxLeverage": 40.0, + "info": { + "bracket": "2", + "initialLeverage": "40", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.007", + "cum": "5.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 25.0, + "info": { + "bracket": "3", + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "25000", + "maintMarginRatio": "0.01", + "cum": "80.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 50000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "4", + "initialLeverage": "20", + "notionalCap": "400000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "830.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 400000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "5", + "initialLeverage": "10", + "notionalCap": "800000", + "notionalFloor": "400000", + "maintMarginRatio": "0.05", + "cum": "10830.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 800000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "6", + "initialLeverage": "5", + "notionalCap": "2000000", + "notionalFloor": "800000", + "maintMarginRatio": "0.1", + "cum": "50830.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 2000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "7", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.125", + "cum": "100830.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "8", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "725830.0" + } + }, + { + "tier": 9.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "9", + "initialLeverage": "1", + "notionalCap": "20000000", + "notionalFloor": "12000000", + "maintMarginRatio": "0.5", + "cum": "3725830.0" + } + } + ], + "MINA/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "1", "initialLeverage": "25", - "notionalCap": "50000", + "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.01", "cum": "0.0" @@ -13575,97 +16005,97 @@ { "tier": 2.0, "currency": "USDT", - "minNotional": 50000.0, - "maxNotional": 150000.0, + "minNotional": 5000.0, + "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, "maxLeverage": 20.0, "info": { "bracket": "2", "initialLeverage": "20", - "notionalCap": "150000", - "notionalFloor": "50000", + "notionalCap": "25000", + "notionalFloor": "5000", "maintMarginRatio": "0.025", - "cum": "750.0" + "cum": "75.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 150000.0, - "maxNotional": 250000.0, + "minNotional": 25000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "250000", - "notionalFloor": "150000", + "notionalCap": "400000", + "notionalFloor": "25000", "maintMarginRatio": "0.05", - "cum": "4500.0" + "cum": "700.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 500000.0, + "minNotional": 400000.0, + "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "500000", - "notionalFloor": "250000", + "notionalCap": "1000000", + "notionalFloor": "400000", "maintMarginRatio": "0.1", - "cum": "17000.0" + "cum": "20700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 500000.0, - "maxNotional": 750000.0, + "minNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { "bracket": "5", "initialLeverage": "4", - "notionalCap": "750000", - "notionalFloor": "500000", + "notionalCap": "2000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.125", - "cum": "29500.0" + "cum": "45700.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 750000.0, - "maxNotional": 1000000.0, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { "bracket": "6", "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "750000", + "notionalCap": "6000000", + "notionalFloor": "2000000", "maintMarginRatio": "0.25", - "cum": "123250.0" + "cum": "295700.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 8000000.0, + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "7", "initialLeverage": "1", - "notionalCap": "8000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "373250.0" + "cum": "1795700.0" } } ], @@ -14000,13 +16430,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 150000.0, - "maxNotional": 250000.0, + "maxNotional": 600000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "250000", + "notionalCap": "600000", "notionalFloor": "150000", "maintMarginRatio": "0.05", "cum": "4500.0" @@ -14015,65 +16445,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 500000.0, + "minNotional": 600000.0, + "maxNotional": 1600000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "500000", - "notionalFloor": "250000", + "notionalCap": "1600000", + "notionalFloor": "600000", "maintMarginRatio": "0.1", - "cum": "17000.0" + "cum": "34500.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 500000.0, - "maxNotional": 1000000.0, + "minNotional": 1600000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { "bracket": "5", "initialLeverage": "4", - "notionalCap": "1000000", - "notionalFloor": "500000", + "notionalCap": "2000000", + "notionalFloor": "1600000", "maintMarginRatio": "0.125", - "cum": "29500.0" + "cum": "74500.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 2000000.0, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { "bracket": "6", "initialLeverage": "2", - "notionalCap": "2000000", - "notionalFloor": "1000000", + "notionalCap": "6000000", + "notionalFloor": "2000000", "maintMarginRatio": "0.25", - "cum": "154500.0" + "cum": "324500.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 2000000.0, - "maxNotional": 5000000.0, + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "2000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "654500.0" + "cum": "1824500.0" } } ], @@ -14083,14 +16513,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "25", + "initialLeverage": "50", "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, @@ -14098,80 +16528,112 @@ "tier": 2.0, "currency": "USDT", "minNotional": 5000.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 20.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "20", - "notionalCap": "25000", + "initialLeverage": "25", + "notionalCap": "50000", "notionalFloor": "5000", - "maintMarginRatio": "0.025", - "cum": "75.0" + "maintMarginRatio": "0.01", + "cum": "20.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 10.0, + "minNotional": 50000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "10", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "700.0" + "initialLeverage": "20", + "notionalCap": "400000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "770.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 400000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5700.0" + "initialLeverage": "10", + "notionalCap": "800000", + "notionalFloor": "400000", + "maintMarginRatio": "0.05", + "cum": "10770.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "minNotional": 800000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11950.0" + "initialLeverage": "5", + "notionalCap": "2000000", + "notionalFloor": "800000", + "maintMarginRatio": "0.1", + "cum": "50770.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, + "minNotional": 2000000.0, "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.125", + "cum": "100770.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "725770.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "20000000", + "notionalFloor": "12000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "3725770.0" } } ], @@ -14280,10 +16742,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 21.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "21", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -14310,13 +16772,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "100000", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -14325,23 +16787,23 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 2.0, @@ -14349,25 +16811,25 @@ "bracket": "5", "initialLeverage": "2", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "6", "initialLeverage": "1", - "notionalCap": "3000000", + "notionalCap": "5000000", "notionalFloor": "1000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "398150.0" } } ], @@ -14769,14 +17231,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "50", "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, @@ -14784,80 +17246,112 @@ "tier": 2.0, "currency": "USDT", "minNotional": 5000.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "10", - "notionalCap": "25000", + "initialLeverage": "25", + "notionalCap": "50000", "notionalFloor": "5000", - "maintMarginRatio": "0.025", - "cum": "75.0" + "maintMarginRatio": "0.01", + "cum": "20.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "minNotional": 50000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "700.0" + "initialLeverage": "20", + "notionalCap": "400000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "770.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 400000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5700.0" + "initialLeverage": "10", + "notionalCap": "800000", + "notionalFloor": "400000", + "maintMarginRatio": "0.05", + "cum": "10770.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "minNotional": 800000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11950.0" + "initialLeverage": "5", + "notionalCap": "2000000", + "notionalFloor": "800000", + "maintMarginRatio": "0.1", + "cum": "50770.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 8000000.0, + "minNotional": 2000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.125", + "cum": "100770.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "725770.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "8000000", - "notionalFloor": "1000000", + "notionalCap": "20000000", + "notionalFloor": "12000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "3725770.0" } } ], @@ -14959,6 +17453,120 @@ } } ], + "PERP/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "200000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 200000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "200000", + "maintMarginRatio": "0.1", + "cum": "10650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "23150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "898150.0" + } + } + ], "PHB/BUSD:BUSD": [ { "tier": 1.0, @@ -15057,6 +17665,104 @@ } } ], + "PHB/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "386900.0" + } + } + ], "QNT/USDT:USDT": [ { "tier": 1.0, @@ -15192,13 +17898,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "100000", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "700.0" @@ -15207,49 +17913,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5700.0" + "cum": "10700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", + "initialLeverage": "4", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11950.0" + "cum": "23200.0" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148200.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "898200.0" } } ], @@ -15456,10 +18178,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 10.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "10", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -15472,10 +18194,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 8.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "8", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -15486,13 +18208,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 6.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "6", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -15501,49 +18223,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", + "initialLeverage": "4", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, - "maxNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "1500000", - "notionalFloor": "1000000", + "notionalCap": "5000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "898150.0" } } ], @@ -15629,105 +18367,7 @@ } } ], - "ROSE/USDT:USDT": [ - { - "tier": 1.0, - "currency": "USDT", - "minNotional": 0.0, - "maxNotional": 5000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, - "info": { - "bracket": "1", - "initialLeverage": "20", - "notionalCap": "5000", - "notionalFloor": "0", - "maintMarginRatio": "0.01", - "cum": "0.0" - } - }, - { - "tier": 2.0, - "currency": "USDT", - "minNotional": 5000.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, - "info": { - "bracket": "2", - "initialLeverage": "10", - "notionalCap": "25000", - "notionalFloor": "5000", - "maintMarginRatio": "0.025", - "cum": "75.0" - } - }, - { - "tier": 3.0, - "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, - "info": { - "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "700.0" - } - }, - { - "tier": 4.0, - "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, - "info": { - "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5700.0" - } - }, - { - "tier": 5.0, - "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, - "info": { - "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11950.0" - } - }, - { - "tier": 6.0, - "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, - "maintenanceMarginRate": 0.5, - "maxLeverage": 1.0, - "info": { - "bracket": "6", - "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.5", - "cum": "386950.0" - } - } - ], - "RSR/USDT:USDT": [ + "RNDR/USDT:USDT": [ { "tier": 1.0, "currency": "USDT", @@ -15750,10 +18390,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -15766,10 +18406,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -15825,6 +18465,234 @@ } } ], + "ROSE/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.01", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "75.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "200000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "700.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 200000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "200000", + "maintMarginRatio": "0.1", + "cum": "10700.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "23200.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148200.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "898200.0" + } + } + ], + "RSR/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 300000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "300000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 300000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "800000", + "notionalFloor": "300000", + "maintMarginRatio": "0.1", + "cum": "15650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 800000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "800000", + "maintMarginRatio": "0.125", + "cum": "35650.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "160650.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "910650.0" + } + } + ], "RUNE/USDT:USDT": [ { "tier": 1.0, @@ -16124,96 +18992,96 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 50000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 25.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "25", - "notionalCap": "50000", + "initialLeverage": "50", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", - "minNotional": 50000.0, - "maxNotional": 150000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 20.0, + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.007, + "maxLeverage": 40.0, "info": { "bracket": "2", - "initialLeverage": "20", - "notionalCap": "150000", - "notionalFloor": "50000", - "maintMarginRatio": "0.025", - "cum": "750.0" + "initialLeverage": "40", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.007", + "cum": "5.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 150000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 10.0, + "minNotional": 25000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "3", - "initialLeverage": "10", - "notionalCap": "250000", - "notionalFloor": "150000", - "maintMarginRatio": "0.05", - "cum": "4500.0" + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "25000", + "maintMarginRatio": "0.01", + "cum": "80.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 500000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 50000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "500000", - "notionalFloor": "250000", - "maintMarginRatio": "0.1", - "cum": "17000.0" + "initialLeverage": "20", + "notionalCap": "400000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "830.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 500000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 4.0, + "minNotional": 400000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "5", - "initialLeverage": "4", - "notionalCap": "1000000", - "notionalFloor": "500000", - "maintMarginRatio": "0.125", - "cum": "29500.0" + "initialLeverage": "10", + "notionalCap": "800000", + "notionalFloor": "400000", + "maintMarginRatio": "0.05", + "cum": "10830.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, + "minNotional": 800000.0, "maxNotional": 2000000.0, - "maintenanceMarginRate": 0.25, - "maxLeverage": 2.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "6", - "initialLeverage": "2", + "initialLeverage": "5", "notionalCap": "2000000", - "notionalFloor": "1000000", - "maintMarginRatio": "0.25", - "cum": "154500.0" + "notionalFloor": "800000", + "maintMarginRatio": "0.1", + "cum": "50830.0" } }, { @@ -16221,15 +19089,47 @@ "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "7", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.125", + "cum": "100830.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "8", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "725830.0" + } + }, + { + "tier": 9.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "9", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "2000000", + "notionalCap": "20000000", + "notionalFloor": "12000000", "maintMarginRatio": "0.5", - "cum": "654500.0" + "cum": "3725830.0" } } ], @@ -16338,10 +19238,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -16354,10 +19254,10 @@ "minNotional": 5000.0, "maxNotional": 15000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "15000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -16370,10 +19270,10 @@ "minNotional": 15000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "15000", "maintMarginRatio": "0.05", @@ -16400,13 +19300,13 @@ "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 3000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 2.0, "info": { "bracket": "5", "initialLeverage": "2", - "notionalCap": "1000000", + "notionalCap": "3000000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11650.0" @@ -16415,17 +19315,17 @@ { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 3000000.0, + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { "bracket": "6", "initialLeverage": "1", - "notionalCap": "3000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386650.0" + "cum": "1136650.0" } } ], @@ -16534,10 +19434,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.01", @@ -16550,10 +19450,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -16564,13 +19464,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "400000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "700.0" @@ -16579,49 +19479,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 400000.0, + "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "1000000", + "notionalFloor": "400000", "maintMarginRatio": "0.1", - "cum": "5700.0" + "cum": "20700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, + "minNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.125", - "cum": "11950.0" + "cum": "45700.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295700.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "1795700.0" } } ], @@ -16630,96 +19546,112 @@ "tier": 1.0, "currency": "BUSD", "minNotional": 0.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 20.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", - "notionalCap": "100000", + "initialLeverage": "25", + "notionalCap": "50000", "notionalFloor": "0", - "maintMarginRatio": "0.025", + "maintMarginRatio": "0.02", "cum": "0.0" } }, { "tier": 2.0, "currency": "BUSD", + "minNotional": 50000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "100000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "250.0" + } + }, + { + "tier": 3.0, + "currency": "BUSD", "minNotional": 100000.0, "maxNotional": 500000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "10", "notionalCap": "500000", "notionalFloor": "100000", "maintMarginRatio": "0.05", - "cum": "2500.0" + "cum": "2750.0" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "BUSD", "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "5", "notionalCap": "1000000", "notionalFloor": "500000", "maintMarginRatio": "0.1", - "cum": "27500.0" + "cum": "27750.0" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "BUSD", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "3", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.15", - "cum": "77500.0" + "cum": "77750.0" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "BUSD", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "2", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.25", - "cum": "277500.0" + "cum": "277750.0" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "BUSD", "minNotional": 5000000.0, "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", "notionalCap": "8000000", "notionalFloor": "5000000", "maintMarginRatio": "0.5", - "cum": "1527500.0" + "cum": "1527750.0" } } ], @@ -16728,112 +19660,128 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 150000.0, - "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "20", - "notionalCap": "150000", + "initialLeverage": "50", + "notionalCap": "50000", "notionalFloor": "0", - "maintMarginRatio": "0.02", + "maintMarginRatio": "0.01", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 50000.0, + "maxNotional": 150000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "150000", + "notionalFloor": "50000", + "maintMarginRatio": "0.02", + "cum": "500.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", "minNotional": 150000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.025, "maxLeverage": 15.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "15", "notionalCap": "250000", "notionalFloor": "150000", "maintMarginRatio": "0.025", - "cum": "750.0" + "cum": "1250.0" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "7000.0" + "cum": "7500.0" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "57000.0" + "cum": "57500.0" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "107000.0" + "cum": "107500.0" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "2", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.25", - "cum": "732000.0" + "cum": "732500.0" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 10000000.0, - "maxNotional": 11000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "11000000", + "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.5", - "cum": "3232000.0" + "cum": "3232500.0" } } ], @@ -17017,7 +19965,7 @@ } } ], - "STG/USDT:USDT": [ + "SSV/USDT:USDT": [ { "tier": 1.0, "currency": "USDT", @@ -17040,10 +19988,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -17054,13 +20002,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -17069,33 +20017,33 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", + "initialLeverage": "4", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { @@ -17103,15 +20051,145 @@ "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "898150.0" + } + } + ], + "STG/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "200000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 200000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "200000", + "maintMarginRatio": "0.1", + "cum": "10650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "23150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", "notionalCap": "3000000", "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "898150.0" } } ], @@ -17250,13 +20328,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 300000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "100000", + "notionalCap": "300000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "700.0" @@ -17265,49 +20343,179 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 300000.0, + "maxNotional": 800000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "800000", + "notionalFloor": "300000", "maintMarginRatio": "0.1", - "cum": "5700.0" + "cum": "15700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 800000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", + "initialLeverage": "4", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "800000", "maintMarginRatio": "0.125", - "cum": "11950.0" + "cum": "35700.0" } }, { "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "160700.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "910700.0" + } + } + ], + "STX/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.01", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "2", + "initialLeverage": "20", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "75.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "400000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "700.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 400000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "1000000", + "notionalFloor": "400000", + "maintMarginRatio": "0.1", + "cum": "20700.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.125", + "cum": "45700.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295700.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "10000000", + "notionalFloor": "6000000", + "maintMarginRatio": "0.5", + "cum": "1795700.0" } } ], @@ -17507,6 +20715,104 @@ } } ], + "T/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 20.0, + "info": { + "bracket": "1", + "initialLeverage": "20", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 100000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "100000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 100000.0, + "maxNotional": 250000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "250000", + "notionalFloor": "100000", + "maintMarginRatio": "0.1", + "cum": "5650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 250000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 2.0, + "info": { + "bracket": "5", + "initialLeverage": "2", + "notionalCap": "1000000", + "notionalFloor": "250000", + "maintMarginRatio": "0.125", + "cum": "11900.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "6", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.5", + "cum": "386900.0" + } + } + ], "THETA/USDT:USDT": [ { "tier": 1.0, @@ -18029,10 +21335,10 @@ } } ], - "TRX/BUSD:BUSD": [ + "TRU/USDT:USDT": [ { "tier": 1.0, - "currency": "BUSD", + "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, @@ -18046,16 +21352,130 @@ "cum": "0.0" } }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 25000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 15.0, + "info": { + "bracket": "2", + "initialLeverage": "15", + "notionalCap": "25000", + "notionalFloor": "5000", + "maintMarginRatio": "0.025", + "cum": "25.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 25000.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "3", + "initialLeverage": "10", + "notionalCap": "200000", + "notionalFloor": "25000", + "maintMarginRatio": "0.05", + "cum": "650.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 200000.0, + "maxNotional": 500000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "4", + "initialLeverage": "5", + "notionalCap": "500000", + "notionalFloor": "200000", + "maintMarginRatio": "0.1", + "cum": "10650.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 500000.0, + "maxNotional": 1000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "5", + "initialLeverage": "4", + "notionalCap": "1000000", + "notionalFloor": "500000", + "maintMarginRatio": "0.125", + "cum": "23150.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 1000000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "7", + "initialLeverage": "1", + "notionalCap": "5000000", + "notionalFloor": "3000000", + "maintMarginRatio": "0.5", + "cum": "898150.0" + } + } + ], + "TRX/BUSD:BUSD": [ + { + "tier": 1.0, + "currency": "BUSD", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.02, + "maxLeverage": 25.0, + "info": { + "bracket": "1", + "initialLeverage": "25", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.02", + "cum": "0.0" + } + }, { "tier": 2.0, "currency": "BUSD", "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 15.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "15", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -18068,10 +21488,10 @@ "minNotional": 25000.0, "maxNotional": 100000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", + "initialLeverage": "10", "notionalCap": "100000", "notionalFloor": "25000", "maintMarginRatio": "0.05", @@ -18098,13 +21518,13 @@ "tier": 5.0, "currency": "BUSD", "minNotional": 250000.0, - "maxNotional": 1000000.0, + "maxNotional": 1500000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", + "initialLeverage": "4", + "notionalCap": "1500000", "notionalFloor": "250000", "maintMarginRatio": "0.125", "cum": "11900.0" @@ -18113,17 +21533,33 @@ { "tier": 6.0, "currency": "BUSD", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 1500000.0, + "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1500000", + "maintMarginRatio": "0.25", + "cum": "199400.0" + } + }, + { + "tier": 7.0, + "currency": "BUSD", + "minNotional": 3000000.0, + "maxNotional": 8000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "8000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "949400.0" } } ], @@ -18134,10 +21570,10 @@ "minNotional": 0.0, "maxNotional": 10000.0, "maintenanceMarginRate": 0.0065, - "maxLeverage": 30.0, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "30", + "initialLeverage": "50", "notionalCap": "10000", "notionalFloor": "0", "maintMarginRatio": "0.0065", @@ -18148,13 +21584,13 @@ "tier": 2.0, "currency": "USDT", "minNotional": 10000.0, - "maxNotional": 50000.0, + "maxNotional": 90000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 25.0, "info": { "bracket": "2", "initialLeverage": "25", - "notionalCap": "50000", + "notionalCap": "90000", "notionalFloor": "10000", "maintMarginRatio": "0.01", "cum": "35.0" @@ -18163,87 +21599,87 @@ { "tier": 3.0, "currency": "USDT", - "minNotional": 50000.0, - "maxNotional": 250000.0, + "minNotional": 90000.0, + "maxNotional": 645000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 20.0, "info": { "bracket": "3", "initialLeverage": "20", - "notionalCap": "250000", - "notionalFloor": "50000", + "notionalCap": "645000", + "notionalFloor": "90000", "maintMarginRatio": "0.02", - "cum": "535.0" + "cum": "935.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, + "minNotional": 645000.0, + "maxNotional": 1200000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "4", "initialLeverage": "10", - "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalCap": "1200000", + "notionalFloor": "645000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "20285.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 2000000.0, + "minNotional": 1200000.0, + "maxNotional": 3000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "5", "initialLeverage": "5", - "notionalCap": "2000000", - "notionalFloor": "1000000", + "notionalCap": "3000000", + "notionalFloor": "1200000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "80285.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 2000000.0, - "maxNotional": 5000000.0, + "minNotional": 3000000.0, + "maxNotional": 6000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { "bracket": "6", "initialLeverage": "4", - "notionalCap": "5000000", - "notionalFloor": "2000000", + "notionalCap": "6000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "155285.0" } }, { "tier": 7.0, "currency": "USDT", - "minNotional": 5000000.0, - "maxNotional": 10000000.0, + "minNotional": 6000000.0, + "maxNotional": 12000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { "bracket": "7", "initialLeverage": "3", - "notionalCap": "10000000", - "notionalFloor": "5000000", + "notionalCap": "12000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "305285.0" } }, { "tier": 8.0, "currency": "USDT", - "minNotional": 10000000.0, + "minNotional": 12000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, @@ -18251,9 +21687,9 @@ "bracket": "8", "initialLeverage": "2", "notionalCap": "20000000", - "notionalFloor": "10000000", + "notionalFloor": "12000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1505285.0" } }, { @@ -18269,7 +21705,7 @@ "notionalCap": "30000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6505285.0" } } ], @@ -18475,14 +21911,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "50", "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, @@ -18490,80 +21926,242 @@ "tier": 2.0, "currency": "USDT", "minNotional": 5000.0, - "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "10", - "notionalCap": "25000", + "initialLeverage": "25", + "notionalCap": "50000", "notionalFloor": "5000", - "maintMarginRatio": "0.025", - "cum": "75.0" + "maintMarginRatio": "0.01", + "cum": "20.0" } }, { "tier": 3.0, "currency": "USDT", - "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "minNotional": 50000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", - "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "700.0" + "initialLeverage": "20", + "notionalCap": "400000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "770.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 400000.0, + "maxNotional": 800000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5700.0" + "initialLeverage": "10", + "notionalCap": "800000", + "notionalFloor": "400000", + "maintMarginRatio": "0.05", + "cum": "10770.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "minNotional": 800000.0, + "maxNotional": 2000000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11950.0" + "initialLeverage": "5", + "notionalCap": "2000000", + "notionalFloor": "800000", + "maintMarginRatio": "0.1", + "cum": "50770.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, + "minNotional": 2000000.0, "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.125", + "cum": "100770.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "725770.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "20000000", + "notionalFloor": "12000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "3725770.0" + } + } + ], + "USDC/USDT:USDT": [ + { + "tier": 1.0, + "currency": "USDT", + "minNotional": 0.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 30.0, + "info": { + "bracket": "1", + "initialLeverage": "30", + "notionalCap": "5000", + "notionalFloor": "0", + "maintMarginRatio": "0.006", + "cum": "0.0" + } + }, + { + "tier": 2.0, + "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 50000.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, + "info": { + "bracket": "2", + "initialLeverage": "25", + "notionalCap": "50000", + "notionalFloor": "5000", + "maintMarginRatio": "0.01", + "cum": "20.0" + } + }, + { + "tier": 3.0, + "currency": "USDT", + "minNotional": 50000.0, + "maxNotional": 600000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, + "info": { + "bracket": "3", + "initialLeverage": "20", + "notionalCap": "600000", + "notionalFloor": "50000", + "maintMarginRatio": "0.025", + "cum": "770.0" + } + }, + { + "tier": 4.0, + "currency": "USDT", + "minNotional": 600000.0, + "maxNotional": 1200000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, + "info": { + "bracket": "4", + "initialLeverage": "10", + "notionalCap": "1200000", + "notionalFloor": "600000", + "maintMarginRatio": "0.05", + "cum": "15770.0" + } + }, + { + "tier": 5.0, + "currency": "USDT", + "minNotional": 1200000.0, + "maxNotional": 3200000.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, + "info": { + "bracket": "5", + "initialLeverage": "5", + "notionalCap": "3200000", + "notionalFloor": "1200000", + "maintMarginRatio": "0.1", + "cum": "75770.0" + } + }, + { + "tier": 6.0, + "currency": "USDT", + "minNotional": 3200000.0, + "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "3200000", + "maintMarginRatio": "0.125", + "cum": "155770.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 12000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "12000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "780770.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 12000000.0, + "maxNotional": 20000000.0, + "maintenanceMarginRate": 0.5, + "maxLeverage": 1.0, + "info": { + "bracket": "8", + "initialLeverage": "1", + "notionalCap": "20000000", + "notionalFloor": "12000000", + "maintMarginRatio": "0.5", + "cum": "3780770.0" } } ], @@ -18573,14 +22171,14 @@ "currency": "USDT", "minNotional": 0.0, "maxNotional": 5000.0, - "maintenanceMarginRate": 0.01, - "maxLeverage": 20.0, + "maintenanceMarginRate": 0.006, + "maxLeverage": 50.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "50", "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.01", + "maintMarginRatio": "0.006", "cum": "0.0" } }, @@ -18589,63 +22187,63 @@ "currency": "USDT", "minNotional": 5000.0, "maxNotional": 25000.0, - "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maintenanceMarginRate": 0.01, + "maxLeverage": 25.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "25", "notionalCap": "25000", "notionalFloor": "5000", - "maintMarginRatio": "0.025", - "cum": "75.0" + "maintMarginRatio": "0.01", + "cum": "20.0" } }, { "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, - "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxNotional": 200000.0, + "maintenanceMarginRate": 0.025, + "maxLeverage": 20.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "20", + "notionalCap": "200000", "notionalFloor": "25000", - "maintMarginRatio": "0.05", - "cum": "700.0" + "maintMarginRatio": "0.025", + "cum": "395.0" } }, { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, - "maintenanceMarginRate": 0.1, - "maxLeverage": 5.0, + "minNotional": 200000.0, + "maxNotional": 400000.0, + "maintenanceMarginRate": 0.05, + "maxLeverage": 10.0, "info": { "bracket": "4", - "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", - "maintMarginRatio": "0.1", - "cum": "5700.0" + "initialLeverage": "10", + "notionalCap": "400000", + "notionalFloor": "200000", + "maintMarginRatio": "0.05", + "cum": "5395.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 400000.0, "maxNotional": 1000000.0, - "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maintenanceMarginRate": 0.1, + "maxLeverage": 5.0, "info": { "bracket": "5", - "initialLeverage": "2", + "initialLeverage": "5", "notionalCap": "1000000", - "notionalFloor": "250000", - "maintMarginRatio": "0.125", - "cum": "11950.0" + "notionalFloor": "400000", + "maintMarginRatio": "0.1", + "cum": "25395.0" } }, { @@ -18653,15 +22251,47 @@ "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 5000000.0, + "maintenanceMarginRate": 0.125, + "maxLeverage": 4.0, + "info": { + "bracket": "6", + "initialLeverage": "4", + "notionalCap": "5000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.125", + "cum": "50395.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 5000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "7", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "5000000", + "maintMarginRatio": "0.25", + "cum": "675395.0" + } + }, + { + "tier": 8.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "8", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "2175395.0" } } ], @@ -18770,10 +22400,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 10.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "10", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -18786,10 +22416,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 8.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "8", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -18800,13 +22430,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 200000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 6.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "6", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "200000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -18815,33 +22445,33 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 200000.0, + "maxNotional": 500000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "500000", + "notionalFloor": "200000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "10650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, + "minNotional": 500000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", + "initialLeverage": "4", "notionalCap": "1000000", - "notionalFloor": "250000", + "notionalFloor": "500000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "23150.0" } }, { @@ -18849,15 +22479,31 @@ "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 3000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "3000000", + "notionalFloor": "1000000", + "maintMarginRatio": "0.25", + "cum": "148150.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 3000000.0, + "maxNotional": 5000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "3000000", - "notionalFloor": "1000000", + "notionalCap": "5000000", + "notionalFloor": "3000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "898150.0" } } ], @@ -18966,10 +22612,10 @@ "minNotional": 0.0, "maxNotional": 5000.0, "maintenanceMarginRate": 0.02, - "maxLeverage": 20.0, + "maxLeverage": 25.0, "info": { "bracket": "1", - "initialLeverage": "20", + "initialLeverage": "25", "notionalCap": "5000", "notionalFloor": "0", "maintMarginRatio": "0.02", @@ -18982,10 +22628,10 @@ "minNotional": 5000.0, "maxNotional": 25000.0, "maintenanceMarginRate": 0.025, - "maxLeverage": 10.0, + "maxLeverage": 20.0, "info": { "bracket": "2", - "initialLeverage": "10", + "initialLeverage": "20", "notionalCap": "25000", "notionalFloor": "5000", "maintMarginRatio": "0.025", @@ -18996,13 +22642,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, - "maxLeverage": 8.0, + "maxLeverage": 10.0, "info": { "bracket": "3", - "initialLeverage": "8", - "notionalCap": "100000", + "initialLeverage": "10", + "notionalCap": "400000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "650.0" @@ -19011,49 +22657,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 400000.0, + "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "1000000", + "notionalFloor": "400000", "maintMarginRatio": "0.1", - "cum": "5650.0" + "cum": "20650.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, + "minNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.125", - "cum": "11900.0" + "cum": "45650.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 3000000.0, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295650.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "3000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386900.0" + "cum": "1795650.0" } } ], @@ -19388,144 +23050,160 @@ "tier": 1.0, "currency": "USDT", "minNotional": 0.0, - "maxNotional": 10000.0, - "maintenanceMarginRate": 0.0065, - "maxLeverage": 50.0, + "maxNotional": 5000.0, + "maintenanceMarginRate": 0.005, + "maxLeverage": 75.0, "info": { "bracket": "1", - "initialLeverage": "50", - "notionalCap": "10000", + "initialLeverage": "75", + "notionalCap": "5000", "notionalFloor": "0", - "maintMarginRatio": "0.0065", + "maintMarginRatio": "0.005", "cum": "0.0" } }, { "tier": 2.0, "currency": "USDT", + "minNotional": 5000.0, + "maxNotional": 10000.0, + "maintenanceMarginRate": 0.0065, + "maxLeverage": 50.0, + "info": { + "bracket": "2", + "initialLeverage": "50", + "notionalCap": "10000", + "notionalFloor": "5000", + "maintMarginRatio": "0.0065", + "cum": "7.5" + } + }, + { + "tier": 3.0, + "currency": "USDT", "minNotional": 10000.0, "maxNotional": 50000.0, "maintenanceMarginRate": 0.01, "maxLeverage": 40.0, "info": { - "bracket": "2", + "bracket": "3", "initialLeverage": "40", "notionalCap": "50000", "notionalFloor": "10000", "maintMarginRatio": "0.01", - "cum": "35.0" + "cum": "42.5" } }, { - "tier": 3.0, + "tier": 4.0, "currency": "USDT", "minNotional": 50000.0, "maxNotional": 250000.0, "maintenanceMarginRate": 0.02, "maxLeverage": 25.0, "info": { - "bracket": "3", + "bracket": "4", "initialLeverage": "25", "notionalCap": "250000", "notionalFloor": "50000", "maintMarginRatio": "0.02", - "cum": "535.0" + "cum": "542.5" } }, { - "tier": 4.0, + "tier": 5.0, "currency": "USDT", "minNotional": 250000.0, "maxNotional": 1000000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { - "bracket": "4", + "bracket": "5", "initialLeverage": "10", "notionalCap": "1000000", "notionalFloor": "250000", "maintMarginRatio": "0.05", - "cum": "8035.0" + "cum": "8042.5" } }, { - "tier": 5.0, + "tier": 6.0, "currency": "USDT", "minNotional": 1000000.0, "maxNotional": 2000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { - "bracket": "5", + "bracket": "6", "initialLeverage": "5", "notionalCap": "2000000", "notionalFloor": "1000000", "maintMarginRatio": "0.1", - "cum": "58035.0" + "cum": "58042.5" } }, { - "tier": 6.0, + "tier": 7.0, "currency": "USDT", "minNotional": 2000000.0, "maxNotional": 5000000.0, "maintenanceMarginRate": 0.125, "maxLeverage": 4.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "4", "notionalCap": "5000000", "notionalFloor": "2000000", "maintMarginRatio": "0.125", - "cum": "108035.0" + "cum": "108042.5" } }, { - "tier": 7.0, + "tier": 8.0, "currency": "USDT", "minNotional": 5000000.0, "maxNotional": 10000000.0, "maintenanceMarginRate": 0.15, "maxLeverage": 3.0, "info": { - "bracket": "7", + "bracket": "8", "initialLeverage": "3", "notionalCap": "10000000", "notionalFloor": "5000000", "maintMarginRatio": "0.15", - "cum": "233035.0" + "cum": "233042.5" } }, { - "tier": 8.0, + "tier": 9.0, "currency": "USDT", "minNotional": 10000000.0, "maxNotional": 20000000.0, "maintenanceMarginRate": 0.25, "maxLeverage": 2.0, "info": { - "bracket": "8", + "bracket": "9", "initialLeverage": "2", "notionalCap": "20000000", "notionalFloor": "10000000", "maintMarginRatio": "0.25", - "cum": "1233035.0" + "cum": "1233042.5" } }, { - "tier": 9.0, + "tier": 10.0, "currency": "USDT", "minNotional": 20000000.0, "maxNotional": 50000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "9", + "bracket": "10", "initialLeverage": "1", "notionalCap": "50000000", "notionalFloor": "20000000", "maintMarginRatio": "0.5", - "cum": "6233035.0" + "cum": "6233042.5" } } ], @@ -19680,13 +23358,13 @@ "tier": 3.0, "currency": "USDT", "minNotional": 25000.0, - "maxNotional": 100000.0, + "maxNotional": 400000.0, "maintenanceMarginRate": 0.05, "maxLeverage": 10.0, "info": { "bracket": "3", "initialLeverage": "10", - "notionalCap": "100000", + "notionalCap": "400000", "notionalFloor": "25000", "maintMarginRatio": "0.05", "cum": "700.0" @@ -19695,49 +23373,65 @@ { "tier": 4.0, "currency": "USDT", - "minNotional": 100000.0, - "maxNotional": 250000.0, + "minNotional": 400000.0, + "maxNotional": 1000000.0, "maintenanceMarginRate": 0.1, "maxLeverage": 5.0, "info": { "bracket": "4", "initialLeverage": "5", - "notionalCap": "250000", - "notionalFloor": "100000", + "notionalCap": "1000000", + "notionalFloor": "400000", "maintMarginRatio": "0.1", - "cum": "5700.0" + "cum": "20700.0" } }, { "tier": 5.0, "currency": "USDT", - "minNotional": 250000.0, - "maxNotional": 1000000.0, + "minNotional": 1000000.0, + "maxNotional": 2000000.0, "maintenanceMarginRate": 0.125, - "maxLeverage": 2.0, + "maxLeverage": 4.0, "info": { "bracket": "5", - "initialLeverage": "2", - "notionalCap": "1000000", - "notionalFloor": "250000", + "initialLeverage": "4", + "notionalCap": "2000000", + "notionalFloor": "1000000", "maintMarginRatio": "0.125", - "cum": "11950.0" + "cum": "45700.0" } }, { "tier": 6.0, "currency": "USDT", - "minNotional": 1000000.0, - "maxNotional": 5000000.0, + "minNotional": 2000000.0, + "maxNotional": 6000000.0, + "maintenanceMarginRate": 0.25, + "maxLeverage": 2.0, + "info": { + "bracket": "6", + "initialLeverage": "2", + "notionalCap": "6000000", + "notionalFloor": "2000000", + "maintMarginRatio": "0.25", + "cum": "295700.0" + } + }, + { + "tier": 7.0, + "currency": "USDT", + "minNotional": 6000000.0, + "maxNotional": 10000000.0, "maintenanceMarginRate": 0.5, "maxLeverage": 1.0, "info": { - "bracket": "6", + "bracket": "7", "initialLeverage": "1", - "notionalCap": "5000000", - "notionalFloor": "1000000", + "notionalCap": "10000000", + "notionalFloor": "6000000", "maintMarginRatio": "0.5", - "cum": "386950.0" + "cum": "1795700.0" } } ], diff --git a/freqtrade/exchange/bybit.py b/freqtrade/exchange/bybit.py index d14c7c192..a4b070741 100644 --- a/freqtrade/exchange/bybit.py +++ b/freqtrade/exchange/bybit.py @@ -1,9 +1,16 @@ """ Bybit exchange subclass """ import logging -from typing import Dict, List, Tuple +from datetime import datetime +from typing import Any, Dict, List, Optional, Tuple -from freqtrade.enums import MarginMode, TradingMode +import ccxt + +from freqtrade.constants import BuySell +from freqtrade.enums import MarginMode, PriceType, TradingMode +from freqtrade.exceptions import DDosProtection, OperationalException, TemporaryError from freqtrade.exchange import Exchange +from freqtrade.exchange.common import retrier +from freqtrade.exchange.exchange_utils import timeframe_to_msecs logger = logging.getLogger(__name__) @@ -20,18 +27,27 @@ class Bybit(Exchange): """ _ft_has: Dict = { - "ohlcv_candle_limit": 1000, - "ccxt_futures_name": "linear", + "ohlcv_candle_limit": 200, "ohlcv_has_history": False, } _ft_has_futures: Dict = { "ohlcv_has_history": True, + "mark_ohlcv_timeframe": "4h", + "funding_fee_timeframe": "8h", + "stoploss_on_exchange": True, + "stoploss_order_types": {"limit": "limit", "market": "market"}, + "stop_price_type_field": "triggerBy", + "stop_price_type_value_mapping": { + PriceType.LAST: "LastPrice", + PriceType.MARK: "MarkPrice", + PriceType.INDEX: "IndexPrice", + }, } _supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [ # TradingMode.SPOT always supported and not required in this list # (TradingMode.FUTURES, MarginMode.CROSS), - # (TradingMode.FUTURES, MarginMode.ISOLATED) + (TradingMode.FUTURES, MarginMode.ISOLATED) ] @property @@ -47,3 +63,158 @@ class Bybit(Exchange): }) config.update(super()._ccxt_config) return config + + def market_is_future(self, market: Dict[str, Any]) -> bool: + main = super().market_is_future(market) + # For ByBit, we'll only support USDT markets for now. + return ( + main and market['settle'] == 'USDT' + ) + + @retrier + def additional_exchange_init(self) -> None: + """ + Additional exchange initialization logic. + .api will be available at this point. + Must be overridden in child methods if required. + """ + try: + if self.trading_mode == TradingMode.FUTURES and not self._config['dry_run']: + position_mode = self._api.set_position_mode(False) + self._log_exchange_response('set_position_mode', position_mode) + except ccxt.DDoSProtection as e: + raise DDosProtection(e) from e + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + f'Error in additional_exchange_init due to {e.__class__.__name__}. Message: {e}' + ) from e + except ccxt.BaseError as e: + raise OperationalException(e) from e + + async def _fetch_funding_rate_history( + self, + pair: str, + timeframe: str, + limit: int, + since_ms: Optional[int] = None, + ) -> List[List]: + """ + Fetch funding rate history + Necessary workaround until https://github.com/ccxt/ccxt/issues/15990 is fixed. + """ + params = {} + if since_ms: + until = since_ms + (timeframe_to_msecs(timeframe) * self._ft_has['ohlcv_candle_limit']) + params.update({'until': until}) + # Funding rate + data = await self._api_async.fetch_funding_rate_history( + pair, since=since_ms, + params=params) + # Convert funding rate to candle pattern + data = [[x['timestamp'], x['fundingRate'], 0, 0, 0, 0] for x in data] + return data + + def _lev_prep(self, pair: str, leverage: float, side: BuySell, accept_fail: bool = False): + if self.trading_mode != TradingMode.SPOT: + params = {'leverage': leverage} + self.set_margin_mode(pair, self.margin_mode, accept_fail=True, params=params) + self._set_leverage(leverage, pair, accept_fail=True) + + def _get_params( + self, + side: BuySell, + ordertype: str, + leverage: float, + reduceOnly: bool, + time_in_force: str = 'GTC', + ) -> Dict: + params = super()._get_params( + side=side, + ordertype=ordertype, + leverage=leverage, + reduceOnly=reduceOnly, + time_in_force=time_in_force, + ) + if self.trading_mode == TradingMode.FUTURES and self.margin_mode: + params['position_idx'] = 0 + return params + + def dry_run_liquidation_price( + self, + pair: str, + open_rate: float, # Entry price of position + is_short: bool, + amount: float, + stake_amount: float, + leverage: float, + wallet_balance: float, # Or margin balance + mm_ex_1: float = 0.0, # (Binance) Cross only + upnl_ex_1: float = 0.0, # (Binance) Cross only + ) -> Optional[float]: + """ + Important: Must be fetching data from cached values as this is used by backtesting! + PERPETUAL: + bybit: + https://www.bybithelp.com/HelpCenterKnowledge/bybitHC_Article?language=en_US&id=000001067 + + Long: + Liquidation Price = ( + Entry Price * (1 - Initial Margin Rate + Maintenance Margin Rate) + - Extra Margin Added/ Contract) + Short: + Liquidation Price = ( + Entry Price * (1 + Initial Margin Rate - Maintenance Margin Rate) + + Extra Margin Added/ Contract) + + Implementation Note: Extra margin is currently not used. + + :param pair: Pair to calculate liquidation price for + :param open_rate: Entry price of position + :param is_short: True if the trade is a short, false otherwise + :param amount: Absolute value of position size incl. leverage (in base currency) + :param stake_amount: Stake amount - Collateral in settle currency. + :param leverage: Leverage used for this position. + :param trading_mode: SPOT, MARGIN, FUTURES, etc. + :param margin_mode: Either ISOLATED or CROSS + :param wallet_balance: Amount of margin_mode in the wallet being used to trade + Cross-Margin Mode: crossWalletBalance + Isolated-Margin Mode: isolatedWalletBalance + """ + + market = self.markets[pair] + mm_ratio, _ = self.get_maintenance_ratio_and_amt(pair, stake_amount) + + if self.trading_mode == TradingMode.FUTURES and self.margin_mode == MarginMode.ISOLATED: + + if market['inverse']: + raise OperationalException( + "Freqtrade does not yet support inverse contracts") + initial_margin_rate = 1 / leverage + + # See docstring - ignores extra margin! + if is_short: + return open_rate * (1 + initial_margin_rate - mm_ratio) + else: + return open_rate * (1 - initial_margin_rate + mm_ratio) + + else: + raise OperationalException( + "Freqtrade only supports isolated futures for leverage trading") + + def get_funding_fees( + self, pair: str, amount: float, is_short: bool, open_date: datetime) -> float: + """ + Fetch funding fees, either from the exchange (live) or calculates them + based on funding rate/mark price history + :param pair: The quote/base pair of the trade + :param is_short: trade direction + :param amount: Trade amount + :param open_date: Open date of the trade + :return: funding fee since open_date + :raises: ExchangeError if something goes wrong. + """ + # Bybit does not provide "applied" funding fees per position. + if self.trading_mode == TradingMode.FUTURES: + return self._fetch_and_calculate_funding_fees( + pair, amount, is_short, open_date) + return 0.0 diff --git a/freqtrade/exchange/common.py b/freqtrade/exchange/common.py index 6d09c4f95..42a7094ba 100644 --- a/freqtrade/exchange/common.py +++ b/freqtrade/exchange/common.py @@ -46,13 +46,13 @@ MAP_EXCHANGE_CHILDCLASS = { 'binanceje': 'binance', 'binanceusdm': 'binance', 'okex': 'okx', - 'gate': 'gateio', + 'gateio': 'gate', } SUPPORTED_EXCHANGES = [ 'binance', 'bittrex', - 'gateio', + 'gate', 'huobi', 'kraken', 'okx', diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index c72f9479d..104eaa221 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -3,11 +3,11 @@ Cryptocurrency Exchanges support """ import asyncio -import http import inspect import logging from copy import deepcopy from datetime import datetime, timedelta, timezone +from math import floor from threading import Lock from typing import Any, Coroutine, Dict, List, Literal, Optional, Tuple, Union @@ -21,9 +21,10 @@ from pandas import DataFrame, concat from freqtrade.constants import (DEFAULT_AMOUNT_RESERVE_PERCENT, NON_OPEN_EXCHANGE_STATES, BidAsk, BuySell, Config, EntryExit, ListPairsWithTimeframes, MakerTaker, - PairWithTimeframe) + OBLiteral, PairWithTimeframe) from freqtrade.data.converter import clean_ohlcv_dataframe, ohlcv_to_dataframe, trades_dict_to_list from freqtrade.enums import OPTIMIZE_MODES, CandleType, MarginMode, TradingMode +from freqtrade.enums.pricetype import PriceType from freqtrade.exceptions import (DDosProtection, ExchangeError, InsufficientFundsError, InvalidOrderException, OperationalException, PricingError, RetryableOrderError, TemporaryError) @@ -36,7 +37,7 @@ from freqtrade.exchange.exchange_utils import (CcxtModuleType, amount_to_contrac price_to_precision, timeframe_to_minutes, timeframe_to_msecs, timeframe_to_next_date, timeframe_to_prev_date, timeframe_to_seconds) -from freqtrade.exchange.types import OHLCVResponse, Ticker, Tickers +from freqtrade.exchange.types import OHLCVResponse, OrderBook, Ticker, Tickers from freqtrade.misc import (chunks, deep_merge_dicts, file_dump_json, file_load_json, safe_value_fallback2) from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist @@ -45,12 +46,6 @@ from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist logger = logging.getLogger(__name__) -# Workaround for adding samesite support to pre 3.8 python -# Only applies to python3.7, and only on certain exchanges (kraken) -# Replicates the fix from starlette (which is actually causing this problem) -http.cookies.Morsel._reserved["samesite"] = "SameSite" # type: ignore - - class Exchange: # Parameters to add directly to buy/sell calls (like agreeing to trading agreement) @@ -65,7 +60,6 @@ class Exchange: _ft_has_default: Dict = { "stoploss_on_exchange": False, "order_time_in_force": ["GTC"], - "time_in_force_parameter": "timeInForce", "ohlcv_params": {}, "ohlcv_candle_limit": 500, "ohlcv_has_history": True, # Some exchanges (Kraken) don't provide history via ohlcv @@ -74,6 +68,7 @@ class Exchange: # Check https://github.com/ccxt/ccxt/issues/10767 for removal of ohlcv_volume_currency "ohlcv_volume_currency": "base", # "base" or "quote" "tickers_have_quoteVolume": True, + "tickers_have_bid_ask": True, # bid / ask empty for fetch_tickers "tickers_have_price": True, "trades_pagination": "time", # Possible are "time" or "id" "trades_pagination_arg": "since", @@ -606,12 +601,27 @@ class Exchange: if not self.exchange_has('createMarketOrder'): raise OperationalException( f'Exchange {self.name} does not support market orders.') + self.validate_stop_ordertypes(order_types) + def validate_stop_ordertypes(self, order_types: Dict) -> None: + """ + Validate stoploss order types + """ if (order_types.get("stoploss_on_exchange") and not self._ft_has.get("stoploss_on_exchange", False)): raise OperationalException( f'On exchange stoploss is not supported for {self.name}.' ) + if self.trading_mode == TradingMode.FUTURES: + price_mapping = self._ft_has.get('stop_price_type_value_mapping', {}).keys() + if ( + order_types.get("stoploss_on_exchange", False) is True + and 'stoploss_price_type' in order_types + and order_types['stoploss_price_type'] not in price_mapping + ): + raise OperationalException( + f'On exchange stoploss price type is not supported for {self.name}.' + ) def validate_pricing(self, pricing: Dict) -> None: if pricing.get('use_order_book', False) and not self.exchange_has('fetchL2OrderBook'): @@ -682,7 +692,7 @@ class Exchange: f"Freqtrade does not support {mm_value} {trading_mode.value} on {self.name}" ) - def get_option(self, param: str, default: Any = None) -> Any: + def get_option(self, param: str, default: Optional[Any] = None) -> Any: """ Get parameter value from _ft_has """ @@ -840,7 +850,7 @@ class Exchange: 'remaining': _amount, 'datetime': arrow.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ'), 'timestamp': arrow.utcnow().int_timestamp * 1000, - 'status': "closed" if ordertype == "market" and not stop_loss else "open", + 'status': "open", 'fee': None, 'info': {}, 'leverage': leverage @@ -850,20 +860,33 @@ class Exchange: dry_order["stopPrice"] = dry_order["price"] # Workaround to avoid filling stoploss orders immediately dry_order["ft_order_type"] = "stoploss" + orderbook: Optional[OrderBook] = None + if self.exchange_has('fetchL2OrderBook'): + orderbook = self.fetch_l2_order_book(pair, 20) + if ordertype == "limit" and orderbook: + # Allow a 3% price difference + allowed_diff = 0.03 + if self._dry_is_price_crossed(pair, side, rate, orderbook, allowed_diff): + logger.info( + f"Converted order {pair} to market order due to price {rate} crossing spread " + f"by more than {allowed_diff:.2%}.") + dry_order["type"] = "market" if dry_order["type"] == "market" and not dry_order.get("ft_order_type"): # Update market order pricing - average = self.get_dry_market_fill_price(pair, side, amount, rate) + average = self.get_dry_market_fill_price(pair, side, amount, rate, orderbook) dry_order.update({ 'average': average, 'filled': _amount, 'remaining': 0.0, + 'status': "closed", 'cost': (dry_order['amount'] * average) / leverage }) # market orders will always incurr taker fees dry_order = self.add_dry_order_fee(pair, dry_order, 'taker') - dry_order = self.check_dry_limit_order_filled(dry_order, immediate=True) + dry_order = self.check_dry_limit_order_filled( + dry_order, immediate=True, orderbook=orderbook) self._dry_run_open_orders[dry_order["id"]] = dry_order # Copy order and close it - so the returned order is open unless it's a market order @@ -885,20 +908,22 @@ class Exchange: }) return dry_order - def get_dry_market_fill_price(self, pair: str, side: str, amount: float, rate: float) -> float: + def get_dry_market_fill_price(self, pair: str, side: str, amount: float, rate: float, + orderbook: Optional[OrderBook]) -> float: """ Get the market order fill price based on orderbook interpolation """ if self.exchange_has('fetchL2OrderBook'): - ob = self.fetch_l2_order_book(pair, 20) - ob_type = 'asks' if side == 'buy' else 'bids' + if not orderbook: + orderbook = self.fetch_l2_order_book(pair, 20) + ob_type: OBLiteral = 'asks' if side == 'buy' else 'bids' slippage = 0.05 max_slippage_val = rate * ((1 + slippage) if side == 'buy' else (1 - slippage)) remaining_amount = amount filled_amount = 0.0 book_entry_price = 0.0 - for book_entry in ob[ob_type]: + for book_entry in orderbook[ob_type]: book_entry_price = book_entry[0] book_entry_coin_volume = book_entry[1] if remaining_amount > 0: @@ -926,20 +951,20 @@ class Exchange: return rate - def _is_dry_limit_order_filled(self, pair: str, side: str, limit: float) -> bool: + def _dry_is_price_crossed(self, pair: str, side: str, limit: float, + orderbook: Optional[OrderBook] = None, offset: float = 0.0) -> bool: if not self.exchange_has('fetchL2OrderBook'): return True - ob = self.fetch_l2_order_book(pair, 1) + if not orderbook: + orderbook = self.fetch_l2_order_book(pair, 1) try: if side == 'buy': - price = ob['asks'][0][0] - logger.debug(f"{pair} checking dry buy-order: price={price}, limit={limit}") - if limit >= price: + price = orderbook['asks'][0][0] + if limit * (1 - offset) >= price: return True else: - price = ob['bids'][0][0] - logger.debug(f"{pair} checking dry sell-order: price={price}, limit={limit}") - if limit <= price: + price = orderbook['bids'][0][0] + if limit * (1 + offset) <= price: return True except IndexError: # Ignore empty orderbooks when filling - can be filled with the next iteration. @@ -947,7 +972,8 @@ class Exchange: return False def check_dry_limit_order_filled( - self, order: Dict[str, Any], immediate: bool = False) -> Dict[str, Any]: + self, order: Dict[str, Any], immediate: bool = False, + orderbook: Optional[OrderBook] = None) -> Dict[str, Any]: """ Check dry-run limit order fill and update fee (if it filled). """ @@ -955,7 +981,7 @@ class Exchange: and order['type'] in ["limit"] and not order.get('ft_order_type')): pair = order['symbol'] - if self._is_dry_limit_order_filled(pair, order['side'], order['price']): + if self._dry_is_price_crossed(pair, order['side'], order['price'], orderbook): order.update({ 'status': 'closed', 'filled': order['amount'], @@ -992,10 +1018,10 @@ class Exchange: # Order handling - def _lev_prep(self, pair: str, leverage: float, side: BuySell): + def _lev_prep(self, pair: str, leverage: float, side: BuySell, accept_fail: bool = False): if self.trading_mode != TradingMode.SPOT: - self.set_margin_mode(pair, self.margin_mode) - self._set_leverage(leverage, pair) + self.set_margin_mode(pair, self.margin_mode, accept_fail) + self._set_leverage(leverage, pair, accept_fail) def _get_params( self, @@ -1007,8 +1033,7 @@ class Exchange: ) -> Dict: params = self._params.copy() if time_in_force != 'GTC' and ordertype != 'market': - param = self._ft_has.get('time_in_force_parameter', '') - params.update({param: time_in_force.upper()}) + params.update({'timeInForce': time_in_force.upper()}) if reduceOnly: params.update({'reduceOnly': True}) return params @@ -1060,7 +1085,7 @@ class Exchange: f'Tried to {side} amount {amount} at rate {rate}.' f'Message: {e}') from e except ccxt.InvalidOrder as e: - raise ExchangeError( + raise InvalidOrderException( f'Could not create {ordertype} {side} order on market {pair}. ' f'Tried to {side} amount {amount} at rate {rate}. ' f'Message: {e}') from e @@ -1110,8 +1135,15 @@ class Exchange: "sell" else (stop_price >= limit_rate)) # Ensure rate is less than stop price if bad_stop_price: - raise OperationalException( - 'In stoploss limit order, stop price should be more than limit price') + # This can for example happen if the stop / liquidation price is set to 0 + # Which is possible if a market-order closes right away. + # The InvalidOrderException will bubble up to exit_positions, where it will be + # handled gracefully. + raise InvalidOrderException( + "In stoploss limit order, stop price should be more than limit price. " + f"Stop price: {stop_price}, Limit price: {limit_rate}, " + f"Limit Price pct: {limit_price_pct}" + ) return limit_rate def _get_stop_params(self, side: BuySell, ordertype: str, stop_price: float) -> Dict: @@ -1121,8 +1153,8 @@ class Exchange: return params @retrier(retries=0) - def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict, - side: BuySell, leverage: float) -> Dict: + def create_stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict, + side: BuySell, leverage: float) -> Dict: """ creates a stoploss order. requires `_ft_has['stoploss_order_types']` to be set as a dict mapping limit and market @@ -1167,10 +1199,14 @@ class Exchange: stop_price=stop_price_norm) if self.trading_mode == TradingMode.FUTURES: params['reduceOnly'] = True + if 'stoploss_price_type' in order_types and 'stop_price_type_field' in self._ft_has: + price_type = self._ft_has['stop_price_type_value_mapping'][ + order_types.get('stoploss_price_type', PriceType.LAST)] + params[self._ft_has['stop_price_type_field']] = price_type amount = self.amount_to_precision(pair, self._amount_to_contracts(pair, amount)) - self._lev_prep(pair, leverage, side) + self._lev_prep(pair, leverage, side, accept_fail=True) order = self._api.create_order(symbol=pair, type=ordertype, side=side, amount=amount, price=limit_rate, params=params) self._log_exchange_response('create_stoploss_order', order) @@ -1357,7 +1393,7 @@ class Exchange: raise OperationalException(e) from e @retrier - def fetch_positions(self, pair: str = None) -> List[Dict]: + def fetch_positions(self, pair: Optional[str] = None) -> List[Dict]: """ Fetch positions from the exchange. If no pair is given, all positions are returned. @@ -1497,7 +1533,7 @@ class Exchange: return result @retrier - def fetch_l2_order_book(self, pair: str, limit: int = 100) -> dict: + def fetch_l2_order_book(self, pair: str, limit: int = 100) -> OrderBook: """ Get L2 order book from exchange. Can be limited to a certain amount (if supported). @@ -1540,7 +1576,7 @@ class Exchange: def get_rate(self, pair: str, refresh: bool, side: EntryExit, is_short: bool, - order_book: Optional[dict] = None, ticker: Optional[Ticker] = None) -> float: + order_book: Optional[OrderBook] = None, ticker: Optional[Ticker] = None) -> float: """ Calculates bid/ask target bid rate - between current ask price and last price @@ -1578,7 +1614,8 @@ class Exchange: logger.debug('order_book %s', order_book) # top 1 = index 0 try: - rate = order_book[f"{price_side}s"][order_book_top - 1][0] + obside: OBLiteral = 'bids' if price_side == 'bid' else 'asks' + rate = order_book[obside][order_book_top - 1][0] except (IndexError, KeyError) as e: logger.warning( f"{pair} - {name} Price at location {order_book_top} from orderbook " @@ -1801,7 +1838,7 @@ class Exchange: def get_historic_ohlcv(self, pair: str, timeframe: str, since_ms: int, candle_type: CandleType, is_new_pair: bool = False, - until_ms: int = None) -> List: + until_ms: Optional[int] = None) -> List: """ Get candle history using asyncio and returns the list of candles. Handles all async work for this. @@ -1930,7 +1967,8 @@ class Exchange: cache: bool, drop_incomplete: bool) -> DataFrame: # keeping last candle time as last refreshed time of the pair if ticks and cache: - self._pairs_last_refresh_time[(pair, timeframe, c_type)] = ticks[-1][0] // 1000 + idx = -2 if drop_incomplete and len(ticks) > 1 else -1 + self._pairs_last_refresh_time[(pair, timeframe, c_type)] = ticks[idx][0] // 1000 # keeping parsed dataframe in cache ohlcv_df = ohlcv_to_dataframe(ticks, timeframe, pair=pair, fill_missing=True, drop_incomplete=drop_incomplete) @@ -1984,9 +2022,9 @@ class Exchange: continue # Deconstruct tuple (has 5 elements) pair, timeframe, c_type, ticks, drop_hint = res - drop_incomplete = drop_hint if drop_incomplete is None else drop_incomplete + drop_incomplete_ = drop_hint if drop_incomplete is None else drop_incomplete ohlcv_df = self._process_ohlcv_df( - pair, timeframe, c_type, ticks, cache, drop_incomplete) + pair, timeframe, c_type, ticks, cache, drop_incomplete_) results_df[(pair, timeframe, c_type)] = ohlcv_df @@ -2003,7 +2041,9 @@ class Exchange: # Timeframe in seconds interval_in_sec = timeframe_to_seconds(timeframe) plr = self._pairs_last_refresh_time.get((pair, timeframe, candle_type), 0) + interval_in_sec - return plr < arrow.utcnow().int_timestamp + # current,active candle open date + now = int(timeframe_to_prev_date(timeframe).timestamp()) + return plr < now @retrier_async async def _async_get_candle_history( @@ -2491,7 +2531,7 @@ class Exchange: self, leverage: float, pair: Optional[str] = None, - trading_mode: Optional[TradingMode] = None + accept_fail: bool = False, ): """ Set's the leverage before making a trade, in order to not @@ -2500,12 +2540,18 @@ class Exchange: if self._config['dry_run'] or not self.exchange_has("setLeverage"): # Some exchanges only support one margin_mode type return - + if self._ft_has.get('floor_leverage', False) is True: + # Rounding for binance ... + leverage = floor(leverage) try: res = self._api.set_leverage(symbol=pair, leverage=leverage) self._log_exchange_response('set_leverage', res) except ccxt.DDoSProtection as e: raise DDosProtection(e) from e + except (ccxt.BadRequest, ccxt.InsufficientFunds) as e: + if not accept_fail: + raise TemporaryError( + f'Could not set leverage due to {e.__class__.__name__}. Message: {e}') from e except (ccxt.NetworkError, ccxt.ExchangeError) as e: raise TemporaryError( f'Could not set leverage due to {e.__class__.__name__}. Message: {e}') from e @@ -2527,7 +2573,8 @@ class Exchange: return open_date.minute > 0 or open_date.second > 0 @retrier - def set_margin_mode(self, pair: str, margin_mode: MarginMode, params: dict = {}): + def set_margin_mode(self, pair: str, margin_mode: MarginMode, accept_fail: bool = False, + params: dict = {}): """ Set's the margin mode on the exchange to cross or isolated for a specific pair :param pair: base/quote currency pair (e.g. "ADA/USDT") @@ -2541,6 +2588,10 @@ class Exchange: self._log_exchange_response('set_margin_mode', res) except ccxt.DDoSProtection as e: raise DDosProtection(e) from e + except ccxt.BadRequest as e: + if not accept_fail: + raise TemporaryError( + f'Could not set margin mode due to {e.__class__.__name__}. Message: {e}') from e except (ccxt.NetworkError, ccxt.ExchangeError) as e: raise TemporaryError( f'Could not set margin mode due to {e.__class__.__name__}. Message: {e}') from e @@ -2674,7 +2725,7 @@ class Exchange: :param amount: Trade amount :param open_date: Open date of the trade :return: funding fee since open_date - :raies: ExchangeError if something goes wrong. + :raises: ExchangeError if something goes wrong. """ if self.trading_mode == TradingMode.FUTURES: if self._config['dry_run']: @@ -2694,6 +2745,7 @@ class Exchange: is_short: bool, amount: float, # Absolute value of position size stake_amount: float, + leverage: float, wallet_balance: float, mm_ex_1: float = 0.0, # (Binance) Cross only upnl_ex_1: float = 0.0, # (Binance) Cross only @@ -2707,14 +2759,15 @@ class Exchange: raise OperationalException( f"{self.name} does not support {self.margin_mode} {self.trading_mode}") - isolated_liq = None + liquidation_price = None if self._config['dry_run'] or not self.exchange_has("fetchPositions"): - isolated_liq = self.dry_run_liquidation_price( + liquidation_price = self.dry_run_liquidation_price( pair=pair, open_rate=open_rate, is_short=is_short, amount=amount, + leverage=leverage, stake_amount=stake_amount, wallet_balance=wallet_balance, mm_ex_1=mm_ex_1, @@ -2724,16 +2777,16 @@ class Exchange: positions = self.fetch_positions(pair) if len(positions) > 0: pos = positions[0] - isolated_liq = pos['liquidationPrice'] + liquidation_price = pos['liquidationPrice'] - if isolated_liq: - buffer_amount = abs(open_rate - isolated_liq) * self.liquidation_buffer - isolated_liq = ( - isolated_liq - buffer_amount + if liquidation_price is not None: + buffer_amount = abs(open_rate - liquidation_price) * self.liquidation_buffer + liquidation_price_buffer = ( + liquidation_price - buffer_amount if is_short else - isolated_liq + buffer_amount + liquidation_price + buffer_amount ) - return isolated_liq + return max(liquidation_price_buffer, 0.0) else: return None @@ -2744,6 +2797,7 @@ class Exchange: is_short: bool, amount: float, stake_amount: float, + leverage: float, wallet_balance: float, # Or margin balance mm_ex_1: float = 0.0, # (Binance) Cross only upnl_ex_1: float = 0.0, # (Binance) Cross only @@ -2751,7 +2805,7 @@ class Exchange: """ Important: Must be fetching data from cached values as this is used by backtesting! PERPETUAL: - gateio: https://www.gate.io/help/futures/futures/27724/liquidation-price-bankruptcy-price + gate: https://www.gate.io/help/futures/futures/27724/liquidation-price-bankruptcy-price > Liquidation Price = (Entry Price ± Margin / Contract Multiplier / Size) / [ 1 ± (Maintenance Margin Ratio + Taker Rate)] Wherein, "+" or "-" depends on whether the contract goes long or short: @@ -2765,13 +2819,14 @@ class Exchange: :param is_short: True if the trade is a short, false otherwise :param amount: Absolute value of position size incl. leverage (in base currency) :param stake_amount: Stake amount - Collateral in settle currency. + :param leverage: Leverage used for this position. :param trading_mode: SPOT, MARGIN, FUTURES, etc. :param margin_mode: Either ISOLATED or CROSS :param wallet_balance: Amount of margin_mode in the wallet being used to trade Cross-Margin Mode: crossWalletBalance Isolated-Margin Mode: isolatedWalletBalance - # * Not required by Gateio or OKX + # * Not required by Gate or OKX :param mm_ex_1: :param upnl_ex_1: """ diff --git a/freqtrade/exchange/exchange_utils.py b/freqtrade/exchange/exchange_utils.py index cb6333869..6d3371a59 100644 --- a/freqtrade/exchange/exchange_utils.py +++ b/freqtrade/exchange/exchange_utils.py @@ -15,18 +15,19 @@ from freqtrade.util import FtPrecise CcxtModuleType = Any -def is_exchange_known_ccxt(exchange_name: str, ccxt_module: CcxtModuleType = None) -> bool: +def is_exchange_known_ccxt( + exchange_name: str, ccxt_module: Optional[CcxtModuleType] = None) -> bool: return exchange_name in ccxt_exchanges(ccxt_module) -def ccxt_exchanges(ccxt_module: CcxtModuleType = None) -> List[str]: +def ccxt_exchanges(ccxt_module: Optional[CcxtModuleType] = None) -> List[str]: """ Return the list of all exchanges known to ccxt """ return ccxt_module.exchanges if ccxt_module is not None else ccxt.exchanges -def available_exchanges(ccxt_module: CcxtModuleType = None) -> List[str]: +def available_exchanges(ccxt_module: Optional[CcxtModuleType] = None) -> List[str]: """ Return exchanges available to the bot, i.e. non-bad exchanges in the ccxt list """ @@ -86,7 +87,7 @@ def timeframe_to_msecs(timeframe: str) -> int: return ccxt.Exchange.parse_timeframe(timeframe) * 1000 -def timeframe_to_prev_date(timeframe: str, date: datetime = None) -> datetime: +def timeframe_to_prev_date(timeframe: str, date: Optional[datetime] = None) -> datetime: """ Use Timeframe and determine the candle start date for this date. Does not round when given a candle start date. @@ -102,7 +103,7 @@ def timeframe_to_prev_date(timeframe: str, date: datetime = None) -> datetime: return datetime.fromtimestamp(new_timestamp, tz=timezone.utc) -def timeframe_to_next_date(timeframe: str, date: datetime = None) -> datetime: +def timeframe_to_next_date(timeframe: str, date: Optional[datetime] = None) -> datetime: """ Use Timeframe and determine next candle. :param timeframe: timeframe in string format (e.g. "5m") diff --git a/freqtrade/exchange/gateio.py b/freqtrade/exchange/gate.py similarity index 90% rename from freqtrade/exchange/gateio.py rename to freqtrade/exchange/gate.py index de178af02..bf6d5b59c 100644 --- a/freqtrade/exchange/gateio.py +++ b/freqtrade/exchange/gate.py @@ -4,7 +4,7 @@ from datetime import datetime from typing import Any, Dict, List, Optional, Tuple from freqtrade.constants import BuySell -from freqtrade.enums import MarginMode, TradingMode +from freqtrade.enums import MarginMode, PriceType, TradingMode from freqtrade.exceptions import OperationalException from freqtrade.exchange import Exchange from freqtrade.misc import safe_value_fallback2 @@ -13,7 +13,7 @@ from freqtrade.misc import safe_value_fallback2 logger = logging.getLogger(__name__) -class Gateio(Exchange): +class Gate(Exchange): """ Gate.io exchange class. Contains adjustments needed for Freqtrade to work with this exchange. @@ -32,8 +32,15 @@ class Gateio(Exchange): _ft_has_futures: Dict = { "needs_trading_fees": True, + "tickers_have_bid_ask": False, "fee_cost_in_contracts": False, # Set explicitly to false for clarity "order_props_in_contracts": ['amount', 'filled', 'remaining'], + "stop_price_type_field": "price_type", + "stop_price_type_value_mapping": { + PriceType.LAST: 0, + PriceType.MARK: 1, + PriceType.INDEX: 2, + }, } _supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [ @@ -49,6 +56,7 @@ class Gateio(Exchange): if any(v == 'market' for k, v in order_types.items()): raise OperationalException( f'Exchange {self.name} does not support market orders.') + super().validate_stop_ordertypes(order_types) def _get_params( self, @@ -67,8 +75,7 @@ class Gateio(Exchange): ) if ordertype == 'market' and self.trading_mode == TradingMode.FUTURES: params['type'] = 'market' - param = self._ft_has.get('time_in_force_parameter', '') - params.update({param: 'IOC'}) + params.update({'timeInForce': 'IOC'}) return params def get_trades_for_order(self, order_id: str, pair: str, since: datetime, @@ -77,7 +84,7 @@ class Gateio(Exchange): if self.trading_mode == TradingMode.FUTURES: # Futures usually don't contain fees in the response. - # As such, futures orders on gateio will not contain a fee, which causes + # As such, futures orders on gate will not contain a fee, which causes # a repeated "update fee" cycle and wrong calculations. # Therefore we patch the response with fees if it's not available. # An alternative also contianing fees would be diff --git a/freqtrade/exchange/hitbtc.py b/freqtrade/exchange/hitbtc.py index a48c9a198..bc4c7aa81 100644 --- a/freqtrade/exchange/hitbtc.py +++ b/freqtrade/exchange/hitbtc.py @@ -19,5 +19,4 @@ class Hitbtc(Exchange): _ft_has: Dict = { "ohlcv_candle_limit": 1000, - "ohlcv_params": {"sort": "DESC"} } diff --git a/freqtrade/exchange/kraken.py b/freqtrade/exchange/kraken.py index 5d8c1ad29..b1a19fa69 100644 --- a/freqtrade/exchange/kraken.py +++ b/freqtrade/exchange/kraken.py @@ -97,8 +97,8 @@ class Kraken(Exchange): )) @retrier(retries=0) - def stoploss(self, pair: str, amount: float, stop_price: float, - order_types: Dict, side: BuySell, leverage: float) -> Dict: + def create_stoploss(self, pair: str, amount: float, stop_price: float, + order_types: Dict, side: BuySell, leverage: float) -> Dict: """ Creates a stoploss market order. Stoploss market orders is the only stoploss type supported by kraken. @@ -158,7 +158,7 @@ class Kraken(Exchange): self, leverage: float, pair: Optional[str] = None, - trading_mode: Optional[TradingMode] = None + accept_fail: bool = False, ): """ Kraken set's the leverage as an option in the order object, so we need to diff --git a/freqtrade/exchange/kucoin.py b/freqtrade/exchange/kucoin.py index 6c7d7acfc..20e558513 100644 --- a/freqtrade/exchange/kucoin.py +++ b/freqtrade/exchange/kucoin.py @@ -36,3 +36,35 @@ class Kucoin(Exchange): 'stop': 'loss' }) return params + + def create_order( + self, + *, + pair: str, + ordertype: str, + side: BuySell, + amount: float, + rate: float, + leverage: float, + reduceOnly: bool = False, + time_in_force: str = 'GTC', + ) -> Dict: + + res = super().create_order( + pair=pair, + ordertype=ordertype, + side=side, + amount=amount, + rate=rate, + leverage=leverage, + reduceOnly=reduceOnly, + time_in_force=time_in_force, + ) + # Kucoin returns only the order-id. + # ccxt returns status = 'closed' at the moment - which is information ccxt invented. + # Since we rely on status heavily, we must set it to 'open' here. + # ref: https://github.com/ccxt/ccxt/pull/16674, (https://github.com/ccxt/ccxt/pull/16553) + if not self._config['dry_run']: + res['type'] = ordertype + res['status'] = 'open' + return res diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 6792c2cba..a4fcaeca0 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -1,13 +1,16 @@ import logging -from typing import Dict, List, Optional, Tuple +from typing import Any, Dict, List, Optional, Tuple import ccxt from freqtrade.constants import BuySell from freqtrade.enums import CandleType, MarginMode, TradingMode -from freqtrade.exceptions import DDosProtection, OperationalException, TemporaryError +from freqtrade.enums.pricetype import PriceType +from freqtrade.exceptions import (DDosProtection, OperationalException, RetryableOrderError, + TemporaryError) from freqtrade.exchange import Exchange, date_minus_candles from freqtrade.exchange.common import retrier +from freqtrade.misc import safe_value_fallback2 logger = logging.getLogger(__name__) @@ -23,10 +26,18 @@ class Okx(Exchange): "ohlcv_candle_limit": 100, # Warning, special case with data prior to X months "mark_ohlcv_timeframe": "4h", "funding_fee_timeframe": "8h", + "stoploss_order_types": {"limit": "limit"}, + "stoploss_on_exchange": True, } _ft_has_futures: Dict = { "tickers_have_quoteVolume": False, "fee_cost_in_contracts": True, + "stop_price_type_field": "slTriggerPxType", + "stop_price_type_value_mapping": { + PriceType.LAST: "last", + PriceType.MARK: "index", + PriceType.INDEX: "mark", + }, } _supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [ @@ -114,17 +125,18 @@ class Okx(Exchange): return params @retrier - def _lev_prep(self, pair: str, leverage: float, side: BuySell): + def _lev_prep(self, pair: str, leverage: float, side: BuySell, accept_fail: bool = False): if self.trading_mode != TradingMode.SPOT and self.margin_mode is not None: try: - # TODO-lev: Test me properly (check mgnMode passed) - self._api.set_leverage( + res = self._api.set_leverage( leverage=leverage, symbol=pair, params={ "mgnMode": self.margin_mode.value, "posSide": self._get_posSide(side, False), }) + self._log_exchange_response('set_leverage', res) + except ccxt.DDoSProtection as e: raise DDosProtection(e) from e except (ccxt.NetworkError, ccxt.ExchangeError) as e: @@ -148,3 +160,78 @@ class Okx(Exchange): pair_tiers = self._leverage_tiers[pair] return pair_tiers[-1]['maxNotional'] / leverage + + def _get_stop_params(self, side: BuySell, ordertype: str, stop_price: float) -> Dict: + + params = self._params.copy() + # Verify if stopPrice works for your exchange! + params.update({'stopLossPrice': stop_price}) + + if self.trading_mode == TradingMode.FUTURES and self.margin_mode: + params['tdMode'] = self.margin_mode.value + params['posSide'] = self._get_posSide(side, True) + return params + + def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool: + """ + OKX uses non-default stoploss price naming. + """ + if not self._ft_has.get('stoploss_on_exchange'): + raise OperationalException(f"stoploss is not implemented for {self.name}.") + + return ( + order.get('stopLossPrice', None) is None + or ((side == "sell" and stop_loss > float(order['stopLossPrice'])) or + (side == "buy" and stop_loss < float(order['stopLossPrice']))) + ) + + def fetch_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: + if self._config['dry_run']: + return self.fetch_dry_run_order(order_id) + + try: + params1 = {'stop': True} + order_reg = self._api.fetch_order(order_id, pair, params=params1) + self._log_exchange_response('fetch_stoploss_order', order_reg) + return order_reg + except ccxt.OrderNotFound: + pass + params2 = {'stop': True, 'ordType': 'conditional'} + for method in (self._api.fetch_open_orders, self._api.fetch_closed_orders, + self._api.fetch_canceled_orders): + try: + orders = method(pair, params=params2) + orders_f = [order for order in orders if order['id'] == order_id] + if orders_f: + order = orders_f[0] + if (order['status'] == 'closed' + and (real_order_id := order.get('info', {}).get('ordId')) is not None): + # Once a order triggered, we fetch the regular followup order. + order_reg = self.fetch_order(real_order_id, pair) + self._log_exchange_response('fetch_stoploss_order1', order_reg) + order_reg['id_stop'] = order_reg['id'] + order_reg['id'] = order_id + order_reg['type'] = 'stoploss' + order_reg['status_stop'] = 'triggered' + return order_reg + order['type'] = 'stoploss' + return order + except ccxt.BaseError: + pass + raise RetryableOrderError( + f'StoplossOrder not found (pair: {pair} id: {order_id}).') + + def get_order_id_conditional(self, order: Dict[str, Any]) -> str: + if order['type'] == 'stop': + return safe_value_fallback2(order, order, 'id_stop', 'id') + return order['id'] + + def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict: + params1 = {'stop': True} + # 'ordType': 'conditional' + # + return self.cancel_order( + order_id=order_id, + pair=pair, + params=params1, + ) diff --git a/freqtrade/exchange/types.py b/freqtrade/exchange/types.py index 813b09297..5568e4336 100644 --- a/freqtrade/exchange/types.py +++ b/freqtrade/exchange/types.py @@ -15,6 +15,15 @@ class Ticker(TypedDict): # Several more - only listing required. +class OrderBook(TypedDict): + symbol: str + bids: List[Tuple[float, float]] + asks: List[Tuple[float, float]] + timestamp: Optional[int] + datetime: Optional[str] + nonce: Optional[int] + + Tickers = Dict[str, Ticker] # pair, timeframe, candleType, OHLCV, drop last?, diff --git a/freqtrade/freqai/RL/Base3ActionRLEnv.py b/freqtrade/freqai/RL/Base3ActionRLEnv.py index 3b5fffc58..a108d776e 100644 --- a/freqtrade/freqai/RL/Base3ActionRLEnv.py +++ b/freqtrade/freqai/RL/Base3ActionRLEnv.py @@ -47,7 +47,7 @@ class Base3ActionRLEnv(BaseEnvironment): self._update_unrealized_total_profit() step_reward = self.calculate_reward(action) self.total_reward += step_reward - self.tensorboard_log(self.actions._member_names_[action]) + self.tensorboard_log(self.actions._member_names_[action], category="actions") trade_type = None if self.is_tradesignal(action): diff --git a/freqtrade/freqai/RL/Base4ActionRLEnv.py b/freqtrade/freqai/RL/Base4ActionRLEnv.py index 8f45028b1..4f093f06c 100644 --- a/freqtrade/freqai/RL/Base4ActionRLEnv.py +++ b/freqtrade/freqai/RL/Base4ActionRLEnv.py @@ -48,7 +48,7 @@ class Base4ActionRLEnv(BaseEnvironment): self._update_unrealized_total_profit() step_reward = self.calculate_reward(action) self.total_reward += step_reward - self.tensorboard_log(self.actions._member_names_[action]) + self.tensorboard_log(self.actions._member_names_[action], category="actions") trade_type = None if self.is_tradesignal(action): diff --git a/freqtrade/freqai/RL/Base5ActionRLEnv.py b/freqtrade/freqai/RL/Base5ActionRLEnv.py index 22d3cae30..490ef3601 100644 --- a/freqtrade/freqai/RL/Base5ActionRLEnv.py +++ b/freqtrade/freqai/RL/Base5ActionRLEnv.py @@ -49,7 +49,7 @@ class Base5ActionRLEnv(BaseEnvironment): self._update_unrealized_total_profit() step_reward = self.calculate_reward(action) self.total_reward += step_reward - self.tensorboard_log(self.actions._member_names_[action]) + self.tensorboard_log(self.actions._member_names_[action], category="actions") trade_type = None if self.is_tradesignal(action): diff --git a/freqtrade/freqai/RL/BaseEnvironment.py b/freqtrade/freqai/RL/BaseEnvironment.py index ef1c02a3b..7ac77361c 100644 --- a/freqtrade/freqai/RL/BaseEnvironment.py +++ b/freqtrade/freqai/RL/BaseEnvironment.py @@ -45,7 +45,8 @@ class BaseEnvironment(gym.Env): def __init__(self, df: DataFrame = DataFrame(), prices: DataFrame = DataFrame(), reward_kwargs: dict = {}, window_size=10, starting_point=True, id: str = 'baseenv-1', seed: int = 1, config: dict = {}, live: bool = False, - fee: float = 0.0015, can_short: bool = False): + fee: float = 0.0015, can_short: bool = False, pair: str = "", + df_raw: DataFrame = DataFrame()): """ Initializes the training/eval environment. :param df: dataframe of features @@ -60,12 +61,14 @@ class BaseEnvironment(gym.Env): :param fee: The fee to use for environmental interactions. :param can_short: Whether or not the environment can short """ - self.config = config - self.rl_config = config['freqai']['rl_config'] - self.add_state_info = self.rl_config.get('add_state_info', False) - self.id = id - self.max_drawdown = 1 - self.rl_config.get('max_training_drawdown_pct', 0.8) - self.compound_trades = config['stake_amount'] == 'unlimited' + self.config: dict = config + self.rl_config: dict = config['freqai']['rl_config'] + self.add_state_info: bool = self.rl_config.get('add_state_info', False) + self.id: str = id + self.max_drawdown: float = 1 - self.rl_config.get('max_training_drawdown_pct', 0.8) + self.compound_trades: bool = config['stake_amount'] == 'unlimited' + self.pair: str = pair + self.raw_features: DataFrame = df_raw if self.config.get('fee', None) is not None: self.fee = self.config['fee'] else: @@ -74,8 +77,8 @@ class BaseEnvironment(gym.Env): # set here to default 5Ac, but all children envs can override this self.actions: Type[Enum] = BaseActions self.tensorboard_metrics: dict = {} - self.can_short = can_short - self.live = live + self.can_short: bool = can_short + self.live: bool = live if not self.live and self.add_state_info: self.add_state_info = False logger.warning("add_state_info is not available in backtesting. Deactivating.") @@ -93,13 +96,12 @@ class BaseEnvironment(gym.Env): :param reward_kwargs: extra config settings assigned by user in `rl_config` :param starting_point: start at edge of window or not """ - self.df = df - self.signal_features = self.df - self.prices = prices - self.window_size = window_size - self.starting_point = starting_point - self.rr = reward_kwargs["rr"] - self.profit_aim = reward_kwargs["profit_aim"] + self.signal_features: DataFrame = df + self.prices: DataFrame = prices + self.window_size: int = window_size + self.starting_point: bool = starting_point + self.rr: float = reward_kwargs["rr"] + self.profit_aim: float = reward_kwargs["profit_aim"] # # spaces if self.add_state_info: @@ -135,7 +137,8 @@ class BaseEnvironment(gym.Env): self.np_random, seed = seeding.np_random(seed) return [seed] - def tensorboard_log(self, metric: str, value: Union[int, float] = 1, inc: bool = True): + def tensorboard_log(self, metric: str, value: Optional[Union[int, float]] = None, + inc: Optional[bool] = None, category: str = "custom"): """ Function builds the tensorboard_metrics dictionary to be parsed by the TensorboardCallback. This @@ -147,17 +150,24 @@ class BaseEnvironment(gym.Env): def calculate_reward(self, action: int) -> float: if not self._is_valid(action): - self.tensorboard_log("is_valid") + self.tensorboard_log("invalid") return -2 :param metric: metric to be tracked and incremented - :param value: value to increment `metric` by - :param inc: sets whether the `value` is incremented or not + :param value: `metric` value + :param inc: (deprecated) sets whether the `value` is incremented or not + :param category: `metric` category """ - if not inc or metric not in self.tensorboard_metrics: - self.tensorboard_metrics[metric] = value + increment = True if value is None else False + value = 1 if increment else value + + if category not in self.tensorboard_metrics: + self.tensorboard_metrics[category] = {} + + if not increment or metric not in self.tensorboard_metrics[category]: + self.tensorboard_metrics[category][metric] = value else: - self.tensorboard_metrics[metric] += value + self.tensorboard_metrics[category][metric] += value def reset_tensorboard_log(self): self.tensorboard_metrics = {} diff --git a/freqtrade/freqai/RL/BaseReinforcementLearningModel.py b/freqtrade/freqai/RL/BaseReinforcementLearningModel.py index 3a4d0d0e6..e10880f46 100644 --- a/freqtrade/freqai/RL/BaseReinforcementLearningModel.py +++ b/freqtrade/freqai/RL/BaseReinforcementLearningModel.py @@ -1,3 +1,4 @@ +import copy import importlib import logging from abc import abstractmethod @@ -50,6 +51,7 @@ class BaseReinforcementLearningModel(IFreqaiModel): self.eval_callback: Optional[EvalCallback] = None self.model_type = self.freqai_info['rl_config']['model_type'] self.rl_config = self.freqai_info['rl_config'] + self.df_raw: DataFrame = DataFrame() self.continual_learning = self.freqai_info.get('continual_learning', False) if self.model_type in SB3_MODELS: import_str = 'stable_baselines3' @@ -107,10 +109,12 @@ class BaseReinforcementLearningModel(IFreqaiModel): data_dictionary: Dict[str, Any] = dk.make_train_test_datasets( features_filtered, labels_filtered) + self.df_raw = copy.deepcopy(data_dictionary["train_features"]) dk.fit_labels() # FIXME useless for now, but just satiating append methods # normalize all data based on train_dataset only prices_train, prices_test = self.build_ohlc_price_dataframes(dk.data_dictionary, pair, dk) + data_dictionary = dk.normalize_data(data_dictionary) # data cleaning/analysis @@ -143,14 +147,10 @@ class BaseReinforcementLearningModel(IFreqaiModel): train_df = data_dictionary["train_features"] test_df = data_dictionary["test_features"] - env_info = self.pack_env_dict() + env_info = self.pack_env_dict(dk.pair) - self.train_env = self.MyRLEnv(df=train_df, - prices=prices_train, - **env_info) - self.eval_env = Monitor(self.MyRLEnv(df=test_df, - prices=prices_test, - **env_info)) + self.train_env = self.MyRLEnv(df=train_df, prices=prices_train, **env_info) + self.eval_env = Monitor(self.MyRLEnv(df=test_df, prices=prices_test, **env_info)) self.eval_callback = EvalCallback(self.eval_env, deterministic=True, render=False, eval_freq=len(train_df), best_model_save_path=str(dk.data_path)) @@ -158,7 +158,7 @@ class BaseReinforcementLearningModel(IFreqaiModel): actions = self.train_env.get_actions() self.tensorboard_callback = TensorboardCallback(verbose=1, actions=actions) - def pack_env_dict(self) -> Dict[str, Any]: + def pack_env_dict(self, pair: str) -> Dict[str, Any]: """ Create dictionary of environment arguments """ @@ -166,7 +166,9 @@ class BaseReinforcementLearningModel(IFreqaiModel): "reward_kwargs": self.reward_params, "config": self.config, "live": self.live, - "can_short": self.can_short} + "can_short": self.can_short, + "pair": pair, + "df_raw": self.df_raw} if self.data_provider: env_info["fee"] = self.data_provider._exchange \ .get_fee(symbol=self.data_provider.current_whitelist()[0]) # type: ignore @@ -233,6 +235,9 @@ class BaseReinforcementLearningModel(IFreqaiModel): filtered_dataframe, _ = dk.filter_features( unfiltered_df, dk.training_features_list, training_filter=False ) + + filtered_dataframe = self.drop_ohlc_from_df(filtered_dataframe, dk) + filtered_dataframe = dk.normalize_data_from_metadata(filtered_dataframe) dk.data_dictionary["prediction_features"] = filtered_dataframe @@ -280,7 +285,6 @@ class BaseReinforcementLearningModel(IFreqaiModel): train_df = data_dictionary["train_features"] test_df = data_dictionary["test_features"] - # %-raw_volume_gen_shift-2_ETH/USDT_1h # price data for model training and evaluation tf = self.config['timeframe'] rename_dict = {'%-raw_open': 'open', '%-raw_low': 'low', @@ -313,8 +317,24 @@ class BaseReinforcementLearningModel(IFreqaiModel): prices_test.rename(columns=rename_dict, inplace=True) prices_test.reset_index(drop=True) + train_df = self.drop_ohlc_from_df(train_df, dk) + test_df = self.drop_ohlc_from_df(test_df, dk) + return prices_train, prices_test + def drop_ohlc_from_df(self, df: DataFrame, dk: FreqaiDataKitchen): + """ + Given a dataframe, drop the ohlc data + """ + drop_list = ['%-raw_open', '%-raw_low', '%-raw_high', '%-raw_close'] + + if self.rl_config["drop_ohlc_from_features"]: + df.drop(drop_list, axis=1, inplace=True) + feature_list = dk.training_features_list + dk.training_features_list = [e for e in feature_list if e not in drop_list] + + return df + def load_model_from_disk(self, dk: FreqaiDataKitchen) -> Any: """ Can be used by user if they are trying to limit_ram_usage *and* @@ -347,7 +367,7 @@ class BaseReinforcementLearningModel(IFreqaiModel): sets a custom reward based on profit and trade duration. """ - def calculate_reward(self, action: int) -> float: + def calculate_reward(self, action: int) -> float: # noqa: C901 """ An example reward function. This is the one function that users will likely wish to inject their own creativity into. @@ -363,10 +383,19 @@ class BaseReinforcementLearningModel(IFreqaiModel): pnl = self.get_unrealized_profit() factor = 100. + # you can use feature values from dataframe + rsi_now = self.raw_features[f"%-rsi-period-10_shift-1_{self.pair}_" + f"{self.config['timeframe']}"].iloc[self._current_tick] + # reward agent for entering trades if (action in (Actions.Long_enter.value, Actions.Short_enter.value) and self._position == Positions.Neutral): - return 25 + if rsi_now < 40: + factor = 40 / rsi_now + else: + factor = 1 + return 25 * factor + # discourage agent from not entering trades if action == Actions.Neutral.value and self._position == Positions.Neutral: return -1 diff --git a/freqtrade/freqai/RL/TensorboardCallback.py b/freqtrade/freqai/RL/TensorboardCallback.py index b596742e9..7f8c76956 100644 --- a/freqtrade/freqai/RL/TensorboardCallback.py +++ b/freqtrade/freqai/RL/TensorboardCallback.py @@ -13,7 +13,7 @@ class TensorboardCallback(BaseCallback): episodic summary reports. """ def __init__(self, verbose=1, actions: Type[Enum] = BaseActions): - super(TensorboardCallback, self).__init__(verbose) + super().__init__(verbose) self.model: Any = None self.logger = None # type: Any self.training_env: BaseEnvironment = None # type: ignore @@ -46,14 +46,12 @@ class TensorboardCallback(BaseCallback): local_info = self.locals["infos"][0] tensorboard_metrics = self.training_env.get_attr("tensorboard_metrics")[0] - for info in local_info: - if info not in ["episode", "terminal_observation"]: - self.logger.record(f"_info/{info}", local_info[info]) + for metric in local_info: + if metric not in ["episode", "terminal_observation"]: + self.logger.record(f"info/{metric}", local_info[metric]) - for info in tensorboard_metrics: - if info in [action.name for action in self.actions]: - self.logger.record(f"_actions/{info}", tensorboard_metrics[info]) - else: - self.logger.record(f"_custom/{info}", tensorboard_metrics[info]) + for category in tensorboard_metrics: + for metric in tensorboard_metrics[category]: + self.logger.record(f"{category}/{metric}", tensorboard_metrics[category][metric]) return True diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index 848fb20eb..14986d854 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -59,7 +59,7 @@ class FreqaiDataDrawer: Juha Nykänen @suikula, Wagner Costa @wagnercosta, Johan Vlugt @Jooopieeert """ - def __init__(self, full_path: Path, config: Config, follow_mode: bool = False): + def __init__(self, full_path: Path, config: Config): self.config = config self.freqai_info = config.get("freqai", {}) @@ -72,21 +72,13 @@ class FreqaiDataDrawer: self.model_return_values: Dict[str, DataFrame] = {} self.historic_data: Dict[str, Dict[str, DataFrame]] = {} self.historic_predictions: Dict[str, DataFrame] = {} - self.follower_dict: Dict[str, pair_info] = {} self.full_path = full_path - self.follower_name: str = self.config.get("bot_name", "follower1") - self.follower_dict_path = Path( - self.full_path / f"follower_dictionary-{self.follower_name}.json" - ) self.historic_predictions_path = Path(self.full_path / "historic_predictions.pkl") self.historic_predictions_bkp_path = Path( self.full_path / "historic_predictions.backup.pkl") self.pair_dictionary_path = Path(self.full_path / "pair_dictionary.json") self.global_metadata_path = Path(self.full_path / "global_metadata.json") self.metric_tracker_path = Path(self.full_path / "metric_tracker.json") - self.follow_mode = follow_mode - if follow_mode: - self.create_follower_dict() self.load_drawer_from_disk() self.load_historic_predictions_from_disk() self.metric_tracker: Dict[str, Dict[str, Dict[str, list]]] = {} @@ -134,7 +126,7 @@ class FreqaiDataDrawer: """ exists = self.global_metadata_path.is_file() if exists: - with open(self.global_metadata_path, "r") as fp: + with self.global_metadata_path.open("r") as fp: metatada_dict = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) return metatada_dict return {} @@ -147,15 +139,10 @@ class FreqaiDataDrawer: """ exists = self.pair_dictionary_path.is_file() if exists: - with open(self.pair_dictionary_path, "r") as fp: + with self.pair_dictionary_path.open("r") as fp: self.pair_dict = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) - elif not self.follow_mode: - logger.info("Could not find existing datadrawer, starting from scratch") else: - logger.warning( - f"Follower could not find pair_dictionary at {self.full_path} " - "sending null values back to strategy" - ) + logger.info("Could not find existing datadrawer, starting from scratch") def load_metric_tracker_from_disk(self): """ @@ -165,7 +152,7 @@ class FreqaiDataDrawer: if self.freqai_info.get('write_metrics_to_disk', False): exists = self.metric_tracker_path.is_file() if exists: - with open(self.metric_tracker_path, "r") as fp: + with self.metric_tracker_path.open("r") as fp: self.metric_tracker = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) logger.info("Loading existing metric tracker from disk.") else: @@ -179,7 +166,7 @@ class FreqaiDataDrawer: exists = self.historic_predictions_path.is_file() if exists: try: - with open(self.historic_predictions_path, "rb") as fp: + with self.historic_predictions_path.open("rb") as fp: self.historic_predictions = cloudpickle.load(fp) logger.info( f"Found existing historic predictions at {self.full_path}, but beware " @@ -189,17 +176,12 @@ class FreqaiDataDrawer: except EOFError: logger.warning( 'Historical prediction file was corrupted. Trying to load backup file.') - with open(self.historic_predictions_bkp_path, "rb") as fp: + with self.historic_predictions_bkp_path.open("rb") as fp: self.historic_predictions = cloudpickle.load(fp) logger.warning('FreqAI successfully loaded the backup historical predictions file.') - elif not self.follow_mode: - logger.info("Could not find existing historic_predictions, starting from scratch") else: - logger.warning( - f"Follower could not find historic predictions at {self.full_path} " - "sending null values back to strategy" - ) + logger.info("Could not find existing historic_predictions, starting from scratch") return exists @@ -207,7 +189,7 @@ class FreqaiDataDrawer: """ Save historic predictions pickle to disk """ - with open(self.historic_predictions_path, "wb") as fp: + with self.historic_predictions_path.open("wb") as fp: cloudpickle.dump(self.historic_predictions, fp, protocol=cloudpickle.DEFAULT_PROTOCOL) # create a backup @@ -218,58 +200,33 @@ class FreqaiDataDrawer: Save metric tracker of all pair metrics collected. """ with self.save_lock: - with open(self.metric_tracker_path, 'w') as fp: + with self.metric_tracker_path.open('w') as fp: rapidjson.dump(self.metric_tracker, fp, default=self.np_encoder, number_mode=rapidjson.NM_NATIVE) - def save_drawer_to_disk(self): + def save_drawer_to_disk(self) -> None: """ Save data drawer full of all pair model metadata in present model folder. """ with self.save_lock: - with open(self.pair_dictionary_path, 'w') as fp: + with self.pair_dictionary_path.open('w') as fp: rapidjson.dump(self.pair_dict, fp, default=self.np_encoder, number_mode=rapidjson.NM_NATIVE) - def save_follower_dict_to_disk(self): - """ - Save follower dictionary to disk (used by strategy for persistent prediction targets) - """ - with open(self.follower_dict_path, "w") as fp: - rapidjson.dump(self.follower_dict, fp, default=self.np_encoder, - number_mode=rapidjson.NM_NATIVE) - def save_global_metadata_to_disk(self, metadata: Dict[str, Any]): """ Save global metadata json to disk """ with self.save_lock: - with open(self.global_metadata_path, 'w') as fp: + with self.global_metadata_path.open('w') as fp: rapidjson.dump(metadata, fp, default=self.np_encoder, number_mode=rapidjson.NM_NATIVE) - def create_follower_dict(self): - """ - Create or dictionary for each follower to maintain unique persistent prediction targets - """ - - whitelist_pairs = self.config.get("exchange", {}).get("pair_whitelist") - - exists = self.follower_dict_path.is_file() - - if exists: - logger.info("Found an existing follower dictionary") - - for pair in whitelist_pairs: - self.follower_dict[pair] = {} - - self.save_follower_dict_to_disk() - def np_encoder(self, object): if isinstance(object, np.generic): return object.item() - def get_pair_dict_info(self, pair: str) -> Tuple[str, int, bool]: + def get_pair_dict_info(self, pair: str) -> Tuple[str, int]: """ Locate and load existing model metadata from persistent storage. If not located, create a new one and append the current pair to it and prepare it for its first @@ -278,32 +235,19 @@ class FreqaiDataDrawer: :return: model_filename: str = unique filename used for loading persistent objects from disk trained_timestamp: int = the last time the coin was trained - return_null_array: bool = Follower could not find pair metadata """ pair_dict = self.pair_dict.get(pair) - data_path_set = self.pair_dict.get(pair, self.empty_pair_dict).get("data_path", "") - return_null_array = False if pair_dict: model_filename = pair_dict["model_filename"] trained_timestamp = pair_dict["trained_timestamp"] - elif not self.follow_mode: + else: self.pair_dict[pair] = self.empty_pair_dict.copy() model_filename = "" trained_timestamp = 0 - if not data_path_set and self.follow_mode: - logger.warning( - f"Follower could not find current pair {pair} in " - f"pair_dictionary at path {self.full_path}, sending null values " - "back to strategy." - ) - trained_timestamp = 0 - model_filename = '' - return_null_array = True - - return model_filename, trained_timestamp, return_null_array + return model_filename, trained_timestamp def set_pair_dict_info(self, metadata: dict) -> None: pair_in_dict = self.pair_dict.get(metadata["pair"]) @@ -311,7 +255,6 @@ class FreqaiDataDrawer: return else: self.pair_dict[metadata["pair"]] = self.empty_pair_dict.copy() - return def set_initial_return_values(self, pair: str, pred_df: DataFrame) -> None: @@ -423,6 +366,12 @@ class FreqaiDataDrawer: def purge_old_models(self) -> None: + num_keep = self.freqai_info["purge_old_models"] + if not num_keep: + return + elif type(num_keep) == bool: + num_keep = 2 + model_folders = [x for x in self.full_path.iterdir() if x.is_dir()] pattern = re.compile(r"sub-train-(\w+)_(\d{10})") @@ -445,11 +394,11 @@ class FreqaiDataDrawer: delete_dict[coin]["timestamps"][int(timestamp)] = dir for coin in delete_dict: - if delete_dict[coin]["num_folders"] > 2: + if delete_dict[coin]["num_folders"] > num_keep: sorted_dict = collections.OrderedDict( sorted(delete_dict[coin]["timestamps"].items()) ) - num_delete = len(sorted_dict) - 2 + num_delete = len(sorted_dict) - num_keep deleted = 0 for k, v in sorted_dict.items(): if deleted >= num_delete: @@ -458,12 +407,6 @@ class FreqaiDataDrawer: shutil.rmtree(v) deleted += 1 - def update_follower_metadata(self): - # follower needs to load from disk to get any changes made by leader to pair_dict - self.load_drawer_from_disk() - if self.config.get("freqai", {}).get("purge_old_models", False): - self.purge_old_models() - def save_metadata(self, dk: FreqaiDataKitchen) -> None: """ Saves only metadata for backtesting studies if user prefers @@ -481,7 +424,7 @@ class FreqaiDataDrawer: dk.data["training_features_list"] = list(dk.data_dictionary["train_features"].columns) dk.data["label_list"] = dk.label_list - with open(save_path / f"{dk.model_filename}_metadata.json", "w") as fp: + with (save_path / f"{dk.model_filename}_metadata.json").open("w") as fp: rapidjson.dump(dk.data, fp, default=self.np_encoder, number_mode=rapidjson.NM_NATIVE) return @@ -514,7 +457,7 @@ class FreqaiDataDrawer: dk.data["training_features_list"] = dk.training_features_list dk.data["label_list"] = dk.label_list # store the metadata - with open(save_path / f"{dk.model_filename}_metadata.json", "w") as fp: + with (save_path / f"{dk.model_filename}_metadata.json").open("w") as fp: rapidjson.dump(dk.data, fp, default=self.np_encoder, number_mode=rapidjson.NM_NATIVE) # save the train data to file so we can check preds for area of applicability later @@ -528,7 +471,7 @@ class FreqaiDataDrawer: if self.freqai_info["feature_parameters"].get("principal_component_analysis"): cloudpickle.dump( - dk.pca, open(dk.data_path / f"{dk.model_filename}_pca_object.pkl", "wb") + dk.pca, (dk.data_path / f"{dk.model_filename}_pca_object.pkl").open("wb") ) self.model_dictionary[coin] = model @@ -548,7 +491,7 @@ class FreqaiDataDrawer: Load only metadata into datakitchen to increase performance during presaved backtesting (prediction file loading). """ - with open(dk.data_path / f"{dk.model_filename}_metadata.json", "r") as fp: + with (dk.data_path / f"{dk.model_filename}_metadata.json").open("r") as fp: dk.data = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) dk.training_features_list = dk.data["training_features_list"] dk.label_list = dk.data["label_list"] @@ -571,7 +514,7 @@ class FreqaiDataDrawer: dk.data = self.meta_data_dictionary[coin]["meta_data"] dk.data_dictionary["train_features"] = self.meta_data_dictionary[coin]["train_df"] else: - with open(dk.data_path / f"{dk.model_filename}_metadata.json", "r") as fp: + with (dk.data_path / f"{dk.model_filename}_metadata.json").open("r") as fp: dk.data = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) dk.data_dictionary["train_features"] = pd.read_pickle( @@ -609,7 +552,7 @@ class FreqaiDataDrawer: if self.config["freqai"]["feature_parameters"]["principal_component_analysis"]: dk.pca = cloudpickle.load( - open(dk.data_path / f"{dk.model_filename}_pca_object.pkl", "rb") + (dk.data_path / f"{dk.model_filename}_pca_object.pkl").open("rb") ) return model @@ -627,12 +570,12 @@ class FreqaiDataDrawer: for pair in dk.all_pairs: for tf in feat_params.get("include_timeframes"): - + hist_df = history_data[pair][tf] # check if newest candle is already appended df_dp = strategy.dp.get_pair_dataframe(pair, tf) if len(df_dp.index) == 0: continue - if str(history_data[pair][tf].iloc[-1]["date"]) == str( + if str(hist_df.iloc[-1]["date"]) == str( df_dp.iloc[-1:]["date"].iloc[-1] ): continue @@ -640,21 +583,30 @@ class FreqaiDataDrawer: try: index = ( df_dp.loc[ - df_dp["date"] == history_data[pair][tf].iloc[-1]["date"] + df_dp["date"] == hist_df.iloc[-1]["date"] ].index[0] + 1 ) except IndexError: - logger.warning( - f"Unable to update pair history for {pair}. " - "If this does not resolve itself after 1 additional candle, " - "please report the error to #freqai discord channel" - ) - return + if hist_df.iloc[-1]['date'] < df_dp['date'].iloc[0]: + raise OperationalException("In memory historical data is older than " + f"oldest DataProvider candle for {pair} on " + f"timeframe {tf}") + else: + index = -1 + logger.warning( + f"No common dates in historical data and dataprovider for {pair}. " + f"Appending latest dataprovider candle to historical data " + "but please be aware that there is likely a gap in the historical " + "data. \n" + f"Historical data ends at {hist_df.iloc[-1]['date']} " + f"while dataprovider starts at {df_dp['date'].iloc[0]} and" + f"ends at {df_dp['date'].iloc[0]}." + ) history_data[pair][tf] = pd.concat( [ - history_data[pair][tf], + hist_df, df_dp.iloc[index:], ], ignore_index=True, diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index 9fdc2c98e..52d487b08 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -1,11 +1,12 @@ import copy import inspect import logging +import random import shutil from datetime import datetime, timezone from math import cos, sin from pathlib import Path -from typing import Any, Dict, List, Tuple +from typing import Any, Dict, List, Optional, Tuple import numpy as np import numpy.typing as npt @@ -112,7 +113,7 @@ class FreqaiDataKitchen: def set_paths( self, pair: str, - trained_timestamp: int = None, + trained_timestamp: Optional[int] = None, ) -> None: """ Set the paths to the data for the present coin/botloop @@ -170,6 +171,19 @@ class FreqaiDataKitchen: train_labels = labels train_weights = weights + if feat_dict["shuffle_after_split"]: + rint1 = random.randint(0, 100) + rint2 = random.randint(0, 100) + train_features = train_features.sample( + frac=1, random_state=rint1).reset_index(drop=True) + train_labels = train_labels.sample(frac=1, random_state=rint1).reset_index(drop=True) + train_weights = pd.DataFrame(train_weights).sample( + frac=1, random_state=rint1).reset_index(drop=True).to_numpy()[:, 0] + test_features = test_features.sample(frac=1, random_state=rint2).reset_index(drop=True) + test_labels = test_labels.sample(frac=1, random_state=rint2).reset_index(drop=True) + test_weights = pd.DataFrame(test_weights).sample( + frac=1, random_state=rint2).reset_index(drop=True).to_numpy()[:, 0] + # Simplest way to reverse the order of training and test data: if self.freqai_config['feature_parameters'].get('reverse_train_test_order', False): return self.build_data_dictionary( @@ -237,7 +251,7 @@ class FreqaiDataKitchen: (drop_index == 0) & (drop_index_labels == 0) ] logger.info( - f"dropped {len(unfiltered_df) - len(filtered_df)} training points" + f"{self.pair}: dropped {len(unfiltered_df) - len(filtered_df)} training points" f" due to NaNs in populated dataset {len(unfiltered_df)}." ) if (1 - len(filtered_df) / len(unfiltered_df)) > 0.1 and self.live: @@ -661,7 +675,7 @@ class FreqaiDataKitchen: ] logger.info( - f"SVM tossed {len(y_pred) - kept_points.sum()}" + f"{self.pair}: SVM tossed {len(y_pred) - kept_points.sum()}" f" test points from {len(y_pred)} total points." ) @@ -935,7 +949,7 @@ class FreqaiDataKitchen: if (len(do_predict) - do_predict.sum()) > 0: logger.info( - f"DI tossed {len(do_predict) - do_predict.sum()} predictions for " + f"{self.pair}: DI tossed {len(do_predict) - do_predict.sum()} predictions for " "being too far from training data." ) @@ -1247,17 +1261,19 @@ class FreqaiDataKitchen: tfs: List[str] = self.freqai_config["feature_parameters"].get("include_timeframes") for tf in tfs: + metadata = {"pair": pair, "tf": tf} informative_df = self.get_pair_data_for_features( pair, tf, strategy, corr_dataframes, base_dataframes, is_corr_pairs) informative_copy = informative_df.copy() for t in self.freqai_config["feature_parameters"]["indicator_periods_candles"]: df_features = strategy.feature_engineering_expand_all( - informative_copy.copy(), t) + informative_copy.copy(), t, metadata=metadata) suffix = f"{t}" informative_df = self.merge_features(informative_df, df_features, tf, tf, suffix) - generic_df = strategy.feature_engineering_expand_basic(informative_copy.copy()) + generic_df = strategy.feature_engineering_expand_basic( + informative_copy.copy(), metadata=metadata) suffix = "gen" informative_df = self.merge_features(informative_df, generic_df, tf, tf, suffix) @@ -1299,123 +1315,54 @@ class FreqaiDataKitchen: dataframe: DataFrame = dataframe containing populated indicators """ - # this is a hack to check if the user is using the populate_any_indicators function + # check if the user is using the deprecated populate_any_indicators function new_version = inspect.getsource(strategy.populate_any_indicators) == ( inspect.getsource(IStrategy.populate_any_indicators)) - if new_version: - tfs: List[str] = self.freqai_config["feature_parameters"].get("include_timeframes") - pairs: List[str] = self.freqai_config["feature_parameters"].get( - "include_corr_pairlist", []) + if not new_version: + raise OperationalException( + "You are using the `populate_any_indicators()` function" + " which was deprecated on March 1, 2023. Please refer " + "to the strategy migration guide to use the new " + "feature_engineering_* methods: \n" + "https://www.freqtrade.io/en/stable/strategy_migration/#freqai-strategy \n" + "And the feature_engineering_* documentation: \n" + "https://www.freqtrade.io/en/latest/freqai-feature-engineering/" + ) - for tf in tfs: - if tf not in base_dataframes: - base_dataframes[tf] = pd.DataFrame() - for p in pairs: - if p not in corr_dataframes: - corr_dataframes[p] = {} - if tf not in corr_dataframes[p]: - corr_dataframes[p][tf] = pd.DataFrame() - - if not prediction_dataframe.empty: - dataframe = prediction_dataframe.copy() - else: - dataframe = base_dataframes[self.config["timeframe"]].copy() - - corr_pairs: List[str] = self.freqai_config["feature_parameters"].get( - "include_corr_pairlist", []) - dataframe = self.populate_features(dataframe.copy(), pair, strategy, - corr_dataframes, base_dataframes) - - dataframe = strategy.feature_engineering_standard(dataframe.copy()) - # ensure corr pairs are always last - for corr_pair in corr_pairs: - if pair == corr_pair: - continue # dont repeat anything from whitelist - if corr_pairs and do_corr_pairs: - dataframe = self.populate_features(dataframe.copy(), corr_pair, strategy, - corr_dataframes, base_dataframes, True) - - dataframe = strategy.set_freqai_targets(dataframe.copy()) - - self.get_unique_classes_from_labels(dataframe) - - dataframe = self.remove_special_chars_from_feature_names(dataframe) - - if self.config.get('reduce_df_footprint', False): - dataframe = reduce_dataframe_footprint(dataframe) - - return dataframe - - else: - # the user is using the populate_any_indicators functions which is deprecated - - df = self.use_strategy_to_populate_indicators_old_version( - strategy, corr_dataframes, base_dataframes, pair, - prediction_dataframe, do_corr_pairs) - return df - - def use_strategy_to_populate_indicators_old_version( - self, - strategy: IStrategy, - corr_dataframes: dict = {}, - base_dataframes: dict = {}, - pair: str = "", - prediction_dataframe: DataFrame = pd.DataFrame(), - do_corr_pairs: bool = True, - ) -> DataFrame: - """ - Use the user defined strategy for populating indicators during retrain - :param strategy: IStrategy = user defined strategy object - :param corr_dataframes: dict = dict containing the df pair dataframes - (for user defined timeframes) - :param base_dataframes: dict = dict containing the current pair dataframes - (for user defined timeframes) - :param metadata: dict = strategy furnished pair metadata - :return: - dataframe: DataFrame = dataframe containing populated indicators - """ - - # for prediction dataframe creation, we let dataprovider handle everything in the strategy - # so we create empty dictionaries, which allows us to pass None to - # `populate_any_indicators()`. Signaling we want the dp to give us the live dataframe. tfs: List[str] = self.freqai_config["feature_parameters"].get("include_timeframes") - pairs: List[str] = self.freqai_config["feature_parameters"].get("include_corr_pairlist", []) + pairs: List[str] = self.freqai_config["feature_parameters"].get( + "include_corr_pairlist", []) + + for tf in tfs: + if tf not in base_dataframes: + base_dataframes[tf] = pd.DataFrame() + for p in pairs: + if p not in corr_dataframes: + corr_dataframes[p] = {} + if tf not in corr_dataframes[p]: + corr_dataframes[p][tf] = pd.DataFrame() + if not prediction_dataframe.empty: dataframe = prediction_dataframe.copy() - for tf in tfs: - base_dataframes[tf] = None - for p in pairs: - if p not in corr_dataframes: - corr_dataframes[p] = {} - corr_dataframes[p][tf] = None else: dataframe = base_dataframes[self.config["timeframe"]].copy() - sgi = False - for tf in tfs: - if tf == tfs[-1]: - sgi = True # doing this last allows user to use all tf raw prices in labels - dataframe = strategy.populate_any_indicators( - pair, - dataframe.copy(), - tf, - informative=base_dataframes[tf], - set_generalized_indicators=sgi - ) - + corr_pairs: List[str] = self.freqai_config["feature_parameters"].get( + "include_corr_pairlist", []) + dataframe = self.populate_features(dataframe.copy(), pair, strategy, + corr_dataframes, base_dataframes) + metadata = {"pair": pair} + dataframe = strategy.feature_engineering_standard(dataframe.copy(), metadata=metadata) # ensure corr pairs are always last - for corr_pair in pairs: + for corr_pair in corr_pairs: if pair == corr_pair: continue # dont repeat anything from whitelist - for tf in tfs: - if pairs and do_corr_pairs: - dataframe = strategy.populate_any_indicators( - corr_pair, - dataframe.copy(), - tf, - informative=corr_dataframes[corr_pair][tf] - ) + if corr_pairs and do_corr_pairs: + dataframe = self.populate_features(dataframe.copy(), corr_pair, strategy, + corr_dataframes, base_dataframes, True) + + dataframe = strategy.set_freqai_targets(dataframe.copy(), metadata=metadata) self.get_unique_classes_from_labels(dataframe) @@ -1546,3 +1493,25 @@ class FreqaiDataKitchen: dataframe.columns = dataframe.columns.str.replace(c, "") return dataframe + + def buffer_timerange(self, timerange: TimeRange): + """ + Buffer the start and end of the timerange. This is used *after* the indicators + are populated. + + The main example use is when predicting maxima and minima, the argrelextrema + function cannot know the maxima/minima at the edges of the timerange. To improve + model accuracy, it is best to compute argrelextrema on the full timerange + and then use this function to cut off the edges (buffer) by the kernel. + + In another case, if the targets are set to a shifted price movement, this + buffer is unnecessary because the shifted candles at the end of the timerange + will be NaN and FreqAI will automatically cut those off of the training + dataset. + """ + buffer = self.freqai_config["feature_parameters"]["buffer_train_data_candles"] + if buffer: + timerange.stopts -= buffer * timeframe_to_seconds(self.config["timeframe"]) + timerange.startts += buffer * timeframe_to_seconds(self.config["timeframe"]) + + return timerange diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index 830970ba0..07c357de3 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -1,4 +1,3 @@ -import inspect import logging import threading import time @@ -66,12 +65,11 @@ class IFreqaiModel(ABC): self.retrain = False self.first = True self.set_full_path() - self.follow_mode: bool = self.freqai_info.get("follow_mode", False) self.save_backtest_models: bool = self.freqai_info.get("save_backtest_models", True) if self.save_backtest_models: logger.info('Backtesting module configured to save all models.') - self.dd = FreqaiDataDrawer(Path(self.full_path), self.config, self.follow_mode) + self.dd = FreqaiDataDrawer(Path(self.full_path), self.config) # set current candle to arbitrary historical date self.current_candle: datetime = datetime.fromtimestamp(637887600, tz=timezone.utc) self.dd.current_candle = self.current_candle @@ -106,8 +104,7 @@ class IFreqaiModel(ABC): self.data_provider: Optional[DataProvider] = None self.max_system_threads = max(int(psutil.cpu_count() * 2 - 2), 1) self.can_short = True # overridden in start() with strategy.can_short - - self.warned_deprecated_populate_any_indicators = False + self.model: Any = None record_params(config, self.full_path) @@ -139,9 +136,6 @@ class IFreqaiModel(ABC): self.data_provider = strategy.dp self.can_short = strategy.can_short - # check if the strategy has deprecated populate_any_indicators function - self.check_deprecated_populate_any_indicators(strategy) - if self.live: self.inference_timer('start') self.dk = FreqaiDataKitchen(self.config, self.live, metadata["pair"]) @@ -153,7 +147,7 @@ class IFreqaiModel(ABC): # (backtest window, i.e. window immediately following the training window). # FreqAI slides the window and sequentially builds the backtesting results before returning # the concatenated results for the full backtesting period back to the strategy. - elif not self.follow_mode: + else: self.dk = FreqaiDataKitchen(self.config, self.live, metadata["pair"]) if not self.config.get("freqai_backtest_live_models", False): logger.info(f"Training {len(self.dk.training_timeranges)} timeranges") @@ -228,7 +222,7 @@ class IFreqaiModel(ABC): logger.warning(f'{pair} not in current whitelist, removing from train queue.') continue - (_, trained_timestamp, _) = self.dd.get_pair_dict_info(pair) + (_, trained_timestamp) = self.dd.get_pair_dict_info(pair) dk = FreqaiDataKitchen(self.config, self.live, pair) ( @@ -286,7 +280,7 @@ class IFreqaiModel(ABC): # following tr_train. Both of these windows slide through the # entire backtest for tr_train, tr_backtest in zip(dk.training_timeranges, dk.backtesting_timeranges): - (_, _, _) = self.dd.get_pair_dict_info(pair) + (_, _) = self.dd.get_pair_dict_info(pair) train_it += 1 total_trains = len(dk.backtesting_timeranges) self.training_timerange = tr_train @@ -325,9 +319,13 @@ class IFreqaiModel(ABC): populate_indicators = False dataframe_base_train = dataframe.loc[dataframe["date"] < tr_train.stopdt, :] - dataframe_base_train = strategy.set_freqai_targets(dataframe_base_train) + dataframe_base_train = strategy.set_freqai_targets( + dataframe_base_train, metadata=metadata) dataframe_base_backtest = dataframe.loc[dataframe["date"] < tr_backtest.stopdt, :] - dataframe_base_backtest = strategy.set_freqai_targets(dataframe_base_backtest) + dataframe_base_backtest = strategy.set_freqai_targets( + dataframe_base_backtest, metadata=metadata) + + tr_train = dk.buffer_timerange(tr_train) dataframe_train = dk.slice_dataframe(tr_train, dataframe_base_train) dataframe_backtest = dk.slice_dataframe(tr_backtest, dataframe_base_backtest) @@ -341,13 +339,14 @@ class IFreqaiModel(ABC): except Exception as msg: logger.warning( f"Training {pair} raised exception {msg.__class__.__name__}. " - f"Message: {msg}, skipping.") + f"Message: {msg}, skipping.", exc_info=True) + self.model = None self.dd.pair_dict[pair]["trained_timestamp"] = int( tr_train.stopts) - if self.plot_features: + if self.plot_features and self.model is not None: plot_feature_importance(self.model, pair, dk, self.plot_features) - if self.save_backtest_models: + if self.save_backtest_models and self.model is not None: logger.info('Saving backtest model to disk.') self.dd.save_data(self.model, pair, dk) else: @@ -379,18 +378,9 @@ class IFreqaiModel(ABC): :returns: dk: FreqaiDataKitchen = Data management/analysis tool associated to present pair only """ - # update follower - if self.follow_mode: - self.dd.update_follower_metadata() # get the model metadata associated with the current pair - (_, trained_timestamp, return_null_array) = self.dd.get_pair_dict_info(metadata["pair"]) - - # if the metadata doesn't exist, the follower returns null arrays to strategy - if self.follow_mode and return_null_array: - logger.info("Returning null array from follower to strategy") - self.dd.return_null_values_to_strategy(dataframe, dk) - return dk + (_, trained_timestamp) = self.dd.get_pair_dict_info(metadata["pair"]) # append the historic data once per round if self.dd.historic_data: @@ -398,27 +388,18 @@ class IFreqaiModel(ABC): logger.debug(f'Updating historic data on pair {metadata["pair"]}') self.track_current_candle() - if not self.follow_mode: + (_, new_trained_timerange, data_load_timerange) = dk.check_if_new_training_required( + trained_timestamp + ) + dk.set_paths(metadata["pair"], new_trained_timerange.stopts) - (_, new_trained_timerange, data_load_timerange) = dk.check_if_new_training_required( - trained_timestamp - ) - dk.set_paths(metadata["pair"], new_trained_timerange.stopts) + # load candle history into memory if it is not yet. + if not self.dd.historic_data: + self.dd.load_all_pair_histories(data_load_timerange, dk) - # load candle history into memory if it is not yet. - if not self.dd.historic_data: - self.dd.load_all_pair_histories(data_load_timerange, dk) - - if not self.scanning: - self.scanning = True - self.start_scanning(strategy) - - elif self.follow_mode: - dk.set_paths(metadata["pair"], trained_timestamp) - logger.info( - "FreqAI instance set to follow_mode, finding existing pair " - f"using { self.identifier }" - ) + if not self.scanning: + self.scanning = True + self.start_scanning(strategy) # load the model and associated data into the data kitchen self.model = self.dd.load_data(metadata["pair"], dk) @@ -506,7 +487,7 @@ class IFreqaiModel(ABC): "strategy is furnishing the same features as the pretrained" "model. In case of --strategy-list, please be aware that FreqAI " "requires all strategies to maintain identical " - "populate_any_indicator() functions" + "feature_engineering_* functions" ) def data_cleaning_train(self, dk: FreqaiDataKitchen) -> None: @@ -580,7 +561,13 @@ class IFreqaiModel(ABC): :return: :boolean: whether the model file exists or not. """ - path_to_modelfile = Path(dk.data_path / f"{dk.model_filename}_model.joblib") + if self.dd.model_type == 'joblib': + file_type = ".joblib" + elif self.dd.model_type == 'keras': + file_type = ".h5" + elif 'stable_baselines' in self.dd.model_type or 'sb3_contrib' == self.dd.model_type: + file_type = ".zip" + path_to_modelfile = Path(dk.data_path / f"{dk.model_filename}_model{file_type}") file_exists = path_to_modelfile.is_file() if file_exists: logger.info("Found model at %s", dk.data_path / dk.model_filename) @@ -612,7 +599,7 @@ class IFreqaiModel(ABC): :param strategy: IStrategy = user defined strategy object :param dk: FreqaiDataKitchen = non-persistent data container for current coin/loop :param data_load_timerange: TimeRange = the amount of data to be loaded - for populate_any_indicators + for populating indicators (larger than new_trained_timerange so that new_trained_timerange does not contain any NaNs) """ @@ -625,6 +612,8 @@ class IFreqaiModel(ABC): strategy, corr_dataframes, base_dataframes, pair ) + new_trained_timerange = dk.buffer_timerange(new_trained_timerange) + unfiltered_dataframe = dk.slice_dataframe(new_trained_timerange, unfiltered_dataframe) # find the features indicated by strategy and store in datakitchen @@ -640,8 +629,7 @@ class IFreqaiModel(ABC): if self.plot_features: plot_feature_importance(model, pair, dk, self.plot_features) - if self.freqai_info.get("purge_old_models", False): - self.dd.purge_old_models() + self.dd.purge_old_models() def set_initial_historic_predictions( self, pred_df: DataFrame, dk: FreqaiDataKitchen, pair: str, strat_df: DataFrame @@ -817,7 +805,7 @@ class IFreqaiModel(ABC): logger.warning("Couldn't cache corr_pair dataframes for improved performance. " "Consider ensuring that the full coin/stake, e.g. XYZ/USD, " "is included in the column names when you are creating features " - "in `populate_any_indicators()`.") + "in `feature_engineering_*` functions.") self.get_corr_dataframes = not bool(self.corr_dataframes) elif self.corr_dataframes: dataframe = dk.attach_corr_pair_columns( @@ -944,26 +932,6 @@ class IFreqaiModel(ABC): dk.return_dataframe, saved_dataframe, how='left', left_on='date', right_on="date_pred") return dk - def check_deprecated_populate_any_indicators(self, strategy: IStrategy): - """ - Check and warn if the deprecated populate_any_indicators function is used. - :param strategy: strategy object - """ - - if not self.warned_deprecated_populate_any_indicators: - self.warned_deprecated_populate_any_indicators = True - old_version = inspect.getsource(strategy.populate_any_indicators) != ( - inspect.getsource(IStrategy.populate_any_indicators)) - - if old_version: - logger.warning("DEPRECATION WARNING: " - "You are using the deprecated populate_any_indicators function. " - "This function will raise an error on March 1 2023. " - "Please update your strategy by using " - "the new feature_engineering functions. See \n" - "https://www.freqtrade.io/en/latest/freqai-feature-engineering/" - "for details.") - # Following methods which are overridden by user made prediction models. # See freqai/prediction_models/CatboostPredictionModel.py for an example. diff --git a/freqtrade/freqai/prediction_models/ReinforcementLearner.py b/freqtrade/freqai/prediction_models/ReinforcementLearner.py index 2a87151f9..e795703d4 100644 --- a/freqtrade/freqai/prediction_models/ReinforcementLearner.py +++ b/freqtrade/freqai/prediction_models/ReinforcementLearner.py @@ -100,7 +100,7 @@ class ReinforcementLearner(BaseReinforcementLearningModel): """ # first, penalize if the action is not valid if not self._is_valid(action): - self.tensorboard_log("is_valid") + self.tensorboard_log("invalid", category="actions") return -2 pnl = self.get_unrealized_profit() diff --git a/freqtrade/freqai/prediction_models/ReinforcementLearner_multiproc.py b/freqtrade/freqai/prediction_models/ReinforcementLearner_multiproc.py index a9be87b0b..b3b8c40e6 100644 --- a/freqtrade/freqai/prediction_models/ReinforcementLearner_multiproc.py +++ b/freqtrade/freqai/prediction_models/ReinforcementLearner_multiproc.py @@ -34,7 +34,12 @@ class ReinforcementLearner_multiproc(ReinforcementLearner): train_df = data_dictionary["train_features"] test_df = data_dictionary["test_features"] - env_info = self.pack_env_dict() + if self.train_env: + self.train_env.close() + if self.eval_env: + self.eval_env.close() + + env_info = self.pack_env_dict(dk.pair) env_id = "train_env" self.train_env = SubprocVecEnv([make_env(self.MyRLEnv, env_id, i, 1, diff --git a/freqtrade/freqai/utils.py b/freqtrade/freqai/utils.py index 806e3ca15..2ba49ac40 100644 --- a/freqtrade/freqai/utils.py +++ b/freqtrade/freqai/utils.py @@ -211,7 +211,7 @@ def record_params(config: Dict[str, Any], full_path: Path) -> None: "pairs": config.get('exchange', {}).get('pair_whitelist') } - with open(params_record_path, "w") as handle: + with params_record_path.open("w") as handle: rapidjson.dump( run_params, handle, diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index f77e1e815..623d39c09 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -127,19 +127,19 @@ class FreqtradeBot(LoggingMixin): for minutes in [0, 15, 30, 45]: t = str(time(time_slot, minutes, 2)) self._schedule.every().day.at(t).do(update) - self.last_process = datetime(1970, 1, 1, tzinfo=timezone.utc) + self.last_process: Optional[datetime] = None self.strategy.ft_bot_start() # Initialize protections AFTER bot start - otherwise parameters are not loaded. self.protections = ProtectionManager(self.config, self.strategy.protections) - def notify_status(self, msg: str) -> None: + def notify_status(self, msg: str, msg_type=RPCMessageType.STATUS) -> None: """ Public method for users of this class (worker, etc.) to send notifications via RPC about changes in the bot status. """ self.rpc.send_msg({ - 'type': RPCMessageType.STATUS, + 'type': msg_type, 'status': msg }) @@ -344,7 +344,15 @@ class FreqtradeBot(LoggingMixin): try: fo = self.exchange.fetch_order_or_stoploss_order(order.order_id, order.ft_pair, order.ft_order_side == 'stoploss') - + if not order.trade: + # This should not happen, but it does if trades were deleted manually. + # This can only incur on sqlite, which doesn't enforce foreign constraints. + logger.warning( + f"Order {order.order_id} has no trade attached. " + "This may suggest a database corruption. " + f"The expected trade ID is {order.ft_trade_id}. Ignoring this order." + ) + continue self.update_trade_state(order.trade, order.order_id, fo, stoploss_order=(order.ft_order_side == 'stoploss')) @@ -355,7 +363,7 @@ class FreqtradeBot(LoggingMixin): "Order is older than 5 days. Assuming order was fully cancelled.") fo = order.to_ccxt_object() fo['status'] = 'canceled' - self.handle_timedout_order(fo, order.trade) + self.handle_cancel_order(fo, order.trade, constants.CANCEL_REASON['TIMEOUT']) except ExchangeError as e: @@ -578,7 +586,7 @@ class FreqtradeBot(LoggingMixin): min_entry_stake = self.exchange.get_min_pair_stake_amount(trade.pair, current_entry_rate, - self.strategy.stoploss) + 0.0) min_exit_stake = self.exchange.get_min_pair_stake_amount(trade.pair, current_exit_rate, self.strategy.stoploss) @@ -586,7 +594,7 @@ class FreqtradeBot(LoggingMixin): stake_available = self.wallets.get_available_stake_amount() logger.debug(f"Calling adjust_trade_position for pair {trade.pair}") stake_amount = strategy_safe_wrapper(self.strategy.adjust_trade_position, - default_retval=None)( + default_retval=None, supress_error=True)( trade=trade, current_time=datetime.now(timezone.utc), current_rate=current_entry_rate, current_profit=current_entry_profit, min_stake=min_entry_stake, @@ -625,7 +633,7 @@ class FreqtradeBot(LoggingMixin): return remaining = (trade.amount - amount) * current_exit_rate - if remaining < min_exit_stake: + if min_exit_stake and remaining < min_exit_stake: logger.info(f"Remaining amount of {remaining} would be smaller " f"than the minimum of {min_exit_stake}.") return @@ -692,7 +700,8 @@ class FreqtradeBot(LoggingMixin): pos_adjust = trade is not None enter_limit_requested, stake_amount, leverage = self.get_valid_enter_price_and_stake( - pair, price, stake_amount, trade_side, enter_tag, trade, order_adjust, leverage_) + pair, price, stake_amount, trade_side, enter_tag, trade, order_adjust, leverage_, + pos_adjust) if not stake_amount: return False @@ -750,13 +759,15 @@ class FreqtradeBot(LoggingMixin): self.exchange.name, order['filled'], order['amount'], order['remaining'] ) - amount = safe_value_fallback(order, 'filled', 'amount') - enter_limit_filled_price = safe_value_fallback(order, 'average', 'price') + amount = safe_value_fallback(order, 'filled', 'amount', amount) + enter_limit_filled_price = safe_value_fallback( + order, 'average', 'price', enter_limit_filled_price) # in case of FOK the order may be filled immediately and fully elif order_status == 'closed': - amount = safe_value_fallback(order, 'filled', 'amount') - enter_limit_filled_price = safe_value_fallback(order, 'average', 'price') + amount = safe_value_fallback(order, 'filled', 'amount', amount) + enter_limit_filled_price = safe_value_fallback( + order, 'average', 'price', enter_limit_requested) # Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL fee = self.exchange.get_fee(symbol=pair, taker_or_maker='maker') @@ -799,6 +810,9 @@ class FreqtradeBot(LoggingMixin): precision_mode=self.exchange.precisionMode, contract_size=self.exchange.get_contract_size(pair), ) + stoploss = self.strategy.stoploss if not self.edge else self.edge.get_stoploss(pair) + trade.adjust_stop_loss(trade.open_rate, stoploss, initial=True) + else: # This is additional buy, we reset fee_open_currency so timeout checking can work trade.is_open = True @@ -808,7 +822,7 @@ class FreqtradeBot(LoggingMixin): trade.orders.append(order_obj) trade.recalc_trade_from_orders() - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() # Updating wallets @@ -831,7 +845,7 @@ class FreqtradeBot(LoggingMixin): def cancel_stoploss_on_exchange(self, trade: Trade) -> Trade: # First cancelling stoploss on exchange ... - if self.strategy.order_types.get('stoploss_on_exchange') and trade.stoploss_order_id: + if trade.stoploss_order_id: try: logger.info(f"Canceling stoploss on exchange for {trade}") co = self.exchange.cancel_stoploss_order_with_result( @@ -850,7 +864,12 @@ class FreqtradeBot(LoggingMixin): trade: Optional[Trade], order_adjust: bool, leverage_: Optional[float], + pos_adjust: bool, ) -> Tuple[float, float, float]: + """ + Validate and eventually adjust (within limits) limit, amount and leverage + :return: Tuple with (price, amount, leverage) + """ if price: enter_limit_requested = price @@ -896,7 +915,9 @@ class FreqtradeBot(LoggingMixin): # We do however also need min-stake to determine leverage, therefore this is ignored as # edge-case for now. min_stake_amount = self.exchange.get_min_pair_stake_amount( - pair, enter_limit_requested, self.strategy.stoploss, leverage) + pair, enter_limit_requested, + self.strategy.stoploss if not pos_adjust else 0.0, + leverage) max_stake_amount = self.exchange.get_max_pair_stake_amount( pair, enter_limit_requested, leverage) @@ -1003,12 +1024,16 @@ class FreqtradeBot(LoggingMixin): trades_closed = 0 for trade in trades: try: + try: + if (self.strategy.order_types.get('stoploss_on_exchange') and + self.handle_stoploss_on_exchange(trade)): + trades_closed += 1 + Trade.commit() + continue - if (self.strategy.order_types.get('stoploss_on_exchange') and - self.handle_stoploss_on_exchange(trade)): - trades_closed += 1 - Trade.commit() - continue + except InvalidOrderException as exception: + logger.warning( + f'Unable to handle stoploss on exchange for {trade.pair}: {exception}') # Check if we can sell our current pair if trade.open_order_id is None and trade.is_open and self.handle_trade(trade): trades_closed += 1 @@ -1068,7 +1093,7 @@ class FreqtradeBot(LoggingMixin): datetime.now(timezone.utc), enter=enter, exit_=exit_, - force_stoploss=self.edge.stoploss(trade.pair) if self.edge else 0 + force_stoploss=self.edge.get_stoploss(trade.pair) if self.edge else 0 ) for should_exit in exits: if should_exit.exit_flag: @@ -1088,7 +1113,7 @@ class FreqtradeBot(LoggingMixin): :return: True if the order succeeded, and False in case of problems. """ try: - stoploss_order = self.exchange.stoploss( + stoploss_order = self.exchange.create_stoploss( pair=trade.pair, amount=trade.amount, stop_price=stop_price, @@ -1112,8 +1137,7 @@ class FreqtradeBot(LoggingMixin): trade.stoploss_order_id = None logger.error(f'Unable to place a stoploss order on exchange. {e}') logger.warning('Exiting the trade forcefully') - self.execute_trade_exit(trade, stop_price, exit_check=ExitCheckTuple( - exit_type=ExitType.EMERGENCY_EXIT)) + self.emergency_exit(trade, stop_price) except ExchangeError: trade.stoploss_order_id = None @@ -1160,15 +1184,13 @@ class FreqtradeBot(LoggingMixin): # If enter order is fulfilled but there is no stoploss, we add a stoploss on exchange if not stoploss_order: - stoploss = ( - self.edge.stoploss(pair=trade.pair) - if self.edge else - trade.stop_loss_pct / trade.leverage - ) - if trade.is_short: - stop_price = trade.open_rate * (1 - stoploss) - else: - stop_price = trade.open_rate * (1 + stoploss) + stop_price = trade.stoploss_or_liquidation + if self.edge: + stoploss = self.edge.get_stoploss(pair=trade.pair) + stop_price = ( + trade.open_rate * (1 - stoploss) if trade.is_short + else trade.open_rate * (1 + stoploss) + ) if self.create_stoploss_order(trade=trade, stop_price=stop_price): # The above will return False if the placement failed and the trade was force-sold. @@ -1253,11 +1275,11 @@ class FreqtradeBot(LoggingMixin): if not_closed: if fully_cancelled or (order_obj and self.strategy.ft_check_timed_out( trade, order_obj, datetime.now(timezone.utc))): - self.handle_timedout_order(order, trade) + self.handle_cancel_order(order, trade, constants.CANCEL_REASON['TIMEOUT']) else: self.replace_order(order, order_obj, trade) - def handle_timedout_order(self, order: Dict, trade: Trade) -> None: + def handle_cancel_order(self, order: Dict, trade: Trade, reason: str) -> None: """ Check if current analyzed order timed out and cancel if necessary. :param order: Order dict grabbed with exchange.fetch_order() @@ -1265,22 +1287,24 @@ class FreqtradeBot(LoggingMixin): :return: None """ if order['side'] == trade.entry_side: - self.handle_cancel_enter(trade, order, constants.CANCEL_REASON['TIMEOUT']) + self.handle_cancel_enter(trade, order, reason) else: - canceled = self.handle_cancel_exit( - trade, order, constants.CANCEL_REASON['TIMEOUT']) + canceled = self.handle_cancel_exit(trade, order, reason) canceled_count = trade.get_exit_order_count() max_timeouts = self.config.get('unfilledtimeout', {}).get('exit_timeout_count', 0) if canceled and max_timeouts > 0 and canceled_count >= max_timeouts: logger.warning(f'Emergency exiting trade {trade}, as the exit order ' f'timed out {max_timeouts} times.') - try: - self.execute_trade_exit( - trade, order['price'], - exit_check=ExitCheckTuple(exit_type=ExitType.EMERGENCY_EXIT)) - except DependencyException as exception: - logger.warning( - f'Unable to emergency sell trade {trade.pair}: {exception}') + self.emergency_exit(trade, order['price']) + + def emergency_exit(self, trade: Trade, price: float) -> None: + try: + self.execute_trade_exit( + trade, price, + exit_check=ExitCheckTuple(exit_type=ExitType.EMERGENCY_EXIT)) + except DependencyException as exception: + logger.warning( + f'Unable to emergency exit trade {trade.pair}: {exception}') def replace_order(self, order: Dict, order_obj: Optional[Order], trade: Trade) -> None: """ @@ -1307,7 +1331,7 @@ class FreqtradeBot(LoggingMixin): default_retval=order_obj.price)( trade=trade, order=order_obj, pair=trade.pair, current_time=datetime.now(timezone.utc), proposed_rate=proposed_rate, - current_order_rate=order_obj.price, entry_tag=trade.enter_tag, + current_order_rate=order_obj.safe_price, entry_tag=trade.enter_tag, side=trade.entry_side) replacing = True @@ -1323,7 +1347,8 @@ class FreqtradeBot(LoggingMixin): # place new order only if new price is supplied self.execute_entry( pair=trade.pair, - stake_amount=(order_obj.remaining * order_obj.price / trade.leverage), + stake_amount=( + order_obj.safe_remaining * order_obj.safe_price / trade.leverage), price=adjusted_entry_price, trade=trade, is_short=trade.is_short, @@ -1337,6 +1362,8 @@ class FreqtradeBot(LoggingMixin): """ for trade in Trade.get_open_order_trades(): + if not trade.open_order_id: + continue try: order = self.exchange.fetch_order(trade.open_order_id, trade.pair) except (ExchangeError): @@ -1361,6 +1388,9 @@ class FreqtradeBot(LoggingMixin): """ was_trade_fully_canceled = False side = trade.entry_side.capitalize() + if not trade.open_order_id: + logger.warning(f"No open order for {trade}.") + return False # Cancelled orders may have the status of 'canceled' or 'closed' if order['status'] not in constants.NON_OPEN_EXCHANGE_STATES: @@ -1447,34 +1477,32 @@ class FreqtradeBot(LoggingMixin): return False try: - co = self.exchange.cancel_order_with_result(trade.open_order_id, trade.pair, - trade.amount) + order = self.exchange.cancel_order_with_result(order['id'], trade.pair, + trade.amount) except InvalidOrderException: logger.exception( f"Could not cancel {trade.exit_side} order {trade.open_order_id}") return False - trade.close_rate = None - trade.close_rate_requested = None - trade.close_profit = None - trade.close_profit_abs = None + # Set exit_reason for fill message exit_reason_prev = trade.exit_reason trade.exit_reason = trade.exit_reason + f", {reason}" if trade.exit_reason else reason - self.update_trade_state(trade, trade.open_order_id, co) # Order might be filled above in odd timing issues. - if co.get('status') in ('canceled', 'cancelled'): + if order.get('status') in ('canceled', 'cancelled'): trade.exit_reason = None - trade.open_order_id = None else: trade.exit_reason = exit_reason_prev - - logger.info(f'{trade.exit_side.capitalize()} order {reason} for {trade}.') cancelled = True else: reason = constants.CANCEL_REASON['CANCELLED_ON_EXCHANGE'] - logger.info(f'{trade.exit_side.capitalize()} order {reason} for {trade}.') - self.update_trade_state(trade, trade.open_order_id, order) - trade.open_order_id = None + trade.exit_reason = None + + self.update_trade_state(trade, trade.open_order_id, order) + + logger.info(f'{trade.exit_side.capitalize()} order {reason} for {trade}.') + trade.open_order_id = None + trade.close_rate = None + trade.close_rate_requested = None self._notify_exit_cancel( trade, @@ -1522,7 +1550,7 @@ class FreqtradeBot(LoggingMixin): *, exit_tag: Optional[str] = None, ordertype: Optional[str] = None, - sub_trade_amt: float = None, + sub_trade_amt: Optional[float] = None, ) -> bool: """ Executes a trade exit for the given trade and limit @@ -1616,7 +1644,7 @@ class FreqtradeBot(LoggingMixin): return True def _notify_exit(self, trade: Trade, order_type: str, fill: bool = False, - sub_trade: bool = False, order: Order = None) -> None: + sub_trade: bool = False, order: Optional[Order] = None) -> None: """ Sends rpc notification when a sell occurred. """ @@ -1626,13 +1654,13 @@ class FreqtradeBot(LoggingMixin): # second condition is for mypy only; order will always be passed during sub trade if sub_trade and order is not None: - amount = order.safe_filled if fill else order.amount + amount = order.safe_filled if fill else order.safe_amount order_rate: float = order.safe_price profit = trade.calc_profit(rate=order_rate, amount=amount, open_rate=trade.open_rate) profit_ratio = trade.calc_profit_ratio(order_rate, amount, trade.open_rate) else: - order_rate = trade.close_rate if trade.close_rate else trade.close_rate_requested + order_rate = trade.safe_close_rate profit = trade.calc_profit(rate=order_rate) + (0.0 if fill else trade.realized_profit) profit_ratio = trade.calc_profit_ratio(order_rate) amount = trade.amount @@ -1687,7 +1715,7 @@ class FreqtradeBot(LoggingMixin): raise DependencyException( f"Order_obj not found for {order_id}. This should not have happened.") - profit_rate = trade.close_rate if trade.close_rate else trade.close_rate_requested + profit_rate: float = trade.safe_close_rate profit_trade = trade.calc_profit(rate=profit_rate) current_rate = self.exchange.get_rate( trade.pair, side='exit', is_short=trade.is_short, refresh=False) @@ -1729,8 +1757,10 @@ class FreqtradeBot(LoggingMixin): # Common update trade state methods # - def update_trade_state(self, trade: Trade, order_id: str, action_order: Dict[str, Any] = None, - stoploss_order: bool = False, send_msg: bool = True) -> bool: + def update_trade_state( + self, trade: Trade, order_id: Optional[str], + action_order: Optional[Dict[str, Any]] = None, + stoploss_order: bool = False, send_msg: bool = True) -> bool: """ Checks trades with open orders and updates the amount if necessary Handles closing both buy and sell orders. @@ -1788,6 +1818,7 @@ class FreqtradeBot(LoggingMixin): is_short=trade.is_short, amount=trade.amount, stake_amount=trade.stake_amount, + leverage=trade.leverage, wallet_balance=trade.stake_amount, )) diff --git a/freqtrade/leverage/__init__.py b/freqtrade/leverage/__init__.py index ae78f4722..d4526dbec 100644 --- a/freqtrade/leverage/__init__.py +++ b/freqtrade/leverage/__init__.py @@ -1,2 +1 @@ -# flake8: noqa: F401 -from freqtrade.leverage.interest import interest +from freqtrade.leverage.interest import interest # noqa: F401 diff --git a/freqtrade/loggers.py b/freqtrade/loggers.py index f365053c9..823fa174e 100644 --- a/freqtrade/loggers.py +++ b/freqtrade/loggers.py @@ -103,9 +103,9 @@ def setup_logging(config: Config) -> None: logging.root.addHandler(handler_sl) elif s[0] == 'journald': # pragma: no cover try: - from systemd.journal import JournaldLogHandler + from cysystemd.journal import JournaldLogHandler except ImportError: - raise OperationalException("You need the systemd python package be installed in " + raise OperationalException("You need the cysystemd python package be installed in " "order to use logging to journald.") handler_jd = get_existing_handlers(JournaldLogHandler) if handler_jd: diff --git a/freqtrade/main.py b/freqtrade/main.py index 0a46747ea..a10620498 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -5,7 +5,7 @@ Read the documentation to know what cli arguments you need. """ import logging import sys -from typing import Any, List +from typing import Any, List, Optional from freqtrade.util.gc_setup import gc_set_threshold @@ -23,7 +23,7 @@ from freqtrade.loggers import setup_logging_pre logger = logging.getLogger('freqtrade') -def main(sysargv: List[str] = None) -> None: +def main(sysargv: Optional[List[str]] = None) -> None: """ This function will initiate the bot and start the trading loop. :return: None diff --git a/freqtrade/misc.py b/freqtrade/misc.py index 34df3185b..0cd5c6ffd 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -6,8 +6,7 @@ import logging import re from datetime import datetime from pathlib import Path -from typing import Any, Dict, Iterator, List, Mapping, Union -from typing.io import IO +from typing import Any, Dict, Iterator, List, Mapping, Optional, TextIO, Union from urllib.parse import urlparse import orjson @@ -81,7 +80,7 @@ def file_dump_json(filename: Path, data: Any, is_zip: bool = False, log: bool = else: if log: logger.info(f'dumping json to "{filename}"') - with open(filename, 'w') as fp: + with filename.open('w') as fp: rapidjson.dump(data, fp, default=str, number_mode=rapidjson.NM_NATIVE) logger.debug(f'done json to "{filename}"') @@ -98,12 +97,12 @@ def file_dump_joblib(filename: Path, data: Any, log: bool = True) -> None: if log: logger.info(f'dumping joblib to "{filename}"') - with open(filename, 'wb') as fp: + with filename.open('wb') as fp: joblib.dump(data, fp) logger.debug(f'done joblib dump to "{filename}"') -def json_load(datafile: IO) -> Any: +def json_load(datafile: Union[gzip.GzipFile, TextIO]) -> Any: """ load data with rapidjson Use this to have a consistent experience, @@ -112,7 +111,7 @@ def json_load(datafile: IO) -> Any: return rapidjson.load(datafile, number_mode=rapidjson.NM_NATIVE) -def file_load_json(file): +def file_load_json(file: Path): if file.suffix != ".gz": gzipfile = file.with_suffix(file.suffix + '.gz') @@ -125,7 +124,7 @@ def file_load_json(file): pairdata = json_load(datafile) elif file.is_file(): logger.debug(f"Loading historical data from file {file}") - with open(file) as datafile: + with file.open() as datafile: pairdata = json_load(datafile) else: return None @@ -205,7 +204,7 @@ def safe_value_fallback2(dict1: dictMap, dict2: dictMap, key1: str, key2: str, d return default_value -def plural(num: float, singular: str, plural: str = None) -> str: +def plural(num: float, singular: str, plural: Optional[str] = None) -> str: return singular if (num == 1 or num == -1) else plural or singular + 's' diff --git a/freqtrade/mixins/__init__.py b/freqtrade/mixins/__init__.py index f4a640fa3..c5363c076 100644 --- a/freqtrade/mixins/__init__.py +++ b/freqtrade/mixins/__init__.py @@ -1,2 +1 @@ -# flake8: noqa: F401 -from freqtrade.mixins.logging_mixin import LoggingMixin +from freqtrade.mixins.logging_mixin import LoggingMixin # noqa: F401 diff --git a/freqtrade/optimize/backtest_caching.py b/freqtrade/optimize/backtest_caching.py index d9d270072..f34bbffef 100644 --- a/freqtrade/optimize/backtest_caching.py +++ b/freqtrade/optimize/backtest_caching.py @@ -29,7 +29,7 @@ def get_strategy_run_id(strategy) -> str: # Include _ft_params_from_file - so changing parameter files cause cache eviction digest.update(rapidjson.dumps( strategy._ft_params_from_file, default=str, number_mode=rapidjson.NM_NAN).encode('utf-8')) - with open(strategy.__file__, 'rb') as fp: + with Path(strategy.__file__).open('rb') as fp: digest.update(fp.read()) return digest.hexdigest().lower() diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index bd543ff93..fe6667ad9 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -15,7 +15,7 @@ from pandas import DataFrame from freqtrade import constants from freqtrade.configuration import TimeRange, validate_config_consistency -from freqtrade.constants import DATETIME_PRINT_FORMAT, Config, LongShort +from freqtrade.constants import DATETIME_PRINT_FORMAT, Config, IntOrInf, LongShort from freqtrade.data import history from freqtrade.data.btanalysis import find_existing_backtest_stats, trade_list_to_dataframe from freqtrade.data.converter import trim_dataframe, trim_dataframes @@ -93,7 +93,7 @@ class Backtesting: if self.config.get('strategy_list'): if self.config.get('freqai', {}).get('enabled', False): logger.warning("Using --strategy-list with FreqAI REQUIRES all strategies " - "to have identical populate_any_indicators.") + "to have identical feature_engineering_* functions.") for strat in list(self.config['strategy_list']): stratconf = deepcopy(self.config) stratconf['strategy'] = strat @@ -440,11 +440,8 @@ class Backtesting: side_1 * abs(self.strategy.trailing_stop_positive / leverage))) else: # Worst case: price ticks tiny bit above open and dives down. - stop_rate = row[OPEN_IDX] * (1 - side_1 * abs(trade.stop_loss_pct / leverage)) - if is_short: - assert stop_rate > row[LOW_IDX] - else: - assert stop_rate < row[HIGH_IDX] + stop_rate = row[OPEN_IDX] * (1 - side_1 * abs( + (trade.stop_loss_pct or 0.0) / leverage)) # Limit lower-end to candle low to avoid exits below the low. # This still remains "worst case" - but "worst realistic case". @@ -472,7 +469,7 @@ class Backtesting: # - (Expected abs profit - open_rate - open_fee) / (fee_close -1) roi_rate = trade.open_rate * roi / leverage open_fee_rate = side_1 * trade.open_rate * (1 + side_1 * trade.fee_open) - close_rate = -(roi_rate + open_fee_rate) / (trade.fee_close - side_1 * 1) + close_rate = -(roi_rate + open_fee_rate) / ((trade.fee_close or 0.0) - side_1 * 1) if is_short: is_new_roi = row[OPEN_IDX] < close_rate else: @@ -525,7 +522,7 @@ class Backtesting: max_stake = self.exchange.get_max_pair_stake_amount(trade.pair, current_rate) stake_available = self.wallets.get_available_stake_amount() stake_amount = strategy_safe_wrapper(self.strategy.adjust_trade_position, - default_retval=None)( + default_retval=None, supress_error=True)( trade=trade, # type: ignore[arg-type] current_time=current_date, current_rate=current_rate, current_profit=current_profit, min_stake=min_stake, @@ -563,7 +560,7 @@ class Backtesting: pos_trade = self._get_exit_for_signal(trade, row, exit_, amount) if pos_trade is not None: order = pos_trade.orders[-1] - if self._get_order_filled(order.price, row): + if self._get_order_filled(order.ft_price, row): order.close_bt_order(current_date, trade) trade.recalc_trade_from_orders() self.wallets.update() @@ -575,26 +572,6 @@ class Backtesting: """ Rate is within candle, therefore filled""" return row[LOW_IDX] <= rate <= row[HIGH_IDX] - def _get_exit_trade_entry_for_candle(self, trade: LocalTrade, - row: Tuple) -> Optional[LocalTrade]: - - # Check if we need to adjust our current positions - if self.strategy.position_adjustment_enable: - trade = self._get_adjust_trade_entry_for_candle(trade, row) - - enter = row[SHORT_IDX] if trade.is_short else row[LONG_IDX] - exit_sig = row[ESHORT_IDX] if trade.is_short else row[ELONG_IDX] - exits = self.strategy.should_exit( - trade, row[OPEN_IDX], row[DATE_IDX].to_pydatetime(), # type: ignore - enter=enter, exit_=exit_sig, - low=row[LOW_IDX], high=row[HIGH_IDX] - ) - for exit_ in exits: - t = self._get_exit_for_signal(trade, row, exit_) - if t: - return t - return None - def _get_exit_for_signal( self, trade: LocalTrade, row: Tuple, exit_: ExitCheckTuple, amount: Optional[float] = None) -> Optional[LocalTrade]: @@ -664,7 +641,7 @@ class Backtesting: return None def _exit_trade(self, trade: LocalTrade, sell_row: Tuple, - close_rate: float, amount: float = None) -> Optional[LocalTrade]: + close_rate: float, amount: Optional[float] = None) -> Optional[LocalTrade]: self.order_id_counter += 1 exit_candle_time = sell_row[DATE_IDX].to_pydatetime() order_type = self.strategy.order_types['exit'] @@ -684,6 +661,7 @@ class Backtesting: side=trade.exit_side, order_type=order_type, status="open", + ft_price=close_rate, price=close_rate, average=close_rate, amount=amount, @@ -694,11 +672,10 @@ class Backtesting: trade.orders.append(order) return trade - def _get_exit_trade_entry( - self, trade: LocalTrade, row: Tuple, is_first: bool) -> Optional[LocalTrade]: + def _check_trade_exit(self, trade: LocalTrade, row: Tuple) -> Optional[LocalTrade]: exit_candle_time: datetime = row[DATE_IDX].to_pydatetime() - if is_first and self.trading_mode == TradingMode.FUTURES: + if self.trading_mode == TradingMode.FUTURES: trade.funding_fees = self.exchange.calculate_funding_fees( self.futures_data[trade.pair], amount=trade.amount, @@ -707,7 +684,22 @@ class Backtesting: close_date=exit_candle_time, ) - return self._get_exit_trade_entry_for_candle(trade, row) + # Check if we need to adjust our current positions + if self.strategy.position_adjustment_enable: + trade = self._get_adjust_trade_entry_for_candle(trade, row) + + enter = row[SHORT_IDX] if trade.is_short else row[LONG_IDX] + exit_sig = row[ESHORT_IDX] if trade.is_short else row[ELONG_IDX] + exits = self.strategy.should_exit( + trade, row[OPEN_IDX], row[DATE_IDX].to_pydatetime(), # type: ignore + enter=enter, exit_=exit_sig, + low=row[LOW_IDX], high=row[HIGH_IDX] + ) + for exit_ in exits: + t = self._get_exit_for_signal(trade, row, exit_) + if t: + return t + return None def get_valid_price_and_stake( self, pair: str, row: Tuple, propose_rate: float, stake_amount: float, @@ -753,7 +745,7 @@ class Backtesting: leverage = min(max(leverage, 1.0), max_leverage) min_stake_amount = self.exchange.get_min_pair_stake_amount( - pair, propose_rate, -0.05, leverage=leverage) or 0 + pair, propose_rate, -0.05 if not pos_adjust else 0.0, leverage=leverage) or 0 max_stake_amount = self.exchange.get_max_pair_stake_amount( pair, propose_rate, leverage=leverage) stake_available = self.wallets.get_available_stake_amount() @@ -781,6 +773,11 @@ class Backtesting: trade: Optional[LocalTrade] = None, requested_rate: Optional[float] = None, requested_stake: Optional[float] = None) -> Optional[LocalTrade]: + """ + :param trade: Trade to adjust - initial entry if None + :param requested_rate: Adjusted entry rate + :param requested_stake: Stake amount for adjusted orders (`adjust_entry_price`). + """ current_time = row[DATE_IDX].to_pydatetime() entry_tag = row[ENTER_TAG_IDX] if len(row) >= ENTER_TAG_IDX + 1 else None @@ -806,7 +803,7 @@ class Backtesting: return trade time_in_force = self.strategy.order_time_in_force['entry'] - if stake_amount and (not min_stake_amount or stake_amount > min_stake_amount): + if stake_amount and (not min_stake_amount or stake_amount >= min_stake_amount): self.order_id_counter += 1 base_currency = self.exchange.get_pair_base_currency(pair) amount_p = (stake_amount / propose_rate) * leverage @@ -869,6 +866,7 @@ class Backtesting: open_rate=propose_rate, amount=amount, stake_amount=trade.stake_amount, + leverage=trade.leverage, wallet_balance=trade.stake_amount, is_short=is_short, )) @@ -887,6 +885,7 @@ class Backtesting: order_date=current_time, order_filled_date=current_time, order_update_date=current_time, + ft_price=propose_rate, price=propose_rate, average=propose_rate, amount=amount, @@ -895,7 +894,7 @@ class Backtesting: cost=stake_amount + trade.fee_open, ) trade.orders.append(order) - if pos_adjust and self._get_order_filled(order.price, row): + if pos_adjust and self._get_order_filled(order.ft_price, row): order.close_bt_order(current_time, trade) else: trade.open_order_id = str(self.order_id_counter) @@ -922,8 +921,9 @@ class Backtesting: trade.close(exit_row[OPEN_IDX], show_msg=False) LocalTrade.close_bt_trade(trade) - def trade_slot_available(self, max_open_trades: int, open_trade_count: int) -> bool: + def trade_slot_available(self, open_trade_count: int) -> bool: # Always allow trades when max_open_trades is enabled. + max_open_trades: IntOrInf = self.config['max_open_trades'] if max_open_trades <= 0 or open_trade_count < max_open_trades: return True # Rejected trade @@ -1007,15 +1007,15 @@ class Backtesting: # only check on new candles for open entry orders if order.side == trade.entry_side and current_time > order.order_date_utc: requested_rate = strategy_safe_wrapper(self.strategy.adjust_entry_price, - default_retval=order.price)( + default_retval=order.ft_price)( trade=trade, # type: ignore[arg-type] order=order, pair=trade.pair, current_time=current_time, - proposed_rate=row[OPEN_IDX], current_order_rate=order.price, + proposed_rate=row[OPEN_IDX], current_order_rate=order.ft_price, entry_tag=trade.enter_tag, side=trade.trade_direction ) # default value is current order price # cancel existing order whenever a new rate is requested (or None) - if requested_rate == order.price: + if requested_rate == order.ft_price: # assumption: there can't be multiple open entry orders at any given time return False else: @@ -1027,7 +1027,8 @@ class Backtesting: if requested_rate: self._enter_trade(pair=trade.pair, row=row, trade=trade, requested_rate=requested_rate, - requested_stake=(order.remaining * order.price / trade.leverage), + requested_stake=( + order.safe_remaining * order.ft_price / trade.leverage), direction='short' if trade.is_short else 'long') self.replaced_entry_orders += 1 else: @@ -1053,7 +1054,7 @@ class Backtesting: def backtest_loop( self, row: Tuple, pair: str, current_time: datetime, end_date: datetime, - max_open_trades: int, open_trade_count_start: int, trade_dir: Optional[LongShort], + open_trade_count_start: int, trade_dir: Optional[LongShort], is_first: bool = True) -> int: """ NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized. @@ -1076,7 +1077,7 @@ class Backtesting: if ( (self._position_stacking or len(LocalTrade.bt_trades_open_pp[pair]) == 0) and is_first - and self.trade_slot_available(max_open_trades, open_trade_count_start) + and self.trade_slot_available(open_trade_count_start) and current_time != end_date and trade_dir is not None and not PairLocks.is_pair_locked(pair, row[DATE_IDX], trade_dir) @@ -1094,18 +1095,18 @@ class Backtesting: for trade in list(LocalTrade.bt_trades_open_pp[pair]): # 3. Process entry orders. order = trade.select_order(trade.entry_side, is_open=True) - if order and self._get_order_filled(order.price, row): + if order and self._get_order_filled(order.ft_price, row): order.close_bt_order(current_time, trade) trade.open_order_id = None self.wallets.update() # 4. Create exit orders (if any) if not trade.open_order_id: - self._get_exit_trade_entry(trade, row, is_first) # Place exit order if necessary + self._check_trade_exit(trade, row) # Place exit order if necessary # 5. Process exit orders. order = trade.select_order(trade.exit_side, is_open=True) - if order and self._get_order_filled(order.price, row): + if order and self._get_order_filled(order.ft_price, row): order.close_bt_order(current_time, trade) trade.open_order_id = None sub_trade = order.safe_amount_after_fee != trade.amount @@ -1114,7 +1115,7 @@ class Backtesting: trade.recalc_trade_from_orders() else: trade.close_date = current_time - trade.close(order.price, show_msg=False) + trade.close(order.ft_price, show_msg=False) # logger.debug(f"{pair} - Backtesting exit {trade}") LocalTrade.close_bt_trade(trade) @@ -1123,8 +1124,7 @@ class Backtesting: return open_trade_count_start def backtest(self, processed: Dict, - start_date: datetime, end_date: datetime, - max_open_trades: int = 0) -> Dict[str, Any]: + start_date: datetime, end_date: datetime) -> Dict[str, Any]: """ Implement backtesting functionality @@ -1136,7 +1136,6 @@ class Backtesting: optimize memory usage! :param start_date: backtesting timerange start datetime :param end_date: backtesting timerange end datetime - :param max_open_trades: maximum number of concurrent trades, <= 0 means unlimited :return: DataFrame with trades (results of backtesting) """ self.prepare_backtest(self.enable_protections) @@ -1185,7 +1184,7 @@ class Backtesting: if len(detail_data) == 0: # Fall back to "regular" data if no detail data was found for this candle open_trade_count_start = self.backtest_loop( - row, pair, current_time, end_date, max_open_trades, + row, pair, current_time, end_date, open_trade_count_start, trade_dir) continue detail_data.loc[:, 'enter_long'] = row[LONG_IDX] @@ -1198,13 +1197,13 @@ class Backtesting: current_time_det = current_time for det_row in detail_data[HEADERS].values.tolist(): open_trade_count_start = self.backtest_loop( - det_row, pair, current_time_det, end_date, max_open_trades, + det_row, pair, current_time_det, end_date, open_trade_count_start, trade_dir, is_first) current_time_det += timedelta(minutes=self.timeframe_detail_min) is_first = False else: open_trade_count_start = self.backtest_loop( - row, pair, current_time, end_date, max_open_trades, + row, pair, current_time, end_date, open_trade_count_start, trade_dir) # Move time one configured time_interval ahead. @@ -1237,13 +1236,11 @@ class Backtesting: self._set_strategy(strat) # Use max_open_trades in backtesting, except --disable-max-market-positions is set - if self.config.get('use_max_market_positions', True): - # Must come from strategy config, as the strategy may modify this setting. - max_open_trades = self.strategy.config['max_open_trades'] - else: + if not self.config.get('use_max_market_positions', True): logger.info( 'Ignoring max_open_trades (--disable-max-market-positions was used) ...') - max_open_trades = 0 + self.strategy.max_open_trades = float('inf') + self.config.update({'max_open_trades': self.strategy.max_open_trades}) # need to reprocess data every time to populate signals preprocessed = self.strategy.advise_all_indicators(data) @@ -1266,7 +1263,6 @@ class Backtesting: processed=preprocessed, start_date=min_date, end_date=max_date, - max_open_trades=max_open_trades, ) backtest_end_time = datetime.now(timezone.utc) results.update({ diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index b459d59f2..96c95c4a2 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -74,6 +74,7 @@ class Hyperopt: self.roi_space: List[Dimension] = [] self.stoploss_space: List[Dimension] = [] self.trailing_space: List[Dimension] = [] + self.max_open_trades_space: List[Dimension] = [] self.dimensions: List[Dimension] = [] self.config = config @@ -117,11 +118,10 @@ class Hyperopt: self.current_best_epoch: Optional[Dict[str, Any]] = None # Use max_open_trades for hyperopt as well, except --disable-max-market-positions is set - if self.config.get('use_max_market_positions', True): - self.max_open_trades = self.config['max_open_trades'] - else: + if not self.config.get('use_max_market_positions', True): logger.debug('Ignoring max_open_trades (--disable-max-market-positions was used) ...') - self.max_open_trades = 0 + self.backtesting.strategy.max_open_trades = float('inf') + config.update({'max_open_trades': self.backtesting.strategy.max_open_trades}) if HyperoptTools.has_space(self.config, 'sell'): # Make sure use_exit_signal is enabled @@ -209,6 +209,10 @@ class Hyperopt: result['stoploss'] = {p.name: params.get(p.name) for p in self.stoploss_space} if HyperoptTools.has_space(self.config, 'trailing'): result['trailing'] = self.custom_hyperopt.generate_trailing_params(params) + if HyperoptTools.has_space(self.config, 'trades'): + result['max_open_trades'] = { + 'max_open_trades': self.backtesting.strategy.max_open_trades + if self.backtesting.strategy.max_open_trades != float('inf') else -1} return result @@ -229,6 +233,8 @@ class Hyperopt: 'trailing_stop_positive_offset': strategy.trailing_stop_positive_offset, 'trailing_only_offset_is_reached': strategy.trailing_only_offset_is_reached, } + if not HyperoptTools.has_space(self.config, 'trades'): + result['max_open_trades'] = {'max_open_trades': strategy.max_open_trades} return result def print_results(self, results) -> None: @@ -280,8 +286,13 @@ class Hyperopt: logger.debug("Hyperopt has 'trailing' space") self.trailing_space = self.custom_hyperopt.trailing_space() + if HyperoptTools.has_space(self.config, 'trades'): + logger.debug("Hyperopt has 'trades' space") + self.max_open_trades_space = self.custom_hyperopt.max_open_trades_space() + self.dimensions = (self.buy_space + self.sell_space + self.protection_space - + self.roi_space + self.stoploss_space + self.trailing_space) + + self.roi_space + self.stoploss_space + self.trailing_space + + self.max_open_trades_space) def assign_params(self, params_dict: Dict, category: str) -> None: """ @@ -328,6 +339,20 @@ class Hyperopt: self.backtesting.strategy.trailing_only_offset_is_reached = \ d['trailing_only_offset_is_reached'] + if HyperoptTools.has_space(self.config, 'trades'): + if self.config["stake_amount"] == "unlimited" and \ + (params_dict['max_open_trades'] == -1 or params_dict['max_open_trades'] == 0): + # Ignore unlimited max open trades if stake amount is unlimited + params_dict.update({'max_open_trades': self.config['max_open_trades']}) + + updated_max_open_trades = int(params_dict['max_open_trades']) \ + if (params_dict['max_open_trades'] != -1 + and params_dict['max_open_trades'] != 0) else float('inf') + + self.config.update({'max_open_trades': updated_max_open_trades}) + + self.backtesting.strategy.max_open_trades = updated_max_open_trades + with self.data_pickle_file.open('rb') as f: processed = load(f, mmap_mode='r') if self.analyze_per_epoch: @@ -337,8 +362,7 @@ class Hyperopt: bt_results = self.backtesting.backtest( processed=processed, start_date=self.min_date, - end_date=self.max_date, - max_open_trades=self.max_open_trades, + end_date=self.max_date ) backtest_end_time = datetime.now(timezone.utc) bt_results.update({ diff --git a/freqtrade/optimize/hyperopt_auto.py b/freqtrade/optimize/hyperopt_auto.py index 5bc0af42b..13c036a28 100644 --- a/freqtrade/optimize/hyperopt_auto.py +++ b/freqtrade/optimize/hyperopt_auto.py @@ -91,5 +91,8 @@ class HyperOptAuto(IHyperOpt): def trailing_space(self) -> List['Dimension']: return self._get_func('trailing_space')() + def max_open_trades_space(self) -> List['Dimension']: + return self._get_func('max_open_trades_space')() + def generate_estimator(self, dimensions: List['Dimension'], **kwargs) -> EstimatorType: return self._get_func('generate_estimator')(dimensions=dimensions, **kwargs) diff --git a/freqtrade/optimize/hyperopt_interface.py b/freqtrade/optimize/hyperopt_interface.py index a7c64ffb0..65dd7ed87 100644 --- a/freqtrade/optimize/hyperopt_interface.py +++ b/freqtrade/optimize/hyperopt_interface.py @@ -191,6 +191,16 @@ class IHyperOpt(ABC): Categorical([True, False], name='trailing_only_offset_is_reached'), ] + def max_open_trades_space(self) -> List[Dimension]: + """ + Create a max open trades space. + + You may override it in your custom Hyperopt class. + """ + return [ + Integer(-1, 10, name='max_open_trades'), + ] + # This is needed for proper unpickling the class attribute timeframe # which is set to the actual value by the resolver. # Why do I still need such shamanic mantras in modern python? diff --git a/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sharpe_daily.py b/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sharpe_daily.py index 9520123ee..88c97989a 100644 --- a/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sharpe_daily.py +++ b/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sharpe_daily.py @@ -44,7 +44,7 @@ class SharpeHyperOptLossDaily(IHyperOptLoss): sum_daily = ( results.resample(resample_freq, on='close_date').agg( - {"profit_ratio_after_slippage": sum}).reindex(t_index).fillna(0) + {"profit_ratio_after_slippage": 'sum'}).reindex(t_index).fillna(0) ) total_profit = sum_daily["profit_ratio_after_slippage"] - risk_free_rate diff --git a/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sortino_daily.py b/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sortino_daily.py index fac96664d..f5fe4590e 100644 --- a/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sortino_daily.py +++ b/freqtrade/optimize/hyperopt_loss/hyperopt_loss_sortino_daily.py @@ -46,7 +46,7 @@ class SortinoHyperOptLossDaily(IHyperOptLoss): sum_daily = ( results.resample(resample_freq, on='close_date').agg( - {"profit_ratio_after_slippage": sum}).reindex(t_index).fillna(0) + {"profit_ratio_after_slippage": 'sum'}).reindex(t_index).fillna(0) ) total_profit = sum_daily["profit_ratio_after_slippage"] - minimum_acceptable_return diff --git a/freqtrade/optimize/hyperopt_tools.py b/freqtrade/optimize/hyperopt_tools.py old mode 100755 new mode 100644 index 7007ec55e..e2133a956 --- a/freqtrade/optimize/hyperopt_tools.py +++ b/freqtrade/optimize/hyperopt_tools.py @@ -1,4 +1,3 @@ -import io import logging from copy import deepcopy from datetime import datetime, timezone @@ -96,7 +95,7 @@ class HyperoptTools(): Tell if the space value is contained in the configuration """ # 'trailing' and 'protection spaces are not included in the 'default' set of spaces - if space in ('trailing', 'protection'): + if space in ('trailing', 'protection', 'trades'): return any(s in config['spaces'] for s in [space, 'all']) else: return any(s in config['spaces'] for s in [space, 'all', 'default']) @@ -170,7 +169,7 @@ class HyperoptTools(): @staticmethod def show_epoch_details(results, total_epochs: int, print_json: bool, - no_header: bool = False, header_str: str = None) -> None: + no_header: bool = False, header_str: Optional[str] = None) -> None: """ Display details of the hyperopt result """ @@ -187,7 +186,8 @@ class HyperoptTools(): if print_json: result_dict: Dict = {} - for s in ['buy', 'sell', 'protection', 'roi', 'stoploss', 'trailing']: + for s in ['buy', 'sell', 'protection', + 'roi', 'stoploss', 'trailing', 'max_open_trades']: HyperoptTools._params_update_for_json(result_dict, params, non_optimized, s) print(rapidjson.dumps(result_dict, default=str, number_mode=rapidjson.NM_NATIVE)) @@ -201,6 +201,8 @@ class HyperoptTools(): HyperoptTools._params_pretty_print(params, 'roi', "ROI table:", non_optimized) HyperoptTools._params_pretty_print(params, 'stoploss', "Stoploss:", non_optimized) HyperoptTools._params_pretty_print(params, 'trailing', "Trailing stop:", non_optimized) + HyperoptTools._params_pretty_print( + params, 'max_open_trades', "Max Open Trades:", non_optimized) @staticmethod def _params_update_for_json(result_dict, params, non_optimized, space: str) -> None: @@ -239,7 +241,9 @@ class HyperoptTools(): if space == "stoploss": stoploss = safe_value_fallback2(space_params, no_params, space, space) result += (f"stoploss = {stoploss}{appendix}") - + elif space == "max_open_trades": + max_open_trades = safe_value_fallback2(space_params, no_params, space, space) + result += (f"max_open_trades = {max_open_trades}{appendix}") elif space == "roi": result = result[:-1] + f'{appendix}\n' minimal_roi_result = rapidjson.dumps({ @@ -259,7 +263,7 @@ class HyperoptTools(): print(result) @staticmethod - def _space_params(params, space: str, r: int = None) -> Dict: + def _space_params(params, space: str, r: Optional[int] = None) -> Dict: d = params.get(space) if d: # Round floats to `r` digits after the decimal point if requested @@ -459,8 +463,8 @@ class HyperoptTools(): return try: - io.open(csv_file, 'w+').close() - except IOError: + Path(csv_file).open('w+').close() + except OSError: logger.error(f"Failed to create CSV file: {csv_file}") return diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index 7de8f1a47..83f698fbe 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -8,7 +8,7 @@ from pandas import DataFrame, to_datetime from tabulate import tabulate from freqtrade.constants import (DATETIME_PRINT_FORMAT, LAST_BT_RESULT_FN, UNLIMITED_STAKE_AMOUNT, - Config) + Config, IntOrInf) from freqtrade.data.metrics import (calculate_cagr, calculate_calmar, calculate_csum, calculate_expectancy, calculate_market_change, calculate_max_drawdown, calculate_sharpe, calculate_sortino) @@ -191,7 +191,7 @@ def generate_tag_metrics(tag_type: str, return [] -def generate_exit_reason_stats(max_open_trades: int, results: DataFrame) -> List[Dict]: +def generate_exit_reason_stats(max_open_trades: IntOrInf, results: DataFrame) -> List[Dict]: """ Generate small table outlining Backtest results :param max_open_trades: Max_open_trades parameter diff --git a/freqtrade/optimize/space/__init__.py b/freqtrade/optimize/space/__init__.py index bbdac4ab9..6c59a4d8f 100644 --- a/freqtrade/optimize/space/__init__.py +++ b/freqtrade/optimize/space/__init__.py @@ -1,4 +1,3 @@ -# flake8: noqa: F401 -from skopt.space import Categorical, Dimension, Integer, Real +from skopt.space import Categorical, Dimension, Integer, Real # noqa: F401 -from .decimalspace import SKDecimal +from .decimalspace import SKDecimal # noqa: F401 diff --git a/freqtrade/persistence/base.py b/freqtrade/persistence/base.py index fb2d561e1..fc2dac75e 100644 --- a/freqtrade/persistence/base.py +++ b/freqtrade/persistence/base.py @@ -1,7 +1,9 @@ -from typing import Any - -from sqlalchemy.orm import declarative_base +from sqlalchemy.orm import DeclarativeBase, Session, scoped_session -_DECL_BASE: Any = declarative_base() +SessionType = scoped_session[Session] + + +class ModelBase(DeclarativeBase): + pass diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 7f851322e..2315c0acc 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -2,6 +2,9 @@ This module contains the class to persist trades into SQLite """ import logging +import threading +from contextvars import ContextVar +from typing import Any, Dict, Final, Optional from sqlalchemy import create_engine, inspect from sqlalchemy.exc import NoSuchModuleError @@ -9,7 +12,7 @@ from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.pool import StaticPool from freqtrade.exceptions import OperationalException -from freqtrade.persistence.base import _DECL_BASE +from freqtrade.persistence.base import ModelBase from freqtrade.persistence.migrations import check_migrate from freqtrade.persistence.pairlock import PairLock from freqtrade.persistence.trade_model import Order, Trade @@ -18,6 +21,22 @@ from freqtrade.persistence.trade_model import Order, Trade logger = logging.getLogger(__name__) +REQUEST_ID_CTX_KEY: Final[str] = 'request_id' +_request_id_ctx_var: ContextVar[Optional[str]] = ContextVar(REQUEST_ID_CTX_KEY, default=None) + + +def get_request_or_thread_id() -> Optional[str]: + """ + Helper method to get either async context (for fastapi requests), or thread id + """ + id = _request_id_ctx_var.get() + if id is None: + # when not in request context - use thread id + id = str(threading.current_thread().ident) + + return id + + _SQL_DOCS_URL = 'http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls' @@ -29,7 +48,7 @@ def init_db(db_url: str) -> None: :param db_url: Database to use :return: None """ - kwargs = {} + kwargs: Dict[str, Any] = {} if db_url == 'sqlite:///': raise OperationalException( @@ -52,12 +71,12 @@ def init_db(db_url: str) -> None: # https://docs.sqlalchemy.org/en/13/orm/contextual.html#thread-local-scope # Scoped sessions proxy requests to the appropriate thread-local session. - # We should use the scoped_session object - not a seperately initialized version - Trade._session = scoped_session(sessionmaker(bind=engine, autoflush=False)) - Trade.query = Trade._session.query_property() - Order.query = Trade._session.query_property() - PairLock.query = Trade._session.query_property() + # Since we also use fastAPI, we need to make it aware of the request id, too + Trade.session = scoped_session(sessionmaker( + bind=engine, autoflush=False), scopefunc=get_request_or_thread_id) + Order.session = Trade.session + PairLock.session = Trade.session previous_tables = inspect(engine).get_table_names() - _DECL_BASE.metadata.create_all(engine) - check_migrate(engine, decl_base=_DECL_BASE, previous_tables=previous_tables) + ModelBase.metadata.create_all(engine) + check_migrate(engine, decl_base=ModelBase, previous_tables=previous_tables) diff --git a/freqtrade/persistence/pairlock.py b/freqtrade/persistence/pairlock.py index 926c641b0..1b254c2b2 100644 --- a/freqtrade/persistence/pairlock.py +++ b/freqtrade/persistence/pairlock.py @@ -1,33 +1,34 @@ from datetime import datetime, timezone -from typing import Any, Dict, Optional +from typing import Any, ClassVar, Dict, Optional -from sqlalchemy import Boolean, Column, DateTime, Integer, String, or_ -from sqlalchemy.orm import Query +from sqlalchemy import ScalarResult, String, or_, select +from sqlalchemy.orm import Mapped, mapped_column from freqtrade.constants import DATETIME_PRINT_FORMAT -from freqtrade.persistence.base import _DECL_BASE +from freqtrade.persistence.base import ModelBase, SessionType -class PairLock(_DECL_BASE): +class PairLock(ModelBase): """ Pair Locks database model. """ __tablename__ = 'pairlocks' + session: ClassVar[SessionType] - id = Column(Integer, primary_key=True) + id: Mapped[int] = mapped_column(primary_key=True) - pair = Column(String(25), nullable=False, index=True) + pair: Mapped[str] = mapped_column(String(25), nullable=False, index=True) # lock direction - long, short or * (for both) - side = Column(String(25), nullable=False, default="*") - reason = Column(String(255), nullable=True) + side: Mapped[str] = mapped_column(String(25), nullable=False, default="*") + reason: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) # Time the pair was locked (start time) - lock_time = Column(DateTime, nullable=False) + lock_time: Mapped[datetime] = mapped_column(nullable=False) # Time until the pair is locked (end time) - lock_end_time = Column(DateTime, nullable=False, index=True) + lock_end_time: Mapped[datetime] = mapped_column(nullable=False, index=True) - active = Column(Boolean, nullable=False, default=True, index=True) + active: Mapped[bool] = mapped_column(nullable=False, default=True, index=True) - def __repr__(self): + def __repr__(self) -> str: lock_time = self.lock_time.strftime(DATETIME_PRINT_FORMAT) lock_end_time = self.lock_end_time.strftime(DATETIME_PRINT_FORMAT) return ( @@ -35,7 +36,8 @@ class PairLock(_DECL_BASE): f'lock_end_time={lock_end_time}, reason={self.reason}, active={self.active})') @staticmethod - def query_pair_locks(pair: Optional[str], now: datetime, side: str = '*') -> Query: + def query_pair_locks( + pair: Optional[str], now: datetime, side: str = '*') -> ScalarResult['PairLock']: """ Get all currently active locks for this pair :param pair: Pair to check for. Returns all current locks if pair is empty @@ -51,9 +53,11 @@ class PairLock(_DECL_BASE): else: filters.append(PairLock.side == '*') - return PairLock.query.filter( - *filters - ) + return PairLock.session.scalars(select(PairLock).filter(*filters)) + + @staticmethod + def get_all_locks() -> ScalarResult['PairLock']: + return PairLock.session.scalars(select(PairLock)) def to_json(self) -> Dict[str, Any]: return { diff --git a/freqtrade/persistence/pairlock_middleware.py b/freqtrade/persistence/pairlock_middleware.py index 69d8b098b..29169a50d 100644 --- a/freqtrade/persistence/pairlock_middleware.py +++ b/freqtrade/persistence/pairlock_middleware.py @@ -1,6 +1,8 @@ import logging from datetime import datetime, timezone -from typing import List, Optional +from typing import List, Optional, Sequence + +from sqlalchemy import select from freqtrade.exchange import timeframe_to_next_date from freqtrade.persistence.models import PairLock @@ -30,8 +32,8 @@ class PairLocks(): PairLocks.locks = [] @staticmethod - def lock_pair(pair: str, until: datetime, reason: str = None, *, - now: datetime = None, side: str = '*') -> PairLock: + def lock_pair(pair: str, until: datetime, reason: Optional[str] = None, *, + now: Optional[datetime] = None, side: str = '*') -> PairLock: """ Create PairLock from now to "until". Uses database by default, unless PairLocks.use_db is set to False, @@ -51,15 +53,15 @@ class PairLocks(): active=True ) if PairLocks.use_db: - PairLock.query.session.add(lock) - PairLock.query.session.commit() + PairLock.session.add(lock) + PairLock.session.commit() else: PairLocks.locks.append(lock) return lock @staticmethod - def get_pair_locks( - pair: Optional[str], now: Optional[datetime] = None, side: str = '*') -> List[PairLock]: + def get_pair_locks(pair: Optional[str], now: Optional[datetime] = None, + side: str = '*') -> Sequence[PairLock]: """ Get all currently active locks for this pair :param pair: Pair to check for. Returns all current locks if pair is empty @@ -106,7 +108,7 @@ class PairLocks(): for lock in locks: lock.active = False if PairLocks.use_db: - PairLock.query.session.commit() + PairLock.session.commit() @staticmethod def unlock_reason(reason: str, now: Optional[datetime] = None) -> None: @@ -126,15 +128,15 @@ class PairLocks(): PairLock.active.is_(True), PairLock.reason == reason ] - locks = PairLock.query.filter(*filters) + locks = PairLock.session.scalars(select(PairLock).filter(*filters)).all() for lock in locks: logger.info(f"Releasing lock for {lock.pair} with reason '{reason}'.") lock.active = False - PairLock.query.session.commit() + PairLock.session.commit() else: # used in backtesting mode; don't show log messages for speed - locks = PairLocks.get_pair_locks(None) - for lock in locks: + locksb = PairLocks.get_pair_locks(None) + for lock in locksb: if lock.reason == reason: lock.active = False @@ -165,11 +167,11 @@ class PairLocks(): ) @staticmethod - def get_all_locks() -> List[PairLock]: + def get_all_locks() -> Sequence[PairLock]: """ Return all locks, also locks with expired end date """ if PairLocks.use_db: - return PairLock.query.all() + return PairLock.get_all_locks().all() else: return PairLocks.locks diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 3013df2b8..27be0d726 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -5,11 +5,11 @@ import logging from collections import defaultdict from datetime import datetime, timedelta, timezone from math import isclose -from typing import Any, Dict, List, Optional +from typing import Any, ClassVar, Dict, List, Optional, Sequence, cast -from sqlalchemy import (Boolean, Column, DateTime, Enum, Float, ForeignKey, Integer, String, - UniqueConstraint, desc, func) -from sqlalchemy.orm import Query, lazyload, relationship +from sqlalchemy import (Enum, Float, ForeignKey, Integer, ScalarResult, Select, String, + UniqueConstraint, desc, func, select) +from sqlalchemy.orm import Mapped, lazyload, mapped_column, relationship from freqtrade.constants import (DATETIME_PRINT_FORMAT, MATH_CLOSE_PREC, NON_OPEN_EXCHANGE_STATES, BuySell, LongShort) @@ -17,14 +17,14 @@ from freqtrade.enums import ExitType, TradingMode from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.exchange import amount_to_contract_precision, price_to_precision from freqtrade.leverage import interest -from freqtrade.persistence.base import _DECL_BASE +from freqtrade.persistence.base import ModelBase, SessionType from freqtrade.util import FtPrecise logger = logging.getLogger(__name__) -class Order(_DECL_BASE): +class Order(ModelBase): """ Order database model Keeps a record of all orders placed on the exchange @@ -36,41 +36,43 @@ class Order(_DECL_BASE): Mirrors CCXT Order structure """ __tablename__ = 'orders' + session: ClassVar[SessionType] + # Uniqueness should be ensured over pair, order_id # its likely that order_id is unique per Pair on some exchanges. __table_args__ = (UniqueConstraint('ft_pair', 'order_id', name="_order_pair_order_id"),) - id = Column(Integer, primary_key=True) - ft_trade_id = Column(Integer, ForeignKey('trades.id'), index=True) + id: Mapped[int] = mapped_column(Integer, primary_key=True) + ft_trade_id: Mapped[int] = mapped_column(Integer, ForeignKey('trades.id'), index=True) - trade = relationship("Trade", back_populates="orders") + trade: Mapped[List["Trade"]] = relationship("Trade", back_populates="orders") # order_side can only be 'buy', 'sell' or 'stoploss' - ft_order_side: str = Column(String(25), nullable=False) - ft_pair: str = Column(String(25), nullable=False) - ft_is_open = Column(Boolean, nullable=False, default=True, index=True) - ft_amount = Column(Float, nullable=False) - ft_price = Column(Float, nullable=False) + ft_order_side: Mapped[str] = mapped_column(String(25), nullable=False) + ft_pair: Mapped[str] = mapped_column(String(25), nullable=False) + ft_is_open: Mapped[bool] = mapped_column(nullable=False, default=True, index=True) + ft_amount: Mapped[float] = mapped_column(Float(), nullable=False) + ft_price: Mapped[float] = mapped_column(Float(), nullable=False) - order_id: str = Column(String(255), nullable=False, index=True) - status = Column(String(255), nullable=True) - symbol = Column(String(25), nullable=True) - order_type: str = Column(String(50), nullable=True) - side = Column(String(25), nullable=True) - price = Column(Float, nullable=True) - average = Column(Float, nullable=True) - amount = Column(Float, nullable=True) - filled = Column(Float, nullable=True) - remaining = Column(Float, nullable=True) - cost = Column(Float, nullable=True) - stop_price = Column(Float, nullable=True) - order_date = Column(DateTime, nullable=True, default=datetime.utcnow) - order_filled_date = Column(DateTime, nullable=True) - order_update_date = Column(DateTime, nullable=True) + order_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True) + status: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) + symbol: Mapped[Optional[str]] = mapped_column(String(25), nullable=True) + # TODO: type: order_type type is Optional[str] + order_type: Mapped[str] = mapped_column(String(50), nullable=True) + side: Mapped[str] = mapped_column(String(25), nullable=True) + price: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) + average: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) + amount: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) + filled: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) + remaining: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) + cost: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) + stop_price: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) + order_date: Mapped[datetime] = mapped_column(nullable=True, default=datetime.utcnow) + order_filled_date: Mapped[Optional[datetime]] = mapped_column(nullable=True) + order_update_date: Mapped[Optional[datetime]] = mapped_column(nullable=True) + funding_fee: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) - funding_fee = Column(Float, nullable=True) - - ft_fee_base = Column(Float, nullable=True) + ft_fee_base: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) @property def order_date_utc(self) -> datetime: @@ -96,6 +98,10 @@ class Order(_DECL_BASE): def safe_filled(self) -> float: return self.filled if self.filled is not None else self.amount or 0.0 + @property + def safe_cost(self) -> float: + return self.cost or 0.0 + @property def safe_remaining(self) -> float: return ( @@ -113,8 +119,9 @@ class Order(_DECL_BASE): def __repr__(self): - return (f'Order(id={self.id}, order_id={self.order_id}, trade_id={self.ft_trade_id}, ' - f'side={self.side}, order_type={self.order_type}, status={self.status})') + return (f"Order(id={self.id}, order_id={self.order_id}, trade_id={self.ft_trade_id}, " + f"side={self.side}, filled={self.safe_filled}, price={self.safe_price}, " + f"order_type={self.order_type}, status={self.status})") def update_from_ccxt_object(self, order): """ @@ -146,12 +153,12 @@ class Order(_DECL_BASE): # Assign funding fee up to this point # (represents the funding fee since the last order) self.funding_fee = self.trade.funding_fees - if (order.get('filled', 0.0) or 0.0) > 0: + if (order.get('filled', 0.0) or 0.0) > 0 and not self.order_filled_date: self.order_filled_date = datetime.now(timezone.utc) self.order_update_date = datetime.now(timezone.utc) def to_ccxt_object(self) -> Dict[str, Any]: - return { + order: Dict[str, Any] = { 'id': self.order_id, 'symbol': self.ft_pair, 'price': self.price, @@ -169,10 +176,13 @@ class Order(_DECL_BASE): 'fee': None, 'info': {}, } + if self.ft_order_side == 'stoploss': + order['ft_order_type'] = 'stoploss' + return order def to_json(self, entry_side: str, minified: bool = False) -> Dict[str, Any]: resp = { - 'amount': self.amount, + 'amount': self.safe_amount, 'safe_price': self.safe_price, 'ft_order_side': self.ft_order_side, 'order_filled_timestamp': int(self.order_filled_date.replace( @@ -210,7 +220,7 @@ class Order(_DECL_BASE): # Assumes backtesting will use date_last_filled_utc to calculate future funding fees. self.funding_fee = trade.funding_fees - if (self.ft_order_side == trade.entry_side): + if (self.ft_order_side == trade.entry_side and self.price): trade.open_rate = self.price trade.recalc_trade_from_orders() trade.adjust_stop_loss(trade.open_rate, trade.stop_loss_pct, refresh=True) @@ -252,12 +262,12 @@ class Order(_DECL_BASE): return o @staticmethod - def get_open_orders() -> List['Order']: + def get_open_orders() -> Sequence['Order']: """ Retrieve open orders from the database :return: List of open orders """ - return Order.query.filter(Order.ft_is_open.is_(True)).all() + return Order.session.scalars(select(Order).filter(Order.ft_is_open.is_(True))).all() @staticmethod def order_by_id(order_id: str) -> Optional['Order']: @@ -265,7 +275,7 @@ class Order(_DECL_BASE): Retrieve order based on order_id :return: Order or None """ - return Order.query.filter(Order.order_id == order_id).first() + return Order.session.scalars(select(Order).filter(Order.order_id == order_id)).first() class LocalTrade(): @@ -290,15 +300,15 @@ class LocalTrade(): exchange: str = '' pair: str = '' - base_currency: str = '' - stake_currency: str = '' + base_currency: Optional[str] = '' + stake_currency: Optional[str] = '' is_open: bool = True fee_open: float = 0.0 fee_open_cost: Optional[float] = None - fee_open_currency: str = '' - fee_close: float = 0.0 + fee_open_currency: Optional[str] = '' + fee_close: Optional[float] = 0.0 fee_close_cost: Optional[float] = None - fee_close_currency: str = '' + fee_close_currency: Optional[str] = '' open_rate: float = 0.0 open_rate_requested: Optional[float] = None # open_trade_value - calculated via _calc_open_trade_value @@ -308,7 +318,7 @@ class LocalTrade(): close_profit: Optional[float] = None close_profit_abs: Optional[float] = None stake_amount: float = 0.0 - max_stake_amount: float = 0.0 + max_stake_amount: Optional[float] = 0.0 amount: float = 0.0 amount_requested: Optional[float] = None open_date: datetime @@ -317,9 +327,9 @@ class LocalTrade(): # absolute value of the stop loss stop_loss: float = 0.0 # percentage value of the stop loss - stop_loss_pct: float = 0.0 + stop_loss_pct: Optional[float] = 0.0 # absolute value of the initial stop loss - initial_stop_loss: float = 0.0 + initial_stop_loss: Optional[float] = 0.0 # percentage value of the initial stop loss initial_stop_loss_pct: Optional[float] = None # stoploss order id which is on exchange @@ -327,12 +337,12 @@ class LocalTrade(): # last update time of the stoploss order on exchange stoploss_last_update: Optional[datetime] = None # absolute value of the highest reached price - max_rate: float = 0.0 + max_rate: Optional[float] = None # Lowest price reached - min_rate: float = 0.0 - exit_reason: str = '' - exit_order_status: str = '' - strategy: str = '' + min_rate: Optional[float] = None + exit_reason: Optional[str] = '' + exit_order_status: Optional[str] = '' + strategy: Optional[str] = '' enter_tag: Optional[str] = None timeframe: Optional[int] = None @@ -508,6 +518,8 @@ class LocalTrade(): 'close_timestamp': int(self.close_date.replace( tzinfo=timezone.utc).timestamp() * 1000) if self.close_date else None, 'realized_profit': self.realized_profit or 0.0, + # Close-profit corresponds to relative realized_profit ratio + 'realized_profit_ratio': self.close_profit or None, 'close_rate': self.close_rate, 'close_rate_requested': self.close_rate_requested, 'close_profit': self.close_profit, # Deprecated @@ -589,7 +601,7 @@ class LocalTrade(): self.stop_loss_pct = -1 * abs(percent) - def adjust_stop_loss(self, current_price: float, stoploss: float, + def adjust_stop_loss(self, current_price: float, stoploss: Optional[float], initial: bool = False, refresh: bool = False) -> None: """ This adjusts the stop loss to it's most recently observed setting @@ -598,7 +610,7 @@ class LocalTrade(): :param initial: Called to initiate stop_loss. Skips everything if self.stop_loss is already set. """ - if initial and not (self.stop_loss is None or self.stop_loss == 0): + if stoploss is None or (initial and not (self.stop_loss is None or self.stop_loss == 0)): # Don't modify if called with initial and nothing to do return refresh = True if refresh and self.nr_of_successful_entries == 1 else False @@ -637,7 +649,7 @@ class LocalTrade(): f"initial_stop_loss={self.initial_stop_loss:.8f}, " f"stop_loss={self.stop_loss:.8f}. " f"Trailing stoploss saved us: " - f"{float(self.stop_loss) - float(self.initial_stop_loss):.8f}.") + f"{float(self.stop_loss) - float(self.initial_stop_loss or 0.0):.8f}.") def update_trade(self, order: Order) -> None: """ @@ -789,17 +801,17 @@ class LocalTrade(): return interest(exchange_name=self.exchange, borrowed=borrowed, rate=rate, hours=hours) - def _calc_base_close(self, amount: FtPrecise, rate: float, fee: float) -> FtPrecise: + def _calc_base_close(self, amount: FtPrecise, rate: float, fee: Optional[float]) -> FtPrecise: close_trade = amount * FtPrecise(rate) - fees = close_trade * FtPrecise(fee) + fees = close_trade * FtPrecise(fee or 0.0) if self.is_short: return close_trade + fees else: return close_trade - fees - def calc_close_trade_value(self, rate: float, amount: float = None) -> float: + def calc_close_trade_value(self, rate: float, amount: Optional[float] = None) -> float: """ Calculate the Trade's close value including fees :param rate: rate to compare with. @@ -837,7 +849,8 @@ class LocalTrade(): raise OperationalException( f"{self.trading_mode.value} trading is not yet available using freqtrade") - def calc_profit(self, rate: float, amount: float = None, open_rate: float = None) -> float: + def calc_profit(self, rate: float, amount: Optional[float] = None, + open_rate: Optional[float] = None) -> float: """ Calculate the absolute profit in stake currency between Close and Open trade :param rate: close rate to compare with. @@ -858,7 +871,8 @@ class LocalTrade(): return float(f"{profit:.8f}") def calc_profit_ratio( - self, rate: float, amount: float = None, open_rate: float = None) -> float: + self, rate: float, amount: Optional[float] = None, + open_rate: Optional[float] = None) -> float: """ Calculates the profit as ratio (including fee). :param rate: rate to compare with. @@ -1054,13 +1068,18 @@ class LocalTrade(): return len(self.select_filled_orders('sell')) @property - def sell_reason(self) -> str: + def sell_reason(self) -> Optional[str]: """ DEPRECATED! Please use exit_reason instead.""" return self.exit_reason + @property + def safe_close_rate(self) -> float: + return self.close_rate or self.close_rate_requested or 0.0 + @staticmethod - def get_trades_proxy(*, pair: str = None, is_open: bool = None, - open_date: datetime = None, close_date: datetime = None, + def get_trades_proxy(*, pair: Optional[str] = None, is_open: Optional[bool] = None, + open_date: Optional[datetime] = None, + close_date: Optional[datetime] = None, ) -> List['LocalTrade']: """ Helper function to query Trades. @@ -1068,6 +1087,11 @@ class LocalTrade(): In live mode, converts the filter to a database query and returns all rows In Backtest mode, uses filters on Trade.trades to get the result. + :param pair: Filter by pair + :param is_open: Filter by open/closed status + :param open_date: Filter by open_date (filters via trade.open_date > input) + :param close_date: Filter by close_date (filters via trade.close_date > input) + Will implicitly only return closed trades. :return: unsorted List[Trade] """ @@ -1118,7 +1142,7 @@ class LocalTrade(): @staticmethod def get_open_trades() -> List[Any]: """ - Query trades from persistence layer + Retrieve open trades """ return Trade.get_trades_proxy(is_open=True) @@ -1128,7 +1152,9 @@ class LocalTrade(): get open trade count """ if Trade.use_db: - return Trade.query.filter(Trade.is_open.is_(True)).count() + return Trade.session.execute( + select(func.count(Trade.id)).filter(Trade.is_open.is_(True)) + ).scalar_one() else: return LocalTrade.bt_open_open_trade_count @@ -1153,7 +1179,7 @@ class LocalTrade(): logger.info(f"New stoploss: {trade.stop_loss}.") -class Trade(_DECL_BASE, LocalTrade): +class Trade(ModelBase, LocalTrade): """ Trade database model. Also handles updating and querying trades @@ -1161,79 +1187,97 @@ class Trade(_DECL_BASE, LocalTrade): Note: Fields must be aligned with LocalTrade class """ __tablename__ = 'trades' + session: ClassVar[SessionType] use_db: bool = True - id = Column(Integer, primary_key=True) + id: Mapped[int] = mapped_column(Integer, primary_key=True) # type: ignore - orders = relationship("Order", order_by="Order.id", cascade="all, delete-orphan", - lazy="selectin", innerjoin=True) + orders: Mapped[List[Order]] = relationship( + "Order", order_by="Order.id", cascade="all, delete-orphan", lazy="selectin", + innerjoin=True) # type: ignore - exchange = Column(String(25), nullable=False) - pair = Column(String(25), nullable=False, index=True) - base_currency = Column(String(25), nullable=True) - stake_currency = Column(String(25), nullable=True) - is_open = Column(Boolean, nullable=False, default=True, index=True) - fee_open = Column(Float, nullable=False, default=0.0) - fee_open_cost = Column(Float, nullable=True) - fee_open_currency = Column(String(25), nullable=True) - fee_close = Column(Float, nullable=False, default=0.0) - fee_close_cost = Column(Float, nullable=True) - fee_close_currency = Column(String(25), nullable=True) - open_rate: float = Column(Float) - open_rate_requested = Column(Float) + exchange: Mapped[str] = mapped_column(String(25), nullable=False) # type: ignore + pair: Mapped[str] = mapped_column(String(25), nullable=False, index=True) # type: ignore + base_currency: Mapped[Optional[str]] = mapped_column(String(25), nullable=True) # type: ignore + stake_currency: Mapped[Optional[str]] = mapped_column(String(25), nullable=True) # type: ignore + is_open: Mapped[bool] = mapped_column(nullable=False, default=True, index=True) # type: ignore + fee_open: Mapped[float] = mapped_column(Float(), nullable=False, default=0.0) # type: ignore + fee_open_cost: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) # type: ignore + fee_open_currency: Mapped[Optional[str]] = mapped_column( + String(25), nullable=True) # type: ignore + fee_close: Mapped[Optional[float]] = mapped_column( + Float(), nullable=False, default=0.0) # type: ignore + fee_close_cost: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) # type: ignore + fee_close_currency: Mapped[Optional[str]] = mapped_column( + String(25), nullable=True) # type: ignore + open_rate: Mapped[float] = mapped_column(Float()) # type: ignore + open_rate_requested: Mapped[Optional[float]] = mapped_column( + Float(), nullable=True) # type: ignore # open_trade_value - calculated via _calc_open_trade_value - open_trade_value = Column(Float) - close_rate: Optional[float] = Column(Float) - close_rate_requested = Column(Float) - realized_profit = Column(Float, default=0.0) - close_profit = Column(Float) - close_profit_abs = Column(Float) - stake_amount = Column(Float, nullable=False) - max_stake_amount = Column(Float) - amount = Column(Float) - amount_requested = Column(Float) - open_date = Column(DateTime, nullable=False, default=datetime.utcnow) - close_date = Column(DateTime) - open_order_id = Column(String(255)) + open_trade_value: Mapped[float] = mapped_column(Float(), nullable=True) # type: ignore + close_rate: Mapped[Optional[float]] = mapped_column(Float()) # type: ignore + close_rate_requested: Mapped[Optional[float]] = mapped_column(Float()) # type: ignore + realized_profit: Mapped[float] = mapped_column( + Float(), default=0.0, nullable=True) # type: ignore + close_profit: Mapped[Optional[float]] = mapped_column(Float()) # type: ignore + close_profit_abs: Mapped[Optional[float]] = mapped_column(Float()) # type: ignore + stake_amount: Mapped[float] = mapped_column(Float(), nullable=False) # type: ignore + max_stake_amount: Mapped[Optional[float]] = mapped_column(Float()) # type: ignore + amount: Mapped[float] = mapped_column(Float()) # type: ignore + amount_requested: Mapped[Optional[float]] = mapped_column(Float()) # type: ignore + open_date: Mapped[datetime] = mapped_column( + nullable=False, default=datetime.utcnow) # type: ignore + close_date: Mapped[Optional[datetime]] = mapped_column() # type: ignore + open_order_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) # type: ignore # absolute value of the stop loss - stop_loss = Column(Float, nullable=True, default=0.0) + stop_loss: Mapped[float] = mapped_column(Float(), nullable=True, default=0.0) # type: ignore # percentage value of the stop loss - stop_loss_pct = Column(Float, nullable=True) + stop_loss_pct: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) # type: ignore # absolute value of the initial stop loss - initial_stop_loss = Column(Float, nullable=True, default=0.0) + initial_stop_loss: Mapped[Optional[float]] = mapped_column( + Float(), nullable=True, default=0.0) # type: ignore # percentage value of the initial stop loss - initial_stop_loss_pct = Column(Float, nullable=True) + initial_stop_loss_pct: Mapped[Optional[float]] = mapped_column( + Float(), nullable=True) # type: ignore # stoploss order id which is on exchange - stoploss_order_id = Column(String(255), nullable=True, index=True) + stoploss_order_id: Mapped[Optional[str]] = mapped_column( + String(255), nullable=True, index=True) # type: ignore # last update time of the stoploss order on exchange - stoploss_last_update = Column(DateTime, nullable=True) + stoploss_last_update: Mapped[Optional[datetime]] = mapped_column(nullable=True) # type: ignore # absolute value of the highest reached price - max_rate = Column(Float, nullable=True, default=0.0) + max_rate: Mapped[Optional[float]] = mapped_column( + Float(), nullable=True, default=0.0) # type: ignore # Lowest price reached - min_rate = Column(Float, nullable=True) - exit_reason = Column(String(100), nullable=True) - exit_order_status = Column(String(100), nullable=True) - strategy = Column(String(100), nullable=True) - enter_tag = Column(String(100), nullable=True) - timeframe = Column(Integer, nullable=True) + min_rate: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) # type: ignore + exit_reason: Mapped[Optional[str]] = mapped_column(String(100), nullable=True) # type: ignore + exit_order_status: Mapped[Optional[str]] = mapped_column( + String(100), nullable=True) # type: ignore + strategy: Mapped[Optional[str]] = mapped_column(String(100), nullable=True) # type: ignore + enter_tag: Mapped[Optional[str]] = mapped_column(String(100), nullable=True) # type: ignore + timeframe: Mapped[Optional[int]] = mapped_column(Integer, nullable=True) # type: ignore - trading_mode = Column(Enum(TradingMode), nullable=True) - amount_precision = Column(Float, nullable=True) - price_precision = Column(Float, nullable=True) - precision_mode = Column(Integer, nullable=True) - contract_size = Column(Float, nullable=True) + trading_mode: Mapped[TradingMode] = mapped_column( + Enum(TradingMode), nullable=True) # type: ignore + amount_precision: Mapped[Optional[float]] = mapped_column( + Float(), nullable=True) # type: ignore + price_precision: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) # type: ignore + precision_mode: Mapped[Optional[int]] = mapped_column(Integer, nullable=True) # type: ignore + contract_size: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) # type: ignore # Leverage trading properties - leverage = Column(Float, nullable=True, default=1.0) - is_short = Column(Boolean, nullable=False, default=False) - liquidation_price = Column(Float, nullable=True) + leverage: Mapped[float] = mapped_column(Float(), nullable=True, default=1.0) # type: ignore + is_short: Mapped[bool] = mapped_column(nullable=False, default=False) # type: ignore + liquidation_price: Mapped[Optional[float]] = mapped_column( + Float(), nullable=True) # type: ignore # Margin Trading Properties - interest_rate = Column(Float, nullable=False, default=0.0) + interest_rate: Mapped[float] = mapped_column( + Float(), nullable=False, default=0.0) # type: ignore # Futures properties - funding_fees = Column(Float, nullable=True, default=None) + funding_fees: Mapped[Optional[float]] = mapped_column( + Float(), nullable=True, default=None) # type: ignore def __init__(self, **kwargs): super().__init__(**kwargs) @@ -1243,22 +1287,23 @@ class Trade(_DECL_BASE, LocalTrade): def delete(self) -> None: for order in self.orders: - Order.query.session.delete(order) + Order.session.delete(order) - Trade.query.session.delete(self) + Trade.session.delete(self) Trade.commit() @staticmethod def commit(): - Trade.query.session.commit() + Trade.session.commit() @staticmethod def rollback(): - Trade.query.session.rollback() + Trade.session.rollback() @staticmethod - def get_trades_proxy(*, pair: str = None, is_open: bool = None, - open_date: datetime = None, close_date: datetime = None, + def get_trades_proxy(*, pair: Optional[str] = None, is_open: Optional[bool] = None, + open_date: Optional[datetime] = None, + close_date: Optional[datetime] = None, ) -> List['LocalTrade']: """ Helper function to query Trades.j @@ -1278,7 +1323,7 @@ class Trade(_DECL_BASE, LocalTrade): trade_filter.append(Trade.close_date > close_date) if is_open is not None: trade_filter.append(Trade.is_open.is_(is_open)) - return Trade.get_trades(trade_filter).all() + return cast(List[LocalTrade], Trade.get_trades(trade_filter).all()) else: return LocalTrade.get_trades_proxy( pair=pair, is_open=is_open, @@ -1287,7 +1332,7 @@ class Trade(_DECL_BASE, LocalTrade): ) @staticmethod - def get_trades(trade_filter=None, include_orders: bool = True) -> Query: + def get_trades_query(trade_filter=None, include_orders: bool = True) -> Select: """ Helper function to query Trades using filters. NOTE: Not supported in Backtesting. @@ -1302,22 +1347,35 @@ class Trade(_DECL_BASE, LocalTrade): if trade_filter is not None: if not isinstance(trade_filter, list): trade_filter = [trade_filter] - this_query = Trade.query.filter(*trade_filter) + this_query = select(Trade).filter(*trade_filter) else: - this_query = Trade.query + this_query = select(Trade) if not include_orders: # Don't load order relations # Consider using noload or raiseload instead of lazyload this_query = this_query.options(lazyload(Trade.orders)) return this_query + @staticmethod + def get_trades(trade_filter=None, include_orders: bool = True) -> ScalarResult['Trade']: + """ + Helper function to query Trades using filters. + NOTE: Not supported in Backtesting. + :param trade_filter: Optional filter to apply to trades + Can be either a Filter object, or a List of filters + e.g. `(trade_filter=[Trade.id == trade_id, Trade.is_open.is_(True),])` + e.g. `(trade_filter=Trade.id == trade_id)` + :return: unsorted query object + """ + return Trade.session.scalars(Trade.get_trades_query(trade_filter, include_orders)) + @staticmethod def get_open_order_trades() -> List['Trade']: """ Returns all open trades NOTE: Not supported in Backtesting. """ - return Trade.get_trades(Trade.open_order_id.isnot(None)).all() + return cast(List[Trade], Trade.get_trades(Trade.open_order_id.isnot(None)).all()) @staticmethod def get_open_trades_without_assigned_fees(): @@ -1347,11 +1405,12 @@ class Trade(_DECL_BASE, LocalTrade): Retrieves total realized profit """ if Trade.use_db: - total_profit = Trade.query.with_entities( - func.sum(Trade.close_profit_abs)).filter(Trade.is_open.is_(False)).scalar() + total_profit: float = Trade.session.execute( + select(func.sum(Trade.close_profit_abs)).filter(Trade.is_open.is_(False)) + ).scalar_one() else: - total_profit = sum( - t.close_profit_abs for t in LocalTrade.get_trades_proxy(is_open=False)) + total_profit = sum(t.close_profit_abs # type: ignore + for t in LocalTrade.get_trades_proxy(is_open=False)) return total_profit or 0 @staticmethod @@ -1361,8 +1420,9 @@ class Trade(_DECL_BASE, LocalTrade): in stake currency """ if Trade.use_db: - total_open_stake_amount = Trade.query.with_entities( - func.sum(Trade.stake_amount)).filter(Trade.is_open.is_(True)).scalar() + total_open_stake_amount = Trade.session.scalar( + select(func.sum(Trade.stake_amount)).filter(Trade.is_open.is_(True)) + ) else: total_open_stake_amount = sum( t.stake_amount for t in LocalTrade.get_trades_proxy(is_open=True)) @@ -1374,19 +1434,22 @@ class Trade(_DECL_BASE, LocalTrade): Returns List of dicts containing all Trades, including profit and trade count NOTE: Not supported in Backtesting. """ - filters = [Trade.is_open.is_(False)] + filters: List = [Trade.is_open.is_(False)] if minutes: start_date = datetime.now(timezone.utc) - timedelta(minutes=minutes) filters.append(Trade.close_date >= start_date) - pair_rates = Trade.query.with_entities( - Trade.pair, - func.sum(Trade.close_profit).label('profit_sum'), - func.sum(Trade.close_profit_abs).label('profit_sum_abs'), - func.count(Trade.pair).label('count') - ).filter(*filters)\ - .group_by(Trade.pair) \ - .order_by(desc('profit_sum_abs')) \ - .all() + + pair_rates = Trade.session.execute( + select( + Trade.pair, + func.sum(Trade.close_profit).label('profit_sum'), + func.sum(Trade.close_profit_abs).label('profit_sum_abs'), + func.count(Trade.pair).label('count') + ).filter(*filters) + .group_by(Trade.pair) + .order_by(desc('profit_sum_abs')) + ).all() + return [ { 'pair': pair, @@ -1407,19 +1470,20 @@ class Trade(_DECL_BASE, LocalTrade): NOTE: Not supported in Backtesting. """ - filters = [Trade.is_open.is_(False)] + filters: List = [Trade.is_open.is_(False)] if (pair is not None): filters.append(Trade.pair == pair) - enter_tag_perf = Trade.query.with_entities( - Trade.enter_tag, - func.sum(Trade.close_profit).label('profit_sum'), - func.sum(Trade.close_profit_abs).label('profit_sum_abs'), - func.count(Trade.pair).label('count') - ).filter(*filters)\ - .group_by(Trade.enter_tag) \ - .order_by(desc('profit_sum_abs')) \ - .all() + enter_tag_perf = Trade.session.execute( + select( + Trade.enter_tag, + func.sum(Trade.close_profit).label('profit_sum'), + func.sum(Trade.close_profit_abs).label('profit_sum_abs'), + func.count(Trade.pair).label('count') + ).filter(*filters) + .group_by(Trade.enter_tag) + .order_by(desc('profit_sum_abs')) + ).all() return [ { @@ -1440,19 +1504,19 @@ class Trade(_DECL_BASE, LocalTrade): NOTE: Not supported in Backtesting. """ - filters = [Trade.is_open.is_(False)] + filters: List = [Trade.is_open.is_(False)] if (pair is not None): filters.append(Trade.pair == pair) - - sell_tag_perf = Trade.query.with_entities( - Trade.exit_reason, - func.sum(Trade.close_profit).label('profit_sum'), - func.sum(Trade.close_profit_abs).label('profit_sum_abs'), - func.count(Trade.pair).label('count') - ).filter(*filters)\ - .group_by(Trade.exit_reason) \ - .order_by(desc('profit_sum_abs')) \ - .all() + sell_tag_perf = Trade.session.execute( + select( + Trade.exit_reason, + func.sum(Trade.close_profit).label('profit_sum'), + func.sum(Trade.close_profit_abs).label('profit_sum_abs'), + func.count(Trade.pair).label('count') + ).filter(*filters) + .group_by(Trade.exit_reason) + .order_by(desc('profit_sum_abs')) + ).all() return [ { @@ -1473,21 +1537,21 @@ class Trade(_DECL_BASE, LocalTrade): NOTE: Not supported in Backtesting. """ - filters = [Trade.is_open.is_(False)] + filters: List = [Trade.is_open.is_(False)] if (pair is not None): filters.append(Trade.pair == pair) - - mix_tag_perf = Trade.query.with_entities( - Trade.id, - Trade.enter_tag, - Trade.exit_reason, - func.sum(Trade.close_profit).label('profit_sum'), - func.sum(Trade.close_profit_abs).label('profit_sum_abs'), - func.count(Trade.pair).label('count') - ).filter(*filters)\ - .group_by(Trade.id) \ - .order_by(desc('profit_sum_abs')) \ - .all() + mix_tag_perf = Trade.session.execute( + select( + Trade.id, + Trade.enter_tag, + Trade.exit_reason, + func.sum(Trade.close_profit).label('profit_sum'), + func.sum(Trade.close_profit_abs).label('profit_sum_abs'), + func.count(Trade.pair).label('count') + ).filter(*filters) + .group_by(Trade.id) + .order_by(desc('profit_sum_abs')) + ).all() return_list: List[Dict] = [] for id, enter_tag, exit_reason, profit, profit_abs, count in mix_tag_perf: @@ -1523,11 +1587,15 @@ class Trade(_DECL_BASE, LocalTrade): NOTE: Not supported in Backtesting. :returns: Tuple containing (pair, profit_sum) """ - best_pair = Trade.query.with_entities( - Trade.pair, func.sum(Trade.close_profit).label('profit_sum') - ).filter(Trade.is_open.is_(False) & (Trade.close_date >= start_date)) \ - .group_by(Trade.pair) \ - .order_by(desc('profit_sum')).first() + best_pair = Trade.session.execute( + select( + Trade.pair, + func.sum(Trade.close_profit).label('profit_sum') + ).filter(Trade.is_open.is_(False) & (Trade.close_date >= start_date)) + .group_by(Trade.pair) + .order_by(desc('profit_sum')) + ).first() + return best_pair @staticmethod @@ -1537,12 +1605,13 @@ class Trade(_DECL_BASE, LocalTrade): NOTE: Not supported in Backtesting. :returns: Tuple containing (pair, profit_sum) """ - trading_volume = Order.query.with_entities( - func.sum(Order.cost).label('volume') - ).filter( - Order.order_filled_date >= start_date, - Order.status == 'closed' - ).scalar() + trading_volume = Trade.session.execute( + select( + func.sum(Order.cost).label('volume') + ).filter( + Order.order_filled_date >= start_date, + Order.status == 'closed' + )).scalar_one() return trading_volume @staticmethod diff --git a/freqtrade/plot/plotting.py b/freqtrade/plot/plotting.py index 9c8787242..1b2ee44da 100644 --- a/freqtrade/plot/plotting.py +++ b/freqtrade/plot/plotting.py @@ -436,11 +436,11 @@ def create_scatter( return None -def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFrame = None, *, - indicators1: List[str] = [], - indicators2: List[str] = [], - plot_config: Dict[str, Dict] = {}, - ) -> go.Figure: +def generate_candlestick_graph( + pair: str, data: pd.DataFrame, trades: Optional[pd.DataFrame] = None, *, + indicators1: List[str] = [], indicators2: List[str] = [], + plot_config: Dict[str, Dict] = {}, + ) -> go.Figure: """ Generate the graph from the data generated by Backtesting or from DB Volume will always be ploted in row2, so Row 1 and 3 are to our disposal for custom indicators diff --git a/freqtrade/plugins/pairlist/RemotePairList.py b/freqtrade/plugins/pairlist/RemotePairList.py index b54be1fa7..764c16f1a 100644 --- a/freqtrade/plugins/pairlist/RemotePairList.py +++ b/freqtrade/plugins/pairlist/RemotePairList.py @@ -157,7 +157,7 @@ class RemotePairList(IPairList): file_path = Path(filename) if file_path.exists(): - with open(filename) as json_file: + with file_path.open() as json_file: # Load the JSON data into a dictionary jsonparse = json.load(json_file) diff --git a/freqtrade/plugins/pairlist/SpreadFilter.py b/freqtrade/plugins/pairlist/SpreadFilter.py index 207328d08..d47b68568 100644 --- a/freqtrade/plugins/pairlist/SpreadFilter.py +++ b/freqtrade/plugins/pairlist/SpreadFilter.py @@ -5,6 +5,7 @@ import logging from typing import Any, Dict, Optional from freqtrade.constants import Config +from freqtrade.exceptions import OperationalException from freqtrade.exchange.types import Ticker from freqtrade.plugins.pairlist.IPairList import IPairList @@ -22,6 +23,12 @@ class SpreadFilter(IPairList): self._max_spread_ratio = pairlistconfig.get('max_spread_ratio', 0.005) self._enabled = self._max_spread_ratio != 0 + if not self._exchange.get_option('tickers_have_bid_ask'): + raise OperationalException( + f"{self.name} requires exchange to have bid/ask data for tickers, " + "which is not available for the selected exchange / trading mode." + ) + @property def needstickers(self) -> bool: """ diff --git a/freqtrade/plugins/pairlistmanager.py b/freqtrade/plugins/pairlistmanager.py index 20a264fd8..b300f06be 100644 --- a/freqtrade/plugins/pairlistmanager.py +++ b/freqtrade/plugins/pairlistmanager.py @@ -23,7 +23,8 @@ logger = logging.getLogger(__name__) class PairListManager(LoggingMixin): - def __init__(self, exchange, config: Config, dataprovider: DataProvider = None) -> None: + def __init__( + self, exchange, config: Config, dataprovider: Optional[DataProvider] = None) -> None: self._exchange = exchange self._config = config self._whitelist = self._config['exchange'].get('pair_whitelist') @@ -153,7 +154,8 @@ class PairListManager(LoggingMixin): return [] return whitelist - def create_pair_list(self, pairs: List[str], timeframe: str = None) -> ListPairsWithTimeframes: + def create_pair_list( + self, pairs: List[str], timeframe: Optional[str] = None) -> ListPairsWithTimeframes: """ Create list of pair tuples with (pair, timeframe) """ diff --git a/freqtrade/resolvers/strategy_resolver.py b/freqtrade/resolvers/strategy_resolver.py index 67df49dcb..6f5b6655d 100644 --- a/freqtrade/resolvers/strategy_resolver.py +++ b/freqtrade/resolvers/strategy_resolver.py @@ -33,7 +33,7 @@ class StrategyResolver(IResolver): extra_path = "strategy_path" @staticmethod - def load_strategy(config: Config = None) -> IStrategy: + def load_strategy(config: Optional[Config] = None) -> IStrategy: """ Load the custom class from config parameter :param config: configuration dictionary or None @@ -76,6 +76,7 @@ class StrategyResolver(IResolver): ("ignore_buying_expired_candle_after", 0), ("position_adjustment_enable", False), ("max_entry_position_adjustment", -1), + ("max_open_trades", -1) ] for attribute, default in attributes: StrategyResolver._override_attribute_helper(strategy, config, @@ -110,7 +111,11 @@ class StrategyResolver(IResolver): val = getattr(strategy, attribute) # None's cannot exist in the config, so do not copy them if val is not None: - config[attribute] = val + # max_open_trades set to -1 in the strategy will be copied as infinity in the config + if attribute == 'max_open_trades' and val == -1: + config[attribute] = float('inf') + else: + config[attribute] = val # Explicitly check for None here as other "falsy" values are possible elif default is not None: setattr(strategy, attribute, default) @@ -128,6 +133,8 @@ class StrategyResolver(IResolver): key=lambda t: t[0])) if hasattr(strategy, 'stoploss'): strategy.stoploss = float(strategy.stoploss) + if hasattr(strategy, 'max_open_trades') and strategy.max_open_trades < 0: + strategy.max_open_trades = float('inf') return strategy @staticmethod diff --git a/freqtrade/rpc/__init__.py b/freqtrade/rpc/__init__.py index 957565e2c..07f83abc0 100644 --- a/freqtrade/rpc/__init__.py +++ b/freqtrade/rpc/__init__.py @@ -1,3 +1,2 @@ -# flake8: noqa: F401 -from .rpc import RPC, RPCException, RPCHandler -from .rpc_manager import RPCManager +from .rpc import RPC, RPCException, RPCHandler # noqa: F401 +from .rpc_manager import RPCManager # noqa: F401 diff --git a/freqtrade/rpc/api_server/__init__.py b/freqtrade/rpc/api_server/__init__.py index df255c186..b2ed3e6e0 100644 --- a/freqtrade/rpc/api_server/__init__.py +++ b/freqtrade/rpc/api_server/__init__.py @@ -1,2 +1 @@ -# flake8: noqa: F401 -from .webserver import ApiServer +from .webserver import ApiServer # noqa: F401 diff --git a/freqtrade/rpc/api_server/api_backtest.py b/freqtrade/rpc/api_server/api_backtest.py index bc2a40d91..d9d7a27f1 100644 --- a/freqtrade/rpc/api_server/api_backtest.py +++ b/freqtrade/rpc/api_server/api_backtest.py @@ -10,7 +10,7 @@ from fastapi.exceptions import HTTPException from freqtrade.configuration.config_validation import validate_config_consistency from freqtrade.data.btanalysis import get_backtest_resultlist, load_and_merge_backtest_result from freqtrade.enums import BacktestState -from freqtrade.exceptions import DependencyException +from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.misc import deep_merge_dicts from freqtrade.rpc.api_server.api_schemas import (BacktestHistoryEntry, BacktestRequest, BacktestResponse) @@ -26,9 +26,10 @@ router = APIRouter() @router.post('/backtest', response_model=BacktestResponse, tags=['webserver', 'backtest']) -# flake8: noqa: C901 -async def api_start_backtest(bt_settings: BacktestRequest, background_tasks: BackgroundTasks, - config=Depends(get_config), ws_mode=Depends(is_webserver_mode)): +async def api_start_backtest( # noqa: C901 + bt_settings: BacktestRequest, background_tasks: BackgroundTasks, + config=Depends(get_config), ws_mode=Depends(is_webserver_mode)): + ApiServer._bt['bt_error'] = None """Start backtesting if not done so already""" if ApiServer._bgtask_running: raise RPCException('Bot Background task already running') @@ -60,30 +61,31 @@ async def api_start_backtest(bt_settings: BacktestRequest, background_tasks: Bac asyncio.set_event_loop(asyncio.new_event_loop()) try: # Reload strategy - lastconfig = ApiServer._bt_last_config + lastconfig = ApiServer._bt['last_config'] strat = StrategyResolver.load_strategy(btconfig) validate_config_consistency(btconfig) if ( - not ApiServer._bt + not ApiServer._bt['bt'] or lastconfig.get('timeframe') != strat.timeframe or lastconfig.get('timeframe_detail') != btconfig.get('timeframe_detail') or lastconfig.get('timerange') != btconfig['timerange'] ): from freqtrade.optimize.backtesting import Backtesting - ApiServer._bt = Backtesting(btconfig) - ApiServer._bt.load_bt_data_detail() + ApiServer._bt['bt'] = Backtesting(btconfig) + ApiServer._bt['bt'].load_bt_data_detail() else: - ApiServer._bt.config = btconfig - ApiServer._bt.init_backtest() + ApiServer._bt['bt'].config = btconfig + ApiServer._bt['bt'].init_backtest() # Only reload data if timeframe changed. if ( - not ApiServer._bt_data - or not ApiServer._bt_timerange + not ApiServer._bt['data'] + or not ApiServer._bt['timerange'] or lastconfig.get('timeframe') != strat.timeframe or lastconfig.get('timerange') != btconfig['timerange'] ): - ApiServer._bt_data, ApiServer._bt_timerange = ApiServer._bt.load_bt_data() + ApiServer._bt['data'], ApiServer._bt['timerange'] = ApiServer._bt[ + 'bt'].load_bt_data() lastconfig['timerange'] = btconfig['timerange'] lastconfig['timeframe'] = strat.timeframe @@ -91,34 +93,35 @@ async def api_start_backtest(bt_settings: BacktestRequest, background_tasks: Bac lastconfig['enable_protections'] = btconfig.get('enable_protections') lastconfig['dry_run_wallet'] = btconfig.get('dry_run_wallet') - ApiServer._bt.enable_protections = btconfig.get('enable_protections', False) - ApiServer._bt.strategylist = [strat] - ApiServer._bt.results = {} - ApiServer._bt.load_prior_backtest() + ApiServer._bt['bt'].enable_protections = btconfig.get('enable_protections', False) + ApiServer._bt['bt'].strategylist = [strat] + ApiServer._bt['bt'].results = {} + ApiServer._bt['bt'].load_prior_backtest() - ApiServer._bt.abort = False - if (ApiServer._bt.results and - strat.get_strategy_name() in ApiServer._bt.results['strategy']): + ApiServer._bt['bt'].abort = False + if (ApiServer._bt['bt'].results and + strat.get_strategy_name() in ApiServer._bt['bt'].results['strategy']): # When previous result hash matches - reuse that result and skip backtesting. logger.info(f'Reusing result of previous backtest for {strat.get_strategy_name()}') else: - min_date, max_date = ApiServer._bt.backtest_one_strategy( - strat, ApiServer._bt_data, ApiServer._bt_timerange) + min_date, max_date = ApiServer._bt['bt'].backtest_one_strategy( + strat, ApiServer._bt['data'], ApiServer._bt['timerange']) - ApiServer._bt.results = generate_backtest_stats( - ApiServer._bt_data, ApiServer._bt.all_results, + ApiServer._bt['bt'].results = generate_backtest_stats( + ApiServer._bt['data'], ApiServer._bt['bt'].all_results, min_date=min_date, max_date=max_date) if btconfig.get('export', 'none') == 'trades': store_backtest_stats( - btconfig['exportfilename'], ApiServer._bt.results, + btconfig['exportfilename'], ApiServer._bt['bt'].results, datetime.now().strftime("%Y-%m-%d_%H-%M-%S") ) logger.info("Backtest finished.") - except DependencyException as e: - logger.info(f"Backtesting caused an error: {e}") + except (Exception, OperationalException, DependencyException) as e: + logger.exception(f"Backtesting caused an error: {e}") + ApiServer._bt['bt_error'] = str(e) pass finally: ApiServer._bgtask_running = False @@ -146,13 +149,14 @@ def api_get_backtest(ws_mode=Depends(is_webserver_mode)): return { "status": "running", "running": True, - "step": ApiServer._bt.progress.action if ApiServer._bt else str(BacktestState.STARTUP), - "progress": ApiServer._bt.progress.progress if ApiServer._bt else 0, + "step": (ApiServer._bt['bt'].progress.action if ApiServer._bt['bt'] + else str(BacktestState.STARTUP)), + "progress": ApiServer._bt['bt'].progress.progress if ApiServer._bt['bt'] else 0, "trade_count": len(LocalTrade.trades), "status_msg": "Backtest running", } - if not ApiServer._bt: + if not ApiServer._bt['bt']: return { "status": "not_started", "running": False, @@ -160,6 +164,14 @@ def api_get_backtest(ws_mode=Depends(is_webserver_mode)): "progress": 0, "status_msg": "Backtest not yet executed" } + if ApiServer._bt['bt_error']: + return { + "status": "error", + "running": False, + "step": "", + "progress": 0, + "status_msg": f"Backtest failed with {ApiServer._bt['bt_error']}" + } return { "status": "ended", @@ -167,7 +179,7 @@ def api_get_backtest(ws_mode=Depends(is_webserver_mode)): "status_msg": "Backtest ended", "step": "finished", "progress": 1, - "backtest_result": ApiServer._bt.results, + "backtest_result": ApiServer._bt['bt'].results, } @@ -182,12 +194,12 @@ def api_delete_backtest(ws_mode=Depends(is_webserver_mode)): "progress": 0, "status_msg": "Backtest running", } - if ApiServer._bt: - ApiServer._bt.cleanup() - del ApiServer._bt - ApiServer._bt = None - del ApiServer._bt_data - ApiServer._bt_data = None + if ApiServer._bt['bt']: + ApiServer._bt['bt'].cleanup() + del ApiServer._bt['bt'] + ApiServer._bt['bt'] = None + del ApiServer._bt['data'] + ApiServer._bt['data'] = None logger.info("Backtesting reset") return { "status": "reset", @@ -208,7 +220,7 @@ def api_backtest_abort(ws_mode=Depends(is_webserver_mode)): "progress": 0, "status_msg": "Backtest ended", } - ApiServer._bt.abort = True + ApiServer._bt['bt'].abort = True return { "status": "stopping", "running": False, @@ -218,14 +230,17 @@ def api_backtest_abort(ws_mode=Depends(is_webserver_mode)): } -@router.get('/backtest/history', response_model=List[BacktestHistoryEntry], tags=['webserver', 'backtest']) +@router.get('/backtest/history', response_model=List[BacktestHistoryEntry], + tags=['webserver', 'backtest']) def api_backtest_history(config=Depends(get_config), ws_mode=Depends(is_webserver_mode)): # Get backtest result history, read from metadata files return get_backtest_resultlist(config['user_data_dir'] / 'backtest_results') -@router.get('/backtest/history/result', response_model=BacktestResponse, tags=['webserver', 'backtest']) -def api_backtest_history_result(filename: str, strategy: str, config=Depends(get_config), ws_mode=Depends(is_webserver_mode)): +@router.get('/backtest/history/result', response_model=BacktestResponse, + tags=['webserver', 'backtest']) +def api_backtest_history_result(filename: str, strategy: str, config=Depends(get_config), + ws_mode=Depends(is_webserver_mode)): # Get backtest result history, read from metadata files fn = config['user_data_dir'] / 'backtest_results' / filename results: Dict[str, Any] = { diff --git a/freqtrade/rpc/api_server/api_schemas.py b/freqtrade/rpc/api_server/api_schemas.py index 404d64d16..18621ccbd 100644 --- a/freqtrade/rpc/api_server/api_schemas.py +++ b/freqtrade/rpc/api_server/api_schemas.py @@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional, Union from pydantic import BaseModel -from freqtrade.constants import DATETIME_PRINT_FORMAT +from freqtrade.constants import DATETIME_PRINT_FORMAT, IntOrInf from freqtrade.enums import OrderTypeValues, SignalDirection, TradingMode @@ -165,9 +165,10 @@ class ShowConfig(BaseModel): stake_amount: str available_capital: Optional[float] stake_currency_decimals: int - max_open_trades: int + max_open_trades: IntOrInf minimal_roi: Dict[str, Any] stoploss: Optional[float] + stoploss_on_exchange: bool trailing_stop: Optional[bool] trailing_stop_positive: Optional[float] trailing_stop_positive_offset: Optional[float] @@ -227,24 +228,33 @@ class TradeSchema(BaseModel): fee_close: Optional[float] fee_close_cost: Optional[float] fee_close_currency: Optional[str] + open_date: str open_timestamp: int open_rate: float open_rate_requested: Optional[float] open_trade_value: float + close_date: Optional[str] close_timestamp: Optional[int] close_rate: Optional[float] close_rate_requested: Optional[float] + close_profit: Optional[float] close_profit_pct: Optional[float] close_profit_abs: Optional[float] + profit_ratio: Optional[float] profit_pct: Optional[float] profit_abs: Optional[float] profit_fiat: Optional[float] + + realized_profit: float + realized_profit_ratio: Optional[float] + exit_reason: Optional[str] exit_order_status: Optional[str] + stop_loss_abs: Optional[float] stop_loss_ratio: Optional[float] stop_loss_pct: Optional[float] @@ -254,6 +264,7 @@ class TradeSchema(BaseModel): initial_stop_loss_abs: Optional[float] initial_stop_loss_ratio: Optional[float] initial_stop_loss_pct: Optional[float] + min_rate: Optional[float] max_rate: Optional[float] open_order_id: Optional[str] @@ -272,10 +283,11 @@ class OpenTradeSchema(TradeSchema): stoploss_current_dist_ratio: Optional[float] stoploss_entry_dist: Optional[float] stoploss_entry_dist_ratio: Optional[float] - current_profit: float - current_profit_abs: float - current_profit_pct: float current_rate: float + total_profit_abs: float + total_profit_fiat: Optional[float] + total_profit_ratio: Optional[float] + open_order: Optional[str] @@ -299,7 +311,7 @@ class LockModel(BaseModel): lock_timestamp: int pair: str side: str - reason: str + reason: Optional[str] class Locks(BaseModel): @@ -422,7 +434,7 @@ class BacktestRequest(BaseModel): timeframe: Optional[str] timeframe_detail: Optional[str] timerange: Optional[str] - max_open_trades: Optional[int] + max_open_trades: Optional[IntOrInf] stake_amount: Optional[str] enable_protections: bool dry_run_wallet: Optional[float] @@ -455,5 +467,5 @@ class SysInfo(BaseModel): class Health(BaseModel): - last_process: datetime - last_process_ts: int + last_process: Optional[datetime] + last_process_ts: Optional[int] diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index e26df6eea..8ea70bb69 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -40,7 +40,10 @@ logger = logging.getLogger(__name__) # 2.20: Add websocket endpoints # 2.21: Add new_candle messagetype # 2.22: Add FreqAI to backtesting -API_VERSION = 2.22 +# 2.23: Allow plot config request in webserver mode +# 2.24: Add cancel_open_order endpoint +# 2.25: Add several profit values to /status endpoint +API_VERSION = 2.25 # Public API, requires no auth. router_public = APIRouter() @@ -122,6 +125,12 @@ def trades_delete(tradeid: int, rpc: RPC = Depends(get_rpc)): return rpc._rpc_delete(tradeid) +@router.delete('/trades/{tradeid}/open-order', response_model=OpenTradeSchema, tags=['trading']) +def cancel_open_order(tradeid: int, rpc: RPC = Depends(get_rpc)): + rpc._rpc_cancel_open_order(tradeid) + return rpc._rpc_trade_status([tradeid])[0] + + # TODO: Missing response model @router.get('/edge', tags=['info']) def edge(rpc: RPC = Depends(get_rpc)): @@ -248,8 +257,18 @@ def pair_history(pair: str, timeframe: str, timerange: str, strategy: str, @router.get('/plot_config', response_model=PlotConfig, tags=['candle data']) -def plot_config(rpc: RPC = Depends(get_rpc)): - return PlotConfig.parse_obj(rpc._rpc_plot_config()) +def plot_config(strategy: Optional[str] = None, config=Depends(get_config), + rpc: Optional[RPC] = Depends(get_rpc_optional)): + if not strategy: + if not rpc: + raise RPCException("Strategy is mandatory in webserver mode.") + return PlotConfig.parse_obj(rpc._rpc_plot_config()) + else: + config1 = deepcopy(config) + config1.update({ + 'strategy': strategy + }) + return PlotConfig.parse_obj(RPC._rpc_plot_config_with_strategy(config1)) @router.get('/strategies', response_model=StrategyListResponse, tags=['strategy']) @@ -328,4 +347,4 @@ def sysinfo(): @router.get('/health', response_model=Health, tags=['info']) def health(rpc: RPC = Depends(get_rpc)): - return rpc._health() + return rpc.health() diff --git a/freqtrade/rpc/api_server/api_ws.py b/freqtrade/rpc/api_server/api_ws.py index 18714f15f..b253d66c2 100644 --- a/freqtrade/rpc/api_server/api_ws.py +++ b/freqtrade/rpc/api_server/api_ws.py @@ -90,7 +90,7 @@ async def _process_consumer_request( elif type == RPCRequestType.ANALYZED_DF: # Limit the amount of candles per dataframe to 'limit' or 1500 - limit = min(data.get('limit', 1500), 1500) if data else None + limit = int(min(data.get('limit', 1500), 1500)) if data else None pair = data.get('pair', None) if data else None # For every pair in the generator, send a separate message diff --git a/freqtrade/rpc/api_server/deps.py b/freqtrade/rpc/api_server/deps.py index aed97367b..f5b1bcd74 100644 --- a/freqtrade/rpc/api_server/deps.py +++ b/freqtrade/rpc/api_server/deps.py @@ -1,9 +1,11 @@ -from typing import Any, Dict, Iterator, Optional +from typing import Any, AsyncIterator, Dict, Optional +from uuid import uuid4 from fastapi import Depends from freqtrade.enums import RunMode from freqtrade.persistence import Trade +from freqtrade.persistence.models import _request_id_ctx_var from freqtrade.rpc.rpc import RPC, RPCException from .webserver import ApiServer @@ -15,12 +17,19 @@ def get_rpc_optional() -> Optional[RPC]: return None -def get_rpc() -> Optional[Iterator[RPC]]: +async def get_rpc() -> Optional[AsyncIterator[RPC]]: + _rpc = get_rpc_optional() if _rpc: + request_id = str(uuid4()) + ctx_token = _request_id_ctx_var.set(request_id) Trade.rollback() - yield _rpc - Trade.rollback() + try: + yield _rpc + finally: + Trade.session.remove() + _request_id_ctx_var.reset(ctx_token) + else: raise RPCException('Bot is not in the correct state') diff --git a/freqtrade/rpc/api_server/webserver.py b/freqtrade/rpc/api_server/webserver.py index 92bded1c5..b53662451 100644 --- a/freqtrade/rpc/api_server/webserver.py +++ b/freqtrade/rpc/api_server/webserver.py @@ -36,10 +36,13 @@ class ApiServer(RPCHandler): _rpc: RPC # Backtesting type: Backtesting - _bt = None - _bt_data = None - _bt_timerange = None - _bt_last_config: Config = {} + _bt: Dict[str, Any] = { + 'bt': None, + 'data': None, + 'timerange': None, + 'last_config': {}, + 'bt_error': None, + } _has_rpc: bool = False _bgtask_running: bool = False _config: Config = {} diff --git a/freqtrade/rpc/api_server/ws/__init__.py b/freqtrade/rpc/api_server/ws/__init__.py index 0b94d3fee..b76428119 100644 --- a/freqtrade/rpc/api_server/ws/__init__.py +++ b/freqtrade/rpc/api_server/ws/__init__.py @@ -1,7 +1,6 @@ -# flake8: noqa: F401 # isort: off -from freqtrade.rpc.api_server.ws.types import WebSocketType -from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy -from freqtrade.rpc.api_server.ws.serializer import HybridJSONWebSocketSerializer -from freqtrade.rpc.api_server.ws.channel import WebSocketChannel -from freqtrade.rpc.api_server.ws.message_stream import MessageStream +from freqtrade.rpc.api_server.ws.types import WebSocketType # noqa: F401 +from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy # noqa: F401 +from freqtrade.rpc.api_server.ws.serializer import HybridJSONWebSocketSerializer # noqa: F401 +from freqtrade.rpc.api_server.ws.channel import WebSocketChannel # noqa: F401 +from freqtrade.rpc.api_server.ws.message_stream import MessageStream # noqa: F401 diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index ed905d844..c6a6f5cae 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -5,7 +5,7 @@ import logging from abc import abstractmethod from datetime import date, datetime, timedelta, timezone from math import isnan -from typing import Any, Dict, Generator, List, Optional, Tuple, Union +from typing import Any, Dict, Generator, List, Optional, Sequence, Tuple, Union import arrow import psutil @@ -13,14 +13,15 @@ from dateutil.relativedelta import relativedelta from dateutil.tz import tzlocal from numpy import NAN, inf, int64, mean from pandas import DataFrame, NaT +from sqlalchemy import func, select from freqtrade import __version__ from freqtrade.configuration.timerange import TimeRange from freqtrade.constants import CANCEL_REASON, DATETIME_PRINT_FORMAT, Config from freqtrade.data.history import load_data from freqtrade.data.metrics import calculate_max_drawdown -from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, SignalDirection, State, - TradingMode) +from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, MarketDirection, SignalDirection, + State, TradingMode) from freqtrade.exceptions import ExchangeError, PricingError from freqtrade.exchange import timeframe_to_minutes, timeframe_to_msecs from freqtrade.loggers import bufferHandler @@ -122,6 +123,8 @@ class RPC: if config['max_open_trades'] != float('inf') else -1), 'minimal_roi': config['minimal_roi'].copy() if 'minimal_roi' in config else {}, 'stoploss': config.get('stoploss'), + 'stoploss_on_exchange': config.get('order_types', + {}).get('stoploss_on_exchange', False), 'trailing_stop': config.get('trailing_stop'), 'trailing_stop_positive': config.get('trailing_stop_positive'), 'trailing_stop_positive_offset': config.get('trailing_stop_positive_offset'), @@ -157,7 +160,7 @@ class RPC: """ # Fetch open trades if trade_ids: - trades: List[Trade] = Trade.get_trades(trade_filter=Trade.id.in_(trade_ids)).all() + trades: Sequence[Trade] = Trade.get_trades(trade_filter=Trade.id.in_(trade_ids)).all() else: trades = Trade.get_open_trades() @@ -168,6 +171,7 @@ class RPC: for trade in trades: order: Optional[Order] = None current_profit_fiat: Optional[float] = None + total_profit_fiat: Optional[float] = None if trade.open_order_id: order = trade.select_order_by_order_id(trade.open_order_id) # calculate profit and send message to user @@ -187,8 +191,14 @@ class RPC: else: # Closed trade ... current_rate = trade.close_rate - current_profit = trade.close_profit - current_profit_abs = trade.close_profit_abs + current_profit = trade.close_profit or 0.0 + current_profit_abs = trade.close_profit_abs or 0.0 + total_profit_abs = trade.realized_profit + current_profit_abs + total_profit_ratio: Optional[float] = None + if trade.max_stake_amount: + total_profit_ratio = ( + (total_profit_abs / trade.max_stake_amount) * trade.leverage + ) # Calculate fiat profit if not isnan(current_profit_abs) and self._fiat_converter: @@ -197,6 +207,11 @@ class RPC: self._freqtrade.config['stake_currency'], self._freqtrade.config['fiat_display_currency'] ) + total_profit_fiat = self._fiat_converter.convert_amount( + total_profit_abs, + self._freqtrade.config['stake_currency'], + self._freqtrade.config['fiat_display_currency'] + ) # Calculate guaranteed profit (in case of trailing stop) stoploss_entry_dist = trade.calc_profit(trade.stop_loss) @@ -209,14 +224,14 @@ class RPC: trade_dict.update(dict( close_profit=trade.close_profit if not trade.is_open else None, current_rate=current_rate, - current_profit=current_profit, # Deprecated - current_profit_pct=round(current_profit * 100, 2), # Deprecated - current_profit_abs=current_profit_abs, # Deprecated profit_ratio=current_profit, profit_pct=round(current_profit * 100, 2), profit_abs=current_profit_abs, profit_fiat=current_profit_fiat, + total_profit_abs=total_profit_abs, + total_profit_fiat=total_profit_fiat, + total_profit_ratio=total_profit_ratio, stoploss_current_dist=stoploss_current_dist, stoploss_current_dist_ratio=round(stoploss_current_dist_ratio, 8), stoploss_current_dist_pct=round(stoploss_current_dist_ratio * 100, 2), @@ -326,11 +341,13 @@ class RPC: for day in range(0, timescale): profitday = start_date - time_offset(day) # Only query for necessary columns for performance reasons. - trades = Trade.query.session.query(Trade.close_profit_abs).filter( - Trade.is_open.is_(False), - Trade.close_date >= profitday, - Trade.close_date < (profitday + time_offset(1)) - ).order_by(Trade.close_date).all() + trades = Trade.session.execute( + select(Trade.close_profit_abs) + .filter(Trade.is_open.is_(False), + Trade.close_date >= profitday, + Trade.close_date < (profitday + time_offset(1))) + .order_by(Trade.close_date) + ).all() curdayprofit = sum( trade.close_profit_abs for trade in trades if trade.close_profit_abs is not None) @@ -366,21 +383,27 @@ class RPC: def _rpc_trade_history(self, limit: int, offset: int = 0, order_by_id: bool = False) -> Dict: """ Returns the X last trades """ - order_by = Trade.id if order_by_id else Trade.close_date.desc() + order_by: Any = Trade.id if order_by_id else Trade.close_date.desc() if limit: - trades = Trade.get_trades([Trade.is_open.is_(False)]).order_by( - order_by).limit(limit).offset(offset) + trades = Trade.session.scalars( + Trade.get_trades_query([Trade.is_open.is_(False)]) + .order_by(order_by) + .limit(limit) + .offset(offset)) else: - trades = Trade.get_trades([Trade.is_open.is_(False)]).order_by( - Trade.close_date.desc()).all() + trades = Trade.session.scalars( + Trade.get_trades_query([Trade.is_open.is_(False)]) + .order_by(Trade.close_date.desc())) output = [trade.to_json() for trade in trades] + total_trades = Trade.session.scalar( + select(func.count(Trade.id)).filter(Trade.is_open.is_(False))) return { "trades": output, "trades_count": len(output), "offset": offset, - "total_trades": Trade.get_trades([Trade.is_open.is_(False)]).count(), + "total_trades": total_trades, } def _rpc_stats(self) -> Dict[str, Any]: @@ -394,7 +417,7 @@ class RPC: return 'losses' else: return 'draws' - trades: List[Trade] = Trade.get_trades([Trade.is_open.is_(False)], include_orders=False) + trades = Trade.get_trades([Trade.is_open.is_(False)], include_orders=False) # Sell reason exit_reasons = {} for trade in trades: @@ -403,7 +426,7 @@ class RPC: exit_reasons[trade.exit_reason][trade_win_loss(trade)] += 1 # Duration - dur: Dict[str, List[int]] = {'wins': [], 'draws': [], 'losses': []} + dur: Dict[str, List[float]] = {'wins': [], 'draws': [], 'losses': []} for trade in trades: if trade.close_date is not None and trade.open_date is not None: trade_dur = (trade.close_date - trade.open_date).total_seconds() @@ -422,8 +445,8 @@ class RPC: """ Returns cumulative profit statistics """ trade_filter = ((Trade.is_open.is_(False) & (Trade.close_date >= start_date)) | Trade.is_open.is_(True)) - trades: List[Trade] = Trade.get_trades( - trade_filter, include_orders=False).order_by(Trade.id).all() + trades: Sequence[Trade] = Trade.session.scalars(Trade.get_trades_query( + trade_filter, include_orders=False).order_by(Trade.id)).all() profit_all_coin = [] profit_all_ratio = [] @@ -442,11 +465,11 @@ class RPC: durations.append((trade.close_date - trade.open_date).total_seconds()) if not trade.is_open: - profit_ratio = trade.close_profit - profit_abs = trade.close_profit_abs + profit_ratio = trade.close_profit or 0.0 + profit_abs = trade.close_profit_abs or 0.0 profit_closed_coin.append(profit_abs) profit_closed_ratio.append(profit_ratio) - if trade.close_profit >= 0: + if profit_ratio >= 0: winning_trades += 1 winning_profit += profit_abs else: @@ -499,7 +522,7 @@ class RPC: trades_df = DataFrame([{'close_date': trade.close_date.strftime(DATETIME_PRINT_FORMAT), 'profit_abs': trade.close_profit_abs} - for trade in trades if not trade.is_open]) + for trade in trades if not trade.is_open and trade.close_date]) max_drawdown_abs = 0.0 max_drawdown = 0.0 if len(trades_df) > 0: @@ -673,6 +696,7 @@ class RPC: if self._freqtrade.state == State.RUNNING: # Set 'max_open_trades' to 0 self._freqtrade.config['max_open_trades'] = 0 + self._freqtrade.strategy.max_open_trades = 0 return {'status': 'No more entries will occur from now. Run /reload_config to reset.'} @@ -777,7 +801,8 @@ class RPC: # check if valid pair # check if pair already has an open pair - trade: Trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair == pair]).first() + trade: Optional[Trade] = Trade.get_trades( + [Trade.is_open.is_(True), Trade.pair == pair]).first() is_short = (order_side == SignalDirection.SHORT) if trade: is_short = trade.is_short @@ -811,6 +836,29 @@ class RPC: else: raise RPCException(f'Failed to enter position for {pair}.') + def _rpc_cancel_open_order(self, trade_id: int): + if self._freqtrade.state != State.RUNNING: + raise RPCException('trader is not running') + with self._freqtrade._exit_lock: + # Query for trade + trade = Trade.get_trades( + trade_filter=[Trade.id == trade_id, Trade.is_open.is_(True), ] + ).first() + if not trade: + logger.warning('cancel_open_order: Invalid trade_id received.') + raise RPCException('Invalid trade_id.') + if not trade.open_order_id: + logger.warning('cancel_open_order: No open order for trade_id.') + raise RPCException('No open order for trade_id.') + + try: + order = self._freqtrade.exchange.fetch_order(trade.open_order_id, trade.pair) + except ExchangeError as e: + logger.info(f"Cannot query order for {trade} due to {e}.", exc_info=True) + raise RPCException("Order not found.") + self._freqtrade.handle_cancel_order(order, trade, CANCEL_REASON['USER_CANCEL']) + Trade.commit() + def _rpc_delete(self, trade_id: int) -> Dict[str, Union[str, int]]: """ Handler for delete . @@ -907,12 +955,12 @@ class RPC: def _rpc_delete_lock(self, lockid: Optional[int] = None, pair: Optional[str] = None) -> Dict[str, Any]: """ Delete specific lock(s) """ - locks = [] + locks: Sequence[PairLock] = [] if pair: locks = PairLocks.get_pair_locks(pair) if lockid: - locks = PairLock.query.filter(PairLock.id == lockid).all() + locks = PairLock.session.scalars(select(PairLock).filter(PairLock.id == lockid)).all() for lock in locks: lock.active = False @@ -944,7 +992,7 @@ class RPC: resp['errors'] = errors return resp - def _rpc_blacklist(self, add: List[str] = None) -> Dict: + def _rpc_blacklist(self, add: Optional[List[str]] = None) -> Dict: """ Returns the currently active blacklist""" errors = {} if add: @@ -1126,12 +1174,12 @@ class RPC: return self._freqtrade.active_pair_whitelist @staticmethod - def _rpc_analysed_history_full(config, pair: str, timeframe: str, + def _rpc_analysed_history_full(config: Config, pair: str, timeframe: str, timerange: str, exchange) -> Dict[str, Any]: timerange_parsed = TimeRange.parse_timerange(timerange) _data = load_data( - datadir=config.get("datadir"), + datadir=config["datadir"], pairs=[pair], timeframe=timeframe, timerange=timerange_parsed, @@ -1156,6 +1204,16 @@ class RPC: self._freqtrade.strategy.plot_config['subplots'] = {} return self._freqtrade.strategy.plot_config + @staticmethod + def _rpc_plot_config_with_strategy(config: Config) -> Dict[str, Any]: + + from freqtrade.resolvers.strategy_resolver import StrategyResolver + strategy = StrategyResolver.load_strategy(config) + + if (strategy.plot_config and 'subplots' not in strategy.plot_config): + strategy.plot_config['subplots'] = {} + return strategy.plot_config + @staticmethod def _rpc_sysinfo() -> Dict[str, Any]: return { @@ -1163,10 +1221,23 @@ class RPC: "ram_pct": psutil.virtual_memory().percent } - def _health(self) -> Dict[str, Union[str, int]]: + def health(self) -> Dict[str, Optional[Union[str, int]]]: last_p = self._freqtrade.last_process + if last_p is None: + return { + "last_process": None, + "last_process_loc": None, + "last_process_ts": None, + } + return { - 'last_process': str(last_p), - 'last_process_loc': last_p.astimezone(tzlocal()).strftime(DATETIME_PRINT_FORMAT), - 'last_process_ts': int(last_p.timestamp()), + "last_process": str(last_p), + "last_process_loc": last_p.astimezone(tzlocal()).strftime(DATETIME_PRINT_FORMAT), + "last_process_ts": int(last_p.timestamp()), } + + def _update_market_direction(self, direction: MarketDirection) -> None: + self._freqtrade.strategy.market_direction = direction + + def _get_market_direction(self) -> MarketDirection: + return self._freqtrade.strategy.market_direction diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 38fe0cd13..962c5e058 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -25,7 +25,7 @@ from telegram.utils.helpers import escape_markdown from freqtrade.__init__ import __version__ from freqtrade.constants import DUST_PER_COIN, Config -from freqtrade.enums import RPCMessageType, SignalDirection, TradingMode +from freqtrade.enums import MarketDirection, RPCMessageType, SignalDirection, TradingMode from freqtrade.exceptions import OperationalException from freqtrade.misc import chunks, plural, round_coin_value from freqtrade.persistence import Trade @@ -83,6 +83,8 @@ def authorized_only(command_handler: Callable[..., None]) -> Callable[..., Any]: self._send_msg(str(e)) except BaseException: logger.exception('Exception occurred within Telegram module') + finally: + Trade.session.remove() return wrapper @@ -129,7 +131,8 @@ class Telegram(RPCHandler): r'/weekly$', r'/weekly \d+$', r'/monthly$', r'/monthly \d+$', r'/forcebuy$', r'/forcelong$', r'/forceshort$', r'/forcesell$', r'/forceexit$', - r'/edge$', r'/health$', r'/help$', r'/version$' + r'/edge$', r'/health$', r'/help$', r'/version$', r'/marketdir (long|short|even|none)$', + r'/marketdir$' ] # Create keys for generation valid_keys_print = [k.replace('$', '') for k in valid_keys] @@ -174,6 +177,7 @@ class Telegram(RPCHandler): self._force_enter, order_side=SignalDirection.SHORT)), CommandHandler('trades', self._trades), CommandHandler('delete', self._delete_trade), + CommandHandler(['coo', 'cancel_open_order'], self._cancel_open_order), CommandHandler('performance', self._performance), CommandHandler(['buys', 'entries'], self._enter_tag_performance), CommandHandler(['sells', 'exits'], self._exit_reason_performance), @@ -196,6 +200,7 @@ class Telegram(RPCHandler): CommandHandler('health', self._health), CommandHandler('help', self._help), CommandHandler('version', self._version), + CommandHandler('marketdir', self._changemarketdir) ] callbacks = [ CallbackQueryHandler(self._status_table, pattern='update_status_table'), @@ -318,31 +323,33 @@ class Telegram(RPCHandler): and self._rpc._fiat_converter): msg['profit_fiat'] = self._rpc._fiat_converter.convert_amount( msg['profit_amount'], msg['stake_currency'], msg['fiat_currency']) - msg['profit_extra'] = ( - f" / {msg['profit_fiat']:.3f} {msg['fiat_currency']}") + msg['profit_extra'] = f" / {msg['profit_fiat']:.3f} {msg['fiat_currency']}" else: msg['profit_extra'] = '' msg['profit_extra'] = ( f" ({msg['gain']}: {msg['profit_amount']:.8f} {msg['stake_currency']}" f"{msg['profit_extra']})") + is_fill = msg['type'] == RPCMessageType.EXIT_FILL is_sub_trade = msg.get('sub_trade') is_sub_profit = msg['profit_amount'] != msg.get('cumulative_profit') - profit_prefix = ('Sub ' if is_sub_profit - else 'Cumulative ') if is_sub_trade else '' + profit_prefix = ('Sub ' if is_sub_profit else 'Cumulative ') if is_sub_trade else '' cp_extra = '' + exit_wording = 'Exited' if is_fill else 'Exiting' if is_sub_profit and is_sub_trade: if self._rpc._fiat_converter: cp_fiat = self._rpc._fiat_converter.convert_amount( msg['cumulative_profit'], msg['stake_currency'], msg['fiat_currency']) cp_extra = f" / {cp_fiat:.3f} {msg['fiat_currency']}" - else: - cp_extra = '' - cp_extra = f"*Cumulative Profit:* (`{msg['cumulative_profit']:.8f} " \ - f"{msg['stake_currency']}{cp_extra}`)\n" + exit_wording = f"Partially {exit_wording.lower()}" + cp_extra = ( + f"*Cumulative Profit:* (`{msg['cumulative_profit']:.8f} " + f"{msg['stake_currency']}{cp_extra}`)\n" + ) + message = ( f"{msg['emoji']} *{self._exchange_from_msg(msg)}:* " - f"{'Exited' if is_fill else 'Exiting'} {msg['pair']} (#{msg['trade_id']})\n" + f"{exit_wording} {msg['pair']} (#{msg['trade_id']})\n" f"{self._add_analyzed_candle(msg['pair'])}" f"*{f'{profit_prefix}Profit' if is_fill else f'Unrealized {profit_prefix}Profit'}:* " f"`{msg['profit_ratio']:.2%}{msg['profit_extra']}`\n" @@ -361,7 +368,7 @@ class Telegram(RPCHandler): elif msg['type'] == RPCMessageType.EXIT_FILL: message += f"*Exit Rate:* `{msg['close_rate']:.8f}`" - if msg.get('sub_trade'): + if is_sub_trade: if self._rpc._fiat_converter: msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount( msg['stake_amount'], msg['stake_currency'], msg['fiat_currency']) @@ -409,6 +416,9 @@ class Telegram(RPCHandler): elif msg_type == RPCMessageType.WARNING: message = f"\N{WARNING SIGN} *Warning:* `{msg['status']}`" + elif msg_type == RPCMessageType.EXCEPTION: + # Errors will contain exceptions, which are wrapped in tripple ticks. + message = f"\N{WARNING SIGN} *ERROR:* \n {msg['status']}" elif msg_type == RPCMessageType.STARTUP: message = f"{msg['status']}" @@ -468,44 +478,51 @@ class Telegram(RPCHandler): lines_detail: List[str] = [] if len(filled_orders) > 0: first_avg = filled_orders[0]["safe_price"] - - for x, order in enumerate(filled_orders): + order_nr = 0 + for order in filled_orders: lines: List[str] = [] if order['is_open'] is True: continue + order_nr += 1 wording = 'Entry' if order['ft_is_entry'] else 'Exit' cur_entry_datetime = arrow.get(order["order_filled_date"]) cur_entry_amount = order["filled"] or order["amount"] cur_entry_average = order["safe_price"] lines.append(" ") - if x == 0: - lines.append(f"*{wording} #{x+1}:*") + if order_nr == 1: + lines.append(f"*{wording} #{order_nr}:*") lines.append( - f"*Amount:* {cur_entry_amount} ({order['cost']:.8f} {quote_currency})") + f"*Amount:* {cur_entry_amount} " + f"({round_coin_value(order['cost'], quote_currency)})" + ) lines.append(f"*Average Price:* {cur_entry_average}") else: - sumA = 0 - sumB = 0 - for y in range(x): - amount = filled_orders[y]["filled"] or filled_orders[y]["amount"] - sumA += amount * filled_orders[y]["safe_price"] - sumB += amount - prev_avg_price = sumA / sumB + sum_stake = 0 + sum_amount = 0 + for y in range(order_nr): + loc_order = filled_orders[y] + if loc_order['is_open'] is True: + # Skip open orders (e.g. stop orders) + continue + amount = loc_order["filled"] or loc_order["amount"] + sum_stake += amount * loc_order["safe_price"] + sum_amount += amount + prev_avg_price = sum_stake / sum_amount # TODO: This calculation ignores fees. price_to_1st_entry = ((cur_entry_average - first_avg) / first_avg) minus_on_entry = 0 if prev_avg_price: minus_on_entry = (cur_entry_average - prev_avg_price) / prev_avg_price - lines.append(f"*{wording} #{x+1}:* at {minus_on_entry:.2%} avg profit") + lines.append(f"*{wording} #{order_nr}:* at {minus_on_entry:.2%} avg Profit") if is_open: lines.append("({})".format(cur_entry_datetime .humanize(granularity=["day", "hour", "minute"]))) - lines.append( - f"*Amount:* {cur_entry_amount} ({order['cost']:.8f} {quote_currency})") + lines.append(f"*Amount:* {cur_entry_amount} " + f"({round_coin_value(order['cost'], quote_currency)})") lines.append(f"*Average {wording} Price:* {cur_entry_average} " - f"({price_to_1st_entry:.2%} from 1st entry rate)") + f"({price_to_1st_entry:.2%} from 1st entry Rate)") lines.append(f"*Order filled:* {order['order_filled_date']}") # TODO: is this really useful? @@ -517,6 +534,7 @@ class Telegram(RPCHandler): # lines.append( # f"({days}d {hours}h {minutes}m {seconds}s from previous {wording.lower()})") lines_detail.append("\n".join(lines)) + return lines_detail @authorized_only @@ -552,35 +570,54 @@ class Telegram(RPCHandler): for r in results: r['open_date_hum'] = arrow.get(r['open_date']).humanize() r['num_entries'] = len([o for o in r['orders'] if o['ft_is_entry']]) + r['num_exits'] = len([o for o in r['orders'] if not o['ft_is_entry'] + and not o['ft_order_side'] == 'stoploss']) r['exit_reason'] = r.get('exit_reason', "") + r['stake_amount_r'] = round_coin_value(r['stake_amount'], r['quote_currency']) + r['max_stake_amount_r'] = round_coin_value( + r['max_stake_amount'] or r['stake_amount'], r['quote_currency']) + r['profit_abs_r'] = round_coin_value(r['profit_abs'], r['quote_currency']) + r['realized_profit_r'] = round_coin_value(r['realized_profit'], r['quote_currency']) + r['total_profit_abs_r'] = round_coin_value( + r['total_profit_abs'], r['quote_currency']) lines = [ "*Trade ID:* `{trade_id}`" + (" `(since {open_date_hum})`" if r['is_open'] else ""), "*Current Pair:* {pair}", - "*Direction:* " + ("`Short`" if r.get('is_short') else "`Long`"), - "*Leverage:* `{leverage}`" if r.get('leverage') else "", - "*Amount:* `{amount} ({stake_amount} {quote_currency})`", + f"*Direction:* {'`Short`' if r.get('is_short') else '`Long`'}" + + " ` ({leverage}x)`" if r.get('leverage') else "", + "*Amount:* `{amount} ({stake_amount_r})`", + "*Total invested:* `{max_stake_amount_r}`" if position_adjust else "", "*Enter Tag:* `{enter_tag}`" if r['enter_tag'] else "", "*Exit Reason:* `{exit_reason}`" if r['exit_reason'] else "", ] if position_adjust: max_buy_str = (f"/{max_entries + 1}" if (max_entries > 0) else "") - lines.append("*Number of Entries:* `{num_entries}`" + max_buy_str) + lines.extend([ + "*Number of Entries:* `{num_entries}" + max_buy_str + "`", + "*Number of Exits:* `{num_exits}`" + ]) lines.extend([ "*Open Rate:* `{open_rate:.8f}`", "*Close Rate:* `{close_rate:.8f}`" if r['close_rate'] else "", "*Open Date:* `{open_date}`", "*Close Date:* `{close_date}`" if r['close_date'] else "", - "*Current Rate:* `{current_rate:.8f}`" if r['is_open'] else "", - ("*Current Profit:* " if r['is_open'] else "*Close Profit: *") - + "`{profit_ratio:.2%}`", + " \n*Current Rate:* `{current_rate:.8f}`" if r['is_open'] else "", + ("*Unrealized Profit:* " if r['is_open'] else "*Close Profit: *") + + "`{profit_ratio:.2%}` `({profit_abs_r})`", ]) if r['is_open']: if r.get('realized_profit'): - lines.append("*Realized Profit:* `{realized_profit:.8f}`") + lines.extend([ + "*Realized Profit:* `{realized_profit_ratio:.2%} ({realized_profit_r})`", + "*Total Profit:* `{total_profit_ratio:.2%} ({total_profit_abs_r})`" + ]) + + # Append empty line to improve readability + lines.append(" ") if (r['stop_loss_abs'] != r['initial_stop_loss_abs'] and r['initial_stop_loss_ratio'] is not None): # Adding initial stoploss only if it is different from stoploss @@ -1039,10 +1076,14 @@ class Telegram(RPCHandler): query.answer() query.edit_message_text(text="Force exit canceled.") return - trade: Trade = Trade.get_trades(trade_filter=Trade.id == trade_id).first() + trade: Optional[Trade] = Trade.get_trades(trade_filter=Trade.id == trade_id).first() query.answer() - query.edit_message_text(text=f"Manually exiting Trade #{trade_id}, {trade.pair}") - self._force_exit_action(trade_id) + if trade: + query.edit_message_text( + text=f"Manually exiting Trade #{trade_id}, {trade.pair}") + self._force_exit_action(trade_id) + else: + query.edit_message_text(text=f"Trade {trade_id} not found.") def _force_enter_action(self, pair, price: Optional[float], order_side: SignalDirection): if pair != 'cancel': @@ -1144,10 +1185,25 @@ class Telegram(RPCHandler): raise RPCException("Trade-id not set.") trade_id = int(context.args[0]) msg = self._rpc._rpc_delete(trade_id) - self._send_msg(( + self._send_msg( f"`{msg['result_msg']}`\n" 'Please make sure to take care of this asset on the exchange manually.' - )) + ) + + @authorized_only + def _cancel_open_order(self, update: Update, context: CallbackContext) -> None: + """ + Handler for /cancel_open_order . + Cancel open order for tradeid + :param bot: telegram bot + :param update: message update + :return: None + """ + if not context.args or len(context.args) == 0: + raise RPCException("Trade-id not set.") + trade_id = int(context.args[0]) + self._rpc._rpc_cancel_open_order(trade_id) + self._send_msg('Open order canceled.') @authorized_only def _performance(self, update: Update, context: CallbackContext) -> None: @@ -1286,7 +1342,7 @@ class Telegram(RPCHandler): message = tabulate({k: [v] for k, v in counts.items()}, headers=['current', 'max', 'total stake'], tablefmt='simple') - message = "
{}
".format(message) + message = f"
{message}
" logger.debug(message) self._send_msg(message, parse_mode=ParseMode.HTML, reload_able=True, callback_path="update_count", @@ -1456,6 +1512,10 @@ class Telegram(RPCHandler): "*/fx |all:* `Alias to /forceexit`\n" f"{force_enter_text if self._config.get('force_entry_enable', False) else ''}" "*/delete :* `Instantly delete the given trade in the database`\n" + "*/cancel_open_order :* `Cancels open orders for trade. " + "Only valid when the trade has open orders.`\n" + "*/coo |all:* `Alias to /cancel_open_order`\n" + "*/whitelist [sorted] [baseonly]:* `Show current whitelist. Optionally in " "order and/or only displaying the base currency of each pairing.`\n" "*/blacklist [pair]:* `Show current blacklist, or adds one or more pairs " @@ -1474,6 +1534,9 @@ class Telegram(RPCHandler): "*/count:* `Show number of active trades compared to allowed number of trades`\n" "*/edge:* `Shows validated pairs by Edge if it is enabled` \n" "*/health* `Show latest process timestamp - defaults to 1970-01-01 00:00:00` \n" + "*/marketdir [long | short | even | none]:* `Updates the user managed variable " + "that represents the current market direction. If no direction is provided `" + "`the currently set market direction will be output.` \n" "_Statistics_\n" "------------\n" @@ -1507,7 +1570,7 @@ class Telegram(RPCHandler): Handler for /health Shows the last process timestamp """ - health = self._rpc._health() + health = self._rpc.health() message = f"Last process: `{health['last_process_loc']}`" self._send_msg(message) @@ -1581,7 +1644,7 @@ class Telegram(RPCHandler): ]) else: reply_markup = InlineKeyboardMarkup([[]]) - msg += "\nUpdated: {}".format(datetime.now().ctime()) + msg += f"\nUpdated: {datetime.now().ctime()}" if not query.message: return chat_id = query.message.chat_id @@ -1605,7 +1668,7 @@ class Telegram(RPCHandler): def _send_msg(self, msg: str, parse_mode: str = ParseMode.MARKDOWN, disable_notification: bool = False, - keyboard: List[List[InlineKeyboardButton]] = None, + keyboard: Optional[List[List[InlineKeyboardButton]]] = None, callback_path: str = "", reload_able: bool = False, query: Optional[CallbackQuery] = None) -> None: @@ -1657,3 +1720,39 @@ class Telegram(RPCHandler): 'TelegramError: %s! Giving up on that message.', telegram_err.message ) + + @authorized_only + def _changemarketdir(self, update: Update, context: CallbackContext) -> None: + """ + Handler for /marketdir. + Updates the bot's market_direction + :param bot: telegram bot + :param update: message update + :return: None + """ + if context.args and len(context.args) == 1: + new_market_dir_arg = context.args[0] + old_market_dir = self._rpc._get_market_direction() + new_market_dir = None + if new_market_dir_arg == "long": + new_market_dir = MarketDirection.LONG + elif new_market_dir_arg == "short": + new_market_dir = MarketDirection.SHORT + elif new_market_dir_arg == "even": + new_market_dir = MarketDirection.EVEN + elif new_market_dir_arg == "none": + new_market_dir = MarketDirection.NONE + + if new_market_dir is not None: + self._rpc._update_market_direction(new_market_dir) + self._send_msg("Successfully updated market direction" + f" from *{old_market_dir}* to *{new_market_dir}*.") + else: + raise RPCException("Invalid market direction provided. \n" + "Valid market directions: *long, short, even, none*") + elif context.args is not None and len(context.args) == 0: + old_market_dir = self._rpc._get_market_direction() + self._send_msg(f"Currently set market direction: *{old_market_dir}*") + else: + raise RPCException("Invalid usage of command /marketdir. \n" + "Usage: */marketdir [short | long | even | none]*") diff --git a/freqtrade/rpc/webhook.py b/freqtrade/rpc/webhook.py index d81d8d24f..118ebed88 100644 --- a/freqtrade/rpc/webhook.py +++ b/freqtrade/rpc/webhook.py @@ -58,6 +58,7 @@ class Webhook(RPCHandler): valuedict = whconfig.get('webhookexitcancel') elif msg['type'] in (RPCMessageType.STATUS, RPCMessageType.STARTUP, + RPCMessageType.EXCEPTION, RPCMessageType.WARNING): valuedict = whconfig.get('webhookstatus') elif msg['type'].value in whconfig: @@ -112,7 +113,7 @@ class Webhook(RPCHandler): response = post(self._url, data=payload['data'], headers={'Content-Type': 'text/plain'}) else: - raise NotImplementedError('Unknown format: {}'.format(self._format)) + raise NotImplementedError(f'Unknown format: {self._format}') # Throw a RequestException if the post was not successful response.raise_for_status() diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index 6f62c9d3d..52ba22951 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -4,7 +4,7 @@ This module defines a base class for auto-hyperoptable strategies. """ import logging from pathlib import Path -from typing import Any, Dict, Iterator, List, Tuple, Type, Union +from typing import Any, Dict, Iterator, List, Optional, Tuple, Type, Union from freqtrade.constants import Config from freqtrade.exceptions import OperationalException @@ -36,7 +36,8 @@ class HyperStrategyMixin: self._ft_params_from_file = params # Init/loading of parameters is done as part of ft_bot_start(). - def enumerate_parameters(self, category: str = None) -> Iterator[Tuple[str, BaseParameter]]: + def enumerate_parameters( + self, category: Optional[str] = None) -> Iterator[Tuple[str, BaseParameter]]: """ Find all optimizable parameters and return (name, attr) iterator. :param category: @@ -80,6 +81,8 @@ class HyperStrategyMixin: self.stoploss = params.get('stoploss', {}).get( 'stoploss', getattr(self, 'stoploss', -0.1)) + self.max_open_trades = params.get('max_open_trades', {}).get( + 'max_open_trades', getattr(self, 'max_open_trades', -1)) trailing = params.get('trailing', {}) self.trailing_stop = trailing.get( 'trailing_stop', getattr(self, 'trailing_stop', False)) @@ -160,7 +163,7 @@ class HyperStrategyMixin: else: logger.info(f'Strategy Parameter(default): {attr_name} = {attr.value}') - def get_no_optimize_params(self): + def get_no_optimize_params(self) -> Dict[str, Dict]: """ Returns list of Parameters that are not part of the current optimize job """ @@ -170,7 +173,7 @@ class HyperStrategyMixin: 'protection': {}, } for name, p in self.enumerate_parameters(): - if not p.optimize or not p.in_space: + if p.category and (not p.optimize or not p.in_space): params[p.category][name] = p.value return params diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 50ae2341e..96b2ac8ce 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -10,10 +10,10 @@ from typing import Dict, List, Optional, Tuple, Union import arrow from pandas import DataFrame -from freqtrade.constants import Config, ListPairsWithTimeframes +from freqtrade.constants import Config, IntOrInf, ListPairsWithTimeframes from freqtrade.data.dataprovider import DataProvider -from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, RunMode, SignalDirection, - SignalTagType, SignalType, TradingMode) +from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, MarketDirection, RunMode, + SignalDirection, SignalTagType, SignalType, TradingMode) from freqtrade.exceptions import OperationalException, StrategyError from freqtrade.exchange import timeframe_to_minutes, timeframe_to_next_date, timeframe_to_seconds from freqtrade.misc import remove_entry_exit_signals @@ -54,6 +54,9 @@ class IStrategy(ABC, HyperStrategyMixin): # associated stoploss stoploss: float + # max open trades for the strategy + max_open_trades: IntOrInf + # trailing stoploss trailing_stop: bool = False trailing_stop_positive: Optional[float] = None @@ -119,6 +122,9 @@ class IStrategy(ABC, HyperStrategyMixin): # Definition of plot_config. See plotting documentation for more details. plot_config: Dict = {} + # A self set parameter that represents the market direction. filled from configuration + market_direction: MarketDirection = MarketDirection.NONE + def __init__(self, config: Config) -> None: self.config = config # Dict to determine if analysis is necessary @@ -595,7 +601,7 @@ class IStrategy(ABC, HyperStrategyMixin): return None def populate_any_indicators(self, pair: str, df: DataFrame, tf: str, - informative: DataFrame = None, + informative: Optional[DataFrame] = None, set_generalized_indicators: bool = False) -> DataFrame: """ DEPRECATED - USE FEATURE ENGINEERING FUNCTIONS INSTEAD @@ -611,8 +617,8 @@ class IStrategy(ABC, HyperStrategyMixin): """ return df - def feature_engineering_expand_all(self, dataframe: DataFrame, - period: int, **kwargs): + def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, + metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This function will automatically expand the defined features on the config defined @@ -631,13 +637,14 @@ class IStrategy(ABC, HyperStrategyMixin): https://www.freqtrade.io/en/latest/freqai-feature-engineering/#defining-the-features - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features :param period: period of the indicator - usage example: + :param metadata: metadata of current pair dataframe["%-ema-period"] = ta.EMA(dataframe, timeperiod=period) """ return dataframe - def feature_engineering_expand_basic(self, dataframe: DataFrame, **kwargs): + def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This function will automatically expand the defined features on the config defined @@ -659,13 +666,14 @@ class IStrategy(ABC, HyperStrategyMixin): https://www.freqtrade.io/en/latest/freqai-feature-engineering/#defining-the-features - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features + :param metadata: metadata of current pair dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-ema-200"] = ta.EMA(dataframe, timeperiod=200) """ return dataframe - def feature_engineering_standard(self, dataframe: DataFrame, **kwargs): + def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This optional function will be called once with the dataframe of the base timeframe. @@ -683,12 +691,13 @@ class IStrategy(ABC, HyperStrategyMixin): https://www.freqtrade.io/en/latest/freqai-feature-engineering - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features + :param metadata: metadata of current pair usage example: dataframe["%-day_of_week"] = (dataframe["date"].dt.dayofweek + 1) / 7 """ return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* Required function to set the targets for the model. @@ -698,7 +707,8 @@ class IStrategy(ABC, HyperStrategyMixin): https://www.freqtrade.io/en/latest/freqai-feature-engineering - :param df: strategy dataframe which will receive the targets + :param dataframe: strategy dataframe which will receive the targets + :param metadata: metadata of current pair usage example: dataframe["&-target"] = dataframe["close"].shift(-1) / dataframe["close"] """ return dataframe @@ -756,7 +766,8 @@ class IStrategy(ABC, HyperStrategyMixin): """ return self.__class__.__name__ - def lock_pair(self, pair: str, until: datetime, reason: str = None, side: str = '*') -> None: + def lock_pair(self, pair: str, until: datetime, + reason: Optional[str] = None, side: str = '*') -> None: """ Locks pair until a given timestamp happens. Locked pairs are not analyzed, and are prevented from opening new trades. @@ -788,7 +799,8 @@ class IStrategy(ABC, HyperStrategyMixin): """ PairLocks.unlock_reason(reason, datetime.now(timezone.utc)) - def is_pair_locked(self, pair: str, *, candle_date: datetime = None, side: str = '*') -> bool: + def is_pair_locked(self, pair: str, *, candle_date: Optional[datetime] = None, + side: str = '*') -> bool: """ Checks if a pair is currently locked The 2nd, optional parameter ensures that locks are applied until the new candle arrives, @@ -959,7 +971,7 @@ class IStrategy(ABC, HyperStrategyMixin): pair: str, timeframe: str, dataframe: DataFrame, - is_short: bool = None + is_short: Optional[bool] = None ) -> Tuple[bool, bool, Optional[str]]: """ Calculates current exit signal based based on the dataframe @@ -1058,7 +1070,7 @@ class IStrategy(ABC, HyperStrategyMixin): def should_exit(self, trade: Trade, rate: float, current_time: datetime, *, enter: bool, exit_: bool, - low: float = None, high: float = None, + low: Optional[float] = None, high: Optional[float] = None, force_stoploss: float = 0) -> List[ExitCheckTuple]: """ This function evaluates if one of the conditions required to trigger an exit order @@ -1074,10 +1086,10 @@ class IStrategy(ABC, HyperStrategyMixin): trade.adjust_min_max_rates(high or current_rate, low or current_rate) - stoplossflag = self.stop_loss_reached(current_rate=current_rate, trade=trade, - current_time=current_time, - current_profit=current_profit, - force_stoploss=force_stoploss, low=low, high=high) + stoplossflag = self.ft_stoploss_reached(current_rate=current_rate, trade=trade, + current_time=current_time, + current_profit=current_profit, + force_stoploss=force_stoploss, low=low, high=high) # Set current rate to high for backtesting exits current_rate = (low if trade.is_short else high) or rate @@ -1144,13 +1156,12 @@ class IStrategy(ABC, HyperStrategyMixin): return exits - def stop_loss_reached(self, current_rate: float, trade: Trade, - current_time: datetime, current_profit: float, - force_stoploss: float, low: float = None, - high: float = None) -> ExitCheckTuple: + def ft_stoploss_adjust(self, current_rate: float, trade: Trade, + current_time: datetime, current_profit: float, + force_stoploss: float, low: Optional[float] = None, + high: Optional[float] = None) -> None: """ - Based on current profit of the trade and configured (trailing) stoploss, - decides to exit or not + Adjust stop-loss dynamically if configured to do so. :param current_profit: current profit as ratio :param low: Low value of this candle, only set in backtesting :param high: High value of this candle, only set in backtesting @@ -1196,6 +1207,20 @@ class IStrategy(ABC, HyperStrategyMixin): trade.adjust_stop_loss(bound or current_rate, stop_loss_value) + def ft_stoploss_reached(self, current_rate: float, trade: Trade, + current_time: datetime, current_profit: float, + force_stoploss: float, low: Optional[float] = None, + high: Optional[float] = None) -> ExitCheckTuple: + """ + Based on current profit of the trade and configured (trailing) stoploss, + decides to exit or not + :param current_profit: current profit as ratio + :param low: Low value of this candle, only set in backtesting + :param high: High value of this candle, only set in backtesting + """ + self.ft_stoploss_adjust(current_rate, trade, current_time, current_profit, + force_stoploss, low, high) + sl_higher_long = (trade.stop_loss >= (low or current_rate) and not trade.is_short) sl_lower_short = (trade.stop_loss <= (high or current_rate) and trade.is_short) liq_higher_long = (trade.liquidation_price diff --git a/freqtrade/strategy/strategy_helper.py b/freqtrade/strategy/strategy_helper.py index aa753a829..27ebe7e69 100644 --- a/freqtrade/strategy/strategy_helper.py +++ b/freqtrade/strategy/strategy_helper.py @@ -86,37 +86,41 @@ def merge_informative_pair(dataframe: pd.DataFrame, informative: pd.DataFrame, def stoploss_from_open( open_relative_stop: float, current_profit: float, - is_short: bool = False + is_short: bool = False, + leverage: float = 1.0 ) -> float: """ - - Given the current profit, and a desired stop loss value relative to the open price, + Given the current profit, and a desired stop loss value relative to the trade entry price, return a stop loss value that is relative to the current price, and which can be returned from `custom_stoploss`. The requested stop can be positive for a stop above the open price, or negative for a stop below the open price. The return value is always >= 0. + `open_relative_stop` will be considered as adjusted for leverage if leverage is provided.. Returns 0 if the resulting stop price would be above/below (longs/shorts) the current price - :param open_relative_stop: Desired stop loss percentage relative to open price + :param open_relative_stop: Desired stop loss percentage, relative to the open price, + adjusted for leverage :param current_profit: The current profit percentage :param is_short: When true, perform the calculation for short instead of long + :param leverage: Leverage to use for the calculation :return: Stop loss value relative to current price """ # formula is undefined for current_profit -1 (longs) or 1 (shorts), return maximum value - if (current_profit == -1 and not is_short) or (is_short and current_profit == 1): + _current_profit = current_profit / leverage + if (_current_profit == -1 and not is_short) or (is_short and _current_profit == 1): return 1 if is_short is True: - stoploss = -1 + ((1 - open_relative_stop) / (1 - current_profit)) + stoploss = -1 + ((1 - open_relative_stop / leverage) / (1 - _current_profit)) else: - stoploss = 1 - ((1 + open_relative_stop) / (1 + current_profit)) + stoploss = 1 - ((1 + open_relative_stop / leverage) / (1 + _current_profit)) # negative stoploss values indicate the requested stop price is higher/lower # (long/short) than the current price - return max(stoploss, 0.0) + return max(stoploss * leverage, 0.0) def stoploss_from_absolute(stop_rate: float, current_rate: float, is_short: bool = False) -> float: diff --git a/freqtrade/strategy/strategyupdater.py b/freqtrade/strategy/strategyupdater.py new file mode 100644 index 000000000..2669dcc4a --- /dev/null +++ b/freqtrade/strategy/strategyupdater.py @@ -0,0 +1,255 @@ +import shutil +from pathlib import Path + +import ast_comments + +from freqtrade.constants import Config + + +class StrategyUpdater: + name_mapping = { + 'ticker_interval': 'timeframe', + 'buy': 'enter_long', + 'sell': 'exit_long', + 'buy_tag': 'enter_tag', + 'sell_reason': 'exit_reason', + + 'sell_signal': 'exit_signal', + 'custom_sell': 'custom_exit', + 'force_sell': 'force_exit', + 'emergency_sell': 'emergency_exit', + + # Strategy/config settings: + 'use_sell_signal': 'use_exit_signal', + 'sell_profit_only': 'exit_profit_only', + 'sell_profit_offset': 'exit_profit_offset', + 'ignore_roi_if_buy_signal': 'ignore_roi_if_entry_signal', + 'forcebuy_enable': 'force_entry_enable', + } + + function_mapping = { + 'populate_buy_trend': 'populate_entry_trend', + 'populate_sell_trend': 'populate_exit_trend', + 'custom_sell': 'custom_exit', + 'check_buy_timeout': 'check_entry_timeout', + 'check_sell_timeout': 'check_exit_timeout', + # '': '', + } + # order_time_in_force, order_types, unfilledtimeout + otif_ot_unfilledtimeout = { + 'buy': 'entry', + 'sell': 'exit', + } + + # create a dictionary that maps the old column names to the new ones + rename_dict = {'buy': 'enter_long', 'sell': 'exit_long', 'buy_tag': 'enter_tag'} + + def start(self, config: Config, strategy_obj: dict) -> None: + """ + Run strategy updater + It updates a strategy to v3 with the help of the ast-module + :return: None + """ + + source_file = strategy_obj['location'] + strategies_backup_folder = Path.joinpath(config['user_data_dir'], "strategies_orig_updater") + target_file = Path.joinpath(strategies_backup_folder, strategy_obj['location_rel']) + + # read the file + with Path(source_file).open('r') as f: + old_code = f.read() + if not strategies_backup_folder.is_dir(): + Path(strategies_backup_folder).mkdir(parents=True, exist_ok=True) + + # backup original + # => currently no date after the filename, + # could get overridden pretty fast if this is fired twice! + # The folder is always the same and the file name too (currently). + shutil.copy(source_file, target_file) + + # update the code + new_code = self.update_code(old_code) + # write the modified code to the destination folder + with Path(source_file).open('w') as f: + f.write(new_code) + + # define the function to update the code + def update_code(self, code): + # parse the code into an AST + tree = ast_comments.parse(code) + + # use the AST to update the code + updated_code = self.modify_ast(tree) + + # return the modified code without executing it + return updated_code + + # function that uses the ast module to update the code + def modify_ast(self, tree): # noqa + # use the visitor to update the names and functions in the AST + NameUpdater().visit(tree) + + # first fix the comments, so it understands "\n" properly inside multi line comments. + ast_comments.fix_missing_locations(tree) + ast_comments.increment_lineno(tree, n=1) + + # generate the new code from the updated AST + # without indent {} parameters would just be written straight one after the other. + + # ast_comments would be amazing since this is the only solution that carries over comments, + # but it does currently not have an unparse function, hopefully in the future ... ! + # return ast_comments.unparse(tree) + + return ast_comments.unparse(tree) + + +# Here we go through each respective node, slice, elt, key ... to replace outdated entries. +class NameUpdater(ast_comments.NodeTransformer): + def generic_visit(self, node): + + # space is not yet transferred from buy/sell to entry/exit and thereby has to be skipped. + if isinstance(node, ast_comments.keyword): + if node.arg == "space": + return node + + # from here on this is the original function. + for field, old_value in ast_comments.iter_fields(node): + if isinstance(old_value, list): + new_values = [] + for value in old_value: + if isinstance(value, ast_comments.AST): + value = self.visit(value) + if value is None: + continue + elif not isinstance(value, ast_comments.AST): + new_values.extend(value) + continue + new_values.append(value) + old_value[:] = new_values + elif isinstance(old_value, ast_comments.AST): + new_node = self.visit(old_value) + if new_node is None: + delattr(node, field) + else: + setattr(node, field, new_node) + return node + + def visit_Expr(self, node): + if hasattr(node.value, "left") and hasattr(node.value.left, "id"): + node.value.left.id = self.check_dict(StrategyUpdater.name_mapping, node.value.left.id) + self.visit(node.value) + return node + + # Renames an element if contained inside a dictionary. + @staticmethod + def check_dict(current_dict: dict, element: str): + if element in current_dict: + element = current_dict[element] + return element + + def visit_arguments(self, node): + if isinstance(node.args, list): + for arg in node.args: + arg.arg = self.check_dict(StrategyUpdater.name_mapping, arg.arg) + return node + + def visit_Name(self, node): + # if the name is in the mapping, update it + node.id = self.check_dict(StrategyUpdater.name_mapping, node.id) + return node + + def visit_Import(self, node): + # do not update the names in import statements + return node + + def visit_ImportFrom(self, node): + # if hasattr(node, "module"): + # if node.module == "freqtrade.strategy.hyper": + # node.module = "freqtrade.strategy" + return node + + def visit_If(self, node: ast_comments.If): + for child in ast_comments.iter_child_nodes(node): + self.visit(child) + return node + + def visit_FunctionDef(self, node): + node.name = self.check_dict(StrategyUpdater.function_mapping, node.name) + self.generic_visit(node) + return node + + def visit_Attribute(self, node): + if ( + isinstance(node.value, ast_comments.Name) + and node.value.id == 'trade' + and node.attr == 'nr_of_successful_buys' + ): + node.attr = 'nr_of_successful_entries' + return node + + def visit_ClassDef(self, node): + # check if the class is derived from IStrategy + if any(isinstance(base, ast_comments.Name) and + base.id == 'IStrategy' for base in node.bases): + # check if the INTERFACE_VERSION variable exists + has_interface_version = any( + isinstance(child, ast_comments.Assign) and + isinstance(child.targets[0], ast_comments.Name) and + child.targets[0].id == 'INTERFACE_VERSION' + for child in node.body + ) + + # if the INTERFACE_VERSION variable does not exist, add it as the first child + if not has_interface_version: + node.body.insert(0, ast_comments.parse('INTERFACE_VERSION = 3').body[0]) + # otherwise, update its value to 3 + else: + for child in node.body: + if ( + isinstance(child, ast_comments.Assign) + and isinstance(child.targets[0], ast_comments.Name) + and child.targets[0].id == 'INTERFACE_VERSION' + ): + child.value = ast_comments.parse('3').body[0].value + self.generic_visit(node) + return node + + def visit_Subscript(self, node): + if isinstance(node.slice, ast_comments.Constant): + if node.slice.value in StrategyUpdater.rename_dict: + # Replace the slice attributes with the values from rename_dict + node.slice.value = StrategyUpdater.rename_dict[node.slice.value] + if hasattr(node.slice, "elts"): + self.visit_elts(node.slice.elts) + if hasattr(node.slice, "value"): + if hasattr(node.slice.value, "elts"): + self.visit_elts(node.slice.value.elts) + return node + + # elts can have elts (technically recursively) + def visit_elts(self, elts): + if isinstance(elts, list): + for elt in elts: + self.visit_elt(elt) + else: + self.visit_elt(elts) + return elts + + # sub function again needed since the structure itself is highly flexible ... + def visit_elt(self, elt): + if isinstance(elt, ast_comments.Constant) and elt.value in StrategyUpdater.rename_dict: + elt.value = StrategyUpdater.rename_dict[elt.value] + if hasattr(elt, "elts"): + self.visit_elts(elt.elts) + if hasattr(elt, "args"): + if isinstance(elt.args, ast_comments.arguments): + self.visit_elts(elt.args) + else: + for arg in elt.args: + self.visit_elts(arg) + return elt + + def visit_Constant(self, node): + node.value = self.check_dict(StrategyUpdater.otif_ot_unfilledtimeout, node.value) + node.value = self.check_dict(StrategyUpdater.name_mapping, node.value) + return node diff --git a/freqtrade/templates/FreqaiExampleHybridStrategy.py b/freqtrade/templates/FreqaiExampleHybridStrategy.py index c5dbe8dbd..0e7113f8c 100644 --- a/freqtrade/templates/FreqaiExampleHybridStrategy.py +++ b/freqtrade/templates/FreqaiExampleHybridStrategy.py @@ -1,12 +1,13 @@ import logging +from typing import Dict -import numpy as np -import pandas as pd +import numpy as np # noqa +import pandas as pd # noqa import talib.abstract as ta from pandas import DataFrame from technical import qtpylib -from freqtrade.strategy import IntParameter, IStrategy, merge_informative_pair +from freqtrade.strategy import IntParameter, IStrategy, merge_informative_pair # noqa logger = logging.getLogger(__name__) @@ -26,7 +27,7 @@ class FreqaiExampleHybridStrategy(IStrategy): "freqai": { "enabled": true, - "purge_old_models": true, + "purge_old_models": 2, "train_period_days": 15, "identifier": "uniqe-id", "feature_parameters": { @@ -95,7 +96,8 @@ class FreqaiExampleHybridStrategy(IStrategy): short_rsi = IntParameter(low=51, high=100, default=70, space='sell', optimize=True, load=True) exit_short_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True) - def feature_engineering_expand_all(self, dataframe, period, **kwargs): + def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, + metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This function will automatically expand the defined features on the config defined @@ -114,8 +116,9 @@ class FreqaiExampleHybridStrategy(IStrategy): https://www.freqtrade.io/en/latest/freqai-feature-engineering/#defining-the-features - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features :param period: period of the indicator - usage example: + :param metadata: metadata of current pair dataframe["%-ema-period"] = ta.EMA(dataframe, timeperiod=period) """ @@ -148,7 +151,7 @@ class FreqaiExampleHybridStrategy(IStrategy): return dataframe - def feature_engineering_expand_basic(self, dataframe, **kwargs): + def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This function will automatically expand the defined features on the config defined @@ -170,7 +173,8 @@ class FreqaiExampleHybridStrategy(IStrategy): https://www.freqtrade.io/en/latest/freqai-feature-engineering/#defining-the-features - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features + :param metadata: metadata of current pair dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-ema-200"] = ta.EMA(dataframe, timeperiod=200) """ @@ -179,7 +183,7 @@ class FreqaiExampleHybridStrategy(IStrategy): dataframe["%-raw_price"] = dataframe["close"] return dataframe - def feature_engineering_standard(self, dataframe, **kwargs): + def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This optional function will be called once with the dataframe of the base timeframe. @@ -197,14 +201,15 @@ class FreqaiExampleHybridStrategy(IStrategy): https://www.freqtrade.io/en/latest/freqai-feature-engineering - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features + :param metadata: metadata of current pair usage example: dataframe["%-day_of_week"] = (dataframe["date"].dt.dayofweek + 1) / 7 """ dataframe["%-day_of_week"] = dataframe["date"].dt.dayofweek dataframe["%-hour_of_day"] = dataframe["date"].dt.hour return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* Required function to set the targets for the model. @@ -214,16 +219,16 @@ class FreqaiExampleHybridStrategy(IStrategy): https://www.freqtrade.io/en/latest/freqai-feature-engineering - :param df: strategy dataframe which will receive the targets + :param dataframe: strategy dataframe which will receive the targets + :param metadata: metadata of current pair usage example: dataframe["&-target"] = dataframe["close"].shift(-1) / dataframe["close"] """ dataframe['&s-up_or_down'] = np.where(dataframe["close"].shift(-50) > - dataframe["close"], 'up', 'down') + dataframe["close"], 'up', 'down') return dataframe - # flake8: noqa: C901 - def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # noqa: C901 # User creates their own custom strat here. Present example is a supertrend # based strategy. diff --git a/freqtrade/templates/FreqaiExampleStrategy.py b/freqtrade/templates/FreqaiExampleStrategy.py index 8e34d733e..0093c7f7a 100644 --- a/freqtrade/templates/FreqaiExampleStrategy.py +++ b/freqtrade/templates/FreqaiExampleStrategy.py @@ -1,5 +1,6 @@ import logging from functools import reduce +from typing import Dict import talib.abstract as ta from pandas import DataFrame @@ -46,7 +47,8 @@ class FreqaiExampleStrategy(IStrategy): std_dev_multiplier_sell = CategoricalParameter( [0.75, 1, 1.25, 1.5, 1.75], space="sell", default=1.25, optimize=True) - def feature_engineering_expand_all(self, dataframe, period, **kwargs): + def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, + metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This function will automatically expand the defined features on the config defined @@ -58,6 +60,10 @@ class FreqaiExampleStrategy(IStrategy): All features must be prepended with `%` to be recognized by FreqAI internals. + Access metadata such as the current pair/timeframe with: + + `metadata["pair"]` `metadata["tf"]` + More details on how these config defined parameters accelerate feature engineering in the documentation at: @@ -65,8 +71,9 @@ class FreqaiExampleStrategy(IStrategy): https://www.freqtrade.io/en/latest/freqai-feature-engineering/#defining-the-features - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features :param period: period of the indicator - usage example: + :param metadata: metadata of current pair dataframe["%-ema-period"] = ta.EMA(dataframe, timeperiod=period) """ @@ -99,7 +106,7 @@ class FreqaiExampleStrategy(IStrategy): return dataframe - def feature_engineering_expand_basic(self, dataframe, **kwargs): + def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This function will automatically expand the defined features on the config defined @@ -114,6 +121,10 @@ class FreqaiExampleStrategy(IStrategy): All features must be prepended with `%` to be recognized by FreqAI internals. + Access metadata such as the current pair/timeframe with: + + `metadata["pair"]` `metadata["tf"]` + More details on how these config defined parameters accelerate feature engineering in the documentation at: @@ -121,7 +132,8 @@ class FreqaiExampleStrategy(IStrategy): https://www.freqtrade.io/en/latest/freqai-feature-engineering/#defining-the-features - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features + :param metadata: metadata of current pair dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-ema-200"] = ta.EMA(dataframe, timeperiod=200) """ @@ -130,7 +142,7 @@ class FreqaiExampleStrategy(IStrategy): dataframe["%-raw_price"] = dataframe["close"] return dataframe - def feature_engineering_standard(self, dataframe, **kwargs): + def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* This optional function will be called once with the dataframe of the base timeframe. @@ -144,28 +156,38 @@ class FreqaiExampleStrategy(IStrategy): All features must be prepended with `%` to be recognized by FreqAI internals. + Access metadata such as the current pair with: + + `metadata["pair"]` + More details about feature engineering available: https://www.freqtrade.io/en/latest/freqai-feature-engineering - :param df: strategy dataframe which will receive the features + :param dataframe: strategy dataframe which will receive the features + :param metadata: metadata of current pair usage example: dataframe["%-day_of_week"] = (dataframe["date"].dt.dayofweek + 1) / 7 """ dataframe["%-day_of_week"] = dataframe["date"].dt.dayofweek dataframe["%-hour_of_day"] = dataframe["date"].dt.hour return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs): """ *Only functional with FreqAI enabled strategies* Required function to set the targets for the model. All targets must be prepended with `&` to be recognized by the FreqAI internals. + Access metadata such as the current pair with: + + `metadata["pair"]` + More details about feature engineering available: https://www.freqtrade.io/en/latest/freqai-feature-engineering - :param df: strategy dataframe which will receive the targets + :param dataframe: strategy dataframe which will receive the targets + :param metadata: metadata of current pair usage example: dataframe["&-target"] = dataframe["close"].shift(-1) / dataframe["close"] """ dataframe["&-s_close"] = ( diff --git a/freqtrade/templates/base_config.json.j2 b/freqtrade/templates/base_config.json.j2 index 299734a50..1a4552c11 100644 --- a/freqtrade/templates/base_config.json.j2 +++ b/freqtrade/templates/base_config.json.j2 @@ -41,20 +41,6 @@ "pairlists": [ {{ '{"method": "StaticPairList"}' if exchange_name == 'bittrex' else volume_pairlist }} ], - "edge": { - "enabled": false, - "process_throttle_secs": 3600, - "calculate_since_number_of_days": 7, - "allowed_risk": 0.01, - "stoploss_range_min": -0.01, - "stoploss_range_max": -0.1, - "stoploss_range_step": -0.01, - "minimum_winrate": 0.60, - "minimum_expectancy": 0.20, - "min_trade_number": 10, - "max_trade_duration_minute": 1440, - "remove_pumps": false - }, "telegram": { "enabled": {{ telegram | lower }}, "token": "{{ telegram_token }}", diff --git a/freqtrade/templates/strategy_analysis_example.ipynb b/freqtrade/templates/strategy_analysis_example.ipynb index dfbcedb72..2bfa4155d 100644 --- a/freqtrade/templates/strategy_analysis_example.ipynb +++ b/freqtrade/templates/strategy_analysis_example.ipynb @@ -118,6 +118,7 @@ "from freqtrade.data.dataprovider import DataProvider\n", "strategy = StrategyResolver.load_strategy(config)\n", "strategy.dp = DataProvider(config, None, None)\n", + "strategy.ft_bot_start()\n", "\n", "# Generate buy/sell signals using strategy\n", "df = strategy.analyze_ticker(candles, {'pair': pair})\n", diff --git a/freqtrade/util/__init__.py b/freqtrade/util/__init__.py index 7980b7ca2..3c3c034c1 100644 --- a/freqtrade/util/__init__.py +++ b/freqtrade/util/__init__.py @@ -1,3 +1,2 @@ -# flake8: noqa: F401 -from freqtrade.util.ft_precise import FtPrecise -from freqtrade.util.periodic_cache import PeriodicCache +from freqtrade.util.ft_precise import FtPrecise # noqa: F401 +from freqtrade.util.periodic_cache import PeriodicCache # noqa: F401 diff --git a/freqtrade/util/binance_mig.py b/freqtrade/util/binance_mig.py index 708bb1db7..37a2d2ef1 100644 --- a/freqtrade/util/binance_mig.py +++ b/freqtrade/util/binance_mig.py @@ -1,6 +1,7 @@ import logging from packaging import version +from sqlalchemy import select from freqtrade.constants import Config from freqtrade.enums.tradingmode import TradingMode @@ -44,7 +45,7 @@ def _migrate_binance_futures_db(config: Config): # Should symbol be migrated too? # order.symbol = new_pair Trade.commit() - pls = PairLock.query.filter(PairLock.pair.notlike('%:%')) + pls = PairLock.session.scalars(select(PairLock).filter(PairLock.pair.notlike('%:%'))).all() for pl in pls: pl.pair = f"{pl.pair}:{config['stake_currency']}" # print(pls) diff --git a/freqtrade/vendor/qtpylib/indicators.py b/freqtrade/vendor/qtpylib/indicators.py index 4f14ae13c..63797d462 100644 --- a/freqtrade/vendor/qtpylib/indicators.py +++ b/freqtrade/vendor/qtpylib/indicators.py @@ -1,6 +1,3 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# # QTPyLib: Quantitative Trading Python Library # https://github.com/ranaroussi/qtpylib # @@ -19,7 +16,6 @@ # limitations under the License. # -import sys import warnings from datetime import datetime, timedelta @@ -28,11 +24,6 @@ import pandas as pd from pandas.core.base import PandasObject -# ============================================= -# check min, python version -if sys.version_info < (3, 4): - raise SystemError("QTPyLib requires Python version >= 3.4") - # ============================================= warnings.simplefilter(action="ignore", category=RuntimeWarning) diff --git a/freqtrade/wallets.py b/freqtrade/wallets.py index 97db3fba5..8dcc92af4 100644 --- a/freqtrade/wallets.py +++ b/freqtrade/wallets.py @@ -297,16 +297,16 @@ class Wallets: logger.debug(f"Stake amount is {stake_amount}, ignoring possible trade for {pair}.") return 0 - max_stake_amount = min(max_stake_amount, self.get_available_stake_amount()) + max_allowed_stake = min(max_stake_amount, self.get_available_stake_amount()) if trade_amount: # if in a trade, then the resulting trade size cannot go beyond the max stake # Otherwise we could no longer exit. - max_stake_amount = min(max_stake_amount, max_stake_amount - trade_amount) + max_allowed_stake = min(max_allowed_stake, max_stake_amount - trade_amount) - if min_stake_amount is not None and min_stake_amount > max_stake_amount: + if min_stake_amount is not None and min_stake_amount > max_allowed_stake: if self._log: logger.warning("Minimum stake amount > available balance. " - f"{min_stake_amount} > {max_stake_amount}") + f"{min_stake_amount} > {max_allowed_stake}") return 0 if min_stake_amount is not None and stake_amount < min_stake_amount: if self._log: @@ -325,11 +325,11 @@ class Wallets: return 0 stake_amount = min_stake_amount - if stake_amount > max_stake_amount: + if stake_amount > max_allowed_stake: if self._log: logger.info( f"Stake amount for pair {pair} is too big " - f"({stake_amount} > {max_stake_amount}), adjusting to {max_stake_amount}." + f"({stake_amount} > {max_allowed_stake}), adjusting to {max_allowed_stake}." ) - stake_amount = max_stake_amount + stake_amount = max_allowed_stake return stake_amount diff --git a/freqtrade/worker.py b/freqtrade/worker.py old mode 100755 new mode 100644 index 27f067b07..fb89e7a2d --- a/freqtrade/worker.py +++ b/freqtrade/worker.py @@ -12,7 +12,7 @@ import sdnotify from freqtrade import __version__ from freqtrade.configuration import Configuration from freqtrade.constants import PROCESS_THROTTLE_SECS, RETRY_TIMEOUT, Config -from freqtrade.enums import State +from freqtrade.enums import RPCMessageType, State from freqtrade.exceptions import OperationalException, TemporaryError from freqtrade.exchange import timeframe_to_next_date from freqtrade.freqtradebot import FreqtradeBot @@ -26,7 +26,7 @@ class Worker: Freqtradebot worker class """ - def __init__(self, args: Dict[str, Any], config: Config = None) -> None: + def __init__(self, args: Dict[str, Any], config: Optional[Config] = None) -> None: """ Init all variables and objects the bot needs to work """ @@ -185,7 +185,10 @@ class Worker: tb = traceback.format_exc() hint = 'Issue `/start` if you think it is safe to restart.' - self.freqtrade.notify_status(f'OperationalException:\n```\n{tb}```{hint}') + self.freqtrade.notify_status( + f'*OperationalException:*\n```\n{tb}```\n {hint}', + msg_type=RPCMessageType.EXCEPTION + ) logger.exception('OperationalException. Stopping trader ...') self.freqtrade.state = State.STOPPED diff --git a/pyproject.toml b/pyproject.toml index 2de2c957b..baf707c68 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,7 @@ +[build-system] +requires = ["setuptools >= 46.4.0", "wheel"] +build-backend = "setuptools.build_meta" + [tool.black] line-length = 100 exclude = ''' @@ -31,19 +35,22 @@ asyncio_mode = "auto" [tool.mypy] ignore_missing_imports = true namespace_packages = false -implicit_optional = true warn_unused_ignores = true exclude = [ '^build_helpers\.py$' ] +plugins = [ + "sqlalchemy.ext.mypy.plugin" +] [[tool.mypy.overrides]] module = "tests.*" ignore_errors = true -[build-system] -requires = ["setuptools >= 46.4.0", "wheel"] -build-backend = "setuptools.build_meta" +[[tool.mypy.overrides]] +# Telegram does not use implicit_optional = false in the current version. +module = "telegram.*" +implicit_optional = true [tool.pyright] include = ["freqtrade"] @@ -53,5 +60,27 @@ exclude = [ ] ignore = ["freqtrade/vendor/**"] -# Align pyright to mypy config -strictParameterNoneValue = false + +[tool.ruff] +line-length = 100 +extend-exclude = [".env"] +target-version = "py38" +extend-select = [ + "C90", # mccabe + # "N", # pep8-naming + "UP", # pyupgrade + "TID", # flake8-tidy-imports + # "EXE", # flake8-executable + "YTT", # flake8-2020 + # "S", # flake8-bandit + # "DTZ", # flake8-datetimez + # "RSE", # flake8-raise + # "TCH", # flake8-type-checking + "PTH", # flake8-use-pathlib +] + +[tool.ruff.mccabe] +max-complexity = 12 + +[tool.ruff.per-file-ignores] +"tests/*" = ["S"] diff --git a/requirements-dev.txt b/requirements-dev.txt index e5e63cb11..8312e2820 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,27 +7,26 @@ -r docs/requirements-docs.txt coveralls==3.3.1 -flake8==6.0.0 -flake8-tidy-imports==4.8.0 -mypy==0.991 -pre-commit==2.21.0 -pytest==7.2.1 -pytest-asyncio==0.20.3 +ruff==0.0.257 +mypy==1.1.1 +pre-commit==3.2.0 +pytest==7.2.2 +pytest-asyncio==0.21.0 pytest-cov==4.0.0 pytest-mock==3.10.0 pytest-random-order==1.1.0 -isort==5.11.4 +isort==5.12.0 # For datetime mocking time-machine==2.9.0 # fastapi testing httpx==0.23.3 # Convert jupyter notebooks to markdown documents -nbconvert==7.2.7 +nbconvert==7.2.10 # mypy types -types-cachetools==5.2.1 +types-cachetools==5.3.0.4 types-filelock==3.2.7 -types-requests==2.28.11.7 -types-tabulate==0.9.0.0 -types-python-dateutil==2.8.19.5 +types-requests==2.28.11.15 +types-tabulate==0.9.0.1 +types-python-dateutil==2.8.19.10 diff --git a/requirements-freqai-rl.txt b/requirements-freqai-rl.txt index db8d8d169..4de7d8fab 100644 --- a/requirements-freqai-rl.txt +++ b/requirements-freqai-rl.txt @@ -2,8 +2,9 @@ -r requirements-freqai.txt # Required for freqai-rl -torch==1.13.1 -stable-baselines3==1.6.2 -sb3-contrib==1.6.2 +torch==1.13.1; python_version < '3.11' +stable-baselines3==1.7.0; python_version < '3.11' +sb3-contrib==1.7.0; python_version < '3.11' # Gym is forced to this version by stable-baselines3. -gym==0.21 +setuptools==65.5.1 # Should be removed when gym is fixed. +gym==0.21; python_version < '3.11' diff --git a/requirements-freqai.txt b/requirements-freqai.txt index 575e4f1e1..bc0be85e5 100644 --- a/requirements-freqai.txt +++ b/requirements-freqai.txt @@ -5,7 +5,7 @@ # Required for freqai scikit-learn==1.1.3 joblib==1.2.0 -catboost==1.1.1; platform_machine != 'aarch64' -lightgbm==3.3.4 -xgboost==1.7.3 -tensorboard==2.11.2 +catboost==1.1.1; platform_machine != 'aarch64' and python_version < '3.11' +lightgbm==3.3.5 +xgboost==1.7.4 +tensorboard==2.12.0 diff --git a/requirements-hyperopt.txt b/requirements-hyperopt.txt index 171ede929..4d86da2b6 100644 --- a/requirements-hyperopt.txt +++ b/requirements-hyperopt.txt @@ -2,8 +2,8 @@ -r requirements.txt # Required for hyperopt -scipy==1.10.0 +scipy==1.10.1 scikit-learn==1.1.3 scikit-optimize==0.9.0 -filelock==3.9.0 +filelock==3.10.0 progressbar2==4.2.0 diff --git a/requirements-plot.txt b/requirements-plot.txt index 75e3234a1..ad7bade95 100644 --- a/requirements-plot.txt +++ b/requirements-plot.txt @@ -1,4 +1,4 @@ # Include all requirements to run the bot. -r requirements.txt -plotly==5.11.0 +plotly==5.13.1 diff --git a/requirements.txt b/requirements.txt index 9678e5ec6..ad34883ae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,53 +1,51 @@ -numpy==1.24.1 -pandas==1.5.2 +numpy==1.24.2 +pandas==1.5.3 pandas-ta==0.3.14b -ccxt==2.6.26 -# Pin cryptography for now due to rust build errors with piwheels -cryptography==38.0.1; platform_machine == 'armv7l' -cryptography==39.0.0; platform_machine != 'armv7l' -aiohttp==3.8.3 -SQLAlchemy==1.4.46 +ccxt==3.0.23 +cryptography==39.0.2 +aiohttp==3.8.4 +SQLAlchemy==2.0.7 python-telegram-bot==13.15 arrow==1.2.3 cachetools==4.2.2 requests==2.28.2 -urllib3==1.26.14 +urllib3==1.26.15 jsonschema==4.17.3 TA-Lib==0.4.25 -technical==1.3.0 +technical==1.4.0 tabulate==0.9.0 pycoingecko==3.1.0 jinja2==3.1.2 tables==3.8.0 blosc==1.11.1 joblib==1.2.0 -pyarrow==10.0.1; platform_machine != 'armv7l' +pyarrow==11.0.0; platform_machine != 'armv7l' # find first, C search in arrays py_find_1st==1.1.5 # Load ticker files 30% faster -python-rapidjson==1.9 +python-rapidjson==1.10 # Properly format api responses -orjson==3.8.5 +orjson==3.8.7 # Notify systemd sdnotify==0.3.2 # API Server -fastapi==0.89.1 -pydantic==1.10.4 -uvicorn==0.20.0 +fastapi==0.95.0 +pydantic==1.10.6 +uvicorn==0.21.1 pyjwt==2.6.0 -aiofiles==22.1.0 +aiofiles==23.1.0 psutil==5.9.4 # Support for colorized terminal output colorama==0.4.6 # Building config files interactively questionary==1.10.0 -prompt-toolkit==3.0.36 +prompt-toolkit==3.0.38 # Extensions to datetime library python-dateutil==2.8.2 @@ -57,3 +55,5 @@ schedule==1.1.0 #WS Messages websockets==10.4 janus==1.0.0 + +ast-comments==1.0.1 diff --git a/scripts/rest_client.py b/scripts/rest_client.py index ac6d97133..196542780 100755 --- a/scripts/rest_client.py +++ b/scripts/rest_client.py @@ -14,6 +14,7 @@ import logging import re import sys from pathlib import Path +from typing import Optional from urllib.parse import urlencode, urlparse, urlunparse import rapidjson @@ -36,7 +37,7 @@ class FtRestClient(): self._session = requests.Session() self._session.auth = (username, password) - def _call(self, method, apipath, params: dict = None, data=None, files=None): + def _call(self, method, apipath, params: Optional[dict] = None, data=None, files=None): if str(method).upper() not in ('GET', 'POST', 'PUT', 'DELETE'): raise ValueError(f'invalid method <{method}>') @@ -60,13 +61,13 @@ class FtRestClient(): except ConnectionError: logger.warning("Connection error") - def _get(self, apipath, params: dict = None): + def _get(self, apipath, params: Optional[dict] = None): return self._call("GET", apipath, params=params) - def _delete(self, apipath, params: dict = None): + def _delete(self, apipath, params: Optional[dict] = None): return self._call("DELETE", apipath, params=params) - def _post(self, apipath, params: dict = None, data: dict = None): + def _post(self, apipath, params: Optional[dict] = None, data: Optional[dict] = None): return self._call("POST", apipath, params=params, data=data) def start(self): @@ -176,8 +177,7 @@ class FtRestClient(): return self._get("version") def show_config(self): - """ - Returns part of the configuration, relevant for trading operations. + """ Returns part of the configuration, relevant for trading operations. :return: json object containing the version """ return self._get("show_config") @@ -231,6 +231,14 @@ class FtRestClient(): """ return self._delete(f"trades/{trade_id}") + def cancel_open_order(self, trade_id): + """Cancel open order for trade. + + :param trade_id: Cancels open orders for this trade. + :return: json object + """ + return self._delete(f"trades/{trade_id}/open-order") + def whitelist(self): """Show the current whitelist. @@ -332,11 +340,13 @@ class FtRestClient(): :param limit: Limit result to the last n candles. :return: json object """ - return self._get("pair_candles", params={ + params = { "pair": pair, "timeframe": timeframe, - "limit": limit, - }) + } + if limit: + params['limit'] = limit + return self._get("pair_candles", params=params) def pair_history(self, pair, timeframe, strategy, timerange=None): """Return historic, analyzed dataframe diff --git a/scripts/ws_client.py b/scripts/ws_client.py old mode 100644 new mode 100755 diff --git a/setup.cfg b/setup.cfg index 60ec8a75f..b54b62619 100644 --- a/setup.cfg +++ b/setup.cfg @@ -17,6 +17,7 @@ classifiers = Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 Operating System :: MacOS Operating System :: Unix Topic :: Office/Business :: Financial :: Investment diff --git a/setup.py b/setup.py index 30aacc3f2..edd7b243b 100644 --- a/setup.py +++ b/setup.py @@ -32,8 +32,6 @@ hdf5 = [ develop = [ 'coveralls', - 'flake8', - 'flake8-tidy-imports', 'mypy', 'pytest', 'pytest-asyncio', diff --git a/setup.sh b/setup.sh index 4cb504853..a9ff36536 100755 --- a/setup.sh +++ b/setup.sh @@ -49,48 +49,50 @@ function updateenv() { source .env/bin/activate SYS_ARCH=$(uname -m) echo "pip install in-progress. Please wait..." - ${PYTHON} -m pip install --upgrade pip - read -p "Do you want to install dependencies for dev [y/N]? " + # Setuptools 65.5.0 is the last version that can install gym==0.21.0 + ${PYTHON} -m pip install --upgrade pip wheel setuptools==65.5.1 + REQUIREMENTS_HYPEROPT="" + REQUIREMENTS_PLOT="" + REQUIREMENTS_FREQAI="" + REQUIREMENTS_FREQAI_RL="" + REQUIREMENTS=requirements.txt + + read -p "Do you want to install dependencies for development (Performs a full install with all dependencies) [y/N]? " dev=$REPLY if [[ $REPLY =~ ^[Yy]$ ]] then REQUIREMENTS=requirements-dev.txt else - REQUIREMENTS=requirements.txt - fi - REQUIREMENTS_HYPEROPT="" - REQUIREMENTS_PLOT="" - read -p "Do you want to install plotting dependencies (plotly) [y/N]? " - if [[ $REPLY =~ ^[Yy]$ ]] - then - REQUIREMENTS_PLOT="-r requirements-plot.txt" - fi - if [ "${SYS_ARCH}" == "armv7l" ] || [ "${SYS_ARCH}" == "armv6l" ]; then - echo "Detected Raspberry, installing cython, skipping hyperopt installation." - ${PYTHON} -m pip install --upgrade cython - else - # Is not Raspberry - read -p "Do you want to install hyperopt dependencies [y/N]? " + # requirements-dev.txt includes all the below requirements already, so further questions are pointless. + read -p "Do you want to install plotting dependencies (plotly) [y/N]? " if [[ $REPLY =~ ^[Yy]$ ]] then - REQUIREMENTS_HYPEROPT="-r requirements-hyperopt.txt" + REQUIREMENTS_PLOT="-r requirements-plot.txt" + fi + if [ "${SYS_ARCH}" == "armv7l" ] || [ "${SYS_ARCH}" == "armv6l" ]; then + echo "Detected Raspberry, installing cython, skipping hyperopt installation." + ${PYTHON} -m pip install --upgrade cython + else + # Is not Raspberry + read -p "Do you want to install hyperopt dependencies [y/N]? " + if [[ $REPLY =~ ^[Yy]$ ]] + then + REQUIREMENTS_HYPEROPT="-r requirements-hyperopt.txt" + fi fi - fi - REQUIREMENTS_FREQAI="" - REQUIREMENTS_FREQAI_RL="" - read -p "Do you want to install dependencies for freqai [y/N]? " - dev=$REPLY - if [[ $REPLY =~ ^[Yy]$ ]] - then - REQUIREMENTS_FREQAI="-r requirements-freqai.txt --use-pep517" - read -p "Do you also want dependencies for freqai-rl (~700mb additional space required) [y/N]? " - dev=$REPLY + read -p "Do you want to install dependencies for freqai [y/N]? " if [[ $REPLY =~ ^[Yy]$ ]] then - REQUIREMENTS_FREQAI="-r requirements-freqai-rl.txt" + REQUIREMENTS_FREQAI="-r requirements-freqai.txt --use-pep517" + read -p "Do you also want dependencies for freqai-rl (~700mb additional space required) [y/N]? " + if [[ $REPLY =~ ^[Yy]$ ]] + then + REQUIREMENTS_FREQAI="-r requirements-freqai-rl.txt" + fi fi fi + install_talib ${PYTHON} -m pip install --upgrade -r ${REQUIREMENTS} ${REQUIREMENTS_HYPEROPT} ${REQUIREMENTS_PLOT} ${REQUIREMENTS_FREQAI} ${REQUIREMENTS_FREQAI_RL} if [ $? -ne 0 ]; then @@ -168,21 +170,18 @@ function install_macos() { if [[ $version -ge 9 ]]; then #Checks if python version >= 3.9 install_mac_newer_python_dependencies fi - install_talib } # Install bot Debian_ubuntu function install_debian() { sudo apt-get update sudo apt-get install -y gcc build-essential autoconf libtool pkg-config make wget git curl $(echo lib${PYTHON}-dev ${PYTHON}-venv) - install_talib } # Install bot RedHat_CentOS function install_redhat() { sudo yum update sudo yum install -y gcc gcc-c++ make autoconf libtool pkg-config wget git $(echo ${PYTHON}-devel | sed 's/\.//g') - install_talib } # Upgrade the bot @@ -191,26 +190,37 @@ function update() { updateenv } +function check_git_changes() { + if [ -z "$(git status --porcelain)" ]; then + echo "No changes in git directory" + return 1 + else + echo "Changes in git directory" + return 0 + fi +} + # Reset Develop or Stable branch function reset() { echo_block "Resetting branch and virtual env" if [ "1" == $(git branch -vv |grep -cE "\* develop|\* stable") ] then + if check_git_changes; then + read -p "Keep your local changes? (Otherwise will remove all changes you made!) [Y/n]? " + if [[ $REPLY =~ ^[Nn]$ ]]; then - read -p "Reset git branch? (This will remove all changes you made!) [y/N]? " - if [[ $REPLY =~ ^[Yy]$ ]]; then + git fetch -a - git fetch -a - - if [ "1" == $(git branch -vv | grep -c "* develop") ] - then - echo "- Hard resetting of 'develop' branch." - git reset --hard origin/develop - elif [ "1" == $(git branch -vv | grep -c "* stable") ] - then - echo "- Hard resetting of 'stable' branch." - git reset --hard origin/stable + if [ "1" == $(git branch -vv | grep -c "* develop") ] + then + echo "- Hard resetting of 'develop' branch." + git reset --hard origin/develop + elif [ "1" == $(git branch -vv | grep -c "* stable") ] + then + echo "- Hard resetting of 'stable' branch." + git reset --hard origin/stable + fi fi fi else diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 6cddf8897..318590b32 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -14,7 +14,8 @@ from freqtrade.commands import (start_backtesting_show, start_convert_data, star start_hyperopt_show, start_install_ui, start_list_data, start_list_exchanges, start_list_markets, start_list_strategies, start_list_timeframes, start_new_strategy, start_show_trades, - start_test_pairlist, start_trading, start_webserver) + start_strategy_update, start_test_pairlist, start_trading, + start_webserver) from freqtrade.commands.db_commands import start_convert_db from freqtrade.commands.deploy_commands import (clean_ui_subdir, download_and_install_ui, get_ui_download_url, read_ui_version) @@ -24,7 +25,7 @@ from freqtrade.enums import RunMode from freqtrade.exceptions import OperationalException from freqtrade.persistence.models import init_db from freqtrade.persistence.pairlock_middleware import PairLocks -from tests.conftest import (CURRENT_TEST_STRATEGY, create_mock_trades, get_args, log_has, +from tests.conftest import (CURRENT_TEST_STRATEGY, EXMS, create_mock_trades, get_args, log_has, log_has_re, patch_exchange, patched_configuration_load_config_file) from tests.conftest_trades import MOCK_TRADE_COUNT @@ -454,7 +455,7 @@ def test_list_markets(mocker, markets_static, capsys): assert re.search(r"^BLK/BTC$", captured.out, re.MULTILINE) assert re.search(r"^LTC/USD$", captured.out, re.MULTILINE) - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(side_effect=ValueError)) + mocker.patch(f'{EXMS}.markets', PropertyMock(side_effect=ValueError)) # Test --one-column args = [ "list-markets", @@ -643,9 +644,7 @@ def test_download_data_keyboardInterrupt(mocker, markets): dl_mock = mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', MagicMock(side_effect=KeyboardInterrupt)) patch_exchange(mocker) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) args = [ "download-data", "--exchange", "binance", @@ -664,9 +663,7 @@ def test_download_data_timerange(mocker, markets): dl_mock = mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) patch_exchange(mocker) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) args = [ "download-data", "--exchange", "binance", @@ -715,9 +712,7 @@ def test_download_data_no_markets(mocker, caplog): dl_mock = mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) patch_exchange(mocker, id='binance') - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={})) args = [ "download-data", "--exchange", "binance", @@ -733,9 +728,7 @@ def test_download_data_no_exchange(mocker, caplog): mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) patch_exchange(mocker) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={})) args = [ "download-data", ] @@ -751,9 +744,7 @@ def test_download_data_no_pairs(mocker): mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) patch_exchange(mocker) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={})) args = [ "download-data", "--exchange", @@ -771,9 +762,7 @@ def test_download_data_all_pairs(mocker, markets): dl_mock = mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) patch_exchange(mocker) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) args = [ "download-data", "--exchange", @@ -810,9 +799,7 @@ def test_download_data_trades(mocker, caplog): convert_mock = mocker.patch('freqtrade.commands.data_commands.convert_trades_to_ohlcv', MagicMock(return_value=[])) patch_exchange(mocker) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={})) args = [ "download-data", "--exchange", "kraken", @@ -843,9 +830,7 @@ def test_download_data_trades(mocker, caplog): def test_download_data_data_invalid(mocker): patch_exchange(mocker, id="kraken") - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={})) args = [ "download-data", "--exchange", "kraken", @@ -862,9 +847,7 @@ def test_start_convert_trades(mocker, caplog): convert_mock = mocker.patch('freqtrade.commands.data_commands.convert_trades_to_ohlcv', MagicMock(return_value=[])) patch_exchange(mocker) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={})) args = [ "trades-to-ohlcv", "--exchange", "kraken", @@ -971,7 +954,7 @@ def test_start_list_freqAI_models(capsys): def test_start_test_pairlist(mocker, caplog, tickers, default_conf, capsys): patch_exchange(mocker, mock_markets=True) - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, exchange_has=MagicMock(return_value=True), get_tickers=tickers, ) @@ -1450,9 +1433,9 @@ def test_start_list_data(testdatadir, capsys): start_list_data(pargs) captured = capsys.readouterr() - assert "Found 5 pair / timeframe combinations." in captured.out + assert "Found 6 pair / timeframe combinations." in captured.out assert "\n| Pair | Timeframe | Type |\n" in captured.out - assert "\n| XRP/USDT:USDT | 1h | futures |\n" in captured.out + assert "\n| XRP/USDT:USDT | 5m, 1h | futures |\n" in captured.out assert "\n| XRP/USDT:USDT | 1h, 8h | mark |\n" in captured.out args = [ @@ -1564,3 +1547,37 @@ def test_start_convert_db(mocker, fee, tmpdir, caplog): start_convert_db(pargs) assert db_target_file.is_file() + + +def test_start_strategy_updater(mocker, tmpdir): + sc_mock = mocker.patch('freqtrade.commands.strategy_utils_commands.start_conversion') + teststrats = Path(__file__).parent.parent / 'strategy/strats' + args = [ + "strategy-updater", + "--userdir", + str(tmpdir), + "--strategy-path", + str(teststrats), + ] + pargs = get_args(args) + pargs['config'] = None + start_strategy_update(pargs) + # Number of strategies in the test directory + assert sc_mock.call_count == 11 + + sc_mock.reset_mock() + args = [ + "strategy-updater", + "--userdir", + str(tmpdir), + "--strategy-path", + str(teststrats), + "--strategy-list", + "StrategyTestV3", + "StrategyTestV2" + ] + pargs = get_args(args) + pargs['config'] = None + start_strategy_update(pargs) + # Number of strategies in the test directory + assert sc_mock.call_count == 2 diff --git a/tests/conftest.py b/tests/conftest.py index 1ff3da08c..0aa6e70a8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -40,6 +40,7 @@ np.seterr(all='raise') CURRENT_TEST_STRATEGY = 'StrategyTestV3' TRADE_SIDES = ('long', 'short') +EXMS = 'freqtrade.exchange.exchange.Exchange' def pytest_addoption(parser): @@ -145,22 +146,21 @@ def patch_exchange( mock_markets=True, mock_supported_modes=True ) -> None: - mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.Exchange.validate_config', MagicMock()) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock()) - mocker.patch('freqtrade.exchange.Exchange.id', PropertyMock(return_value=id)) - mocker.patch('freqtrade.exchange.Exchange.name', PropertyMock(return_value=id.title())) - mocker.patch('freqtrade.exchange.Exchange.precisionMode', PropertyMock(return_value=2)) + mocker.patch(f'{EXMS}._load_async_markets', return_value={}) + mocker.patch(f'{EXMS}.validate_config', MagicMock()) + mocker.patch(f'{EXMS}.validate_timeframes', MagicMock()) + mocker.patch(f'{EXMS}.id', PropertyMock(return_value=id)) + mocker.patch(f'{EXMS}.name', PropertyMock(return_value=id.title())) + mocker.patch(f'{EXMS}.precisionMode', PropertyMock(return_value=2)) if mock_markets: if isinstance(mock_markets, bool): mock_markets = get_markets() - mocker.patch('freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=mock_markets)) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=mock_markets)) if mock_supported_modes: mocker.patch( - f'freqtrade.exchange.{id.capitalize()}._supported_trading_mode_margin_pairs', + f'freqtrade.exchange.{id}.{id.capitalize()}._supported_trading_mode_margin_pairs', PropertyMock(return_value=[ (TradingMode.MARGIN, MarginMode.CROSS), (TradingMode.MARGIN, MarginMode.ISOLATED), @@ -170,10 +170,10 @@ def patch_exchange( ) if api_mock: - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._init_ccxt', return_value=api_mock) else: - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock()) - mocker.patch('freqtrade.exchange.Exchange.timeframes', PropertyMock( + mocker.patch(f'{EXMS}._init_ccxt', MagicMock()) + mocker.patch(f'{EXMS}.timeframes', PropertyMock( return_value=['5m', '15m', '1h', '1d'])) @@ -241,7 +241,6 @@ def get_patched_freqtradebot(mocker, config) -> FreqtradeBot: :return: FreqtradeBot """ patch_freqtradebot(mocker, config) - config['datadir'] = Path(config['datadir']) return FreqtradeBot(config) @@ -300,7 +299,7 @@ def create_mock_trades(fee, is_short: Optional[bool] = False, use_db: bool = Tru """ def add_trade(trade): if use_db: - Trade.query.session.add(trade) + Trade.session.add(trade) else: LocalTrade.add_bt_trade(trade) is_short1 = is_short if is_short is not None else True @@ -333,11 +332,11 @@ def create_mock_trades_with_leverage(fee, use_db: bool = True): Create some fake trades ... """ if use_db: - Trade.query.session.rollback() + Trade.session.rollback() def add_trade(trade): if use_db: - Trade.query.session.add(trade) + Trade.session.add(trade) else: LocalTrade.add_bt_trade(trade) @@ -367,7 +366,7 @@ def create_mock_trades_with_leverage(fee, use_db: bool = True): add_trade(trade) if use_db: - Trade.query.session.flush() + Trade.session.flush() def create_mock_trades_usdt(fee, is_short: Optional[bool] = False, use_db: bool = True): @@ -376,7 +375,7 @@ def create_mock_trades_usdt(fee, is_short: Optional[bool] = False, use_db: bool """ def add_trade(trade): if use_db: - Trade.query.session.add(trade) + Trade.session.add(trade) else: LocalTrade.add_bt_trade(trade) @@ -510,7 +509,7 @@ def get_default_conf(testdatadir): "chat_id": "0", "notification_settings": {}, }, - "datadir": str(testdatadir), + "datadir": Path(testdatadir), "initial_state": "running", "db_url": "sqlite://", "user_data_dir": Path("user_data"), @@ -2574,7 +2573,7 @@ def import_fails() -> None: realimport = builtins.__import__ def mockedimport(name, *args, **kwargs): - if name in ["filelock", 'systemd.journal', 'uvloop']: + if name in ["filelock", 'cysystemd.journal', 'uvloop']: raise ImportError(f"No module named '{name}'") return realimport(name, *args, **kwargs) diff --git a/tests/data/test_btanalysis.py b/tests/data/test_btanalysis.py index 345e3c299..2c5515f7c 100644 --- a/tests/data/test_btanalysis.py +++ b/tests/data/test_btanalysis.py @@ -98,7 +98,7 @@ def test_load_backtest_data_new_format(testdatadir): assert bt_data.equals(bt_data3) with pytest.raises(ValueError, match=r"File .* does not exist\."): - load_backtest_data(str("filename") + "nofile") + load_backtest_data("filename" + "nofile") with pytest.raises(ValueError, match=r"Unknown dataformat."): load_backtest_data(testdatadir / "backtest_results" / LAST_BT_RESULT_FN) diff --git a/tests/data/test_datahandler.py b/tests/data/test_datahandler.py index 6fa2de534..f673ede6e 100644 --- a/tests/data/test_datahandler.py +++ b/tests/data/test_datahandler.py @@ -105,6 +105,7 @@ def test_datahandler_ohlcv_get_available_data(testdatadir): # Convert to set to avoid failures due to sorting assert set(paircombs) == { ('UNITTEST/USDT:USDT', '1h', 'mark'), + ('XRP/USDT:USDT', '5m', 'futures'), ('XRP/USDT:USDT', '1h', 'futures'), ('XRP/USDT:USDT', '1h', 'mark'), ('XRP/USDT:USDT', '8h', 'mark'), diff --git a/tests/data/test_dataprovider.py b/tests/data/test_dataprovider.py index e0c79d52a..0e10b5848 100644 --- a/tests/data/test_dataprovider.py +++ b/tests/data/test_dataprovider.py @@ -8,7 +8,7 @@ from freqtrade.data.dataprovider import DataProvider from freqtrade.enums import CandleType, RunMode from freqtrade.exceptions import ExchangeError, OperationalException from freqtrade.plugins.pairlistmanager import PairListManager -from tests.conftest import generate_test_data, get_patched_exchange +from tests.conftest import EXMS, generate_test_data, get_patched_exchange @pytest.mark.parametrize('candle_type', [ @@ -223,7 +223,7 @@ def test_emit_df(mocker, default_conf, ohlcv_history): def test_refresh(mocker, default_conf): refresh_mock = MagicMock() - mocker.patch("freqtrade.exchange.Exchange.refresh_latest_ohlcv", refresh_mock) + mocker.patch(f"{EXMS}.refresh_latest_ohlcv", refresh_mock) exchange = get_patched_exchange(mocker, default_conf, id="binance") timeframe = default_conf["timeframe"] @@ -281,7 +281,7 @@ def test_market(mocker, default_conf, markets): def test_ticker(mocker, default_conf, tickers): ticker_mock = MagicMock(return_value=tickers()['ETH/BTC']) - mocker.patch("freqtrade.exchange.Exchange.fetch_ticker", ticker_mock) + mocker.patch(f"{EXMS}.fetch_ticker", ticker_mock) exchange = get_patched_exchange(mocker, default_conf) dp = DataProvider(default_conf, exchange) res = dp.ticker('ETH/BTC') @@ -290,7 +290,7 @@ def test_ticker(mocker, default_conf, tickers): assert res['symbol'] == 'ETH/BTC' ticker_mock = MagicMock(side_effect=ExchangeError('Pair not found')) - mocker.patch("freqtrade.exchange.Exchange.fetch_ticker", ticker_mock) + mocker.patch(f"{EXMS}.fetch_ticker", ticker_mock) exchange = get_patched_exchange(mocker, default_conf) dp = DataProvider(default_conf, exchange) res = dp.ticker('UNITTEST/BTC') @@ -301,7 +301,7 @@ def test_current_whitelist(mocker, default_conf, tickers): # patch default conf to volumepairlist default_conf['pairlists'][0] = {'method': 'VolumePairList', "number_assets": 5} - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, exchange_has=MagicMock(return_value=True), get_tickers=tickers) exchange = get_patched_exchange(mocker, default_conf) @@ -437,6 +437,7 @@ def test_dp__add_external_df(default_conf_usdt): # Add the same dataframe again - dataframe size shall not change. res = dp._add_external_df('ETH/USDT', df, last_analyzed, timeframe, CandleType.SPOT) assert res[0] is True + assert isinstance(res[1], int) assert res[1] == 0 df, _ = dp.get_producer_df('ETH/USDT', timeframe, CandleType.SPOT) assert len(df) == 24 @@ -446,6 +447,7 @@ def test_dp__add_external_df(default_conf_usdt): res = dp._add_external_df('ETH/USDT', df2, last_analyzed, timeframe, CandleType.SPOT) assert res[0] is True + assert isinstance(res[1], int) assert res[1] == 0 df, _ = dp.get_producer_df('ETH/USDT', timeframe, CandleType.SPOT) assert len(df) == 48 @@ -455,6 +457,7 @@ def test_dp__add_external_df(default_conf_usdt): res = dp._add_external_df('ETH/USDT', df3, last_analyzed, timeframe, CandleType.SPOT) assert res[0] is True + assert isinstance(res[1], int) assert res[1] == 0 df, _ = dp.get_producer_df('ETH/USDT', timeframe, CandleType.SPOT) # New length = 48 + 12 (since we have a 12 hour offset). @@ -478,6 +481,7 @@ def test_dp__add_external_df(default_conf_usdt): res = dp._add_external_df('ETH/USDT', df4, last_analyzed, timeframe, CandleType.SPOT) assert res[0] is False # 36 hours - from 2022-01-03 12:00:00+00:00 to 2022-01-05 00:00:00+00:00 + assert isinstance(res[1], int) assert res[1] == 36 df, _ = dp.get_producer_df('ETH/USDT', timeframe, CandleType.SPOT) # New length = 61 + 1 @@ -488,4 +492,5 @@ def test_dp__add_external_df(default_conf_usdt): res = dp._add_external_df('ETH/USDT', df4, last_analyzed, timeframe, CandleType.SPOT) assert res[0] is False # 36 hours - from 2022-01-03 12:00:00+00:00 to 2022-01-05 00:00:00+00:00 + assert isinstance(res[1], int) assert res[1] == 0 diff --git a/tests/data/test_entryexitanalysis.py b/tests/data/test_entryexitanalysis.py old mode 100755 new mode 100644 diff --git a/tests/data/test_history.py b/tests/data/test_history.py index 7d313c446..24ad8bcc9 100644 --- a/tests/data/test_history.py +++ b/tests/data/test_history.py @@ -26,7 +26,7 @@ from freqtrade.enums import CandleType from freqtrade.exchange import timeframe_to_minutes from freqtrade.misc import file_dump_json from freqtrade.resolvers import StrategyResolver -from tests.conftest import (CURRENT_TEST_STRATEGY, get_patched_exchange, log_has, log_has_re, +from tests.conftest import (CURRENT_TEST_STRATEGY, EXMS, get_patched_exchange, log_has, log_has_re, patch_exchange) @@ -66,7 +66,7 @@ def test_load_data_7min_timeframe(caplog, testdatadir) -> None: def test_load_data_1min_timeframe(ohlcv_history, mocker, caplog, testdatadir) -> None: - mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ohlcv_history) + mocker.patch(f'{EXMS}.get_historic_ohlcv', return_value=ohlcv_history) file = testdatadir / 'UNITTEST_BTC-1m.json' load_data(datadir=testdatadir, timeframe='1m', pairs=['UNITTEST/BTC']) assert file.is_file() @@ -77,7 +77,7 @@ def test_load_data_1min_timeframe(ohlcv_history, mocker, caplog, testdatadir) -> def test_load_data_mark(ohlcv_history, mocker, caplog, testdatadir) -> None: - mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ohlcv_history) + mocker.patch(f'{EXMS}.get_historic_ohlcv', return_value=ohlcv_history) file = testdatadir / 'futures/UNITTEST_USDT_USDT-1h-mark.json' load_data(datadir=testdatadir, timeframe='1h', pairs=['UNITTEST/BTC'], candle_type='mark') assert file.is_file() @@ -109,7 +109,7 @@ def test_load_data_with_new_pair_1min(ohlcv_history_list, mocker, caplog, Test load_pair_history() with 1 min timeframe """ tmpdir1 = Path(tmpdir) - mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ohlcv_history_list) + mocker.patch(f'{EXMS}.get_historic_ohlcv', return_value=ohlcv_history_list) exchange = get_patched_exchange(mocker, default_conf) file = tmpdir1 / 'MEME_BTC-1m.json' @@ -191,7 +191,7 @@ def test_load_cached_data_for_updating(mocker, testdatadir) -> None: test_data = None test_filename = testdatadir.joinpath('UNITTEST_BTC-1m.json') - with open(test_filename, "rt") as file: + with test_filename.open("rt") as file: test_data = json.load(file) test_data_df = ohlcv_to_dataframe(test_data, '1m', 'UNITTEST/BTC', @@ -277,7 +277,7 @@ def test_download_pair_history( subdir, file_tail ) -> None: - mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ohlcv_history_list) + mocker.patch(f'{EXMS}.get_historic_ohlcv', return_value=ohlcv_history_list) exchange = get_patched_exchange(mocker, default_conf) tmpdir1 = Path(tmpdir) file1_1 = tmpdir1 / f'{subdir}MEME_BTC-1m{file_tail}.json' @@ -328,7 +328,7 @@ def test_download_pair_history2(mocker, default_conf, testdatadir) -> None: json_dump_mock = mocker.patch( 'freqtrade.data.history.jsondatahandler.JsonDataHandler.ohlcv_store', return_value=None) - mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=tick) + mocker.patch(f'{EXMS}.get_historic_ohlcv', return_value=tick) exchange = get_patched_exchange(mocker, default_conf) _download_pair_history(datadir=testdatadir, exchange=exchange, pair="UNITTEST/BTC", timeframe='1m', candle_type='spot') @@ -340,7 +340,7 @@ def test_download_pair_history2(mocker, default_conf, testdatadir) -> None: def test_download_backtesting_data_exception(mocker, caplog, default_conf, tmpdir) -> None: - mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', + mocker.patch(f'{EXMS}.get_historic_ohlcv', side_effect=Exception('File Error')) tmpdir1 = Path(tmpdir) exchange = get_patched_exchange(mocker, default_conf) @@ -409,7 +409,7 @@ def test_init_with_refresh(default_conf, mocker) -> None: def test_file_dump_json_tofile(testdatadir) -> None: - file = testdatadir / 'test_{id}.json'.format(id=str(uuid.uuid4())) + file = testdatadir / f'test_{uuid.uuid4()}.json' data = {'bar': 'foo'} # check the file we will create does not exist @@ -506,9 +506,7 @@ def test_refresh_backtest_ohlcv_data( mocker, default_conf, markets, caplog, testdatadir, trademode, callcount): dl_mock = mocker.patch('freqtrade.data.history.history_utils._download_pair_history', MagicMock()) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) mocker.patch.object(Path, "exists", MagicMock(return_value=True)) mocker.patch.object(Path, "unlink", MagicMock()) @@ -531,9 +529,7 @@ def test_download_data_no_markets(mocker, default_conf, caplog, testdatadir): MagicMock()) ex = get_patched_exchange(mocker, default_conf) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={})) timerange = TimeRange.parse_timerange("20190101-20190102") unav_pairs = refresh_backtest_ohlcv_data(exchange=ex, pairs=["BTT/BTC", "LTC/USDT"], timeframes=["1m", "5m"], @@ -551,9 +547,7 @@ def test_download_data_no_markets(mocker, default_conf, caplog, testdatadir): def test_refresh_backtest_trades_data(mocker, default_conf, markets, caplog, testdatadir): dl_mock = mocker.patch('freqtrade.data.history.history_utils._download_trades_history', MagicMock()) - mocker.patch( - 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) mocker.patch.object(Path, "exists", MagicMock(return_value=True)) mocker.patch.object(Path, "unlink", MagicMock()) @@ -577,8 +571,7 @@ def test_download_trades_history(trades_history, mocker, default_conf, testdatad tmpdir) -> None: tmpdir1 = Path(tmpdir) ght_mock = MagicMock(side_effect=lambda pair, *args, **kwargs: (pair, trades_history)) - mocker.patch('freqtrade.exchange.Exchange.get_historic_trades', - ght_mock) + mocker.patch(f'{EXMS}.get_historic_trades', ght_mock) exchange = get_patched_exchange(mocker, default_conf) file1 = tmpdir1 / 'ETH_BTC-trades.json.gz' data_handler = get_datahandler(tmpdir1, data_format='jsongz') @@ -604,8 +597,7 @@ def test_download_trades_history(trades_history, mocker, default_conf, testdatad file1.unlink() - mocker.patch('freqtrade.exchange.Exchange.get_historic_trades', - MagicMock(side_effect=ValueError)) + mocker.patch(f'{EXMS}.get_historic_trades', MagicMock(side_effect=ValueError)) assert not _download_trades_history(data_handler=data_handler, exchange=exchange, pair='ETH/BTC') @@ -615,8 +607,7 @@ def test_download_trades_history(trades_history, mocker, default_conf, testdatad copyfile(testdatadir / file2.name, file2) ght_mock.reset_mock() - mocker.patch('freqtrade.exchange.Exchange.get_historic_trades', - ght_mock) + mocker.patch(f'{EXMS}.get_historic_trades', ght_mock) # Since before first start date since_time = int(trades_history[0][0] // 1000) - 500 timerange = TimeRange('date', None, since_time, 0) diff --git a/tests/edge/test_edge.py b/tests/edge/test_edge.py index 1b0191fda..be0346b78 100644 --- a/tests/edge/test_edge.py +++ b/tests/edge/test_edge.py @@ -14,7 +14,7 @@ from freqtrade.data.converter import ohlcv_to_dataframe from freqtrade.edge import Edge, PairInfo from freqtrade.enums import ExitType from freqtrade.exceptions import OperationalException -from tests.conftest import get_patched_freqtradebot, log_has +from tests.conftest import EXMS, get_patched_freqtradebot, log_has from tests.optimize import (BTContainer, BTrade, _build_backtest_dataframe, _get_frame_time_from_offset) @@ -139,7 +139,7 @@ def test_adjust(mocker, edge_conf): assert (edge.adjust(pairs) == ['E/F', 'C/D']) -def test_stoploss(mocker, edge_conf): +def test_edge_get_stoploss(mocker, edge_conf): freqtrade = get_patched_freqtradebot(mocker, edge_conf) edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock( @@ -150,10 +150,10 @@ def test_stoploss(mocker, edge_conf): } )) - assert edge.stoploss('E/F') == -0.01 + assert edge.get_stoploss('E/F') == -0.01 -def test_nonexisting_stoploss(mocker, edge_conf): +def test_nonexisting_get_stoploss(mocker, edge_conf): freqtrade = get_patched_freqtradebot(mocker, edge_conf) edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock( @@ -162,7 +162,7 @@ def test_nonexisting_stoploss(mocker, edge_conf): } )) - assert edge.stoploss('N/O') == -0.1 + assert edge.get_stoploss('N/O') == -0.1 def test_edge_stake_amount(mocker, edge_conf): @@ -261,7 +261,7 @@ def mocked_load_data(datadir, pairs=[], timeframe='0m', def test_edge_process_downloaded_data(mocker, edge_conf): freqtrade = get_patched_freqtradebot(mocker, edge_conf) - mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001)) + mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.001)) mocker.patch('freqtrade.edge.edge_positioning.refresh_data', MagicMock()) mocker.patch('freqtrade.edge.edge_positioning.load_data', mocked_load_data) edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) @@ -273,7 +273,7 @@ def test_edge_process_downloaded_data(mocker, edge_conf): def test_edge_process_no_data(mocker, edge_conf, caplog): freqtrade = get_patched_freqtradebot(mocker, edge_conf) - mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001)) + mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.001)) mocker.patch('freqtrade.edge.edge_positioning.refresh_data', MagicMock()) mocker.patch('freqtrade.edge.edge_positioning.load_data', MagicMock(return_value={})) edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) @@ -286,7 +286,7 @@ def test_edge_process_no_data(mocker, edge_conf, caplog): def test_edge_process_no_trades(mocker, edge_conf, caplog): freqtrade = get_patched_freqtradebot(mocker, edge_conf) - mocker.patch('freqtrade.exchange.Exchange.get_fee', return_value=0.001) + mocker.patch(f'{EXMS}.get_fee', return_value=0.001) mocker.patch('freqtrade.edge.edge_positioning.refresh_data', ) mocker.patch('freqtrade.edge.edge_positioning.load_data', mocked_load_data) # Return empty @@ -303,7 +303,7 @@ def test_edge_process_no_pairs(mocker, edge_conf, caplog): mocker.patch('freqtrade.freqtradebot.validate_config_consistency') freqtrade = get_patched_freqtradebot(mocker, edge_conf) - fee_mock = mocker.patch('freqtrade.exchange.Exchange.get_fee', return_value=0.001) + fee_mock = mocker.patch(f'{EXMS}.get_fee', return_value=0.001) mocker.patch('freqtrade.edge.edge_positioning.refresh_data') mocker.patch('freqtrade.edge.edge_positioning.load_data', mocked_load_data) # Return empty @@ -319,7 +319,7 @@ def test_edge_process_no_pairs(mocker, edge_conf, caplog): def test_edge_init_error(mocker, edge_conf,): edge_conf['stake_amount'] = 0.5 - mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001)) + mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.001)) with pytest.raises(OperationalException, match='Edge works only with unlimited stake amount'): get_patched_freqtradebot(mocker, edge_conf) diff --git a/tests/exchange/test_binance.py b/tests/exchange/test_binance.py index cb304f699..273860e15 100644 --- a/tests/exchange/test_binance.py +++ b/tests/exchange/test_binance.py @@ -7,10 +7,23 @@ import pytest from freqtrade.enums import CandleType, MarginMode, TradingMode from freqtrade.exceptions import DependencyException, InvalidOrderException, OperationalException -from tests.conftest import get_mock_coro, get_patched_exchange, log_has_re +from tests.conftest import EXMS, get_mock_coro, get_patched_exchange, log_has_re from tests.exchange.test_exchange import ccxt_exceptionhandlers +@pytest.mark.parametrize('side,type,time_in_force,expected', [ + ('buy', 'limit', 'gtc', {'timeInForce': 'GTC'}), + ('buy', 'limit', 'IOC', {'timeInForce': 'IOC'}), + ('buy', 'market', 'IOC', {}), + ('buy', 'limit', 'PO', {'postOnly': True}), + ('sell', 'limit', 'PO', {'postOnly': True}), + ('sell', 'market', 'PO', {}), + ]) +def test__get_params_binance(default_conf, mocker, side, type, time_in_force, expected): + exchange = get_patched_exchange(mocker, default_conf, id='binance') + assert exchange._get_params(side, type, 1, False, time_in_force) == expected + + @pytest.mark.parametrize('trademode', [TradingMode.FUTURES, TradingMode.SPOT]) @pytest.mark.parametrize('limitratio,expected,side', [ (None, 220 * 0.99, "sell"), @@ -20,7 +33,7 @@ from tests.exchange.test_exchange import ccxt_exceptionhandlers (0.99, 220 * 1.01, "buy"), (0.98, 220 * 1.02, "buy"), ]) -def test_stoploss_order_binance(default_conf, mocker, limitratio, expected, side, trademode): +def test_create_stoploss_order_binance(default_conf, mocker, limitratio, expected, side, trademode): api_mock = MagicMock() order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6)) order_type = 'stop_loss_limit' if trademode == TradingMode.SPOT else 'stop' @@ -34,13 +47,13 @@ def test_stoploss_order_binance(default_conf, mocker, limitratio, expected, side default_conf['dry_run'] = False default_conf['margin_mode'] = MarginMode.ISOLATED default_conf['trading_mode'] = trademode - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') - with pytest.raises(OperationalException): - order = exchange.stoploss( + with pytest.raises(InvalidOrderException): + order = exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=190, @@ -50,11 +63,11 @@ def test_stoploss_order_binance(default_conf, mocker, limitratio, expected, side ) api_mock.create_order.reset_mock() - order_types = {'stoploss': 'limit'} + order_types = {'stoploss': 'limit', 'stoploss_price_type': 'mark'} if limitratio is not None: order_types.update({'stoploss_on_exchange_limit_ratio': limitratio}) - order = exchange.stoploss( + order = exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, @@ -75,14 +88,14 @@ def test_stoploss_order_binance(default_conf, mocker, limitratio, expected, side if trademode == TradingMode.SPOT: params_dict = {'stopPrice': 220} else: - params_dict = {'stopPrice': 220, 'reduceOnly': True} + params_dict = {'stopPrice': 220, 'reduceOnly': True, 'workingType': 'MARK_PRICE'} assert api_mock.create_order.call_args_list[0][1]['params'] == params_dict # test exception handling with pytest.raises(DependencyException): api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance")) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') - exchange.stoploss( + exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, @@ -94,7 +107,7 @@ def test_stoploss_order_binance(default_conf, mocker, limitratio, expected, side api_mock.create_order = MagicMock( side_effect=ccxt.InvalidOrder("binance Order would trigger immediately.")) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') - exchange.stoploss( + exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, @@ -104,22 +117,22 @@ def test_stoploss_order_binance(default_conf, mocker, limitratio, expected, side ) ccxt_exceptionhandlers(mocker, default_conf, api_mock, "binance", - "stoploss", "create_order", retries=1, + "create_stoploss", "create_order", retries=1, pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side=side, leverage=1.0) -def test_stoploss_order_dry_run_binance(default_conf, mocker): +def test_create_stoploss_order_dry_run_binance(default_conf, mocker): api_mock = MagicMock() order_type = 'stop_loss_limit' default_conf['dry_run'] = True - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') - with pytest.raises(OperationalException): - order = exchange.stoploss( + with pytest.raises(InvalidOrderException): + order = exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=190, @@ -130,7 +143,7 @@ def test_stoploss_order_dry_run_binance(default_conf, mocker): api_mock.create_order.reset_mock() - order = exchange.stoploss( + order = exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, @@ -495,7 +508,8 @@ def test_fill_leverage_tiers_binance_dryrun(default_conf, mocker, leverage_tiers for key, value in leverage_tiers.items(): v = exchange._leverage_tiers[key] assert isinstance(v, list) - assert len(v) == len(value) + # Assert if conftest leverage tiers have less or equal tiers than the exchange + assert len(v) >= len(value) def test_additional_exchange_init_binance(default_conf, mocker): @@ -522,8 +536,15 @@ def test__set_leverage_binance(mocker, default_conf): api_mock.set_leverage = MagicMock() type(api_mock).has = PropertyMock(return_value={'setLeverage': True}) default_conf['dry_run'] = False - exchange = get_patched_exchange(mocker, default_conf, id="binance") - exchange._set_leverage(3.0, trading_mode=TradingMode.MARGIN) + default_conf['trading_mode'] = TradingMode.FUTURES + default_conf['margin_mode'] = MarginMode.ISOLATED + + exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance") + exchange._set_leverage(3.2, 'BTC/USDT:USDT') + assert api_mock.set_leverage.call_count == 1 + # Leverage is rounded to 3. + assert api_mock.set_leverage.call_args_list[0][1]['leverage'] == 3 + assert api_mock.set_leverage.call_args_list[0][1]['symbol'] == 'BTC/USDT:USDT' ccxt_exceptionhandlers( mocker, @@ -534,7 +555,6 @@ def test__set_leverage_binance(mocker, default_conf): "set_leverage", pair="XRP/USDT", leverage=5.0, - trading_mode=TradingMode.FUTURES ) @@ -592,7 +612,7 @@ def test_get_maintenance_ratio_and_amt_binance( mm_ratio, amt, ): - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf, id="binance") exchange._leverage_tiers = leverage_tiers (result_ratio, result_amt) = exchange.get_maintenance_ratio_and_amt(pair, nominal_value) diff --git a/tests/exchange/test_bitpanda.py b/tests/exchange/test_bitpanda.py index 4bd168e7e..de44be986 100644 --- a/tests/exchange/test_bitpanda.py +++ b/tests/exchange/test_bitpanda.py @@ -1,7 +1,7 @@ from datetime import datetime from unittest.mock import MagicMock -from tests.conftest import get_patched_exchange +from tests.conftest import EXMS, get_patched_exchange def test_get_trades_for_order(default_conf, mocker): @@ -9,7 +9,7 @@ def test_get_trades_for_order(default_conf, mocker): order_id = 'ABCD-ABCD' since = datetime(2018, 5, 5, 0, 0, 0) default_conf["dry_run"] = False - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) api_mock = MagicMock() api_mock.fetch_my_trades = MagicMock(return_value=[{'id': 'TTR67E-3PFBD-76IISV', diff --git a/tests/exchange/test_bybit.py b/tests/exchange/test_bybit.py new file mode 100644 index 000000000..d0d5114a1 --- /dev/null +++ b/tests/exchange/test_bybit.py @@ -0,0 +1,74 @@ +from datetime import datetime, timezone +from unittest.mock import MagicMock + +from freqtrade.enums.marginmode import MarginMode +from freqtrade.enums.tradingmode import TradingMode +from freqtrade.exchange.exchange_utils import timeframe_to_msecs +from tests.conftest import get_mock_coro, get_patched_exchange +from tests.exchange.test_exchange import ccxt_exceptionhandlers + + +def test_additional_exchange_init_bybit(default_conf, mocker): + default_conf['dry_run'] = False + default_conf['trading_mode'] = TradingMode.FUTURES + default_conf['margin_mode'] = MarginMode.ISOLATED + api_mock = MagicMock() + api_mock.set_position_mode = MagicMock(return_value={"dualSidePosition": False}) + get_patched_exchange(mocker, default_conf, id="bybit", api_mock=api_mock) + assert api_mock.set_position_mode.call_count == 1 + ccxt_exceptionhandlers(mocker, default_conf, api_mock, 'bybit', + "additional_exchange_init", "set_position_mode") + + +async def test_bybit_fetch_funding_rate(default_conf, mocker): + default_conf['trading_mode'] = 'futures' + default_conf['margin_mode'] = 'isolated' + api_mock = MagicMock() + api_mock.fetch_funding_rate_history = get_mock_coro(return_value=[]) + exchange = get_patched_exchange(mocker, default_conf, id='bybit', api_mock=api_mock) + limit = 200 + # Test fetch_funding_rate_history (current data) + await exchange._fetch_funding_rate_history( + pair='BTC/USDT:USDT', + timeframe='4h', + limit=limit, + ) + + assert api_mock.fetch_funding_rate_history.call_count == 1 + assert api_mock.fetch_funding_rate_history.call_args_list[0][0][0] == 'BTC/USDT:USDT' + kwargs = api_mock.fetch_funding_rate_history.call_args_list[0][1] + assert kwargs['params'] == {} + assert kwargs['since'] is None + + api_mock.fetch_funding_rate_history.reset_mock() + since_ms = 1610000000000 + since_ms_end = since_ms + (timeframe_to_msecs('4h') * limit) + # Test fetch_funding_rate_history (current data) + await exchange._fetch_funding_rate_history( + pair='BTC/USDT:USDT', + timeframe='4h', + limit=limit, + since_ms=since_ms, + ) + + assert api_mock.fetch_funding_rate_history.call_count == 1 + assert api_mock.fetch_funding_rate_history.call_args_list[0][0][0] == 'BTC/USDT:USDT' + kwargs = api_mock.fetch_funding_rate_history.call_args_list[0][1] + assert kwargs['params'] == {'until': since_ms_end} + assert kwargs['since'] == since_ms + + +def test_bybit_get_funding_fees(default_conf, mocker): + now = datetime.now(timezone.utc) + exchange = get_patched_exchange(mocker, default_conf, id='bybit') + exchange._fetch_and_calculate_funding_fees = MagicMock() + exchange.get_funding_fees('BTC/USDT:USDT', 1, False, now) + assert exchange._fetch_and_calculate_funding_fees.call_count == 0 + + default_conf['trading_mode'] = 'futures' + default_conf['margin_mode'] = 'isolated' + exchange = get_patched_exchange(mocker, default_conf, id='bybit') + exchange._fetch_and_calculate_funding_fees = MagicMock() + exchange.get_funding_fees('BTC/USDT:USDT', 1, False, now) + + assert exchange._fetch_and_calculate_funding_fees.call_count == 1 diff --git a/tests/exchange/test_ccxt_compat.py b/tests/exchange/test_ccxt_compat.py index 7ea0a3b9f..872cf5059 100644 --- a/tests/exchange/test_ccxt_compat.py +++ b/tests/exchange/test_ccxt_compat.py @@ -17,7 +17,7 @@ from freqtrade.enums import CandleType from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date from freqtrade.exchange.exchange import Exchange, timeframe_to_msecs from freqtrade.resolvers.exchange_resolver import ExchangeResolver -from tests.conftest import get_default_conf_usdt +from tests.conftest import EXMS, get_default_conf_usdt EXCHANGE_FIXTURE_TYPE = Tuple[Exchange, str] @@ -43,6 +43,49 @@ EXCHANGES = { 'hasQuoteVolumeFutures': True, 'leverage_tiers_public': False, 'leverage_in_spot_market': False, + 'sample_order': [{ + "symbol": "SOLUSDT", + "orderId": 3551312894, + "orderListId": -1, + "clientOrderId": "x-R4DD3S8297c73a11ccb9dc8f2811ba", + "transactTime": 1674493798550, + "price": "15.50000000", + "origQty": "1.10000000", + "executedQty": "0.00000000", + "cummulativeQuoteQty": "0.00000000", + "status": "NEW", + "timeInForce": "GTC", + "type": "LIMIT", + "side": "BUY", + "workingTime": 1674493798550, + "fills": [], + "selfTradePreventionMode": "NONE", + }] + }, + 'binanceus': { + 'pair': 'BTC/USDT', + 'stake_currency': 'USDT', + 'hasQuoteVolume': True, + 'timeframe': '5m', + 'futures': False, + 'sample_order': [{ + "symbol": "SOLUSDT", + "orderId": 3551312894, + "orderListId": -1, + "clientOrderId": "x-R4DD3S8297c73a11ccb9dc8f2811ba", + "transactTime": 1674493798550, + "price": "15.50000000", + "origQty": "1.10000000", + "executedQty": "0.00000000", + "cummulativeQuoteQty": "0.00000000", + "status": "NEW", + "timeInForce": "GTC", + "type": "LIMIT", + "side": "BUY", + "workingTime": 1674493798550, + "fills": [], + "selfTradePreventionMode": "NONE", + }] }, 'kraken': { 'pair': 'BTC/USDT', @@ -59,8 +102,42 @@ EXCHANGES = { 'timeframe': '5m', 'leverage_tiers_public': False, 'leverage_in_spot_market': True, + 'sample_order': [ + {'id': '63d6742d0adc5570001d2bbf7'}, # create order + { + 'id': '63d6742d0adc5570001d2bbf7', + 'symbol': 'SOL-USDT', + 'opType': 'DEAL', + 'type': 'limit', + 'side': 'buy', + 'price': '15.5', + 'size': '1.1', + 'funds': '0', + 'dealFunds': '17.05', + 'dealSize': '1.1', + 'fee': '0.000065252', + 'feeCurrency': 'USDT', + 'stp': '', + 'stop': '', + 'stopTriggered': False, + 'stopPrice': '0', + 'timeInForce': 'GTC', + 'postOnly': False, + 'hidden': False, + 'iceberg': False, + 'visibleSize': '0', + 'cancelAfter': 0, + 'channel': 'API', + 'clientOid': '0a053870-11bf-41e5-be61-b272a4cb62e1', + 'remark': None, + 'tags': 'partner:ccxt', + 'isActive': False, + 'cancelExist': False, + 'createdAt': 1674493798550, + 'tradeType': 'TRADE' + }], }, - 'gateio': { + 'gate': { 'pair': 'BTC/USDT', 'stake_currency': 'USDT', 'hasQuoteVolume': True, @@ -70,6 +147,69 @@ EXCHANGES = { 'hasQuoteVolumeFutures': True, 'leverage_tiers_public': True, 'leverage_in_spot_market': True, + 'sample_order': [ + { + "id": "276266139423", + "text": "apiv4", + "create_time": "1674493798", + "update_time": "1674493798", + "create_time_ms": "1674493798550", + "update_time_ms": "1674493798550", + "status": "closed", + "currency_pair": "SOL_USDT", + "type": "limit", + "account": "spot", + "side": "buy", + "amount": "1.1", + "price": "15.5", + "time_in_force": "gtc", + "iceberg": "0", + "left": "0", + "fill_price": "17.05", + "filled_total": "17.05", + "avg_deal_price": "15.5", + "fee": "0.0000018", + "fee_currency": "SOL", + "point_fee": "0", + "gt_fee": "0", + "gt_maker_fee": "0", + "gt_taker_fee": "0.0015", + "gt_discount": True, + "rebated_fee": "0", + "rebated_fee_currency": "USDT" + }, + { + # market order + 'id': '276401180529', + 'text': 'apiv4', + 'create_time': '1674493798', + 'update_time': '1674493798', + 'create_time_ms': '1674493798550', + 'update_time_ms': '1674493798550', + 'status': 'cancelled', + 'currency_pair': 'SOL_USDT', + 'type': 'market', + 'account': 'spot', + 'side': 'buy', + 'amount': '17.05', + 'price': '0', + 'time_in_force': 'ioc', + 'iceberg': '0', + 'left': '0.0000000016228', + 'fill_price': '17.05', + 'filled_total': '17.05', + 'avg_deal_price': '15.5', + 'fee': '0', + 'fee_currency': 'SOL', + 'point_fee': '0.0199999999967544', + 'gt_fee': '0', + 'gt_maker_fee': '0', + 'gt_taker_fee': '0', + 'gt_discount': False, + 'rebated_fee': '0', + 'rebated_fee_currency': 'USDT' + } + ], }, 'okx': { 'pair': 'BTC/USDT', @@ -82,6 +222,33 @@ EXCHANGES = { 'leverage_tiers_public': True, 'leverage_in_spot_market': True, }, + 'bybit': { + 'pair': 'BTC/USDT', + 'stake_currency': 'USDT', + 'hasQuoteVolume': True, + 'timeframe': '5m', + 'futures_pair': 'BTC/USDT:USDT', + 'futures': True, + 'leverage_tiers_public': True, + 'leverage_in_spot_market': True, + 'sample_order': [ + { + "orderId": "1274754916287346280", + "orderLinkId": "1666798627015730", + "symbol": "SOLUSDT", + "createTime": "1674493798550", + "orderPrice": "15.5", + "orderQty": "1.1", + "orderType": "LIMIT", + "side": "BUY", + "status": "NEW", + "timeInForce": "GTC", + "accountId": "5555555", + "execQty": "0", + "orderCategory": "0" + } + ] + }, 'huobi': { 'pair': 'ETH/BTC', 'stake_currency': 'BTC', @@ -142,7 +309,7 @@ def exchange(request, exchange_conf): @pytest.fixture(params=EXCHANGES, scope="class") def exchange_futures(request, exchange_conf, class_mocker): - if not EXCHANGES[request.param].get('futures') is True: + if EXCHANGES[request.param].get('futures') is not True: yield None, request.param else: exchange_conf = set_test_proxy( @@ -155,12 +322,12 @@ def exchange_futures(request, exchange_conf, class_mocker): class_mocker.patch( 'freqtrade.exchange.binance.Binance.fill_leverage_tiers') - class_mocker.patch('freqtrade.exchange.exchange.Exchange.fetch_trading_fees') + class_mocker.patch(f'{EXMS}.fetch_trading_fees') class_mocker.patch('freqtrade.exchange.okx.Okx.additional_exchange_init') class_mocker.patch('freqtrade.exchange.binance.Binance.additional_exchange_init') - class_mocker.patch('freqtrade.exchange.exchange.Exchange.load_cached_leverage_tiers', - return_value=None) - class_mocker.patch('freqtrade.exchange.exchange.Exchange.cache_leverage_tiers') + class_mocker.patch('freqtrade.exchange.bybit.Bybit.additional_exchange_init') + class_mocker.patch(f'{EXMS}.load_cached_leverage_tiers', return_value=None) + class_mocker.patch(f'{EXMS}.cache_leverage_tiers') exchange = ExchangeResolver.load_exchange( request.param, exchange_conf, validate=True, load_leverage_tiers=True) @@ -189,8 +356,8 @@ class TestCCXTExchange(): 'stoploss': 'limit', }) - if exchangename == 'gateio': - # gateio doesn't have market orders on spot + if exchangename == 'gate': + # gate doesn't have market orders on spot return exch.validate_ordertypes({ 'entry': 'market', @@ -211,6 +378,32 @@ class TestCCXTExchange(): assert exchange.market_is_future(markets[pair]) + def test_ccxt_order_parse(self, exchange: EXCHANGE_FIXTURE_TYPE): + exch, exchange_name = exchange + if orders := EXCHANGES[exchange_name].get('sample_order'): + for order in orders: + po = exch._api.parse_order(order) + assert isinstance(po['id'], str) + assert po['id'] is not None + if len(order.keys()) < 5: + # Kucoin case + assert po['status'] == 'closed' + continue + assert po['timestamp'] == 1674493798550 + assert isinstance(po['datetime'], str) + assert isinstance(po['timestamp'], int) + assert isinstance(po['price'], float) + assert po['price'] == 15.5 + if po['average'] is not None: + assert isinstance(po['average'], float) + assert po['average'] == 15.5 + assert po['symbol'] == 'SOL/USDT' + assert isinstance(po['amount'], float) + assert po['amount'] == 1.1 + assert isinstance(po['status'], str) + else: + pytest.skip(f"No sample order available for exchange {exchange_name}") + def test_ccxt_fetch_tickers(self, exchange: EXCHANGE_FIXTURE_TYPE): exch, exchangename = exchange pair = EXCHANGES[exchangename]['pair'] @@ -227,7 +420,7 @@ class TestCCXTExchange(): def test_ccxt_fetch_tickers_futures(self, exchange_futures: EXCHANGE_FIXTURE_TYPE): exch, exchangename = exchange_futures - if not exch or exchangename in ('gateio'): + if not exch or exchangename in ('gate'): # exchange_futures only returns values for supported exchanges return @@ -267,10 +460,12 @@ class TestCCXTExchange(): assert len(l2['bids']) >= 1 l2_limit_range = exch._ft_has['l2_limit_range'] l2_limit_range_required = exch._ft_has['l2_limit_range_required'] - if exchangename == 'gateio': - # TODO: Gateio is unstable here at the moment, ignoring the limit partially. + if exchangename == 'gate': + # TODO: Gate is unstable here at the moment, ignoring the limit partially. return - for val in [1, 2, 5, 25, 100]: + for val in [1, 2, 5, 25, 50, 100]: + if val > 50 and exchangename == 'bybit': + continue l2 = exch.fetch_l2_order_book(pair, val) if not l2_limit_range or val in l2_limit_range: if val > 50: @@ -340,9 +535,12 @@ class TestCCXTExchange(): def test_ccxt__async_get_candle_history(self, exchange: EXCHANGE_FIXTURE_TYPE): exc, exchangename = exchange - # For some weired reason, this test returns random lengths for bittrex. - if not exc._ft_has['ohlcv_has_history'] or exchangename in ('bittrex'): - return + if exchangename in ('bittrex'): + # For some weired reason, this test returns random lengths for bittrex. + pytest.skip("Exchange doesn't provide stable ohlcv history") + + if not exc._ft_has['ohlcv_has_history']: + pytest.skip("Exchange does not support candle history") pair = EXCHANGES[exchangename]['pair'] timeframe = EXCHANGES[exchangename]['timeframe'] self.ccxt__async_get_candle_history( @@ -522,23 +720,25 @@ class TestCCXTExchange(): ) liquidation_price = futures.dry_run_liquidation_price( - futures_pair, - 40000, - False, - 100, - 100, - 100, + pair=futures_pair, + open_rate=40000, + is_short=False, + amount=100, + stake_amount=100, + leverage=5, + wallet_balance=100, ) assert (isinstance(liquidation_price, float)) assert liquidation_price >= 0.0 liquidation_price = futures.dry_run_liquidation_price( - futures_pair, - 40000, - False, - 100, - 100, - 100, + pair=futures_pair, + open_rate=40000, + is_short=False, + amount=100, + stake_amount=100, + leverage=5, + wallet_balance=100, ) assert (isinstance(liquidation_price, float)) assert liquidation_price >= 0.0 diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 0fa7f90ec..586f023b4 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -12,8 +12,8 @@ from pandas import DataFrame from freqtrade.enums import CandleType, MarginMode, TradingMode from freqtrade.exceptions import (DDosProtection, DependencyException, ExchangeError, - InvalidOrderException, OperationalException, PricingError, - TemporaryError) + InsufficientFundsError, InvalidOrderException, + OperationalException, PricingError, TemporaryError) from freqtrade.exchange import (Binance, Bittrex, Exchange, Kraken, amount_to_precision, date_minus_candles, market_is_active, price_to_precision, timeframe_to_minutes, timeframe_to_msecs, timeframe_to_next_date, @@ -22,12 +22,12 @@ from freqtrade.exchange.common import (API_FETCH_ORDER_RETRY_COUNT, API_RETRY_CO calculate_backoff, remove_credentials) from freqtrade.exchange.exchange import amount_to_contract_precision from freqtrade.resolvers.exchange_resolver import ExchangeResolver -from tests.conftest import (generate_test_data_raw, get_mock_coro, get_patched_exchange, log_has, - log_has_re, num_log_has_re) +from tests.conftest import (EXMS, generate_test_data_raw, get_mock_coro, get_patched_exchange, + log_has, log_has_re, num_log_has_re) # Make sure to always keep one exchange here which is NOT subclassed!! -EXCHANGES = ['bittrex', 'binance', 'kraken', 'gateio'] +EXCHANGES = ['bittrex', 'binance', 'kraken', 'gate', 'kucoin', 'bybit'] get_entry_rate_data = [ ('other', 20, 19, 10, 0.0, 20), # Full ask side @@ -150,9 +150,9 @@ def test_remove_credentials(default_conf, caplog) -> None: def test_init_ccxt_kwargs(default_conf, mocker, caplog): - mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - aei_mock = mocker.patch('freqtrade.exchange.Exchange.additional_exchange_init') + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_stakecurrency') + aei_mock = mocker.patch(f'{EXMS}.additional_exchange_init') caplog.set_level(logging.INFO) conf = copy.deepcopy(default_conf) @@ -218,12 +218,12 @@ def test_init_exception(default_conf, mocker): def test_exchange_resolver(default_conf, mocker, caplog): - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=MagicMock())) - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=MagicMock())) + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') exchange = ExchangeResolver.load_exchange('zaif', default_conf) assert isinstance(exchange, Exchange) @@ -362,9 +362,8 @@ def test_price_to_precision(price, precision_mode, precision, expected): def test_price_get_one_pip(default_conf, mocker, price, precision_mode, precision, expected): markets = PropertyMock(return_value={'ETH/BTC': {'precision': {'price': precision}}}) exchange = get_patched_exchange(mocker, default_conf, id="binance") - mocker.patch('freqtrade.exchange.Exchange.markets', markets) - mocker.patch('freqtrade.exchange.Exchange.precisionMode', - PropertyMock(return_value=precision_mode)) + mocker.patch(f'{EXMS}.markets', markets) + mocker.patch(f'{EXMS}.precisionMode', PropertyMock(return_value=precision_mode)) pair = 'ETH/BTC' assert pytest.approx(exchange.price_get_one_pip(pair, price)) == expected @@ -376,10 +375,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: markets = {'ETH/BTC': {'symbol': 'ETH/BTC'}} # no pair found - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) with pytest.raises(ValueError, match=r'.*get market information.*'): exchange.get_min_pair_stake_amount('BNB/BTC', 1, stoploss) @@ -388,10 +384,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: 'cost': {'min': None, 'max': None}, 'amount': {'min': None, 'max': None}, } - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss) assert result is None result = exchange.get_max_pair_stake_amount('ETH/BTC', 1) @@ -402,10 +395,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: 'cost': {'min': 2, 'max': 10000}, 'amount': {'min': None, 'max': None}, } - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) # min result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss) expected_result = 2 * (1 + 0.05) / (1 - abs(stoploss)) @@ -422,10 +412,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: 'cost': {'min': None, 'max': None}, 'amount': {'min': 2, 'max': 10000}, } - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss) expected_result = 2 * 2 * (1 + 0.05) / (1 - abs(stoploss)) assert pytest.approx(result) == expected_result @@ -441,10 +428,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: 'cost': {'min': 2, 'max': None}, 'amount': {'min': 2, 'max': None}, } - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss) expected_result = max(2, 2 * 2) * (1 + 0.05) / (1 - abs(stoploss)) assert pytest.approx(result) == expected_result @@ -457,10 +441,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: 'cost': {'min': 8, 'max': 10000}, 'amount': {'min': 2, 'max': 500}, } - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss) expected_result = max(8, 2 * 2) * (1 + 0.05) / (1 - abs(stoploss)) assert pytest.approx(result) == expected_result @@ -496,10 +477,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: default_conf['trading_mode'] = 'futures' default_conf['margin_mode'] = 'isolated' exchange = get_patched_exchange(mocker, default_conf, id="binance") - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) # Contract size 0.01 result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1) @@ -509,10 +487,7 @@ def test__get_stake_amount_limit(mocker, default_conf) -> None: assert result == 10 markets["ETH/BTC"]["contractSize"] = '10' - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) # With Leverage, Contract size 10 result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0) assert pytest.approx(result) == (expected_result / 12) * 10.0 @@ -531,10 +506,7 @@ def test_get_min_pair_stake_amount_real_data(mocker, default_conf) -> None: 'cost': {'min': 0.0001, 'max': 4000}, 'amount': {'min': 0.001, 'max': 10000}, } - mocker.patch( - 'freqtrade.exchange.Exchange.markets', - PropertyMock(return_value=markets) - ) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) result = exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss) expected_result = max(0.0001, 0.001 * 0.020405) * (1 + 0.05) / (1 - abs(stoploss)) assert round(result, 8) == round(expected_result, 8) @@ -592,12 +564,12 @@ def test_set_sandbox_exception(default_conf, mocker): def test__load_async_markets(default_conf, mocker, caplog): - mocker.patch('freqtrade.exchange.Exchange._init_ccxt') - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') + mocker.patch(f'{EXMS}._init_ccxt') + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_markets') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') exchange = Exchange(default_conf) exchange._api_async.load_markets = get_mock_coro(None) exchange._load_async_markets() @@ -614,19 +586,19 @@ def test__load_markets(default_conf, mocker, caplog): caplog.set_level(logging.INFO) api_mock = MagicMock() api_mock.load_markets = MagicMock(side_effect=ccxt.BaseError("SomeError")) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') Exchange(default_conf) assert log_has('Unable to initialize markets.', caplog) expected_return = {'ETH/BTC': 'available'} api_mock = MagicMock() api_mock.load_markets = MagicMock(return_value=expected_return) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) default_conf['exchange']['pair_whitelist'] = ['ETH/BTC'] ex = Exchange(default_conf) @@ -684,11 +656,11 @@ def test_validate_stakecurrency(default_conf, stake_currency, mocker, caplog): 'ETH/BTC': {'quote': 'BTC'}, 'LTC/BTC': {'quote': 'BTC'}, 'XRP/ETH': {'quote': 'ETH'}, 'NEO/USDT': {'quote': 'USDT'}, }) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_pricing') Exchange(default_conf) @@ -699,17 +671,17 @@ def test_validate_stakecurrency_error(default_conf, mocker, caplog): 'ETH/BTC': {'quote': 'BTC'}, 'LTC/BTC': {'quote': 'BTC'}, 'XRP/ETH': {'quote': 'ETH'}, 'NEO/USDT': {'quote': 'USDT'}, }) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') with pytest.raises(OperationalException, match=r'XRP is not available as stake on .*' 'Available currencies are: BTC, ETH, USDT'): Exchange(default_conf) type(api_mock).load_markets = MagicMock(side_effect=ccxt.NetworkError('No connection.')) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) with pytest.raises(OperationalException, match=r'Could not load markets, therefore cannot start\. Please.*'): @@ -757,11 +729,11 @@ def test_validate_pairs(default_conf, mocker): # test exchange.validate_pairs d id_mock = PropertyMock(return_value='test_exchange') type(api_mock).id = id_mock - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') Exchange(default_conf) @@ -770,10 +742,10 @@ def test_validate_pairs_not_available(default_conf, mocker): type(api_mock).markets = PropertyMock(return_value={ 'XRP/BTC': {'inactive': True, 'base': 'XRP', 'quote': 'BTC'} }) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}._load_async_markets') with pytest.raises(OperationalException, match=r'not available'): Exchange(default_conf) @@ -782,19 +754,19 @@ def test_validate_pairs_not_available(default_conf, mocker): def test_validate_pairs_exception(default_conf, mocker, caplog): caplog.set_level(logging.INFO) api_mock = MagicMock() - mocker.patch('freqtrade.exchange.Exchange.name', PropertyMock(return_value='Binance')) + mocker.patch(f'{EXMS}.name', PropertyMock(return_value='Binance')) type(api_mock).markets = PropertyMock(return_value={}) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', api_mock) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') + mocker.patch(f'{EXMS}._init_ccxt', api_mock) + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') + mocker.patch(f'{EXMS}._load_async_markets') with pytest.raises(OperationalException, match=r'Pair ETH/BTC is not available on Binance'): Exchange(default_conf) - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value={})) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={})) Exchange(default_conf) assert log_has('Unable to validate pairs (assuming they are correct).', caplog) @@ -806,11 +778,11 @@ def test_validate_pairs_restricted(default_conf, mocker, caplog): 'XRP/BTC': {'quote': 'BTC', 'info': {'prohibitedIn': ['US']}}, 'NEO/BTC': {'quote': 'BTC', 'info': 'TestString'}, # info can also be a string ... }) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_pricing') + mocker.patch(f'{EXMS}.validate_stakecurrency') Exchange(default_conf) assert log_has("Pair XRP/BTC is restricted for some users on this exchange." @@ -825,11 +797,11 @@ def test_validate_pairs_stakecompatibility(default_conf, mocker, caplog): 'XRP/BTC': {'quote': 'BTC'}, 'NEO/BTC': {'quote': 'BTC'}, 'HELLO-WORLD': {'quote': 'BTC'}, }) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') Exchange(default_conf) @@ -842,11 +814,11 @@ def test_validate_pairs_stakecompatibility_downloaddata(default_conf, mocker, ca 'XRP/BTC': {'quote': 'BTC'}, 'NEO/BTC': {'quote': 'BTC'}, 'HELLO-WORLD': {'quote': 'BTC'}, }) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') Exchange(default_conf) assert type(api_mock).load_markets.call_count == 1 @@ -860,10 +832,10 @@ def test_validate_pairs_stakecompatibility_fail(default_conf, mocker, caplog): 'XRP/BTC': {'quote': 'BTC'}, 'NEO/BTC': {'quote': 'BTC'}, 'HELLO-WORLD': {'quote': 'USDT'}, }) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_stakecurrency') with pytest.raises(OperationalException, match=r"Stake-currency 'BTC' not compatible with.*"): Exchange(default_conf) @@ -883,11 +855,11 @@ def test_validate_timeframes(default_conf, mocker, timeframe): '1h': '1h'}) type(api_mock).timeframes = timeframes - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') Exchange(default_conf) @@ -903,9 +875,9 @@ def test_validate_timeframes_failed(default_conf, mocker): '1h': '1h'}) type(api_mock).timeframes = timeframes - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock()) + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_pairs', MagicMock()) with pytest.raises(OperationalException, match=r"Invalid timeframe '3m'. This exchange supports.*"): Exchange(default_conf) @@ -925,10 +897,10 @@ def test_validate_timeframes_emulated_ohlcv_1(default_conf, mocker): # delete timeframes so magicmock does not autocreate it del api_mock.timeframes - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_stakecurrency') with pytest.raises(OperationalException, match=r'The ccxt library does not provide the list of timeframes ' r'for the exchange .* and this exchange ' @@ -945,11 +917,11 @@ def test_validate_timeframes_emulated_ohlcvi_2(default_conf, mocker): # delete timeframes so magicmock does not autocreate it del api_mock.timeframes - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange._load_markets', + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={'timeframes': None})) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock()) - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pairs', MagicMock()) + mocker.patch(f'{EXMS}.validate_stakecurrency') with pytest.raises(OperationalException, match=r'The ccxt library does not provide the list of timeframes ' r'for the exchange .* and this exchange ' @@ -969,12 +941,12 @@ def test_validate_timeframes_not_in_config(default_conf, mocker): '1h': '1h'}) type(api_mock).timeframes = timeframes - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') - mocker.patch('freqtrade.exchange.Exchange.validate_required_startup_candles') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') + mocker.patch(f'{EXMS}.validate_required_startup_candles') Exchange(default_conf) @@ -985,13 +957,13 @@ def test_validate_pricing(default_conf, mocker): 'fetchTicker': True, } type(api_mock).has = PropertyMock(return_value=has) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.exchange.Exchange.validate_trading_mode_and_margin_mode') - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.name', 'Binance') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_trading_mode_and_margin_mode') + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.name', 'Binance') ExchangeResolver.load_exchange('binance', default_conf) has.update({'fetchTicker': False}) with pytest.raises(OperationalException, match="Ticker pricing not available for .*"): @@ -1020,13 +992,13 @@ def test_validate_ordertypes(default_conf, mocker): api_mock = MagicMock() type(api_mock).has = PropertyMock(return_value={'createMarketOrder': True}) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') - mocker.patch('freqtrade.exchange.Exchange.name', 'Bittrex') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') + mocker.patch(f'{EXMS}.name', 'Bittrex') default_conf['order_types'] = { 'entry': 'limit', @@ -1037,7 +1009,7 @@ def test_validate_ordertypes(default_conf, mocker): Exchange(default_conf) type(api_mock).has = PropertyMock(return_value={'createMarketOrder': False}) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) default_conf['order_types'] = { 'entry': 'limit', @@ -1060,14 +1032,55 @@ def test_validate_ordertypes(default_conf, mocker): Exchange(default_conf) +@pytest.mark.parametrize('exchange_name,stopadv, expected', [ + ('binance', 'last', True), + ('binance', 'mark', True), + ('binance', 'index', False), + ('bybit', 'last', True), + ('bybit', 'mark', True), + ('bybit', 'index', True), + ('okx', 'last', True), + ('okx', 'mark', True), + ('okx', 'index', True), + ('gate', 'last', True), + ('gate', 'mark', True), + ('gate', 'index', True), + ]) +def test_validate_ordertypes_stop_advanced(default_conf, mocker, exchange_name, stopadv, expected): + + api_mock = MagicMock() + default_conf['trading_mode'] = TradingMode.FUTURES + default_conf['margin_mode'] = MarginMode.ISOLATED + type(api_mock).has = PropertyMock(return_value={'createMarketOrder': True}) + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') + default_conf['order_types'] = { + 'entry': 'limit', + 'exit': 'limit', + 'stoploss': 'limit', + 'stoploss_on_exchange': True, + 'stoploss_price_type': stopadv, + } + if expected: + ExchangeResolver.load_exchange(exchange_name, default_conf) + else: + with pytest.raises(OperationalException, + match=r'On exchange stoploss price type is not supported for .*'): + ExchangeResolver.load_exchange(exchange_name, default_conf) + + def test_validate_order_types_not_in_config(default_conf, mocker): api_mock = MagicMock() - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) - mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={})) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') + mocker.patch(f'{EXMS}._init_ccxt', MagicMock(return_value=api_mock)) + mocker.patch(f'{EXMS}._load_markets', MagicMock(return_value={})) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}.validate_pricing') + mocker.patch(f'{EXMS}.validate_stakecurrency') conf = copy.deepcopy(default_conf) Exchange(conf) @@ -1075,14 +1088,14 @@ def test_validate_order_types_not_in_config(default_conf, mocker): def test_validate_required_startup_candles(default_conf, mocker, caplog): api_mock = MagicMock() - mocker.patch('freqtrade.exchange.Exchange.name', PropertyMock(return_value='Binance')) + mocker.patch(f'{EXMS}.name', PropertyMock(return_value='Binance')) - mocker.patch('freqtrade.exchange.Exchange._init_ccxt', api_mock) - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange._load_async_markets') - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') + mocker.patch(f'{EXMS}._init_ccxt', api_mock) + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}._load_async_markets') + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_pricing') + mocker.patch(f'{EXMS}.validate_stakecurrency') default_conf['startup_candle_count'] = 20 ex = Exchange(default_conf) @@ -1179,11 +1192,10 @@ def test_create_dry_run_order_fees( fee, ): mocker.patch( - 'freqtrade.exchange.Exchange.get_fee', - side_effect=lambda symbol, taker_or_maker: 2.0 if taker_or_maker == 'taker' else 1.0 + f'{EXMS}.get_fee', + side_effect=lambda symbol, taker_or_maker: 2.0 if taker_or_maker == 'taker' else 1.0 ) - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', - return_value=price_side == 'other') + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=price_side == 'other') exchange = get_patched_exchange(mocker, default_conf) order = exchange.create_dry_run_order( @@ -1200,33 +1212,34 @@ def test_create_dry_run_order_fees( else: assert order['fee'] is None - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', - return_value=price_side != 'other') + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=price_side != 'other') order1 = exchange.fetch_dry_run_order(order['id']) assert order1['fee']['rate'] == fee -@pytest.mark.parametrize("side,price,filled", [ +@pytest.mark.parametrize("side,price,filled,converted", [ # order_book_l2_usd spread: # best ask: 25.566 # best bid: 25.563 - ("buy", 25.563, False), - ("buy", 25.566, True), - ("sell", 25.566, False), - ("sell", 25.563, True), + ("buy", 25.563, False, False), + ("buy", 25.566, True, False), + ("sell", 25.566, False, False), + ("sell", 25.563, True, False), + ("buy", 29.563, True, True), + ("sell", 21.563, True, True), ]) @pytest.mark.parametrize("exchange_name", EXCHANGES) -def test_create_dry_run_order_limit_fill(default_conf, mocker, side, price, filled, - exchange_name, order_book_l2_usd): +def test_create_dry_run_order_limit_fill(default_conf, mocker, side, price, filled, caplog, + exchange_name, order_book_l2_usd, converted): default_conf['dry_run'] = True exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, exchange_has=MagicMock(return_value=True), fetch_l2_order_book=order_book_l2_usd, ) - order = exchange.create_dry_run_order( + order = exchange.create_order( pair='LTC/USDT', ordertype='limit', side=side, @@ -1238,9 +1251,16 @@ def test_create_dry_run_order_limit_fill(default_conf, mocker, side, price, fill assert 'id' in order assert f'dry_run_{side}_' in order["id"] assert order["side"] == side - assert order["type"] == "limit" + if not converted: + assert order["average"] == price + assert order["type"] == "limit" + else: + # Converted to market order + assert order["type"] == "market" + assert 25.5 < order["average"] < 25.6 + assert log_has_re(r"Converted .* to market order.*", caplog) + assert order["symbol"] == "LTC/USDT" - assert order["average"] == price assert order['status'] == 'open' if not filled else 'closed' order_book_l2_usd.reset_mock() @@ -1253,8 +1273,7 @@ def test_create_dry_run_order_limit_fill(default_conf, mocker, side, price, fill order_book_l2_usd.reset_mock() # Empty orderbook test - mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', - return_value={'asks': [], 'bids': []}) + mocker.patch(f'{EXMS}.fetch_l2_order_book', return_value={'asks': [], 'bids': []}) exchange._dry_run_open_orders[order['id']]['status'] = 'open' order_closed = exchange.fetch_dry_run_order(order['id']) @@ -1277,12 +1296,12 @@ def test_create_dry_run_order_market_fill(default_conf, mocker, side, rate, amou exchange_name, order_book_l2_usd): default_conf['dry_run'] = True exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, exchange_has=MagicMock(return_value=True), fetch_l2_order_book=order_book_l2_usd, ) - order = exchange.create_dry_run_order( + order = exchange.create_order( pair='LTC/USDT', ordertype='market', side=side, @@ -1322,8 +1341,8 @@ def test_create_order(default_conf, mocker, side, ordertype, rate, marketprice, }) default_conf['dry_run'] = False default_conf['margin_mode'] = 'isolated' - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange._set_leverage = MagicMock() exchange.set_margin_mode = MagicMock() @@ -1375,9 +1394,10 @@ def test_create_order(default_conf, mocker, side, ordertype, rate, marketprice, assert order['amount'] == 0.01 -def test_buy_dry_run(default_conf, mocker): +@pytest.mark.parametrize("exchange_name", EXCHANGES) +def test_buy_dry_run(default_conf, mocker, exchange_name): default_conf['dry_run'] = True - exchange = get_patched_exchange(mocker, default_conf) + exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) order = exchange.create_order(pair='ETH/BTC', ordertype='limit', side="buy", amount=1, rate=200, leverage=1.0, @@ -1401,8 +1421,8 @@ def test_buy_prod(default_conf, mocker, exchange_name): } }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) order = exchange.create_order(pair='ETH/BTC', ordertype=order_type, side="buy", @@ -1485,8 +1505,8 @@ def test_buy_considers_time_in_force(default_conf, mocker, exchange_name): } }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) order_type = 'limit' @@ -1551,8 +1571,8 @@ def test_sell_prod(default_conf, mocker, exchange_name): }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) order = exchange.create_order(pair='ETH/BTC', ordertype=order_type, @@ -1579,13 +1599,13 @@ def test_sell_prod(default_conf, mocker, exchange_name): assert api_mock.create_order.call_args[0][4] == 200 # test exception handling - with pytest.raises(DependencyException): + with pytest.raises(InsufficientFundsError): api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance")) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange.create_order(pair='ETH/BTC', ordertype=order_type, side="sell", amount=1, rate=200, leverage=1.0) - with pytest.raises(DependencyException): + with pytest.raises(InvalidOrderException): api_mock.create_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found")) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange.create_order(pair='ETH/BTC', ordertype='limit', side="sell", amount=1, rate=200, @@ -1624,8 +1644,8 @@ def test_sell_considers_time_in_force(default_conf, mocker, exchange_name): }) api_mock.options = {} default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) order_type = 'limit' @@ -1691,7 +1711,7 @@ def test_get_balances_prod(default_conf, mocker, exchange_name): @pytest.mark.parametrize("exchange_name", EXCHANGES) def test_fetch_positions(default_conf, mocker, exchange_name): - mocker.patch('freqtrade.exchange.Exchange.validate_trading_mode_and_margin_mode') + mocker.patch(f'{EXMS}.validate_trading_mode_and_margin_mode') api_mock = MagicMock() api_mock.fetch_positions = MagicMock(return_value=[ {'symbol': 'ETH/USDT:USDT', 'leverage': 5}, @@ -1742,12 +1762,12 @@ def test_fetch_trading_fees(default_conf, mocker): 'maker': 0.0, 'taker': 0.0005} } - exchange_name = 'gateio' + exchange_name = 'gate' default_conf['dry_run'] = False default_conf['trading_mode'] = TradingMode.FUTURES default_conf['margin_mode'] = MarginMode.ISOLATED api_mock.fetch_trading_fees = MagicMock(return_value=tick) - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) assert '1INCH/USDT:USDT' in exchange._trading_fees @@ -1762,7 +1782,7 @@ def test_fetch_trading_fees(default_conf, mocker): api_mock.fetch_trading_fees = MagicMock(return_value={}) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange.fetch_trading_fees() - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) assert exchange.fetch_trading_fees() == {} @@ -1782,7 +1802,7 @@ def test_fetch_bids_asks(default_conf, mocker): } exchange_name = 'binance' api_mock.fetch_bids_asks = MagicMock(return_value=tick) - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) # retrieve original ticker bidsasks = exchange.fetch_bids_asks() @@ -1815,7 +1835,7 @@ def test_fetch_bids_asks(default_conf, mocker): api_mock.fetch_bids_asks = MagicMock(return_value={}) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange.fetch_bids_asks() - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) assert exchange.fetch_bids_asks() == {} @@ -1834,7 +1854,7 @@ def test_get_tickers(default_conf, mocker, exchange_name): 'last': 41, } } - mocker.patch('freqtrade.exchange.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) api_mock.fetch_tickers = MagicMock(return_value=tick) api_mock.fetch_bids_asks = MagicMock(return_value={}) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) @@ -1877,7 +1897,7 @@ def test_get_tickers(default_conf, mocker, exchange_name): api_mock.fetch_bids_asks.reset_mock() default_conf['trading_mode'] = TradingMode.FUTURES default_conf['margin_mode'] = MarginMode.ISOLATED - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange.get_tickers() @@ -1886,7 +1906,7 @@ def test_get_tickers(default_conf, mocker, exchange_name): api_mock.fetch_tickers.reset_mock() api_mock.fetch_bids_asks.reset_mock() - mocker.patch('freqtrade.exchange.exchange.Exchange.exchange_has', return_value=False) + mocker.patch(f'{EXMS}.exchange_has', return_value=False) assert exchange.get_tickers() == {} @@ -2140,7 +2160,7 @@ def test_refresh_latest_ohlcv_cache(mocker, default_conf, candle_type, time_mach time_machine.move_to(start + timedelta(hours=99, minutes=30)) exchange = get_patched_exchange(mocker, default_conf) - mocker.patch("freqtrade.exchange.Exchange.ohlcv_candle_limit", return_value=100) + mocker.patch(f"{EXMS}.ohlcv_candle_limit", return_value=100) assert exchange._startup_candle_count == 0 exchange._api_async.fetch_ohlcv = get_mock_coro(ohlcv) @@ -2165,7 +2185,7 @@ def test_refresh_latest_ohlcv_cache(mocker, default_conf, candle_type, time_mach assert len(res[pair1]) == 99 assert len(res[pair2]) == 99 assert exchange._klines - assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-1][0] // 1000 + assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-2][0] // 1000 exchange._api_async.fetch_ohlcv.reset_mock() # Returned from cache @@ -2174,7 +2194,7 @@ def test_refresh_latest_ohlcv_cache(mocker, default_conf, candle_type, time_mach assert len(res) == 2 assert len(res[pair1]) == 99 assert len(res[pair2]) == 99 - assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-1][0] // 1000 + assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-2][0] // 1000 # Move time 1 candle further but result didn't change yet time_machine.move_to(start + timedelta(hours=101)) @@ -2184,13 +2204,13 @@ def test_refresh_latest_ohlcv_cache(mocker, default_conf, candle_type, time_mach assert len(res[pair1]) == 99 assert len(res[pair2]) == 99 assert res[pair2].at[0, 'open'] - assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-1][0] // 1000 + assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-2][0] // 1000 refresh_pior = exchange._pairs_last_refresh_time[pair1] # New candle on exchange - return 100 candles - but skip one candle so we actually get 2 candles # in one go new_startdate = (start + timedelta(hours=2)).strftime('%Y-%m-%d %H:%M') - # mocker.patch("freqtrade.exchange.Exchange.ohlcv_candle_limit", return_value=100) + # mocker.patch(f"{EXMS}.ohlcv_candle_limit", return_value=100) ohlcv = generate_test_data_raw('1h', 100, new_startdate) exchange._api_async.fetch_ohlcv = get_mock_coro(ohlcv) res = exchange.refresh_latest_ohlcv(pairs) @@ -2202,8 +2222,8 @@ def test_refresh_latest_ohlcv_cache(mocker, default_conf, candle_type, time_mach assert res[pair2].at[0, 'open'] assert refresh_pior != exchange._pairs_last_refresh_time[pair1] - assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-1][0] // 1000 - assert exchange._pairs_last_refresh_time[pair2] == ohlcv[-1][0] // 1000 + assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-2][0] // 1000 + assert exchange._pairs_last_refresh_time[pair2] == ohlcv[-2][0] // 1000 exchange._api_async.fetch_ohlcv.reset_mock() # Retry same call - from cache @@ -2290,8 +2310,8 @@ async def test__async_kucoin_get_candle_history(default_conf, mocker, caplog): "kucoin GET https://openapi-v2.kucoin.com/api/v1/market/candles?" "symbol=ETH-BTC&type=5min&startAt=1640268735&endAt=1640418735" "429 Too Many Requests" '{"code":"429000","msg":"Too Many Requests"}')) - exchange = get_patched_exchange(mocker, default_conf, api_mock, id="KuCoin") - mocker.patch('freqtrade.exchange.Exchange.name', PropertyMock(return_value='KuCoin')) + exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kucoin") + mocker.patch(f'{EXMS}.name', PropertyMock(return_value='KuCoin')) msg = "Kucoin 429 error, avoid triggering DDosProtection backoff delay" assert not num_log_has_re(msg, caplog) @@ -2449,8 +2469,7 @@ def test_get_entry_rate(mocker, default_conf, caplog, side, ask, bid, default_conf['entry_pricing']['price_last_balance'] = last_ab default_conf['entry_pricing']['price_side'] = side exchange = get_patched_exchange(mocker, default_conf) - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', - return_value={'ask': ask, 'last': last, 'bid': bid}) + mocker.patch(f'{EXMS}.fetch_ticker', return_value={'ask': ask, 'last': last, 'bid': bid}) assert exchange.get_rate('ETH/BTC', side="entry", is_short=False, refresh=True) == expected assert not log_has("Using cached entry rate for ETH/BTC.", caplog) @@ -2471,8 +2490,7 @@ def test_get_exit_rate(default_conf, mocker, caplog, side, bid, ask, default_conf['exit_pricing']['price_side'] = side if last_ab is not None: default_conf['exit_pricing']['price_last_balance'] = last_ab - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', - return_value={'ask': ask, 'bid': bid, 'last': last}) + mocker.patch(f'{EXMS}.fetch_ticker', return_value={'ask': ask, 'bid': bid, 'last': last}) pair = "ETH/BTC" # Test regular mode @@ -2505,8 +2523,7 @@ def test_get_ticker_rate_error(mocker, entry, default_conf, caplog, side, is_sho default_conf['exit_pricing']['price_side'] = side default_conf['exit_pricing']['price_last_balance'] = last_ab exchange = get_patched_exchange(mocker, default_conf) - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', - return_value={'ask': ask, 'last': last, 'bid': bid}) + mocker.patch(f'{EXMS}.fetch_ticker', return_value={'ask': ask, 'last': last, 'bid': bid}) with pytest.raises(PricingError): exchange.get_rate('ETH/BTC', refresh=True, side=entry, is_short=is_short) @@ -2530,7 +2547,7 @@ def test_get_exit_rate_orderbook( default_conf['exit_pricing']['use_order_book'] = True default_conf['exit_pricing']['order_book_top'] = 1 pair = "ETH/BTC" - mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2) + mocker.patch(f'{EXMS}.fetch_l2_order_book', order_book_l2) exchange = get_patched_exchange(mocker, default_conf) rate = exchange.get_rate(pair, refresh=True, side="exit", is_short=is_short) assert not log_has("Using cached exit rate for ETH/BTC.", caplog) @@ -2548,8 +2565,7 @@ def test_get_exit_rate_orderbook_exception(default_conf, mocker, caplog): default_conf['exit_pricing']['order_book_top'] = 1 pair = "ETH/BTC" # Test What happens if the exchange returns an empty orderbook. - mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', - return_value={'bids': [[]], 'asks': [[]]}) + mocker.patch(f'{EXMS}.fetch_l2_order_book', return_value={'bids': [[]], 'asks': [[]]}) exchange = get_patched_exchange(mocker, default_conf) with pytest.raises(PricingError): exchange.get_rate(pair, refresh=True, side="exit", is_short=False) @@ -2563,8 +2579,7 @@ def test_get_exit_rate_exception(default_conf, mocker, is_short): # Ticker on one side can be empty in certain circumstances. default_conf['exit_pricing']['price_side'] = 'ask' pair = "ETH/BTC" - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', - return_value={'ask': None, 'bid': 0.12, 'last': None}) + mocker.patch(f'{EXMS}.fetch_ticker', return_value={'ask': None, 'bid': 0.12, 'last': None}) exchange = get_patched_exchange(mocker, default_conf) with pytest.raises(PricingError, match=r"Exit-Rate for ETH/BTC was empty."): exchange.get_rate(pair, refresh=True, side="exit", is_short=is_short) @@ -2572,8 +2587,7 @@ def test_get_exit_rate_exception(default_conf, mocker, is_short): exchange._config['exit_pricing']['price_side'] = 'bid' assert exchange.get_rate(pair, refresh=True, side="exit", is_short=is_short) == 0.12 # Reverse sides - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', - return_value={'ask': 0.13, 'bid': None, 'last': None}) + mocker.patch(f'{EXMS}.fetch_ticker', return_value={'ask': 0.13, 'bid': None, 'last': None}) with pytest.raises(PricingError, match=r"Exit-Rate for ETH/BTC was empty."): exchange.get_rate(pair, refresh=True, side="exit", is_short=is_short) @@ -2939,7 +2953,7 @@ async def test__async_get_trade_history_time_empty(default_conf, mocker, caplog, @pytest.mark.parametrize("exchange_name", EXCHANGES) def test_get_historic_trades(default_conf, mocker, caplog, exchange_name, trades_history): - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) pair = 'ETH/BTC' @@ -2961,7 +2975,7 @@ def test_get_historic_trades(default_conf, mocker, caplog, exchange_name, trades @pytest.mark.parametrize("exchange_name", EXCHANGES) def test_get_historic_trades_notsupported(default_conf, mocker, caplog, exchange_name, trades_history): - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=False) + mocker.patch(f'{EXMS}.exchange_has', return_value=False) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) pair = 'ETH/BTC' @@ -2977,7 +2991,7 @@ def test_get_historic_trades_notsupported(default_conf, mocker, caplog, exchange def test_cancel_order_dry_run(default_conf, mocker, exchange_name): default_conf['dry_run'] = True exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=True) + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=True) assert exchange.cancel_order(order_id='123', pair='TKN/BTC') == {} assert exchange.cancel_stoploss_order(order_id='123', pair='TKN/BTC') == {} @@ -3105,33 +3119,33 @@ def test_cancel_stoploss_order(default_conf, mocker, exchange_name): @pytest.mark.parametrize("exchange_name", EXCHANGES) def test_cancel_stoploss_order_with_result(default_conf, mocker, exchange_name): default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', return_value={'for': 123}) - mocker.patch('freqtrade.exchange.Gateio.fetch_stoploss_order', return_value={'for': 123}) + mocker.patch(f'{EXMS}.fetch_stoploss_order', return_value={'for': 123}) + mocker.patch('freqtrade.exchange.gate.Gate.fetch_stoploss_order', return_value={'for': 123}) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) res = {'fee': {}, 'status': 'canceled', 'amount': 1234} - mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order', return_value=res) - mocker.patch('freqtrade.exchange.Gateio.cancel_stoploss_order', return_value=res) + mocker.patch(f'{EXMS}.cancel_stoploss_order', return_value=res) + mocker.patch('freqtrade.exchange.gate.Gate.cancel_stoploss_order', return_value=res) co = exchange.cancel_stoploss_order_with_result(order_id='_', pair='TKN/BTC', amount=555) assert co == res - mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order', return_value='canceled') - mocker.patch('freqtrade.exchange.Gateio.cancel_stoploss_order', return_value='canceled') + mocker.patch(f'{EXMS}.cancel_stoploss_order', return_value='canceled') + mocker.patch('freqtrade.exchange.gate.Gate.cancel_stoploss_order', return_value='canceled') # Fall back to fetch_stoploss_order co = exchange.cancel_stoploss_order_with_result(order_id='_', pair='TKN/BTC', amount=555) assert co == {'for': 123} exc = InvalidOrderException("") - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', side_effect=exc) - mocker.patch('freqtrade.exchange.Gateio.fetch_stoploss_order', side_effect=exc) + mocker.patch(f'{EXMS}.fetch_stoploss_order', side_effect=exc) + mocker.patch('freqtrade.exchange.gate.Gate.fetch_stoploss_order', side_effect=exc) co = exchange.cancel_stoploss_order_with_result(order_id='_', pair='TKN/BTC', amount=555) assert co['amount'] == 555 assert co == {'fee': {}, 'status': 'canceled', 'amount': 555, 'info': {}} with pytest.raises(InvalidOrderException): exc = InvalidOrderException("Did not find order") - mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order', side_effect=exc) - mocker.patch('freqtrade.exchange.Gateio.cancel_stoploss_order', side_effect=exc) + mocker.patch(f'{EXMS}.cancel_stoploss_order', side_effect=exc) + mocker.patch('freqtrade.exchange.gate.Gate.cancel_stoploss_order', side_effect=exc) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) exchange.cancel_stoploss_order_with_result(order_id='_', pair='TKN/BTC', amount=123) @@ -3224,7 +3238,7 @@ def test_fetch_order_or_stoploss_order(default_conf, mocker): exchange = get_patched_exchange(mocker, default_conf, id='binance') fetch_order_mock = MagicMock() fetch_stoploss_order_mock = MagicMock() - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, fetch_order=fetch_order_mock, fetch_stoploss_order=fetch_stoploss_order_mock, ) @@ -3264,7 +3278,7 @@ def test_get_trades_for_order(default_conf, mocker, exchange_name, trading_mode, default_conf["dry_run"] = False default_conf["trading_mode"] = trading_mode default_conf["margin_mode"] = 'isolated' - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) api_mock = MagicMock() api_mock.fetch_my_trades = MagicMock(return_value=[{'id': 'TTR67E-3PFBD-76IISV', @@ -3307,7 +3321,7 @@ def test_get_trades_for_order(default_conf, mocker, exchange_name, trading_mode, 'get_trades_for_order', 'fetch_my_trades', order_id=order_id, pair='ETH/USDT:USDT', since=since) - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=False)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=False)) assert exchange.get_trades_for_order(order_id, 'ETH/USDT:USDT', since) == [] @@ -3339,7 +3353,7 @@ def test_get_fee(default_conf, mocker, exchange_name): def test_stoploss_order_unsupported_exchange(default_conf, mocker): exchange = get_patched_exchange(mocker, default_conf, id='bittrex') with pytest.raises(OperationalException, match=r"stoploss is not implemented .*"): - exchange.stoploss( + exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, @@ -3353,7 +3367,7 @@ def test_stoploss_order_unsupported_exchange(default_conf, mocker): def test_merge_ft_has_dict(default_conf, mocker): - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, _init_ccxt=MagicMock(return_value=MagicMock()), _load_async_markets=MagicMock(), validate_pairs=MagicMock(), @@ -3373,7 +3387,7 @@ def test_merge_ft_has_dict(default_conf, mocker): ex = Binance(default_conf) assert ex._ft_has != Exchange._ft_has_default assert ex.get_option('stoploss_on_exchange') - assert ex.get_option('order_time_in_force') == ['GTC', 'FOK', 'IOC'] + assert ex.get_option('order_time_in_force') == ['GTC', 'FOK', 'IOC', 'PO'] assert ex.get_option('trades_pagination') == 'id' assert ex.get_option('trades_pagination_arg') == 'fromId' @@ -3388,7 +3402,7 @@ def test_merge_ft_has_dict(default_conf, mocker): def test_get_valid_pair_combination(default_conf, mocker, markets): - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, _init_ccxt=MagicMock(return_value=MagicMock()), _load_async_markets=MagicMock(), validate_pairs=MagicMock(), @@ -3480,7 +3494,7 @@ def test_get_markets(default_conf, mocker, markets_static, spot_only, futures_only, expected_keys, test_comment # Here for debugging purposes (Not used within method) ): - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, _init_ccxt=MagicMock(return_value=MagicMock()), _load_async_markets=MagicMock(), validate_pairs=MagicMock(), @@ -3499,7 +3513,7 @@ def test_get_markets(default_conf, mocker, markets_static, def test_get_markets_error(default_conf, mocker): ex = get_patched_exchange(mocker, default_conf) - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=None)) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=None)) with pytest.raises(OperationalException, match="Markets were not loaded."): ex.get_markets('LTC', 'USDT', True, False) @@ -3644,7 +3658,7 @@ def test_market_is_tradable( quote, spot, margin, futures, trademode, add_dict, exchange, expected_result ) -> None: default_conf['trading_mode'] = trademode - mocker.patch('freqtrade.exchange.exchange.Exchange.validate_trading_mode_and_margin_mode') + mocker.patch(f'{EXMS}.validate_trading_mode_and_margin_mode') ex = get_patched_exchange(mocker, default_conf, id=exchange) market = { 'symbol': market_symbol, @@ -3689,7 +3703,7 @@ def test_order_has_fee(order, expected) -> None: (0.34, 'USDT', 0.01)), ]) def test_extract_cost_curr_rate(mocker, default_conf, order, expected) -> None: - mocker.patch('freqtrade.exchange.Exchange.calculate_fee_rate', MagicMock(return_value=0.01)) + mocker.patch(f'{EXMS}.calculate_fee_rate', MagicMock(return_value=0.01)) ex = get_patched_exchange(mocker, default_conf) assert ex.extract_cost_curr_rate(order['fee'], order['symbol'], cost=20, amount=1) == expected @@ -3734,7 +3748,7 @@ def test_extract_cost_curr_rate(mocker, default_conf, order, expected) -> None: 'fee': {'currency': None, 'cost': 0.005}}, None, None), ]) def test_calculate_fee_rate(mocker, default_conf, order, expected, unknown_fee_rate) -> None: - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', return_value={'last': 0.081}) + mocker.patch(f'{EXMS}.fetch_ticker', return_value={'last': 0.081}) if unknown_fee_rate: default_conf['exchange']['unknown_fee_rate'] = unknown_fee_rate @@ -3806,7 +3820,7 @@ def test__get_funding_fees_from_exchange(default_conf, mocker, exchange_name): ]) type(api_mock).has = PropertyMock(return_value={'fetchFundingHistory': True}) - # mocker.patch('freqtrade.exchange.Exchange.get_funding_fees', lambda pair, since: y) + # mocker.patch(f'{EXMS}.get_funding_fees', lambda pair, since: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) date_time = datetime.strptime("2021-09-01T00:00:01.000Z", '%Y-%m-%dT%H:%M:%S.%fZ') unix_time = int(date_time.timestamp()) @@ -3854,29 +3868,6 @@ def test_get_stake_amount_considering_leverage( stake_amount, leverage) == min_stake_with_lev -@pytest.mark.parametrize("exchange_name,trading_mode", [ - ("binance", TradingMode.FUTURES), -]) -def test__set_leverage(mocker, default_conf, exchange_name, trading_mode): - - api_mock = MagicMock() - api_mock.set_leverage = MagicMock() - type(api_mock).has = PropertyMock(return_value={'setLeverage': True}) - default_conf['dry_run'] = False - - ccxt_exceptionhandlers( - mocker, - default_conf, - api_mock, - exchange_name, - "_set_leverage", - "set_leverage", - pair="XRP/USDT", - leverage=5.0, - trading_mode=trading_mode - ) - - @pytest.mark.parametrize("margin_mode", [ (MarginMode.CROSS), (MarginMode.ISOLATED) @@ -3911,14 +3902,14 @@ def test_set_margin_mode(mocker, default_conf, margin_mode): ("bittrex", TradingMode.MARGIN, MarginMode.ISOLATED, True), ("bittrex", TradingMode.FUTURES, MarginMode.CROSS, True), ("bittrex", TradingMode.FUTURES, MarginMode.ISOLATED, True), - ("gateio", TradingMode.MARGIN, MarginMode.ISOLATED, True), + ("gate", TradingMode.MARGIN, MarginMode.ISOLATED, True), ("okx", TradingMode.SPOT, None, False), ("okx", TradingMode.MARGIN, MarginMode.CROSS, True), ("okx", TradingMode.MARGIN, MarginMode.ISOLATED, True), ("okx", TradingMode.FUTURES, MarginMode.CROSS, True), ("binance", TradingMode.FUTURES, MarginMode.ISOLATED, False), - ("gateio", TradingMode.FUTURES, MarginMode.ISOLATED, False), + ("gate", TradingMode.FUTURES, MarginMode.ISOLATED, False), ("okx", TradingMode.FUTURES, MarginMode.ISOLATED, False), # * Remove once implemented @@ -3926,16 +3917,16 @@ def test_set_margin_mode(mocker, default_conf, margin_mode): ("binance", TradingMode.FUTURES, MarginMode.CROSS, True), ("kraken", TradingMode.MARGIN, MarginMode.CROSS, True), ("kraken", TradingMode.FUTURES, MarginMode.CROSS, True), - ("gateio", TradingMode.MARGIN, MarginMode.CROSS, True), - ("gateio", TradingMode.FUTURES, MarginMode.CROSS, True), + ("gate", TradingMode.MARGIN, MarginMode.CROSS, True), + ("gate", TradingMode.FUTURES, MarginMode.CROSS, True), # * Uncomment once implemented # ("binance", TradingMode.MARGIN, MarginMode.CROSS, False), # ("binance", TradingMode.FUTURES, MarginMode.CROSS, False), # ("kraken", TradingMode.MARGIN, MarginMode.CROSS, False), # ("kraken", TradingMode.FUTURES, MarginMode.CROSS, False), - # ("gateio", TradingMode.MARGIN, MarginMode.CROSS, False), - # ("gateio", TradingMode.FUTURES, MarginMode.CROSS, False), + # ("gate", TradingMode.MARGIN, MarginMode.CROSS, False), + # ("gate", TradingMode.FUTURES, MarginMode.CROSS, False), ]) def test_validate_trading_mode_and_margin_mode( default_conf, @@ -3959,8 +3950,8 @@ def test_validate_trading_mode_and_margin_mode( ("binance", "margin", {"options": {"defaultType": "margin"}}), ("binance", "futures", {"options": {"defaultType": "swap"}}), ("bybit", "spot", {"options": {"defaultType": "spot"}}), - ("bybit", "futures", {"options": {"defaultType": "linear"}}), - ("gateio", "futures", {"options": {"defaultType": "swap"}}), + ("bybit", "futures", {"options": {"defaultType": "swap"}}), + ("gate", "futures", {"options": {"defaultType": "swap"}}), ("hitbtc", "futures", {"options": {"defaultType": "swap"}}), ("kraken", "futures", {"options": {"defaultType": "swap"}}), ("kucoin", "futures", {"options": {"defaultType": "swap"}}), @@ -3991,7 +3982,7 @@ def test_get_max_leverage_from_margin(default_conf, mocker, pair, nominal_value, default_conf['margin_mode'] = 'isolated' api_mock = MagicMock() type(api_mock).has = PropertyMock(return_value={'fetchLeverageTiers': False}) - exchange = get_patched_exchange(mocker, default_conf, api_mock, id="gateio") + exchange = get_patched_exchange(mocker, default_conf, api_mock, id="gate") assert exchange.get_max_leverage(pair, nominal_value) == max_lev @@ -4136,10 +4127,10 @@ def test_combine_funding_and_mark( # ('kraken', "2021-09-01 00:00:00", "2021-09-01 07:59:59", 30.0, -0.0012443999999999999), # ('kraken', "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, 0.0045759), # ('kraken', "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0008289), - ('gateio', 0, 2, "2021-09-01 00:10:00", "2021-09-01 04:00:00", 30.0, 0.0), - ('gateio', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.0009140999), - ('gateio', 0, 2, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, -0.0009140999), - ('gateio', 1, 2, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0002493), + ('gate', 0, 2, "2021-09-01 00:10:00", "2021-09-01 04:00:00", 30.0, 0.0), + ('gate', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.0009140999), + ('gate', 0, 2, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, -0.0009140999), + ('gate', 1, 2, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0002493), ('binance', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 50.0, -0.0015235), # TODO: Uncoment once _calculate_funding_fees can pas time_in_ratio to exchange._get_funding_fee # ('kraken', "2021-09-01 00:00:00", "2021-09-01 08:00:00", 50.0, -0.0024895), @@ -4197,7 +4188,7 @@ def test__fetch_and_calculate_funding_fees( d2 = datetime.strptime(f"{d2} +0000", '%Y-%m-%d %H:%M:%S %z') funding_rate_history = { 'binance': funding_rate_history_octohourly, - 'gateio': funding_rate_history_octohourly, + 'gate': funding_rate_history_octohourly, }[exchange][rate_start:rate_end] api_mock = MagicMock() api_mock.fetch_funding_rate_history = get_mock_coro(return_value=funding_rate_history) @@ -4206,8 +4197,7 @@ def test__fetch_and_calculate_funding_fees( type(api_mock).has = PropertyMock(return_value={'fetchFundingRateHistory': True}) ex = get_patched_exchange(mocker, default_conf, api_mock, id=exchange) - mocker.patch('freqtrade.exchange.Exchange.timeframes', PropertyMock( - return_value=['1h', '4h', '8h'])) + mocker.patch(f'{EXMS}.timeframes', PropertyMock(return_value=['1h', '4h', '8h'])) funding_fees = ex._fetch_and_calculate_funding_fees( pair='ADA/USDT', amount=amount, is_short=True, open_date=d1, close_date=d2) assert pytest.approx(funding_fees) == expected_fees @@ -4217,7 +4207,7 @@ def test__fetch_and_calculate_funding_fees( assert pytest.approx(funding_fees) == -expected_fees # Return empty "refresh_latest" - mocker.patch("freqtrade.exchange.Exchange.refresh_latest_ohlcv", return_value={}) + mocker.patch(f"{EXMS}.refresh_latest_ohlcv", return_value={}) ex = get_patched_exchange(mocker, default_conf, api_mock, id=exchange) with pytest.raises(ExchangeError, match="Could not find funding rates."): ex._fetch_and_calculate_funding_fees( @@ -4226,7 +4216,7 @@ def test__fetch_and_calculate_funding_fees( @pytest.mark.parametrize('exchange,expected_fees', [ ('binance', -0.0009140999999999999), - ('gateio', -0.0009140999999999999), + ('gate', -0.0009140999999999999), ]) def test__fetch_and_calculate_funding_fees_datetime_called( mocker, @@ -4243,7 +4233,7 @@ def test__fetch_and_calculate_funding_fees_datetime_called( return_value=funding_rate_history_octohourly) type(api_mock).has = PropertyMock(return_value={'fetchOHLCV': True}) type(api_mock).has = PropertyMock(return_value={'fetchFundingRateHistory': True}) - mocker.patch('freqtrade.exchange.Exchange.timeframes', PropertyMock(return_value=['4h', '8h'])) + mocker.patch(f'{EXMS}.timeframes', PropertyMock(return_value=['4h', '8h'])) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange) d1 = datetime.strptime("2021-09-01 00:00:00 +0000", '%Y-%m-%d %H:%M:%S %z') @@ -4266,7 +4256,7 @@ def test__get_contract_size(mocker, default_conf, pair, expected_size, trading_m default_conf['trading_mode'] = trading_mode default_conf['margin_mode'] = 'isolated' exchange = get_patched_exchange(mocker, default_conf, api_mock) - mocker.patch('freqtrade.exchange.Exchange.markets', { + mocker.patch(f'{EXMS}.markets', { 'LTC/USD': { 'symbol': 'LTC/USD', 'contractSize': None, @@ -4302,7 +4292,7 @@ def test__order_contracts_to_amount( api_mock = MagicMock() default_conf['trading_mode'] = trading_mode default_conf['margin_mode'] = 'isolated' - mocker.patch('freqtrade.exchange.Exchange.markets', markets) + mocker.patch(f'{EXMS}.markets', markets) exchange = get_patched_exchange(mocker, default_conf, api_mock) orders = [ @@ -4367,7 +4357,7 @@ def test__order_contracts_to_amount( 'info': {}, }, { - # Realistic stoploss order on gateio. + # Realistic stoploss order on gate. 'id': '123456380', 'clientOrderId': '12345638203', 'timestamp': None, @@ -4424,7 +4414,7 @@ def test__trades_contracts_to_amount( api_mock = MagicMock() default_conf['trading_mode'] = trading_mode default_conf['margin_mode'] = 'isolated' - mocker.patch('freqtrade.exchange.Exchange.markets', markets) + mocker.patch(f'{EXMS}.markets', markets) exchange = get_patched_exchange(mocker, default_conf, api_mock) trades = [ @@ -4460,7 +4450,7 @@ def test__amount_to_contracts( default_conf['trading_mode'] = 'spot' default_conf['margin_mode'] = 'isolated' exchange = get_patched_exchange(mocker, default_conf, api_mock) - mocker.patch('freqtrade.exchange.Exchange.markets', { + mocker.patch(f'{EXMS}.markets', { 'LTC/USD': { 'symbol': 'LTC/USD', 'contractSize': None, @@ -4566,6 +4556,7 @@ def test_liquidation_price_is_none( is_short=is_short, amount=71200.81144, stake_amount=open_rate * 71200.81144, + leverage=5, wallet_balance=-56354.57, mm_ex_1=0.10, upnl_ex_1=0.0 @@ -4586,7 +4577,7 @@ def test_liquidation_price_is_none( ("binance", False, 'futures', 'cross', 1535443.01, 356512.508, -448192.89, 16300.000, 109.488, 32481.980, 0.025, 26316.89) ]) -def test_liquidation_price( +def test_liquidation_price_binance( mocker, default_conf, exchange_name, open_rate, is_short, trading_mode, margin_mode, wallet_balance, mm_ex_1, upnl_ex_1, maintenance_amt, amount, mm_ratio, expected ): @@ -4604,6 +4595,7 @@ def test_liquidation_price( upnl_ex_1=upnl_ex_1, amount=amount, stake_amount=open_rate * amount, + leverage=5, ), 2)) == expected @@ -4716,7 +4708,7 @@ def test_get_max_pair_stake_amount( }, } - mocker.patch('freqtrade.exchange.Exchange.markets', markets) + mocker.patch(f'{EXMS}.markets', markets) assert exchange.get_max_pair_stake_amount('XRP/USDT:USDT', 2.0) == 20000 assert exchange.get_max_pair_stake_amount('XRP/USDT:USDT', 2.0, 5) == 4000 assert exchange.get_max_pair_stake_amount('LTC/USDT:USDT', 2.0) == float('inf') @@ -4726,7 +4718,7 @@ def test_get_max_pair_stake_amount( default_conf['trading_mode'] = 'spot' exchange = get_patched_exchange(mocker, default_conf, api_mock) - mocker.patch('freqtrade.exchange.Exchange.markets', markets) + mocker.patch(f'{EXMS}.markets', markets) assert exchange.get_max_pair_stake_amount('BTC/USDT', 2.0) == 20000 assert exchange.get_max_pair_stake_amount('ADA/USDT', 2.0) == 500 @@ -4737,7 +4729,7 @@ def test_load_leverage_tiers(mocker, default_conf, leverage_tiers, exchange_name api_mock.fetch_leverage_tiers = MagicMock() type(api_mock).has = PropertyMock(return_value={'fetchLeverageTiers': True}) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.exchange.Exchange.validate_trading_mode_and_margin_mode') + mocker.patch(f'{EXMS}.validate_trading_mode_and_margin_mode') api_mock.fetch_leverage_tiers = MagicMock(return_value={ 'ADA/USDT:USDT': [ @@ -4890,7 +4882,7 @@ def test_get_maintenance_ratio_and_amt_exceptions(mocker, default_conf, leverage api_mock = MagicMock() default_conf['trading_mode'] = 'futures' default_conf['margin_mode'] = 'isolated' - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf, api_mock) exchange._leverage_tiers = leverage_tiers @@ -4927,7 +4919,7 @@ def test_get_maintenance_ratio_and_amt( api_mock = MagicMock() default_conf['trading_mode'] = 'futures' default_conf['margin_mode'] = 'isolated' - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf, api_mock) exchange._leverage_tiers = leverage_tiers exchange.get_maintenance_ratio_and_amt(pair, value) == (mmr, maintAmt) @@ -4963,10 +4955,10 @@ def test_get_max_leverage_futures(default_conf, mocker, leverage_tiers): exchange.get_max_leverage("BTC/USDT:USDT", 1000000000.01) -@pytest.mark.parametrize("exchange_name", ['bittrex', 'binance', 'kraken', 'gateio', 'okx']) +@pytest.mark.parametrize("exchange_name", ['bittrex', 'binance', 'kraken', 'gate', 'okx', 'bybit']) def test__get_params(mocker, default_conf, exchange_name): api_mock = MagicMock() - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange._params = {'test': True} @@ -4984,6 +4976,9 @@ def test__get_params(mocker, default_conf, exchange_name): params2['tdMode'] = 'isolated' params2['posSide'] = 'net' + if exchange_name == 'bybit': + params2['position_idx'] = 0 + assert exchange._get_params( side="buy", ordertype='market', @@ -5025,6 +5020,7 @@ def test__get_params(mocker, default_conf, exchange_name): def test_get_liquidation_price1(mocker, default_conf): api_mock = MagicMock() + leverage = 9.97 positions = [ { 'info': {}, @@ -5037,7 +5033,7 @@ def test_get_liquidation_price1(mocker, default_conf): 'maintenanceMarginPercentage': 0.025, 'entryPrice': 18.884, 'notional': 15.1072, - 'leverage': 9.97, + 'leverage': leverage, 'unrealizedPnl': 0.0048, 'contracts': 8, 'contractSize': 0.1, @@ -5052,7 +5048,7 @@ def test_get_liquidation_price1(mocker, default_conf): ] api_mock.fetch_positions = MagicMock(return_value=positions) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, exchange_has=MagicMock(return_value=True), ) default_conf['dry_run'] = False @@ -5067,6 +5063,7 @@ def test_get_liquidation_price1(mocker, default_conf): is_short=False, amount=0.8, stake_amount=18.884 * 0.8, + leverage=leverage, wallet_balance=18.884 * 0.8, ) assert liq_price == 17.47 @@ -5079,6 +5076,7 @@ def test_get_liquidation_price1(mocker, default_conf): is_short=False, amount=0.8, stake_amount=18.884 * 0.8, + leverage=leverage, wallet_balance=18.884 * 0.8, ) assert liq_price == 17.540699999999998 @@ -5091,6 +5089,7 @@ def test_get_liquidation_price1(mocker, default_conf): is_short=False, amount=0.8, stake_amount=18.884 * 0.8, + leverage=leverage, wallet_balance=18.884 * 0.8, ) assert liq_price is None @@ -5104,17 +5103,18 @@ def test_get_liquidation_price1(mocker, default_conf): is_short=False, amount=0.8, stake_amount=18.884 * 0.8, + leverage=leverage, wallet_balance=18.884 * 0.8, ) -@pytest.mark.parametrize('liquidation_buffer', [0.0, 0.05]) +@pytest.mark.parametrize('liquidation_buffer', [0.0]) @pytest.mark.parametrize( "is_short,trading_mode,exchange_name,margin_mode,leverage,open_rate,amount,expected_liq", [ (False, 'spot', 'binance', '', 5.0, 10.0, 1.0, None), (True, 'spot', 'binance', '', 5.0, 10.0, 1.0, None), - (False, 'spot', 'gateio', '', 5.0, 10.0, 1.0, None), - (True, 'spot', 'gateio', '', 5.0, 10.0, 1.0, None), + (False, 'spot', 'gate', '', 5.0, 10.0, 1.0, None), + (True, 'spot', 'gate', '', 5.0, 10.0, 1.0, None), (False, 'spot', 'okx', '', 5.0, 10.0, 1.0, None), (True, 'spot', 'okx', '', 5.0, 10.0, 1.0, None), # Binance, short @@ -5127,16 +5127,26 @@ def test_get_liquidation_price1(mocker, default_conf): (False, 'futures', 'binance', 'isolated', 5, 8, 1.0, 6.454545454545454), (False, 'futures', 'binance', 'isolated', 3, 10, 1.0, 6.723905723905723), (False, 'futures', 'binance', 'isolated', 5, 10, 0.6, 8.063973063973064), - # Gateio/okx, short - (True, 'futures', 'gateio', 'isolated', 5, 10, 1.0, 11.87413417771621), - (True, 'futures', 'gateio', 'isolated', 5, 10, 2.0, 11.87413417771621), - (True, 'futures', 'gateio', 'isolated', 3, 10, 1.0, 13.193482419684678), - (True, 'futures', 'gateio', 'isolated', 5, 8, 1.0, 9.499307342172967), + # Gate/okx, short + (True, 'futures', 'gate', 'isolated', 5, 10, 1.0, 11.87413417771621), + (True, 'futures', 'gate', 'isolated', 5, 10, 2.0, 11.87413417771621), + (True, 'futures', 'gate', 'isolated', 3, 10, 1.0, 13.193482419684678), + (True, 'futures', 'gate', 'isolated', 5, 8, 1.0, 9.499307342172967), (True, 'futures', 'okx', 'isolated', 3, 10, 1.0, 13.193482419684678), - # Gateio/okx, long - (False, 'futures', 'gateio', 'isolated', 5.0, 10.0, 1.0, 8.085708510208207), - (False, 'futures', 'gateio', 'isolated', 3.0, 10.0, 1.0, 6.738090425173506), + # Gate/okx, long + (False, 'futures', 'gate', 'isolated', 5.0, 10.0, 1.0, 8.085708510208207), + (False, 'futures', 'gate', 'isolated', 3.0, 10.0, 1.0, 6.738090425173506), (False, 'futures', 'okx', 'isolated', 3.0, 10.0, 1.0, 6.738090425173506), + # bybit, long + (False, 'futures', 'bybit', 'isolated', 1.0, 10.0, 1.0, 0.1), + (False, 'futures', 'bybit', 'isolated', 3.0, 10.0, 1.0, 6.7666666), + (False, 'futures', 'bybit', 'isolated', 5.0, 10.0, 1.0, 8.1), + (False, 'futures', 'bybit', 'isolated', 10.0, 10.0, 1.0, 9.1), + # bybit, short + (True, 'futures', 'bybit', 'isolated', 1.0, 10.0, 1.0, 19.9), + (True, 'futures', 'bybit', 'isolated', 3.0, 10.0, 1.0, 13.233333), + (True, 'futures', 'bybit', 'isolated', 5.0, 10.0, 1.0, 11.9), + (True, 'futures', 'bybit', 'isolated', 10.0, 10.0, 1.0, 10.9), ] ) def test_get_liquidation_price( @@ -5182,7 +5192,7 @@ def test_get_liquidation_price( leverage = 5, open_rate = 10, amount = 0.6 ((1.6 + 0.01) - (1 * 0.6 * 10)) / ((0.6 * 0.01) - (1 * 0.6)) = 7.39057239057239 - Gateio/Okx, Short + Gate/Okx, Short leverage = 5, open_rate = 10, amount = 1.0 (open_rate + (wallet_balance / position)) / (1 + (mm_ratio + taker_fee_rate)) (10 + (2 / 1.0)) / (1 + (0.01 + 0.0006)) = 11.87413417771621 @@ -5193,7 +5203,7 @@ def test_get_liquidation_price( leverage = 5, open_rate = 8, amount = 1.0 (8 + (1.6 / 1.0)) / (1 + (0.01 + 0.0006)) = 9.499307342172967 - Gateio/Okx, Long + Gate/Okx, Long leverage = 5, open_rate = 10, amount = 1.0 (open_rate - (wallet_balance / position)) / (1 - (mm_ratio + taker_fee_rate)) (10 - (2 / 1)) / (1 - (0.01 + 0.0006)) = 8.085708510208207 @@ -5208,7 +5218,7 @@ def test_get_liquidation_price( default_conf_usdt['trading_mode'] = trading_mode default_conf_usdt['exchange']['name'] = exchange_name default_conf_usdt['margin_mode'] = margin_mode - mocker.patch('freqtrade.exchange.Gateio.validate_ordertypes') + mocker.patch('freqtrade.exchange.gate.Gate.validate_ordertypes') exchange = get_patched_exchange(mocker, default_conf_usdt, id=exchange_name) exchange.get_maintenance_ratio_and_amt = MagicMock(return_value=(0.01, 0.01)) @@ -5222,7 +5232,7 @@ def test_get_liquidation_price( amount=amount, stake_amount=amount * open_rate / leverage, wallet_balance=amount * open_rate / leverage, - # leverage=leverage, + leverage=leverage, is_short=is_short, ) if expected_liq is None: @@ -5253,14 +5263,14 @@ def test_stoploss_contract_size(mocker, default_conf, contract_size, order_amoun 'symbol': 'ETH/BTC', }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock) exchange.get_contract_size = MagicMock(return_value=contract_size) api_mock.create_order.reset_mock() - order = exchange.stoploss( + order = exchange.create_stoploss( pair='ETH/BTC', amount=100, stop_price=220, diff --git a/tests/exchange/test_gateio.py b/tests/exchange/test_gate.py similarity index 75% rename from tests/exchange/test_gateio.py rename to tests/exchange/test_gate.py index dabdbba65..db7591a40 100644 --- a/tests/exchange/test_gateio.py +++ b/tests/exchange/test_gate.py @@ -5,22 +5,22 @@ import pytest from freqtrade.enums import MarginMode, TradingMode from freqtrade.exceptions import OperationalException -from freqtrade.exchange import Gateio +from freqtrade.exchange import Gate from freqtrade.resolvers.exchange_resolver import ExchangeResolver -from tests.conftest import get_patched_exchange +from tests.conftest import EXMS, get_patched_exchange -def test_validate_order_types_gateio(default_conf, mocker): - default_conf['exchange']['name'] = 'gateio' - mocker.patch('freqtrade.exchange.Exchange._init_ccxt') - mocker.patch('freqtrade.exchange.Exchange._load_markets', return_value={}) - mocker.patch('freqtrade.exchange.Exchange.validate_pairs') - mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') - mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') - mocker.patch('freqtrade.exchange.Exchange.validate_pricing') - mocker.patch('freqtrade.exchange.Exchange.name', 'Bittrex') - exch = ExchangeResolver.load_exchange('gateio', default_conf, True) - assert isinstance(exch, Gateio) +def test_validate_order_types_gate(default_conf, mocker): + default_conf['exchange']['name'] = 'gate' + mocker.patch(f'{EXMS}._init_ccxt') + mocker.patch(f'{EXMS}._load_markets', return_value={}) + mocker.patch(f'{EXMS}.validate_pairs') + mocker.patch(f'{EXMS}.validate_timeframes') + mocker.patch(f'{EXMS}.validate_stakecurrency') + mocker.patch(f'{EXMS}.validate_pricing') + mocker.patch(f'{EXMS}.name', 'Gate') + exch = ExchangeResolver.load_exchange('gate', default_conf, True) + assert isinstance(exch, Gate) default_conf['order_types'] = { 'entry': 'market', @@ -31,18 +31,18 @@ def test_validate_order_types_gateio(default_conf, mocker): with pytest.raises(OperationalException, match=r'Exchange .* does not support market orders.'): - ExchangeResolver.load_exchange('gateio', default_conf, True) + ExchangeResolver.load_exchange('gate', default_conf, True) # market-orders supported on futures markets. default_conf['trading_mode'] = 'futures' default_conf['margin_mode'] = 'isolated' - ex = ExchangeResolver.load_exchange('gateio', default_conf, True) + ex = ExchangeResolver.load_exchange('gate', default_conf, True) assert ex @pytest.mark.usefixtures("init_persistence") -def test_fetch_stoploss_order_gateio(default_conf, mocker): - exchange = get_patched_exchange(mocker, default_conf, id='gateio') +def test_fetch_stoploss_order_gate(default_conf, mocker): + exchange = get_patched_exchange(mocker, default_conf, id='gate') fetch_order_mock = MagicMock() exchange.fetch_order = fetch_order_mock @@ -56,7 +56,7 @@ def test_fetch_stoploss_order_gateio(default_conf, mocker): default_conf['trading_mode'] = 'futures' default_conf['margin_mode'] = 'isolated' - exchange = get_patched_exchange(mocker, default_conf, id='gateio') + exchange = get_patched_exchange(mocker, default_conf, id='gate') exchange.fetch_order = MagicMock(return_value={ 'status': 'closed', @@ -73,8 +73,8 @@ def test_fetch_stoploss_order_gateio(default_conf, mocker): assert exchange.fetch_order.call_args_list[1][1]['order_id'] == '222555' -def test_cancel_stoploss_order_gateio(default_conf, mocker): - exchange = get_patched_exchange(mocker, default_conf, id='gateio') +def test_cancel_stoploss_order_gate(default_conf, mocker): + exchange = get_patched_exchange(mocker, default_conf, id='gate') cancel_order_mock = MagicMock() exchange.cancel_order = cancel_order_mock @@ -90,8 +90,8 @@ def test_cancel_stoploss_order_gateio(default_conf, mocker): (1501, 1499, 1501, "sell"), (1499, 1501, 1499, "buy") ]) -def test_stoploss_adjust_gateio(mocker, default_conf, sl1, sl2, sl3, side): - exchange = get_patched_exchange(mocker, default_conf, id='gateio') +def test_stoploss_adjust_gate(mocker, default_conf, sl1, sl2, sl3, side): + exchange = get_patched_exchange(mocker, default_conf, id='gate') order = { 'price': 1500, 'stopPrice': 1500, @@ -104,8 +104,8 @@ def test_stoploss_adjust_gateio(mocker, default_conf, sl1, sl2, sl3, side): ('taker', 0.0005, 0.0001554325), ('maker', 0.0, 0.0), ]) -def test_fetch_my_trades_gateio(mocker, default_conf, takerormaker, rate, cost): - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) +def test_fetch_my_trades_gate(mocker, default_conf, takerormaker, rate, cost): + mocker.patch(f'{EXMS}.exchange_has', return_value=True) tick = {'ETH/USDT:USDT': { 'info': {'user_id': '', 'taker_fee': '0.0018', @@ -134,7 +134,7 @@ def test_fetch_my_trades_gateio(mocker, default_conf, takerormaker, rate, cost): 'takerOrMaker': takerormaker, 'amount': 1, # 1 contract }]) - exchange = get_patched_exchange(mocker, default_conf, api_mock=api_mock, id='gateio') + exchange = get_patched_exchange(mocker, default_conf, api_mock=api_mock, id='gate') exchange._trading_fees = tick trades = exchange.get_trades_for_order('22255', 'ETH/USDT:USDT', datetime.now(timezone.utc)) trade = trades[0] diff --git a/tests/exchange/test_huobi.py b/tests/exchange/test_huobi.py index 2ce379a47..85d2ced9d 100644 --- a/tests/exchange/test_huobi.py +++ b/tests/exchange/test_huobi.py @@ -4,8 +4,8 @@ from unittest.mock import MagicMock import ccxt import pytest -from freqtrade.exceptions import DependencyException, InvalidOrderException, OperationalException -from tests.conftest import get_patched_exchange +from freqtrade.exceptions import DependencyException, InvalidOrderException +from tests.conftest import EXMS, get_patched_exchange from tests.exchange.test_exchange import ccxt_exceptionhandlers @@ -14,7 +14,7 @@ from tests.exchange.test_exchange import ccxt_exceptionhandlers (0.99, 220 * 0.99, "sell"), (0.98, 220 * 0.98, "sell"), ]) -def test_stoploss_order_huobi(default_conf, mocker, limitratio, expected, side): +def test_create_stoploss_order_huobi(default_conf, mocker, limitratio, expected, side): api_mock = MagicMock() order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6)) order_type = 'stop-limit' @@ -26,21 +26,21 @@ def test_stoploss_order_huobi(default_conf, mocker, limitratio, expected, side): } }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'huobi') - with pytest.raises(OperationalException): - order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190, - order_types={'stoploss_on_exchange_limit_ratio': 1.05}, - side=side, - leverage=1.0) + with pytest.raises(InvalidOrderException): + order = exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=190, + order_types={'stoploss_on_exchange_limit_ratio': 1.05}, + side=side, + leverage=1.0) api_mock.create_order.reset_mock() order_types = {} if limitratio is None else {'stoploss_on_exchange_limit_ratio': limitratio} - order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types=order_types, - side=side, leverage=1.0) + order = exchange.create_stoploss( + pair='ETH/BTC', amount=1, stop_price=220, order_types=order_types, side=side, leverage=1.0) assert 'id' in order assert 'info' in order @@ -59,40 +59,40 @@ def test_stoploss_order_huobi(default_conf, mocker, limitratio, expected, side): with pytest.raises(DependencyException): api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance")) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'huobi') - exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, - order_types={}, side=side, leverage=1.0) + exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=220, + order_types={}, side=side, leverage=1.0) with pytest.raises(InvalidOrderException): api_mock.create_order = MagicMock( side_effect=ccxt.InvalidOrder("binance Order would trigger immediately.")) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') - exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, - order_types={}, side=side, leverage=1.0) + exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=220, + order_types={}, side=side, leverage=1.0) ccxt_exceptionhandlers(mocker, default_conf, api_mock, "huobi", - "stoploss", "create_order", retries=1, + "create_stoploss", "create_order", retries=1, pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side=side, leverage=1.0) -def test_stoploss_order_dry_run_huobi(default_conf, mocker): +def test_create_stoploss_order_dry_run_huobi(default_conf, mocker): api_mock = MagicMock() order_type = 'stop-limit' default_conf['dry_run'] = True - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'huobi') - with pytest.raises(OperationalException): - order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190, - order_types={'stoploss_on_exchange_limit_ratio': 1.05}, - side='sell', leverage=1.0) + with pytest.raises(InvalidOrderException): + order = exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=190, + order_types={'stoploss_on_exchange_limit_ratio': 1.05}, + side='sell', leverage=1.0) api_mock.create_order.reset_mock() - order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, - order_types={}, side='sell', leverage=1.0) + order = exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=220, + order_types={}, side='sell', leverage=1.0) assert 'id' in order assert 'info' in order diff --git a/tests/exchange/test_kraken.py b/tests/exchange/test_kraken.py index 66006f2fe..40a5a5b38 100644 --- a/tests/exchange/test_kraken.py +++ b/tests/exchange/test_kraken.py @@ -5,7 +5,7 @@ import ccxt import pytest from freqtrade.exceptions import DependencyException, InvalidOrderException -from tests.conftest import get_patched_exchange +from tests.conftest import EXMS, get_patched_exchange from tests.exchange.test_exchange import ccxt_exceptionhandlers @@ -28,8 +28,8 @@ def test_buy_kraken_trading_agreement(default_conf, mocker): }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken") order = exchange.create_order( @@ -68,8 +68,8 @@ def test_sell_kraken_trading_agreement(default_conf, mocker): }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken") order = exchange.create_order(pair='ETH/BTC', ordertype=order_type, @@ -179,7 +179,7 @@ def test_get_balances_prod(default_conf, mocker): ("sell", 217.8), ("buy", 222.2), ]) -def test_stoploss_order_kraken(default_conf, mocker, ordertype, side, adjustedprice): +def test_create_stoploss_order_kraken(default_conf, mocker, ordertype, side, adjustedprice): api_mock = MagicMock() order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6)) @@ -191,12 +191,12 @@ def test_stoploss_order_kraken(default_conf, mocker, ordertype, side, adjustedpr }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') - order = exchange.stoploss( + order = exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, @@ -230,7 +230,7 @@ def test_stoploss_order_kraken(default_conf, mocker, ordertype, side, adjustedpr with pytest.raises(DependencyException): api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance")) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') - exchange.stoploss( + exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, @@ -243,7 +243,7 @@ def test_stoploss_order_kraken(default_conf, mocker, ordertype, side, adjustedpr api_mock.create_order = MagicMock( side_effect=ccxt.InvalidOrder("kraken Order would trigger immediately.")) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') - exchange.stoploss( + exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, @@ -253,23 +253,23 @@ def test_stoploss_order_kraken(default_conf, mocker, ordertype, side, adjustedpr ) ccxt_exceptionhandlers(mocker, default_conf, api_mock, "kraken", - "stoploss", "create_order", retries=1, + "create_stoploss", "create_order", retries=1, pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side=side, leverage=1.0) @pytest.mark.parametrize('side', ['buy', 'sell']) -def test_stoploss_order_dry_run_kraken(default_conf, mocker, side): +def test_create_stoploss_order_dry_run_kraken(default_conf, mocker, side): api_mock = MagicMock() default_conf['dry_run'] = True - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') api_mock.create_order.reset_mock() - order = exchange.stoploss( + order = exchange.create_stoploss( pair='ETH/BTC', amount=1, stop_price=220, diff --git a/tests/exchange/test_kucoin.py b/tests/exchange/test_kucoin.py index ebaf5ae81..07f3fb6a3 100644 --- a/tests/exchange/test_kucoin.py +++ b/tests/exchange/test_kucoin.py @@ -4,8 +4,8 @@ from unittest.mock import MagicMock import ccxt import pytest -from freqtrade.exceptions import DependencyException, InvalidOrderException, OperationalException -from tests.conftest import get_patched_exchange +from freqtrade.exceptions import DependencyException, InvalidOrderException +from tests.conftest import EXMS, get_patched_exchange from tests.exchange.test_exchange import ccxt_exceptionhandlers @@ -15,7 +15,7 @@ from tests.exchange.test_exchange import ccxt_exceptionhandlers (0.99, 220 * 0.99, "sell"), (0.98, 220 * 0.98, "sell"), ]) -def test_stoploss_order_kucoin(default_conf, mocker, limitratio, expected, side, order_type): +def test_create_stoploss_order_kucoin(default_conf, mocker, limitratio, expected, side, order_type): api_mock = MagicMock() order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6)) @@ -26,24 +26,24 @@ def test_stoploss_order_kucoin(default_conf, mocker, limitratio, expected, side, } }) default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kucoin') if order_type == 'limit': - with pytest.raises(OperationalException): - order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190, - order_types={ - 'stoploss': order_type, - 'stoploss_on_exchange_limit_ratio': 1.05}, - side=side, leverage=1.0) + with pytest.raises(InvalidOrderException): + order = exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=190, + order_types={ + 'stoploss': order_type, + 'stoploss_on_exchange_limit_ratio': 1.05}, + side=side, leverage=1.0) api_mock.create_order.reset_mock() order_types = {'stoploss': order_type} if limitratio is not None: order_types.update({'stoploss_on_exchange_limit_ratio': limitratio}) - order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, - order_types=order_types, side=side, leverage=1.0) + order = exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=220, + order_types=order_types, side=side, leverage=1.0) assert 'id' in order assert 'info' in order @@ -67,18 +67,18 @@ def test_stoploss_order_kucoin(default_conf, mocker, limitratio, expected, side, with pytest.raises(DependencyException): api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance")) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kucoin') - exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, - order_types={}, side=side, leverage=1.0) + exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=220, + order_types={}, side=side, leverage=1.0) with pytest.raises(InvalidOrderException): api_mock.create_order = MagicMock( side_effect=ccxt.InvalidOrder("kucoin Order would trigger immediately.")) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kucoin') - exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, - order_types={}, side=side, leverage=1.0) + exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=220, + order_types={}, side=side, leverage=1.0) ccxt_exceptionhandlers(mocker, default_conf, api_mock, "kucoin", - "stoploss", "create_order", retries=1, + "create_stoploss", "create_order", retries=1, pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side=side, leverage=1.0) @@ -87,21 +87,21 @@ def test_stoploss_order_dry_run_kucoin(default_conf, mocker): api_mock = MagicMock() order_type = 'market' default_conf['dry_run'] = True - mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kucoin') - with pytest.raises(OperationalException): - order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190, - order_types={'stoploss': 'limit', - 'stoploss_on_exchange_limit_ratio': 1.05}, - side='sell', leverage=1.0) + with pytest.raises(InvalidOrderException): + order = exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=190, + order_types={'stoploss': 'limit', + 'stoploss_on_exchange_limit_ratio': 1.05}, + side='sell', leverage=1.0) api_mock.create_order.reset_mock() - order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, - order_types={}, side='sell', leverage=1.0) + order = exchange.create_stoploss(pair='ETH/BTC', amount=1, stop_price=220, + order_types={}, side='sell', leverage=1.0) assert 'id' in order assert 'info' in order @@ -125,3 +125,45 @@ def test_stoploss_adjust_kucoin(mocker, default_conf): # Test with invalid order case order['stopPrice'] = None assert exchange.stoploss_adjust(1501, order, 'sell') + + +@pytest.mark.parametrize("side", ["buy", "sell"]) +@pytest.mark.parametrize("ordertype,rate", [ + ("market", None), + ("market", 200), + ("limit", 200), + ("stop_loss_limit", 200) +]) +def test_kucoin_create_order(default_conf, mocker, side, ordertype, rate): + api_mock = MagicMock() + order_id = 'test_prod_{}_{}'.format(side, randint(0, 10 ** 6)) + api_mock.create_order = MagicMock(return_value={ + 'id': order_id, + 'info': { + 'foo': 'bar' + }, + 'symbol': 'XRP/USDT', + 'amount': 1 + }) + default_conf['dry_run'] = False + mocker.patch(f'{EXMS}.amount_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) + exchange = get_patched_exchange(mocker, default_conf, api_mock, id='kucoin') + exchange._set_leverage = MagicMock() + exchange.set_margin_mode = MagicMock() + + order = exchange.create_order( + pair='XRP/USDT', + ordertype=ordertype, + side=side, + amount=1, + rate=rate, + leverage=1.0 + ) + + assert 'id' in order + assert 'info' in order + assert order['id'] == order_id + assert order['amount'] == 1 + # Status must be faked to open for kucoin. + assert order['status'] == 'open' diff --git a/tests/exchange/test_okx.py b/tests/exchange/test_okx.py index 46b1852a0..7a3fa22f0 100644 --- a/tests/exchange/test_okx.py +++ b/tests/exchange/test_okx.py @@ -2,11 +2,13 @@ from datetime import datetime, timedelta, timezone from pathlib import Path from unittest.mock import MagicMock, PropertyMock +import ccxt import pytest from freqtrade.enums import CandleType, MarginMode, TradingMode +from freqtrade.exceptions import RetryableOrderError from freqtrade.exchange.exchange import timeframe_to_minutes -from tests.conftest import get_mock_coro, get_patched_exchange, log_has +from tests.conftest import EXMS, get_mock_coro, get_patched_exchange, log_has from tests.exchange.test_exchange import ccxt_exceptionhandlers @@ -46,7 +48,7 @@ def test_get_maintenance_ratio_and_amt_okx( default_conf['margin_mode'] = 'isolated' default_conf['dry_run'] = False mocker.patch.multiple( - 'freqtrade.exchange.Okx', + 'freqtrade.exchange.okx.Okx', exchange_has=MagicMock(return_value=True), load_leverage_tiers=MagicMock(return_value={ 'ETH/USDT:USDT': [ @@ -476,3 +478,116 @@ def test_load_leverage_tiers_okx(default_conf, mocker, markets, tmpdir, caplog, exchange.load_leverage_tiers() assert log_has(logmsg, caplog) + + +def test__set_leverage_okx(mocker, default_conf): + + api_mock = MagicMock() + api_mock.set_leverage = MagicMock() + type(api_mock).has = PropertyMock(return_value={'setLeverage': True}) + default_conf['dry_run'] = False + default_conf['trading_mode'] = TradingMode.FUTURES + default_conf['margin_mode'] = MarginMode.ISOLATED + + exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx") + exchange._lev_prep('BTC/USDT:USDT', 3.2, 'buy') + assert api_mock.set_leverage.call_count == 1 + # Leverage is rounded to 3. + assert api_mock.set_leverage.call_args_list[0][1]['leverage'] == 3.2 + assert api_mock.set_leverage.call_args_list[0][1]['symbol'] == 'BTC/USDT:USDT' + assert api_mock.set_leverage.call_args_list[0][1]['params'] == { + 'mgnMode': 'isolated', + 'posSide': 'net'} + + ccxt_exceptionhandlers( + mocker, + default_conf, + api_mock, + "okx", + "_lev_prep", + "set_leverage", + pair="XRP/USDT:USDT", + leverage=5.0, + side='buy' + ) + + +@pytest.mark.usefixtures("init_persistence") +def test_fetch_stoploss_order_okx(default_conf, mocker): + default_conf['dry_run'] = False + api_mock = MagicMock() + api_mock.fetch_order = MagicMock() + + exchange = get_patched_exchange(mocker, default_conf, api_mock, id='okx') + + exchange.fetch_stoploss_order('1234', 'ETH/BTC') + assert api_mock.fetch_order.call_count == 1 + assert api_mock.fetch_order.call_args_list[0][0][0] == '1234' + assert api_mock.fetch_order.call_args_list[0][0][1] == 'ETH/BTC' + assert api_mock.fetch_order.call_args_list[0][1]['params'] == {'stop': True} + + api_mock.fetch_order = MagicMock(side_effect=ccxt.OrderNotFound) + api_mock.fetch_open_orders = MagicMock(return_value=[]) + api_mock.fetch_closed_orders = MagicMock(return_value=[]) + api_mock.fetch_canceled_orders = MagicMock(creturn_value=[]) + + with pytest.raises(RetryableOrderError): + exchange.fetch_stoploss_order('1234', 'ETH/BTC') + assert api_mock.fetch_order.call_count == 1 + assert api_mock.fetch_open_orders.call_count == 1 + assert api_mock.fetch_closed_orders.call_count == 1 + assert api_mock.fetch_canceled_orders.call_count == 1 + + api_mock.fetch_order.reset_mock() + api_mock.fetch_open_orders.reset_mock() + api_mock.fetch_closed_orders.reset_mock() + api_mock.fetch_canceled_orders.reset_mock() + + api_mock.fetch_closed_orders = MagicMock(return_value=[ + { + 'id': '1234', + 'status': 'closed', + 'info': {'ordId': '123455'} + } + ]) + mocker.patch(f"{EXMS}.fetch_order", MagicMock(return_value={'id': '123455'})) + resp = exchange.fetch_stoploss_order('1234', 'ETH/BTC') + assert api_mock.fetch_order.call_count == 1 + assert api_mock.fetch_open_orders.call_count == 1 + assert api_mock.fetch_closed_orders.call_count == 1 + assert api_mock.fetch_canceled_orders.call_count == 0 + + assert resp['id'] == '1234' + assert resp['id_stop'] == '123455' + assert resp['type'] == 'stoploss' + + default_conf['dry_run'] = True + exchange = get_patched_exchange(mocker, default_conf, api_mock, id='okx') + dro_mock = mocker.patch(f"{EXMS}.fetch_dry_run_order", MagicMock(return_value={'id': '123455'})) + + api_mock.fetch_order.reset_mock() + api_mock.fetch_open_orders.reset_mock() + api_mock.fetch_closed_orders.reset_mock() + api_mock.fetch_canceled_orders.reset_mock() + resp = exchange.fetch_stoploss_order('1234', 'ETH/BTC') + + assert api_mock.fetch_order.call_count == 0 + assert api_mock.fetch_open_orders.call_count == 0 + assert api_mock.fetch_closed_orders.call_count == 0 + assert api_mock.fetch_canceled_orders.call_count == 0 + assert dro_mock.call_count == 1 + + +@pytest.mark.parametrize('sl1,sl2,sl3,side', [ + (1501, 1499, 1501, "sell"), + (1499, 1501, 1499, "buy") +]) +def test_stoploss_adjust_okx(mocker, default_conf, sl1, sl2, sl3, side): + exchange = get_patched_exchange(mocker, default_conf, id='okx') + order = { + 'type': 'stoploss', + 'price': 1500, + 'stopLossPrice': 1500, + } + assert exchange.stoploss_adjust(sl1, order, side=side) + assert not exchange.stoploss_adjust(sl2, order, side=side) diff --git a/tests/freqai/conftest.py b/tests/freqai/conftest.py index bee7df27e..e140ee80b 100644 --- a/tests/freqai/conftest.py +++ b/tests/freqai/conftest.py @@ -27,7 +27,7 @@ def freqai_conf(default_conf, tmpdir): "timerange": "20180110-20180115", "freqai": { "enabled": True, - "purge_old_models": True, + "purge_old_models": 2, "train_period_days": 2, "backtest_period_days": 10, "live_retrain_hours": 0, @@ -46,6 +46,8 @@ def freqai_conf(default_conf, tmpdir): "use_SVM_to_remove_outliers": True, "stratify_training_data": 0, "indicator_periods_candles": [10], + "shuffle_after_split": False, + "buffer_train_data_candles": 0 }, "data_split_parameters": {"test_size": 0.33, "shuffle": False}, "model_training_parameters": {"n_estimators": 100}, @@ -76,7 +78,9 @@ def make_rl_config(conf): "rr": 1, "profit_aim": 0.02, "win_reward_factor": 2 - }} + }, + "drop_ohlc_from_features": False + } return conf diff --git a/tests/freqai/test_freqai_backtesting.py b/tests/freqai/test_freqai_backtesting.py index 60963e762..0a8059966 100644 --- a/tests/freqai/test_freqai_backtesting.py +++ b/tests/freqai/test_freqai_backtesting.py @@ -35,8 +35,8 @@ def test_freqai_backtest_start_backtest_list(freqai_conf, mocker, testdatadir, c args = get_args(args) bt_config = setup_optimize_configuration(args, RunMode.BACKTEST) Backtesting(bt_config) - assert log_has_re('Using --strategy-list with FreqAI REQUIRES all strategies to have identical ' - 'populate_any_indicators.', caplog) + assert log_has_re('Using --strategy-list with FreqAI REQUIRES all strategies to have identical', + caplog) Backtesting.cleanup() diff --git a/tests/freqai/test_freqai_interface.py b/tests/freqai/test_freqai_interface.py index 4ef99720a..3b370aea4 100644 --- a/tests/freqai/test_freqai_interface.py +++ b/tests/freqai/test_freqai_interface.py @@ -1,5 +1,6 @@ import platform import shutil +import sys from pathlib import Path from unittest.mock import MagicMock @@ -13,10 +14,14 @@ from freqtrade.freqai.utils import download_all_data_for_training, get_required_ from freqtrade.optimize.backtesting import Backtesting from freqtrade.persistence import Trade from freqtrade.plugins.pairlistmanager import PairListManager -from tests.conftest import create_mock_trades, get_patched_exchange, log_has_re +from tests.conftest import EXMS, create_mock_trades, get_patched_exchange, log_has_re from tests.freqai.conftest import get_patched_freqai_strategy, make_rl_config +def is_py11() -> bool: + return sys.version_info >= (3, 11) + + def is_arm() -> bool: machine = platform.machine() return "arm" in machine or "aarch64" in machine @@ -27,25 +32,32 @@ def is_mac() -> bool: return "Darwin" in machine -@pytest.mark.parametrize('model, pca, dbscan, float32, can_short', [ - ('LightGBMRegressor', True, False, True, True), - ('XGBoostRegressor', False, True, False, True), - ('XGBoostRFRegressor', False, False, False, True), - ('CatboostRegressor', False, False, False, True), - ('ReinforcementLearner', False, True, False, True), - ('ReinforcementLearner_multiproc', False, False, False, True), - ('ReinforcementLearner_test_3ac', False, False, False, False), - ('ReinforcementLearner_test_3ac', False, False, False, True), - ('ReinforcementLearner_test_4ac', False, False, False, True) - ]) -def test_extract_data_and_train_model_Standard(mocker, freqai_conf, model, pca, - dbscan, float32, can_short): - if is_arm() and model == 'CatboostRegressor': +def can_run_model(model: str) -> None: + if (is_arm() or is_py11()) and "Catboost" in model: pytest.skip("CatBoost is not supported on ARM") if is_mac() and not is_arm() and 'Reinforcement' in model: pytest.skip("Reinforcement learning module not available on intel based Mac OS") + if is_py11() and 'Reinforcement' in model: + pytest.skip("Reinforcement learning currently not available on python 3.11.") + + +@pytest.mark.parametrize('model, pca, dbscan, float32, can_short, shuffle, buffer', [ + ('LightGBMRegressor', True, False, True, True, False, 0), + ('XGBoostRegressor', False, True, False, True, False, 10), + ('XGBoostRFRegressor', False, False, False, True, False, 0), + ('CatboostRegressor', False, False, False, True, True, 0), + ('ReinforcementLearner', False, True, False, True, False, 0), + ('ReinforcementLearner_multiproc', False, False, False, True, False, 0), + ('ReinforcementLearner_test_3ac', False, False, False, False, False, 0), + ('ReinforcementLearner_test_3ac', False, False, False, True, False, 0), + ('ReinforcementLearner_test_4ac', False, False, False, True, False, 0) + ]) +def test_extract_data_and_train_model_Standard(mocker, freqai_conf, model, pca, + dbscan, float32, can_short, shuffle, buffer): + + can_run_model(model) model_save_ext = 'joblib' freqai_conf.update({"freqaimodel": model}) freqai_conf.update({"timerange": "20180110-20180130"}) @@ -53,13 +65,8 @@ def test_extract_data_and_train_model_Standard(mocker, freqai_conf, model, pca, freqai_conf['freqai']['feature_parameters'].update({"principal_component_analysis": pca}) freqai_conf['freqai']['feature_parameters'].update({"use_DBSCAN_to_remove_outliers": dbscan}) freqai_conf.update({"reduce_df_footprint": float32}) - - if 'ReinforcementLearner' in model: - model_save_ext = 'zip' - freqai_conf = make_rl_config(freqai_conf) - # test the RL guardrails - freqai_conf['freqai']['feature_parameters'].update({"use_SVM_to_remove_outliers": True}) - freqai_conf['freqai']['data_split_parameters'].update({'shuffle': True}) + freqai_conf['freqai']['feature_parameters'].update({"shuffle_after_split": shuffle}) + freqai_conf['freqai']['feature_parameters'].update({"buffer_train_data_candles": buffer}) if 'ReinforcementLearner' in model: model_save_ext = 'zip' @@ -70,6 +77,7 @@ def test_extract_data_and_train_model_Standard(mocker, freqai_conf, model, pca, if 'test_3ac' in model or 'test_4ac' in model: freqai_conf["freqaimodel_path"] = str(Path(__file__).parents[1] / "freqai" / "test_models") + freqai_conf["freqai"]["rl_config"]["drop_ohlc_from_features"] = True strategy = get_patched_freqai_strategy(mocker, freqai_conf) exchange = get_patched_exchange(mocker, freqai_conf) @@ -114,7 +122,7 @@ def test_extract_data_and_train_model_Standard(mocker, freqai_conf, model, pca, ('CatboostClassifierMultiTarget', "freqai_test_multimodel_classifier_strat") ]) def test_extract_data_and_train_model_MultiTargets(mocker, freqai_conf, model, strat): - if is_arm() and 'Catboost' in model: + if (is_arm() or is_py11()) and 'Catboost' in model: pytest.skip("CatBoost is not supported on ARM") freqai_conf.update({"timerange": "20180110-20180130"}) @@ -156,7 +164,7 @@ def test_extract_data_and_train_model_MultiTargets(mocker, freqai_conf, model, s 'XGBoostRFClassifier', ]) def test_extract_data_and_train_model_Classifiers(mocker, freqai_conf, model): - if is_arm() and model == 'CatboostClassifier': + if (is_arm() or is_py11()) and model == 'CatboostClassifier': pytest.skip("CatBoost is not supported on ARM") freqai_conf.update({"freqaimodel": model}) @@ -203,13 +211,11 @@ def test_extract_data_and_train_model_Classifiers(mocker, freqai_conf, model): ], ) def test_start_backtesting(mocker, freqai_conf, model, num_files, strat, caplog): + can_run_model(model) + freqai_conf.get("freqai", {}).update({"save_backtest_models": True}) freqai_conf['runmode'] = RunMode.BACKTEST - if is_arm() and "Catboost" in model: - pytest.skip("CatBoost is not supported on ARM") - if is_mac() and 'Reinforcement' in model: - pytest.skip("Reinforcement learning module not available on intel based Mac OS") Trade.use_db = False freqai_conf.update({"freqaimodel": model}) @@ -376,57 +382,6 @@ def test_backtesting_fit_live_predictions(mocker, freqai_conf, caplog): shutil.rmtree(Path(freqai.dk.full_path)) -def test_follow_mode(mocker, freqai_conf): - freqai_conf.update({"timerange": "20180110-20180130"}) - - strategy = get_patched_freqai_strategy(mocker, freqai_conf) - exchange = get_patched_exchange(mocker, freqai_conf) - strategy.dp = DataProvider(freqai_conf, exchange) - strategy.freqai_info = freqai_conf.get("freqai", {}) - freqai = strategy.freqai - freqai.live = True - freqai.dk = FreqaiDataKitchen(freqai_conf) - timerange = TimeRange.parse_timerange("20180110-20180130") - freqai.dd.load_all_pair_histories(timerange, freqai.dk) - - metadata = {"pair": "ADA/BTC"} - freqai.dd.set_pair_dict_info(metadata) - - data_load_timerange = TimeRange.parse_timerange("20180110-20180130") - new_timerange = TimeRange.parse_timerange("20180120-20180130") - - freqai.extract_data_and_train_model( - new_timerange, "ADA/BTC", strategy, freqai.dk, data_load_timerange) - - assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_model.joblib").is_file() - assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_metadata.json").is_file() - assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_trained_df.pkl").is_file() - assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_svm_model.joblib").is_file() - - # start the follower and ask it to predict on existing files - - freqai_conf.get("freqai", {}).update({"follow_mode": "true"}) - - strategy = get_patched_freqai_strategy(mocker, freqai_conf) - exchange = get_patched_exchange(mocker, freqai_conf) - strategy.dp = DataProvider(freqai_conf, exchange) - strategy.freqai_info = freqai_conf.get("freqai", {}) - freqai = strategy.freqai - freqai.live = True - freqai.dk = FreqaiDataKitchen(freqai_conf, freqai.live) - timerange = TimeRange.parse_timerange("20180110-20180130") - freqai.dd.load_all_pair_histories(timerange, freqai.dk) - - df = strategy.dp.get_pair_dataframe('ADA/BTC', '5m') - - freqai.dk.pair = "ADA/BTC" - freqai.start_live(df, metadata, strategy, freqai.dk) - - assert len(freqai.dk.return_dataframe.index) == 5702 - - shutil.rmtree(Path(freqai.dk.full_path)) - - def test_principal_component_analysis(mocker, freqai_conf): freqai_conf.update({"timerange": "20180110-20180130"}) freqai_conf.get("freqai", {}).get("feature_parameters", {}).update( @@ -557,6 +512,8 @@ def test_get_state_info(mocker, freqai_conf, dp_exists, caplog, tickers): if is_mac(): pytest.skip("Reinforcement learning module not available on intel based Mac OS") + if is_py11(): + pytest.skip("Reinforcement learning currently not available on python 3.11.") freqai_conf.update({"freqaimodel": "ReinforcementLearner"}) freqai_conf.update({"timerange": "20180110-20180130"}) @@ -568,7 +525,7 @@ def test_get_state_info(mocker, freqai_conf, dp_exists, caplog, tickers): strategy = get_patched_freqai_strategy(mocker, freqai_conf) exchange = get_patched_exchange(mocker, freqai_conf) ticker_mock = MagicMock(return_value=tickers()['ETH/BTC']) - mocker.patch("freqtrade.exchange.Exchange.fetch_ticker", ticker_mock) + mocker.patch(f"{EXMS}.fetch_ticker", ticker_mock) strategy.dp = DataProvider(freqai_conf, exchange) if not dp_exists: diff --git a/tests/optimize/test_backtest_detail.py b/tests/optimize/test_backtest_detail.py index a18196507..2cb42c003 100644 --- a/tests/optimize/test_backtest_detail.py +++ b/tests/optimize/test_backtest_detail.py @@ -8,7 +8,7 @@ from freqtrade.data.history import get_timerange from freqtrade.enums import ExitType from freqtrade.optimize.backtesting import Backtesting from freqtrade.persistence.trade_model import LocalTrade -from tests.conftest import patch_exchange +from tests.conftest import EXMS, patch_exchange from tests.optimize import (BTContainer, BTrade, _build_backtest_dataframe, _get_frame_time_from_offset, tests_timeframe) @@ -919,11 +919,12 @@ def test_backtest_results(default_conf, fee, mocker, caplog, data: BTContainer) default_conf["trailing_stop_positive"] = data.trailing_stop_positive default_conf["trailing_stop_positive_offset"] = data.trailing_stop_positive_offset default_conf["use_exit_signal"] = data.use_exit_signal + default_conf["max_open_trades"] = 10 - mocker.patch("freqtrade.exchange.Exchange.get_fee", return_value=0.0) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) - mocker.patch("freqtrade.exchange.Binance.get_max_leverage", return_value=100) + mocker.patch(f"{EXMS}.get_fee", return_value=0.0) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f"{EXMS}.get_max_leverage", return_value=100) patch_exchange(mocker) frame = _build_backtest_dataframe(data.data) backtesting = Backtesting(default_conf) @@ -951,7 +952,6 @@ def test_backtest_results(default_conf, fee, mocker, caplog, data: BTContainer) processed=data_processed, start_date=min_date, end_date=max_date, - max_open_trades=10, ) results = result['results'] diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index 9c6086b44..8dee45b6d 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -19,15 +19,15 @@ from freqtrade.data.btanalysis import BT_DATA_COLUMNS, evaluate_result_multi from freqtrade.data.converter import clean_ohlcv_dataframe from freqtrade.data.dataprovider import DataProvider from freqtrade.data.history import get_timerange -from freqtrade.enums import ExitType, RunMode +from freqtrade.enums import CandleType, ExitType, RunMode from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.exchange.exchange import timeframe_to_next_date from freqtrade.optimize.backtest_caching import get_strategy_run_id from freqtrade.optimize.backtesting import Backtesting -from freqtrade.persistence import LocalTrade +from freqtrade.persistence import LocalTrade, Trade from freqtrade.resolvers import StrategyResolver -from tests.conftest import (CURRENT_TEST_STRATEGY, get_args, log_has, log_has_re, patch_exchange, - patched_configuration_load_config_file) +from tests.conftest import (CURRENT_TEST_STRATEGY, EXMS, get_args, log_has, log_has_re, + patch_exchange, patched_configuration_load_config_file) ORDER_TYPES = [ @@ -96,7 +96,6 @@ def _make_backtest_conf(mocker, datadir, conf=None, pair='UNITTEST/BTC'): 'processed': processed, 'start_date': min_date, 'end_date': max_date, - 'max_open_trades': 10, } @@ -246,7 +245,7 @@ def test_setup_optimize_configuration_stake_amount(mocker, default_conf, caplog) def test_start(mocker, fee, default_conf, caplog) -> None: start_mock = MagicMock() - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch(f'{EXMS}.get_fee', fee) patch_exchange(mocker) mocker.patch('freqtrade.optimize.backtesting.Backtesting.start', start_mock) patched_configuration_load_config_file(mocker, default_conf) @@ -270,7 +269,7 @@ def test_backtesting_init(mocker, default_conf, order_types) -> None: """ default_conf["order_types"] = order_types patch_exchange(mocker) - get_fee = mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.5)) + get_fee = mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.5)) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) assert backtesting.config == default_conf @@ -291,7 +290,7 @@ def test_backtesting_init_no_timeframe(mocker, default_conf, caplog) -> None: default_conf['strategy_list'] = [CURRENT_TEST_STRATEGY, 'HyperoptableStrategy'] - mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.5)) + mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.5)) with pytest.raises(OperationalException, match=r"Timeframe needs to be set in either configuration"): Backtesting(default_conf) @@ -301,7 +300,7 @@ def test_data_with_fee(default_conf, mocker) -> None: patch_exchange(mocker) default_conf['fee'] = 0.1234 - fee_mock = mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.5)) + fee_mock = mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.5)) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) assert backtesting.fee == 0.1234 @@ -360,7 +359,6 @@ def test_backtesting_start(default_conf, mocker, testdatadir, caplog) -> None: PropertyMock(return_value=['UNITTEST/BTC'])) default_conf['timeframe'] = '1m' - default_conf['datadir'] = testdatadir default_conf['export'] = 'signals' default_conf['exportfilename'] = 'export.txt' default_conf['timerange'] = '-1510694220' @@ -396,7 +394,6 @@ def test_backtesting_start_no_data(default_conf, mocker, caplog, testdatadir) -> PropertyMock(return_value=['UNITTEST/BTC'])) default_conf['timeframe'] = "1m" - default_conf['datadir'] = testdatadir default_conf['export'] = 'none' default_conf['timerange'] = '20180101-20180102' @@ -407,7 +404,7 @@ def test_backtesting_start_no_data(default_conf, mocker, caplog, testdatadir) -> def test_backtesting_no_pair_left(default_conf, mocker, caplog, testdatadir) -> None: - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) mocker.patch('freqtrade.data.history.history_utils.load_pair_history', MagicMock(return_value=pd.DataFrame())) mocker.patch('freqtrade.data.history.get_timerange', get_timerange) @@ -417,7 +414,6 @@ def test_backtesting_no_pair_left(default_conf, mocker, caplog, testdatadir) -> PropertyMock(return_value=[])) default_conf['timeframe'] = "1m" - default_conf['datadir'] = testdatadir default_conf['export'] = 'none' default_conf['timerange'] = '20180101-20180102' @@ -440,9 +436,9 @@ def test_backtesting_no_pair_left(default_conf, mocker, caplog, testdatadir) -> def test_backtesting_pairlist_list(default_conf, mocker, caplog, testdatadir, tickers) -> None: - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) - mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers) - mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.get_tickers', tickers) + mocker.patch(f'{EXMS}.price_to_precision', lambda s, x, y: y) mocker.patch('freqtrade.data.history.get_timerange', get_timerange) patch_exchange(mocker) mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest') @@ -451,7 +447,6 @@ def test_backtesting_pairlist_list(default_conf, mocker, caplog, testdatadir, ti mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.refresh_pairlist') default_conf['ticker_interval'] = "1m" - default_conf['datadir'] = testdatadir default_conf['export'] = 'none' # Use stoploss from strategy del default_conf['stoploss'] @@ -479,9 +474,9 @@ def test_backtesting_pairlist_list(default_conf, mocker, caplog, testdatadir, ti def test_backtest__enter_trade(default_conf, fee, mocker) -> None: default_conf['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f'{EXMS}.get_min_pair_stake_amount', return_value=0.00001) + mocker.patch(f'{EXMS}.get_max_pair_stake_amount', return_value=float('inf')) patch_exchange(mocker) default_conf['stake_amount'] = 'unlimited' default_conf['max_open_trades'] = 2 @@ -530,7 +525,7 @@ def test_backtest__enter_trade(default_conf, fee, mocker) -> None: assert trade.stake_amount == 495 assert trade.is_short is True - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=300.0) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=300.0) trade = backtesting._enter_trade(pair, row=row, direction='long') assert trade assert trade.stake_amount == 300.0 @@ -538,10 +533,10 @@ def test_backtest__enter_trade(default_conf, fee, mocker) -> None: def test_backtest__enter_trade_futures(default_conf_usdt, fee, mocker) -> None: default_conf_usdt['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) - mocker.patch("freqtrade.exchange.Exchange.get_max_leverage", return_value=100) + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f"{EXMS}.get_max_leverage", return_value=100) mocker.patch("freqtrade.optimize.backtesting.price_to_precision", lambda p, *args: p) patch_exchange(mocker) default_conf_usdt['stake_amount'] = 300 @@ -549,7 +544,6 @@ def test_backtest__enter_trade_futures(default_conf_usdt, fee, mocker) -> None: default_conf_usdt['trading_mode'] = 'futures' default_conf_usdt['margin_mode'] = 'isolated' default_conf_usdt['stake_currency'] = 'USDT' - default_conf_usdt['datadir'] = Path(default_conf_usdt['datadir']) default_conf_usdt['exchange']['pair_whitelist'] = ['.*'] backtesting = Backtesting(default_conf_usdt) backtesting._set_strategy(backtesting.strategylist[0]) @@ -570,7 +564,7 @@ def test_backtest__enter_trade_futures(default_conf_usdt, fee, mocker) -> None: ] backtesting.strategy.leverage = MagicMock(return_value=5.0) - mocker.patch("freqtrade.exchange.Exchange.get_maintenance_ratio_and_amt", + mocker.patch(f"{EXMS}.get_maintenance_ratio_and_amt", return_value=(0.01, 0.01)) # leverage = 5 @@ -607,7 +601,7 @@ def test_backtest__enter_trade_futures(default_conf_usdt, fee, mocker) -> None: assert pytest.approx(trade.liquidation_price) == 0.11787191 # Stake-amount too high! - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=600.0) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=600.0) trade = backtesting._enter_trade(pair, row=row, direction='long') assert trade is None @@ -620,11 +614,11 @@ def test_backtest__enter_trade_futures(default_conf_usdt, fee, mocker) -> None: assert trade is None -def test_backtest__get_sell_trade_entry(default_conf, fee, mocker) -> None: +def test_backtest__check_trade_exit(default_conf, fee, mocker) -> None: default_conf['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) patch_exchange(mocker) default_conf['timeframe_detail'] = '1m' default_conf['max_open_trades'] = 2 @@ -666,7 +660,7 @@ def test_backtest__get_sell_trade_entry(default_conf, fee, mocker) -> None: ] # No data available. - res = backtesting._get_exit_trade_entry(trade, row_sell, True) + res = backtesting._check_trade_exit(trade, row_sell) assert res is not None assert res.exit_reason == ExitType.ROI.value assert res.close_date_utc == datetime(2020, 1, 1, 5, 0, tzinfo=timezone.utc) @@ -679,15 +673,17 @@ def test_backtest__get_sell_trade_entry(default_conf, fee, mocker) -> None: [], columns=['date', 'open', 'high', 'low', 'close', 'enter_long', 'exit_long', 'enter_short', 'exit_short', 'long_tag', 'short_tag', 'exit_tag']) - res = backtesting._get_exit_trade_entry(trade, row, True) + res = backtesting._check_trade_exit(trade, row) assert res is None def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None: default_conf['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + default_conf['max_open_trades'] = 10 + + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) patch_exchange(mocker) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) @@ -702,7 +698,6 @@ def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None: processed=deepcopy(processed), start_date=min_date, end_date=max_date, - max_open_trades=10, ) results = result['results'] assert not results.empty @@ -771,9 +766,9 @@ def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None: @pytest.mark.parametrize('use_detail', [True, False]) def test_backtest_one_detail(default_conf_usdt, fee, mocker, testdatadir, use_detail) -> None: default_conf_usdt['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) if use_detail: default_conf_usdt['timeframe_detail'] = '1m' patch_exchange(mocker) @@ -786,6 +781,8 @@ def test_backtest_one_detail(default_conf_usdt, fee, mocker, testdatadir, use_de def custom_entry_price(proposed_rate, **kwargs): return proposed_rate * 0.997 + default_conf_usdt['max_open_trades'] = 10 + backtesting = Backtesting(default_conf_usdt) backtesting._set_strategy(backtesting.strategylist[0]) backtesting.strategy.populate_entry_trend = advise_entry @@ -793,10 +790,10 @@ def test_backtest_one_detail(default_conf_usdt, fee, mocker, testdatadir, use_de pair = 'XRP/ETH' # Pick a timerange adapted to the pair we use to test timerange = TimeRange.parse_timerange('20191010-20191013') - data = history.load_data(datadir=testdatadir, timeframe='5m', pairs=['XRP/ETH'], + data = history.load_data(datadir=testdatadir, timeframe='5m', pairs=[pair], timerange=timerange) if use_detail: - data_1m = history.load_data(datadir=testdatadir, timeframe='1m', pairs=['XRP/ETH'], + data_1m = history.load_data(datadir=testdatadir, timeframe='1m', pairs=[pair], timerange=timerange) backtesting.detail_data = data_1m processed = backtesting.strategy.advise_all_indicators(data) @@ -806,7 +803,6 @@ def test_backtest_one_detail(default_conf_usdt, fee, mocker, testdatadir, use_de processed=deepcopy(processed), start_date=min_date, end_date=max_date, - max_open_trades=10, ) results = result['results'] assert not results.empty @@ -850,16 +846,175 @@ def test_backtest_one_detail(default_conf_usdt, fee, mocker, testdatadir, use_de assert late_entry > 0 +@pytest.mark.parametrize('use_detail', [True, False]) +def test_backtest_one_detail_futures( + default_conf_usdt, fee, mocker, testdatadir, use_detail) -> None: + default_conf_usdt['use_exit_signal'] = False + default_conf_usdt['trading_mode'] = 'futures' + default_conf_usdt['margin_mode'] = 'isolated' + default_conf_usdt['candle_type_def'] = CandleType.FUTURES + + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist', + PropertyMock(return_value=['XRP/USDT:USDT'])) + mocker.patch(f"{EXMS}.get_maintenance_ratio_and_amt", + return_value=(0.01, 0.01)) + default_conf_usdt['timeframe'] = '1h' + if use_detail: + default_conf_usdt['timeframe_detail'] = '5m' + patch_exchange(mocker) + + def advise_entry(df, *args, **kwargs): + # Mock function to force several entries + df.loc[(df['rsi'] < 40), 'enter_long'] = 1 + return df + + def custom_entry_price(proposed_rate, **kwargs): + return proposed_rate * 0.997 + + default_conf_usdt['max_open_trades'] = 10 + + backtesting = Backtesting(default_conf_usdt) + backtesting._set_strategy(backtesting.strategylist[0]) + backtesting.strategy.populate_entry_trend = advise_entry + backtesting.strategy.custom_entry_price = custom_entry_price + pair = 'XRP/USDT:USDT' + # Pick a timerange adapted to the pair we use to test + timerange = TimeRange.parse_timerange('20211117-20211119') + data = history.load_data(datadir=Path(testdatadir), timeframe='1h', pairs=[pair], + timerange=timerange, candle_type=CandleType.FUTURES) + backtesting.load_bt_data_detail() + processed = backtesting.strategy.advise_all_indicators(data) + min_date, max_date = get_timerange(processed) + + result = backtesting.backtest( + processed=deepcopy(processed), + start_date=min_date, + end_date=max_date, + ) + results = result['results'] + assert not results.empty + # Timeout settings from default_conf = entry: 10, exit: 30 + assert len(results) == (5 if use_detail else 2) + + assert 'orders' in results.columns + data_pair = processed[pair] + + data_1m_pair = backtesting.detail_data[pair] if use_detail else pd.DataFrame() + late_entry = 0 + for _, t in results.iterrows(): + assert len(t['orders']) == 2 + + entryo = t['orders'][0] + entry_ts = datetime.fromtimestamp(entryo['order_filled_timestamp'] // 1000, tz=timezone.utc) + if entry_ts > t['open_date']: + late_entry += 1 + + # Get "entry fill" candle + ln = (data_1m_pair.loc[data_1m_pair["date"] == entry_ts] + if use_detail else data_pair.loc[data_pair["date"] == entry_ts]) + # Check open trade rate aligns to open rate + assert not ln.empty + + assert round(ln.iloc[0]["low"], 6) <= round( + t["open_rate"], 6) <= round(ln.iloc[0]["high"], 6) + # check close trade rate aligns to close rate or is between high and low + ln1 = data_pair.loc[data_pair["date"] == t["close_date"]] + if use_detail: + ln1_1m = data_1m_pair.loc[data_1m_pair["date"] == t["close_date"]] + assert not ln1.empty or not ln1_1m.empty + else: + assert not ln1.empty + ln2 = ln1_1m if ln1.empty else ln1 + + assert (round(ln2.iloc[0]["low"], 6) <= round( + t["close_rate"], 6) <= round(ln2.iloc[0]["high"], 6)) + assert -0.0181 < Trade.trades[1].funding_fees < -0.01 + # assert late_entry > 0 + + +@pytest.mark.parametrize('use_detail', [True, False]) +def test_backtest_one_detail_futures_funding_fees( + default_conf_usdt, fee, mocker, testdatadir, use_detail) -> None: + default_conf_usdt['use_exit_signal'] = False + default_conf_usdt['trading_mode'] = 'futures' + default_conf_usdt['margin_mode'] = 'isolated' + default_conf_usdt['candle_type_def'] = CandleType.FUTURES + default_conf_usdt['minimal_roi'] = {'0': 1} + default_conf_usdt['dry_run_wallet'] = 100000 + + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist', + PropertyMock(return_value=['XRP/USDT:USDT'])) + mocker.patch(f"{EXMS}.get_maintenance_ratio_and_amt", + return_value=(0.01, 0.01)) + default_conf_usdt['timeframe'] = '1h' + if use_detail: + default_conf_usdt['timeframe_detail'] = '5m' + patch_exchange(mocker) + + def advise_entry(df, *args, **kwargs): + # Mock function to force several entries + df.loc[:, 'enter_long'] = 1 + return df + + def adjust_trade_position(trade, current_time, **kwargs): + if current_time > datetime(2021, 11, 18, 2, 0, 0, tzinfo=timezone.utc): + return None + return default_conf_usdt['stake_amount'] + + default_conf_usdt['max_open_trades'] = 1 + + backtesting = Backtesting(default_conf_usdt) + backtesting._set_strategy(backtesting.strategylist[0]) + backtesting.strategy.populate_entry_trend = advise_entry + backtesting.strategy.adjust_trade_position = adjust_trade_position + backtesting.strategy.leverage = lambda **kwargs: 1 + backtesting.strategy.position_adjustment_enable = True + pair = 'XRP/USDT:USDT' + # Pick a timerange adapted to the pair we use to test + timerange = TimeRange.parse_timerange('20211117-20211119') + data = history.load_data(datadir=Path(testdatadir), timeframe='1h', pairs=[pair], + timerange=timerange, candle_type=CandleType.FUTURES) + backtesting.load_bt_data_detail() + processed = backtesting.strategy.advise_all_indicators(data) + min_date, max_date = get_timerange(processed) + + result = backtesting.backtest( + processed=deepcopy(processed), + start_date=min_date, + end_date=max_date, + ) + results = result['results'] + assert not results.empty + # Only one result - as we're not selling. + assert len(results) == 1 + + assert 'orders' in results.columns + + for t in Trade.trades: + # At least 4 adjustment orders + assert t.nr_of_successful_entries >= 6 + # Funding fees will vary depending on the number of adjustment orders + # That number is a lot higher with detail data. + assert -20 < t.funding_fees < -0.1 + + def test_backtest_timedout_entry_orders(default_conf, fee, mocker, testdatadir) -> None: # This strategy intentionally places unfillable orders. default_conf['strategy'] = 'StrategyTestV3CustomEntryPrice' default_conf['startup_candle_count'] = 0 # Cancel unfilled order after 4 minutes on 5m timeframe. default_conf["unfilledtimeout"] = {"entry": 4} - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) patch_exchange(mocker) + default_conf['max_open_trades'] = 1 backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) # Testing dataframe contains 11 candles. Expecting 10 timed out orders. @@ -872,7 +1027,6 @@ def test_backtest_timedout_entry_orders(default_conf, fee, mocker, testdatadir) processed=deepcopy(data), start_date=min_date, end_date=max_date, - max_open_trades=1, ) assert result['timedout_entry_orders'] == 10 @@ -880,9 +1034,10 @@ def test_backtest_timedout_entry_orders(default_conf, fee, mocker, testdatadir) def test_backtest_1min_timeframe(default_conf, fee, mocker, testdatadir) -> None: default_conf['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + default_conf['max_open_trades'] = 1 + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) patch_exchange(mocker) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) @@ -897,7 +1052,6 @@ def test_backtest_1min_timeframe(default_conf, fee, mocker, testdatadir) -> None processed=processed, start_date=min_date, end_date=max_date, - max_open_trades=1, ) assert not results['results'].empty assert len(results['results']) == 1 @@ -905,9 +1059,11 @@ def test_backtest_1min_timeframe(default_conf, fee, mocker, testdatadir) -> None def test_backtest_trim_no_data_left(default_conf, fee, mocker, testdatadir) -> None: default_conf['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + default_conf['max_open_trades'] = 10 + + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) patch_exchange(mocker) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) @@ -928,7 +1084,6 @@ def test_backtest_trim_no_data_left(default_conf, fee, mocker, testdatadir) -> N processed=deepcopy(processed), start_date=min_date, end_date=max_date, - max_open_trades=10, ) @@ -949,9 +1104,10 @@ def test_processed(default_conf, mocker, testdatadir) -> None: def test_backtest_dataprovider_analyzed_df(default_conf, fee, mocker, testdatadir) -> None: default_conf['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=100000) + default_conf['max_open_trades'] = 10 + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=100000) patch_exchange(mocker) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) @@ -982,7 +1138,6 @@ def test_backtest_dataprovider_analyzed_df(default_conf, fee, mocker, testdatadi processed=deepcopy(processed), start_date=min_date, end_date=max_date, - max_open_trades=10, ) assert count == 5 @@ -999,9 +1154,10 @@ def test_backtest_pricecontours_protections(default_conf, fee, mocker, testdatad default_conf['enable_protections'] = True default_conf['timeframe'] = '1m' - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + default_conf['max_open_trades'] = 1 + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) tests = [ ['sine', 9], ['raise', 10], @@ -1025,7 +1181,6 @@ def test_backtest_pricecontours_protections(default_conf, fee, mocker, testdatad processed=processed, start_date=min_date, end_date=max_date, - max_open_trades=1, ) assert len(results['results']) == numres @@ -1048,9 +1203,9 @@ def test_backtest_pricecontours(default_conf, fee, mocker, testdatadir, default_conf['protections'] = protections default_conf['enable_protections'] = True - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f'{EXMS}.get_fee', fee) # While entry-signals are unrealistic, running backtesting # over and over again should not cause different results @@ -1063,11 +1218,12 @@ def test_backtest_pricecontours(default_conf, fee, mocker, testdatadir, processed = backtesting.strategy.advise_all_indicators(data) min_date, max_date = get_timerange(processed) assert isinstance(processed, dict) + backtesting.strategy.max_open_trades = 1 + backtesting.config.update({'max_open_trades': 1}) results = backtesting.backtest( processed=processed, start_date=min_date, end_date=max_date, - max_open_trades=1, ) assert len(results['results']) == expected @@ -1078,7 +1234,7 @@ def test_backtest_clash_buy_sell(mocker, default_conf, testdatadir): buy_value = 1 sell_value = 1 return _trend(dataframe, buy_value, sell_value) - + default_conf['max_open_trades'] = 10 backtest_conf = _make_backtest_conf(mocker, conf=default_conf, datadir=testdatadir) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) @@ -1095,6 +1251,7 @@ def test_backtest_only_sell(mocker, default_conf, testdatadir): sell_value = 1 return _trend(dataframe, buy_value, sell_value) + default_conf['max_open_trades'] = 10 backtest_conf = _make_backtest_conf(mocker, conf=default_conf, datadir=testdatadir) backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) @@ -1105,9 +1262,10 @@ def test_backtest_only_sell(mocker, default_conf, testdatadir): def test_backtest_alternate_buy_sell(default_conf, fee, mocker, testdatadir): - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f'{EXMS}.get_fee', fee) + default_conf['max_open_trades'] = 10 backtest_conf = _make_backtest_conf(mocker, conf=default_conf, pair='UNITTEST/BTC', datadir=testdatadir) default_conf['timeframe'] = '1m' @@ -1152,9 +1310,9 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) dataframe['exit_short'] = 0 return dataframe - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f'{EXMS}.get_fee', fee) patch_exchange(mocker) pairs = ['ADA/BTC', 'DASH/BTC', 'ETH/BTC', 'LTC/BTC', 'NXT/BTC'] @@ -1166,6 +1324,7 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) if tres > 0: data[pair] = data[pair][tres:].reset_index() default_conf['timeframe'] = '5m' + default_conf['max_open_trades'] = 3 backtesting = Backtesting(default_conf) backtesting._set_strategy(backtesting.strategylist[0]) @@ -1174,11 +1333,11 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) processed = backtesting.strategy.advise_all_indicators(data) min_date, max_date = get_timerange(processed) + backtest_conf = { 'processed': deepcopy(processed), 'start_date': min_date, 'end_date': max_date, - 'max_open_trades': 3, } results = backtesting.backtest(**backtest_conf) @@ -1196,11 +1355,12 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) backtesting.dataprovider.get_analyzed_dataframe('NXT/BTC', '5m')[0] ) == len(data['NXT/BTC']) - 1 - backtesting.strategy.startup_candle_count + backtesting.strategy.max_open_trades = 1 + backtesting.config.update({'max_open_trades': 1}) backtest_conf = { 'processed': deepcopy(processed), 'start_date': min_date, 'end_date': max_date, - 'max_open_trades': 1, } results = backtesting.backtest(**backtest_conf) assert len(evaluate_result_multi(results['results'], '5m', 1)) == 0 diff --git a/tests/optimize/test_backtesting_adjust_position.py b/tests/optimize/test_backtesting_adjust_position.py index 5c740458f..9fc726bd1 100644 --- a/tests/optimize/test_backtesting_adjust_position.py +++ b/tests/optimize/test_backtesting_adjust_position.py @@ -12,16 +12,17 @@ from freqtrade.data import history from freqtrade.data.history import get_timerange from freqtrade.enums import ExitType from freqtrade.optimize.backtesting import Backtesting -from tests.conftest import patch_exchange +from tests.conftest import EXMS, patch_exchange def test_backtest_position_adjustment(default_conf, fee, mocker, testdatadir) -> None: default_conf['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + default_conf['max_open_trades'] = 10 + mocker.patch(f'{EXMS}.get_fee', fee) mocker.patch('freqtrade.optimize.backtesting.amount_to_contract_precision', lambda x, *args, **kwargs: round(x, 8)) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) patch_exchange(mocker) default_conf.update({ "stake_amount": 100.0, @@ -41,7 +42,6 @@ def test_backtest_position_adjustment(default_conf, fee, mocker, testdatadir) -> processed=deepcopy(processed), start_date=min_date, end_date=max_date, - max_open_trades=10, ) results = result['results'] assert not results.empty @@ -99,10 +99,10 @@ def test_backtest_position_adjustment(default_conf, fee, mocker, testdatadir) -> ]) def test_backtest_position_adjustment_detailed(default_conf, fee, mocker, leverage) -> None: default_conf['use_exit_signal'] = False - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=10) - mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) - mocker.patch("freqtrade.exchange.Exchange.get_max_leverage", return_value=10) + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=10) + mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf')) + mocker.patch(f"{EXMS}.get_max_leverage", return_value=10) patch_exchange(mocker) default_conf.update({ diff --git a/tests/optimize/test_edge_cli.py b/tests/optimize/test_edge_cli.py index 8241a5362..64172bf1c 100644 --- a/tests/optimize/test_edge_cli.py +++ b/tests/optimize/test_edge_cli.py @@ -6,7 +6,7 @@ from unittest.mock import MagicMock from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_edge from freqtrade.enums import RunMode from freqtrade.optimize.edge_cli import EdgeCli -from tests.conftest import (CURRENT_TEST_STRATEGY, get_args, log_has, patch_exchange, +from tests.conftest import (CURRENT_TEST_STRATEGY, EXMS, get_args, log_has, patch_exchange, patched_configuration_load_config_file) @@ -71,7 +71,7 @@ def test_setup_edge_configuration_with_arguments(mocker, edge_conf, caplog) -> N def test_start(mocker, fee, edge_conf, caplog) -> None: start_mock = MagicMock() - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch(f'{EXMS}.get_fee', fee) patch_exchange(mocker) mocker.patch('freqtrade.optimize.edge_cli.EdgeCli.start', start_mock) patched_configuration_load_config_file(mocker, edge_conf) @@ -101,7 +101,7 @@ def test_edge_init_fee(mocker, edge_conf) -> None: patch_exchange(mocker) edge_conf['fee'] = 0.1234 edge_conf['stake_amount'] = 20 - fee_mock = mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.5)) + fee_mock = mocker.patch(f'{EXMS}.get_fee', return_value=0.5) edge_cli = EdgeCli(edge_conf) assert edge_cli.edge.fee == 0.1234 assert fee_mock.call_count == 0 diff --git a/tests/optimize/test_hyperopt.py b/tests/optimize/test_hyperopt.py index 5bce9f419..998798580 100644 --- a/tests/optimize/test_hyperopt.py +++ b/tests/optimize/test_hyperopt.py @@ -1,5 +1,6 @@ # pragma pylint: disable=missing-docstring,W0212,C0103 from datetime import datetime, timedelta +from functools import wraps from pathlib import Path from unittest.mock import ANY, MagicMock, PropertyMock @@ -7,6 +8,7 @@ import pandas as pd import pytest from arrow import Arrow from filelock import Timeout +from skopt.space import Integer from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_hyperopt from freqtrade.data.history import load_data @@ -18,7 +20,7 @@ from freqtrade.optimize.hyperopt_tools import HyperoptTools from freqtrade.optimize.optimize_reports import generate_strategy_stats from freqtrade.optimize.space import SKDecimal from freqtrade.strategy import IntParameter -from tests.conftest import (CURRENT_TEST_STRATEGY, get_args, get_markets, log_has, log_has_re, +from tests.conftest import (CURRENT_TEST_STRATEGY, EXMS, get_args, get_markets, log_has, log_has_re, patch_exchange, patched_configuration_load_config_file) @@ -292,6 +294,8 @@ def test_params_no_optimize_details(hyperopt) -> None: assert res['roi']['0'] == 0.04 assert "stoploss" in res assert res['stoploss']['stoploss'] == -0.1 + assert "max_open_trades" in res + assert res['max_open_trades']['max_open_trades'] == 1 def test_start_calls_optimizer(mocker, hyperopt_conf, capsys) -> None: @@ -334,8 +338,7 @@ def test_start_calls_optimizer(mocker, hyperopt_conf, capsys) -> None: assert dumper2.call_count == 1 assert hasattr(hyperopt.backtesting.strategy, "advise_exit") assert hasattr(hyperopt.backtesting.strategy, "advise_entry") - assert hasattr(hyperopt, "max_open_trades") - assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] + assert hyperopt.backtesting.strategy.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt.backtesting, "_position_stacking") @@ -474,6 +477,7 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: 'trailing_stop_positive': 0.02, 'trailing_stop_positive_offset_p1': 0.05, 'trailing_only_offset_is_reached': False, + 'max_open_trades': 3, } response_expected = { 'loss': 1.9147239021396234, @@ -499,7 +503,9 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None: 'trailing': {'trailing_only_offset_is_reached': False, 'trailing_stop': True, 'trailing_stop_positive': 0.02, - 'trailing_stop_positive_offset': 0.07}}, + 'trailing_stop_positive_offset': 0.07}, + 'max_open_trades': {'max_open_trades': 3} + }, 'params_dict': optimizer_param, 'params_not_optimized': {'buy': {}, 'protection': {}, 'sell': {}}, 'results_metrics': ANY, @@ -548,7 +554,8 @@ def test_print_json_spaces_all(mocker, hyperopt_conf, capsys) -> None: 'buy': {'mfi-value': None}, 'sell': {'sell-mfi-value': None}, 'roi': {}, 'stoploss': {'stoploss': None}, - 'trailing': {'trailing_stop': None} + 'trailing': {'trailing_stop': None}, + 'max_open_trades': {'max_open_trades': None} }, 'results_metrics': generate_result_metrics(), }]) @@ -571,7 +578,7 @@ def test_print_json_spaces_all(mocker, hyperopt_conf, capsys) -> None: out, err = capsys.readouterr() result_str = ( '{"params":{"mfi-value":null,"sell-mfi-value":null},"minimal_roi"' - ':{},"stoploss":null,"trailing_stop":null}' + ':{},"stoploss":null,"trailing_stop":null,"max_open_trades":null}' ) assert result_str in out # noqa: E501 # Should be called for historical candle data @@ -702,8 +709,7 @@ def test_simplified_interface_roi_stoploss(mocker, hyperopt_conf, capsys) -> Non assert hasattr(hyperopt.backtesting.strategy, "advise_exit") assert hasattr(hyperopt.backtesting.strategy, "advise_entry") - assert hasattr(hyperopt, "max_open_trades") - assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] + assert hyperopt.backtesting.strategy.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt.backtesting, "_position_stacking") @@ -776,8 +782,7 @@ def test_simplified_interface_buy(mocker, hyperopt_conf, capsys) -> None: assert dumper2.call_count == 1 assert hasattr(hyperopt.backtesting.strategy, "advise_exit") assert hasattr(hyperopt.backtesting.strategy, "advise_entry") - assert hasattr(hyperopt, "max_open_trades") - assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] + assert hyperopt.backtesting.strategy.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt.backtesting, "_position_stacking") @@ -819,8 +824,7 @@ def test_simplified_interface_sell(mocker, hyperopt_conf, capsys) -> None: assert dumper2.call_count == 1 assert hasattr(hyperopt.backtesting.strategy, "advise_exit") assert hasattr(hyperopt.backtesting.strategy, "advise_entry") - assert hasattr(hyperopt, "max_open_trades") - assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] + assert hyperopt.backtesting.strategy.max_open_trades == hyperopt_conf['max_open_trades'] assert hasattr(hyperopt.backtesting, "_position_stacking") @@ -855,7 +859,7 @@ def test_simplified_interface_failed(mocker, hyperopt_conf, space) -> None: def test_in_strategy_auto_hyperopt(mocker, hyperopt_conf, tmpdir, fee) -> None: patch_exchange(mocker) - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch(f'{EXMS}.get_fee', fee) (Path(tmpdir) / 'hyperopt_results').mkdir(parents=True) # No hyperopt needed hyperopt_conf.update({ @@ -874,6 +878,7 @@ def test_in_strategy_auto_hyperopt(mocker, hyperopt_conf, tmpdir, fee) -> None: assert hyperopt.backtesting.strategy.buy_rsi.value == 35 assert hyperopt.backtesting.strategy.sell_rsi.value == 74 assert hyperopt.backtesting.strategy.protection_cooldown_lookback.value == 30 + assert hyperopt.backtesting.strategy.max_open_trades == 1 buy_rsi_range = hyperopt.backtesting.strategy.buy_rsi.range assert isinstance(buy_rsi_range, range) # Range from 0 - 50 (inclusive) @@ -884,6 +889,7 @@ def test_in_strategy_auto_hyperopt(mocker, hyperopt_conf, tmpdir, fee) -> None: assert hyperopt.backtesting.strategy.protection_cooldown_lookback.value != 30 assert hyperopt.backtesting.strategy.buy_rsi.value != 35 assert hyperopt.backtesting.strategy.sell_rsi.value != 74 + assert hyperopt.backtesting.strategy.max_open_trades != 1 hyperopt.custom_hyperopt.generate_estimator = lambda *args, **kwargs: 'ET1' with pytest.raises(OperationalException, match="Estimator ET1 not supported."): @@ -891,10 +897,10 @@ def test_in_strategy_auto_hyperopt(mocker, hyperopt_conf, tmpdir, fee) -> None: def test_in_strategy_auto_hyperopt_with_parallel(mocker, hyperopt_conf, tmpdir, fee) -> None: - mocker.patch('freqtrade.exchange.Exchange.validate_config', MagicMock()) - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) - mocker.patch('freqtrade.exchange.Exchange._load_markets') - mocker.patch('freqtrade.exchange.Exchange.markets', + mocker.patch(f'{EXMS}.validate_config', MagicMock()) + mocker.patch(f'{EXMS}.get_fee', fee) + mocker.patch(f'{EXMS}._load_markets') + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=get_markets())) (Path(tmpdir) / 'hyperopt_results').mkdir(parents=True) # No hyperopt needed @@ -932,7 +938,7 @@ def test_in_strategy_auto_hyperopt_with_parallel(mocker, hyperopt_conf, tmpdir, def test_in_strategy_auto_hyperopt_per_epoch(mocker, hyperopt_conf, tmpdir, fee) -> None: patch_exchange(mocker) - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch(f'{EXMS}.get_fee', fee) (Path(tmpdir) / 'hyperopt_results').mkdir(parents=True) hyperopt_conf.update({ @@ -984,3 +990,124 @@ def test_SKDecimal(): assert space.transform([2.0]) == [200] assert space.transform([1.0]) == [100] assert space.transform([1.5, 1.6]) == [150, 160] + + +def test_stake_amount_unlimited_max_open_trades(mocker, hyperopt_conf, tmpdir, fee) -> None: + # This test is to ensure that unlimited max_open_trades are ignored for the backtesting + # if we have an unlimited stake amount + patch_exchange(mocker) + mocker.patch(f'{EXMS}.get_fee', fee) + (Path(tmpdir) / 'hyperopt_results').mkdir(parents=True) + hyperopt_conf.update({ + 'strategy': 'HyperoptableStrategy', + 'user_data_dir': Path(tmpdir), + 'hyperopt_random_state': 42, + 'spaces': ['trades'], + 'stake_amount': 'unlimited' + }) + hyperopt = Hyperopt(hyperopt_conf) + mocker.patch('freqtrade.optimize.hyperopt.Hyperopt._get_params_dict', + return_value={ + 'max_open_trades': -1 + }) + + assert isinstance(hyperopt.custom_hyperopt, HyperOptAuto) + + assert hyperopt.backtesting.strategy.max_open_trades == 1 + + hyperopt.start() + + assert hyperopt.backtesting.strategy.max_open_trades == 1 + + +def test_max_open_trades_dump(mocker, hyperopt_conf, tmpdir, fee, capsys) -> None: + # This test is to ensure that after hyperopting, max_open_trades is never + # saved as inf in the output json params + patch_exchange(mocker) + mocker.patch(f'{EXMS}.get_fee', fee) + (Path(tmpdir) / 'hyperopt_results').mkdir(parents=True) + hyperopt_conf.update({ + 'strategy': 'HyperoptableStrategy', + 'user_data_dir': Path(tmpdir), + 'hyperopt_random_state': 42, + 'spaces': ['trades'], + }) + hyperopt = Hyperopt(hyperopt_conf) + mocker.patch('freqtrade.optimize.hyperopt.Hyperopt._get_params_dict', + return_value={ + 'max_open_trades': -1 + }) + + assert isinstance(hyperopt.custom_hyperopt, HyperOptAuto) + + hyperopt.start() + + out, err = capsys.readouterr() + + assert 'max_open_trades = -1' in out + assert 'max_open_trades = inf' not in out + + ############## + + hyperopt_conf.update({'print_json': True}) + + hyperopt = Hyperopt(hyperopt_conf) + mocker.patch('freqtrade.optimize.hyperopt.Hyperopt._get_params_dict', + return_value={ + 'max_open_trades': -1 + }) + + assert isinstance(hyperopt.custom_hyperopt, HyperOptAuto) + + hyperopt.start() + + out, err = capsys.readouterr() + + assert '"max_open_trades":-1' in out + + +def test_max_open_trades_consistency(mocker, hyperopt_conf, tmpdir, fee) -> None: + # This test is to ensure that max_open_trades is the same across all functions needing it + # after it has been changed from the hyperopt + patch_exchange(mocker) + mocker.patch(f'{EXMS}.get_fee', return_value=0) + + (Path(tmpdir) / 'hyperopt_results').mkdir(parents=True) + hyperopt_conf.update({ + 'strategy': 'HyperoptableStrategy', + 'user_data_dir': Path(tmpdir), + 'hyperopt_random_state': 42, + 'spaces': ['trades'], + 'stake_amount': 'unlimited', + 'dry_run_wallet': 8, + 'available_capital': 8, + 'dry_run': True, + 'epochs': 1 + }) + hyperopt = Hyperopt(hyperopt_conf) + + assert isinstance(hyperopt.custom_hyperopt, HyperOptAuto) + + hyperopt.custom_hyperopt.max_open_trades_space = lambda: [ + Integer(1, 10, name='max_open_trades')] + + first_time_evaluated = False + + def stake_amount_interceptor(func): + @wraps(func) + def wrapper(*args, **kwargs): + nonlocal first_time_evaluated + stake_amount = func(*args, **kwargs) + if first_time_evaluated is False: + assert stake_amount == 1 + first_time_evaluated = True + return stake_amount + return wrapper + + hyperopt.backtesting.wallets._calculate_unlimited_stake_amount = stake_amount_interceptor( + hyperopt.backtesting.wallets._calculate_unlimited_stake_amount) + + hyperopt.start() + + assert hyperopt.backtesting.strategy.max_open_trades == 8 + assert hyperopt.config['max_open_trades'] == 8 diff --git a/tests/optimize/test_hyperopt_tools.py b/tests/optimize/test_hyperopt_tools.py index 7d4fef3bd..eace78eee 100644 --- a/tests/optimize/test_hyperopt_tools.py +++ b/tests/optimize/test_hyperopt_tools.py @@ -66,52 +66,58 @@ def test_load_previous_results2(mocker, testdatadir, caplog) -> None: @pytest.mark.parametrize("spaces, expected_results", [ (['buy'], {'buy': True, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': False, - 'protection': False}), + 'protection': False, 'trades': False}), (['sell'], {'buy': False, 'sell': True, 'roi': False, 'stoploss': False, 'trailing': False, - 'protection': False}), + 'protection': False, 'trades': False}), (['roi'], {'buy': False, 'sell': False, 'roi': True, 'stoploss': False, 'trailing': False, - 'protection': False}), + 'protection': False, 'trades': False}), (['stoploss'], {'buy': False, 'sell': False, 'roi': False, 'stoploss': True, 'trailing': False, - 'protection': False}), + 'protection': False, 'trades': False}), (['trailing'], {'buy': False, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': True, - 'protection': False}), + 'protection': False, 'trades': False}), (['buy', 'sell', 'roi', 'stoploss'], {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False, - 'protection': False}), + 'protection': False, 'trades': False}), (['buy', 'sell', 'roi', 'stoploss', 'trailing'], {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, - 'protection': False}), + 'protection': False, 'trades': False}), (['buy', 'roi'], {'buy': True, 'sell': False, 'roi': True, 'stoploss': False, 'trailing': False, - 'protection': False}), + 'protection': False, 'trades': False}), (['all'], {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, - 'protection': True}), + 'protection': True, 'trades': True}), (['default'], {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False, - 'protection': False}), + 'protection': False, 'trades': False}), (['default', 'trailing'], {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, - 'protection': False}), + 'protection': False, 'trades': False}), (['all', 'buy'], {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, - 'protection': True}), + 'protection': True, 'trades': True}), (['default', 'buy'], {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False, - 'protection': False}), + 'protection': False, 'trades': False}), (['all'], {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, - 'protection': True}), + 'protection': True, 'trades': True}), (['protection'], {'buy': False, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': False, - 'protection': True}), + 'protection': True, 'trades': False}), + (['trades'], + {'buy': False, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': False, + 'protection': False, 'trades': True}), + (['default', 'trades'], + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False, + 'protection': False, 'trades': True}), ]) def test_has_space(hyperopt_conf, spaces, expected_results): - for s in ['buy', 'sell', 'roi', 'stoploss', 'trailing', 'protection']: + for s in ['buy', 'sell', 'roi', 'stoploss', 'trailing', 'protection', 'trades']: hyperopt_conf.update({'spaces': spaces}) assert HyperoptTools.has_space(hyperopt_conf, s) == expected_results[s] @@ -193,6 +199,9 @@ def test_export_params(tmpdir): "346": 0.08499, "507": 0.049, "1595": 0 + }, + "max_open_trades": { + "max_open_trades": 5 } }, "params_not_optimized": { @@ -219,6 +228,7 @@ def test_export_params(tmpdir): assert "roi" in content["params"] assert "stoploss" in content["params"] assert "trailing" in content["params"] + assert "max_open_trades" in content["params"] def test_try_export_params(default_conf, tmpdir, caplog, mocker): @@ -297,6 +307,9 @@ def test_params_print(capsys): "trailing_stop_positive_offset": 0.1, "trailing_only_offset_is_reached": True }, + "max_open_trades": { + "max_open_trades": 5 + } } HyperoptTools._params_pretty_print(params, 'buy', 'No header', non_optimized) @@ -327,6 +340,13 @@ def test_params_print(capsys): assert re.search('trailing_stop_positive_offset = 0.1 # value loaded.*\n', captured.out) assert re.search('trailing_only_offset_is_reached = True # value loaded.*\n', captured.out) + HyperoptTools._params_pretty_print( + params, 'max_open_trades', "Max Open Trades:", non_optimized) + captured = capsys.readouterr() + + assert re.search("# Max Open Trades:", captured.out) + assert re.search('max_open_trades = 5 # value loaded.*\n', captured.out) + def test_hyperopt_serializer(): diff --git a/tests/optimize/test_optimize_reports.py b/tests/optimize/test_optimize_reports.py index 549202284..0cc32baaf 100644 --- a/tests/optimize/test_optimize_reports.py +++ b/tests/optimize/test_optimize_reports.py @@ -236,7 +236,7 @@ def test_store_backtest_candles(testdatadir, mocker): assert dump_mock.call_count == 1 assert isinstance(dump_mock.call_args_list[0][0][0], Path) - assert str(dump_mock.call_args_list[0][0][0]).endswith(str('_signals.pkl')) + assert str(dump_mock.call_args_list[0][0][0]).endswith('_signals.pkl') dump_mock.reset_mock() # mock file exporting @@ -245,7 +245,7 @@ def test_store_backtest_candles(testdatadir, mocker): assert dump_mock.call_count == 1 assert isinstance(dump_mock.call_args_list[0][0][0], Path) # result will be testdatadir / testresult-_signals.pkl - assert str(dump_mock.call_args_list[0][0][0]).endswith(str('_signals.pkl')) + assert str(dump_mock.call_args_list[0][0][0]).endswith('_signals.pkl') dump_mock.reset_mock() @@ -255,7 +255,7 @@ def test_write_read_backtest_candles(tmpdir): # test directory exporting stored_file = store_backtest_signal_candles(Path(tmpdir), candle_dict, '2022_01_01_15_05_13') - scp = open(stored_file, "rb") + scp = stored_file.open("rb") pickled_signal_candles = joblib.load(scp) scp.close() @@ -269,7 +269,7 @@ def test_write_read_backtest_candles(tmpdir): # test file exporting filename = Path(tmpdir / 'testresult') stored_file = store_backtest_signal_candles(filename, candle_dict, '2022_01_01_15_05_13') - scp = open(stored_file, "rb") + scp = stored_file.open("rb") pickled_signal_candles = joblib.load(scp) scp.close() diff --git a/tests/persistence/test_migrations.py b/tests/persistence/test_migrations.py index 2a6959d58..854d39994 100644 --- a/tests/persistence/test_migrations.py +++ b/tests/persistence/test_migrations.py @@ -4,7 +4,7 @@ from pathlib import Path from unittest.mock import MagicMock import pytest -from sqlalchemy import create_engine, text +from sqlalchemy import create_engine, select, text from freqtrade.constants import DEFAULT_DB_PROD_URL from freqtrade.enums import TradingMode @@ -21,8 +21,8 @@ spot, margin, futures = TradingMode.SPOT, TradingMode.MARGIN, TradingMode.FUTURE def test_init_create_session(default_conf): # Check if init create a session init_db(default_conf['db_url']) - assert hasattr(Trade, '_session') - assert 'scoped_session' in type(Trade._session).__name__ + assert hasattr(Trade, 'session') + assert 'scoped_session' in type(Trade.session).__name__ def test_init_custom_db_url(default_conf, tmpdir): @@ -34,7 +34,7 @@ def test_init_custom_db_url(default_conf, tmpdir): init_db(default_conf['db_url']) assert Path(filename).is_file() - r = Trade._session.execute(text("PRAGMA journal_mode")) + r = Trade.session.execute(text("PRAGMA journal_mode")) assert r.first() == ('wal',) @@ -235,8 +235,9 @@ def test_migrate_new(mocker, default_conf, fee, caplog): # Run init to test migration init_db(default_conf['db_url']) - assert len(Trade.query.filter(Trade.id == 1).all()) == 1 - trade = Trade.query.filter(Trade.id == 1).first() + trades = Trade.session.scalars(select(Trade).filter(Trade.id == 1)).all() + assert len(trades) == 1 + trade = trades[0] assert trade.fee_open == fee.return_value assert trade.fee_close == fee.return_value assert trade.open_rate_requested is None @@ -404,9 +405,9 @@ def test_migrate_pairlocks(mocker, default_conf, fee, caplog): init_db(default_conf['db_url']) - assert len(PairLock.query.all()) == 2 - assert len(PairLock.query.filter(PairLock.pair == '*').all()) == 1 - pairlocks = PairLock.query.filter(PairLock.pair == 'ETH/BTC').all() + assert len(PairLock.get_all_locks().all()) == 2 + assert len(PairLock.session.scalars(select(PairLock).filter(PairLock.pair == '*')).all()) == 1 + pairlocks = PairLock.session.scalars(select(PairLock).filter(PairLock.pair == 'ETH/BTC')).all() assert len(pairlocks) == 1 pairlocks[0].pair == 'ETH/BTC' pairlocks[0].side == '*' diff --git a/tests/persistence/test_persistence.py b/tests/persistence/test_persistence.py index e12e919fc..db882d56d 100644 --- a/tests/persistence/test_persistence.py +++ b/tests/persistence/test_persistence.py @@ -4,6 +4,7 @@ from types import FunctionType import arrow import pytest +from sqlalchemy import select from freqtrade.constants import DATETIME_PRINT_FORMAT from freqtrade.enums import TradingMode @@ -1362,6 +1363,7 @@ def test_to_json(fee): 'trade_duration': None, 'trade_duration_s': None, 'realized_profit': 0.0, + 'realized_profit_ratio': None, 'close_profit': None, 'close_profit_pct': None, 'close_profit_abs': None, @@ -1438,6 +1440,7 @@ def test_to_json(fee): 'initial_stop_loss_pct': None, 'initial_stop_loss_ratio': None, 'realized_profit': 0.0, + 'realized_profit_ratio': None, 'close_profit': None, 'close_profit_pct': None, 'close_profit_abs': None, @@ -1492,7 +1495,7 @@ def test_stoploss_reinitialization(default_conf, fee): assert trade.stop_loss_pct == -0.05 assert trade.initial_stop_loss == 0.95 assert trade.initial_stop_loss_pct == -0.05 - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() # Lower stoploss @@ -1554,7 +1557,7 @@ def test_stoploss_reinitialization_leverage(default_conf, fee): assert trade.stop_loss_pct == -0.1 assert trade.initial_stop_loss == 0.98 assert trade.initial_stop_loss_pct == -0.1 - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() # Lower stoploss @@ -1616,7 +1619,7 @@ def test_stoploss_reinitialization_short(default_conf, fee): assert trade.stop_loss_pct == -0.1 assert trade.initial_stop_loss == 1.02 assert trade.initial_stop_loss_pct == -0.1 - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() # Lower stoploss Trade.stoploss_reinitialization(-0.15) @@ -1791,17 +1794,17 @@ def test_get_trades_proxy(fee, use_db, is_short): @pytest.mark.usefixtures("init_persistence") @pytest.mark.parametrize('is_short', [True, False]) def test_get_trades__query(fee, is_short): - query = Trade.get_trades([]) + query = Trade.get_trades_query([]) # without orders there should be no join issued. - query1 = Trade.get_trades([], include_orders=False) + query1 = Trade.get_trades_query([], include_orders=False) # Empty "with-options -> default - selectin" assert query._with_options == () assert query1._with_options != () create_mock_trades(fee, is_short) - query = Trade.get_trades([]) - query1 = Trade.get_trades([], include_orders=False) + query = Trade.get_trades_query([]) + query1 = Trade.get_trades_query([], include_orders=False) assert query._with_options == () assert query1._with_options != () @@ -1868,7 +1871,10 @@ def test_get_exit_order_count(fee, is_short): @pytest.mark.usefixtures("init_persistence") -def test_update_order_from_ccxt(caplog): +def test_update_order_from_ccxt(caplog, time_machine): + start = datetime(2023, 1, 1, 4, tzinfo=timezone.utc) + time_machine.move_to(start, tick=False) + # Most basic order return (only has orderid) o = Order.parse_from_ccxt_object({'id': '1234'}, 'ADA/USDT', 'buy', 20.01, 1234.6) assert isinstance(o, Order) @@ -1917,7 +1923,9 @@ def test_update_order_from_ccxt(caplog): assert o.filled == 20.0 assert o.remaining == 0.0 assert not o.ft_is_open - assert o.order_filled_date is not None + assert o.order_filled_date == start + # Move time + time_machine.move_to(start + timedelta(hours=1), tick=False) ccxt_order.update({'id': 'somethingelse'}) with pytest.raises(DependencyException, match=r"Order-id's don't match"): @@ -1930,6 +1938,12 @@ def test_update_order_from_ccxt(caplog): # Call regular update - shouldn't fail. Order.update_orders([o], {'id': '1234'}) + assert o.order_filled_date == start + + # Fill order again - shouldn't update filled date + ccxt_order.update({'id': '1234'}) + Order.update_orders([o], ccxt_order) + assert o.order_filled_date == start @pytest.mark.usefixtures("init_persistence") @@ -2003,6 +2017,7 @@ def test_Trade_object_idem(): 'get_open_trades_without_assigned_fees', 'get_open_order_trades', 'get_trades', + 'get_trades_query', 'get_exit_reason_performance', 'get_enter_tag_performance', 'get_mix_tag_performance', @@ -2429,8 +2444,9 @@ def test_select_filled_orders(fee): def test_order_to_ccxt(limit_buy_order_open): order = Order.parse_from_ccxt_object(limit_buy_order_open, 'mocked', 'buy') - order.query.session.add(order) - Order.query.session.commit() + order.ft_trade_id = 1 + order.session.add(order) + Order.session.commit() order_resp = Order.order_by_id(limit_buy_order_open['id']) assert order_resp @@ -2532,7 +2548,7 @@ def test_recalc_trade_from_orders_dca(data) -> None: leverage=1.0, trading_mode=TradingMode.SPOT ) - Trade.query.session.add(trade) + Trade.session.add(trade) for idx, (order, result) in enumerate(data['orders']): amount = order[1] @@ -2561,11 +2577,11 @@ def test_recalc_trade_from_orders_dca(data) -> None: trade.recalc_trade_from_orders() Trade.commit() - orders1 = Order.query.all() + orders1 = Order.session.scalars(select(Order)).all() assert orders1 assert len(orders1) == idx + 1 - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert len(trade.orders) == idx + 1 if idx < len(data) - 1: @@ -2582,6 +2598,6 @@ def test_recalc_trade_from_orders_dca(data) -> None: assert pytest.approx(trade.close_profit_abs) == data['end_profit'] assert pytest.approx(trade.close_profit) == data['end_profit_ratio'] assert not trade.is_open - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id is None diff --git a/tests/plugins/test_pairlist.py b/tests/plugins/test_pairlist.py index 739c3a7ac..bc8fe84f1 100644 --- a/tests/plugins/test_pairlist.py +++ b/tests/plugins/test_pairlist.py @@ -18,8 +18,8 @@ from freqtrade.persistence import Trade from freqtrade.plugins.pairlist.pairlist_helpers import dynamic_expand_pairlist, expand_pairlist from freqtrade.plugins.pairlistmanager import PairListManager from freqtrade.resolvers import PairListResolver -from tests.conftest import (create_mock_trades_usdt, get_patched_exchange, get_patched_freqtradebot, - log_has, log_has_re, num_log_has) +from tests.conftest import (EXMS, create_mock_trades_usdt, get_patched_exchange, + get_patched_freqtradebot, log_has, log_has_re, num_log_has) # Exclude RemotePairList from tests. @@ -116,7 +116,7 @@ def static_pl_conf(whitelist_conf): def test_log_cached(mocker, static_pl_conf, markets, tickers): - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -139,7 +139,7 @@ def test_log_cached(mocker, static_pl_conf, markets, tickers): def test_load_pairlist_noexist(mocker, markets, default_conf): freqtrade = get_patched_freqtradebot(mocker, default_conf) - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets)) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) plm = PairListManager(freqtrade.exchange, default_conf, MagicMock()) with pytest.raises(OperationalException, match=r"Impossible to load Pairlist 'NonexistingPairList'. " @@ -150,7 +150,7 @@ def test_load_pairlist_noexist(mocker, markets, default_conf): def test_load_pairlist_verify_multi(mocker, markets_static, default_conf): freqtrade = get_patched_freqtradebot(mocker, default_conf) - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets_static)) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets_static)) plm = PairListManager(freqtrade.exchange, default_conf, MagicMock()) # Call different versions one after the other, should always consider what was passed in # and have no side-effects (therefore the same check multiple times) @@ -166,7 +166,7 @@ def test_refresh_market_pair_not_in_whitelist(mocker, markets, static_pl_conf): freqtrade = get_patched_freqtradebot(mocker, static_pl_conf) - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets)) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) freqtrade.pairlists.refresh_pairlist() # List ordered by BaseVolume whitelist = ['ETH/BTC', 'TKN/BTC'] @@ -180,7 +180,7 @@ def test_refresh_market_pair_not_in_whitelist(mocker, markets, static_pl_conf): def test_refresh_static_pairlist(mocker, markets, static_pl_conf): freqtrade = get_patched_freqtradebot(mocker, static_pl_conf) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, exchange_has=MagicMock(return_value=True), markets=PropertyMock(return_value=markets), ) @@ -204,7 +204,7 @@ def test_refresh_static_pairlist_noexist(mocker, markets, static_pl_conf, pairs, static_pl_conf['exchange']['pair_whitelist'] += pairs freqtrade = get_patched_freqtradebot(mocker, static_pl_conf) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, exchange_has=MagicMock(return_value=True), markets=PropertyMock(return_value=markets), ) @@ -221,7 +221,7 @@ def test_invalid_blacklist(mocker, markets, static_pl_conf, caplog): static_pl_conf['exchange']['pair_blacklist'] = ['*/BTC'] freqtrade = get_patched_freqtradebot(mocker, static_pl_conf) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, exchange_has=MagicMock(return_value=True), markets=PropertyMock(return_value=markets), ) @@ -237,7 +237,7 @@ def test_remove_logs_for_pairs_already_in_blacklist(mocker, markets, static_pl_c logger = logging.getLogger(__name__) freqtrade = get_patched_freqtradebot(mocker, static_pl_conf) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, exchange_has=MagicMock(return_value=True), markets=PropertyMock(return_value=markets), ) @@ -264,14 +264,14 @@ def test_remove_logs_for_pairs_already_in_blacklist(mocker, markets, static_pl_c def test_refresh_pairlist_dynamic(mocker, shitcoinmarkets, tickers, whitelist_conf): mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_tickers=tickers, exchange_has=MagicMock(return_value=True), ) freqtrade = get_patched_freqtradebot(mocker, whitelist_conf) # Remock markets with shitcoinmarkets since get_patched_freqtradebot uses the markets fixture mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=shitcoinmarkets), ) # argument: use the whitelist dynamically by exchange-volume @@ -291,7 +291,7 @@ def test_refresh_pairlist_dynamic_2(mocker, shitcoinmarkets, tickers, whitelist_ tickers_dict = tickers() mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, exchange_has=MagicMock(return_value=True), ) # Remove caching of ticker data to emulate changing volume by the time of second call @@ -302,7 +302,7 @@ def test_refresh_pairlist_dynamic_2(mocker, shitcoinmarkets, tickers, whitelist_ freqtrade = get_patched_freqtradebot(mocker, whitelist_conf_2) # Remock markets with shitcoinmarkets since get_patched_freqtradebot uses the markets fixture mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=shitcoinmarkets), ) @@ -320,11 +320,11 @@ def test_refresh_pairlist_dynamic_2(mocker, shitcoinmarkets, tickers, whitelist_ def test_VolumePairList_refresh_empty(mocker, markets_empty, whitelist_conf): mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, exchange_has=MagicMock(return_value=True), ) freqtrade = get_patched_freqtradebot(mocker, whitelist_conf) - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets_empty)) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets_empty)) # argument: use the whitelist dynamically by exchange-volume whitelist = [] @@ -523,15 +523,15 @@ def test_VolumePairList_whitelist_gen(mocker, whitelist_conf, shitcoinmarkets, t ('HOT/BTC', '1d', CandleType.SPOT): ohlcv_history_high_vola, } - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) freqtrade = get_patched_freqtradebot(mocker, whitelist_conf) - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, get_tickers=tickers, markets=PropertyMock(return_value=shitcoinmarkets) ) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, refresh_latest_ohlcv=MagicMock(return_value=ohlcv_data), ) @@ -649,7 +649,7 @@ def test_VolumePairList_range(mocker, whitelist_conf, shitcoinmarkets, tickers, ('HOT/BTC', '1d', CandleType.SPOT): ohlcv_history_high_volume, } - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) if volumefilter_result == 'default_refresh_too_short': with pytest.raises(OperationalException, @@ -675,7 +675,7 @@ def test_VolumePairList_range(mocker, whitelist_conf, shitcoinmarkets, tickers, else: freqtrade = get_patched_freqtradebot(mocker, whitelist_conf) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_tickers=tickers, markets=PropertyMock(return_value=shitcoinmarkets) ) @@ -687,7 +687,7 @@ def test_VolumePairList_range(mocker, whitelist_conf, shitcoinmarkets, tickers, ohlcv_data = [] mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, refresh_latest_ohlcv=MagicMock(return_value=ohlcv_data), ) @@ -702,7 +702,7 @@ def test_PrecisionFilter_error(mocker, whitelist_conf) -> None: whitelist_conf['pairlists'] = [{"method": "StaticPairList"}, {"method": "PrecisionFilter"}] del whitelist_conf['stoploss'] - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) with pytest.raises(OperationalException, match=r"PrecisionFilter can only work with stoploss defined\..*"): @@ -711,9 +711,9 @@ def test_PrecisionFilter_error(mocker, whitelist_conf) -> None: def test_PerformanceFilter_error(mocker, whitelist_conf, caplog) -> None: whitelist_conf['pairlists'] = [{"method": "StaticPairList"}, {"method": "PerformanceFilter"}] - if hasattr(Trade, 'query'): - del Trade.query - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + if hasattr(Trade, 'session'): + del Trade.session + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) exchange = get_patched_exchange(mocker, whitelist_conf) pm = PairListManager(exchange, whitelist_conf, MagicMock()) pm.refresh_pairlist() @@ -755,7 +755,7 @@ def test_PerformanceFilter_lookback(mocker, default_conf_usdt, fee, caplog) -> N {"method": "StaticPairList"}, {"method": "PerformanceFilter", "minutes": 60, "min_profit": 0.01} ] - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) exchange = get_patched_exchange(mocker, default_conf_usdt) pm = PairListManager(exchange, default_conf_usdt) pm.refresh_pairlist() @@ -781,7 +781,7 @@ def test_PerformanceFilter_keep_mid_order(mocker, default_conf_usdt, fee, caplog {"method": "StaticPairList", "allow_inactive": True}, {"method": "PerformanceFilter", "minutes": 60, } ] - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) exchange = get_patched_exchange(mocker, default_conf_usdt) pm = PairListManager(exchange, default_conf_usdt) pm.refresh_pairlist() @@ -806,7 +806,7 @@ def test_PerformanceFilter_keep_mid_order(mocker, default_conf_usdt, fee, caplog def test_gen_pair_whitelist_not_supported(mocker, default_conf, tickers) -> None: default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 10}] - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, get_tickers=tickers, exchange_has=MagicMock(return_value=False), ) @@ -819,7 +819,7 @@ def test_gen_pair_whitelist_not_supported(mocker, default_conf, tickers) -> None def test_pair_whitelist_not_supported_Spread(mocker, default_conf, tickers) -> None: default_conf['pairlists'] = [{'method': 'StaticPairList'}, {'method': 'SpreadFilter'}] - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, get_tickers=tickers, exchange_has=MagicMock(return_value=False), ) @@ -828,11 +828,17 @@ def test_pair_whitelist_not_supported_Spread(mocker, default_conf, tickers) -> N match=r'Exchange does not support fetchTickers, .*'): get_patched_freqtradebot(mocker, default_conf) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.get_option', MagicMock(return_value=False)) + with pytest.raises(OperationalException, + match=r'.*requires exchange to have bid/ask data'): + get_patched_freqtradebot(mocker, default_conf) + @pytest.mark.parametrize("pairlist", TESTABLE_PAIRLISTS) def test_pairlist_class(mocker, whitelist_conf, markets, pairlist): whitelist_conf['pairlists'][0]['method'] = pairlist - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True) ) @@ -861,7 +867,7 @@ def test_pairlist_class(mocker, whitelist_conf, markets, pairlist): def test__whitelist_for_active_markets(mocker, whitelist_conf, markets, pairlist, whitelist, caplog, log_message, tickers): whitelist_conf['pairlists'][0]['method'] = pairlist - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -881,10 +887,10 @@ def test__whitelist_for_active_markets(mocker, whitelist_conf, markets, pairlist def test__whitelist_for_active_markets_empty(mocker, whitelist_conf, pairlist, tickers): whitelist_conf['pairlists'][0]['method'] = pairlist - mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) freqtrade = get_patched_freqtradebot(mocker, whitelist_conf) - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=None), get_tickers=tickers ) @@ -897,7 +903,7 @@ def test__whitelist_for_active_markets_empty(mocker, whitelist_conf, pairlist, t def test_volumepairlist_invalid_sortvalue(mocker, whitelist_conf): whitelist_conf['pairlists'][0].update({"sort_key": "asdf"}) - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) with pytest.raises(OperationalException, match=r"key asdf not in .*"): get_patched_freqtradebot(mocker, whitelist_conf) @@ -905,7 +911,7 @@ def test_volumepairlist_invalid_sortvalue(mocker, whitelist_conf): def test_volumepairlist_caching(mocker, markets, whitelist_conf, tickers): - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -925,7 +931,7 @@ def test_agefilter_min_days_listed_too_small(mocker, default_conf, markets, tick default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 10}, {'method': 'AgeFilter', 'min_days_listed': -1}] - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -941,7 +947,7 @@ def test_agefilter_max_days_lower_than_min_days(mocker, default_conf, markets, t {'method': 'AgeFilter', 'min_days_listed': 3, "max_days_listed": 2}] - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -956,7 +962,7 @@ def test_agefilter_min_days_listed_too_large(mocker, default_conf, markets, tick default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 10}, {'method': 'AgeFilter', 'min_days_listed': 99999}] - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -976,7 +982,7 @@ def test_agefilter_caching(mocker, markets, whitelist_conf_agefilter, tickers, o ('LTC/BTC', '1d', CandleType.SPOT): ohlcv_history, } mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers, @@ -1000,14 +1006,14 @@ def test_agefilter_caching(mocker, markets, whitelist_conf_agefilter, tickers, o ('LTC/BTC', '1d', CandleType.SPOT): ohlcv_history, ('XRP/BTC', '1d', CandleType.SPOT): ohlcv_history.iloc[[0]], } - mocker.patch('freqtrade.exchange.Exchange.refresh_latest_ohlcv', return_value=ohlcv_data) + mocker.patch(f'{EXMS}.refresh_latest_ohlcv', return_value=ohlcv_data) freqtrade.pairlists.refresh_pairlist() assert len(freqtrade.pairlists.whitelist) == 3 assert freqtrade.exchange.refresh_latest_ohlcv.call_count == 1 # Move to next day t.move_to("2021-09-02 01:00:00 +00:00") - mocker.patch('freqtrade.exchange.Exchange.refresh_latest_ohlcv', return_value=ohlcv_data) + mocker.patch(f'{EXMS}.refresh_latest_ohlcv', return_value=ohlcv_data) freqtrade.pairlists.refresh_pairlist() assert len(freqtrade.pairlists.whitelist) == 3 assert freqtrade.exchange.refresh_latest_ohlcv.call_count == 1 @@ -1021,7 +1027,7 @@ def test_agefilter_caching(mocker, markets, whitelist_conf_agefilter, tickers, o ('LTC/BTC', '1d', CandleType.SPOT): ohlcv_history, ('XRP/BTC', '1d', CandleType.SPOT): ohlcv_history, } - mocker.patch('freqtrade.exchange.Exchange.refresh_latest_ohlcv', return_value=ohlcv_data) + mocker.patch(f'{EXMS}.refresh_latest_ohlcv', return_value=ohlcv_data) freqtrade.pairlists.refresh_pairlist() assert len(freqtrade.pairlists.whitelist) == 4 # Called once (only for XRP/BTC) @@ -1033,7 +1039,7 @@ def test_OffsetFilter_error(mocker, whitelist_conf) -> None: [{"method": "StaticPairList"}, {"method": "OffsetFilter", "offset": -1}] ) - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) with pytest.raises(OperationalException, match=r'OffsetFilter requires offset to be >= 0'): @@ -1044,7 +1050,7 @@ def test_rangestabilityfilter_checks(mocker, default_conf, markets, tickers): default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 10}, {'method': 'RangeStabilityFilter', 'lookback_days': 99999}] - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -1074,7 +1080,7 @@ def test_rangestabilityfilter_caching(mocker, markets, default_conf, tickers, oh 'min_rate_of_change': min_rate_of_change, "max_rate_of_change": max_rate_of_change}] - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -1088,7 +1094,7 @@ def test_rangestabilityfilter_caching(mocker, markets, default_conf, tickers, oh ('BLK/BTC', '1d', CandleType.SPOT): ohlcv_history, } mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, refresh_latest_ohlcv=MagicMock(return_value=ohlcv_data), ) @@ -1109,7 +1115,7 @@ def test_spreadfilter_invalid_data(mocker, default_conf, markets, tickers, caplo default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 10}, {'method': 'SpreadFilter', 'max_spread_ratio': 0.1}] - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), get_tickers=tickers @@ -1123,7 +1129,7 @@ def test_spreadfilter_invalid_data(mocker, default_conf, markets, tickers, caplo tickers.return_value['ETH/BTC']['ask'] = 0.0 del tickers.return_value['TKN/BTC'] del tickers.return_value['LTC/BTC'] - mocker.patch.multiple('freqtrade.exchange.Exchange', get_tickers=tickers) + mocker.patch.multiple(EXMS, get_tickers=tickers) ftbot.pairlists.refresh_pairlist() assert log_has_re(r'Removed .* invalid ticker data.*', caplog) @@ -1197,7 +1203,7 @@ def test_spreadfilter_invalid_data(mocker, default_conf, markets, tickers, caplo ]) def test_pricefilter_desc(mocker, whitelist_conf, markets, pairlistconfig, desc_expected, exception_expected): - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True) ) @@ -1214,7 +1220,7 @@ def test_pricefilter_desc(mocker, whitelist_conf, markets, pairlistconfig, def test_pairlistmanager_no_pairlist(mocker, whitelist_conf): - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) whitelist_conf['pairlists'] = [] @@ -1266,14 +1272,14 @@ def test_performance_filter(mocker, whitelist_conf, pairlists, pair_allowlist, o allowlist_conf['pairlists'] = pairlists allowlist_conf['exchange']['pair_whitelist'] = pair_allowlist - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) freqtrade = get_patched_freqtradebot(mocker, allowlist_conf) - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, get_tickers=tickers, markets=PropertyMock(return_value=markets) ) - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch.multiple(EXMS, get_historic_ohlcv=MagicMock(return_value=ohlcv_history_list), ) mocker.patch.multiple('freqtrade.persistence.Trade', @@ -1371,7 +1377,7 @@ def test_expand_pairlist_keep_invalid(wildcardlist, pairs, expected): def test_ProducerPairlist_no_emc(mocker, whitelist_conf): - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) whitelist_conf['pairlists'] = [ { @@ -1388,8 +1394,8 @@ def test_ProducerPairlist_no_emc(mocker, whitelist_conf): def test_ProducerPairlist(mocker, whitelist_conf, markets): - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) - mocker.patch.multiple('freqtrade.exchange.Exchange', + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) + mocker.patch.multiple(EXMS, markets=PropertyMock(return_value=markets), exchange_has=MagicMock(return_value=True), ) diff --git a/tests/plugins/test_pairlocks.py b/tests/plugins/test_pairlocks.py index 0ba9bb746..6b7112f98 100644 --- a/tests/plugins/test_pairlocks.py +++ b/tests/plugins/test_pairlocks.py @@ -14,7 +14,7 @@ def test_PairLocks(use_db): PairLocks.use_db = use_db # No lock should be present if use_db: - assert len(PairLock.query.all()) == 0 + assert len(PairLock.get_all_locks().all()) == 0 assert PairLocks.use_db == use_db @@ -88,13 +88,13 @@ def test_PairLocks(use_db): if use_db: locks = PairLocks.get_all_locks() - locks_db = PairLock.query.all() + locks_db = PairLock.get_all_locks().all() assert len(locks) == len(locks_db) assert len(locks_db) > 0 else: # Nothing was pushed to the database assert len(PairLocks.get_all_locks()) > 0 - assert len(PairLock.query.all()) == 0 + assert len(PairLock.get_all_locks().all()) == 0 # Reset use-db variable PairLocks.reset_locks() PairLocks.use_db = True @@ -107,7 +107,7 @@ def test_PairLocks_getlongestlock(use_db): # No lock should be present PairLocks.use_db = use_db if use_db: - assert len(PairLock.query.all()) == 0 + assert len(PairLock.get_all_locks().all()) == 0 assert PairLocks.use_db == use_db @@ -139,7 +139,7 @@ def test_PairLocks_reason(use_db): PairLocks.use_db = use_db # No lock should be present if use_db: - assert len(PairLock.query.all()) == 0 + assert len(PairLock.get_all_locks().all()) == 0 assert PairLocks.use_db == use_db diff --git a/tests/plugins/test_protections.py b/tests/plugins/test_protections.py index 2bbdf3d4f..5e6128c73 100644 --- a/tests/plugins/test_protections.py +++ b/tests/plugins/test_protections.py @@ -74,7 +74,7 @@ def generate_mock_trade(pair: str, fee: float, is_open: bool, trade.close(close_price) trade.exit_reason = exit_reason - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() return trade diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 4871d9b24..4e2dc94ae 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -1,12 +1,10 @@ -# pragma pylint: disable=missing-docstring, C0103 -# pragma pylint: disable=invalid-sequence-index, invalid-name, too-many-arguments - from copy import deepcopy from datetime import datetime, timedelta, timezone from unittest.mock import ANY, MagicMock, PropertyMock import pytest from numpy import isnan +from sqlalchemy import select from freqtrade.edge import PairInfo from freqtrade.enums import SignalDirection, State, TradingMode @@ -15,19 +13,10 @@ from freqtrade.persistence import Trade from freqtrade.persistence.pairlock_middleware import PairLocks from freqtrade.rpc import RPC, RPCException from freqtrade.rpc.fiat_convert import CryptoToFiatConverter -from tests.conftest import (create_mock_trades, create_mock_trades_usdt, get_patched_freqtradebot, - patch_get_signal) +from tests.conftest import (EXMS, create_mock_trades, create_mock_trades_usdt, + get_patched_freqtradebot, patch_get_signal) -# Functions for recurrent object patching -def prec_satoshi(a, b) -> float: - """ - :return: True if A and B differs less than one satoshi. - """ - return abs(a - b) < 0.00000001 - - -# Unit tests def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: gen_response = { 'trade_id': 1, @@ -62,15 +51,12 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: 'amount': 91.07468123, 'amount_requested': 91.07468124, 'stake_amount': 0.001, - 'max_stake_amount': ANY, + 'max_stake_amount': None, 'trade_duration': None, 'trade_duration_s': None, 'close_profit': None, 'close_profit_pct': None, 'close_profit_abs': None, - 'current_profit': -0.00408133, - 'current_profit_pct': -0.41, - 'current_profit_abs': -4.09e-06, 'profit_ratio': -0.00408133, 'profit_pct': -0.41, 'profit_abs': -4.09e-06, @@ -91,6 +77,10 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: 'stoploss_entry_dist_ratio': -0.10376381, 'open_order': None, 'realized_profit': 0.0, + 'realized_profit_ratio': None, + 'total_profit_abs': -4.09e-06, + 'total_profit_fiat': ANY, + 'total_profit_ratio': None, 'exchange': 'binance', 'leverage': 1.0, 'interest_rate': 0.0, @@ -109,10 +99,10 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: } mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(side_effect=[False, True]), + _dry_is_price_crossed=MagicMock(side_effect=[False, True]), ) freqtradebot = get_patched_freqtradebot(mocker, default_conf) @@ -134,20 +124,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: 'profit_ratio': 0.0, 'profit_pct': 0.0, 'profit_abs': 0.0, - 'current_profit': 0.0, - 'current_profit_pct': 0.0, - 'current_profit_abs': 0.0, - 'stop_loss_abs': 0.0, - 'stop_loss_pct': None, - 'stop_loss_ratio': None, - 'stoploss_current_dist': -1.099e-05, - 'stoploss_current_dist_ratio': -1.0, - 'stoploss_current_dist_pct': pytest.approx(-100.0), - 'stoploss_entry_dist': -0.0010025, - 'stoploss_entry_dist_ratio': -1.0, - 'initial_stop_loss_abs': 0.0, - 'initial_stop_loss_pct': None, - 'initial_stop_loss_ratio': None, + 'total_profit_abs': 0.0, 'open_order': '(limit buy rem=91.07468123)', }) response_unfilled['orders'][0].update({ @@ -182,12 +159,16 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: results = rpc._rpc_trade_status() response = deepcopy(gen_response) + response.update({ + 'max_stake_amount': 0.001, + 'total_profit_ratio': pytest.approx(-0.00409), + }) assert results[0] == response - mocker.patch('freqtrade.exchange.Exchange.get_rate', + mocker.patch(f'{EXMS}.get_rate', MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available"))) results = rpc._rpc_trade_status() - assert isnan(results[0]['current_profit']) + assert isnan(results[0]['profit_ratio']) assert isnan(results[0]['current_rate']) response_norate = deepcopy(gen_response) # Update elements that are NaN when no rate is available. @@ -195,12 +176,12 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: 'stoploss_current_dist': ANY, 'stoploss_current_dist_ratio': ANY, 'stoploss_current_dist_pct': ANY, + 'max_stake_amount': 0.001, 'profit_ratio': ANY, 'profit_pct': ANY, 'profit_abs': ANY, - 'current_profit_abs': ANY, - 'current_profit': ANY, - 'current_profit_pct': ANY, + 'total_profit_abs': ANY, + 'total_profit_ratio': ANY, 'current_rate': ANY, }) assert results[0] == response_norate @@ -214,7 +195,7 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -226,7 +207,7 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None: freqtradebot.state = State.RUNNING with pytest.raises(RPCException, match=r'.*no active trade*'): rpc._rpc_status_table(default_conf['stake_currency'], 'USD') - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=False) + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=False) freqtradebot.enter_positions() result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') @@ -237,7 +218,7 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None: assert '0.00' == result[0][3] assert isnan(fiat_profit_sum) - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=True) + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=True) freqtradebot.process() result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') @@ -248,7 +229,7 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None: assert '-0.41%' == result[0][3] assert isnan(fiat_profit_sum) - # Test with fiatconvert + # Test with fiat convert rpc._fiat_converter = CryptoToFiatConverter() result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') assert "Since" in headers @@ -268,7 +249,7 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None: # 3 on top of the initial one. assert result[0][4] == '1/4' - mocker.patch('freqtrade.exchange.Exchange.get_rate', + mocker.patch(f'{EXMS}.get_rate', MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available"))) result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') assert 'instantly' == result[0][2] @@ -281,7 +262,7 @@ def test__rpc_timeunit_profit(default_conf_usdt, ticker, fee, limit_buy_order, limit_sell_order, markets, mocker) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, markets=PropertyMock(return_value=markets) @@ -322,7 +303,7 @@ def test__rpc_timeunit_profit(default_conf_usdt, ticker, fee, def test_rpc_trade_history(mocker, default_conf, markets, fee, is_short): mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets) ) @@ -350,7 +331,7 @@ def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog, is_short): stoploss_mock = MagicMock() cancel_mock = MagicMock() mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets), cancel_order=cancel_mock, cancel_stoploss_order=stoploss_mock, @@ -363,7 +344,7 @@ def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog, is_short): with pytest.raises(RPCException, match='invalid argument'): rpc._rpc_delete('200') - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() trades[1].stoploss_order_id = '1234' trades[2].stoploss_order_id = '1234' assert len(trades) > 2 @@ -384,15 +365,13 @@ def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog, is_short): assert stoploss_mock.call_count == 1 assert res['cancel_order_count'] == 2 - stoploss_mock = mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order', - side_effect=InvalidOrderException) + stoploss_mock = mocker.patch(f'{EXMS}.cancel_stoploss_order', side_effect=InvalidOrderException) res = rpc._rpc_delete('3') assert stoploss_mock.call_count == 1 stoploss_mock.reset_mock() - cancel_mock = mocker.patch('freqtrade.exchange.Exchange.cancel_order', - side_effect=InvalidOrderException) + cancel_mock = mocker.patch(f'{EXMS}.cancel_order', side_effect=InvalidOrderException) res = rpc._rpc_delete('4') assert cancel_mock.call_count == 1 @@ -403,7 +382,7 @@ def test_rpc_trade_statistics(default_conf_usdt, ticker, fee, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=1.1) mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -440,7 +419,7 @@ def test_rpc_trade_statistics(default_conf_usdt, ticker, fee, mocker) -> None: assert stats['best_rate'] == 10.0 # Test non-available pair - mocker.patch('freqtrade.exchange.Exchange.get_rate', + mocker.patch(f'{EXMS}.get_rate', MagicMock(side_effect=ExchangeError("Pair 'XRP/USDT' not available"))) stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency) assert stats['trade_count'] == 7 @@ -474,7 +453,7 @@ def test_rpc_balance_handle_error(default_conf, mocker): mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=mock_balance), get_tickers=MagicMock(side_effect=TemporaryError('Could not load ticker due to xxx')) ) @@ -537,7 +516,7 @@ def test_rpc_balance_handle(default_conf, mocker, tickers): mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, validate_trading_mode_and_margin_mode=MagicMock(), get_balances=MagicMock(return_value=mock_balance), fetch_positions=MagicMock(return_value=mock_pos), @@ -553,8 +532,8 @@ def test_rpc_balance_handle(default_conf, mocker, tickers): rpc._fiat_converter = CryptoToFiatConverter() result = rpc._rpc_balance(default_conf['stake_currency'], default_conf['fiat_display_currency']) - assert prec_satoshi(result['total'], 30.30909624) - assert prec_satoshi(result['value'], 454636.44360691) + assert pytest.approx(result['total']) == 30.30909624 + assert pytest.approx(result['value']) == 454636.44360691 assert tickers.call_count == 1 assert tickers.call_args_list[0][1]['cached'] is True assert 'USD' == result['symbol'] @@ -614,7 +593,7 @@ def test_rpc_balance_handle(default_conf, mocker, tickers): def test_rpc_start(mocker, default_conf) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock() ) @@ -635,7 +614,7 @@ def test_rpc_start(mocker, default_conf) -> None: def test_rpc_stop(mocker, default_conf) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock() ) @@ -657,7 +636,7 @@ def test_rpc_stop(mocker, default_conf) -> None: def test_rpc_stopentry(mocker, default_conf) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock() ) @@ -677,7 +656,7 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None: cancel_order_mock = MagicMock() mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, cancel_order=cancel_order_mock, fetch_order=MagicMock( @@ -688,7 +667,7 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None: 'filled': 0.0, } ), - _is_dry_limit_order_filled=MagicMock(return_value=True), + _dry_is_price_crossed=MagicMock(return_value=True), get_fee=fee, ) mocker.patch('freqtrade.wallets.Wallets.get_free', return_value=1000) @@ -725,15 +704,14 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None: freqtradebot.state = State.RUNNING assert cancel_order_mock.call_count == 0 - mocker.patch( - 'freqtrade.exchange.Exchange._is_dry_limit_order_filled', MagicMock(return_value=False)) + mocker.patch(f'{EXMS}._dry_is_price_crossed', MagicMock(return_value=False)) freqtradebot.enter_positions() # make an limit-buy open trade - trade = Trade.query.filter(Trade.id == '3').first() + trade = Trade.session.scalars(select(Trade).filter(Trade.id == '3')).first() filled_amount = trade.amount / 2 # Fetch order - it's open first, and closed after cancel_order is called. mocker.patch( - 'freqtrade.exchange.Exchange.fetch_order', + f'{EXMS}.fetch_order', side_effect=[{ 'id': trade.orders[0].order_id, 'status': 'open', @@ -755,7 +733,7 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None: assert pytest.approx(trade.amount) == filled_amount mocker.patch( - 'freqtrade.exchange.Exchange.fetch_order', + f'{EXMS}.fetch_order', return_value={ 'status': 'open', 'type': 'limit', @@ -765,11 +743,11 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None: freqtradebot.config['max_open_trades'] = 3 freqtradebot.enter_positions() - trade = Trade.query.filter(Trade.id == '2').first() + trade = Trade.session.scalars(select(Trade).filter(Trade.id == '2')).first() amount = trade.amount # make an limit-buy open trade, if there is no 'filled', don't sell it mocker.patch( - 'freqtrade.exchange.Exchange.fetch_order', + f'{EXMS}.fetch_order', return_value={ 'status': 'open', 'type': 'limit', @@ -783,11 +761,11 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None: assert cancel_order_mock.call_count == 2 assert trade.amount == amount - trade = Trade.query.filter(Trade.id == '3').first() + trade = Trade.session.scalars(select(Trade).filter(Trade.id == '3')).first() # make an limit-sell open trade mocker.patch( - 'freqtrade.exchange.Exchange.fetch_order', + f'{EXMS}.fetch_order', return_value={ 'status': 'open', 'type': 'limit', @@ -807,7 +785,7 @@ def test_rpc_force_exit(default_conf, ticker, fee, mocker) -> None: def test_performance_handle(default_conf_usdt, ticker, fee, mocker) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -830,7 +808,7 @@ def test_enter_tag_performance_handle(default_conf, ticker, fee, mocker) -> None mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -862,7 +840,7 @@ def test_enter_tag_performance_handle(default_conf, ticker, fee, mocker) -> None def test_enter_tag_performance_handle_2(mocker, default_conf, markets, fee): mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets) ) @@ -875,23 +853,23 @@ def test_enter_tag_performance_handle_2(mocker, default_conf, markets, fee): assert len(res) == 2 assert res[0]['enter_tag'] == 'TEST1' assert res[0]['count'] == 1 - assert prec_satoshi(res[0]['profit_pct'], 0.5) + assert pytest.approx(res[0]['profit_pct']) == 0.5 assert res[1]['enter_tag'] == 'Other' assert res[1]['count'] == 1 - assert prec_satoshi(res[1]['profit_pct'], 1.0) + assert pytest.approx(res[1]['profit_pct']) == 1.0 # Test for a specific pair res = rpc._rpc_enter_tag_performance('ETC/BTC') assert len(res) == 1 assert res[0]['count'] == 1 assert res[0]['enter_tag'] == 'TEST1' - assert prec_satoshi(res[0]['profit_pct'], 0.5) + assert pytest.approx(res[0]['profit_pct']) == 0.5 def test_exit_reason_performance_handle(default_conf_usdt, ticker, fee, mocker) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -918,7 +896,7 @@ def test_exit_reason_performance_handle(default_conf_usdt, ticker, fee, mocker) def test_exit_reason_performance_handle_2(mocker, default_conf, markets, fee): mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets) ) @@ -931,23 +909,23 @@ def test_exit_reason_performance_handle_2(mocker, default_conf, markets, fee): assert len(res) == 2 assert res[0]['exit_reason'] == 'sell_signal' assert res[0]['count'] == 1 - assert prec_satoshi(res[0]['profit_pct'], 0.5) + assert pytest.approx(res[0]['profit_pct']) == 0.5 assert res[1]['exit_reason'] == 'roi' assert res[1]['count'] == 1 - assert prec_satoshi(res[1]['profit_pct'], 1.0) + assert pytest.approx(res[1]['profit_pct']) == 1.0 # Test for a specific pair res = rpc._rpc_exit_reason_performance('ETC/BTC') assert len(res) == 1 assert res[0]['count'] == 1 assert res[0]['exit_reason'] == 'sell_signal' - assert prec_satoshi(res[0]['profit_pct'], 0.5) + assert pytest.approx(res[0]['profit_pct']) == 0.5 def test_mix_tag_performance_handle(default_conf, ticker, fee, mocker) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -971,7 +949,7 @@ def test_mix_tag_performance_handle(default_conf, ticker, fee, mocker) -> None: def test_mix_tag_performance_handle_2(mocker, default_conf, markets, fee): mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets) ) @@ -984,10 +962,10 @@ def test_mix_tag_performance_handle_2(mocker, default_conf, markets, fee): assert len(res) == 2 assert res[0]['mix_tag'] == 'TEST1 sell_signal' assert res[0]['count'] == 1 - assert prec_satoshi(res[0]['profit_pct'], 0.5) + assert pytest.approx(res[0]['profit_pct']) == 0.5 assert res[1]['mix_tag'] == 'Other roi' assert res[1]['count'] == 1 - assert prec_satoshi(res[1]['profit_pct'], 1.0) + assert pytest.approx(res[1]['profit_pct']) == 1.0 # Test for a specific pair res = rpc._rpc_mix_tag_performance('ETC/BTC') @@ -995,13 +973,13 @@ def test_mix_tag_performance_handle_2(mocker, default_conf, markets, fee): assert len(res) == 1 assert res[0]['count'] == 1 assert res[0]['mix_tag'] == 'TEST1 sell_signal' - assert prec_satoshi(res[0]['profit_pct'], 0.5) + assert pytest.approx(res[0]['profit_pct']) == 0.5 def test_rpc_count(mocker, default_conf, ticker, fee) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -1026,7 +1004,7 @@ def test_rpc_force_entry(mocker, default_conf, ticker, fee, limit_buy_order_open mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) buy_mm = MagicMock(return_value=limit_buy_order_open) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -1155,7 +1133,7 @@ def test_rpc_whitelist_dynamic(mocker, default_conf) -> None: default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 4, }] - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) freqtradebot = get_patched_freqtradebot(mocker, default_conf) @@ -1252,6 +1230,6 @@ def test_rpc_health(mocker, default_conf) -> None: freqtradebot = get_patched_freqtradebot(mocker, default_conf) rpc = RPC(freqtradebot) - result = rpc._health() - assert result['last_process'] == '1970-01-01 00:00:00+00:00' - assert result['last_process_ts'] == 0 + result = rpc.health() + assert result['last_process'] is None + assert result['last_process_ts'] is None diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 5b452627b..97319b78b 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -14,6 +14,7 @@ from fastapi import FastAPI, WebSocketDisconnect from fastapi.exceptions import HTTPException from fastapi.testclient import TestClient from requests.auth import _basic_auth_str +from sqlalchemy import select from freqtrade.__init__ import __version__ from freqtrade.enums import CandleType, RunMode, State, TradingMode @@ -24,7 +25,7 @@ from freqtrade.rpc import RPC from freqtrade.rpc.api_server import ApiServer from freqtrade.rpc.api_server.api_auth import create_token, get_user_from_token from freqtrade.rpc.api_server.uvicorn_threaded import UvicornServer -from tests.conftest import (CURRENT_TEST_STRATEGY, create_mock_trades, get_mock_coro, +from tests.conftest import (CURRENT_TEST_STRATEGY, EXMS, create_mock_trades, get_mock_coro, get_patched_freqtradebot, log_has, log_has_re, patch_get_signal) @@ -473,9 +474,9 @@ def test_api_balance(botclient, mocker, rpc_balance, tickers): ftbot, client = botclient ftbot.config['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value=rpc_balance) - mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers) - mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination', + mocker.patch(f'{EXMS}.get_balances', return_value=rpc_balance) + mocker.patch(f'{EXMS}.get_tickers', tickers) + mocker.patch(f'{EXMS}.get_valid_pair_combination', side_effect=lambda a, b: f"{a}/{b}") ftbot.wallets.update() @@ -507,7 +508,7 @@ def test_api_count(botclient, mocker, ticker, fee, markets, is_short): ftbot, client = botclient patch_get_signal(ftbot) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -594,7 +595,7 @@ def test_api_daily(botclient, mocker, ticker, fee, markets): ftbot, client = botclient patch_get_signal(ftbot) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -613,7 +614,7 @@ def test_api_trades(botclient, mocker, fee, markets, is_short): ftbot, client = botclient patch_get_signal(ftbot) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets) ) rc = client_get(client, f"{BASE_URI}/trades") @@ -624,7 +625,7 @@ def test_api_trades(botclient, mocker, fee, markets, is_short): assert rc.json()['offset'] == 0 create_mock_trades(fee, is_short=is_short) - Trade.query.session.flush() + Trade.session.flush() rc = client_get(client, f"{BASE_URI}/trades") assert_response(rc) @@ -644,7 +645,7 @@ def test_api_trade_single(botclient, mocker, fee, ticker, markets, is_short): ftbot, client = botclient patch_get_signal(ftbot, enter_long=not is_short, enter_short=is_short) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets), fetch_ticker=ticker, ) @@ -652,7 +653,7 @@ def test_api_trade_single(botclient, mocker, fee, ticker, markets, is_short): assert_response(rc, 404) assert rc.json()['detail'] == 'Trade not found.' - Trade.query.session.rollback() + Trade.rollback() create_mock_trades(fee, is_short=is_short) rc = client_get(client, f"{BASE_URI}/trade/3") @@ -668,7 +669,7 @@ def test_api_delete_trade(botclient, mocker, fee, markets, is_short): stoploss_mock = MagicMock() cancel_mock = MagicMock() mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, markets=PropertyMock(return_value=markets), cancel_order=cancel_mock, cancel_stoploss_order=stoploss_mock, @@ -677,7 +678,7 @@ def test_api_delete_trade(botclient, mocker, fee, markets, is_short): create_mock_trades(fee, is_short=is_short) ftbot.strategy.order_types['stoploss_on_exchange'] = True - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() trades[1].stoploss_order_id = '1234' Trade.commit() assert len(trades) > 2 @@ -685,7 +686,7 @@ def test_api_delete_trade(botclient, mocker, fee, markets, is_short): rc = client_delete(client, f"{BASE_URI}/trades/1") assert_response(rc) assert rc.json()['result_msg'] == 'Deleted trade 1. Closed 1 open orders.' - assert len(trades) - 1 == len(Trade.query.all()) + assert len(trades) - 1 == len(Trade.session.scalars(select(Trade)).all()) assert cancel_mock.call_count == 1 cancel_mock.reset_mock() @@ -694,11 +695,11 @@ def test_api_delete_trade(botclient, mocker, fee, markets, is_short): assert_response(rc, 502) assert cancel_mock.call_count == 0 - assert len(trades) - 1 == len(Trade.query.all()) + assert len(trades) - 1 == len(Trade.session.scalars(select(Trade)).all()) rc = client_delete(client, f"{BASE_URI}/trades/2") assert_response(rc) assert rc.json()['result_msg'] == 'Deleted trade 2. Closed 2 open orders.' - assert len(trades) - 2 == len(Trade.query.all()) + assert len(trades) - 2 == len(Trade.session.scalars(select(Trade)).all()) assert stoploss_mock.call_count == 1 rc = client_delete(client, f"{BASE_URI}/trades/502") @@ -706,6 +707,44 @@ def test_api_delete_trade(botclient, mocker, fee, markets, is_short): assert_response(rc, 502) +@pytest.mark.parametrize('is_short', [True, False]) +def test_api_delete_open_order(botclient, mocker, fee, markets, ticker, is_short): + ftbot, client = botclient + patch_get_signal(ftbot, enter_long=not is_short, enter_short=is_short) + stoploss_mock = MagicMock() + cancel_mock = MagicMock() + mocker.patch.multiple( + EXMS, + markets=PropertyMock(return_value=markets), + fetch_ticker=ticker, + cancel_order=cancel_mock, + cancel_stoploss_order=stoploss_mock, + ) + + rc = client_delete(client, f"{BASE_URI}/trades/10/open-order") + assert_response(rc, 502) + assert 'Invalid trade_id.' in rc.json()['error'] + + create_mock_trades(fee, is_short=is_short) + Trade.commit() + + rc = client_delete(client, f"{BASE_URI}/trades/5/open-order") + assert_response(rc, 502) + assert 'No open order for trade_id' in rc.json()['error'] + trade = Trade.get_trades([Trade.id == 6]).first() + mocker.patch(f'{EXMS}.fetch_order', side_effect=ExchangeError) + rc = client_delete(client, f"{BASE_URI}/trades/6/open-order") + assert_response(rc, 502) + assert 'Order not found.' in rc.json()['error'] + + trade = Trade.get_trades([Trade.id == 6]).first() + mocker.patch(f'{EXMS}.fetch_order', return_value=trade.orders[-1].to_ccxt_object()) + + rc = client_delete(client, f"{BASE_URI}/trades/6/open-order") + assert_response(rc) + assert cancel_mock.call_count == 1 + + def test_api_logs(botclient): ftbot, client = botclient rc = client_get(client, f"{BASE_URI}/logs") @@ -742,7 +781,7 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets): ftbot, client = botclient patch_get_signal(ftbot) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -804,7 +843,7 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, is_short, expected) ftbot, client = botclient patch_get_signal(ftbot) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -862,7 +901,7 @@ def test_api_stats(botclient, mocker, ticker, fee, markets, is_short): ftbot, client = botclient patch_get_signal(ftbot, enter_long=not is_short, enter_short=is_short) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -905,7 +944,7 @@ def test_api_performance(botclient, fee): ) trade.close_profit = trade.calc_profit_ratio(trade.close_rate) trade.close_profit_abs = trade.calc_profit(trade.close_rate) - Trade.query.session.add(trade) + Trade.session.add(trade) trade = Trade( pair='XRP/ETH', @@ -922,7 +961,7 @@ def test_api_performance(botclient, fee): trade.close_profit = trade.calc_profit_ratio(trade.close_rate) trade.close_profit_abs = trade.calc_profit(trade.close_rate) - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() rc = client_get(client, f"{BASE_URI}/performance") @@ -943,7 +982,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets, is_short, ftbot, client = botclient patch_get_signal(ftbot) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, @@ -968,13 +1007,15 @@ def test_api_status(botclient, mocker, ticker, fee, markets, is_short, 'close_profit_pct': None, 'close_profit_abs': None, 'close_rate': None, - 'current_profit': ANY, - 'current_profit_pct': ANY, - 'current_profit_abs': ANY, 'profit_ratio': ANY, 'profit_pct': ANY, 'profit_abs': ANY, 'profit_fiat': ANY, + 'total_profit_abs': ANY, + 'total_profit_fiat': ANY, + 'total_profit_ratio': ANY, + 'realized_profit': 0.0, + 'realized_profit_ratio': None, 'current_rate': current_rate, 'open_date': ANY, 'open_timestamp': ANY, @@ -1028,7 +1069,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets, is_short, 'orders': [ANY], } - mocker.patch('freqtrade.exchange.Exchange.get_rate', + mocker.patch(f'{EXMS}.get_rate', MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available"))) rc = client_get(client, f"{BASE_URI}/status") @@ -1141,7 +1182,7 @@ def test_api_force_entry(botclient, mocker, fee, endpoint): ftbot.config['force_entry_enable'] = True fbuy_mock = MagicMock(return_value=None) - mocker.patch("freqtrade.rpc.RPC._rpc_force_entry", fbuy_mock) + mocker.patch("freqtrade.rpc.rpc.RPC._rpc_force_entry", fbuy_mock) rc = client_post(client, f"{BASE_URI}/{endpoint}", data={"pair": "ETH/BTC"}) assert_response(rc) @@ -1167,7 +1208,7 @@ def test_api_force_entry(botclient, mocker, fee, endpoint): strategy=CURRENT_TEST_STRATEGY, trading_mode=TradingMode.SPOT )) - mocker.patch("freqtrade.rpc.RPC._rpc_force_entry", fbuy_mock) + mocker.patch("freqtrade.rpc.rpc.RPC._rpc_force_entry", fbuy_mock) rc = client_post(client, f"{BASE_URI}/{endpoint}", data={"pair": "ETH/BTC"}) @@ -1204,6 +1245,8 @@ def test_api_force_entry(botclient, mocker, fee, endpoint): 'profit_pct': None, 'profit_abs': None, 'profit_fiat': None, + 'realized_profit': 0.0, + 'realized_profit_ratio': None, 'fee_close': 0.0025, 'fee_close_cost': None, 'fee_close_currency': None, @@ -1235,12 +1278,12 @@ def test_api_force_entry(botclient, mocker, fee, endpoint): def test_api_forceexit(botclient, mocker, ticker, fee, markets): ftbot, client = botclient mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value=ticker), fetch_ticker=ticker, get_fee=fee, markets=PropertyMock(return_value=markets), - _is_dry_limit_order_filled=MagicMock(return_value=True), + _dry_is_price_crossed=MagicMock(return_value=True), ) patch_get_signal(ftbot) @@ -1248,7 +1291,7 @@ def test_api_forceexit(botclient, mocker, ticker, fee, markets): data={"tradeid": "1"}) assert_response(rc, 502) assert rc.json() == {"error": "Error querying /api/v1/forceexit: invalid argument"} - Trade.query.session.rollback() + Trade.rollback() create_mock_trades(fee) trade = Trade.get_trades([Trade.id == 5]).first() @@ -1257,7 +1300,7 @@ def test_api_forceexit(botclient, mocker, ticker, fee, markets): data={"tradeid": "5", "ordertype": "market", "amount": 23}) assert_response(rc) assert rc.json() == {'result': 'Created sell order for trade 5.'} - Trade.query.session.rollback() + Trade.rollback() trade = Trade.get_trades([Trade.id == 5]).first() assert pytest.approx(trade.amount) == 100 @@ -1267,7 +1310,7 @@ def test_api_forceexit(botclient, mocker, ticker, fee, markets): data={"tradeid": "5"}) assert_response(rc) assert rc.json() == {'result': 'Created sell order for trade 5.'} - Trade.query.session.rollback() + Trade.rollback() trade = Trade.get_trades([Trade.id == 5]).first() assert trade.is_open is False @@ -1417,7 +1460,7 @@ def test_api_pair_history(botclient, ohlcv_history): "No data for UNITTEST/BTC, 5m in 20200111-20200112 found.") -def test_api_plot_config(botclient): +def test_api_plot_config(botclient, mocker): ftbot, client = botclient rc = client_get(client, f"{BASE_URI}/plot_config") @@ -1441,6 +1484,21 @@ def test_api_plot_config(botclient): assert isinstance(rc.json()['main_plot'], dict) assert isinstance(rc.json()['subplots'], dict) + rc = client_get(client, f"{BASE_URI}/plot_config?strategy=freqai_test_classifier") + assert_response(rc) + res = rc.json() + assert 'target_roi' in res['subplots'] + assert 'do_predict' in res['subplots'] + + rc = client_get(client, f"{BASE_URI}/plot_config?strategy=HyperoptableStrategy") + assert_response(rc) + assert rc.json()['subplots'] == {} + + mocker.patch('freqtrade.rpc.api_server.api_v1.get_rpc_optional', return_value=None) + + rc = client_get(client, f"{BASE_URI}/plot_config") + assert_response(rc) + def test_api_strategies(botclient, tmpdir): ftbot, client = botclient @@ -1575,7 +1633,7 @@ def test_sysinfo(botclient): def test_api_backtesting(botclient, mocker, fee, caplog, tmpdir): ftbot, client = botclient - mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch(f'{EXMS}.get_fee', fee) rc = client_get(client, f"{BASE_URI}/backtest") # Backtest prevented in default mode @@ -1682,9 +1740,15 @@ def test_api_backtesting(botclient, mocker, fee, caplog, tmpdir): data['stake_amount'] = 101 mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest_one_strategy', - side_effect=DependencyException()) + side_effect=DependencyException('DeadBeef')) rc = client_post(client, f"{BASE_URI}/backtest", data=data) - assert log_has("Backtesting caused an error: ", caplog) + assert log_has("Backtesting caused an error: DeadBeef", caplog) + + rc = client_get(client, f"{BASE_URI}/backtest") + assert_response(rc) + result = rc.json() + assert result['status'] == 'error' + assert 'Backtest failed' in result['status_msg'] # Delete backtesting to avoid leakage since the backtest-object may stick around. rc = client_delete(client, f"{BASE_URI}/backtest") @@ -1740,8 +1804,8 @@ def test_health(botclient): assert_response(rc) ret = rc.json() - assert ret['last_process_ts'] == 0 - assert ret['last_process'] == '1970-01-01T00:00:00+00:00' + assert ret["last_process_ts"] is None + assert ret["last_process"] is None def test_api_ws_subscribe(botclient, mocker): diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index 85475ae8e..54f612c59 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -14,13 +14,15 @@ import arrow import pytest import time_machine from pandas import DataFrame +from sqlalchemy import select from telegram import Chat, Message, ReplyKeyboardMarkup, Update from telegram.error import BadRequest, NetworkError, TelegramError from freqtrade import __version__ from freqtrade.constants import CANCEL_REASON from freqtrade.edge import PairInfo -from freqtrade.enums import ExitType, RPCMessageType, RunMode, SignalDirection, State +from freqtrade.enums import (ExitType, MarketDirection, RPCMessageType, RunMode, SignalDirection, + State) from freqtrade.exceptions import OperationalException from freqtrade.freqtradebot import FreqtradeBot from freqtrade.loggers import setup_logging @@ -29,9 +31,9 @@ from freqtrade.persistence.models import Order from freqtrade.rpc import RPC from freqtrade.rpc.rpc import RPCException from freqtrade.rpc.telegram import Telegram, authorized_only -from tests.conftest import (CURRENT_TEST_STRATEGY, create_mock_trades, create_mock_trades_usdt, - get_patched_freqtradebot, log_has, log_has_re, patch_exchange, - patch_get_signal, patch_whitelist) +from tests.conftest import (CURRENT_TEST_STRATEGY, EXMS, create_mock_trades, + create_mock_trades_usdt, get_patched_freqtradebot, log_has, log_has_re, + patch_exchange, patch_get_signal, patch_whitelist) class DummyCls(Telegram): @@ -99,14 +101,14 @@ def test_telegram_init(default_conf, mocker, caplog) -> None: message_str = ("rpc.telegram is listening for following commands: [['status'], ['profit'], " "['balance'], ['start'], ['stop'], " "['forcesell', 'forceexit', 'fx'], ['forcebuy', 'forcelong'], ['forceshort'], " - "['trades'], ['delete'], ['performance'], " + "['trades'], ['delete'], ['coo', 'cancel_open_order'], ['performance'], " "['buys', 'entries'], ['sells', 'exits'], ['mix_tags'], " "['stats'], ['daily'], ['weekly'], ['monthly'], " "['count'], ['locks'], ['unlock', 'delete_locks'], " "['reload_config', 'reload_conf'], ['show_config', 'show_conf'], " "['stopbuy', 'stopentry'], ['whitelist'], ['blacklist'], " "['blacklist_delete', 'bl_delete'], " - "['logs'], ['edge'], ['health'], ['help'], ['version']" + "['logs'], ['edge'], ['health'], ['help'], ['version'], ['marketdir']" "]") assert log_has(message_str, caplog) @@ -197,11 +199,15 @@ def test_telegram_status(default_conf, update, mocker) -> None: 'current_rate': 1.098e-05, 'amount': 90.99181074, 'stake_amount': 90.99181074, + 'max_stake_amount': 90.99181074, 'buy_tag': None, 'enter_tag': None, 'close_profit_ratio': None, 'profit': -0.0059, 'profit_ratio': -0.0059, + 'profit_abs': -0.225, + 'realized_profit': 0.0, + 'total_profit_abs': -0.225, 'initial_stop_loss_abs': 1.098e-05, 'stop_loss_abs': 1.099e-05, 'exit_order_status': None, @@ -236,7 +242,7 @@ def test_telegram_status_multi_entry(default_conf, update, mocker, fee) -> None: default_conf['telegram']['chat_id'] = "123" default_conf['position_adjustment_enable'] = True mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_order=MagicMock(return_value=None), get_rate=MagicMock(return_value=0.22), ) @@ -275,6 +281,7 @@ def test_telegram_status_multi_entry(default_conf, update, mocker, fee) -> None: assert msg_mock.call_count == 4 msg = msg_mock.call_args_list[0][0][0] assert re.search(r'Number of Entries.*2', msg) + assert re.search(r'Number of Exits.*0', msg) assert re.search(r'Average Entry Price', msg) assert re.search(r'Order filled', msg) assert re.search(r'Close Date:', msg) is None @@ -288,7 +295,7 @@ def test_telegram_status_closed_trade(default_conf, update, mocker, fee) -> None default_conf['telegram']['chat_id'] = "123" default_conf['position_adjustment_enable'] = True mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_order=MagicMock(return_value=None), get_rate=MagicMock(return_value=0.22), ) @@ -296,8 +303,7 @@ def test_telegram_status_closed_trade(default_conf, update, mocker, fee) -> None telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) create_mock_trades(fee) - trades = Trade.get_trades([Trade.is_open.is_(False)]) - trade = trades[0] + trade = Trade.get_trades([Trade.is_open.is_(False)]).first() context = MagicMock() context.args = [str(trade.id)] telegram._status(update=update, context=context) @@ -310,10 +316,10 @@ def test_telegram_status_closed_trade(default_conf, update, mocker, fee) -> None def test_status_handle(default_conf, update, ticker, fee, mocker) -> None: default_conf['max_open_trades'] = 3 mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=True), + _dry_is_price_crossed=MagicMock(return_value=True), ) status_table = MagicMock() mocker.patch.multiple( @@ -387,7 +393,7 @@ def test_status_handle(default_conf, update, ticker, fee, mocker) -> None: def test_status_table_handle(default_conf, update, ticker, fee, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -432,7 +438,7 @@ def test_daily_handle(default_conf_usdt, update, ticker, fee, mocker, time_machi return_value=1.1 ) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -487,7 +493,7 @@ def test_daily_handle(default_conf_usdt, update, ticker, fee, mocker, time_machi def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker ) @@ -521,7 +527,7 @@ def test_weekly_handle(default_conf_usdt, update, ticker, fee, mocker, time_mach return_value=1.1 ) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -591,7 +597,7 @@ def test_monthly_handle(default_conf_usdt, update, ticker, fee, mocker, time_mac return_value=1.1 ) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -646,7 +652,7 @@ def test_monthly_handle(default_conf_usdt, update, ticker, fee, mocker, time_mac # The one-digit months should contain a zero, Eg: September 2021 = "2021-09" # Since we loaded the last 12 months, any month should appear - assert str('-09') in msg_mock.call_args_list[0][0][0] + assert '-09' in msg_mock.call_args_list[0][0][0] # Try invalid data msg_mock.reset_mock() @@ -665,14 +671,15 @@ def test_monthly_handle(default_conf_usdt, update, ticker, fee, mocker, time_mac context = MagicMock() context.args = ["february"] telegram._monthly(update=update, context=context) - assert str('Monthly Profit over the last 6 months:') in msg_mock.call_args_list[0][0][0] + assert 'Monthly Profit over the last 6 months:' in msg_mock.call_args_list[0][0][0] -def test_profit_handle(default_conf_usdt, update, ticker_usdt, ticker_sell_up, fee, - limit_sell_order_usdt, mocker) -> None: +def test_telegram_profit_handle( + default_conf_usdt, update, ticker_usdt, ticker_sell_up, fee, + limit_sell_order_usdt, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=1.1) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, ) @@ -687,7 +694,7 @@ def test_profit_handle(default_conf_usdt, update, ticker_usdt, ticker_sell_up, f # Create some test data freqtradebot.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() context = MagicMock() # Test with invalid 2nd argument (should silently pass) @@ -702,8 +709,9 @@ def test_profit_handle(default_conf_usdt, update, ticker_usdt, ticker_sell_up, f msg_mock.reset_mock() # Update the ticker with a market going up - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', ticker_sell_up) + mocker.patch(f'{EXMS}.fetch_ticker', ticker_sell_up) # Simulate fulfilled LIMIT_SELL order for trade + trade = Trade.session.scalars(select(Trade)).first() oobj = Order.parse_from_ccxt_object( limit_sell_order_usdt, limit_sell_order_usdt['symbol'], 'sell') trade.orders.append(oobj) @@ -735,7 +743,7 @@ def test_profit_handle(default_conf_usdt, update, ticker_usdt, ticker_sell_up, f def test_telegram_stats(default_conf, update, ticker, fee, mocker, is_short) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -760,10 +768,9 @@ def test_telegram_stats(default_conf, update, ticker, fee, mocker, is_short) -> def test_telegram_balance_handle(default_conf, update, mocker, rpc_balance, tickers) -> None: default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value=rpc_balance) - mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers) - mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination', - side_effect=lambda a, b: f"{a}/{b}") + mocker.patch(f'{EXMS}.get_balances', return_value=rpc_balance) + mocker.patch(f'{EXMS}.get_tickers', tickers) + mocker.patch(f'{EXMS}.get_valid_pair_combination', side_effect=lambda a, b: f"{a}/{b}") telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot) @@ -786,7 +793,7 @@ def test_telegram_balance_handle(default_conf, update, mocker, rpc_balance, tick def test_balance_handle_empty_response(default_conf, update, mocker) -> None: default_conf['dry_run'] = False - mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value={}) + mocker.patch(f'{EXMS}.get_balances', return_value={}) telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot) @@ -799,7 +806,7 @@ def test_balance_handle_empty_response(default_conf, update, mocker) -> None: def test_balance_handle_empty_response_dry(default_conf, update, mocker) -> None: - mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value={}) + mocker.patch(f'{EXMS}.get_balances', return_value={}) telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot) @@ -927,10 +934,10 @@ def test_telegram_forceexit_handle(default_conf, update, ticker, fee, patch_exchange(mocker) patch_whitelist(mocker, default_conf) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=True), + _dry_is_price_crossed=MagicMock(return_value=True), ) freqtradebot = FreqtradeBot(default_conf) @@ -941,11 +948,11 @@ def test_telegram_forceexit_handle(default_conf, update, ticker, fee, # Create some test data freqtradebot.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade # Increase the price and sell it - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', ticker_sell_up) + mocker.patch(f'{EXMS}.fetch_ticker', ticker_sell_up) # /forceexit 1 context = MagicMock() @@ -996,10 +1003,10 @@ def test_telegram_force_exit_down_handle(default_conf, update, ticker, fee, patch_whitelist(mocker, default_conf) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=True), + _dry_is_price_crossed=MagicMock(return_value=True), ) freqtradebot = FreqtradeBot(default_conf) @@ -1012,11 +1019,11 @@ def test_telegram_force_exit_down_handle(default_conf, update, ticker, fee, # Decrease the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_sell_down ) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade # /forceexit 1 @@ -1067,10 +1074,10 @@ def test_forceexit_all_handle(default_conf, update, ticker, fee, mocker) -> None mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) patch_whitelist(mocker, default_conf) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=True), + _dry_is_price_crossed=MagicMock(return_value=True), ) default_conf['max_open_trades'] = 4 freqtradebot = FreqtradeBot(default_conf) @@ -1152,10 +1159,10 @@ def test_forceexit_handle_invalid(default_conf, update, mocker) -> None: def test_force_exit_no_pair(default_conf, update, ticker, fee, mocker) -> None: default_conf['max_open_trades'] = 4 mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=True), + _dry_is_price_crossed=MagicMock(return_value=True), ) femock = mocker.patch('freqtrade.rpc.rpc.RPC._rpc_force_exit') telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) @@ -1206,7 +1213,7 @@ def test_force_enter_handle(default_conf, update, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) fbuy_mock = MagicMock(return_value=None) - mocker.patch('freqtrade.rpc.RPC._rpc_force_entry', fbuy_mock) + mocker.patch('freqtrade.rpc.rpc.RPC._rpc_force_entry', fbuy_mock) telegram, freqtradebot, _ = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot) @@ -1223,7 +1230,7 @@ def test_force_enter_handle(default_conf, update, mocker) -> None: # Reset and retry with specified price fbuy_mock = MagicMock(return_value=None) - mocker.patch('freqtrade.rpc.RPC._rpc_force_entry', fbuy_mock) + mocker.patch('freqtrade.rpc.rpc.RPC._rpc_force_entry', fbuy_mock) # /forcelong ETH/BTC 0.055 context = MagicMock() context.args = ["ETH/BTC", "0.055"] @@ -1252,7 +1259,7 @@ def test_force_enter_no_pair(default_conf, update, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) fbuy_mock = MagicMock(return_value=None) - mocker.patch('freqtrade.rpc.RPC._rpc_force_entry', fbuy_mock) + mocker.patch('freqtrade.rpc.rpc.RPC._rpc_force_entry', fbuy_mock) telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) @@ -1279,7 +1286,7 @@ def test_force_enter_no_pair(default_conf, update, mocker) -> None: def test_telegram_performance_handle(default_conf_usdt, update, ticker, fee, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -1297,7 +1304,7 @@ def test_telegram_performance_handle(default_conf_usdt, update, ticker, fee, moc def test_telegram_entry_tag_performance_handle( default_conf_usdt, update, ticker, fee, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -1328,7 +1335,7 @@ def test_telegram_entry_tag_performance_handle( def test_telegram_exit_reason_performance_handle(default_conf_usdt, update, ticker, fee, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -1359,7 +1366,7 @@ def test_telegram_exit_reason_performance_handle(default_conf_usdt, update, tick def test_telegram_mix_tag_performance_handle(default_conf_usdt, update, ticker, fee, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -1391,7 +1398,7 @@ def test_telegram_mix_tag_performance_handle(default_conf_usdt, update, ticker, def test_count_handle(default_conf, update, ticker, fee, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -1420,7 +1427,7 @@ def test_count_handle(default_conf, update, ticker, fee, mocker) -> None: def test_telegram_lock_handle(default_conf, update, ticker, fee, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, ) @@ -1488,7 +1495,7 @@ def test_whitelist_static(default_conf, update, mocker) -> None: def test_whitelist_dynamic(default_conf, update, mocker) -> None: - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', return_value=True) default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 4 }] @@ -1678,6 +1685,39 @@ def test_telegram_delete_trade(mocker, update, default_conf, fee, is_short): assert "Please make sure to take care of this asset" in msg_mock.call_args_list[0][0][0] +@pytest.mark.parametrize('is_short', [True, False]) +def test_telegram_delete_open_order(mocker, update, default_conf, fee, is_short, ticker): + + mocker.patch.multiple( + EXMS, + fetch_ticker=ticker, + ) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) + context = MagicMock() + context.args = [] + + telegram._cancel_open_order(update=update, context=context) + assert "Trade-id not set." in msg_mock.call_args_list[0][0][0] + + msg_mock.reset_mock() + create_mock_trades(fee, is_short=is_short) + + context = MagicMock() + context.args = [5] + telegram._cancel_open_order(update=update, context=context) + assert "No open order for trade_id" in msg_mock.call_args_list[0][0][0] + + msg_mock.reset_mock() + + trade = Trade.get_trades([Trade.id == 6]).first() + mocker.patch(f'{EXMS}.fetch_order', return_value=trade.orders[-1].to_ccxt_object()) + context = MagicMock() + context.args = [6] + telegram._cancel_open_order(update=update, context=context) + assert msg_mock.call_count == 1 + assert "Open order canceled." in msg_mock.call_args_list[0][0][0] + + def test_help_handle(default_conf, update, mocker) -> None: telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) @@ -1692,14 +1732,14 @@ def test_version_handle(default_conf, update, mocker) -> None: telegram._version(update=update, context=MagicMock()) assert msg_mock.call_count == 1 - assert '*Version:* `{}`'.format(__version__) in msg_mock.call_args_list[0][0][0] + assert f'*Version:* `{__version__}`' in msg_mock.call_args_list[0][0][0] msg_mock.reset_mock() freqtradebot.strategy.version = lambda: '1.1.1' telegram._version(update=update, context=MagicMock()) assert msg_mock.call_count == 1 - assert '*Version:* `{}`'.format(__version__) in msg_mock.call_args_list[0][0][0] + assert f'*Version:* `{__version__}`' in msg_mock.call_args_list[0][0][0] assert '*Strategy version: * `1.1.1`' in msg_mock.call_args_list[0][0][0] @@ -1975,7 +2015,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: 'sub_trade': True, }) assert msg_mock.call_args[0][0] == ( - '\N{WARNING SIGN} *Binance (dry):* Exiting KEY/ETH (#1)\n' + '\N{WARNING SIGN} *Binance (dry):* Partially exiting KEY/ETH (#1)\n' '*Unrealized Sub Profit:* `-57.41% (loss: -0.05746268 ETH / -24.812 USD)`\n' '*Cumulative Profit:* (`-0.15746268 ETH / -24.812 USD`)\n' '*Enter Tag:* `buy_signal1`\n' @@ -2360,3 +2400,15 @@ def test__send_msg_keyboard(default_conf, mocker, caplog) -> None: assert log_has("using custom keyboard from config.json: " "[['/daily', '/stats', '/balance', '/profit', '/profit 5'], ['/count', " "'/start', '/reload_config', '/help']]", caplog) + + +def test_change_market_direction(default_conf, mocker, update) -> None: + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) + assert telegram._rpc._freqtrade.strategy.market_direction == MarketDirection.NONE + context = MagicMock() + context.args = ["long"] + telegram._changemarketdir(update, context) + assert telegram._rpc._freqtrade.strategy.market_direction == MarketDirection.LONG + context = MagicMock() + context.args = ["invalid"] + assert telegram._rpc._freqtrade.strategy.market_direction == MarketDirection.LONG diff --git a/tests/rpc/test_rpc_webhook.py b/tests/rpc/test_rpc_webhook.py index a8fd0c34b..f55582107 100644 --- a/tests/rpc/test_rpc_webhook.py +++ b/tests/rpc/test_rpc_webhook.py @@ -356,6 +356,14 @@ def test_exception_send_msg(default_conf, mocker, caplog): } webhook.send_msg(msg) + # Test no failure for not implemented but known messagetypes + for e in RPCMessageType: + msg = { + 'type': e, + 'status': 'whatever' + } + webhook.send_msg(msg) + def test__send_msg(default_conf, mocker, caplog): default_conf["webhook"] = get_webhook_dict() diff --git a/tests/strategy/strats/broken_strats/broken_futures_strategies.py b/tests/strategy/strats/broken_strats/broken_futures_strategies.py index 7e6955d37..bb7ce2b32 100644 --- a/tests/strategy/strats/broken_strats/broken_futures_strategies.py +++ b/tests/strategy/strats/broken_strats/broken_futures_strategies.py @@ -7,6 +7,7 @@ from datetime import datetime from pandas import DataFrame +from freqtrade.persistence.trade_model import Order from freqtrade.strategy.interface import IStrategy @@ -35,7 +36,7 @@ class TestStrategyImplementBuyTimeout(TestStrategyNoImplementSell): def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: return super().populate_exit_trend(dataframe, metadata) - def check_buy_timeout(self, pair: str, trade, order: dict, + def check_buy_timeout(self, pair: str, trade, order: Order, current_time: datetime, **kwargs) -> bool: return False @@ -44,6 +45,6 @@ class TestStrategyImplementSellTimeout(TestStrategyNoImplementSell): def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: return super().populate_exit_trend(dataframe, metadata) - def check_sell_timeout(self, pair: str, trade, order: dict, + def check_sell_timeout(self, pair: str, trade, order: Order, current_time: datetime, **kwargs) -> bool: return False diff --git a/tests/strategy/strats/freqai_rl_test_strat.py b/tests/strategy/strats/freqai_rl_test_strat.py index 6fa926fc9..2bf4aaa30 100644 --- a/tests/strategy/strats/freqai_rl_test_strat.py +++ b/tests/strategy/strats/freqai_rl_test_strat.py @@ -1,5 +1,6 @@ import logging from functools import reduce +from typing import Dict import talib.abstract as ta from pandas import DataFrame @@ -24,20 +25,21 @@ class freqai_rl_test_strat(IStrategy): startup_candle_count: int = 300 can_short = False - def feature_engineering_expand_all(self, dataframe, period, **kwargs): + def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, + metadata: Dict, **kwargs): dataframe["%-rsi-period"] = ta.RSI(dataframe, timeperiod=period) return dataframe - def feature_engineering_expand_basic(self, dataframe: DataFrame, **kwargs): + def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-raw_volume"] = dataframe["volume"] return dataframe - def feature_engineering_standard(self, dataframe, **kwargs): + def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-day_of_week"] = dataframe["date"].dt.dayofweek dataframe["%-hour_of_day"] = dataframe["date"].dt.hour @@ -49,7 +51,7 @@ class freqai_rl_test_strat(IStrategy): return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["&-action"] = 0 diff --git a/tests/strategy/strats/freqai_test_classifier.py b/tests/strategy/strats/freqai_test_classifier.py index 02427ab59..61b9f0c37 100644 --- a/tests/strategy/strats/freqai_test_classifier.py +++ b/tests/strategy/strats/freqai_test_classifier.py @@ -1,5 +1,6 @@ import logging from functools import reduce +from typing import Dict import numpy as np import talib.abstract as ta @@ -56,7 +57,8 @@ class freqai_test_classifier(IStrategy): informative_pairs.append((pair, tf)) return informative_pairs - def feature_engineering_expand_all(self, dataframe, period, **kwargs): + def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, + metadata: Dict, **kwargs): dataframe["%-rsi-period"] = ta.RSI(dataframe, timeperiod=period) dataframe["%-mfi-period"] = ta.MFI(dataframe, timeperiod=period) @@ -64,7 +66,7 @@ class freqai_test_classifier(IStrategy): return dataframe - def feature_engineering_expand_basic(self, dataframe: DataFrame, **kwargs): + def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-raw_volume"] = dataframe["volume"] @@ -72,14 +74,14 @@ class freqai_test_classifier(IStrategy): return dataframe - def feature_engineering_standard(self, dataframe, **kwargs): + def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-day_of_week"] = dataframe["date"].dt.dayofweek dataframe["%-hour_of_day"] = dataframe["date"].dt.hour return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe['&s-up_or_down'] = np.where(dataframe["close"].shift(-100) > dataframe["close"], 'up', 'down') diff --git a/tests/strategy/strats/freqai_test_multimodel_classifier_strat.py b/tests/strategy/strats/freqai_test_multimodel_classifier_strat.py index 65f2e4540..b2ddc21e3 100644 --- a/tests/strategy/strats/freqai_test_multimodel_classifier_strat.py +++ b/tests/strategy/strats/freqai_test_multimodel_classifier_strat.py @@ -1,5 +1,6 @@ import logging from functools import reduce +from typing import Dict import numpy as np import talib.abstract as ta @@ -43,7 +44,8 @@ class freqai_test_multimodel_classifier_strat(IStrategy): ) max_roi_time_long = IntParameter(0, 800, default=400, space="sell", optimize=False, load=True) - def feature_engineering_expand_all(self, dataframe, period, **kwargs): + def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, + metadata: Dict, **kwargs): dataframe["%-rsi-period"] = ta.RSI(dataframe, timeperiod=period) dataframe["%-mfi-period"] = ta.MFI(dataframe, timeperiod=period) @@ -51,7 +53,7 @@ class freqai_test_multimodel_classifier_strat(IStrategy): return dataframe - def feature_engineering_expand_basic(self, dataframe: DataFrame, **kwargs): + def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-raw_volume"] = dataframe["volume"] @@ -59,14 +61,14 @@ class freqai_test_multimodel_classifier_strat(IStrategy): return dataframe - def feature_engineering_standard(self, dataframe, **kwargs): + def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-day_of_week"] = dataframe["date"].dt.dayofweek dataframe["%-hour_of_day"] = dataframe["date"].dt.hour return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe['&s-up_or_down'] = np.where(dataframe["close"].shift(-50) > dataframe["close"], 'up', 'down') diff --git a/tests/strategy/strats/freqai_test_multimodel_strat.py b/tests/strategy/strats/freqai_test_multimodel_strat.py index 5c9712629..5b09598a5 100644 --- a/tests/strategy/strats/freqai_test_multimodel_strat.py +++ b/tests/strategy/strats/freqai_test_multimodel_strat.py @@ -1,5 +1,6 @@ import logging from functools import reduce +from typing import Dict import talib.abstract as ta from pandas import DataFrame @@ -42,7 +43,8 @@ class freqai_test_multimodel_strat(IStrategy): ) max_roi_time_long = IntParameter(0, 800, default=400, space="sell", optimize=False, load=True) - def feature_engineering_expand_all(self, dataframe, period, **kwargs): + def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, + metadata: Dict, **kwargs): dataframe["%-rsi-period"] = ta.RSI(dataframe, timeperiod=period) dataframe["%-mfi-period"] = ta.MFI(dataframe, timeperiod=period) @@ -50,7 +52,7 @@ class freqai_test_multimodel_strat(IStrategy): return dataframe - def feature_engineering_expand_basic(self, dataframe: DataFrame, **kwargs): + def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-raw_volume"] = dataframe["volume"] @@ -58,14 +60,14 @@ class freqai_test_multimodel_strat(IStrategy): return dataframe - def feature_engineering_standard(self, dataframe, **kwargs): + def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-day_of_week"] = dataframe["date"].dt.dayofweek dataframe["%-hour_of_day"] = dataframe["date"].dt.hour return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["&-s_close"] = ( dataframe["close"] diff --git a/tests/strategy/strats/freqai_test_strat.py b/tests/strategy/strats/freqai_test_strat.py index b52c95908..6db308406 100644 --- a/tests/strategy/strats/freqai_test_strat.py +++ b/tests/strategy/strats/freqai_test_strat.py @@ -1,5 +1,6 @@ import logging from functools import reduce +from typing import Dict import talib.abstract as ta from pandas import DataFrame @@ -42,7 +43,8 @@ class freqai_test_strat(IStrategy): ) max_roi_time_long = IntParameter(0, 800, default=400, space="sell", optimize=False, load=True) - def feature_engineering_expand_all(self, dataframe, period, **kwargs): + def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, + metadata: Dict, **kwargs): dataframe["%-rsi-period"] = ta.RSI(dataframe, timeperiod=period) dataframe["%-mfi-period"] = ta.MFI(dataframe, timeperiod=period) @@ -50,7 +52,7 @@ class freqai_test_strat(IStrategy): return dataframe - def feature_engineering_expand_basic(self, dataframe: DataFrame, **kwargs): + def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-pct-change"] = dataframe["close"].pct_change() dataframe["%-raw_volume"] = dataframe["volume"] @@ -58,14 +60,14 @@ class freqai_test_strat(IStrategy): return dataframe - def feature_engineering_standard(self, dataframe, **kwargs): + def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["%-day_of_week"] = dataframe["date"].dt.dayofweek dataframe["%-hour_of_day"] = dataframe["date"].dt.hour return dataframe - def set_freqai_targets(self, dataframe, **kwargs): + def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs): dataframe["&-s_close"] = ( dataframe["close"] diff --git a/tests/strategy/strats/hyperoptable_strategy.py b/tests/strategy/strats/hyperoptable_strategy.py index 9850a5675..eadbc533f 100644 --- a/tests/strategy/strats/hyperoptable_strategy.py +++ b/tests/strategy/strats/hyperoptable_strategy.py @@ -34,6 +34,11 @@ class HyperoptableStrategy(StrategyTestV3): protection_enabled = BooleanParameter(default=True) protection_cooldown_lookback = IntParameter([0, 50], default=30) + # Invalid plot config ... + plot_config = { + "main_plot": {}, + } + @property def protections(self): prot = [] diff --git a/tests/strategy/strats/strategy_test_v3.py b/tests/strategy/strats/strategy_test_v3.py index 088ab21d4..2d5121403 100644 --- a/tests/strategy/strats/strategy_test_v3.py +++ b/tests/strategy/strats/strategy_test_v3.py @@ -30,6 +30,9 @@ class StrategyTestV3(IStrategy): "0": 0.04 } + # Optimal max_open_trades for the strategy + max_open_trades = -1 + # Optimal stoploss designed for the strategy stoploss = -0.10 @@ -194,7 +197,7 @@ class StrategyTestV3(IStrategy): if current_profit < -0.0075: orders = trade.select_filled_orders(trade.entry_side) - return round(orders[0].cost, 0) + return round(orders[0].safe_cost, 0) return None diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 294021c83..7b1399507 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -214,12 +214,12 @@ def test_ignore_expired_candle(default_conf): current_time = latest_date + timedelta(seconds=30 + 300) - assert not strategy.ignore_expired_candle( + assert strategy.ignore_expired_candle( latest_date=latest_date, current_time=current_time, timeframe_seconds=300, enter=True - ) is True + ) is not True def test_assert_df_raise(mocker, caplog, ohlcv_history): @@ -291,18 +291,6 @@ def test_advise_all_indicators(default_conf, testdatadir) -> None: assert len(processed['UNITTEST/BTC']) == 103 -def test_populate_any_indicators(default_conf, testdatadir) -> None: - strategy = StrategyResolver.load_strategy(default_conf) - - timerange = TimeRange.parse_timerange('1510694220-1510700340') - data = load_data(testdatadir, '1m', ['UNITTEST/BTC'], timerange=timerange, - fill_up_missing=True) - processed = strategy.populate_any_indicators('UNITTEST/BTC', data, '5m') - assert processed == data - assert id(processed) == id(data) - assert len(processed['UNITTEST/BTC']) == 103 - - def test_freqai_not_initialized(default_conf) -> None: strategy = StrategyResolver.load_strategy(default_conf) strategy.ft_bot_start() @@ -452,8 +440,8 @@ def test_min_roi_reached3(default_conf, fee) -> None: (0.05, 0.9, ExitType.NONE, None, False, True, 0.09, 0.9, ExitType.NONE, lambda **kwargs: None), ]) -def test_stop_loss_reached(default_conf, fee, profit, adjusted, expected, liq, trailing, custom, - profit2, adjusted2, expected2, custom_stop) -> None: +def test_ft_stoploss_reached(default_conf, fee, profit, adjusted, expected, liq, trailing, custom, + profit2, adjusted2, expected2, custom_stop) -> None: strategy = StrategyResolver.load_strategy(default_conf) trade = Trade( @@ -477,9 +465,9 @@ def test_stop_loss_reached(default_conf, fee, profit, adjusted, expected, liq, t now = arrow.utcnow().datetime current_rate = trade.open_rate * (1 + profit) - sl_flag = strategy.stop_loss_reached(current_rate=current_rate, trade=trade, - current_time=now, current_profit=profit, - force_stoploss=0, high=None) + sl_flag = strategy.ft_stoploss_reached(current_rate=current_rate, trade=trade, + current_time=now, current_profit=profit, + force_stoploss=0, high=None) assert isinstance(sl_flag, ExitCheckTuple) assert sl_flag.exit_type == expected if expected == ExitType.NONE: @@ -489,9 +477,9 @@ def test_stop_loss_reached(default_conf, fee, profit, adjusted, expected, liq, t assert round(trade.stop_loss, 2) == adjusted current_rate2 = trade.open_rate * (1 + profit2) - sl_flag = strategy.stop_loss_reached(current_rate=current_rate2, trade=trade, - current_time=now, current_profit=profit2, - force_stoploss=0, high=None) + sl_flag = strategy.ft_stoploss_reached(current_rate=current_rate2, trade=trade, + current_time=now, current_profit=profit2, + force_stoploss=0, high=None) assert sl_flag.exit_type == expected2 if expected2 == ExitType.NONE: assert sl_flag.exit_flag is False @@ -579,7 +567,7 @@ def test_should_sell(default_conf, fee) -> None: assert res == [ExitCheckTuple(exit_type=ExitType.ROI)] strategy.min_roi_reached = MagicMock(return_value=True) - strategy.stop_loss_reached = MagicMock( + strategy.ft_stoploss_reached = MagicMock( return_value=ExitCheckTuple(exit_type=ExitType.STOP_LOSS)) res = strategy.should_exit(trade, 1, now, @@ -603,7 +591,7 @@ def test_should_sell(default_conf, fee) -> None: ExitCheckTuple(exit_type=ExitType.ROI), ] - strategy.stop_loss_reached = MagicMock( + strategy.ft_stoploss_reached = MagicMock( return_value=ExitCheckTuple(exit_type=ExitType.TRAILING_STOP_LOSS)) # Regular exit signal res = strategy.should_exit(trade, 1, now, diff --git a/tests/strategy/test_strategy_helpers.py b/tests/strategy/test_strategy_helpers.py index f42f9e681..a55580780 100644 --- a/tests/strategy/test_strategy_helpers.py +++ b/tests/strategy/test_strategy_helpers.py @@ -119,53 +119,88 @@ def test_merge_informative_pair_suffix_append_timeframe(): merge_informative_pair(data, informative, '15m', '1h', suffix="suf") -def test_stoploss_from_open(): +@pytest.mark.parametrize("side,profitrange", [ + # profit range for long is [-1, inf] while for shorts is [-inf, 1] + ("long", [-0.99, 2, 30]), + ("short", [-2.0, 0.99, 30]), +]) +def test_stoploss_from_open(side, profitrange): open_price_ranges = [ [0.01, 1.00, 30], [1, 100, 30], [100, 10000, 30], ] - # profit range for long is [-1, inf] while for shorts is [-inf, 1] - current_profit_range_dict = {'long': [-0.99, 2, 30], 'short': [-2.0, 0.99, 30]} - desired_stop_range = [-0.50, 0.50, 30] - for side, current_profit_range in current_profit_range_dict.items(): - for open_range in open_price_ranges: - for open_price in np.linspace(*open_range): - for desired_stop in np.linspace(*desired_stop_range): + for open_range in open_price_ranges: + for open_price in np.linspace(*open_range): + for desired_stop in np.linspace(-0.50, 0.50, 30): + if side == 'long': + # -1 is not a valid current_profit, should return 1 + assert stoploss_from_open(desired_stop, -1) == 1 + else: + # 1 is not a valid current_profit for shorts, should return 1 + assert stoploss_from_open(desired_stop, 1, True) == 1 + + for current_profit in np.linspace(*profitrange): if side == 'long': - # -1 is not a valid current_profit, should return 1 - assert stoploss_from_open(desired_stop, -1) == 1 + current_price = open_price * (1 + current_profit) + expected_stop_price = open_price * (1 + desired_stop) + stoploss = stoploss_from_open(desired_stop, current_profit) + stop_price = current_price * (1 - stoploss) else: - # 1 is not a valid current_profit for shorts, should return 1 - assert stoploss_from_open(desired_stop, 1, True) == 1 + current_price = open_price * (1 - current_profit) + expected_stop_price = open_price * (1 - desired_stop) + stoploss = stoploss_from_open(desired_stop, current_profit, True) + stop_price = current_price * (1 + stoploss) - for current_profit in np.linspace(*current_profit_range): - if side == 'long': - current_price = open_price * (1 + current_profit) - expected_stop_price = open_price * (1 + desired_stop) - stoploss = stoploss_from_open(desired_stop, current_profit) - stop_price = current_price * (1 - stoploss) - else: - current_price = open_price * (1 - current_profit) - expected_stop_price = open_price * (1 - desired_stop) - stoploss = stoploss_from_open(desired_stop, current_profit, True) - stop_price = current_price * (1 + stoploss) + assert stoploss >= 0 + # Technically the formula can yield values greater than 1 for shorts + # eventhough it doesn't make sense because the position would be liquidated + if side == 'long': + assert stoploss <= 1 - assert stoploss >= 0 - # Technically the formula can yield values greater than 1 for shorts - # eventhough it doesn't make sense because the position would be liquidated - if side == 'long': - assert stoploss <= 1 + # there is no correct answer if the expected stop price is above + # the current price + if ((side == 'long' and expected_stop_price > current_price) + or (side == 'short' and expected_stop_price < current_price)): + assert stoploss == 0 + else: + assert pytest.approx(stop_price) == expected_stop_price - # there is no correct answer if the expected stop price is above - # the current price - if ((side == 'long' and expected_stop_price > current_price) - or (side == 'short' and expected_stop_price < current_price)): - assert stoploss == 0 - else: - assert pytest.approx(stop_price) == expected_stop_price + +@pytest.mark.parametrize("side,rel_stop,curr_profit,leverage,expected", [ + # profit range for long is [-1, inf] while for shorts is [-inf, 1] + ("long", 0, -1, 1, 1), + ("long", 0, 0.1, 1, 0.09090909), + ("long", -0.1, 0.1, 1, 0.18181818), + ("long", 0.1, 0.2, 1, 0.08333333), + ("long", 0.1, 0.5, 1, 0.266666666), + ("long", 0.1, 5, 1, 0.816666666), # 500% profit, set stoploss to 10% above open price + ("long", 0, 5, 10, 3.3333333), # 500% profit, set stoploss break even + ("long", 0.1, 5, 10, 3.26666666), # 500% profit, set stoploss to 10% above open price + ("long", -0.1, 5, 10, 3.3999999), # 500% profit, set stoploss to 10% belowopen price + + ("short", 0, 0.1, 1, 0.1111111), + ("short", -0.1, 0.1, 1, 0.2222222), + ("short", 0.1, 0.2, 1, 0.125), + ("short", 0.1, 1, 1, 1), + ("short", -0.01, 5, 10, 10.01999999), # 500% profit at 10x +]) +def test_stoploss_from_open_leverage(side, rel_stop, curr_profit, leverage, expected): + + stoploss = stoploss_from_open(rel_stop, curr_profit, side == 'short', leverage) + assert pytest.approx(stoploss) == expected + open_rate = 100 + if stoploss != 1: + if side == 'long': + current_rate = open_rate * (1 + curr_profit / leverage) + stop = current_rate * (1 - stoploss / leverage) + assert pytest.approx(stop) == open_rate * (1 + rel_stop / leverage) + else: + current_rate = open_rate * (1 - curr_profit / leverage) + stop = current_rate * (1 + stoploss / leverage) + assert pytest.approx(stop) == open_rate * (1 - rel_stop / leverage) def test_stoploss_from_absolute(): diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 5fcc75026..4cdb35936 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -6,6 +6,7 @@ from pathlib import Path import pytest from pandas import DataFrame +from freqtrade.configuration import Configuration from freqtrade.exceptions import OperationalException from freqtrade.resolvers import StrategyResolver from freqtrade.strategy.interface import IStrategy @@ -68,7 +69,7 @@ def test_load_strategy(default_conf, dataframe_1m): def test_load_strategy_base64(dataframe_1m, caplog, default_conf): filepath = Path(__file__).parents[2] / 'freqtrade/templates/sample_strategy.py' encoded_string = urlsafe_b64encode(filepath.read_bytes()).decode("utf-8") - default_conf.update({'strategy': 'SampleStrategy:{}'.format(encoded_string)}) + default_conf.update({'strategy': f'SampleStrategy:{encoded_string}'}) strategy = StrategyResolver.load_strategy(default_conf) assert 'rsi' in strategy.advise_indicators(dataframe_1m, {'pair': 'ETH/BTC'}) @@ -175,6 +176,18 @@ def test_strategy_override_stoploss(caplog, default_conf): assert log_has("Override strategy 'stoploss' with value in config file: -0.5.", caplog) +def test_strategy_override_max_open_trades(caplog, default_conf): + caplog.set_level(logging.INFO) + default_conf.update({ + 'strategy': CURRENT_TEST_STRATEGY, + 'max_open_trades': 7 + }) + strategy = StrategyResolver.load_strategy(default_conf) + + assert strategy.max_open_trades == 7 + assert log_has("Override strategy 'max_open_trades' with value in config file: 7.", caplog) + + def test_strategy_override_trailing_stop(caplog, default_conf): caplog.set_level(logging.INFO) default_conf.update({ @@ -349,6 +362,38 @@ def test_strategy_override_use_exit_profit_only(caplog, default_conf): assert log_has("Override strategy 'exit_profit_only' with value in config file: True.", caplog) +def test_strategy_max_open_trades_infinity_from_strategy(caplog, default_conf): + caplog.set_level(logging.INFO) + default_conf.update({ + 'strategy': CURRENT_TEST_STRATEGY, + }) + del default_conf['max_open_trades'] + + strategy = StrategyResolver.load_strategy(default_conf) + + # this test assumes -1 set to 'max_open_trades' in CURRENT_TEST_STRATEGY + assert strategy.max_open_trades == float('inf') + assert default_conf['max_open_trades'] == float('inf') + + +def test_strategy_max_open_trades_infinity_from_config(caplog, default_conf, mocker): + caplog.set_level(logging.INFO) + default_conf.update({ + 'strategy': CURRENT_TEST_STRATEGY, + 'max_open_trades': -1, + 'exchange': 'binance' + }) + + configuration = Configuration(args=default_conf) + parsed_config = configuration.get_config() + + assert parsed_config['max_open_trades'] == float('inf') + + strategy = StrategyResolver.load_strategy(parsed_config) + + assert strategy.max_open_trades == float('inf') + + @ pytest.mark.filterwarnings("ignore:deprecated") def test_missing_implements(default_conf, caplog): @@ -438,3 +483,19 @@ def test_strategy_interface_versioning(dataframe_1m, default_conf): assert isinstance(exitdf, DataFrame) assert 'sell' not in exitdf assert 'exit_long' in exitdf + + +def test_strategy_ft_load_params_from_file(mocker, default_conf): + default_conf.update({'strategy': 'StrategyTestV2'}) + del default_conf['max_open_trades'] + mocker.patch('freqtrade.strategy.hyper.HyperStrategyMixin.load_params_from_file', + return_value={ + 'params': { + 'max_open_trades': { + 'max_open_trades': -1 + } + } + }) + strategy = StrategyResolver.load_strategy(default_conf) + assert strategy.max_open_trades == float('inf') + assert strategy.config['max_open_trades'] == float('inf') diff --git a/tests/test_binance_mig.py b/tests/test_binance_mig.py index a93ffbd28..5a5bbe9dc 100644 --- a/tests/test_binance_mig.py +++ b/tests/test_binance_mig.py @@ -53,7 +53,6 @@ def test_binance_mig_db_conversion(default_conf_usdt, fee, caplog): t.exchange = 'binance' Trade.commit() - default_conf_usdt['datadir'] = Path(default_conf_usdt['datadir']) default_conf_usdt['trading_mode'] = 'futures' migrate_binance_futures_names(default_conf_usdt) assert log_has('Migrating binance futures pairs in database.', caplog) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index cdf9f2f2e..aab868bec 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -58,7 +58,8 @@ def test_load_config_incorrect_stake_amount(default_conf) -> None: def test_load_config_file(default_conf, mocker, caplog) -> None: del default_conf['user_data_dir'] - file_mock = mocker.patch('freqtrade.configuration.load_config.open', mocker.mock_open( + default_conf['datadir'] = str(default_conf['datadir']) + file_mock = mocker.patch('freqtrade.configuration.load_config.Path.open', mocker.mock_open( read_data=json.dumps(default_conf) )) @@ -69,9 +70,11 @@ def test_load_config_file(default_conf, mocker, caplog) -> None: def test_load_config_file_error(default_conf, mocker, caplog) -> None: del default_conf['user_data_dir'] + default_conf['datadir'] = str(default_conf['datadir']) filedata = json.dumps(default_conf).replace( '"stake_amount": 0.001,', '"stake_amount": .001,') - mocker.patch('freqtrade.configuration.load_config.open', mocker.mock_open(read_data=filedata)) + mocker.patch('freqtrade.configuration.load_config.Path.open', + mocker.mock_open(read_data=filedata)) mocker.patch.object(Path, "read_text", MagicMock(return_value=filedata)) with pytest.raises(OperationalException, match=r".*Please verify the following segment.*"): @@ -80,6 +83,7 @@ def test_load_config_file_error(default_conf, mocker, caplog) -> None: def test_load_config_file_error_range(default_conf, mocker, caplog) -> None: del default_conf['user_data_dir'] + default_conf['datadir'] = str(default_conf['datadir']) filedata = json.dumps(default_conf).replace( '"stake_amount": 0.001,', '"stake_amount": .001,') mocker.patch.object(Path, "read_text", MagicMock(return_value=filedata)) @@ -238,6 +242,7 @@ def test_print_config(default_conf, mocker, caplog) -> None: conf1 = deepcopy(default_conf) # Delete non-json elements from default_conf del conf1['user_data_dir'] + conf1['datadir'] = str(conf1['datadir']) config_files = [conf1] configsmock = MagicMock(side_effect=config_files) @@ -268,7 +273,7 @@ def test_load_config_max_open_trades_minus_one(default_conf, mocker, caplog) -> def test_load_config_file_exception(mocker) -> None: mocker.patch( - 'freqtrade.configuration.configuration.open', + 'freqtrade.configuration.configuration.Path.open', MagicMock(side_effect=FileNotFoundError('File not found')) ) @@ -697,15 +702,16 @@ def test_set_loggers_journald(mocker): 'logfile': 'journald', } + setup_logging_pre() setup_logging(config) - assert len(logger.handlers) == 2 + assert len(logger.handlers) == 3 assert [x for x in logger.handlers if type(x).__name__ == "JournaldLogHandler"] assert [x for x in logger.handlers if type(x) == logging.StreamHandler] # reset handlers to not break pytest logger.handlers = orig_handlers -def test_set_loggers_journald_importerror(mocker, import_fails): +def test_set_loggers_journald_importerror(import_fails): logger = logging.getLogger() orig_handlers = logger.handlers logger.handlers = [] @@ -714,7 +720,7 @@ def test_set_loggers_journald_importerror(mocker, import_fails): 'logfile': 'journald', } with pytest.raises(OperationalException, - match=r'You need the systemd python package.*'): + match=r'You need the cysystemd python package.*'): setup_logging(config) logger.handlers = orig_handlers diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 7efd0393d..cea70ec48 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -10,6 +10,7 @@ from unittest.mock import ANY, MagicMock, PropertyMock, patch import arrow import pytest from pandas import DataFrame +from sqlalchemy import select from freqtrade.constants import CANCEL_REASON, UNLIMITED_STAKE_AMOUNT from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, RPCMessageType, RunMode, @@ -22,9 +23,10 @@ from freqtrade.persistence import Order, PairLocks, Trade from freqtrade.persistence.models import PairLock from freqtrade.plugins.protections.iprotection import ProtectionReturn from freqtrade.worker import Worker -from tests.conftest import (create_mock_trades, create_mock_trades_usdt, get_patched_freqtradebot, - get_patched_worker, log_has, log_has_re, patch_edge, patch_exchange, - patch_get_signal, patch_wallet, patch_whitelist) +from tests.conftest import (EXMS, create_mock_trades, create_mock_trades_usdt, + get_patched_freqtradebot, get_patched_worker, log_has, log_has_re, + patch_edge, patch_exchange, patch_get_signal, patch_wallet, + patch_whitelist) from tests.conftest_trades import (MOCK_TRADE_COUNT, entry_side, exit_side, mock_order_1, mock_order_2, mock_order_2_sell, mock_order_3, mock_order_3_sell, mock_order_4, mock_order_5_stoploss, mock_order_6_sell) @@ -46,7 +48,7 @@ def patch_RPCManager(mocker) -> MagicMock: def test_freqtradebot_state(mocker, default_conf_usdt, markets) -> None: - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets)) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) assert freqtrade.state is State.RUNNING @@ -164,7 +166,7 @@ def test_check_available_stake_amount( patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=limit_buy_order_usdt_open), get_fee=fee @@ -234,7 +236,7 @@ def test_edge_overrides_stoploss(limit_order, fee, caplog, mocker, 'last': enter_price, } mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value=ticker_val), get_fee=fee, ) @@ -246,7 +248,7 @@ def test_edge_overrides_stoploss(limit_order, fee, caplog, mocker, patch_get_signal(freqtrade) freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() caplog.clear() ############################################# ticker_val.update({ @@ -269,15 +271,15 @@ def test_total_open_trades_stakes(mocker, default_conf_usdt, ticker_usdt, fee) - patch_exchange(mocker) default_conf_usdt['max_open_trades'] = 2 mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=False), + _dry_is_price_crossed=MagicMock(return_value=False), ) freqtrade = FreqtradeBot(default_conf_usdt) patch_get_signal(freqtrade) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade is not None assert trade.stake_amount == 60.0 @@ -285,7 +287,7 @@ def test_total_open_trades_stakes(mocker, default_conf_usdt, ticker_usdt, fee) - assert trade.open_date is not None freqtrade.enter_positions() - trade = Trade.query.order_by(Trade.id.desc()).first() + trade = Trade.session.scalars(select(Trade).order_by(Trade.id.desc())).first() assert trade is not None assert trade.stake_amount == 60.0 @@ -304,10 +306,10 @@ def test_create_trade(default_conf_usdt, ticker_usdt, limit_order, patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=False), + _dry_is_price_crossed=MagicMock(return_value=False), ) # Save state of current whitelist @@ -316,7 +318,7 @@ def test_create_trade(default_conf_usdt, ticker_usdt, limit_order, patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.create_trade('ETH/USDT') - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade is not None assert pytest.approx(trade.stake_amount) == 60.0 @@ -340,7 +342,7 @@ def test_create_trade_no_stake_amount(default_conf_usdt, ticker_usdt, fee, mocke patch_exchange(mocker) patch_wallet(mocker, free=default_conf_usdt['stake_amount'] * 0.5) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, ) @@ -366,7 +368,7 @@ def test_create_trade_minimal_amount( patch_exchange(mocker) enter_mock = MagicMock(return_value=limit_order_open[entry_side(is_short)]) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=enter_mock, get_fee=fee, @@ -401,7 +403,7 @@ def test_enter_positions_no_pairs_left(default_conf_usdt, ticker_usdt, limit_buy patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=limit_buy_order_usdt_open), get_fee=fee, @@ -428,7 +430,7 @@ def test_enter_positions_global_pairlock(default_conf_usdt, ticker_usdt, limit_b patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value={'id': limit_buy_order_usdt['id']}), get_fee=fee, @@ -479,7 +481,7 @@ def test_create_trade_no_signal(default_conf_usdt, fee, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_fee=fee, ) default_conf_usdt['stake_amount'] = 10 @@ -502,7 +504,7 @@ def test_create_trades_multiple_trades( default_conf_usdt['dry_run_wallet'] = 60.0 * max_open mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=limit_buy_order_usdt_open), get_fee=fee, @@ -524,7 +526,7 @@ def test_create_trades_preopen(default_conf_usdt, ticker_usdt, fee, mocker, patch_exchange(mocker) default_conf_usdt['max_open_trades'] = 4 mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=limit_buy_order_usdt_open), get_fee=fee, @@ -558,7 +560,7 @@ def test_process_trade_creation(default_conf_usdt, ticker_usdt, limit_order, lim patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=limit_order_open[entry_side(is_short)]), fetch_order=MagicMock(return_value=limit_order[entry_side(is_short)]), @@ -567,12 +569,12 @@ def test_process_trade_creation(default_conf_usdt, ticker_usdt, limit_order, lim freqtrade = FreqtradeBot(default_conf_usdt) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) - trades = Trade.query.filter(Trade.is_open.is_(True)).all() + trades = Trade.get_open_trades() assert not trades freqtrade.process() - trades = Trade.query.filter(Trade.is_open.is_(True)).all() + trades = Trade.get_open_trades() assert len(trades) == 1 trade = trades[0] assert trade is not None @@ -594,7 +596,7 @@ def test_process_exchange_failures(default_conf_usdt, ticker_usdt, mocker) -> No patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(side_effect=TemporaryError) ) @@ -611,7 +613,7 @@ def test_process_operational_exception(default_conf_usdt, ticker_usdt, mocker) - msg_mock = patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(side_effect=OperationalException) ) @@ -630,7 +632,7 @@ def test_process_trade_handling(default_conf_usdt, ticker_usdt, limit_buy_order_ patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=limit_buy_order_usdt_open), fetch_order=MagicMock(return_value=limit_buy_order_usdt_open), @@ -639,11 +641,11 @@ def test_process_trade_handling(default_conf_usdt, ticker_usdt, limit_buy_order_ freqtrade = FreqtradeBot(default_conf_usdt) patch_get_signal(freqtrade) - trades = Trade.query.filter(Trade.is_open.is_(True)).all() + trades = Trade.get_open_trades() assert not trades freqtrade.process() - trades = Trade.query.filter(Trade.is_open.is_(True)).all() + trades = Trade.get_open_trades() assert len(trades) == 1 # Nothing happened ... @@ -657,7 +659,7 @@ def test_process_trade_no_whitelist_pair(default_conf_usdt, ticker_usdt, limit_b patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value={'id': limit_buy_order_usdt['id']}), fetch_order=MagicMock(return_value=limit_buy_order_usdt), @@ -670,7 +672,7 @@ def test_process_trade_no_whitelist_pair(default_conf_usdt, ticker_usdt, limit_b assert pair not in default_conf_usdt['exchange']['pair_whitelist'] # create open trade not in whitelist - Trade.query.session.add(Trade( + Trade.session.add(Trade( pair=pair, stake_amount=0.001, fee_open=fee.return_value, @@ -680,7 +682,7 @@ def test_process_trade_no_whitelist_pair(default_conf_usdt, ticker_usdt, limit_b open_rate=0.01, exchange='binance', )) - Trade.query.session.add(Trade( + Trade.session.add(Trade( pair='ETH/USDT', stake_amount=0.001, fee_open=fee.return_value, @@ -705,7 +707,7 @@ def test_process_informative_pairs_added(default_conf_usdt, ticker_usdt, mocker) refresh_mock = MagicMock() mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(side_effect=TemporaryError), refresh_latest_ohlcv=refresh_mock, @@ -737,20 +739,22 @@ def test_process_informative_pairs_added(default_conf_usdt, ticker_usdt, mocker) @pytest.mark.parametrize("is_short,trading_mode,exchange_name,margin_mode,liq_buffer,liq_price", [ (False, 'spot', 'binance', None, 0.0, None), (True, 'spot', 'binance', None, 0.0, None), - (False, 'spot', 'gateio', None, 0.0, None), - (True, 'spot', 'gateio', None, 0.0, None), + (False, 'spot', 'gate', None, 0.0, None), + (True, 'spot', 'gate', None, 0.0, None), (False, 'spot', 'okx', None, 0.0, None), (True, 'spot', 'okx', None, 0.0, None), (True, 'futures', 'binance', 'isolated', 0.0, 11.88151815181518), (False, 'futures', 'binance', 'isolated', 0.0, 8.080471380471382), - (True, 'futures', 'gateio', 'isolated', 0.0, 11.87413417771621), - (False, 'futures', 'gateio', 'isolated', 0.0, 8.085708510208207), + (True, 'futures', 'gate', 'isolated', 0.0, 11.87413417771621), + (False, 'futures', 'gate', 'isolated', 0.0, 8.085708510208207), (True, 'futures', 'binance', 'isolated', 0.05, 11.7874422442244), (False, 'futures', 'binance', 'isolated', 0.05, 8.17644781144781), - (True, 'futures', 'gateio', 'isolated', 0.05, 11.7804274688304), - (False, 'futures', 'gateio', 'isolated', 0.05, 8.181423084697796), + (True, 'futures', 'gate', 'isolated', 0.05, 11.7804274688304), + (False, 'futures', 'gate', 'isolated', 0.05, 8.181423084697796), (True, 'futures', 'okx', 'isolated', 0.0, 11.87413417771621), (False, 'futures', 'okx', 'isolated', 0.0, 8.085708510208207), + (True, 'futures', 'bybit', 'isolated', 0.0, 11.9), + (False, 'futures', 'bybit', 'isolated', 0.0, 8.1), ]) def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, limit_order_open, is_short, trading_mode, @@ -766,11 +770,11 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, ((wb + cum_b) - (side_1 * position * ep1)) / ((position * mmr_b) - (side_1 * position)) ((2 + 0.01) - (1 * 1 * 10)) / ((1 * 0.01) - (1 * 1)) = 8.070707070707071 - exchange_name = gateio/okx, is_short = true + exchange_name = gate/okx, is_short = true (open_rate + (wallet_balance / position)) / (1 + (mm_ratio + taker_fee_rate)) (10 + (2 / 1)) / (1 + (0.01 + 0.0006)) = 11.87413417771621 - exchange_name = gateio/okx, is_short = false + exchange_name = gate/okx, is_short = false (open_rate - (wallet_balance / position)) / (1 - (mm_ratio + taker_fee_rate)) (10 - (2 / 1)) / (1 - (0.01 + 0.0006)) = 8.085708510208207 """ @@ -783,7 +787,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, default_conf_usdt['exchange']['name'] = exchange_name if margin_mode: default_conf_usdt['margin_mode'] = margin_mode - mocker.patch('freqtrade.exchange.Gateio.validate_ordertypes') + mocker.patch('freqtrade.exchange.gate.Gate.validate_ordertypes') patch_RPCManager(mocker) patch_exchange(mocker, id=exchange_name) freqtrade = FreqtradeBot(default_conf_usdt) @@ -794,7 +798,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, enter_rate_mock = MagicMock(return_value=bid) enter_mm = MagicMock(return_value=open_order) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_rate=enter_rate_mock, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, @@ -811,7 +815,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, get_max_leverage=MagicMock(return_value=10), ) mocker.patch.multiple( - 'freqtrade.exchange.Okx', + 'freqtrade.exchange.okx.Okx', get_max_pair_stake_amount=MagicMock(return_value=500000), ) pair = 'ETH/USDT' @@ -835,7 +839,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, # Should create an open trade with an open order id # As the order is not fulfilled yet - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade assert trade.is_open is True @@ -860,10 +864,9 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, order['cost'] = 300 order['id'] = '444' - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=order)) + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=order)) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) - trade = Trade.query.all()[2] + trade = Trade.session.scalars(select(Trade)).all()[2] trade.is_short = is_short assert trade assert trade.open_order_id is None @@ -879,10 +882,9 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, order['average'] = 0.5 order['cost'] = 10.0 order['id'] = '555' - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=order)) + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=order)) assert freqtrade.execute_entry(pair, stake_amount) - trade = Trade.query.all()[3] + trade = Trade.session.scalars(select(Trade)).all()[3] trade.is_short = is_short assert trade assert trade.open_order_id is None @@ -895,7 +897,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, freqtrade.strategy.custom_stake_amount = lambda **kwargs: 150.0 assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) - trade = Trade.query.all()[4] + trade = Trade.session.scalars(select(Trade)).all()[4] trade.is_short = is_short assert trade assert pytest.approx(trade.stake_amount) == 150 @@ -904,7 +906,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, order['id'] = '557' freqtrade.strategy.custom_stake_amount = lambda **kwargs: 20 / 0 assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) - trade = Trade.query.all()[5] + trade = Trade.session.scalars(select(Trade)).all()[5] trade.is_short = is_short assert trade assert pytest.approx(trade.stake_amount) == 2.0 @@ -917,24 +919,23 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, order['average'] = 0.5 order['cost'] = 0.0 order['id'] = '66' - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=order)) + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=order)) assert not freqtrade.execute_entry(pair, stake_amount) assert freqtrade.strategy.leverage.call_count == 0 if trading_mode == 'spot' else 2 # Fail to get price... - mocker.patch('freqtrade.exchange.Exchange.get_rate', MagicMock(return_value=0.0)) + mocker.patch(f'{EXMS}.get_rate', MagicMock(return_value=0.0)) with pytest.raises(PricingError, match="Could not determine entry price."): freqtrade.execute_entry(pair, stake_amount, is_short=is_short) # In case of custom entry price - mocker.patch('freqtrade.exchange.Exchange.get_rate', return_value=0.50) + mocker.patch(f'{EXMS}.get_rate', return_value=0.50) order['status'] = 'open' order['id'] = '5566' freqtrade.strategy.custom_entry_price = lambda **kwargs: 0.508 assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) - trade = Trade.query.all()[6] + trade = Trade.session.scalars(select(Trade)).all()[6] trade.is_short = is_short assert trade assert trade.open_rate_requested == 0.508 @@ -946,12 +947,12 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, freqtrade.strategy.custom_entry_price = lambda **kwargs: None mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_rate=MagicMock(return_value=10), ) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) - trade = Trade.query.all()[7] + trade = Trade.session.scalars(select(Trade)).all()[7] trade.is_short = is_short assert trade assert trade.open_rate_requested == 10 @@ -961,7 +962,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, order['id'] = '5568' freqtrade.strategy.custom_entry_price = lambda **kwargs: "string price" assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) - trade = Trade.query.all()[8] + trade = Trade.session.scalars(select(Trade)).all()[8] # Trade(id=9, pair=ETH/USDT, amount=0.20000000, is_short=False, # leverage=1.0, open_rate=10.00000000, open_since=...) # Trade(id=9, pair=ETH/USDT, amount=0.60000000, is_short=True, @@ -976,13 +977,13 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, order['id'] = '55672' mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_max_pair_stake_amount=MagicMock(return_value=500), ) freqtrade.exchange.get_max_pair_stake_amount = MagicMock(return_value=500) assert freqtrade.execute_entry(pair, 2000, is_short=is_short) - trade = Trade.query.all()[9] + trade = Trade.session.scalars(select(Trade)).all()[9] trade.is_short = is_short assert pytest.approx(trade.stake_amount) == 500 @@ -991,7 +992,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, freqtrade.strategy.leverage.reset_mock() assert freqtrade.execute_entry(pair, 200, leverage_=3) assert freqtrade.strategy.leverage.call_count == 0 - trade = Trade.query.all()[10] + trade = Trade.session.scalars(select(Trade)).all()[10] assert trade.leverage == 1 if trading_mode == 'spot' else 3 @@ -999,7 +1000,7 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order, def test_execute_entry_confirm_error(mocker, default_conf_usdt, fee, limit_order, is_short) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -1034,7 +1035,7 @@ def test_execute_entry_min_leverage(mocker, default_conf_usdt, fee, limit_order, default_conf_usdt['margin_mode'] = 'isolated' freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -1053,7 +1054,7 @@ def test_execute_entry_min_leverage(mocker, default_conf_usdt, fee, limit_order, freqtrade.strategy.leverage = MagicMock(return_value=5.0) assert freqtrade.execute_entry(pair, stake_amount, is_short=is_short) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade.leverage == 5.0 # assert trade.stake_amount == 2 @@ -1064,11 +1065,11 @@ def test_add_stoploss_on_exchange(mocker, default_conf_usdt, limit_order, is_sho patch_exchange(mocker) order = limit_order[entry_side(is_short)] mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_trade', MagicMock(return_value=True)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=order) - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[]) + mocker.patch(f'{EXMS}.fetch_order', return_value=order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=[]) stoploss = MagicMock(return_value={'id': 13434334}) - mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss) + mocker.patch(f'{EXMS}.create_stoploss', stoploss) freqtrade = FreqtradeBot(default_conf_usdt) freqtrade.strategy.order_types['stoploss_on_exchange'] = True @@ -1096,7 +1097,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -1107,7 +1108,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ exit_order, ]), get_fee=fee, - stoploss=stoploss + create_stoploss=stoploss ) freqtrade = FreqtradeBot(default_conf_usdt) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) @@ -1133,7 +1134,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ trade.stoploss_order_id = "100" hanging_stoploss_order = MagicMock(return_value={'status': 'open'}) - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', hanging_stoploss_order) + mocker.patch(f'{EXMS}.fetch_stoploss_order', hanging_stoploss_order) assert freqtrade.handle_stoploss_on_exchange(trade) is False assert trade.stoploss_order_id == "100" @@ -1146,7 +1147,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ trade.stoploss_order_id = "100" canceled_stoploss_order = MagicMock(return_value={'status': 'canceled'}) - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', canceled_stoploss_order) + mocker.patch(f'{EXMS}.fetch_stoploss_order', canceled_stoploss_order) stoploss.reset_mock() assert freqtrade.handle_stoploss_on_exchange(trade) is False @@ -1158,7 +1159,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ # as a trade actually happened caplog.clear() freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True trade.open_order_id = None @@ -1181,17 +1182,14 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ 'average': 2, 'amount': enter_order['amount'], }) - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', stoploss_order_hit) + mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hit) assert freqtrade.handle_stoploss_on_exchange(trade) is True assert log_has_re(r'STOP_LOSS_LIMIT is hit for Trade\(id=1, .*\)\.', caplog) assert trade.stoploss_order_id is None assert trade.is_open is False caplog.clear() - mocker.patch( - 'freqtrade.exchange.Exchange.stoploss', - side_effect=ExchangeError() - ) + mocker.patch(f'{EXMS}.create_stoploss', side_effect=ExchangeError()) trade.is_open = True freqtrade.handle_stoploss_on_exchange(trade) assert log_has('Unable to place a stoploss order on exchange.', caplog) @@ -1201,9 +1199,8 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ # It should try to add stoploss order trade.stoploss_order_id = 100 stoploss.reset_mock() - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', - side_effect=InvalidOrderException()) - mocker.patch('freqtrade.exchange.Exchange.stoploss', stoploss) + mocker.patch(f'{EXMS}.fetch_stoploss_order', side_effect=InvalidOrderException()) + mocker.patch(f'{EXMS}.create_stoploss', stoploss) freqtrade.handle_stoploss_on_exchange(trade) assert stoploss.call_count == 1 @@ -1212,8 +1209,8 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ trade.stoploss_order_id = None trade.is_open = False stoploss.reset_mock() - mocker.patch('freqtrade.exchange.Exchange.fetch_order') - mocker.patch('freqtrade.exchange.Exchange.stoploss', stoploss) + mocker.patch(f'{EXMS}.fetch_order') + mocker.patch(f'{EXMS}.create_stoploss', stoploss) assert freqtrade.handle_stoploss_on_exchange(trade) is False assert stoploss.call_count == 0 @@ -1235,10 +1232,10 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ freqtrade.config['trailing_stop'] = True stoploss = MagicMock(side_effect=InvalidOrderException()) - mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order_with_result', + mocker.patch(f'{EXMS}.cancel_stoploss_order_with_result', side_effect=InvalidOrderException()) - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', stoploss_order_cancelled) - mocker.patch('freqtrade.exchange.Exchange.stoploss', stoploss) + mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_cancelled) + mocker.patch(f'{EXMS}.create_stoploss', stoploss) assert freqtrade.handle_stoploss_on_exchange(trade) is False assert trade.stoploss_order_id is None assert trade.is_open is False @@ -1254,7 +1251,7 @@ def test_handle_sle_cancel_cant_recreate(mocker, default_conf_usdt, fee, caplog, patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -1267,15 +1264,15 @@ def test_handle_sle_cancel_cant_recreate(mocker, default_conf_usdt, fee, caplog, get_fee=fee, ) mocker.patch.multiple( - 'freqtrade.exchange.Binance', + EXMS, fetch_stoploss_order=MagicMock(return_value={'status': 'canceled', 'id': 100}), - stoploss=MagicMock(side_effect=ExchangeError()), + create_stoploss=MagicMock(side_effect=ExchangeError()), ) freqtrade = FreqtradeBot(default_conf_usdt) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True trade.open_order_id = None @@ -1301,7 +1298,7 @@ def test_create_stoploss_order_invalid_order( {'id': order['id']} ]) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -1311,16 +1308,16 @@ def test_create_stoploss_order_invalid_order( get_fee=fee, ) mocker.patch.multiple( - 'freqtrade.exchange.Binance', + EXMS, fetch_order=MagicMock(return_value={'status': 'canceled'}), - stoploss=MagicMock(side_effect=InvalidOrderException()), + create_stoploss=MagicMock(side_effect=InvalidOrderException()), ) freqtrade = FreqtradeBot(default_conf_usdt) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.strategy.order_types['stoploss_on_exchange'] = True freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short caplog.clear() freqtrade.create_stoploss_order(trade, 200) @@ -1350,7 +1347,7 @@ def test_create_stoploss_order_insufficient_funds( mock_insuf = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_insufficient_funds') mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -1364,14 +1361,14 @@ def test_create_stoploss_order_insufficient_funds( fetch_order=MagicMock(return_value={'status': 'canceled'}), ) mocker.patch.multiple( - 'freqtrade.exchange.Binance', - stoploss=MagicMock(side_effect=InsufficientFundsError()), + EXMS, + create_stoploss=MagicMock(side_effect=InsufficientFundsError()), ) patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.strategy.order_types['stoploss_on_exchange'] = True freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short caplog.clear() freqtrade.create_stoploss_order(trade, 200) @@ -1401,7 +1398,7 @@ def test_handle_stoploss_on_exchange_trailing( stoploss = MagicMock(return_value={'id': 13434334}) patch_RPCManager(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 2.19, 'ask': 2.2, @@ -1414,8 +1411,8 @@ def test_handle_stoploss_on_exchange_trailing( get_fee=fee, ) mocker.patch.multiple( - 'freqtrade.exchange.Binance', - stoploss=stoploss, + EXMS, + create_stoploss=stoploss, stoploss_adjust=MagicMock(return_value=True), ) @@ -1439,7 +1436,7 @@ def test_handle_stoploss_on_exchange_trailing( patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True trade.open_order_id = None @@ -1457,7 +1454,7 @@ def test_handle_stoploss_on_exchange_trailing( } }) - mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', stoploss_order_hanging) + mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hanging) # stoploss initially at 5% assert freqtrade.handle_trade(trade) is False @@ -1465,7 +1462,7 @@ def test_handle_stoploss_on_exchange_trailing( # price jumped 2x mocker.patch( - 'freqtrade.exchange.Exchange.fetch_ticker', + f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': bid[0], 'ask': ask[0], @@ -1475,8 +1472,8 @@ def test_handle_stoploss_on_exchange_trailing( cancel_order_mock = MagicMock() stoploss_order_mock = MagicMock(return_value={'id': 'so1'}) - mocker.patch('freqtrade.exchange.Binance.cancel_stoploss_order', cancel_order_mock) - mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss_order_mock) + mocker.patch(f'{EXMS}.cancel_stoploss_order', cancel_order_mock) + mocker.patch(f'{EXMS}.create_stoploss', stoploss_order_mock) # stoploss should not be updated as the interval is 60 seconds assert freqtrade.handle_trade(trade) is False @@ -1504,7 +1501,7 @@ def test_handle_stoploss_on_exchange_trailing( # price fell below stoploss, so dry-run sells trade. mocker.patch( - 'freqtrade.exchange.Exchange.fetch_ticker', + f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': bid[1], 'ask': ask[1], @@ -1526,7 +1523,7 @@ def test_handle_stoploss_on_exchange_trailing_error( patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -1539,8 +1536,8 @@ def test_handle_stoploss_on_exchange_trailing_error( get_fee=fee, ) mocker.patch.multiple( - 'freqtrade.exchange.Binance', - stoploss=stoploss, + EXMS, + create_stoploss=stoploss, stoploss_adjust=MagicMock(return_value=True), ) @@ -1558,7 +1555,7 @@ def test_handle_stoploss_on_exchange_trailing_error( freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 60 patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True trade.open_order_id = None @@ -1577,9 +1574,9 @@ def test_handle_stoploss_on_exchange_trailing_error( 'stopPrice': '0.1' } } - mocker.patch('freqtrade.exchange.Binance.cancel_stoploss_order', + mocker.patch(f'{EXMS}.cancel_stoploss_order', side_effect=InvalidOrderException()) - mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', + mocker.patch(f'{EXMS}.fetch_stoploss_order', return_value=stoploss_order_hanging) freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging) assert log_has_re(r"Could not cancel stoploss order abcd for pair ETH/USDT.*", caplog) @@ -1590,8 +1587,8 @@ def test_handle_stoploss_on_exchange_trailing_error( # Fail creating stoploss order trade.stoploss_last_update = arrow.utcnow().shift(minutes=-601).datetime caplog.clear() - cancel_mock = mocker.patch("freqtrade.exchange.Binance.cancel_stoploss_order", MagicMock()) - mocker.patch("freqtrade.exchange.Binance.stoploss", side_effect=ExchangeError()) + cancel_mock = mocker.patch(f'{EXMS}.cancel_stoploss_order') + mocker.patch(f'{EXMS}.create_stoploss', side_effect=ExchangeError()) freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging) assert cancel_mock.call_count == 1 assert log_has_re(r"Could not create trailing stoploss order for pair ETH/USDT\..*", caplog) @@ -1601,15 +1598,15 @@ def test_stoploss_on_exchange_price_rounding( mocker, default_conf_usdt, fee, open_trade_usdt) -> None: patch_RPCManager(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_fee=fee, ) price_mock = MagicMock(side_effect=lambda p, s: int(s)) stoploss_mock = MagicMock(return_value={'id': '13434334'}) adjust_mock = MagicMock(return_value=False) mocker.patch.multiple( - 'freqtrade.exchange.Binance', - stoploss=stoploss_mock, + EXMS, + create_stoploss=stoploss_mock, stoploss_adjust=adjust_mock, price_to_precision=price_mock, ) @@ -1634,7 +1631,7 @@ def test_handle_stoploss_on_exchange_custom_stop( stoploss = MagicMock(return_value={'id': 13434334}) patch_RPCManager(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -1647,8 +1644,8 @@ def test_handle_stoploss_on_exchange_custom_stop( get_fee=fee, ) mocker.patch.multiple( - 'freqtrade.exchange.Binance', - stoploss=stoploss, + EXMS, + create_stoploss=stoploss, stoploss_adjust=MagicMock(return_value=True), ) @@ -1672,7 +1669,7 @@ def test_handle_stoploss_on_exchange_custom_stop( patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True trade.open_order_id = None @@ -1690,14 +1687,14 @@ def test_handle_stoploss_on_exchange_custom_stop( } }) - mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', stoploss_order_hanging) + mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hanging) assert freqtrade.handle_trade(trade) is False assert freqtrade.handle_stoploss_on_exchange(trade) is False # price jumped 2x mocker.patch( - 'freqtrade.exchange.Exchange.fetch_ticker', + f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': 4.38 if not is_short else 1.9 / 2, 'ask': 4.4 if not is_short else 2.2 / 2, @@ -1707,8 +1704,8 @@ def test_handle_stoploss_on_exchange_custom_stop( cancel_order_mock = MagicMock() stoploss_order_mock = MagicMock(return_value={'id': 'so1'}) - mocker.patch('freqtrade.exchange.Binance.cancel_stoploss_order', cancel_order_mock) - mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss_order_mock) + mocker.patch(f'{EXMS}.cancel_stoploss_order', cancel_order_mock) + mocker.patch(f'{EXMS}.create_stoploss', stoploss_order_mock) # stoploss should not be updated as the interval is 60 seconds assert freqtrade.handle_trade(trade) is False @@ -1738,7 +1735,7 @@ def test_handle_stoploss_on_exchange_custom_stop( # price fell below stoploss, so dry-run sells trade. mocker.patch( - 'freqtrade.exchange.Exchange.fetch_ticker', + f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': 4.17, 'ask': 4.19, @@ -1762,7 +1759,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, limit_orde edge_conf['dry_run_wallet'] = 999.9 edge_conf['exchange']['name'] = 'binance' mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 2.19, 'ask': 2.2, @@ -1773,7 +1770,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, limit_orde {'id': exit_order['id']}, ]), get_fee=fee, - stoploss=stoploss, + create_stoploss=stoploss, ) # enabling TSL @@ -1800,7 +1797,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, limit_orde freqtrade.active_pair_whitelist = freqtrade.edge.adjust(freqtrade.active_pair_whitelist) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_open = True trade.open_order_id = None trade.stoploss_order_id = 100 @@ -1815,7 +1812,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, limit_orde 'stopPrice': '2.178' }) - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', stoploss_order_hanging) + mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hanging) # stoploss initially at 20% as edge dictated it. assert freqtrade.handle_trade(trade) is False @@ -1824,11 +1821,11 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, limit_orde cancel_order_mock = MagicMock() stoploss_order_mock = MagicMock() - mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order', cancel_order_mock) - mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss_order_mock) + mocker.patch(f'{EXMS}.cancel_stoploss_order', cancel_order_mock) + mocker.patch(f'{EXMS}.create_stoploss', stoploss_order_mock) # price goes down 5% - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={ + mocker.patch(f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': 2.19 * 0.95, 'ask': 2.2 * 0.95, 'last': 2.19 * 0.95 @@ -1843,7 +1840,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, limit_orde cancel_order_mock.assert_not_called() # price jumped 2x - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={ + mocker.patch(f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': 4.38, 'ask': 4.4, 'last': 4.38 @@ -1893,9 +1890,8 @@ def test_exit_positions(mocker, default_conf_usdt, limit_order, is_short, caplog freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_trade', MagicMock(return_value=True)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', - return_value=limit_order[entry_side(is_short)]) - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[]) + mocker.patch(f'{EXMS}.fetch_order', return_value=limit_order[entry_side(is_short)]) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=[]) # TODO: should not be magicmock trade = MagicMock() @@ -1919,7 +1915,7 @@ def test_exit_positions(mocker, default_conf_usdt, limit_order, is_short, caplog def test_exit_positions_exception(mocker, default_conf_usdt, limit_order, caplog, is_short) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) order = limit_order[entry_side(is_short)] - mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=order) + mocker.patch(f'{EXMS}.fetch_order', return_value=order) # TODO: should not be magicmock trade = MagicMock() @@ -1945,8 +1941,8 @@ def test_update_trade_state(mocker, default_conf_usdt, limit_order, is_short, ca order = limit_order[entry_side(is_short)] mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_trade', MagicMock(return_value=True)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=order) - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[]) + mocker.patch(f'{EXMS}.fetch_order', return_value=order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=[]) mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount', return_value=0.0) order_id = order['id'] @@ -1997,7 +1993,7 @@ def test_update_trade_state(mocker, default_conf_usdt, limit_order, is_short, ca limit_buy_order_usdt_new['status'] = 'canceled' mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount', side_effect=ValueError) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=limit_buy_order_usdt_new) + mocker.patch(f'{EXMS}.fetch_order', return_value=limit_buy_order_usdt_new) res = freqtrade.update_trade_state(trade, order_id) # Cancelled empty assert res is True @@ -2016,9 +2012,9 @@ def test_update_trade_state_withorderdict( trades_for_order[0]['amount'] = initial_amount order_id = "oid_123456" order['id'] = order_id - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades_for_order) # fetch_order should not be called!! - mocker.patch('freqtrade.exchange.Exchange.fetch_order', MagicMock(side_effect=ValueError)) + mocker.patch(f'{EXMS}.fetch_order', MagicMock(side_effect=ValueError)) patch_exchange(mocker) amount = sum(x['amount'] for x in trades_for_order) freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) @@ -2060,7 +2056,7 @@ def test_update_trade_state_exception(mocker, default_conf_usdt, is_short, limit caplog) -> None: order = limit_order[entry_side(is_short)] freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=order) + mocker.patch(f'{EXMS}.fetch_order', return_value=order) # TODO: should not be magicmock trade = MagicMock() @@ -2078,8 +2074,7 @@ def test_update_trade_state_exception(mocker, default_conf_usdt, is_short, limit def test_update_trade_state_orderexception(mocker, default_conf_usdt, caplog) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', - MagicMock(side_effect=InvalidOrderException)) + mocker.patch(f'{EXMS}.fetch_order', MagicMock(side_effect=InvalidOrderException)) # TODO: should not be magicmock trade = MagicMock() @@ -2099,9 +2094,9 @@ def test_update_trade_state_sell( buy_order = limit_order[entry_side(is_short)] open_order = limit_order_open[exit_side(is_short)] l_order = limit_order[exit_side(is_short)] - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades_for_order) # fetch_order should not be called!! - mocker.patch('freqtrade.exchange.Exchange.fetch_order', MagicMock(side_effect=ValueError)) + mocker.patch(f'{EXMS}.fetch_order', MagicMock(side_effect=ValueError)) wallet_mock = MagicMock() mocker.patch('freqtrade.wallets.Wallets.update', wallet_mock) @@ -2151,7 +2146,7 @@ def test_handle_trade( patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 2.19, 'ask': 2.2, @@ -2168,7 +2163,7 @@ def test_handle_trade( freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade @@ -2204,7 +2199,7 @@ def test_handle_overlapping_signals( patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(side_effect=[ open_order, @@ -2223,7 +2218,7 @@ def test_handle_overlapping_signals( freqtrade.enter_positions() # Buy and Sell triggering, so doing nothing ... - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() nb_trades = len(trades) assert nb_trades == 0 @@ -2231,7 +2226,7 @@ def test_handle_overlapping_signals( # Buy is triggering, so buying ... patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.enter_positions() - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() for trade in trades: trade.is_short = is_short nb_trades = len(trades) @@ -2241,7 +2236,7 @@ def test_handle_overlapping_signals( # Buy and Sell are not triggering, so doing nothing ... patch_get_signal(freqtrade, enter_long=False) assert freqtrade.handle_trade(trades[0]) is False - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() for trade in trades: trade.is_short = is_short nb_trades = len(trades) @@ -2254,7 +2249,7 @@ def test_handle_overlapping_signals( else: patch_get_signal(freqtrade, enter_long=True, exit_long=True) assert freqtrade.handle_trade(trades[0]) is False - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() for trade in trades: trade.is_short = is_short nb_trades = len(trades) @@ -2266,7 +2261,7 @@ def test_handle_overlapping_signals( patch_get_signal(freqtrade, enter_long=False, exit_short=True) else: patch_get_signal(freqtrade, enter_long=False, exit_long=True) - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() for trade in trades: trade.is_short = is_short assert freqtrade.handle_trade(trades[0]) is True @@ -2282,7 +2277,7 @@ def test_handle_trade_roi(default_conf_usdt, ticker_usdt, limit_order_open, fee, patch_RPCManager(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(side_effect=[ open_order, @@ -2297,7 +2292,7 @@ def test_handle_trade_roi(default_conf_usdt, ticker_usdt, limit_order_open, fee, freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True @@ -2325,7 +2320,7 @@ def test_handle_trade_use_exit_signal( caplog.set_level(logging.DEBUG) patch_RPCManager(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(side_effect=[ enter_open_order, @@ -2339,7 +2334,7 @@ def test_handle_trade_use_exit_signal( freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True @@ -2365,7 +2360,7 @@ def test_close_trade( patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=open_order), get_fee=fee, @@ -2376,7 +2371,7 @@ def test_close_trade( # Create trade and sell it freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade @@ -2422,7 +2417,7 @@ def test_manage_open_orders_entry_usercustom( patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=old_order), cancel_order=cancel_order_mock, @@ -2433,7 +2428,7 @@ def test_manage_open_orders_entry_usercustom( open_trade.is_short = is_short open_trade.orders[0].side = 'sell' if is_short else 'buy' open_trade.orders[0].ft_order_side = 'sell' if is_short else 'buy' - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() # Ensure default is to return empty (so not mocked yet) @@ -2444,7 +2439,8 @@ def test_manage_open_orders_entry_usercustom( freqtrade.strategy.check_entry_timeout = MagicMock(return_value=False) freqtrade.manage_open_orders() assert cancel_order_mock.call_count == 0 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() nb_trades = len(trades) assert nb_trades == 1 assert freqtrade.strategy.check_entry_timeout.call_count == 1 @@ -2452,7 +2448,8 @@ def test_manage_open_orders_entry_usercustom( freqtrade.manage_open_orders() assert cancel_order_mock.call_count == 0 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() nb_trades = len(trades) assert nb_trades == 1 assert freqtrade.strategy.check_entry_timeout.call_count == 1 @@ -2462,7 +2459,8 @@ def test_manage_open_orders_entry_usercustom( freqtrade.manage_open_orders() assert cancel_order_wr_mock.call_count == 1 assert rpc_mock.call_count == 2 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() nb_trades = len(trades) assert nb_trades == 0 assert freqtrade.strategy.check_entry_timeout.call_count == 1 @@ -2483,7 +2481,7 @@ def test_manage_open_orders_entry( cancel_order_mock = MagicMock(return_value=limit_buy_cancel) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=old_order), cancel_order_with_result=cancel_order_mock, @@ -2492,7 +2490,7 @@ def test_manage_open_orders_entry( freqtrade = FreqtradeBot(default_conf_usdt) open_trade.is_short = is_short - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() freqtrade.strategy.check_entry_timeout = MagicMock(return_value=False) @@ -2501,7 +2499,8 @@ def test_manage_open_orders_entry( freqtrade.manage_open_orders() assert cancel_order_mock.call_count == 1 assert rpc_mock.call_count == 2 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() nb_trades = len(trades) assert nb_trades == 0 # Custom user buy-timeout is never called @@ -2522,7 +2521,7 @@ def test_adjust_entry_cancel( limit_buy_cancel['status'] = 'canceled' cancel_order_mock = MagicMock(return_value=limit_buy_cancel) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=old_order), cancel_order_with_result=cancel_order_mock, @@ -2530,7 +2529,7 @@ def test_adjust_entry_cancel( ) open_trade.is_short = is_short - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() # Timeout to not interfere @@ -2539,9 +2538,10 @@ def test_adjust_entry_cancel( # check that order is cancelled freqtrade.strategy.adjust_entry_price = MagicMock(return_value=None) freqtrade.manage_open_orders() - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() assert len(trades) == 0 - assert len(Order.query.all()) == 0 + assert len(Order.session.scalars(select(Order)).all()) == 0 assert log_has_re( f"{'Sell' if is_short else 'Buy'} order user requested order cancel*", caplog) assert log_has_re( @@ -2563,7 +2563,7 @@ def test_adjust_entry_maintain_replace( limit_buy_cancel['status'] = 'canceled' cancel_order_mock = MagicMock(return_value=limit_buy_cancel) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=old_order), cancel_order_with_result=cancel_order_mock, @@ -2571,7 +2571,7 @@ def test_adjust_entry_maintain_replace( ) open_trade.is_short = is_short - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() # Timeout to not interfere @@ -2580,7 +2580,8 @@ def test_adjust_entry_maintain_replace( # Check that order is maintained freqtrade.strategy.adjust_entry_price = MagicMock(return_value=old_order['price']) freqtrade.manage_open_orders() - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() assert len(trades) == 1 assert len(Order.get_open_orders()) == 1 # Entry adjustment is called @@ -2590,9 +2591,10 @@ def test_adjust_entry_maintain_replace( freqtrade.get_valid_enter_price_and_stake = MagicMock(return_value={100, 10, 1}) freqtrade.strategy.adjust_entry_price = MagicMock(return_value=1234) freqtrade.manage_open_orders() - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() assert len(trades) == 1 - nb_all_orders = len(Order.query.all()) + nb_all_orders = len(Order.session.scalars(select(Order)).all()) assert nb_all_orders == 2 # New order seems to be in closed status? # nb_open_orders = len(Order.get_open_orders()) @@ -2615,7 +2617,7 @@ def test_check_handle_cancelled_buy( patch_exchange(mocker) old_order.update({"status": "canceled", 'filled': 0.0}) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=old_order), cancel_order=cancel_order_mock, @@ -2624,14 +2626,15 @@ def test_check_handle_cancelled_buy( freqtrade = FreqtradeBot(default_conf_usdt) open_trade.orders = [] open_trade.is_short = is_short - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() # check it does cancel buy orders over the time limit freqtrade.manage_open_orders() assert cancel_order_mock.call_count == 0 assert rpc_mock.call_count == 2 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() assert len(trades) == 0 assert log_has_re( f"{'Sell' if is_short else 'Buy'} order cancelled on exchange for Trade.*", caplog) @@ -2645,7 +2648,7 @@ def test_manage_open_orders_buy_exception( cancel_order_mock = MagicMock() patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, validate_pairs=MagicMock(), fetch_ticker=ticker_usdt, fetch_order=MagicMock(side_effect=ExchangeError), @@ -2655,14 +2658,15 @@ def test_manage_open_orders_buy_exception( freqtrade = FreqtradeBot(default_conf_usdt) open_trade.is_short = is_short - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() # check it does cancel buy orders over the time limit freqtrade.manage_open_orders() assert cancel_order_mock.call_count == 0 assert rpc_mock.call_count == 1 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() nb_trades = len(trades) assert nb_trades == 1 @@ -2683,10 +2687,10 @@ def test_manage_open_orders_exit_usercustom( rpc_mock = patch_RPCManager(mocker) cancel_order_mock = MagicMock() patch_exchange(mocker) - mocker.patch('freqtrade.exchange.Exchange.get_min_pair_stake_amount', return_value=0.0) + mocker.patch(f'{EXMS}.get_min_pair_stake_amount', return_value=0.0) et_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.execute_trade_exit') mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=limit_sell_order_old), cancel_order=cancel_order_mock @@ -2697,7 +2701,7 @@ def test_manage_open_orders_exit_usercustom( open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime open_trade_usdt.close_profit_abs = 0.001 - Trade.query.session.add(open_trade_usdt) + Trade.session.add(open_trade_usdt) Trade.commit() # Ensure default is false freqtrade.manage_open_orders() @@ -2730,21 +2734,21 @@ def test_manage_open_orders_exit_usercustom( assert freqtrade.strategy.check_exit_timeout.call_count == 1 assert freqtrade.strategy.check_entry_timeout.call_count == 0 - # 2nd canceled trade - Fail execute sell + # 2nd canceled trade - Fail execute exit caplog.clear() open_trade_usdt.open_order_id = limit_sell_order_old['id'] mocker.patch('freqtrade.persistence.Trade.get_exit_order_count', return_value=1) mocker.patch('freqtrade.freqtradebot.FreqtradeBot.execute_trade_exit', side_effect=DependencyException) freqtrade.manage_open_orders() - assert log_has_re('Unable to emergency sell .*', caplog) + assert log_has_re('Unable to emergency exit .*', caplog) et_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.execute_trade_exit') caplog.clear() # 2nd canceled trade ... open_trade_usdt.open_order_id = limit_sell_order_old['id'] - # If cancelling fails - no emergency sell! + # If cancelling fails - no emergency exit! with patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_exit', return_value=False): freqtrade.manage_open_orders() assert et_mock.call_count == 0 @@ -2764,7 +2768,7 @@ def test_manage_open_orders_exit( limit_sell_order_old['side'] = 'buy' if is_short else 'sell' patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=limit_sell_order_old), cancel_order=cancel_order_mock, @@ -2777,7 +2781,7 @@ def test_manage_open_orders_exit( open_trade_usdt.close_profit_abs = 0.001 open_trade_usdt.is_short = is_short - Trade.query.session.add(open_trade_usdt) + Trade.session.add(open_trade_usdt) Trade.commit() freqtrade.strategy.check_exit_timeout = MagicMock(return_value=False) @@ -2806,7 +2810,7 @@ def test_check_handle_cancelled_exit( patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=limit_sell_order_old), cancel_order_with_result=cancel_order_mock @@ -2817,7 +2821,7 @@ def test_check_handle_cancelled_exit( open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime open_trade_usdt.is_short = is_short - Trade.query.session.add(open_trade_usdt) + Trade.session.add(open_trade_usdt) Trade.commit() # check it does cancel sell orders over the time limit @@ -2847,14 +2851,14 @@ def test_manage_open_orders_partial( cancel_order_mock = MagicMock(return_value=limit_buy_canceled) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=limit_buy_order_old_partial), cancel_order_with_result=cancel_order_mock ) freqtrade = FreqtradeBot(default_conf_usdt) prior_stake = open_trade.stake_amount - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() # check it does cancel buy orders over the time limit @@ -2862,7 +2866,8 @@ def test_manage_open_orders_partial( freqtrade.manage_open_orders() assert cancel_order_mock.call_count == 1 assert rpc_mock.call_count == 3 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() assert len(trades) == 1 assert trades[0].amount == 23.0 assert trades[0].stake_amount == open_trade.open_rate * trades[0].amount / leverage @@ -2887,7 +2892,7 @@ def test_manage_open_orders_partial_fee( mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=0)) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=limit_buy_order_old_partial), cancel_order_with_result=cancel_order_mock, @@ -2899,7 +2904,7 @@ def test_manage_open_orders_partial_fee( open_trade.fee_open = fee() open_trade.fee_close = fee() - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() # cancelling a half-filled order should update the amount to the bought amount # and apply fees if necessary. @@ -2909,7 +2914,8 @@ def test_manage_open_orders_partial_fee( assert cancel_order_mock.call_count == 1 assert rpc_mock.call_count == 3 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() assert len(trades) == 1 # Verify that trade has been updated assert trades[0].amount == (limit_buy_order_old_partial['amount'] - @@ -2935,7 +2941,7 @@ def test_manage_open_orders_partial_except( cancel_order_mock = MagicMock(return_value=limit_buy_order_old_partial_canceled) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(return_value=limit_buy_order_old_partial), cancel_order_with_result=cancel_order_mock, @@ -2949,7 +2955,7 @@ def test_manage_open_orders_partial_except( open_trade.fee_open = fee() open_trade.fee_close = fee() - Trade.query.session.add(open_trade) + Trade.session.add(open_trade) Trade.commit() # cancelling a half-filled order should update the amount to the bought amount # and apply fees if necessary. @@ -2959,7 +2965,8 @@ def test_manage_open_orders_partial_except( assert cancel_order_mock.call_count == 1 assert rpc_mock.call_count == 3 - trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all() + trades = Trade.session.scalars( + select(Trade).filter(Trade.open_order_id.is_(open_trade.open_order_id))).all() assert len(trades) == 1 # Verify that trade has been updated @@ -2981,14 +2988,14 @@ def test_manage_open_orders_exception(default_conf_usdt, ticker_usdt, open_trade handle_cancel_exit=MagicMock(), ) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, fetch_order=MagicMock(side_effect=ExchangeError('Oh snap')), cancel_order=cancel_order_mock ) freqtrade = FreqtradeBot(default_conf_usdt) - Trade.query.session.add(open_trade_usdt) + Trade.session.add(open_trade_usdt) Trade.commit() caplog.clear() @@ -3011,13 +3018,13 @@ def test_handle_cancel_enter(mocker, caplog, default_conf_usdt, limit_order, is_ del cancel_buy_order['filled'] cancel_order_mock = MagicMock(return_value=cancel_buy_order) - mocker.patch('freqtrade.exchange.Exchange.cancel_order_with_result', cancel_order_mock) + mocker.patch(f'{EXMS}.cancel_order_with_result', cancel_order_mock) freqtrade = FreqtradeBot(default_conf_usdt) freqtrade._notify_enter_cancel = MagicMock() trade = mock_trade_usdt_4(fee, is_short) - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() l_order['filled'] = 0.0 @@ -3042,11 +3049,12 @@ def test_handle_cancel_enter(mocker, caplog, default_conf_usdt, limit_order, is_ # Order remained open for some reason (cancel failed) cancel_buy_order['status'] = 'open' cancel_order_mock = MagicMock(return_value=cancel_buy_order) - mocker.patch('freqtrade.exchange.Exchange.cancel_order_with_result', cancel_order_mock) + trade.open_order_id = 'some_open_order' + mocker.patch(f'{EXMS}.cancel_order_with_result', cancel_order_mock) assert not freqtrade.handle_cancel_enter(trade, l_order, reason) assert log_has_re(r"Order .* for .* not cancelled.", caplog) # min_pair_stake empty should not crash - mocker.patch('freqtrade.exchange.Exchange.get_min_pair_stake_amount', return_value=None) + mocker.patch(f'{EXMS}.get_min_pair_stake_amount', return_value=None) assert not freqtrade.handle_cancel_enter(trade, limit_order[entry_side(is_short)], reason) @@ -3058,15 +3066,15 @@ def test_handle_cancel_enter_exchanges(mocker, caplog, default_conf_usdt, is_sho patch_RPCManager(mocker) patch_exchange(mocker) cancel_order_mock = mocker.patch( - 'freqtrade.exchange.Exchange.cancel_order_with_result', + f'{EXMS}.cancel_order_with_result', return_value=limit_buy_order_canceled_empty) - nofiy_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot._notify_enter_cancel') + notify_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot._notify_enter_cancel') freqtrade = FreqtradeBot(default_conf_usdt) reason = CANCEL_REASON['TIMEOUT'] trade = mock_trade_usdt_4(fee, is_short) - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() assert freqtrade.handle_cancel_enter(trade, limit_buy_order_canceled_empty, reason) assert cancel_order_mock.call_count == 0 @@ -3075,7 +3083,7 @@ def test_handle_cancel_enter_exchanges(mocker, caplog, default_conf_usdt, is_sho r'Removing .* from database\.', caplog ) - assert nofiy_mock.call_count == 1 + assert notify_mock.call_count == 1 @pytest.mark.parametrize("is_short", [False, True]) @@ -3092,7 +3100,7 @@ def test_handle_cancel_enter_corder_empty(mocker, default_conf_usdt, limit_order l_order = limit_order[entry_side(is_short)] cancel_order_mock = MagicMock(return_value=cancelorder) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, cancel_order=cancel_order_mock, fetch_order=MagicMock(side_effect=InvalidOrderException) ) @@ -3100,7 +3108,7 @@ def test_handle_cancel_enter_corder_empty(mocker, default_conf_usdt, limit_order freqtrade = FreqtradeBot(default_conf_usdt) freqtrade._notify_enter_cancel = MagicMock() trade = mock_trade_usdt_4(fee, is_short) - Trade.query.session.add(trade) + Trade.session.add(trade) Trade.commit() l_order['filled'] = 0.0 l_order['status'] = 'open' @@ -3112,7 +3120,7 @@ def test_handle_cancel_enter_corder_empty(mocker, default_conf_usdt, limit_order l_order['filled'] = 1.0 order = deepcopy(l_order) order['status'] = 'canceled' - mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=order) + mocker.patch(f'{EXMS}.fetch_order', return_value=order) assert not freqtrade.handle_cancel_enter(trade, l_order, reason) assert cancel_order_mock.call_count == 1 @@ -3122,11 +3130,11 @@ def test_handle_cancel_exit_limit(mocker, default_conf_usdt, fee) -> None: patch_exchange(mocker) cancel_order_mock = MagicMock() mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, cancel_order=cancel_order_mock, ) - mocker.patch('freqtrade.exchange.Exchange.get_rate', return_value=0.245441) - mocker.patch('freqtrade.exchange.Exchange.get_min_pair_stake_amount', return_value=0.2) + mocker.patch(f'{EXMS}.get_rate', return_value=0.245441) + mocker.patch(f'{EXMS}.get_min_pair_stake_amount', return_value=0.2) mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_order_fee') @@ -3228,9 +3236,8 @@ def test_handle_cancel_exit_limit(mocker, default_conf_usdt, fee) -> None: def test_handle_cancel_exit_cancel_exception(mocker, default_conf_usdt) -> None: patch_RPCManager(mocker) patch_exchange(mocker) - mocker.patch('freqtrade.exchange.Exchange.get_min_pair_stake_amount', return_value=0.0) - mocker.patch('freqtrade.exchange.Exchange.cancel_order_with_result', - side_effect=InvalidOrderException()) + mocker.patch(f'{EXMS}.get_min_pair_stake_amount', return_value=0.0) + mocker.patch(f'{EXMS}.cancel_order_with_result', side_effect=InvalidOrderException()) freqtrade = FreqtradeBot(default_conf_usdt) @@ -3238,6 +3245,7 @@ def test_handle_cancel_exit_cancel_exception(mocker, default_conf_usdt) -> None: trade = MagicMock() reason = CANCEL_REASON['TIMEOUT'] order = {'remaining': 1, + 'id': '125', 'amount': 1, 'status': "open"} assert not freqtrade.handle_cancel_exit(trade, order, reason) @@ -3252,10 +3260,10 @@ def test_execute_trade_exit_up(default_conf_usdt, ticker_usdt, fee, ticker_usdt_ rpc_mock = patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=False), + _dry_is_price_crossed=MagicMock(return_value=False), ) patch_whitelist(mocker, default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt) @@ -3266,14 +3274,14 @@ def test_execute_trade_exit_up(default_conf_usdt, ticker_usdt, fee, ticker_usdt_ freqtrade.enter_positions() rpc_mock.reset_mock() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade.is_short == is_short assert trade assert freqtrade.strategy.confirm_trade_exit.call_count == 0 # Increase the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt_sell_down if is_short else ticker_usdt_sell_up ) # Prevented sell ... @@ -3335,10 +3343,10 @@ def test_execute_trade_exit_down(default_conf_usdt, ticker_usdt, fee, ticker_usd rpc_mock = patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=False), + _dry_is_price_crossed=MagicMock(return_value=False), ) patch_whitelist(mocker, default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt) @@ -3347,13 +3355,13 @@ def test_execute_trade_exit_down(default_conf_usdt, ticker_usdt, fee, ticker_usd # Create some test data freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade # Decrease the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt_sell_up if is_short else ticker_usdt_sell_down ) freqtrade.execute_trade_exit( @@ -3404,10 +3412,10 @@ def test_execute_trade_exit_custom_exit_price( rpc_mock = patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=False), + _dry_is_price_crossed=MagicMock(return_value=False), ) config = deepcopy(default_conf_usdt) config['custom_price_max_distance_ratio'] = 0.1 @@ -3420,14 +3428,14 @@ def test_execute_trade_exit_custom_exit_price( freqtrade.enter_positions() rpc_mock.reset_mock() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade assert freqtrade.strategy.confirm_trade_exit.call_count == 0 # Increase the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt_sell_up ) @@ -3485,10 +3493,10 @@ def test_execute_trade_exit_down_stoploss_on_exchange_dry_run( rpc_mock = patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=False), + _dry_is_price_crossed=MagicMock(return_value=False), ) patch_whitelist(mocker, default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt) @@ -3497,13 +3505,13 @@ def test_execute_trade_exit_down_stoploss_on_exchange_dry_run( # Create some test data freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade.is_short == is_short assert trade # Decrease the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt_sell_up if is_short else ticker_usdt_sell_down ) @@ -3553,8 +3561,7 @@ def test_execute_trade_exit_down_stoploss_on_exchange_dry_run( def test_execute_trade_exit_sloe_cancel_exception( mocker, default_conf_usdt, ticker_usdt, fee, caplog) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) - mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order', - side_effect=InvalidOrderException()) + mocker.patch(f'{EXMS}.cancel_stoploss_order', side_effect=InvalidOrderException()) mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=300)) create_order_mock = MagicMock(side_effect=[ {'id': '12345554'}, @@ -3562,7 +3569,7 @@ def test_execute_trade_exit_sloe_cancel_exception( ]) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, create_order=create_order_mock, @@ -3572,7 +3579,7 @@ def test_execute_trade_exit_sloe_cancel_exception( patch_get_signal(freqtrade) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() PairLock.session = MagicMock() freqtrade.config['dry_run'] = False @@ -3600,14 +3607,14 @@ def test_execute_trade_exit_with_stoploss_on_exchange( cancel_order = MagicMock(return_value=True) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, amount_to_precision=lambda s, x, y: y, price_to_precision=lambda s, x, y: y, - stoploss=stoploss, + create_stoploss=stoploss, cancel_stoploss_order=cancel_order, - _is_dry_limit_order_filled=MagicMock(side_effect=[True, False]), + _dry_is_price_crossed=MagicMock(side_effect=[True, False]), ) freqtrade = FreqtradeBot(default_conf_usdt) @@ -3617,7 +3624,7 @@ def test_execute_trade_exit_with_stoploss_on_exchange( # Create some test data freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade trades = [trade] @@ -3627,7 +3634,7 @@ def test_execute_trade_exit_with_stoploss_on_exchange( # Increase the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt_sell_up ) @@ -3637,7 +3644,7 @@ def test_execute_trade_exit_with_stoploss_on_exchange( exit_check=ExitCheckTuple(exit_type=ExitType.STOP_LOSS) ) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade assert cancel_order.call_count == 1 @@ -3651,12 +3658,12 @@ def test_may_execute_trade_exit_after_stoploss_on_exchange_hit( rpc_mock = patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, amount_to_precision=lambda s, x, y: y, price_to_precision=lambda s, x, y: y, - _is_dry_limit_order_filled=MagicMock(side_effect=[False, True]), + _dry_is_price_crossed=MagicMock(side_effect=[False, True]), ) stoploss = MagicMock(return_value={ @@ -3666,7 +3673,7 @@ def test_may_execute_trade_exit_after_stoploss_on_exchange_hit( } }) - mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss) + mocker.patch(f'{EXMS}.create_stoploss', stoploss) freqtrade = FreqtradeBot(default_conf_usdt) freqtrade.strategy.order_types['stoploss_on_exchange'] = True @@ -3675,7 +3682,7 @@ def test_may_execute_trade_exit_after_stoploss_on_exchange_hit( # Create some test data freqtrade.enter_positions() freqtrade.manage_open_orders() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trades = [trade] assert trade.stoploss_order_id is None @@ -3705,7 +3712,7 @@ def test_may_execute_trade_exit_after_stoploss_on_exchange_hit( "fee": None, "trades": None }) - mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', stoploss_executed) + mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_executed) freqtrade.exit_positions(trades) assert trade.stoploss_order_id is None @@ -3748,10 +3755,10 @@ def test_execute_trade_exit_market_order( rpc_mock = patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=True), + _dry_is_price_crossed=MagicMock(return_value=True), get_funding_fees=MagicMock(side_effect=ExchangeError()), ) patch_whitelist(mocker, default_conf_usdt) @@ -3761,15 +3768,15 @@ def test_execute_trade_exit_market_order( # Create some test data freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade # Increase the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt_sell_up, - _is_dry_limit_order_filled=MagicMock(return_value=False), + _dry_is_price_crossed=MagicMock(return_value=False), ) freqtrade.config['order_types']['exit'] = 'market' @@ -3823,7 +3830,7 @@ def test_execute_trade_exit_insufficient_funds_error(default_conf_usdt, ticker_u freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mock_insuf = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_insufficient_funds') mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, create_order=MagicMock(side_effect=[ @@ -3836,13 +3843,13 @@ def test_execute_trade_exit_insufficient_funds_error(default_conf_usdt, ticker_u # Create some test data freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade # Increase the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt_sell_up ) @@ -3877,7 +3884,7 @@ def test_exit_profit_only( patch_exchange(mocker) eside = entry_side(is_short) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': bid, 'ask': ask, @@ -3900,11 +3907,11 @@ def test_exit_profit_only( if exit_type == ExitType.EXIT_SIGNAL.value: freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) else: - freqtrade.strategy.stop_loss_reached = MagicMock(return_value=ExitCheckTuple( + freqtrade.strategy.ft_stoploss_reached = MagicMock(return_value=ExitCheckTuple( exit_type=ExitType.NONE)) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade.is_short == is_short oobj = Order.parse_from_ccxt_object(limit_order[eside], limit_order[eside]['symbol'], eside) trade.update_order(limit_order[eside]) @@ -3928,7 +3935,7 @@ def test_sell_not_enough_balance(default_conf_usdt, limit_order, limit_order_ope patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 0.00002172, 'ask': 0.00002173, @@ -3947,7 +3954,7 @@ def test_sell_not_enough_balance(default_conf_usdt, limit_order, limit_order_ope freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() amnt = trade.amount oobj = Order.parse_from_ccxt_object(limit_order['buy'], limit_order['buy']['symbol'], 'buy') @@ -4005,7 +4012,7 @@ def test_locked_pairs(default_conf_usdt, ticker_usdt, fee, patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, ) @@ -4015,13 +4022,13 @@ def test_locked_pairs(default_conf_usdt, ticker_usdt, fee, # Create some test data freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short assert trade # Decrease the price and sell it mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt_sell_down ) @@ -4050,7 +4057,7 @@ def test_ignore_roi_if_entry_signal(default_conf_usdt, limit_order, limit_order_ patch_exchange(mocker) eside = entry_side(is_short) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 2.19, 'ask': 2.2, @@ -4070,7 +4077,7 @@ def test_ignore_roi_if_entry_signal(default_conf_usdt, limit_order, limit_order_ freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short oobj = Order.parse_from_ccxt_object( limit_order[eside], limit_order[eside]['symbol'], eside) @@ -4101,7 +4108,7 @@ def test_trailing_stop_loss(default_conf_usdt, limit_order_open, patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 2.0, 'ask': 2.0, @@ -4120,12 +4127,12 @@ def test_trailing_stop_loss(default_conf_usdt, limit_order_open, freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade.is_short == is_short assert freqtrade.handle_trade(trade) is False # Raise praise into profits - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', + mocker.patch(f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': 2.0 * val1, 'ask': 2.0 * val1, @@ -4136,7 +4143,7 @@ def test_trailing_stop_loss(default_conf_usdt, limit_order_open, assert freqtrade.handle_trade(trade) is False caplog.clear() # Price fell - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', + mocker.patch(f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': 2.0 * val2, 'ask': 2.0 * val2, @@ -4171,7 +4178,7 @@ def test_trailing_stop_loss_positive( patch_exchange(mocker) eside = entry_side(is_short) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': enter_price - (-0.01 if is_short else 0.01), 'ask': enter_price - (-0.01 if is_short else 0.01), @@ -4195,7 +4202,7 @@ def test_trailing_stop_loss_positive( freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade.is_short == is_short oobj = Order.parse_from_ccxt_object(limit_order[eside], limit_order[eside]['symbol'], eside) trade.update_order(limit_order[eside]) @@ -4206,7 +4213,7 @@ def test_trailing_stop_loss_positive( # Raise ticker_usdt above buy price mocker.patch( - 'freqtrade.exchange.Exchange.fetch_ticker', + f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': enter_price + (-0.06 if is_short else 0.06), 'ask': enter_price + (-0.06 if is_short else 0.06), @@ -4228,7 +4235,7 @@ def test_trailing_stop_loss_positive( caplog.clear() mocker.patch( - 'freqtrade.exchange.Exchange.fetch_ticker', + f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': enter_price + (-0.135 if is_short else 0.125), 'ask': enter_price + (-0.135 if is_short else 0.125), @@ -4244,7 +4251,7 @@ def test_trailing_stop_loss_positive( assert log_has("ETH/USDT - Adjusting stoploss...", caplog) mocker.patch( - 'freqtrade.exchange.Exchange.fetch_ticker', + f'{EXMS}.fetch_ticker', MagicMock(return_value={ 'bid': enter_price + (-0.02 if is_short else 0.02), 'ask': enter_price + (-0.02 if is_short else 0.02), @@ -4269,7 +4276,7 @@ def test_disable_ignore_roi_if_entry_signal(default_conf_usdt, limit_order, limi patch_exchange(mocker) eside = entry_side(is_short) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 2.0, 'ask': 2.0, @@ -4281,7 +4288,7 @@ def test_disable_ignore_roi_if_entry_signal(default_conf_usdt, limit_order, limi {'id': 1234553383} ]), get_fee=fee, - _is_dry_limit_order_filled=MagicMock(return_value=False), + _dry_is_price_crossed=MagicMock(return_value=False), ) default_conf_usdt['exit_pricing'] = { 'ignore_roi_if_entry_signal': False @@ -4292,7 +4299,7 @@ def test_disable_ignore_roi_if_entry_signal(default_conf_usdt, limit_order, limi freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short oobj = Order.parse_from_ccxt_object( @@ -4310,7 +4317,7 @@ def test_disable_ignore_roi_if_entry_signal(default_conf_usdt, limit_order, limi def test_get_real_amount_quote(default_conf_usdt, trades_for_order, buy_order_fee, fee, caplog, mocker): - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades_for_order) amount = sum(x['amount'] for x in trades_for_order) trade = Trade( pair='LTC/ETH', @@ -4336,7 +4343,7 @@ def test_get_real_amount_quote(default_conf_usdt, trades_for_order, buy_order_fe def test_get_real_amount_quote_dust(default_conf_usdt, trades_for_order, buy_order_fee, fee, caplog, mocker): - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades_for_order) walletmock = mocker.patch('freqtrade.wallets.Wallets.update') mocker.patch('freqtrade.wallets.Wallets.get_free', return_value=8.1122) amount = sum(x['amount'] for x in trades_for_order) @@ -4361,7 +4368,7 @@ def test_get_real_amount_quote_dust(default_conf_usdt, trades_for_order, buy_ord def test_get_real_amount_no_trade(default_conf_usdt, buy_order_fee, caplog, mocker, fee): - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[]) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=[]) amount = buy_order_fee['amount'] trade = Trade( @@ -4415,7 +4422,7 @@ def test_get_real_amount( buy_order['fee'] = fee_par trades_for_order[0]['fee'] = fee_par - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades_for_order) amount = sum(x['amount'] for x in trades_for_order) trade = Trade( pair='LTC/ETH', @@ -4429,7 +4436,7 @@ def test_get_real_amount( freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) if not use_ticker_usdt_rate: - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', side_effect=ExchangeError) + mocker.patch(f'{EXMS}.fetch_ticker', side_effect=ExchangeError) caplog.clear() order_obj = Order.parse_from_ccxt_object(buy_order_fee, 'LTC/ETH', 'buy') @@ -4461,7 +4468,7 @@ def test_get_real_amount_multi( if fee_currency: trades_for_order[0]['fee']['currency'] = fee_currency - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades_for_order) amount = float(sum(x['amount'] for x in trades_for_order)) default_conf_usdt['stake_currency'] = "ETH" @@ -4478,8 +4485,8 @@ def test_get_real_amount_multi( # Fake markets entry to enable fee parsing markets['BNB/ETH'] = markets['ETH/USDT'] freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets)) - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) + mocker.patch(f'{EXMS}.fetch_ticker', return_value={'ask': 0.19, 'last': 0.2}) # Amount is reduced by "fee" @@ -4508,7 +4515,7 @@ def test_get_real_amount_invalid_order(default_conf_usdt, trades_for_order, buy_ limit_buy_order_usdt = deepcopy(buy_order_fee) limit_buy_order_usdt['fee'] = {'cost': 0.004} - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[]) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=[]) amount = float(sum(x['amount'] for x in trades_for_order)) trade = Trade( pair='LTC/ETH', @@ -4529,9 +4536,9 @@ def test_get_real_amount_invalid_order(default_conf_usdt, trades_for_order, buy_ def test_get_real_amount_fees_order(default_conf_usdt, market_buy_order_usdt_doublefee, fee, mocker): - tfo_mock = mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[]) - mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination', return_value='BNB/USDT') - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', return_value={'last': 200}) + tfo_mock = mocker.patch(f'{EXMS}.get_trades_for_order', return_value=[]) + mocker.patch(f'{EXMS}.get_valid_pair_combination', return_value='BNB/USDT') + mocker.patch(f'{EXMS}.fetch_ticker', return_value={'last': 200}) trade = Trade( pair='LTC/USDT', amount=30.0, @@ -4557,7 +4564,7 @@ def test_get_real_amount_wrong_amount(default_conf_usdt, trades_for_order, buy_o limit_buy_order_usdt = deepcopy(buy_order_fee) limit_buy_order_usdt['amount'] = limit_buy_order_usdt['amount'] - 0.001 - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades_for_order) amount = float(sum(x['amount'] for x in trades_for_order)) trade = Trade( pair='LTC/ETH', @@ -4582,7 +4589,7 @@ def test_get_real_amount_wrong_amount_rounding(default_conf_usdt, trades_for_ord limit_buy_order_usdt = deepcopy(buy_order_fee) trades_for_order[0]['amount'] = trades_for_order[0]['amount'] + 1e-15 - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades_for_order) amount = float(sum(x['amount'] for x in trades_for_order)) trade = Trade( pair='LTC/ETH', @@ -4662,7 +4669,7 @@ def test_get_real_amount_in_point(default_conf_usdt, buy_order_fee, fee, mocker, ] }] - mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades) + mocker.patch(f'{EXMS}.get_trades_for_order', return_value=trades) amount = float(sum(x['amount'] for x in trades)) trade = Trade( pair='CEL/USDT', @@ -4744,9 +4751,9 @@ def test_order_book_depth_of_market( default_conf_usdt['entry_pricing']['check_depth_of_market']['bids_to_ask_delta'] = delta patch_RPCManager(mocker) patch_exchange(mocker) - mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2) + mocker.patch(f'{EXMS}.fetch_l2_order_book', order_book_l2) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=limit_order_open[entry_side(is_short)]), get_fee=fee, @@ -4758,7 +4765,7 @@ def test_order_book_depth_of_market( patch_get_signal(freqtrade, enter_short=is_short, enter_long=not is_short) freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() if is_high_delta: assert trade is None else: @@ -4769,7 +4776,7 @@ def test_order_book_depth_of_market( assert trade.open_date is not None assert trade.exchange == 'binance' - assert len(Trade.query.all()) == 1 + assert len(Trade.session.scalars(select(Trade)).all()) == 1 # Simulate fulfilled LIMIT_BUY order for trade oobj = Order.parse_from_ccxt_object( @@ -4792,7 +4799,7 @@ def test_order_book_entry_pricing1(mocker, default_conf_usdt, order_book_l2, exc patch_exchange(mocker) ticker_usdt_mock = MagicMock(return_value={'ask': ask, 'last': last}) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_l2_order_book=MagicMock(return_value=order_book) if order_book else order_book_l2, fetch_ticker=ticker_usdt_mock, ) @@ -4820,7 +4827,7 @@ def test_check_depth_of_market(default_conf_usdt, mocker, order_book_l2) -> None """ patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_l2_order_book=order_book_l2 ) default_conf_usdt['telegram']['enabled'] = False @@ -4841,7 +4848,7 @@ def test_order_book_exit_pricing( """ test order book ask strategy """ - mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2) + mocker.patch(f'{EXMS}.fetch_l2_order_book', order_book_l2) default_conf_usdt['exchange']['name'] = 'binance' default_conf_usdt['exit_pricing']['use_order_book'] = True default_conf_usdt['exit_pricing']['order_book_top'] = 1 @@ -4849,7 +4856,7 @@ def test_order_book_exit_pricing( patch_RPCManager(mocker) patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, 'ask': 2.2, @@ -4866,7 +4873,7 @@ def test_order_book_exit_pricing( freqtrade.enter_positions() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade time.sleep(0.01) # Race condition fix @@ -4882,8 +4889,7 @@ def test_order_book_exit_pricing( assert freqtrade.handle_trade(trade) is True assert trade.close_rate_requested == order_book_l2.return_value['asks'][0][0] - mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', - return_value={'bids': [[]], 'asks': [[]]}) + mocker.patch(f'{EXMS}.fetch_l2_order_book', return_value={'bids': [[]], 'asks': [[]]}) with pytest.raises(PricingError): freqtrade.handle_trade(trade) assert log_has_re( @@ -4895,14 +4901,14 @@ def test_startup_state(default_conf_usdt, mocker): default_conf_usdt['pairlist'] = {'method': 'VolumePairList', 'config': {'number_assets': 20} } - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) worker = get_patched_worker(mocker, default_conf_usdt) assert worker.freqtrade.state is State.RUNNING def test_startup_trade_reinit(default_conf_usdt, edge_conf, mocker): - mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) + mocker.patch(f'{EXMS}.exchange_has', MagicMock(return_value=True)) reinit_mock = MagicMock() mocker.patch('freqtrade.persistence.Trade.stoploss_reinitialization', reinit_mock) @@ -4927,7 +4933,7 @@ def test_sync_wallet_dry_run(mocker, default_conf_usdt, ticker_usdt, fee, limit_ default_conf_usdt['tradable_balance_ratio'] = 1.0 patch_exchange(mocker) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, create_order=MagicMock(return_value=limit_buy_order_usdt_open), get_fee=fee, @@ -4939,7 +4945,7 @@ def test_sync_wallet_dry_run(mocker, default_conf_usdt, ticker_usdt, fee, limit_ n = bot.enter_positions() assert n == 2 - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() assert len(trades) == 2 bot.config['max_open_trades'] = 3 @@ -4959,7 +4965,7 @@ def test_cancel_all_open_orders(mocker, default_conf_usdt, fee, limit_order, lim is_short, buy_calls, sell_calls): default_conf_usdt['cancel_open_orders_on_exit'] = True mocker.patch( - 'freqtrade.exchange.Exchange.fetch_order', + f'{EXMS}.fetch_order', side_effect=[ ExchangeError(), limit_order[exit_side(is_short)], @@ -4972,7 +4978,7 @@ def test_cancel_all_open_orders(mocker, default_conf_usdt, fee, limit_order, lim freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) create_mock_trades(fee, is_short=is_short) - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() assert len(trades) == MOCK_TRADE_COUNT freqtrade.cancel_all_open_orders() assert buy_mock.call_count == buy_calls @@ -4988,7 +4994,7 @@ def test_check_for_open_trades(mocker, default_conf_usdt, fee, is_short): assert freqtrade.rpc.send_msg.call_count == 0 create_mock_trades(fee, is_short) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True @@ -5015,18 +5021,18 @@ def test_startup_update_open_orders(mocker, default_conf_usdt, fee, caplog, is_s matching_buy_order.update({ 'status': 'closed', }) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=matching_buy_order) + mocker.patch(f'{EXMS}.fetch_order', return_value=matching_buy_order) freqtrade.startup_update_open_orders() # Only stoploss and sell orders are kept open assert len(Order.get_open_orders()) == 2 caplog.clear() - mocker.patch('freqtrade.exchange.Exchange.fetch_order', side_effect=ExchangeError) + mocker.patch(f'{EXMS}.fetch_order', side_effect=ExchangeError) freqtrade.startup_update_open_orders() assert log_has_re(r"Error updating Order .*", caplog) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', side_effect=InvalidOrderException) - hto_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_timedout_order') + mocker.patch(f'{EXMS}.fetch_order', side_effect=InvalidOrderException) + hto_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_order') # Orders which are no longer found after X days should be assumed as canceled. freqtrade.startup_update_open_orders() assert log_has_re(r"Order is older than \d days.*", caplog) @@ -5070,7 +5076,7 @@ def test_update_trades_without_assigned_fees(mocker, default_conf_usdt, fee, is_ 'currency': order['symbol'].split('/')[0]}}) return order - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', side_effect=[ patch_with_fee(mock_order_2_sell(is_short=is_short)), patch_with_fee(mock_order_3_sell(is_short=is_short)), @@ -5130,8 +5136,7 @@ def test_reupdate_enter_order_fees(mocker, default_conf_usdt, fee, caplog, is_sh freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mock_uts = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.update_trade_state') - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', - return_value={'status': 'open'}) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', return_value={'status': 'open'}) create_mock_trades(fee, is_short) trades = Trade.get_trades().all() @@ -5157,7 +5162,7 @@ def test_reupdate_enter_order_fees(mocker, default_conf_usdt, fee, caplog, is_sh exchange='binance', is_short=is_short ) - Trade.query.session.add(trade) + Trade.session.add(trade) freqtrade.handle_insufficient_funds(trade) # assert log_has_re(r"Trying to reupdate buy fees for .*", caplog) @@ -5171,7 +5176,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mock_uts = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.update_trade_state') - mock_fo = mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mock_fo = mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', return_value={'status': 'open'}) def reset_open_orders(trade): @@ -5257,7 +5262,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap caplog.clear() # Test error case - mock_fo = mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mock_fo = mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', side_effect=ExchangeError()) order = mock_order_5_stoploss(is_short=is_short) @@ -5437,11 +5442,10 @@ def test_update_funding_fees( return ret - mocker.patch('freqtrade.exchange.Exchange.refresh_latest_ohlcv', - side_effect=refresh_latest_ohlcv_mock) + mocker.patch(f'{EXMS}.refresh_latest_ohlcv', side_effect=refresh_latest_ohlcv_mock) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_rate=enter_rate_mock, fetch_ticker=MagicMock(return_value={ 'bid': 1.9, @@ -5465,7 +5469,7 @@ def test_update_funding_fees( assert len(trades) == 3 for trade in trades: assert pytest.approx(trade.funding_fees) == 0 - mocker.patch('freqtrade.exchange.Exchange.create_order', return_value=open_exit_order) + mocker.patch(f'{EXMS}.create_order', return_value=open_exit_order) time_machine.move_to("2021-09-01 08:00:00 +00:00") if schedule_off: for trade in trades: @@ -5495,7 +5499,7 @@ def test_update_funding_fees( def test_update_funding_fees_error(mocker, default_conf, caplog): - mocker.patch('freqtrade.exchange.Exchange.get_funding_fees', side_effect=ExchangeError()) + mocker.patch(f'{EXMS}.get_funding_fees', side_effect=ExchangeError()) default_conf['trading_mode'] = 'futures' default_conf['margin_mode'] = 'isolated' freqtrade = get_patched_freqtradebot(mocker, default_conf) @@ -5520,7 +5524,7 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: stake_amount = 10 buy_rate_mock = MagicMock(return_value=bid) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_rate=buy_rate_mock, fetch_ticker=MagicMock(return_value={ 'bid': 10, @@ -5549,17 +5553,16 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: 'id': '650', 'order_id': '650' } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=closed_successful_buy_order)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=closed_successful_buy_order)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=closed_successful_buy_order)) assert freqtrade.execute_entry(pair, stake_amount) # Should create an closed trade with an no open order id # Order is filled and trade is open - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 1 - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.is_open is True assert trade.open_order_id is None @@ -5569,7 +5572,7 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: # Assume it does nothing since order is closed and trade is open freqtrade.update_trades_without_assigned_fees() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.is_open is True assert trade.open_order_id is None @@ -5579,7 +5582,7 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: freqtrade.manage_open_orders() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.is_open is True assert trade.open_order_id is None @@ -5601,16 +5604,14 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: 'id': '651', 'order_id': '651' } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=open_dca_order_1)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', - MagicMock(return_value=open_dca_order_1)) + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=open_dca_order_1)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=open_dca_order_1)) assert freqtrade.execute_entry(pair, stake_amount, trade=trade) - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 2 - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id == '651' assert trade.open_rate == 11 @@ -5635,19 +5636,19 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: # Assume it does nothing since order is still open fetch_order_mm = MagicMock(side_effect=make_sure_its_651) - mocker.patch('freqtrade.exchange.Exchange.create_order', fetch_order_mm) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', fetch_order_mm) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', fetch_order_mm) + mocker.patch(f'{EXMS}.create_order', fetch_order_mm) + mocker.patch(f'{EXMS}.fetch_order', fetch_order_mm) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', fetch_order_mm) freqtrade.update_trades_without_assigned_fees() - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 2 # Assert that the trade is found as open and without fees trades: List[Trade] = Trade.get_open_trades_without_assigned_fees() assert len(trades) == 1 # Assert trade is as expected - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id == '651' assert trade.open_rate == 11 @@ -5677,23 +5678,21 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: 'datetime': arrow.utcnow().isoformat(), } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=closed_dca_order_1)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', - MagicMock(return_value=closed_dca_order_1)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=closed_dca_order_1)) + mocker.patch(f'{EXMS}.fetch_order', MagicMock(return_value=closed_dca_order_1)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=closed_dca_order_1)) freqtrade.manage_open_orders() # Assert trade is as expected (averaged dca) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id is None assert pytest.approx(trade.open_rate) == 9.90909090909 assert trade.amount == 22 assert pytest.approx(trade.stake_amount) == 218 - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 2 @@ -5721,23 +5720,21 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: 'id': '652', 'order_id': '652' } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=closed_dca_order_2)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', - MagicMock(return_value=closed_dca_order_2)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=closed_dca_order_2)) + mocker.patch(f'{EXMS}.fetch_order', MagicMock(return_value=closed_dca_order_2)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=closed_dca_order_2)) assert freqtrade.execute_entry(pair, stake_amount, trade=trade) # Assert trade is as expected (averaged dca) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id is None assert pytest.approx(trade.open_rate) == 8.729729729729 assert trade.amount == 37 assert trade.stake_amount == 323 - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 3 @@ -5759,18 +5756,16 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: 'id': '653', 'order_id': '653' } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=closed_sell_dca_order_1)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', - MagicMock(return_value=closed_sell_dca_order_1)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=closed_sell_dca_order_1)) + mocker.patch(f'{EXMS}.fetch_order', MagicMock(return_value=closed_sell_dca_order_1)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=closed_sell_dca_order_1)) assert freqtrade.execute_trade_exit(trade=trade, limit=8, exit_check=ExitCheckTuple(exit_type=ExitType.PARTIAL_EXIT), sub_trade_amt=15) # Assert trade is as expected (averaged dca) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id is None assert trade.is_open @@ -5778,7 +5773,7 @@ def test_position_adjust(mocker, default_conf_usdt, fee) -> None: assert trade.stake_amount == 192.05405405405406 assert pytest.approx(trade.open_rate) == 8.729729729729 - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 4 @@ -5809,7 +5804,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: amount = 100 buy_rate_mock = MagicMock(return_value=bid) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_rate=buy_rate_mock, fetch_ticker=MagicMock(return_value={ 'bid': 10, @@ -5837,17 +5832,16 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: 'id': '600', 'order_id': '600' } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=closed_successful_buy_order)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=closed_successful_buy_order)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=closed_successful_buy_order)) assert freqtrade.execute_entry(pair, amount) # Should create an closed trade with an no open order id # Order is filled and trade is open - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 1 - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.is_open is True assert trade.open_order_id is None @@ -5857,7 +5851,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: # Assume it does nothing since order is closed and trade is open freqtrade.update_trades_without_assigned_fees() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.is_open is True assert trade.open_order_id is None @@ -5867,7 +5861,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: freqtrade.manage_open_orders() - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.is_open is True assert trade.open_order_id is None @@ -5892,11 +5886,9 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: 'id': '601', 'order_id': '601' } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=closed_sell_dca_order_1)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', - MagicMock(return_value=closed_sell_dca_order_1)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=closed_sell_dca_order_1)) + mocker.patch(f'{EXMS}.fetch_order', MagicMock(return_value=closed_sell_dca_order_1)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=closed_sell_dca_order_1)) assert freqtrade.execute_trade_exit(trade=trade, limit=ask, exit_check=ExitCheckTuple(exit_type=ExitType.PARTIAL_EXIT), @@ -5905,7 +5897,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: assert len(trades) == 1 # Assert trade is as expected (averaged dca) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id is None assert trade.amount == 50 @@ -5914,7 +5906,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: assert pytest.approx(trade.realized_profit) == -152.375 assert pytest.approx(trade.close_profit_abs) == -152.375 - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 2 # Make sure the closed order is found as the second order. @@ -5938,18 +5930,16 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: 'id': '602', 'order_id': '602' } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=closed_sell_dca_order_2)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order', - MagicMock(return_value=closed_sell_dca_order_2)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=closed_sell_dca_order_2)) + mocker.patch(f'{EXMS}.fetch_order', MagicMock(return_value=closed_sell_dca_order_2)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=closed_sell_dca_order_2)) assert freqtrade.execute_trade_exit(trade=trade, limit=ask, exit_check=ExitCheckTuple(exit_type=ExitType.PARTIAL_EXIT), sub_trade_amt=amount) # Assert trade is as expected (averaged dca) - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id is None assert trade.amount == 50 @@ -5958,7 +5948,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None: # Trade fully realized assert pytest.approx(trade.realized_profit) == 94.25 assert pytest.approx(trade.close_profit_abs) == 94.25 - orders = Order.query.all() + orders = Order.session.scalars(select(Order)).all() assert orders assert len(orders) == 3 @@ -6005,7 +5995,7 @@ def test_position_adjust3(mocker, default_conf_usdt, fee, data) -> None: price = order[2] price_mock = MagicMock(return_value=price) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_rate=price_mock, fetch_ticker=MagicMock(return_value={ 'bid': 10, @@ -6032,9 +6022,8 @@ def test_position_adjust3(mocker, default_conf_usdt, fee, data) -> None: 'id': f'60{idx}', 'order_id': f'60{idx}' } - mocker.patch('freqtrade.exchange.Exchange.create_order', - MagicMock(return_value=closed_successful_order)) - mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order', + mocker.patch(f'{EXMS}.create_order', MagicMock(return_value=closed_successful_order)) + mocker.patch(f'{EXMS}.fetch_order_or_stoploss_order', MagicMock(return_value=closed_successful_order)) if order[0] == 'buy': assert freqtrade.execute_entry(pair, amount, trade=trade) @@ -6044,11 +6033,11 @@ def test_position_adjust3(mocker, default_conf_usdt, fee, data) -> None: exit_check=ExitCheckTuple(exit_type=ExitType.PARTIAL_EXIT), sub_trade_amt=amount) - orders1 = Order.query.all() + orders1 = Order.session.scalars(select(Order)).all() assert orders1 assert len(orders1) == idx + 1 - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade if idx < len(data) - 1: assert trade.is_open is True @@ -6063,7 +6052,7 @@ def test_position_adjust3(mocker, default_conf_usdt, fee, data) -> None: order_obj = trade.select_order(order[0], False) assert order_obj.order_id == f'60{idx}' - trade = Trade.query.first() + trade = Trade.session.scalars(select(Trade)).first() assert trade assert trade.open_order_id is None assert trade.is_open is False @@ -6092,7 +6081,7 @@ def test_check_and_call_adjust_trade_position(mocker, default_conf_usdt, fee, ca freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) buy_rate_mock = MagicMock(return_value=10) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_rate=buy_rate_mock, fetch_ticker=MagicMock(return_value={ 'bid': 10, diff --git a/tests/test_integration.py b/tests/test_integration.py index 01a2801ad..5cbedd818 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1,12 +1,13 @@ from unittest.mock import MagicMock import pytest +from sqlalchemy import select from freqtrade.enums import ExitCheckTuple, ExitType, TradingMode from freqtrade.persistence import Trade from freqtrade.persistence.models import Order from freqtrade.rpc.rpc import RPC -from tests.conftest import get_patched_freqtradebot, log_has_re, patch_get_signal +from tests.conftest import EXMS, get_patched_freqtradebot, log_has_re, patch_get_signal def test_may_execute_exit_stoploss_on_exchange_multi(default_conf, ticker, fee, @@ -56,9 +57,9 @@ def test_may_execute_exit_stoploss_on_exchange_multi(default_conf, ticker, fee, [ExitCheckTuple(exit_type=ExitType.EXIT_SIGNAL)]] ) cancel_order_mock = MagicMock() - mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, + create_stoploss=stoploss, fetch_ticker=ticker, get_fee=fee, amount_to_precision=lambda s, x, y: y, @@ -91,7 +92,7 @@ def test_may_execute_exit_stoploss_on_exchange_multi(default_conf, ticker, fee, assert freqtrade.strategy.confirm_trade_exit.call_count == 0 wallets_mock.reset_mock() - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() # Make sure stoploss-order is open and trade is bought (since we mock update_trade_state) for trade in trades: stoploss_order_closed['id'] = '3' @@ -147,7 +148,7 @@ def test_forcebuy_last_unlimited(default_conf, ticker, fee, mocker, balance_rati default_conf['telegram']['enabled'] = True mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, get_fee=fee, amount_to_precision=lambda s, x, y: y, @@ -179,13 +180,13 @@ def test_forcebuy_last_unlimited(default_conf, ticker, fee, mocker, balance_rati n = freqtrade.enter_positions() assert n == 4 - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() assert len(trades) == 4 assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC') == result1 rpc._rpc_force_entry('TKN/BTC', None) - trades = Trade.query.all() + trades = Trade.session.scalars(select(Trade)).all() assert len(trades) == 5 for trade in trades: @@ -217,7 +218,7 @@ def test_dca_buying(default_conf_usdt, ticker_usdt, fee, mocker) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, ) @@ -239,7 +240,7 @@ def test_dca_buying(default_conf_usdt, ticker_usdt, fee, mocker) -> None: # Reduce bid amount ticker_usdt_modif = ticker_usdt.return_value ticker_usdt_modif['bid'] = ticker_usdt_modif['bid'] * 0.995 - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', return_value=ticker_usdt_modif) + mocker.patch(f'{EXMS}.fetch_ticker', return_value=ticker_usdt_modif) # additional buy order freqtrade.process() @@ -286,7 +287,7 @@ def test_dca_short(default_conf_usdt, ticker_usdt, fee, mocker) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, amount_to_precision=lambda s, x, y: round(y, 4), @@ -311,7 +312,7 @@ def test_dca_short(default_conf_usdt, ticker_usdt, fee, mocker) -> None: # Reduce bid amount ticker_usdt_modif = ticker_usdt.return_value ticker_usdt_modif['ask'] = ticker_usdt_modif['ask'] * 1.004 - mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', return_value=ticker_usdt_modif) + mocker.patch(f'{EXMS}.fetch_ticker', return_value=ticker_usdt_modif) # additional buy order freqtrade.process() @@ -361,16 +362,16 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, amount_to_precision=lambda s, x, y: y, price_to_precision=lambda s, x, y: y, ) - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=False) - mocker.patch("freqtrade.exchange.Exchange.get_max_leverage", return_value=10) - mocker.patch("freqtrade.exchange.Exchange.get_funding_fees", return_value=0) - mocker.patch("freqtrade.exchange.Exchange.get_maintenance_ratio_and_amt", return_value=(0, 0)) + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=False) + mocker.patch(f"{EXMS}.get_max_leverage", return_value=10) + mocker.patch(f"{EXMS}.get_funding_fees", return_value=0) + mocker.patch(f"{EXMS}.get_maintenance_ratio_and_amt", return_value=(0, 0)) patch_get_signal(freqtrade) freqtrade.strategy.custom_entry_price = lambda **kwargs: ticker_usdt['ask'] * 0.96 @@ -385,12 +386,12 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) assert trade.open_order_id is not None assert pytest.approx(trade.stake_amount) == 60 assert trade.open_rate == 1.96 - assert trade.stop_loss_pct is None - assert trade.stop_loss == 0.0 + assert trade.stop_loss_pct == -0.1 + assert pytest.approx(trade.stop_loss) == trade.open_rate * (1 - 0.1 / leverage) + assert pytest.approx(trade.initial_stop_loss) == trade.open_rate * (1 - 0.1 / leverage) + assert trade.initial_stop_loss_pct == -0.1 assert trade.leverage == leverage assert trade.stake_amount == 60 - assert trade.initial_stop_loss == 0.0 - assert trade.initial_stop_loss_pct is None # No adjustment freqtrade.process() trade = Trade.get_trades().first() @@ -406,14 +407,14 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) assert trade.open_order_id is not None # Open rate is not adjusted yet assert trade.open_rate == 1.96 - assert trade.stop_loss_pct is None - assert trade.stop_loss == 0.0 + assert trade.stop_loss_pct == -0.1 + assert pytest.approx(trade.stop_loss) == trade.open_rate * (1 - 0.1 / leverage) + assert pytest.approx(trade.initial_stop_loss) == trade.open_rate * (1 - 0.1 / leverage) assert trade.stake_amount == 60 - assert trade.initial_stop_loss == 0.0 - assert trade.initial_stop_loss_pct is None + assert trade.initial_stop_loss_pct == -0.1 # Fill order - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=True) + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=True) freqtrade.process() trade = Trade.get_trades().first() assert len(trade.orders) == 2 @@ -423,12 +424,12 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) assert pytest.approx(trade.stake_amount) == 60 assert trade.stop_loss_pct == -0.1 assert pytest.approx(trade.stop_loss) == 1.99 * (1 - 0.1 / leverage) - assert pytest.approx(trade.initial_stop_loss) == 1.99 * (1 - 0.1 / leverage) + assert pytest.approx(trade.initial_stop_loss) == 1.96 * (1 - 0.1 / leverage) assert trade.initial_stop_loss_pct == -0.1 # 2nd order - not filling freqtrade.strategy.adjust_trade_position = MagicMock(return_value=120) - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=False) + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=False) freqtrade.process() trade = Trade.get_trades().first() @@ -452,7 +453,7 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) # Fill DCA order freqtrade.strategy.adjust_trade_position = MagicMock(return_value=None) - mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=True) + mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=True) freqtrade.strategy.adjust_entry_price = MagicMock(side_effect=ValueError) freqtrade.process() @@ -477,14 +478,14 @@ def test_dca_exiting(default_conf_usdt, ticker_usdt, fee, mocker, caplog, levera freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) freqtrade.trading_mode = TradingMode.FUTURES mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker_usdt, get_fee=fee, amount_to_precision=lambda s, x, y: y, price_to_precision=lambda s, x, y: y, get_min_pair_stake_amount=MagicMock(return_value=10), ) - mocker.patch("freqtrade.exchange.Exchange.get_max_leverage", return_value=10) + mocker.patch(f"{EXMS}.get_max_leverage", return_value=10) patch_get_signal(freqtrade) freqtrade.strategy.leverage = MagicMock(return_value=leverage) @@ -532,8 +533,7 @@ def test_dca_exiting(default_conf_usdt, ticker_usdt, fee, mocker, caplog, levera assert trade.is_open # use amount that would trunc to 0.0 once selling - mocker.patch("freqtrade.exchange.Exchange.amount_to_contract_precision", - lambda s, p, v: round(v, 1)) + mocker.patch(f"{EXMS}.amount_to_contract_precision", lambda s, p, v: round(v, 1)) freqtrade.strategy.adjust_trade_position = MagicMock(return_value=-0.01) freqtrade.process() trade = Trade.get_trades().first() diff --git a/tests/test_misc.py b/tests/test_misc.py index 596c7bd51..6b4343ab2 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -46,7 +46,7 @@ def test_shorten_date() -> None: def test_file_dump_json(mocker) -> None: - file_open = mocker.patch('freqtrade.misc.open', MagicMock()) + file_open = mocker.patch('freqtrade.misc.Path.open', MagicMock()) json_dump = mocker.patch('rapidjson.dump', MagicMock()) file_dump_json(Path('somefile'), [1, 2, 3]) assert file_open.call_count == 1 diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 7662ea7f1..9f04ba20a 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -45,7 +45,6 @@ def test_init_plotscript(default_conf, mocker, testdatadir): default_conf['timerange'] = "20180110-20180112" default_conf['trade_source'] = "file" default_conf['timeframe'] = "5m" - default_conf["datadir"] = testdatadir default_conf['exportfilename'] = testdatadir / "backtest-result.json" supported_markets = ["TRX/BTC", "ADA/BTC"] ret = init_plotscript(default_conf, supported_markets) @@ -394,7 +393,6 @@ def test_load_and_plot_trades(default_conf, mocker, caplog, testdatadir): patch_exchange(mocker) default_conf['trade_source'] = 'file' - default_conf["datadir"] = testdatadir default_conf['exportfilename'] = testdatadir / "backtest-result.json" default_conf['indicators1'] = ["sma5", "ema10"] default_conf['indicators2'] = ["macd"] @@ -451,7 +449,6 @@ def test_start_plot_profit_error(mocker): def test_plot_profit(default_conf, mocker, testdatadir): patch_exchange(mocker) default_conf['trade_source'] = 'file' - default_conf['datadir'] = testdatadir default_conf['exportfilename'] = testdatadir / 'backtest-result_test_nofile.json' default_conf['pairs'] = ['ETH/BTC', 'LTC/BTC'] diff --git a/tests/test_strategy_updater.py b/tests/test_strategy_updater.py new file mode 100644 index 000000000..597d49fda --- /dev/null +++ b/tests/test_strategy_updater.py @@ -0,0 +1,214 @@ +# pragma pylint: disable=missing-docstring, protected-access, invalid-name + +import re +import shutil +import sys +from pathlib import Path + +import pytest + +from freqtrade.commands.strategy_utils_commands import start_strategy_update +from freqtrade.strategy.strategyupdater import StrategyUpdater +from tests.conftest import get_args + + +if sys.version_info < (3, 9): + pytest.skip("StrategyUpdater is not compatible with Python 3.8", allow_module_level=True) + + +def test_strategy_updater_start(tmpdir, capsys) -> None: + # Effective test without mocks. + teststrats = Path(__file__).parent / 'strategy/strats' + tmpdirp = Path(tmpdir) / "strategies" + tmpdirp.mkdir() + shutil.copy(teststrats / 'strategy_test_v2.py', tmpdirp) + old_code = (teststrats / 'strategy_test_v2.py').read_text() + + args = [ + "strategy-updater", + "--userdir", + str(tmpdir), + "--strategy-list", + "StrategyTestV2" + ] + pargs = get_args(args) + pargs['config'] = None + + start_strategy_update(pargs) + + assert Path(tmpdir / "strategies_orig_updater").exists() + # Backup file exists + assert Path(tmpdir / "strategies_orig_updater" / 'strategy_test_v2.py').exists() + # updated file exists + new_file = Path(tmpdirp / 'strategy_test_v2.py') + assert new_file.exists() + new_code = new_file.read_text() + assert 'INTERFACE_VERSION = 3' in new_code + assert 'INTERFACE_VERSION = 2' in old_code + captured = capsys.readouterr() + + assert 'Conversion of strategy_test_v2.py started.' in captured.out + assert re.search(r'Conversion of strategy_test_v2\.py took .* seconds', captured.out) + + +def test_strategy_updater_methods(default_conf, caplog) -> None: + + instance_strategy_updater = StrategyUpdater() + modified_code1 = instance_strategy_updater.update_code(""" +class testClass(IStrategy): + def populate_buy_trend(): + pass + def populate_sell_trend(): + pass + def check_buy_timeout(): + pass + def check_sell_timeout(): + pass + def custom_sell(): + pass +""") + + assert "populate_entry_trend" in modified_code1 + assert "populate_exit_trend" in modified_code1 + assert "check_entry_timeout" in modified_code1 + assert "check_exit_timeout" in modified_code1 + assert "custom_exit" in modified_code1 + assert "INTERFACE_VERSION = 3" in modified_code1 + + +def test_strategy_updater_params(default_conf, caplog) -> None: + instance_strategy_updater = StrategyUpdater() + + modified_code2 = instance_strategy_updater.update_code(""" +ticker_interval = '15m' +buy_some_parameter = IntParameter(space='buy') +sell_some_parameter = IntParameter(space='sell') +""") + + assert "timeframe" in modified_code2 + # check for not editing hyperopt spaces + assert "space='buy'" in modified_code2 + assert "space='sell'" in modified_code2 + + +def test_strategy_updater_constants(default_conf, caplog) -> None: + instance_strategy_updater = StrategyUpdater() + modified_code3 = instance_strategy_updater.update_code(""" +use_sell_signal = True +sell_profit_only = True +sell_profit_offset = True +ignore_roi_if_buy_signal = True +forcebuy_enable = True +""") + + assert "use_exit_signal" in modified_code3 + assert "exit_profit_only" in modified_code3 + assert "exit_profit_offset" in modified_code3 + assert "ignore_roi_if_entry_signal" in modified_code3 + assert "force_entry_enable" in modified_code3 + + +def test_strategy_updater_df_columns(default_conf, caplog) -> None: + instance_strategy_updater = StrategyUpdater() + modified_code = instance_strategy_updater.update_code(""" +dataframe.loc[reduce(lambda x, y: x & y, conditions), ["buy", "buy_tag"]] = (1, "buy_signal_1") +dataframe.loc[reduce(lambda x, y: x & y, conditions), 'sell'] = 1 +""") + + assert "enter_long" in modified_code + assert "exit_long" in modified_code + assert "enter_tag" in modified_code + + +def test_strategy_updater_method_params(default_conf, caplog) -> None: + instance_strategy_updater = StrategyUpdater() + modified_code = instance_strategy_updater.update_code(""" +def confirm_trade_exit(sell_reason: str): + nr_orders = trade.nr_of_successful_buys + pass + """) + assert "exit_reason" in modified_code + assert "nr_orders = trade.nr_of_successful_entries" in modified_code + + +def test_strategy_updater_dicts(default_conf, caplog) -> None: + instance_strategy_updater = StrategyUpdater() + modified_code = instance_strategy_updater.update_code(""" +order_time_in_force = { + 'buy': 'gtc', + 'sell': 'ioc' +} +order_types = { + 'buy': 'limit', + 'sell': 'market', + 'stoploss': 'market', + 'stoploss_on_exchange': False +} +unfilledtimeout = { + 'buy': 1, + 'sell': 2 +} +""") + + assert "'entry': 'gtc'" in modified_code + assert "'exit': 'ioc'" in modified_code + assert "'entry': 'limit'" in modified_code + assert "'exit': 'market'" in modified_code + assert "'entry': 1" in modified_code + assert "'exit': 2" in modified_code + + +def test_strategy_updater_comparisons(default_conf, caplog) -> None: + instance_strategy_updater = StrategyUpdater() + modified_code = instance_strategy_updater.update_code(""" +def confirm_trade_exit(sell_reason): + if (sell_reason == 'stop_loss'): + pass +""") + assert "exit_reason" in modified_code + assert "exit_reason == 'stop_loss'" in modified_code + + +def test_strategy_updater_strings(default_conf, caplog) -> None: + instance_strategy_updater = StrategyUpdater() + + modified_code = instance_strategy_updater.update_code(""" +sell_reason == 'sell_signal' +sell_reason == 'force_sell' +sell_reason == 'emergency_sell' +""") + + # those tests currently don't work, next in line. + assert "exit_signal" in modified_code + assert "exit_reason" in modified_code + assert "force_exit" in modified_code + assert "emergency_exit" in modified_code + + +def test_strategy_updater_comments(default_conf, caplog) -> None: + instance_strategy_updater = StrategyUpdater() + modified_code = instance_strategy_updater.update_code(""" +# This is the 1st comment +import talib.abstract as ta +# This is the 2nd comment +import freqtrade.vendor.qtpylib.indicators as qtpylib + + +class someStrategy(IStrategy): + INTERFACE_VERSION = 2 + # This is the 3rd comment + # This attribute will be overridden if the config file contains "minimal_roi" + minimal_roi = { + "0": 0.50 + } + + # This is the 4th comment + stoploss = -0.1 +""") + + assert "This is the 1st comment" in modified_code + assert "This is the 2nd comment" in modified_code + assert "This is the 3rd comment" in modified_code + assert "INTERFACE_VERSION = 3" in modified_code + # currently still missing: + # Webhook terminology, Telegram notification settings, Strategy/Config settings diff --git a/tests/test_wallets.py b/tests/test_wallets.py index 0117f7427..7ccc8d0f5 100644 --- a/tests/test_wallets.py +++ b/tests/test_wallets.py @@ -6,13 +6,13 @@ import pytest from freqtrade.constants import UNLIMITED_STAKE_AMOUNT from freqtrade.exceptions import DependencyException -from tests.conftest import create_mock_trades, get_patched_freqtradebot, patch_wallet +from tests.conftest import EXMS, create_mock_trades, get_patched_freqtradebot, patch_wallet def test_sync_wallet_at_boot(mocker, default_conf): default_conf['dry_run'] = False mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value={ "BNT": { "free": 1.0, @@ -45,7 +45,7 @@ def test_sync_wallet_at_boot(mocker, default_conf): assert 'USDT' in freqtrade.wallets._wallets assert freqtrade.wallets._last_wallet_refresh > 0 mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value={ "BNT": { "free": 1.2, @@ -87,7 +87,7 @@ def test_sync_wallet_at_boot(mocker, default_conf): def test_sync_wallet_missing_data(mocker, default_conf): default_conf['dry_run'] = False mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value={ "BNT": { "free": 1.0, @@ -136,7 +136,7 @@ def test_get_trade_stake_amount_unlimited_amount(default_conf, ticker, balance_r result1, result2, limit_buy_order_open, fee, mocker) -> None: mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, fetch_ticker=ticker, create_order=MagicMock(return_value=limit_buy_order_open), get_fee=fee @@ -190,7 +190,7 @@ def test_get_trade_stake_amount_unlimited_amount(default_conf, ticker, balance_r (1, 15, 10, 10000, None, 0), # Below min stake and min_stake > stake_available (20, 50, 100, 10000, None, 0), # Below min stake and stake * 1.3 > min_stake (1000, None, 1000, 10000, None, 1000), # No min-stake-amount could be determined - (2000, 15, 2000, 3000, 1500, 500), # Rebuy - resulting in too high stake amount. Adjusting. + (2000, 15, 2000, 3000, 1500, 1500), # Rebuy - resulting in too high stake amount. Adjusting. ]) def test_validate_stake_amount( mocker, @@ -312,7 +312,7 @@ def test_sync_wallet_futures_live(mocker, default_conf): } ] mocker.patch.multiple( - 'freqtrade.exchange.Exchange', + EXMS, get_balances=MagicMock(return_value={ "USDT": { "free": 900, diff --git a/tests/test_worker.py b/tests/test_worker.py index 88d495e13..79e2f35d4 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -8,11 +8,11 @@ import time_machine from freqtrade.data.dataprovider import DataProvider from freqtrade.enums import State from freqtrade.worker import Worker -from tests.conftest import get_patched_worker, log_has, log_has_re +from tests.conftest import EXMS, get_patched_worker, log_has, log_has_re def test_worker_state(mocker, default_conf, markets) -> None: - mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets)) + mocker.patch(f'{EXMS}.markets', PropertyMock(return_value=markets)) worker = get_patched_worker(mocker, default_conf) assert worker.freqtrade.state is State.RUNNING diff --git a/tests/testdata/futures/XRP_USDT_USDT-5m-futures.json b/tests/testdata/futures/XRP_USDT_USDT-5m-futures.json new file mode 100644 index 000000000..006bc7508 --- /dev/null +++ b/tests/testdata/futures/XRP_USDT_USDT-5m-futures.json @@ -0,0 +1 @@ +[[1636934400000,1.1893,1.1954,1.1891,1.1941,9289043.5],[1636934700000,1.1941,1.1993,1.1934,1.1972,7267451.9000000004],[1636935000000,1.1972,1.1994,1.1958,1.1963,4199874.2999999998],[1636935300000,1.1963,1.199,1.1963,1.198,3834762.5],[1636935600000,1.198,1.2017,1.198,1.2,5362935.4000000004],[1636935900000,1.2,1.2092,1.1975,1.2083,12116690.4000000004],[1636936200000,1.2084,1.2097,1.2061,1.2079,6959499.2000000002],[1636936500000,1.2079,1.2082,1.2034,1.2056,4608466.2000000002],[1636936800000,1.2056,1.2066,1.2038,1.2055,2005115.8999999999],[1636937100000,1.2055,1.2091,1.2043,1.209,3365548.1000000001],[1636937400000,1.209,1.2159,1.2084,1.2157,8496118.9000000004],[1636937700000,1.2157,1.2165,1.2109,1.2119,5183792.7999999998],[1636938000000,1.2118,1.2147,1.2116,1.2123,3034201.2999999998],[1636938300000,1.2122,1.2133,1.2047,1.2068,5546199.0],[1636938600000,1.2069,1.2089,1.2046,1.2083,2961523.7999999998],[1636938900000,1.2084,1.2092,1.2033,1.2054,2755246.6000000001],[1636939200000,1.2053,1.2059,1.2005,1.2012,3574142.2000000002],[1636939500000,1.2013,1.2047,1.2007,1.2038,2085164.5],[1636939800000,1.2037,1.2064,1.2032,1.2053,1930387.6000000001],[1636940100000,1.2052,1.2074,1.2046,1.2071,1563690.5],[1636940400000,1.207,1.208,1.2064,1.2067,1470500.0],[1636940700000,1.2067,1.2085,1.2062,1.2065,1511111.2],[1636941000000,1.2065,1.2119,1.206,1.2116,3196613.7000000002],[1636941300000,1.2115,1.2119,1.2094,1.2101,2474041.2000000002],[1636941600000,1.2102,1.2116,1.2084,1.2089,1966194.3],[1636941900000,1.2089,1.2102,1.2086,1.2088,1051071.8999999999],[1636942200000,1.2089,1.2092,1.2054,1.2067,1718919.5],[1636942500000,1.2066,1.2088,1.2064,1.2064,1276834.8],[1636942800000,1.2064,1.2074,1.2038,1.2039,1389505.0],[1636943100000,1.2039,1.2076,1.2038,1.2064,1828371.2],[1636943400000,1.2065,1.2082,1.2056,1.2082,1072101.5],[1636943700000,1.2082,1.2116,1.2081,1.2115,2157606.0],[1636944000000,1.2116,1.2136,1.2103,1.2129,1364511.6000000001],[1636944300000,1.2129,1.2138,1.209,1.2095,2037732.3999999999],[1636944600000,1.2095,1.2102,1.2081,1.2086,1376262.8999999999],[1636944900000,1.2087,1.2098,1.2081,1.2089,791578.0],[1636945200000,1.2088,1.2099,1.2075,1.2084,949445.9],[1636945500000,1.2084,1.209,1.2055,1.2073,3957253.0],[1636945800000,1.2071,1.2094,1.207,1.209,1755194.3999999999],[1636946100000,1.2091,1.2091,1.2054,1.2065,2235814.1000000001],[1636946400000,1.2064,1.2079,1.2064,1.2076,1314946.5],[1636946700000,1.2076,1.2076,1.2042,1.2058,1554830.8999999999],[1636947000000,1.2058,1.2073,1.2048,1.2053,1266664.8],[1636947300000,1.2053,1.207,1.2047,1.2067,1332565.7],[1636947600000,1.2067,1.2088,1.2062,1.2083,1539348.6000000001],[1636947900000,1.2082,1.2089,1.207,1.2076,1084474.8999999999],[1636948200000,1.2076,1.2082,1.2067,1.2077,573314.1],[1636948500000,1.2077,1.2095,1.2072,1.2086,1612433.1000000001],[1636948800000,1.2087,1.2114,1.2087,1.2106,1800229.5],[1636949100000,1.2106,1.2109,1.2093,1.2105,1069802.8999999999],[1636949400000,1.2105,1.2105,1.2084,1.2097,1522400.1000000001],[1636949700000,1.2097,1.2126,1.2095,1.2122,1581693.3999999999],[1636950000000,1.2121,1.2124,1.211,1.2113,1502370.0],[1636950300000,1.2113,1.2133,1.2112,1.2125,1275382.6000000001],[1636950600000,1.2127,1.2129,1.2108,1.2112,1669161.7],[1636950900000,1.2112,1.2118,1.2103,1.2109,1281564.8999999999],[1636951200000,1.211,1.2128,1.2107,1.212,1004090.0],[1636951500000,1.212,1.2156,1.2111,1.2155,2016559.1000000001],[1636951800000,1.2154,1.2205,1.2145,1.2178,5314030.4000000004],[1636952100000,1.2178,1.2179,1.2142,1.2148,2687145.2000000002],[1636952400000,1.2148,1.2178,1.2148,1.2178,1223842.8],[1636952700000,1.2177,1.2181,1.2147,1.2152,1500519.6000000001],[1636953000000,1.2152,1.2152,1.211,1.2113,2474496.5],[1636953300000,1.2113,1.214,1.2113,1.2125,1248090.0],[1636953600000,1.2125,1.2141,1.2122,1.2139,921049.0],[1636953900000,1.2139,1.2141,1.2118,1.2128,1198193.3999999999],[1636954200000,1.2127,1.2141,1.211,1.2116,1361937.5],[1636954500000,1.2115,1.2121,1.2104,1.2113,1435204.8999999999],[1636954800000,1.2113,1.2114,1.2091,1.2092,1602274.8999999999],[1636955100000,1.2093,1.2102,1.2092,1.2095,802606.0],[1636955400000,1.2095,1.2095,1.2078,1.2083,1139815.6000000001],[1636955700000,1.2083,1.2109,1.2081,1.2097,1182689.6000000001],[1636956000000,1.2097,1.2103,1.2083,1.2093,1121184.0],[1636956300000,1.2092,1.2112,1.2086,1.2105,1252210.1000000001],[1636956600000,1.2106,1.2162,1.2105,1.2158,2003334.0],[1636956900000,1.2158,1.2158,1.2134,1.2145,2058967.3999999999],[1636957200000,1.2144,1.2177,1.2141,1.2174,1881352.6000000001],[1636957500000,1.2174,1.2189,1.217,1.2184,1592213.6000000001],[1636957800000,1.2183,1.2184,1.2143,1.2151,2138606.5],[1636958100000,1.215,1.2159,1.2127,1.2152,1740152.5],[1636958400000,1.2153,1.2157,1.2145,1.2147,837984.7],[1636958700000,1.2146,1.2159,1.214,1.2145,1068054.1000000001],[1636959000000,1.2144,1.2152,1.2129,1.2129,1080074.0],[1636959300000,1.213,1.2156,1.2126,1.2152,1149888.7],[1636959600000,1.2153,1.2214,1.2152,1.2161,6266177.5999999996],[1636959900000,1.2162,1.217,1.2136,1.2142,1969411.0],[1636960200000,1.2141,1.2192,1.2136,1.2187,2066013.8999999999],[1636960500000,1.2187,1.2188,1.2159,1.217,1351267.1000000001],[1636960800000,1.217,1.2178,1.2153,1.2156,1334536.7],[1636961100000,1.2155,1.2158,1.2128,1.2134,1485421.2],[1636961400000,1.2133,1.2148,1.2105,1.2109,1854928.3999999999],[1636961700000,1.211,1.2128,1.2106,1.2121,1148625.1000000001],[1636962000000,1.2121,1.214,1.2106,1.2112,1123669.3999999999],[1636962300000,1.211,1.212,1.2106,1.2118,877979.1],[1636962600000,1.2118,1.2136,1.2112,1.212,1362349.8],[1636962900000,1.2121,1.2121,1.21,1.21,1193089.0],[1636963200000,1.2101,1.2126,1.2092,1.2105,2158603.5],[1636963500000,1.2105,1.2113,1.2048,1.2055,4513172.2000000002],[1636963800000,1.2055,1.2076,1.2048,1.2067,2875763.3999999999],[1636964100000,1.2066,1.2067,1.2005,1.201,4649136.9000000004],[1636964400000,1.201,1.2044,1.2005,1.2035,2734988.8999999999],[1636964700000,1.2034,1.2047,1.2029,1.2038,1696384.0],[1636965000000,1.2039,1.2047,1.2026,1.2044,1652360.6000000001],[1636965300000,1.2043,1.2063,1.2039,1.206,1682635.8],[1636965600000,1.2061,1.2085,1.2056,1.2081,3810080.7000000002],[1636965900000,1.2081,1.2109,1.2069,1.21,3426972.6000000001],[1636966200000,1.21,1.2108,1.2094,1.2099,1948298.3999999999],[1636966500000,1.2099,1.2108,1.2088,1.2105,1358895.3999999999],[1636966800000,1.2104,1.215,1.2089,1.215,5488008.9000000004],[1636967100000,1.2149,1.2175,1.2145,1.2156,4423040.2000000002],[1636967400000,1.2156,1.2199,1.2156,1.2193,3209461.7000000002],[1636967700000,1.2193,1.22,1.2161,1.2177,3136263.2999999998],[1636968000000,1.2177,1.2178,1.2144,1.2154,2973911.8999999999],[1636968300000,1.2155,1.2185,1.2151,1.2161,2313273.1000000001],[1636968600000,1.2161,1.2161,1.2118,1.2138,2457956.6000000001],[1636968900000,1.2139,1.2147,1.2111,1.212,1467335.5],[1636969200000,1.212,1.2121,1.2102,1.2102,1479763.0],[1636969500000,1.2102,1.2124,1.2101,1.2116,1759979.3],[1636969800000,1.2115,1.2126,1.2114,1.2125,597927.7],[1636970100000,1.2123,1.2127,1.2109,1.2113,846797.8],[1636970400000,1.2113,1.2116,1.2054,1.206,4003530.2999999998],[1636970700000,1.2061,1.2085,1.2061,1.2083,1407880.3],[1636971000000,1.2084,1.2092,1.2071,1.209,686922.7],[1636971300000,1.209,1.2098,1.2089,1.2094,969569.7],[1636971600000,1.2095,1.2101,1.2085,1.2101,958910.5],[1636971900000,1.2101,1.2107,1.206,1.2066,1750771.2],[1636972200000,1.2065,1.2091,1.2065,1.2085,863380.4],[1636972500000,1.2085,1.2112,1.2085,1.211,885808.6],[1636972800000,1.2111,1.2111,1.2091,1.2093,768551.4],[1636973100000,1.2096,1.21,1.2086,1.2088,596104.7],[1636973400000,1.2088,1.2116,1.2088,1.2102,1013463.8],[1636973700000,1.2102,1.2108,1.2089,1.21,868464.8],[1636974000000,1.21,1.2105,1.2084,1.2088,1146027.1000000001],[1636974300000,1.2089,1.2106,1.2088,1.2105,775161.5],[1636974600000,1.2106,1.2106,1.2093,1.2093,549029.5],[1636974900000,1.2094,1.2097,1.2079,1.2085,616718.4],[1636975200000,1.2085,1.2089,1.2049,1.2064,4021848.5],[1636975500000,1.2063,1.2076,1.205,1.2056,1516604.6000000001],[1636975800000,1.2056,1.2056,1.2027,1.2037,3276909.2000000002],[1636976100000,1.2038,1.2058,1.2032,1.2058,1671933.3],[1636976400000,1.2058,1.2075,1.2058,1.2068,1309347.1000000001],[1636976700000,1.2068,1.2069,1.2057,1.2063,945417.0],[1636977000000,1.2063,1.2077,1.2059,1.2071,919673.8],[1636977300000,1.2072,1.2074,1.2062,1.2073,742663.2],[1636977600000,1.2073,1.209,1.205,1.2075,2478967.0],[1636977900000,1.2075,1.2077,1.204,1.2054,1870332.3],[1636978200000,1.2055,1.2059,1.2049,1.2053,565997.4],[1636978500000,1.2052,1.2053,1.2015,1.2037,2654152.1000000001],[1636978800000,1.2036,1.2047,1.2022,1.2038,1807444.1000000001],[1636979100000,1.2038,1.2046,1.2026,1.2027,1065979.8],[1636979400000,1.2027,1.206,1.2027,1.2054,1477203.8],[1636979700000,1.2053,1.2067,1.204,1.2045,2211979.5],[1636980000000,1.2047,1.2059,1.204,1.2058,804279.6],[1636980300000,1.2058,1.2066,1.205,1.2066,1313472.0],[1636980600000,1.2066,1.2067,1.2055,1.2066,834835.7],[1636980900000,1.2067,1.2073,1.2047,1.2047,1000096.8],[1636981200000,1.2047,1.207,1.2046,1.2068,1046862.8],[1636981500000,1.2068,1.2077,1.2066,1.207,1188865.6000000001],[1636981800000,1.2071,1.2104,1.207,1.2101,1875017.0],[1636982100000,1.2102,1.2114,1.2069,1.2072,2439900.5],[1636982400000,1.2072,1.2084,1.2066,1.2078,865686.6],[1636982700000,1.2078,1.2079,1.2064,1.2066,633128.7],[1636983000000,1.2065,1.2087,1.202,1.202,5720475.0999999996],[1636983300000,1.2021,1.2038,1.2005,1.2038,3417445.1000000001],[1636983600000,1.2038,1.2047,1.1915,1.1991,14721245.0999999996],[1636983900000,1.1992,1.1995,1.1953,1.1974,5430137.5],[1636984200000,1.1974,1.1996,1.1973,1.1983,1598326.5],[1636984500000,1.1983,1.1996,1.1979,1.1989,1487378.3999999999],[1636984800000,1.1989,1.1991,1.1941,1.195,3172429.0],[1636985100000,1.195,1.1965,1.1941,1.1964,3403294.0],[1636985400000,1.1965,1.1977,1.1934,1.1945,2772952.6000000001],[1636985700000,1.1944,1.1965,1.1924,1.1928,2853416.8999999999],[1636986000000,1.1927,1.1944,1.1864,1.1881,9859645.0999999996],[1636986300000,1.1881,1.1934,1.1865,1.192,4374940.7000000002],[1636986600000,1.1921,1.1931,1.1904,1.1927,2201904.7000000002],[1636986900000,1.1926,1.1944,1.1924,1.1928,1807342.7],[1636987200000,1.193,1.1941,1.1925,1.193,1354005.2],[1636987500000,1.193,1.193,1.1899,1.1906,2118339.0],[1636987800000,1.1905,1.1918,1.1879,1.1905,4748915.5],[1636988100000,1.1906,1.1927,1.1902,1.191,1886338.7],[1636988400000,1.1911,1.1916,1.1866,1.1866,4544292.0999999996],[1636988700000,1.1866,1.1887,1.1844,1.1871,7310980.2000000002],[1636989000000,1.1872,1.1914,1.1862,1.1908,3315857.5],[1636989300000,1.1908,1.1922,1.189,1.1914,2196378.0],[1636989600000,1.1913,1.1913,1.1895,1.1909,1199510.5],[1636989900000,1.1908,1.1927,1.189,1.1899,1838353.0],[1636990200000,1.1899,1.1923,1.1885,1.1917,1453015.3],[1636990500000,1.1915,1.192,1.1883,1.1893,1395222.3999999999],[1636990800000,1.1893,1.1893,1.186,1.1862,2115580.1000000001],[1636991100000,1.1862,1.1909,1.1854,1.1907,2681118.7000000002],[1636991400000,1.1907,1.1908,1.186,1.1875,1802497.2],[1636991700000,1.1875,1.1896,1.1873,1.1885,1338971.5],[1636992000000,1.1885,1.19,1.1855,1.19,4843721.2000000002],[1636992300000,1.19,1.1919,1.1888,1.1892,2129650.7000000002],[1636992600000,1.1892,1.1904,1.1865,1.187,2149370.6000000001],[1636992900000,1.1871,1.1896,1.1861,1.1872,1778567.1000000001],[1636993200000,1.1871,1.1875,1.1862,1.1864,1184527.7],[1636993500000,1.1864,1.1873,1.1821,1.185,5233565.7999999998],[1636993800000,1.185,1.1894,1.1836,1.1894,2780444.8999999999],[1636994100000,1.1894,1.1913,1.1879,1.1881,1948790.6000000001],[1636994400000,1.1881,1.1893,1.1844,1.1847,2805736.3999999999],[1636994700000,1.1847,1.1857,1.1816,1.1821,3092493.8999999999],[1636995000000,1.1822,1.1849,1.1811,1.1849,2919570.1000000001],[1636995300000,1.1848,1.1897,1.1845,1.1897,2529541.7000000002],[1636995600000,1.1897,1.1898,1.1865,1.1881,1987137.8999999999],[1636995900000,1.188,1.1883,1.1864,1.1882,1229479.8999999999],[1636996200000,1.1882,1.1904,1.188,1.1894,2329656.5],[1636996500000,1.1894,1.1907,1.1877,1.1885,1635524.7],[1636996800000,1.1885,1.1885,1.1861,1.1866,1464212.6000000001],[1636997100000,1.1866,1.1881,1.1859,1.1872,1498541.1000000001],[1636997400000,1.1872,1.1884,1.1866,1.1877,645062.1],[1636997700000,1.1878,1.189,1.1877,1.1887,976463.5],[1636998000000,1.1886,1.1903,1.1884,1.1894,936264.1],[1636998300000,1.1893,1.1902,1.188,1.1881,1076340.3],[1636998600000,1.1881,1.1884,1.1855,1.1877,1660911.8],[1636998900000,1.1877,1.1877,1.1847,1.1848,1546267.1000000001],[1636999200000,1.1848,1.1857,1.1831,1.1843,3223795.7000000002],[1636999500000,1.1842,1.1878,1.184,1.1873,1328197.3999999999],[1636999800000,1.1873,1.1885,1.1849,1.1851,1479273.8999999999],[1637000100000,1.1851,1.1858,1.1826,1.1847,2957345.1000000001],[1637000400000,1.1846,1.1875,1.1841,1.1863,1262899.8999999999],[1637000700000,1.1863,1.1866,1.1846,1.1855,1498078.8],[1637001000000,1.1854,1.186,1.1845,1.1847,907889.9],[1637001300000,1.1847,1.186,1.1846,1.1848,681753.2],[1637001600000,1.1847,1.1852,1.1814,1.1825,1997175.0],[1637001900000,1.1824,1.1826,1.18,1.1804,4002404.7999999998],[1637002200000,1.1804,1.1812,1.1756,1.1811,6730780.9000000004],[1637002500000,1.1811,1.1836,1.1805,1.1825,2072583.3],[1637002800000,1.1825,1.1826,1.1785,1.181,2788627.7999999998],[1637003100000,1.181,1.1817,1.1791,1.1808,1166716.5],[1637003400000,1.1808,1.1835,1.1806,1.1835,1356426.8999999999],[1637003700000,1.1835,1.1839,1.1826,1.1831,791629.1],[1637004000000,1.1831,1.1834,1.1806,1.1833,1158697.8],[1637004300000,1.1833,1.1863,1.1831,1.1853,2155591.2000000002],[1637004600000,1.1854,1.1879,1.1843,1.1878,922252.4],[1637004900000,1.1879,1.1879,1.1847,1.1851,1403022.8],[1637005200000,1.185,1.1875,1.1847,1.1865,1031169.4],[1637005500000,1.1865,1.1874,1.1848,1.1852,662437.6],[1637005800000,1.1853,1.1855,1.184,1.1846,751629.5],[1637006100000,1.1847,1.1859,1.1842,1.1857,949722.6],[1637006400000,1.1857,1.1857,1.1806,1.1816,1733988.6000000001],[1637006700000,1.1815,1.1816,1.1784,1.1796,2888343.2000000002],[1637007000000,1.1796,1.1816,1.1785,1.1808,1077627.7],[1637007300000,1.1807,1.1807,1.178,1.1785,1101824.3999999999],[1637007600000,1.1785,1.1788,1.1764,1.1777,2142094.7000000002],[1637007900000,1.1776,1.1783,1.1759,1.1759,2095915.1000000001],[1637008200000,1.1758,1.1796,1.1755,1.1789,2472558.2000000002],[1637008500000,1.1789,1.1804,1.1764,1.1765,1733359.2],[1637008800000,1.1764,1.1788,1.1735,1.1778,4928727.0],[1637009100000,1.1778,1.1809,1.1766,1.1804,1891969.1000000001],[1637009400000,1.1804,1.1826,1.1793,1.1797,1792413.3999999999],[1637009700000,1.1796,1.1803,1.1768,1.1777,1422290.8],[1637010000000,1.1776,1.1776,1.165,1.1737,15867137.6999999993],[1637010300000,1.1737,1.1766,1.1721,1.1729,2896715.1000000001],[1637010600000,1.1729,1.1765,1.1726,1.1759,1538151.0],[1637010900000,1.1759,1.179,1.1758,1.1784,2383917.6000000001],[1637011200000,1.1785,1.1794,1.1768,1.1777,1266720.3],[1637011500000,1.1776,1.1799,1.1749,1.1772,2843606.2999999998],[1637011800000,1.1773,1.1803,1.1758,1.1801,1386825.8999999999],[1637012100000,1.1801,1.1802,1.1771,1.1783,1025132.1],[1637012400000,1.1781,1.1799,1.1771,1.1798,1095931.5],[1637012700000,1.1797,1.1807,1.1788,1.179,824766.1],[1637013000000,1.179,1.1802,1.178,1.1788,736602.1],[1637013300000,1.1789,1.1827,1.1789,1.1805,1921139.5],[1637013600000,1.1805,1.1807,1.177,1.1775,1388575.0],[1637013900000,1.1774,1.1786,1.1772,1.1777,851274.7],[1637014200000,1.1778,1.1781,1.1754,1.1756,1229630.5],[1637014500000,1.1755,1.1756,1.1725,1.1736,2284743.7000000002],[1637014800000,1.1735,1.1737,1.1701,1.1728,2143691.1000000001],[1637015100000,1.1728,1.1732,1.1715,1.1719,1255188.1000000001],[1637015400000,1.1719,1.1756,1.1719,1.1755,1242001.8],[1637015700000,1.1756,1.1773,1.1754,1.176,990468.2],[1637016000000,1.176,1.1777,1.1759,1.1775,498149.6],[1637016300000,1.1776,1.1793,1.1772,1.1775,750568.6],[1637016600000,1.1775,1.1804,1.1772,1.1803,941858.4],[1637016900000,1.1804,1.1824,1.179,1.1812,2075547.3],[1637017200000,1.1812,1.1812,1.1772,1.1778,1938884.0],[1637017500000,1.1777,1.1792,1.1774,1.1782,799165.2],[1637017800000,1.1782,1.1789,1.1777,1.1777,478035.0],[1637018100000,1.1778,1.1785,1.174,1.1782,4335357.7999999998],[1637018400000,1.1783,1.1784,1.1765,1.1767,697003.9],[1637018700000,1.1767,1.1768,1.1729,1.1734,1483791.0],[1637019000000,1.1733,1.1761,1.1726,1.1751,1632155.0],[1637019300000,1.1752,1.1763,1.1751,1.1756,618483.1],[1637019600000,1.1758,1.1767,1.1748,1.1764,551544.6],[1637019900000,1.1765,1.1767,1.1755,1.1761,769943.5],[1637020200000,1.1761,1.1766,1.175,1.1758,815763.7],[1637020500000,1.1758,1.1764,1.1707,1.1729,2601489.3999999999],[1637020800000,1.1728,1.1729,1.1626,1.1647,10370232.6999999993],[1637021100000,1.1647,1.1679,1.1574,1.1588,14674219.0999999996],[1637021400000,1.1588,1.1603,1.125,1.1505,51767444.6000000015],[1637021700000,1.1506,1.1562,1.1447,1.1485,12232299.4000000004],[1637022000000,1.1484,1.1527,1.1436,1.1519,6240786.5999999996],[1637022300000,1.152,1.1526,1.1458,1.1493,5898863.9000000004],[1637022600000,1.1493,1.1543,1.1453,1.1542,4023262.8999999999],[1637022900000,1.1542,1.1548,1.1503,1.1539,4072291.5],[1637023200000,1.1539,1.1548,1.1513,1.1526,2831077.2999999998],[1637023500000,1.1525,1.1573,1.1523,1.1525,3310198.0],[1637023800000,1.1524,1.1536,1.1482,1.1488,3801330.7000000002],[1637024100000,1.1486,1.1514,1.142,1.1432,8989381.1999999993],[1637024400000,1.1432,1.1472,1.13,1.1324,18542491.1999999993],[1637024700000,1.1322,1.139,1.105,1.137,49788431.8999999985],[1637025000000,1.1371,1.1391,1.1308,1.1388,16654546.9000000004],[1637025300000,1.1387,1.1412,1.1307,1.1356,14918494.0999999996],[1637025600000,1.1356,1.1414,1.1314,1.138,7098702.4000000004],[1637025900000,1.138,1.1416,1.1364,1.1393,5430829.7000000002],[1637026200000,1.1393,1.1401,1.13,1.1307,6803088.2000000002],[1637026500000,1.1307,1.1365,1.1261,1.1363,6164932.5],[1637026800000,1.1363,1.1459,1.136,1.1454,4365617.4000000004],[1637027100000,1.1453,1.1461,1.1419,1.145,4744308.5],[1637027400000,1.145,1.1471,1.1429,1.1453,3721814.3999999999],[1637027700000,1.1453,1.1458,1.1416,1.1424,2823682.3999999999],[1637028000000,1.1425,1.1438,1.1387,1.1402,3710527.2999999998],[1637028300000,1.1401,1.1418,1.1383,1.1411,3186377.5],[1637028600000,1.1411,1.1443,1.1405,1.143,3430024.2999999998],[1637028900000,1.1431,1.1437,1.1414,1.1429,2317514.3999999999],[1637029200000,1.1429,1.1435,1.1411,1.1429,2652508.5],[1637029500000,1.1429,1.1437,1.1415,1.1415,1811976.5],[1637029800000,1.1416,1.1422,1.14,1.1404,1639570.8],[1637030100000,1.1404,1.141,1.135,1.1374,5626771.4000000004],[1637030400000,1.1374,1.1388,1.1361,1.1367,1883461.3999999999],[1637030700000,1.1366,1.1396,1.1345,1.1354,2704401.7999999998],[1637031000000,1.1353,1.1368,1.1314,1.1314,3254363.8999999999],[1637031300000,1.1315,1.1319,1.1274,1.1302,6785507.5999999996],[1637031600000,1.1302,1.1303,1.1237,1.1302,7233618.5],[1637031900000,1.1303,1.1343,1.1296,1.1324,4118353.2999999998],[1637032200000,1.1324,1.1349,1.1321,1.1343,1859638.2],[1637032500000,1.1343,1.1351,1.1326,1.1345,1874930.3999999999],[1637032800000,1.1344,1.1354,1.1245,1.1268,6000321.0],[1637033100000,1.1268,1.1284,1.1121,1.1191,12805164.5999999996],[1637033400000,1.1192,1.1258,1.1164,1.1182,8744035.5999999996],[1637033700000,1.1182,1.1231,1.111,1.118,9232101.1999999993],[1637034000000,1.118,1.1239,1.1163,1.1165,6494642.2000000002],[1637034300000,1.1166,1.1232,1.1111,1.123,8552285.3000000007],[1637034600000,1.123,1.1286,1.121,1.1268,7567469.7999999998],[1637034900000,1.1268,1.1289,1.1211,1.1228,4671142.5999999996],[1637035200000,1.1228,1.1254,1.1159,1.1175,9853542.0],[1637035500000,1.1174,1.1239,1.1148,1.1238,6547575.7000000002],[1637035800000,1.1238,1.124,1.1122,1.1137,7136291.2000000002],[1637036100000,1.1138,1.1188,1.112,1.1128,6459000.0999999996],[1637036400000,1.1127,1.1195,1.1061,1.1195,15800732.3000000007],[1637036700000,1.1195,1.1289,1.1181,1.1273,7311133.7999999998],[1637037000000,1.1272,1.1299,1.1249,1.1287,6075163.2000000002],[1637037300000,1.1287,1.1318,1.1276,1.1309,3822050.1000000001],[1637037600000,1.1309,1.1324,1.1277,1.1314,4134442.7000000002],[1637037900000,1.1314,1.1314,1.1258,1.1289,3024553.7999999998],[1637038200000,1.1288,1.1328,1.1281,1.1324,3120336.0],[1637038500000,1.1323,1.1324,1.1286,1.1286,3527058.7999999998],[1637038800000,1.1285,1.1303,1.1275,1.1284,2208750.0],[1637039100000,1.1284,1.1285,1.1239,1.1283,2784313.2999999998],[1637039400000,1.1282,1.1284,1.1244,1.1249,2212214.7000000002],[1637039700000,1.125,1.1299,1.1248,1.1294,2706724.0],[1637040000000,1.1294,1.1326,1.1285,1.1323,2981264.8999999999],[1637040300000,1.1322,1.1327,1.1254,1.1289,5296197.7000000002],[1637040600000,1.1289,1.1312,1.1283,1.129,2207684.8999999999],[1637040900000,1.1289,1.129,1.1262,1.1262,1581778.5],[1637041200000,1.1264,1.1312,1.1228,1.1303,6245071.4000000004],[1637041500000,1.1302,1.1303,1.1264,1.1268,1430621.3999999999],[1637041800000,1.1268,1.1273,1.1242,1.1261,2663380.2000000002],[1637042100000,1.1262,1.1262,1.1216,1.1234,3088115.1000000001],[1637042400000,1.1235,1.1258,1.1215,1.1257,2607308.1000000001],[1637042700000,1.1257,1.1265,1.1204,1.1212,2930686.7999999998],[1637043000000,1.1213,1.1253,1.1212,1.125,1715592.8],[1637043300000,1.1249,1.1251,1.1213,1.1214,2267349.7000000002],[1637043600000,1.1213,1.1236,1.1208,1.1222,1124855.1000000001],[1637043900000,1.1223,1.1238,1.1155,1.1164,5530558.7999999998],[1637044200000,1.1166,1.1224,1.115,1.1224,5299299.0],[1637044500000,1.1224,1.1239,1.1186,1.1191,3826527.0],[1637044800000,1.1191,1.1214,1.1174,1.1199,2721175.3999999999],[1637045100000,1.1198,1.1244,1.1196,1.1226,3202701.1000000001],[1637045400000,1.1226,1.125,1.1223,1.1235,1864276.0],[1637045700000,1.1236,1.1267,1.122,1.1248,2202205.6000000001],[1637046000000,1.1247,1.1264,1.1197,1.12,3604061.2000000002],[1637046300000,1.1201,1.1277,1.12,1.127,3024670.2999999998],[1637046600000,1.127,1.127,1.1221,1.1228,2208623.7000000002],[1637046900000,1.1227,1.1266,1.1224,1.1228,2422007.5],[1637047200000,1.1228,1.1228,1.1198,1.1202,1741483.3],[1637047500000,1.1201,1.124,1.1197,1.1226,2176612.3999999999],[1637047800000,1.1226,1.1268,1.1208,1.1213,2963321.1000000001],[1637048100000,1.1215,1.1216,1.1187,1.1197,2621660.7999999998],[1637048400000,1.1198,1.122,1.1188,1.1217,1715248.7],[1637048700000,1.1216,1.1238,1.121,1.1237,1076378.6000000001],[1637049000000,1.1237,1.126,1.119,1.1242,2665782.8999999999],[1637049300000,1.1243,1.1258,1.1226,1.1255,2099191.7000000002],[1637049600000,1.1256,1.1274,1.1218,1.1258,4283096.0],[1637049900000,1.1259,1.1293,1.1257,1.1289,3450819.5],[1637050200000,1.129,1.1297,1.1247,1.1263,2658729.6000000001],[1637050500000,1.1263,1.1265,1.1216,1.1248,2353989.0],[1637050800000,1.1248,1.128,1.1235,1.1254,2667401.7999999998],[1637051100000,1.1255,1.1257,1.1224,1.1231,1663270.7],[1637051400000,1.1232,1.126,1.1228,1.1257,1504233.5],[1637051700000,1.1257,1.126,1.1235,1.1249,1636380.3],[1637052000000,1.1249,1.1287,1.1248,1.1286,2152550.8999999999],[1637052300000,1.1287,1.1291,1.1258,1.1258,1642710.6000000001],[1637052600000,1.1258,1.1299,1.1234,1.1295,2840899.0],[1637052900000,1.1294,1.1312,1.1292,1.1301,2753850.0],[1637053200000,1.13,1.1347,1.1275,1.1281,6623462.7000000002],[1637053500000,1.1282,1.1282,1.125,1.1252,3362681.8999999999],[1637053800000,1.1252,1.1281,1.1246,1.1264,1587342.8],[1637054100000,1.1264,1.1264,1.1209,1.121,2291705.7000000002],[1637054400000,1.121,1.1231,1.119,1.1206,4287524.7999999998],[1637054700000,1.1205,1.1211,1.1183,1.121,2149620.3999999999],[1637055000000,1.121,1.1211,1.1177,1.1198,1889836.2],[1637055300000,1.1198,1.1198,1.1109,1.1127,6507103.7000000002],[1637055600000,1.1127,1.1172,1.1125,1.1138,3022620.7999999998],[1637055900000,1.1138,1.1156,1.1101,1.1102,2856203.2000000002],[1637056200000,1.1101,1.1141,1.103,1.1124,13114838.6999999993],[1637056500000,1.1124,1.113,1.1027,1.1032,6424556.0999999996],[1637056800000,1.1031,1.1051,1.08,1.0959,39145521.200000003],[1637057100000,1.0959,1.0959,1.0392,1.0535,56581244.5],[1637057400000,1.0546,1.0729,1.0332,1.0439,42637203.3999999985],[1637057700000,1.0439,1.0661,1.0347,1.0657,38320115.5],[1637058000000,1.0658,1.0692,1.0616,1.064,17025610.8000000007],[1637058300000,1.0641,1.0721,1.057,1.0714,17492906.8000000007],[1637058600000,1.0714,1.0828,1.0676,1.0819,15396169.6999999993],[1637058900000,1.0818,1.089,1.0782,1.0821,15832077.6999999993],[1637059200000,1.0821,1.0877,1.08,1.0869,10945208.6999999993],[1637059500000,1.087,1.0871,1.0819,1.083,6231017.7000000002],[1637059800000,1.083,1.0868,1.0804,1.0858,5392462.9000000004],[1637060100000,1.0856,1.0947,1.0844,1.0927,8293781.2999999998],[1637060400000,1.0927,1.0991,1.0889,1.0948,14201629.4000000004],[1637060700000,1.0948,1.0956,1.092,1.0927,10489689.5999999996],[1637061000000,1.0928,1.0932,1.0893,1.092,5223379.2000000002],[1637061300000,1.092,1.0933,1.0892,1.0917,3130525.8999999999],[1637061600000,1.0917,1.0951,1.091,1.0924,4478758.0],[1637061900000,1.0922,1.0961,1.09,1.0961,4269518.2000000002],[1637062200000,1.0961,1.0965,1.0912,1.0944,4219889.2000000002],[1637062500000,1.0944,1.0963,1.092,1.0933,4445458.7999999998],[1637062800000,1.0932,1.0942,1.0893,1.0903,4145158.7000000002],[1637063100000,1.0904,1.0929,1.0898,1.0915,2970968.0],[1637063400000,1.0915,1.094,1.081,1.0869,14647309.9000000004],[1637063700000,1.0869,1.0912,1.0849,1.091,5089475.5999999996],[1637064000000,1.091,1.091,1.0854,1.0869,6876454.0999999996],[1637064300000,1.087,1.0936,1.0847,1.0915,6451835.0999999996],[1637064600000,1.0915,1.0936,1.0883,1.0935,5260521.0],[1637064900000,1.0936,1.0953,1.0917,1.0936,3561178.1000000001],[1637065200000,1.0936,1.0947,1.0919,1.092,5160451.7999999998],[1637065500000,1.092,1.0933,1.0875,1.0883,7458006.2999999998],[1637065800000,1.0883,1.0916,1.0864,1.0912,4436506.2000000002],[1637066100000,1.0912,1.0913,1.0865,1.0883,3664288.7999999998],[1637066400000,1.0883,1.0904,1.0876,1.0895,2131346.2000000002],[1637066700000,1.0896,1.0911,1.088,1.0891,1823000.0],[1637067000000,1.0891,1.0892,1.0858,1.0869,2434249.6000000001],[1637067300000,1.0869,1.0874,1.0781,1.0807,10353451.5999999996],[1637067600000,1.0807,1.0891,1.0807,1.0872,7115227.4000000004],[1637067900000,1.0872,1.0875,1.0831,1.0861,2835656.5],[1637068200000,1.0861,1.0871,1.0837,1.0862,1842530.0],[1637068500000,1.0863,1.0931,1.0861,1.0919,4535343.2000000002],[1637068800000,1.0919,1.0919,1.0896,1.0908,1767315.6000000001],[1637069100000,1.0909,1.0965,1.0908,1.0925,4127187.2999999998],[1637069400000,1.0925,1.0931,1.0874,1.0889,2829371.3999999999],[1637069700000,1.089,1.0941,1.0889,1.0933,2691450.5],[1637070000000,1.0933,1.0982,1.0933,1.0975,3449425.3999999999],[1637070300000,1.0976,1.0995,1.0949,1.0956,3111204.2000000002],[1637070600000,1.0955,1.0977,1.0922,1.097,3361311.6000000001],[1637070900000,1.0971,1.0978,1.0953,1.0955,1874257.1000000001],[1637071200000,1.0955,1.1002,1.0939,1.0996,2874238.1000000001],[1637071500000,1.0995,1.1072,1.0988,1.106,5584774.5999999996],[1637071800000,1.106,1.1063,1.1006,1.1021,6260439.5],[1637072100000,1.1021,1.1053,1.1007,1.1035,2629958.5],[1637072400000,1.1036,1.1071,1.1035,1.1063,2563542.7000000002],[1637072700000,1.1064,1.1074,1.1034,1.1058,2746573.6000000001],[1637073000000,1.1058,1.1088,1.104,1.1067,3563226.6000000001],[1637073300000,1.1067,1.1145,1.1067,1.1124,6518384.7999999998],[1637073600000,1.1125,1.1148,1.1122,1.1147,3836367.8999999999],[1637073900000,1.1147,1.1166,1.1131,1.1139,6381942.5999999996],[1637074200000,1.1138,1.1145,1.1099,1.1105,4349913.0999999996],[1637074500000,1.1105,1.1107,1.1071,1.1101,3994190.7999999998],[1637074800000,1.1101,1.1135,1.1076,1.1091,3753506.8999999999],[1637075100000,1.1091,1.1134,1.1089,1.1113,3603867.3999999999],[1637075400000,1.1112,1.1113,1.107,1.1083,4664129.0],[1637075700000,1.1083,1.1103,1.1064,1.1094,2488059.8999999999],[1637076000000,1.1093,1.1099,1.1039,1.1058,3100351.5],[1637076300000,1.1057,1.1066,1.1051,1.1065,1414436.3999999999],[1637076600000,1.1065,1.1078,1.1012,1.1028,3693132.0],[1637076900000,1.1027,1.1053,1.1026,1.1032,1753110.5],[1637077200000,1.1033,1.1041,1.1006,1.1018,1495171.1000000001],[1637077500000,1.1019,1.1028,1.0997,1.1002,2868409.6000000001],[1637077800000,1.1002,1.102,1.0994,1.1014,1944395.6000000001],[1637078100000,1.1014,1.1019,1.0958,1.0994,4411719.7000000002],[1637078400000,1.0995,1.1042,1.0993,1.1011,4274375.2000000002],[1637078700000,1.1011,1.1019,1.0976,1.1016,4437800.0],[1637079000000,1.1015,1.1062,1.101,1.1043,3558900.2999999998],[1637079300000,1.1042,1.1084,1.1042,1.1047,3116961.7000000002],[1637079600000,1.1047,1.1064,1.1028,1.1032,2383209.5],[1637079900000,1.1031,1.107,1.1019,1.1059,3174573.7000000002],[1637080200000,1.1059,1.1084,1.1052,1.1053,2027170.3999999999],[1637080500000,1.1054,1.1071,1.1028,1.1041,2097532.7999999998],[1637080800000,1.1039,1.1042,1.1009,1.1022,2384703.5],[1637081100000,1.1022,1.1042,1.1021,1.103,1090303.7],[1637081400000,1.1029,1.106,1.1026,1.1053,1679083.3],[1637081700000,1.1054,1.1062,1.1044,1.1047,1281072.5],[1637082000000,1.1047,1.1082,1.1046,1.1075,1870676.6000000001],[1637082300000,1.1075,1.1084,1.1055,1.106,1514063.3999999999],[1637082600000,1.1061,1.1074,1.1059,1.1066,1127931.8],[1637082900000,1.1067,1.1077,1.1052,1.106,2205945.3999999999],[1637083200000,1.106,1.106,1.1014,1.1032,2175136.6000000001],[1637083500000,1.1033,1.104,1.1026,1.1035,681357.6],[1637083800000,1.1035,1.1036,1.0965,1.0987,5090985.2999999998],[1637084100000,1.0988,1.1014,1.0985,1.1007,1185052.1000000001],[1637084400000,1.1007,1.1012,1.0986,1.1011,1336553.0],[1637084700000,1.1011,1.1022,1.0995,1.101,1619343.1000000001],[1637085000000,1.1011,1.1027,1.0995,1.1019,1152992.1000000001],[1637085300000,1.102,1.1024,1.0986,1.0988,1475514.2],[1637085600000,1.0988,1.1033,1.0987,1.1029,1140507.3],[1637085900000,1.1029,1.1055,1.1021,1.1028,2751272.6000000001],[1637086200000,1.1028,1.1034,1.1021,1.1029,870292.5],[1637086500000,1.1029,1.1039,1.1009,1.1014,856580.6],[1637086800000,1.1014,1.1027,1.1014,1.1018,616637.1],[1637087100000,1.1017,1.1019,1.0975,1.0979,1535581.5],[1637087400000,1.0979,1.1009,1.0977,1.0977,1669346.1000000001],[1637087700000,1.0978,1.1001,1.0924,1.0928,4071053.2999999998],[1637088000000,1.0928,1.0945,1.0886,1.0913,5158080.7000000002],[1637088300000,1.0912,1.0916,1.0875,1.0888,3913693.2000000002],[1637088600000,1.0888,1.092,1.0883,1.091,2824819.2000000002],[1637088900000,1.091,1.0922,1.0863,1.0863,1971506.7],[1637089200000,1.0862,1.0964,1.0849,1.095,5786081.9000000004],[1637089500000,1.0949,1.0994,1.0948,1.0988,2539197.7000000002],[1637089800000,1.0987,1.0998,1.0953,1.0959,2441709.2000000002],[1637090100000,1.096,1.0985,1.0931,1.0984,2598843.5],[1637090400000,1.0984,1.0995,1.0975,1.0986,1570715.7],[1637090700000,1.0985,1.0988,1.0956,1.098,1618041.8],[1637091000000,1.098,1.1013,1.0964,1.0985,2150798.7999999998],[1637091300000,1.0983,1.0998,1.0923,1.0933,3299815.6000000001],[1637091600000,1.0934,1.0947,1.093,1.093,1482874.0],[1637091900000,1.0931,1.0936,1.0814,1.0827,9204218.9000000004],[1637092200000,1.0827,1.0865,1.0817,1.0836,3364077.2000000002],[1637092500000,1.0836,1.0932,1.0835,1.0922,3895856.6000000001],[1637092800000,1.0922,1.0922,1.0854,1.0884,3260526.3999999999],[1637093100000,1.0883,1.0927,1.0883,1.089,2290681.6000000001],[1637093400000,1.0889,1.0906,1.0846,1.0864,3570163.0],[1637093700000,1.0863,1.0866,1.0805,1.0841,5326198.9000000004],[1637094000000,1.0841,1.0884,1.0835,1.0853,2398866.2999999998],[1637094300000,1.0852,1.0889,1.0809,1.0812,5647177.2999999998],[1637094600000,1.0812,1.0837,1.0764,1.0807,8712026.0999999996],[1637094900000,1.0808,1.0821,1.0705,1.0711,8140649.7999999998],[1637095200000,1.071,1.0758,1.0686,1.0717,8179432.7000000002],[1637095500000,1.0719,1.0783,1.0656,1.0768,8720833.5999999996],[1637095800000,1.0768,1.079,1.0711,1.0721,6371077.5],[1637096100000,1.0721,1.0811,1.0721,1.0807,6004000.5999999996],[1637096400000,1.0806,1.0901,1.0793,1.09,5236339.0],[1637096700000,1.09,1.0942,1.088,1.0939,7370866.0],[1637097000000,1.0939,1.0949,1.0906,1.0946,4293189.4000000004],[1637097300000,1.0944,1.0978,1.0933,1.0939,4202180.5999999996],[1637097600000,1.0938,1.0972,1.0922,1.0957,2739087.2000000002],[1637097900000,1.0958,1.096,1.0911,1.0925,2403520.3999999999],[1637098200000,1.0925,1.0972,1.0913,1.0947,2340053.2999999998],[1637098500000,1.0948,1.0964,1.0933,1.0951,2272554.7000000002],[1637098800000,1.0951,1.0966,1.0931,1.0941,3153594.7000000002],[1637099100000,1.094,1.096,1.0923,1.0956,2311184.7999999998],[1637099400000,1.0957,1.0966,1.0945,1.0963,1359327.6000000001],[1637099700000,1.0962,1.0969,1.0943,1.0955,1402210.6000000001],[1637100000000,1.0954,1.0999,1.0954,1.0999,2214508.5],[1637100300000,1.0999,1.1023,1.0971,1.0983,3369029.2999999998],[1637100600000,1.0984,1.1018,1.0983,1.1013,1713877.6000000001],[1637100900000,1.1014,1.1054,1.0995,1.1021,4033393.7999999998],[1637101200000,1.1021,1.1043,1.1017,1.1033,3141523.2000000002],[1637101500000,1.1033,1.1047,1.1005,1.1034,2721262.7000000002],[1637101800000,1.1034,1.104,1.1031,1.1038,907552.1],[1637102100000,1.1038,1.104,1.1005,1.1011,1635420.7],[1637102400000,1.1011,1.1013,1.1005,1.1011,828877.5],[1637102700000,1.1011,1.1014,1.0997,1.1007,1036611.5],[1637103000000,1.1008,1.1016,1.0992,1.1,755849.6],[1637103300000,1.1,1.1005,1.0991,1.0993,682736.6],[1637103600000,1.0994,1.102,1.0993,1.1009,1796997.3],[1637103900000,1.1009,1.1009,1.0976,1.0992,2076148.3],[1637104200000,1.0992,1.101,1.0958,1.0966,1887413.5],[1637104500000,1.0967,1.0976,1.0963,1.0968,1517664.7],[1637104800000,1.0967,1.0989,1.0956,1.0985,1420554.8999999999],[1637105100000,1.0984,1.0996,1.0966,1.0979,1159661.3],[1637105400000,1.098,1.0989,1.0946,1.0963,826631.7],[1637105700000,1.0962,1.0972,1.0936,1.0942,1012139.7],[1637106000000,1.0943,1.0965,1.0934,1.0952,1016698.3],[1637106300000,1.0953,1.0959,1.0914,1.0921,2394921.2999999998],[1637106600000,1.092,1.0932,1.0887,1.0924,1992608.7],[1637106900000,1.0923,1.0932,1.0875,1.0881,1619403.3],[1637107200000,1.0881,1.0923,1.0814,1.0819,7328761.2000000002],[1637107500000,1.0819,1.0949,1.0798,1.0933,7791120.0],[1637107800000,1.0933,1.0957,1.0894,1.0894,3203692.2000000002],[1637108100000,1.0893,1.0923,1.0854,1.0857,3370459.7999999998],[1637108400000,1.0856,1.0857,1.0773,1.0822,7263993.5999999996],[1637108700000,1.0821,1.0833,1.0803,1.0809,2084450.0],[1637109000000,1.0809,1.0869,1.0803,1.0845,2879224.5],[1637109300000,1.0845,1.088,1.0831,1.0854,2858814.3999999999],[1637109600000,1.0855,1.0873,1.0795,1.0819,4147523.7000000002],[1637109900000,1.0819,1.084,1.0765,1.0771,3351790.2999999998],[1637110200000,1.0771,1.0816,1.0768,1.08,2906774.5],[1637110500000,1.0799,1.083,1.0799,1.0802,2021854.0],[1637110800000,1.0803,1.0867,1.0773,1.0776,4514667.4000000004],[1637111100000,1.0776,1.0804,1.077,1.0781,2377557.0],[1637111400000,1.078,1.0852,1.0767,1.0845,2879961.7000000002],[1637111700000,1.0845,1.0857,1.0824,1.0834,1964225.8],[1637112000000,1.0833,1.0875,1.0827,1.0868,1728741.2],[1637112300000,1.0868,1.0954,1.0868,1.0949,4705829.0],[1637112600000,1.095,1.0984,1.0907,1.0968,3564621.7000000002],[1637112900000,1.0969,1.098,1.0937,1.094,3910508.6000000001],[1637113200000,1.0941,1.0956,1.0927,1.095,1694611.6000000001],[1637113500000,1.0949,1.0962,1.087,1.0871,3501061.2000000002],[1637113800000,1.0871,1.0889,1.0823,1.0835,3798244.5],[1637114100000,1.0835,1.0861,1.0775,1.0792,5097158.9000000004],[1637114400000,1.0791,1.0828,1.0757,1.0781,6593764.2999999998],[1637114700000,1.0782,1.0855,1.0777,1.085,4370758.5],[1637115000000,1.0849,1.0858,1.0815,1.0815,2163743.7000000002],[1637115300000,1.0814,1.0838,1.0785,1.0837,2472082.7000000002],[1637115600000,1.0837,1.0877,1.0837,1.086,2309508.2999999998],[1637115900000,1.086,1.0875,1.0854,1.0861,1587844.2],[1637116200000,1.086,1.088,1.0812,1.0815,2928244.2000000002],[1637116500000,1.0815,1.0869,1.0808,1.0859,2455434.7999999998],[1637116800000,1.0859,1.0864,1.0798,1.0803,1623658.6000000001],[1637117100000,1.0804,1.0827,1.079,1.0796,2038073.3999999999],[1637117400000,1.0796,1.0825,1.0733,1.0757,3769966.2999999998],[1637117700000,1.0757,1.0784,1.074,1.0763,2361069.2999999998],[1637118000000,1.0763,1.0821,1.0763,1.0778,3724170.2999999998],[1637118300000,1.0778,1.0782,1.0664,1.0695,9028147.0],[1637118600000,1.0694,1.0721,1.0629,1.0636,9485592.0999999996],[1637118900000,1.0636,1.0708,1.0584,1.0697,10075438.1999999993],[1637119200000,1.0696,1.0708,1.0669,1.0675,2935578.7000000002],[1637119500000,1.0675,1.0803,1.0667,1.0799,4704890.0999999996],[1637119800000,1.08,1.0859,1.0777,1.0822,7328433.0999999996],[1637120100000,1.0822,1.0824,1.0757,1.0786,3887194.3999999999],[1637120400000,1.0786,1.0794,1.0739,1.0767,2839375.5],[1637120700000,1.0768,1.0773,1.0723,1.0747,2334452.6000000001],[1637121000000,1.0748,1.0794,1.0725,1.0728,3459570.2999999998],[1637121300000,1.0727,1.0746,1.0674,1.0683,4035213.2999999998],[1637121600000,1.0684,1.0718,1.0638,1.0684,8054890.0],[1637121900000,1.0684,1.0726,1.0636,1.0638,3362220.7000000002],[1637122200000,1.0638,1.0664,1.0613,1.0614,2737673.2000000002],[1637122500000,1.0612,1.0698,1.0604,1.0679,6707714.5],[1637122800000,1.0678,1.0739,1.065,1.0694,5305425.5],[1637123100000,1.0694,1.0761,1.0689,1.0756,3789937.3999999999],[1637123400000,1.0756,1.079,1.0728,1.0767,4637235.0],[1637123700000,1.0767,1.0767,1.0717,1.0748,3380981.0],[1637124000000,1.0747,1.0782,1.0736,1.0738,2801660.3999999999],[1637124300000,1.0738,1.074,1.066,1.0663,3481897.7999999998],[1637124600000,1.0663,1.0689,1.0645,1.0678,2394145.7000000002],[1637124900000,1.0678,1.0768,1.0671,1.0766,3858423.2999999998],[1637125200000,1.0767,1.0817,1.073,1.0809,3924376.5],[1637125500000,1.0808,1.0833,1.0783,1.083,2728657.0],[1637125800000,1.083,1.0831,1.0795,1.0802,1779158.6000000001],[1637126100000,1.0802,1.0832,1.0786,1.0802,2990620.2999999998],[1637126400000,1.0802,1.0847,1.08,1.0846,2635455.2999999998],[1637126700000,1.0845,1.0845,1.0788,1.0797,4512611.7000000002],[1637127000000,1.0796,1.0837,1.0794,1.0822,1958104.1000000001],[1637127300000,1.0822,1.0857,1.082,1.0849,2294853.2000000002],[1637127600000,1.0849,1.0865,1.0844,1.0844,1880656.6000000001],[1637127900000,1.0844,1.0884,1.0832,1.0845,2602161.3999999999],[1637128200000,1.0846,1.0846,1.0816,1.0824,1432818.8999999999],[1637128500000,1.0824,1.0857,1.082,1.0842,1171212.2],[1637128800000,1.0843,1.086,1.0825,1.084,2035117.5],[1637129100000,1.0841,1.0848,1.0814,1.0819,1370075.7],[1637129400000,1.0819,1.082,1.0775,1.0789,2331354.0],[1637129700000,1.079,1.0801,1.0752,1.0759,1995356.3999999999],[1637130000000,1.0759,1.0798,1.0745,1.0797,2047187.6000000001],[1637130300000,1.0796,1.0796,1.0754,1.0779,2435020.2000000002],[1637130600000,1.078,1.0817,1.0779,1.0816,1979132.8999999999],[1637130900000,1.0817,1.0825,1.0802,1.0821,1801370.0],[1637131200000,1.0821,1.0821,1.0793,1.0801,1510297.2],[1637131500000,1.0802,1.081,1.0786,1.0809,1341184.1000000001],[1637131800000,1.0809,1.0811,1.0747,1.0767,3233574.1000000001],[1637132100000,1.0766,1.0819,1.0766,1.0801,3653328.0],[1637132400000,1.08,1.0841,1.08,1.0802,1885877.1000000001],[1637132700000,1.0801,1.0812,1.0762,1.0763,1922618.1000000001],[1637133000000,1.0762,1.0784,1.075,1.0754,2231747.1000000001],[1637133300000,1.0754,1.0771,1.074,1.0759,2723311.3999999999],[1637133600000,1.076,1.0782,1.0737,1.0775,2133324.0],[1637133900000,1.0775,1.0795,1.0766,1.0775,2760661.2000000002],[1637134200000,1.0775,1.0813,1.0773,1.0813,1752676.6000000001],[1637134500000,1.0812,1.0832,1.0803,1.0823,3506472.5],[1637134800000,1.0824,1.0911,1.0824,1.09,5288956.9000000004],[1637135100000,1.0899,1.0953,1.0876,1.0943,5344265.2000000002],[1637135400000,1.0942,1.0948,1.0916,1.0923,2699549.2000000002],[1637135700000,1.0922,1.0923,1.0865,1.0871,3138860.5],[1637136000000,1.0871,1.089,1.0844,1.0881,2924426.2000000002],[1637136300000,1.0881,1.0906,1.0874,1.0878,2051583.5],[1637136600000,1.0877,1.0901,1.0867,1.0899,1281969.5],[1637136900000,1.0898,1.0916,1.088,1.0903,2105461.6000000001],[1637137200000,1.0903,1.0922,1.0896,1.0897,1548503.8],[1637137500000,1.0898,1.0898,1.0858,1.0861,2406126.1000000001],[1637137800000,1.0862,1.0864,1.0808,1.0818,3634602.3999999999],[1637138100000,1.0818,1.0863,1.0818,1.0862,1386206.1000000001],[1637138400000,1.0863,1.0863,1.0836,1.0851,1375716.0],[1637138700000,1.0851,1.0884,1.0839,1.0859,2153031.5],[1637139000000,1.0859,1.0862,1.0811,1.0818,1906201.7],[1637139300000,1.0817,1.0827,1.0805,1.0806,1106265.3999999999],[1637139600000,1.0805,1.0807,1.0762,1.078,3489174.5],[1637139900000,1.078,1.0793,1.0762,1.0763,1696153.8],[1637140200000,1.0764,1.0773,1.0732,1.0742,3405487.5],[1637140500000,1.0742,1.0779,1.0725,1.0767,3377160.0],[1637140800000,1.0765,1.0797,1.0753,1.076,1986339.8],[1637141100000,1.076,1.0774,1.071,1.0718,4997953.5],[1637141400000,1.0716,1.074,1.07,1.0712,2219457.8999999999],[1637141700000,1.0712,1.0735,1.071,1.0724,1613672.6000000001],[1637142000000,1.0724,1.0736,1.0669,1.0726,4182243.6000000001],[1637142300000,1.0726,1.0753,1.0679,1.0707,5490107.2999999998],[1637142600000,1.0707,1.0744,1.0679,1.073,7715558.5999999996],[1637142900000,1.0731,1.0754,1.0684,1.0706,6229269.0999999996],[1637143200000,1.0707,1.0816,1.0706,1.0801,7898025.7000000002],[1637143500000,1.08,1.0815,1.0781,1.0803,3500217.6000000001],[1637143800000,1.0802,1.0805,1.0764,1.0782,2081784.7],[1637144100000,1.0782,1.0834,1.078,1.0831,2685664.8999999999],[1637144400000,1.083,1.0863,1.0823,1.0852,2647942.1000000001],[1637144700000,1.0852,1.0862,1.0838,1.0838,1861143.2],[1637145000000,1.0838,1.0845,1.076,1.076,4680665.9000000004],[1637145300000,1.076,1.0863,1.0703,1.0852,9234175.9000000004],[1637145600000,1.0853,1.095,1.0825,1.0899,12409553.6999999993],[1637145900000,1.0899,1.0964,1.0862,1.096,6436981.0999999996],[1637146200000,1.096,1.1068,1.096,1.106,11824100.3000000007],[1637146500000,1.106,1.1061,1.0996,1.1038,7977364.5999999996],[1637146800000,1.1039,1.106,1.0979,1.1009,7892673.9000000004],[1637147100000,1.101,1.1057,1.0986,1.1025,6771785.7999999998],[1637147400000,1.1025,1.1062,1.1018,1.1052,3735237.2000000002],[1637147700000,1.1053,1.1103,1.1028,1.1079,5009217.0],[1637148000000,1.1079,1.1084,1.1041,1.1064,3315650.8999999999],[1637148300000,1.1064,1.1069,1.1041,1.1047,3016959.2999999998],[1637148600000,1.1047,1.1058,1.1014,1.1018,3766007.3999999999],[1637148900000,1.1018,1.102,1.0981,1.1,3863771.5],[1637149200000,1.1001,1.1021,1.0994,1.1017,2386893.3999999999],[1637149500000,1.1017,1.1038,1.1007,1.1037,1990758.3999999999],[1637149800000,1.1038,1.1063,1.1032,1.1057,2846771.7000000002],[1637150100000,1.1058,1.1086,1.1048,1.1069,3226272.0],[1637150400000,1.1069,1.1081,1.1026,1.1036,3136059.2999999998],[1637150700000,1.1037,1.1062,1.1022,1.1055,2050220.6000000001],[1637151000000,1.1055,1.1074,1.1054,1.1063,1736577.5],[1637151300000,1.1063,1.1064,1.1035,1.1059,2622736.2999999998],[1637151600000,1.106,1.1068,1.1044,1.1046,2159126.8999999999],[1637151900000,1.1047,1.1059,1.1034,1.1038,1720883.5],[1637152200000,1.1037,1.1075,1.1037,1.1071,1897526.3999999999],[1637152500000,1.1071,1.1088,1.1061,1.1068,2402077.2999999998],[1637152800000,1.1069,1.107,1.1043,1.1061,2425579.7999999998],[1637153100000,1.106,1.1067,1.1039,1.1042,1281319.6000000001],[1637153400000,1.1043,1.1056,1.1042,1.1049,929455.9],[1637153700000,1.1048,1.1065,1.1048,1.1059,1360515.7],[1637154000000,1.1058,1.1133,1.1048,1.1096,6059693.5],[1637154300000,1.1096,1.1105,1.1045,1.1047,3046996.1000000001],[1637154600000,1.1048,1.1069,1.1045,1.1058,1648138.3999999999],[1637154900000,1.1059,1.1063,1.1012,1.1015,2568526.3999999999],[1637155200000,1.1015,1.1032,1.1,1.1006,2735189.7000000002],[1637155500000,1.1007,1.1033,1.1002,1.1029,1968598.8],[1637155800000,1.1028,1.1042,1.0989,1.1039,3753897.0],[1637156100000,1.1038,1.1044,1.1026,1.1038,1000207.1],[1637156400000,1.1038,1.1039,1.1001,1.1009,2153596.2999999998],[1637156700000,1.1009,1.1032,1.099,1.0995,3063064.7999999998],[1637157000000,1.0998,1.1003,1.0972,1.098,2697992.3999999999],[1637157300000,1.098,1.0992,1.096,1.096,2621387.0],[1637157600000,1.0961,1.1014,1.0961,1.1,2654646.5],[1637157900000,1.1001,1.1005,1.0981,1.0982,1341112.3999999999],[1637158200000,1.0982,1.0993,1.0965,1.0966,2182093.0],[1637158500000,1.0965,1.0992,1.0934,1.095,3821465.5],[1637158800000,1.095,1.0966,1.0936,1.095,1940575.5],[1637159100000,1.095,1.0984,1.0923,1.0977,2933823.8999999999],[1637159400000,1.0978,1.0978,1.0917,1.0923,2517615.2999999998],[1637159700000,1.0922,1.0956,1.0907,1.0914,4105409.0],[1637160000000,1.0913,1.0936,1.0906,1.0915,1618029.3999999999],[1637160300000,1.0915,1.0932,1.0886,1.0927,2769392.7999999998],[1637160600000,1.0926,1.0926,1.0847,1.0869,6282562.7999999998],[1637160900000,1.087,1.0875,1.0842,1.0846,2478907.8999999999],[1637161200000,1.0846,1.0879,1.0833,1.0874,3638166.1000000001],[1637161500000,1.0873,1.0896,1.0861,1.0868,2856420.7000000002],[1637161800000,1.0868,1.0882,1.0837,1.0838,2956741.2000000002],[1637162100000,1.0839,1.0865,1.0827,1.0845,2869705.3999999999],[1637162400000,1.0846,1.0867,1.0815,1.0827,3770542.6000000001],[1637162700000,1.0827,1.0839,1.0778,1.0804,4561579.5],[1637163000000,1.0805,1.0833,1.0756,1.0786,6138538.2999999998],[1637163300000,1.0787,1.0827,1.0783,1.0822,2007820.1000000001],[1637163600000,1.0823,1.0863,1.0816,1.085,5314252.5],[1637163900000,1.085,1.0872,1.0845,1.0871,1507044.3],[1637164200000,1.0871,1.0901,1.085,1.0855,2797640.3999999999],[1637164500000,1.0855,1.0875,1.0846,1.0861,1942630.8999999999],[1637164800000,1.0863,1.0866,1.0798,1.0808,3071536.1000000001],[1637165100000,1.0807,1.0856,1.0803,1.0854,1891225.3999999999],[1637165400000,1.0854,1.0901,1.0824,1.0901,3241084.6000000001],[1637165700000,1.0901,1.0919,1.0834,1.0834,4620817.5999999996],[1637166000000,1.0835,1.0896,1.0829,1.0875,2185543.7999999998],[1637166300000,1.0876,1.0912,1.0871,1.0906,1561475.5],[1637166600000,1.0906,1.095,1.09,1.0947,3319919.7000000002],[1637166900000,1.0947,1.0959,1.0915,1.0931,2493865.1000000001],[1637167200000,1.093,1.0943,1.0909,1.0943,2177410.2999999998],[1637167500000,1.0942,1.0963,1.0928,1.0961,1940089.1000000001],[1637167800000,1.0962,1.0963,1.0937,1.0945,1516195.5],[1637168100000,1.0946,1.0954,1.0933,1.095,1323452.1000000001],[1637168400000,1.095,1.096,1.0922,1.0927,1896410.2],[1637168700000,1.0926,1.093,1.0906,1.091,1236879.5],[1637169000000,1.0911,1.0912,1.0879,1.0888,1934815.5],[1637169300000,1.0887,1.0925,1.0879,1.0925,1552221.6000000001],[1637169600000,1.0925,1.0931,1.088,1.0903,1583392.8],[1637169900000,1.0903,1.0905,1.0882,1.0896,1510502.8999999999],[1637170200000,1.0896,1.0924,1.089,1.0923,1132094.8],[1637170500000,1.0923,1.0951,1.0919,1.0937,1582304.3],[1637170800000,1.0937,1.0941,1.0912,1.0929,1549678.0],[1637171100000,1.093,1.096,1.0925,1.0959,1505929.7],[1637171400000,1.0959,1.0984,1.0938,1.0948,2402150.2999999998],[1637171700000,1.095,1.0953,1.0928,1.0948,1127752.1000000001],[1637172000000,1.0948,1.097,1.094,1.0953,1735674.8],[1637172300000,1.0954,1.0974,1.0947,1.096,1491627.8999999999],[1637172600000,1.096,1.0969,1.0955,1.0965,664658.1],[1637172900000,1.0964,1.098,1.0959,1.0969,1453066.6000000001],[1637173200000,1.0969,1.0983,1.0967,1.0972,1340475.8],[1637173500000,1.0971,1.1006,1.0971,1.1002,1625938.8],[1637173800000,1.1001,1.1008,1.0994,1.0996,1679674.8999999999],[1637174100000,1.0997,1.0997,1.0979,1.0985,1132102.1000000001],[1637174400000,1.0985,1.0991,1.0969,1.0991,1373257.7],[1637174700000,1.099,1.1004,1.0982,1.0991,887280.9],[1637175000000,1.099,1.1023,1.099,1.1022,1543792.0],[1637175300000,1.1023,1.1032,1.0996,1.1,1377006.3],[1637175600000,1.0999,1.1031,1.0987,1.1007,2702878.7000000002],[1637175900000,1.1006,1.1012,1.0999,1.1011,799311.3],[1637176200000,1.1011,1.1015,1.0973,1.0979,2322776.1000000001],[1637176500000,1.0979,1.098,1.0959,1.0968,1159578.1000000001],[1637176800000,1.0968,1.0969,1.0947,1.095,1220533.8999999999],[1637177100000,1.0951,1.0985,1.0948,1.098,1157343.8999999999],[1637177400000,1.0979,1.098,1.0959,1.0964,929510.9],[1637177700000,1.0963,1.0986,1.0956,1.0969,1077697.0],[1637178000000,1.0968,1.0971,1.0946,1.0958,1033973.3],[1637178300000,1.0959,1.0977,1.0931,1.0934,1413592.3],[1637178600000,1.0933,1.0952,1.0926,1.0951,967092.4],[1637178900000,1.0951,1.0959,1.0942,1.0951,751371.1],[1637179200000,1.0951,1.0974,1.0933,1.0963,2208356.7999999998],[1637179500000,1.0962,1.0972,1.0945,1.0963,1179075.1000000001],[1637179800000,1.0964,1.097,1.0948,1.0949,1008579.0],[1637180100000,1.095,1.0952,1.0913,1.0917,1845474.7],[1637180400000,1.0917,1.0934,1.0906,1.0931,1408476.7],[1637180700000,1.0931,1.095,1.0931,1.0936,851841.7],[1637181000000,1.0936,1.0975,1.0927,1.0971,1295190.3999999999],[1637181300000,1.0971,1.1021,1.0971,1.1006,2684562.7000000002],[1637181600000,1.1005,1.1023,1.0997,1.102,1366607.1000000001],[1637181900000,1.102,1.1025,1.0976,1.0977,1451796.3999999999],[1637182200000,1.0976,1.1009,1.0973,1.1002,1050928.0],[1637182500000,1.1001,1.1006,1.0989,1.0999,1087095.8999999999],[1637182800000,1.1,1.1002,1.0966,1.0968,1062796.8],[1637183100000,1.0968,1.0969,1.0931,1.0936,1406189.6000000001],[1637183400000,1.0936,1.0971,1.0936,1.0959,908063.2],[1637183700000,1.096,1.0972,1.0928,1.0933,1567443.1000000001],[1637184000000,1.0933,1.0943,1.0914,1.0917,1478413.6000000001],[1637184300000,1.0916,1.0919,1.0877,1.0886,2730549.7999999998],[1637184600000,1.0885,1.0925,1.0885,1.0913,1442823.2],[1637184900000,1.0913,1.0922,1.09,1.0901,966258.9],[1637185200000,1.0902,1.0925,1.0901,1.0923,483252.6],[1637185500000,1.0923,1.0941,1.092,1.0921,760651.6],[1637185800000,1.092,1.0928,1.0904,1.091,859875.9],[1637186100000,1.091,1.0954,1.0908,1.0953,1327832.2],[1637186400000,1.0953,1.0956,1.0942,1.0943,828371.2],[1637186700000,1.0943,1.0945,1.0924,1.0932,1071065.3999999999],[1637187000000,1.0931,1.0953,1.0921,1.0949,753722.1],[1637187300000,1.0949,1.0951,1.0903,1.092,994191.6],[1637187600000,1.092,1.0926,1.0884,1.0896,1821173.5],[1637187900000,1.0896,1.0899,1.0853,1.0881,2776779.7000000002],[1637188200000,1.0882,1.089,1.0877,1.0887,673163.3],[1637188500000,1.0887,1.09,1.0887,1.09,485292.1],[1637188800000,1.0899,1.0934,1.0899,1.091,1079396.8999999999],[1637189100000,1.0909,1.0909,1.0888,1.0894,1290107.8999999999],[1637189400000,1.0894,1.0927,1.0891,1.0918,2200631.1000000001],[1637189700000,1.0916,1.093,1.0909,1.0928,564386.7],[1637190000000,1.0928,1.0948,1.0928,1.0945,747579.7],[1637190300000,1.0945,1.0948,1.0921,1.0921,969558.4],[1637190600000,1.0921,1.0921,1.0878,1.0886,1688184.8999999999],[1637190900000,1.0886,1.0909,1.0883,1.0903,844001.5],[1637191200000,1.0902,1.092,1.0902,1.0914,712580.3],[1637191500000,1.0915,1.0919,1.0903,1.0919,475440.6],[1637191800000,1.0918,1.0924,1.0902,1.0906,692121.2],[1637192100000,1.0907,1.091,1.0894,1.0895,570722.4],[1637192400000,1.0896,1.092,1.0895,1.0919,657315.2],[1637192700000,1.092,1.0975,1.0916,1.0974,1971930.8],[1637193000000,1.0975,1.0997,1.0962,1.0976,3437924.8999999999],[1637193300000,1.0977,1.0977,1.0944,1.0958,1948534.0],[1637193600000,1.0959,1.098,1.0907,1.0924,2614007.7000000002],[1637193900000,1.0922,1.1008,1.0919,1.0998,2948767.2000000002],[1637194200000,1.0997,1.1005,1.0971,1.099,2741193.2000000002],[1637194500000,1.099,1.102,1.0969,1.1005,4643687.9000000004],[1637194800000,1.1006,1.1028,1.0987,1.0998,2682431.3999999999],[1637195100000,1.0997,1.1009,1.0976,1.1001,1593940.1000000001],[1637195400000,1.1002,1.1013,1.0995,1.1011,619804.1],[1637195700000,1.1011,1.1045,1.101,1.1041,1441890.2],[1637196000000,1.1041,1.1056,1.1028,1.1041,2549423.7000000002],[1637196300000,1.1041,1.1061,1.103,1.1059,1393903.8999999999],[1637196600000,1.106,1.1072,1.1048,1.1067,1175572.8],[1637196900000,1.1068,1.1073,1.1049,1.105,1781210.8],[1637197200000,1.105,1.1061,1.1025,1.1039,2318627.7000000002],[1637197500000,1.1039,1.104,1.1027,1.1031,978156.0],[1637197800000,1.1031,1.1033,1.0992,1.1008,2558175.0],[1637198100000,1.1009,1.1019,1.0983,1.1002,3132340.3999999999],[1637198400000,1.1003,1.1013,1.0995,1.1008,788701.3],[1637198700000,1.1008,1.1043,1.1008,1.1041,1273241.0],[1637199000000,1.104,1.1377,1.104,1.1374,36479238.1000000015],[1637199300000,1.1375,1.162,1.131,1.16,37448787.799999997],[1637199600000,1.1603,1.1603,1.1405,1.1415,18739494.6999999993],[1637199900000,1.1415,1.1429,1.13,1.1348,12259697.5999999996],[1637200200000,1.1347,1.1353,1.1232,1.1252,11695103.5999999996],[1637200500000,1.1252,1.1312,1.1237,1.129,6841467.5],[1637200800000,1.129,1.1329,1.1286,1.1299,6573764.7999999998],[1637201100000,1.1298,1.1313,1.1251,1.1256,4100464.8999999999],[1637201400000,1.1256,1.1499,1.1243,1.1332,31686039.8000000007],[1637201700000,1.1331,1.1333,1.1277,1.128,4499818.2999999998],[1637202000000,1.1281,1.1294,1.1266,1.1292,2592096.3999999999],[1637202300000,1.1291,1.1311,1.1287,1.1311,1585982.8],[1637202600000,1.131,1.1393,1.1247,1.1339,11493173.5999999996],[1637202900000,1.1339,1.1364,1.1323,1.1333,3811665.3999999999],[1637203200000,1.1333,1.1342,1.1313,1.1336,2762649.7000000002],[1637203500000,1.1335,1.138,1.1324,1.1359,3023745.2000000002],[1637203800000,1.1359,1.1415,1.1357,1.1405,3071272.5],[1637204100000,1.1405,1.1405,1.1368,1.1385,2900170.0],[1637204400000,1.1385,1.1434,1.1372,1.1425,4306973.0999999996],[1637204700000,1.1424,1.1428,1.1346,1.1349,3390360.2999999998],[1637205000000,1.1348,1.1372,1.133,1.1335,3165410.2000000002],[1637205300000,1.1335,1.1375,1.1327,1.1338,1949274.1000000001],[1637205600000,1.1336,1.1356,1.1327,1.1338,1811876.2],[1637205900000,1.1338,1.1341,1.13,1.1307,4368692.7999999998],[1637206200000,1.1307,1.1325,1.1282,1.1292,3741574.2999999998],[1637206500000,1.1291,1.1322,1.1288,1.1318,2700658.2999999998],[1637206800000,1.1319,1.1321,1.1293,1.1293,1404040.3],[1637207100000,1.1293,1.1313,1.1262,1.1272,2801551.8999999999],[1637207400000,1.1271,1.1277,1.1238,1.1244,5518152.7999999998],[1637207700000,1.1245,1.1272,1.1241,1.1248,1519698.0],[1637208000000,1.1249,1.1321,1.1226,1.131,5786089.0999999996],[1637208300000,1.1309,1.133,1.1306,1.1307,1540875.6000000001],[1637208600000,1.1306,1.1322,1.1275,1.1279,2355058.6000000001],[1637208900000,1.128,1.1298,1.1261,1.1271,2771309.2999999998],[1637209200000,1.1271,1.1279,1.1254,1.1261,1854907.1000000001],[1637209500000,1.1262,1.1327,1.1169,1.1198,16188064.8000000007],[1637209800000,1.1198,1.1481,1.1165,1.1475,16896801.6000000015],[1637210100000,1.1475,1.1481,1.1322,1.1389,14173254.4000000004],[1637210400000,1.139,1.1407,1.1318,1.1364,13631417.6999999993],[1637210700000,1.1364,1.1404,1.1284,1.1294,11131764.9000000004],[1637211000000,1.1294,1.1309,1.1222,1.125,6321553.0],[1637211300000,1.125,1.1254,1.1136,1.115,11839636.9000000004],[1637211600000,1.1149,1.1176,1.1114,1.1155,6914612.5],[1637211900000,1.1153,1.1216,1.1134,1.1208,8321634.2999999998],[1637212200000,1.1208,1.1226,1.1187,1.121,5837211.4000000004],[1637212500000,1.121,1.1238,1.1181,1.1193,5989845.5999999996],[1637212800000,1.1193,1.1252,1.1169,1.1184,6576852.7999999998],[1637213100000,1.1184,1.123,1.1168,1.1219,3616199.8999999999],[1637213400000,1.1219,1.1269,1.1215,1.1219,5057255.5999999996],[1637213700000,1.1219,1.1243,1.1193,1.1198,4922280.5999999996],[1637214000000,1.1198,1.1226,1.1194,1.1194,3917341.2999999998],[1637214300000,1.1195,1.122,1.1137,1.1152,8680504.4000000004],[1637214600000,1.1153,1.1162,1.1118,1.1156,5167395.4000000004],[1637214900000,1.1156,1.1183,1.1117,1.1142,6087973.5],[1637215200000,1.1142,1.1153,1.1111,1.1131,4553846.2999999998],[1637215500000,1.1132,1.1134,1.1084,1.1087,4937834.5],[1637215800000,1.1087,1.1104,1.1064,1.1082,4350161.0],[1637216100000,1.1081,1.114,1.106,1.1139,4203990.5999999996],[1637216400000,1.114,1.1151,1.1102,1.1145,3909556.7000000002],[1637216700000,1.1145,1.1164,1.1123,1.1134,2783403.1000000001],[1637217000000,1.1133,1.1143,1.1104,1.1111,1793417.8],[1637217300000,1.1111,1.1175,1.111,1.1168,2683384.2000000002],[1637217600000,1.1168,1.1168,1.1129,1.1144,1596005.3],[1637217900000,1.1143,1.1143,1.1102,1.1124,1885052.7],[1637218200000,1.1121,1.1136,1.1086,1.1088,1878341.2],[1637218500000,1.1089,1.1145,1.1076,1.1142,2420014.8999999999],[1637218800000,1.1142,1.1147,1.1087,1.1096,3307754.7999999998],[1637219100000,1.1095,1.112,1.1086,1.1104,2543497.0],[1637219400000,1.1104,1.1104,1.1056,1.1059,2295635.2000000002],[1637219700000,1.1058,1.1079,1.1034,1.1073,2723064.3999999999],[1637220000000,1.1074,1.1078,1.103,1.1044,2910400.1000000001],[1637220300000,1.1043,1.1045,1.1022,1.103,2304491.7999999998],[1637220600000,1.1031,1.1052,1.099,1.1009,6195101.9000000004],[1637220900000,1.1009,1.1024,1.0966,1.1015,3942212.8999999999],[1637221200000,1.1015,1.1064,1.1004,1.1058,3304070.8999999999],[1637221500000,1.1057,1.1076,1.1043,1.1064,2473126.7000000002],[1637221800000,1.1065,1.1079,1.1044,1.105,2062578.0],[1637222100000,1.105,1.1078,1.1048,1.1074,1259992.1000000001],[1637222400000,1.1075,1.1104,1.1052,1.1056,3241738.8999999999],[1637222700000,1.1056,1.1082,1.1038,1.1055,2764367.7999999998],[1637223000000,1.1055,1.1065,1.103,1.1033,2205122.6000000001],[1637223300000,1.1034,1.1082,1.1022,1.1078,1854984.3999999999],[1637223600000,1.1077,1.1093,1.1062,1.1073,1405444.0],[1637223900000,1.1074,1.1097,1.107,1.109,1632723.5],[1637224200000,1.109,1.1103,1.1074,1.1078,2075103.8],[1637224500000,1.1079,1.1079,1.104,1.1045,1888507.0],[1637224800000,1.1045,1.1053,1.1033,1.1037,1495111.3999999999],[1637225100000,1.1038,1.1082,1.1028,1.1043,4119453.2999999998],[1637225400000,1.1043,1.107,1.1037,1.1042,1954533.8],[1637225700000,1.1043,1.1079,1.1038,1.1072,1478600.3999999999],[1637226000000,1.107,1.1077,1.1032,1.1035,3126430.7999999998],[1637226300000,1.1035,1.105,1.0976,1.0985,6405633.7000000002],[1637226600000,1.0986,1.0999,1.083,1.0906,12260258.1999999993],[1637226900000,1.0906,1.0923,1.0866,1.089,5197434.7999999998],[1637227200000,1.0891,1.0917,1.0814,1.0839,7086193.5],[1637227500000,1.0839,1.0857,1.0824,1.0853,3046616.2000000002],[1637227800000,1.0853,1.0882,1.079,1.0845,5761242.0999999996],[1637228100000,1.0845,1.0847,1.078,1.084,5052047.0999999996],[1637228400000,1.084,1.0874,1.0831,1.0846,3199587.5],[1637228700000,1.0845,1.0872,1.0815,1.0828,2354368.8999999999],[1637229000000,1.0828,1.0869,1.0811,1.0819,2610370.3999999999],[1637229300000,1.0819,1.0898,1.0773,1.0869,8080943.0],[1637229600000,1.0869,1.0929,1.086,1.0927,4635765.2999999998],[1637229900000,1.0927,1.0927,1.0864,1.0876,2974256.7000000002],[1637230200000,1.0877,1.0878,1.0831,1.0848,2237304.3999999999],[1637230500000,1.0847,1.0858,1.0811,1.0849,2963489.2999999998],[1637230800000,1.085,1.0913,1.081,1.0892,3821236.0],[1637231100000,1.0893,1.0901,1.0872,1.0894,2304758.1000000001],[1637231400000,1.0894,1.0948,1.0886,1.0946,2668053.0],[1637231700000,1.0945,1.095,1.0913,1.0918,2534872.2000000002],[1637232000000,1.0919,1.0944,1.091,1.0942,1711755.1000000001],[1637232300000,1.0941,1.0955,1.0917,1.0944,2627556.3999999999],[1637232600000,1.0944,1.0973,1.0929,1.0973,1769328.3],[1637232900000,1.0973,1.099,1.0954,1.0979,2591498.7000000002],[1637233200000,1.098,1.1027,1.0947,1.1025,5480946.7999999998],[1637233500000,1.1025,1.1025,1.0985,1.099,3196790.7999999998],[1637233800000,1.099,1.0999,1.0973,1.0993,1710833.5],[1637234100000,1.0994,1.0996,1.0954,1.096,2155253.1000000001],[1637234400000,1.096,1.0962,1.0944,1.0946,1713920.6000000001],[1637234700000,1.0947,1.0984,1.0942,1.0973,1729530.2],[1637235000000,1.0973,1.0973,1.0919,1.0926,3354500.5],[1637235300000,1.0925,1.0933,1.0907,1.0914,2148017.6000000001],[1637235600000,1.0914,1.0946,1.0904,1.0909,3060508.7999999998],[1637235900000,1.0909,1.0914,1.0871,1.0888,3127402.1000000001],[1637236200000,1.0886,1.0926,1.0883,1.089,2654773.6000000001],[1637236500000,1.0889,1.089,1.0837,1.085,3756011.2000000002],[1637236800000,1.085,1.0884,1.0818,1.0876,3740646.8999999999],[1637237100000,1.0875,1.0881,1.0813,1.0873,5464295.0],[1637237400000,1.0874,1.0915,1.086,1.0914,2984681.8999999999],[1637237700000,1.0915,1.0955,1.0862,1.0941,4439409.5],[1637238000000,1.0941,1.0954,1.0892,1.0892,4380673.9000000004],[1637238300000,1.0892,1.0897,1.0864,1.0888,2976525.6000000001],[1637238600000,1.0888,1.0913,1.0867,1.0867,1701390.8999999999],[1637238900000,1.0867,1.0877,1.0842,1.0851,2283647.6000000001],[1637239200000,1.0851,1.0866,1.0838,1.0862,1721140.5],[1637239500000,1.0862,1.0898,1.0859,1.0896,1310288.6000000001],[1637239800000,1.0896,1.09,1.0804,1.0807,4536972.0999999996],[1637240100000,1.0807,1.0853,1.0801,1.0838,4762274.5],[1637240400000,1.0836,1.0848,1.0792,1.0804,5064635.0],[1637240700000,1.0804,1.0831,1.08,1.0813,1537403.2],[1637241000000,1.0812,1.0815,1.0747,1.077,5156479.2999999998],[1637241300000,1.0771,1.0797,1.07,1.0747,7183726.9000000004],[1637241600000,1.0747,1.0757,1.0707,1.0731,4569661.2000000002],[1637241900000,1.073,1.0735,1.066,1.0711,6827627.2000000002],[1637242200000,1.0712,1.0744,1.0668,1.0736,4523276.2000000002],[1637242500000,1.0735,1.088,1.0732,1.087,10117740.1999999993],[1637242800000,1.087,1.0877,1.0814,1.083,6775660.9000000004],[1637243100000,1.0829,1.0851,1.081,1.0821,2642695.7000000002],[1637243400000,1.0821,1.0824,1.075,1.0761,4880387.2000000002],[1637243700000,1.0761,1.0778,1.0743,1.0772,1534373.2],[1637244000000,1.0771,1.0772,1.0716,1.0745,4327706.2000000002],[1637244300000,1.0744,1.0813,1.074,1.0803,3020688.8999999999],[1637244600000,1.0803,1.0806,1.0731,1.0736,3849244.6000000001],[1637244900000,1.0735,1.0753,1.0702,1.0725,2651872.2000000002],[1637245200000,1.0725,1.0759,1.0704,1.0754,2410886.6000000001],[1637245500000,1.0754,1.0761,1.0719,1.0719,1779660.3999999999],[1637245800000,1.0719,1.078,1.0715,1.0778,2666691.0],[1637246100000,1.0778,1.0779,1.0727,1.0743,2677487.3999999999],[1637246400000,1.0744,1.0755,1.0691,1.0694,2987468.6000000001],[1637246700000,1.0694,1.0744,1.0678,1.0681,4545253.0999999996],[1637247000000,1.0681,1.0718,1.0673,1.0689,3032031.3999999999],[1637247300000,1.0688,1.0771,1.0668,1.0713,4745771.5],[1637247600000,1.0713,1.077,1.0704,1.0714,5149637.0],[1637247900000,1.0714,1.0737,1.069,1.0698,2278352.5],[1637248200000,1.0698,1.0698,1.059,1.0638,12208164.5999999996],[1637248500000,1.0637,1.0642,1.0562,1.0603,10020211.0999999996],[1637248800000,1.0603,1.0631,1.0507,1.052,17936300.8999999985],[1637249100000,1.0516,1.0667,1.045,1.0659,26789320.8000000007],[1637249400000,1.0659,1.0753,1.0643,1.0722,15230670.6999999993],[1637249700000,1.0721,1.0784,1.0719,1.0756,7623911.2999999998],[1637250000000,1.0756,1.0769,1.0651,1.0654,8434091.4000000004],[1637250300000,1.0652,1.0701,1.0603,1.0618,9904451.5],[1637250600000,1.0618,1.0639,1.0547,1.056,9672806.5],[1637250900000,1.0558,1.0589,1.051,1.0563,7416348.5999999996],[1637251200000,1.0564,1.0607,1.0503,1.0544,9694105.0],[1637251500000,1.0545,1.0562,1.0499,1.0551,8325909.5],[1637251800000,1.055,1.0635,1.054,1.0619,7034183.5999999996],[1637252100000,1.0618,1.0635,1.0535,1.0551,4967031.9000000004],[1637252400000,1.0551,1.0551,1.0428,1.0489,12212691.6999999993],[1637252700000,1.0493,1.0531,1.0405,1.0499,12684642.0999999996],[1637253000000,1.05,1.0524,1.043,1.0442,9199165.8000000007],[1637253300000,1.0442,1.0521,1.0419,1.0483,7289009.9000000004],[1637253600000,1.0483,1.056,1.0461,1.0495,6475553.9000000004],[1637253900000,1.0495,1.0502,1.0434,1.0464,6323164.9000000004],[1637254200000,1.0465,1.0522,1.044,1.0513,4960452.7999999998],[1637254500000,1.0513,1.0515,1.0394,1.0407,6911517.2000000002],[1637254800000,1.0407,1.0444,1.0296,1.0399,23939978.1000000015],[1637255100000,1.0398,1.0405,1.0256,1.026,11352941.5999999996],[1637255400000,1.026,1.03,1.0145,1.0222,30418058.1999999993],[1637255700000,1.022,1.0407,1.0219,1.0405,14897561.9000000004],[1637256000000,1.0405,1.048,1.0354,1.0464,9762881.9000000004],[1637256300000,1.0467,1.0479,1.0434,1.0462,7487226.4000000004],[1637256600000,1.0463,1.0514,1.0445,1.0475,6550172.2000000002],[1637256900000,1.0474,1.0503,1.0425,1.05,6146483.4000000004],[1637257200000,1.0501,1.051,1.046,1.0487,3916486.5],[1637257500000,1.0487,1.0534,1.0428,1.0447,7068007.0],[1637257800000,1.0447,1.0452,1.0408,1.0437,3858984.3999999999],[1637258100000,1.0436,1.0466,1.0388,1.0421,8796545.6999999993],[1637258400000,1.042,1.0457,1.0415,1.0445,3286390.3999999999],[1637258700000,1.0445,1.0484,1.0414,1.0474,3499227.2000000002],[1637259000000,1.0475,1.0491,1.0441,1.0449,2073718.3999999999],[1637259300000,1.0449,1.0484,1.0443,1.0448,1992172.8999999999],[1637259600000,1.0447,1.0532,1.043,1.0517,7335197.7000000002],[1637259900000,1.0513,1.0527,1.0477,1.0481,2694719.1000000001],[1637260200000,1.0481,1.0546,1.0464,1.0535,2788362.8999999999],[1637260500000,1.0533,1.0546,1.048,1.0496,3148787.3999999999],[1637260800000,1.0495,1.0495,1.0471,1.0483,1636203.7],[1637261100000,1.0484,1.052,1.0478,1.0503,2142292.8999999999],[1637261400000,1.0503,1.0521,1.0474,1.052,1521865.5],[1637261700000,1.0521,1.0533,1.0502,1.052,1615455.0],[1637262000000,1.052,1.0534,1.0504,1.0511,2500315.7000000002],[1637262300000,1.0512,1.0533,1.0492,1.0525,1731929.8999999999],[1637262600000,1.0525,1.0526,1.0509,1.0525,1288238.6000000001],[1637262900000,1.0526,1.0534,1.0511,1.0521,1981181.3],[1637263200000,1.0522,1.0549,1.0517,1.0544,2082135.0],[1637263500000,1.0544,1.0589,1.0531,1.0538,4354132.7000000002],[1637263800000,1.0538,1.0592,1.0534,1.0591,3016289.7000000002],[1637264100000,1.0592,1.0597,1.0567,1.0582,2208937.5],[1637264400000,1.0582,1.0584,1.055,1.0559,1592049.6000000001],[1637264700000,1.0559,1.0559,1.05,1.0546,4724945.0999999996],[1637265000000,1.0545,1.0564,1.0541,1.0549,1248205.2],[1637265300000,1.055,1.0561,1.0529,1.0555,1044990.1],[1637265600000,1.0556,1.0562,1.051,1.0511,1374822.2],[1637265900000,1.0512,1.0529,1.0486,1.0503,2055764.1000000001],[1637266200000,1.0502,1.0536,1.0486,1.0523,2311715.2000000002],[1637266500000,1.0524,1.053,1.048,1.0529,1536118.8],[1637266800000,1.0529,1.0548,1.0523,1.0547,1741091.2],[1637267100000,1.0547,1.0549,1.0503,1.051,1699248.0],[1637267400000,1.0511,1.0522,1.0497,1.0499,1616389.8999999999],[1637267700000,1.0499,1.0546,1.0497,1.0542,1268396.3],[1637268000000,1.0543,1.0556,1.0519,1.0544,1719555.8],[1637268300000,1.0543,1.0595,1.054,1.0589,2433730.2000000002],[1637268600000,1.0589,1.0596,1.0554,1.0579,2604349.6000000001],[1637268900000,1.0579,1.0586,1.0473,1.0486,5917066.2999999998],[1637269200000,1.0486,1.0555,1.0482,1.0549,3221514.0],[1637269500000,1.0549,1.0558,1.0517,1.0517,1468725.8999999999],[1637269800000,1.0517,1.0565,1.0517,1.0542,1610096.8999999999],[1637270100000,1.0543,1.0548,1.0518,1.0519,848440.7],[1637270400000,1.052,1.0533,1.0486,1.049,1920701.8],[1637270700000,1.049,1.0504,1.0486,1.0488,784432.5],[1637271000000,1.0488,1.0495,1.042,1.048,7191645.5],[1637271300000,1.048,1.0493,1.0455,1.0458,1702880.2],[1637271600000,1.0457,1.0474,1.0451,1.0454,1642083.3],[1637271900000,1.0454,1.0473,1.0434,1.0465,1665604.3],[1637272200000,1.0464,1.0477,1.0415,1.0445,2543931.0],[1637272500000,1.0445,1.0463,1.0425,1.0455,3287773.8999999999],[1637272800000,1.0454,1.0468,1.0436,1.0437,2387173.8999999999],[1637273100000,1.0438,1.0454,1.0404,1.0422,2824927.5],[1637273400000,1.0421,1.0471,1.0337,1.0468,8781184.3000000007],[1637273700000,1.0467,1.0486,1.0425,1.0457,3452063.3999999999],[1637274000000,1.0458,1.0466,1.0421,1.046,1607743.3999999999],[1637274300000,1.046,1.0481,1.0459,1.0474,1390902.8999999999],[1637274600000,1.0473,1.0526,1.0459,1.0525,1985031.0],[1637274900000,1.0526,1.054,1.0464,1.0466,2085753.3],[1637275200000,1.0466,1.0466,1.0414,1.0439,1734988.8999999999],[1637275500000,1.044,1.0473,1.039,1.0423,2637924.2000000002],[1637275800000,1.0423,1.045,1.0403,1.041,1501560.1000000001],[1637276100000,1.041,1.0458,1.0364,1.0393,3287501.5],[1637276400000,1.0394,1.0408,1.0354,1.037,3141329.3999999999],[1637276700000,1.037,1.0437,1.0365,1.041,2409815.2999999998],[1637277000000,1.041,1.0412,1.0346,1.0362,2711584.7000000002],[1637277300000,1.0362,1.0383,1.0327,1.0334,2474262.7999999998],[1637277600000,1.0334,1.043,1.0329,1.0419,3130971.7999999998],[1637277900000,1.0419,1.0439,1.04,1.0434,3426939.5],[1637278200000,1.0434,1.0464,1.0363,1.0371,3060759.8999999999],[1637278500000,1.0371,1.0412,1.0333,1.0411,2772434.5],[1637278800000,1.0411,1.0411,1.033,1.0358,2457532.7000000002],[1637279100000,1.0359,1.0388,1.0286,1.038,5787787.9000000004],[1637279400000,1.0379,1.04,1.033,1.0345,2665939.3999999999],[1637279700000,1.0345,1.0438,1.034,1.041,4038551.6000000001],[1637280000000,1.0411,1.0464,1.0314,1.0345,9002812.8000000007],[1637280300000,1.0346,1.0416,1.0295,1.0415,5701463.4000000004],[1637280600000,1.0415,1.0479,1.038,1.0438,8495110.0999999996],[1637280900000,1.0438,1.0482,1.0422,1.0446,3843031.8999999999],[1637281200000,1.0446,1.0446,1.0379,1.0394,5906549.0999999996],[1637281500000,1.0395,1.0433,1.0382,1.0429,1947493.5],[1637281800000,1.0429,1.0489,1.0396,1.0475,4989048.5],[1637282100000,1.0475,1.053,1.0475,1.0512,4821880.0999999996],[1637282400000,1.0511,1.0572,1.0483,1.0564,4416235.7000000002],[1637282700000,1.0565,1.0568,1.0505,1.0509,3597824.1000000001],[1637283000000,1.051,1.0546,1.0496,1.0524,2647578.2999999998],[1637283300000,1.0525,1.0563,1.0514,1.0551,3312801.0],[1637283600000,1.055,1.0557,1.0499,1.0517,3101542.5],[1637283900000,1.0517,1.0526,1.0499,1.0524,2397938.1000000001],[1637284200000,1.0524,1.0526,1.0495,1.0512,2091210.3999999999],[1637284500000,1.0512,1.052,1.0475,1.0486,2411746.1000000001],[1637284800000,1.0484,1.0496,1.046,1.0482,2792944.1000000001],[1637285100000,1.0482,1.0483,1.0442,1.0443,1568003.0],[1637285400000,1.0443,1.0463,1.0439,1.0449,1622501.3999999999],[1637285700000,1.0449,1.0464,1.0442,1.0461,1144991.3999999999],[1637286000000,1.0461,1.0477,1.04,1.0405,3626138.2000000002],[1637286300000,1.0404,1.0423,1.0385,1.0405,3132361.7000000002],[1637286600000,1.0402,1.0416,1.0365,1.037,2028380.8],[1637286900000,1.037,1.0401,1.0356,1.0359,2847903.7999999998],[1637287200000,1.0358,1.0392,1.0345,1.0386,3284689.1000000001],[1637287500000,1.0384,1.0389,1.0357,1.037,1779480.5],[1637287800000,1.0371,1.0432,1.036,1.0428,2659037.3999999999],[1637288100000,1.0428,1.0442,1.0407,1.043,1587892.0],[1637288400000,1.043,1.0459,1.0429,1.045,1169724.5],[1637288700000,1.045,1.0452,1.0381,1.0387,3473844.7000000002],[1637289000000,1.0387,1.0407,1.0378,1.0403,1250291.6000000001],[1637289300000,1.0402,1.0416,1.038,1.038,1183740.0],[1637289600000,1.0381,1.039,1.03,1.0306,4775244.7999999998],[1637289900000,1.0306,1.0332,1.0256,1.028,5871642.9000000004],[1637290200000,1.028,1.0282,1.0211,1.0219,8272004.7999999998],[1637290500000,1.0221,1.0272,1.0201,1.0248,8520749.6999999993],[1637290800000,1.0248,1.0274,1.0232,1.0265,5266811.5999999996],[1637291100000,1.0265,1.0275,1.0221,1.0268,3088035.5],[1637291400000,1.0269,1.0294,1.0259,1.028,2541298.6000000001],[1637291700000,1.028,1.0285,1.0236,1.0241,2174009.6000000001],[1637292000000,1.0242,1.026,1.0229,1.0229,1710564.1000000001],[1637292300000,1.0229,1.0253,1.0216,1.0238,2250729.0],[1637292600000,1.0238,1.0252,1.0202,1.0251,5006980.7999999998],[1637292900000,1.025,1.0271,1.0234,1.0252,2973786.5],[1637293200000,1.0252,1.0252,1.0179,1.0191,4405564.2999999998],[1637293500000,1.019,1.0228,1.019,1.0224,2165756.7999999998],[1637293800000,1.0225,1.0272,1.0219,1.027,3005237.2000000002],[1637294100000,1.0269,1.029,1.0251,1.0289,2614775.7000000002],[1637294400000,1.0289,1.0336,1.0261,1.033,5683874.0],[1637294700000,1.033,1.0339,1.0302,1.0316,2516339.0],[1637295000000,1.0316,1.0338,1.0302,1.0335,2668964.7000000002],[1637295300000,1.0333,1.0388,1.0327,1.037,2691412.1000000001],[1637295600000,1.0369,1.0386,1.0359,1.0373,1374237.5],[1637295900000,1.0372,1.0387,1.0365,1.038,1478740.8],[1637296200000,1.038,1.0404,1.0366,1.0394,1943930.3999999999],[1637296500000,1.0395,1.0399,1.0378,1.0398,805676.1],[1637296800000,1.0399,1.0413,1.0382,1.0382,3092379.3999999999],[1637297100000,1.0383,1.0412,1.0377,1.0401,2541816.3999999999],[1637297400000,1.04,1.0413,1.0392,1.0408,1096755.3999999999],[1637297700000,1.0407,1.0451,1.0407,1.0432,4498930.5999999996],[1637298000000,1.0433,1.0443,1.042,1.0421,1396137.1000000001],[1637298300000,1.042,1.0449,1.042,1.0435,1937069.1000000001],[1637298600000,1.0436,1.0446,1.0428,1.0437,1266308.0],[1637298900000,1.0437,1.0444,1.0399,1.0401,2101957.1000000001],[1637299200000,1.0401,1.0413,1.0376,1.0382,2169149.0],[1637299500000,1.0382,1.0388,1.0371,1.0376,1569698.2],[1637299800000,1.0377,1.0439,1.0375,1.0435,2813737.7000000002],[1637300100000,1.0435,1.0466,1.0409,1.042,4241173.2000000002],[1637300400000,1.0421,1.0445,1.0421,1.0437,2477144.1000000001],[1637300700000,1.0437,1.0458,1.0436,1.0444,1621875.8999999999],[1637301000000,1.0444,1.0459,1.0441,1.0458,1097824.8999999999],[1637301300000,1.0459,1.0469,1.0446,1.045,1755199.3],[1637301600000,1.0451,1.0461,1.043,1.0444,1810849.2],[1637301900000,1.0444,1.0498,1.0439,1.0483,2074098.8],[1637302200000,1.0484,1.0495,1.0461,1.0464,1457180.2],[1637302500000,1.0464,1.0479,1.046,1.0466,1174520.3999999999],[1637302800000,1.0467,1.0468,1.0428,1.043,1564176.3],[1637303100000,1.0431,1.0467,1.0429,1.0454,2964933.3999999999],[1637303400000,1.0454,1.0459,1.0438,1.0454,766589.1],[1637303700000,1.0454,1.0484,1.0453,1.0475,1310481.5],[1637304000000,1.0476,1.0476,1.0423,1.0427,2213511.5],[1637304300000,1.0427,1.0456,1.0425,1.0438,1353233.3],[1637304600000,1.0437,1.0443,1.0411,1.043,1957829.1000000001],[1637304900000,1.0431,1.0459,1.0426,1.0448,1671180.0],[1637305200000,1.0448,1.0488,1.0447,1.0467,2016082.3999999999],[1637305500000,1.0467,1.0468,1.0416,1.0416,1787466.0],[1637305800000,1.0416,1.0433,1.0404,1.0405,1569913.6000000001],[1637306100000,1.0405,1.0411,1.0382,1.0393,1937543.5],[1637306400000,1.0392,1.042,1.0392,1.0413,1031926.7],[1637306700000,1.0413,1.0414,1.0381,1.0404,1714814.1000000001],[1637307000000,1.0404,1.0426,1.0392,1.0393,1783571.5],[1637307300000,1.0393,1.0404,1.0384,1.0403,1364430.2],[1637307600000,1.0402,1.0431,1.0389,1.0426,1227451.1000000001],[1637307900000,1.0426,1.0443,1.0397,1.04,1626605.7],[1637308200000,1.0401,1.0421,1.0387,1.0416,1494791.8999999999],[1637308500000,1.0417,1.0429,1.039,1.0421,1559077.3999999999],[1637308800000,1.042,1.0474,1.0418,1.0424,4172338.0],[1637309100000,1.0423,1.0473,1.0423,1.0467,2169063.3999999999],[1637309400000,1.0468,1.0486,1.0466,1.0484,1915057.7],[1637309700000,1.0483,1.0568,1.0483,1.0563,6329233.0999999996],[1637310000000,1.0563,1.0661,1.0544,1.0631,11705595.4000000004],[1637310300000,1.0632,1.0637,1.0589,1.0626,5223845.4000000004],[1637310600000,1.0625,1.063,1.0582,1.0592,3978271.3999999999],[1637310900000,1.0592,1.0621,1.0582,1.0612,2589196.5],[1637311200000,1.0612,1.0619,1.0589,1.0597,1740018.7],[1637311500000,1.0597,1.0631,1.0572,1.0577,2488892.8999999999],[1637311800000,1.0578,1.059,1.0564,1.0569,1876773.3],[1637312100000,1.0568,1.058,1.0555,1.057,2283765.7999999998],[1637312400000,1.0569,1.0589,1.0562,1.0572,2233281.2999999998],[1637312700000,1.0573,1.0608,1.057,1.0596,2130616.0],[1637313000000,1.0597,1.062,1.0596,1.0615,1626153.3],[1637313300000,1.0615,1.0649,1.0607,1.0631,3424118.8999999999],[1637313600000,1.0631,1.0646,1.0612,1.0623,2235360.2000000002],[1637313900000,1.0623,1.0624,1.0594,1.0609,2156914.3999999999],[1637314200000,1.061,1.0621,1.0596,1.0599,1578938.8],[1637314500000,1.0598,1.0619,1.0597,1.0611,1440036.3],[1637314800000,1.061,1.0627,1.061,1.0625,1264797.3999999999],[1637315100000,1.0626,1.0627,1.0598,1.06,1445057.8999999999],[1637315400000,1.0601,1.0621,1.0594,1.0597,1705156.6000000001],[1637315700000,1.0597,1.0603,1.0569,1.0583,2514239.6000000001],[1637316000000,1.0581,1.0592,1.0554,1.0554,1798072.6000000001],[1637316300000,1.0554,1.0566,1.0547,1.055,1512323.8],[1637316600000,1.055,1.0552,1.0532,1.0534,1732325.8999999999],[1637316900000,1.0534,1.0554,1.0518,1.0535,2882577.7000000002],[1637317200000,1.0536,1.0567,1.0536,1.0566,1118497.6000000001],[1637317500000,1.0565,1.057,1.0558,1.0566,1016241.9],[1637317800000,1.0566,1.0569,1.0547,1.0548,1407670.7],[1637318100000,1.0547,1.0553,1.0524,1.0524,2160236.7000000002],[1637318400000,1.0524,1.0543,1.0494,1.0505,8216318.2000000002],[1637318700000,1.0506,1.0538,1.0505,1.0535,2185536.2000000002],[1637319000000,1.0535,1.0538,1.0519,1.053,973366.5],[1637319300000,1.0529,1.055,1.0515,1.0549,1076603.3999999999],[1637319600000,1.0549,1.058,1.0516,1.0526,3025784.3999999999],[1637319900000,1.0528,1.0543,1.0526,1.0532,858447.4],[1637320200000,1.0532,1.0535,1.0464,1.0471,2844673.6000000001],[1637320500000,1.0471,1.0499,1.047,1.0486,1777340.6000000001],[1637320800000,1.0486,1.0499,1.0456,1.0494,2930679.3999999999],[1637321100000,1.0495,1.0512,1.0485,1.0489,1468135.5],[1637321400000,1.0489,1.0514,1.0476,1.0482,2117825.8999999999],[1637321700000,1.0482,1.0506,1.0469,1.05,1484764.5],[1637322000000,1.0499,1.0549,1.0492,1.0526,2848893.7999999998],[1637322300000,1.0526,1.0526,1.0492,1.0502,1876539.3999999999],[1637322600000,1.0503,1.0516,1.0502,1.0512,1289668.8999999999],[1637322900000,1.0512,1.0544,1.0512,1.0538,1193687.5],[1637323200000,1.0538,1.0557,1.0519,1.054,2278614.5],[1637323500000,1.054,1.055,1.0517,1.0538,1639711.8],[1637323800000,1.0539,1.0588,1.0538,1.0583,2866716.7999999998],[1637324100000,1.0582,1.0603,1.0567,1.0599,2282008.2000000002],[1637324400000,1.0598,1.0599,1.0568,1.0581,1824810.7],[1637324700000,1.058,1.061,1.0573,1.0607,2480658.6000000001],[1637325000000,1.0607,1.0608,1.0574,1.0588,1958265.1000000001],[1637325300000,1.0589,1.0601,1.0579,1.0582,1227657.2],[1637325600000,1.0583,1.0596,1.0564,1.0574,1629522.8999999999],[1637325900000,1.0574,1.0584,1.055,1.0572,1654087.0],[1637326200000,1.0572,1.0573,1.0537,1.0544,2593881.6000000001],[1637326500000,1.0545,1.0556,1.0523,1.0528,1929621.1000000001],[1637326800000,1.0528,1.055,1.0495,1.0516,2965527.8999999999],[1637327100000,1.0517,1.0558,1.0514,1.0542,2262686.0],[1637327400000,1.0542,1.0574,1.0532,1.0573,1695916.1000000001],[1637327700000,1.0572,1.0598,1.0552,1.0586,2219687.1000000001],[1637328000000,1.0586,1.0605,1.0571,1.0601,2067080.6000000001],[1637328300000,1.0602,1.0619,1.0585,1.0598,3917029.8999999999],[1637328600000,1.0599,1.0619,1.0591,1.0607,2356014.2999999998],[1637328900000,1.0607,1.0641,1.0602,1.0641,2933633.6000000001],[1637329200000,1.0641,1.0649,1.0608,1.0622,3647307.0],[1637329500000,1.0622,1.0652,1.0622,1.0645,2560422.5],[1637329800000,1.0646,1.065,1.0625,1.0642,1783036.3],[1637330100000,1.0643,1.0654,1.063,1.0651,1559591.0],[1637330400000,1.0652,1.0679,1.0652,1.0669,3404484.0],[1637330700000,1.0669,1.0693,1.0668,1.0679,2853599.2000000002],[1637331000000,1.0679,1.0702,1.0671,1.0697,2969501.7999999998],[1637331300000,1.0696,1.0704,1.0665,1.0685,2572050.3999999999],[1637331600000,1.0685,1.086,1.0685,1.075,14928506.0],[1637331900000,1.0751,1.0775,1.0704,1.0724,5195830.5],[1637332200000,1.0724,1.0729,1.0672,1.0674,3428569.8999999999],[1637332500000,1.0675,1.0702,1.067,1.068,1760836.1000000001],[1637332800000,1.068,1.0744,1.068,1.0738,2232547.3999999999],[1637333100000,1.0739,1.077,1.0726,1.077,2434194.2000000002],[1637333400000,1.077,1.1028,1.0742,1.0919,24851780.8999999985],[1637333700000,1.0919,1.1034,1.0905,1.095,17220289.8999999985],[1637334000000,1.0949,1.0993,1.0886,1.0937,10645506.5],[1637334300000,1.0936,1.0936,1.0857,1.0884,4950515.2999999998],[1637334600000,1.0884,1.0894,1.0832,1.0849,4678329.9000000004],[1637334900000,1.0847,1.0871,1.0815,1.0868,4151758.8999999999],[1637335200000,1.0868,1.0986,1.0856,1.09,9628256.9000000004],[1637335500000,1.0899,1.0907,1.0858,1.0864,2832302.7999999998],[1637335800000,1.0864,1.0913,1.0854,1.0864,4114308.1000000001],[1637336100000,1.0863,1.0996,1.0863,1.0913,12850899.4000000004],[1637336400000,1.0914,1.0923,1.0889,1.0903,2702810.0],[1637336700000,1.0902,1.0905,1.0867,1.0896,2937454.6000000001],[1637337000000,1.0897,1.0933,1.089,1.0914,3057155.3999999999],[1637337300000,1.0913,1.0915,1.0882,1.0891,1900239.8999999999],[1637337600000,1.0891,1.0934,1.0885,1.0918,3386471.8999999999],[1637337900000,1.0918,1.0949,1.0892,1.0943,2656613.2999999998],[1637338200000,1.0943,1.0944,1.0903,1.0907,2280753.5],[1637338500000,1.0906,1.0928,1.0886,1.0896,2880637.2999999998],[1637338800000,1.0896,1.0902,1.0748,1.0809,15229325.0999999996],[1637339100000,1.0809,1.0825,1.0801,1.0815,3759337.2999999998],[1637339400000,1.0815,1.0859,1.0815,1.0841,3276425.1000000001],[1637339700000,1.0842,1.0879,1.0831,1.0866,2705449.7999999998],[1637340000000,1.0867,1.0879,1.0851,1.0867,1940085.5],[1637340300000,1.0867,1.0877,1.0843,1.0857,1990591.6000000001],[1637340600000,1.0857,1.0901,1.0856,1.0899,1749199.3999999999],[1637340900000,1.0899,1.0914,1.0896,1.0909,2214512.1000000001],[1637341200000,1.0909,1.099,1.0904,1.0954,4852518.7000000002],[1637341500000,1.0954,1.0972,1.0931,1.0962,2662937.7000000002],[1637341800000,1.0961,1.0985,1.0956,1.0963,2422019.5],[1637342100000,1.0963,1.098,1.0953,1.0954,1835799.8],[1637342400000,1.0953,1.0963,1.0917,1.0953,2891458.0],[1637342700000,1.0951,1.0984,1.0945,1.0974,2600052.3999999999],[1637343000000,1.0973,1.0975,1.092,1.0943,1826843.7],[1637343300000,1.0942,1.0971,1.093,1.0951,2493674.7000000002],[1637343600000,1.0951,1.0952,1.0889,1.0921,3589679.0],[1637343900000,1.092,1.0934,1.0902,1.0906,2088592.0],[1637344200000,1.0907,1.0909,1.089,1.0908,1497654.0],[1637344500000,1.0907,1.0935,1.0903,1.0908,1551172.0],[1637344800000,1.0908,1.0919,1.0891,1.0908,1371659.8999999999],[1637345100000,1.0909,1.0923,1.0901,1.0912,1099405.2],[1637345400000,1.0912,1.0952,1.0912,1.0937,1544849.7],[1637345700000,1.0937,1.0939,1.0906,1.091,1028408.1],[1637346000000,1.0911,1.0919,1.088,1.0887,1541774.1000000001],[1637346300000,1.0887,1.0888,1.0861,1.0875,1889732.3999999999],[1637346600000,1.0876,1.088,1.0855,1.0879,1551550.0],[1637346900000,1.0879,1.0891,1.0869,1.0876,1318291.0],[1637347200000,1.0875,1.0946,1.0859,1.0929,4563689.9000000004],[1637347500000,1.0929,1.0939,1.0898,1.0915,1896728.6000000001],[1637347800000,1.0916,1.0923,1.0907,1.092,868193.3],[1637348100000,1.092,1.0921,1.0895,1.0911,881530.6],[1637348400000,1.091,1.0929,1.0904,1.0926,1430232.3999999999],[1637348700000,1.0925,1.0928,1.0909,1.0918,1058528.3999999999],[1637349000000,1.0918,1.0944,1.0904,1.0941,1438929.5],[1637349300000,1.094,1.0958,1.0933,1.0951,1214216.0],[1637349600000,1.095,1.0951,1.093,1.0937,1072236.0],[1637349900000,1.0936,1.0952,1.0926,1.0926,853420.7],[1637350200000,1.0926,1.093,1.0885,1.0906,2109807.7999999998],[1637350500000,1.0907,1.0916,1.0894,1.0903,777329.7],[1637350800000,1.0905,1.091,1.0881,1.0894,1155590.5],[1637351100000,1.0894,1.0928,1.0893,1.0909,1574971.2],[1637351400000,1.091,1.0941,1.0909,1.0924,1363640.2],[1637351700000,1.0924,1.0952,1.0917,1.0936,1362357.0],[1637352000000,1.0937,1.0949,1.0905,1.0916,1437675.3],[1637352300000,1.0915,1.0949,1.0909,1.0943,1157343.2],[1637352600000,1.0943,1.0955,1.0916,1.092,1653159.8],[1637352900000,1.0921,1.0927,1.0905,1.0918,945154.5],[1637353200000,1.0918,1.0946,1.0913,1.0926,1311882.8999999999],[1637353500000,1.0925,1.0927,1.086,1.0871,4312836.2000000002],[1637353800000,1.087,1.0885,1.0858,1.0877,1550918.6000000001],[1637354100000,1.0878,1.0878,1.0838,1.0859,2308438.5],[1637354400000,1.0859,1.0888,1.0853,1.0882,1855770.0],[1637354700000,1.0883,1.09,1.0867,1.0893,1192972.0],[1637355000000,1.0893,1.0894,1.0855,1.0864,1148445.5],[1637355300000,1.0866,1.0887,1.0855,1.0857,1099877.3999999999],[1637355600000,1.0856,1.0866,1.0837,1.0844,2039745.1000000001],[1637355900000,1.0844,1.0859,1.0801,1.0817,2892642.1000000001],[1637356200000,1.0816,1.0842,1.0813,1.083,2101040.1000000001],[1637356500000,1.0831,1.0854,1.0831,1.0851,1038172.5],[1637356800000,1.0851,1.0858,1.0836,1.0839,1119516.6000000001],[1637357100000,1.0839,1.0843,1.0817,1.0831,1538972.8],[1637357400000,1.0832,1.0856,1.0828,1.0854,1554488.0],[1637357700000,1.0854,1.0859,1.0832,1.0837,785075.1],[1637358000000,1.0838,1.0865,1.0837,1.0851,906393.9],[1637358300000,1.0851,1.0877,1.0847,1.0864,1111648.0],[1637358600000,1.0865,1.0872,1.0853,1.0862,662570.1],[1637358900000,1.0863,1.087,1.0845,1.087,651297.2],[1637359200000,1.087,1.089,1.0861,1.0863,1561620.3999999999],[1637359500000,1.0862,1.0897,1.0861,1.0896,690657.4],[1637359800000,1.0897,1.0906,1.0891,1.0893,1022069.4],[1637360100000,1.0893,1.0894,1.0872,1.0878,1387351.2],[1637360400000,1.0877,1.0885,1.087,1.0884,862012.8],[1637360700000,1.0885,1.0895,1.0879,1.089,647205.2],[1637361000000,1.089,1.0895,1.0876,1.0878,1066368.8],[1637361300000,1.0878,1.0881,1.0867,1.0879,858945.2],[1637361600000,1.0879,1.0879,1.0834,1.0847,1858069.5],[1637361900000,1.0848,1.0865,1.0845,1.0861,936997.9],[1637362200000,1.0861,1.0862,1.0825,1.0844,1495208.8999999999],[1637362500000,1.0844,1.0858,1.0829,1.0854,1352253.8],[1637362800000,1.0854,1.0871,1.0823,1.0849,1622230.3999999999],[1637363100000,1.0848,1.086,1.0828,1.0856,2417358.7999999998],[1637363400000,1.0855,1.088,1.0853,1.0867,1630532.2],[1637363700000,1.0867,1.0877,1.0848,1.085,760880.6],[1637364000000,1.085,1.0901,1.0848,1.0897,2069419.7],[1637364300000,1.0897,1.0911,1.0879,1.0879,1488516.5],[1637364600000,1.0879,1.0884,1.0856,1.0863,1533068.2],[1637364900000,1.0864,1.0875,1.0855,1.0874,722345.4],[1637365200000,1.0874,1.0874,1.0852,1.0857,822524.9],[1637365500000,1.0856,1.0902,1.0856,1.0902,1403417.8],[1637365800000,1.0902,1.0924,1.0901,1.0923,1636862.3999999999],[1637366100000,1.0923,1.0927,1.0895,1.0903,1399924.0],[1637366400000,1.0903,1.0962,1.0901,1.0914,4490673.2000000002],[1637366700000,1.0912,1.0941,1.0882,1.0941,3047626.5],[1637367000000,1.094,1.0977,1.0923,1.0971,3678381.5],[1637367300000,1.0972,1.1,1.0952,1.097,3997438.1000000001],[1637367600000,1.097,1.0977,1.0945,1.0972,2438163.0],[1637367900000,1.0972,1.0975,1.0956,1.0964,1098586.6000000001],[1637368200000,1.0964,1.0974,1.0897,1.0905,2539457.2999999998],[1637368500000,1.0905,1.0914,1.0865,1.0872,2320597.2000000002],[1637368800000,1.0872,1.0923,1.0864,1.0914,1631094.3],[1637369100000,1.0916,1.092,1.0877,1.0919,4289263.4000000004],[1637369400000,1.0919,1.094,1.091,1.0939,1055489.3],[1637369700000,1.0938,1.094,1.0901,1.0911,1184568.6000000001],[1637370000000,1.0912,1.0939,1.0903,1.0932,1212137.7],[1637370300000,1.0936,1.0955,1.0928,1.0951,1211542.5],[1637370600000,1.0952,1.0967,1.0945,1.096,1305328.2],[1637370900000,1.096,1.0961,1.0935,1.0947,1120283.7],[1637371200000,1.0947,1.0983,1.0939,1.0978,2445861.6000000001],[1637371500000,1.0979,1.0988,1.0969,1.098,1417139.0],[1637371800000,1.098,1.099,1.0967,1.098,1077261.6000000001],[1637372100000,1.0981,1.0986,1.0972,1.0981,674376.4],[1637372400000,1.0981,1.1005,1.0979,1.0983,2364323.5],[1637372700000,1.0982,1.0983,1.0967,1.0978,1247239.3999999999],[1637373000000,1.0978,1.099,1.0973,1.0981,1277629.1000000001],[1637373300000,1.0981,1.0999,1.0975,1.0985,1346729.3],[1637373600000,1.0985,1.0986,1.0944,1.0958,2723454.3999999999],[1637373900000,1.0958,1.0964,1.0929,1.093,1749144.7],[1637374200000,1.0929,1.0949,1.0914,1.0947,2049263.7],[1637374500000,1.0946,1.0956,1.0933,1.095,1205850.3],[1637374800000,1.095,1.0955,1.0898,1.0913,4338975.2000000002],[1637375100000,1.0913,1.0918,1.0894,1.0909,1560380.0],[1637375400000,1.0908,1.0918,1.0904,1.0906,972871.6],[1637375700000,1.0907,1.0913,1.0898,1.09,818448.0],[1637376000000,1.0899,1.0908,1.0894,1.0902,929097.9],[1637376300000,1.0902,1.0906,1.0891,1.0901,708380.7],[1637376600000,1.0901,1.0906,1.0897,1.0898,736159.1],[1637376900000,1.0898,1.0911,1.0896,1.0898,575795.6],[1637377200000,1.0897,1.0918,1.0892,1.0892,1190381.5],[1637377500000,1.0894,1.0921,1.0891,1.0908,765414.3],[1637377800000,1.0908,1.0908,1.0871,1.0878,1903675.5],[1637378100000,1.0879,1.0899,1.0876,1.0892,855392.0],[1637378400000,1.0892,1.0892,1.0864,1.0881,1257836.3999999999],[1637378700000,1.0882,1.0889,1.088,1.0883,536165.5],[1637379000000,1.0884,1.0899,1.088,1.0884,615970.0],[1637379300000,1.0885,1.0885,1.0861,1.0869,1734457.8],[1637379600000,1.0868,1.0871,1.0836,1.0867,2512820.2000000002],[1637379900000,1.0866,1.0868,1.0854,1.0858,859900.4],[1637380200000,1.0859,1.0868,1.0858,1.086,923713.7],[1637380500000,1.086,1.0865,1.0844,1.0856,858783.1],[1637380800000,1.0856,1.089,1.085,1.0859,3112801.1000000001],[1637381100000,1.0859,1.0867,1.0842,1.0845,893190.8],[1637381400000,1.0845,1.086,1.0844,1.0857,1771293.6000000001],[1637381700000,1.0857,1.087,1.0848,1.087,657313.5],[1637382000000,1.087,1.0874,1.0862,1.0871,500026.1],[1637382300000,1.0872,1.0875,1.0854,1.0861,611553.3],[1637382600000,1.086,1.0873,1.0856,1.0864,590790.3],[1637382900000,1.0865,1.0889,1.0864,1.0888,736771.6],[1637383200000,1.0887,1.0892,1.0875,1.0882,564684.6],[1637383500000,1.0883,1.0889,1.0867,1.0885,811903.8],[1637383800000,1.0884,1.0906,1.088,1.0888,1212980.8999999999],[1637384100000,1.0887,1.0893,1.0878,1.0882,464356.2],[1637384400000,1.0881,1.0901,1.0881,1.0899,669739.7],[1637384700000,1.0899,1.0899,1.0872,1.0883,533913.3],[1637385000000,1.0883,1.0896,1.0868,1.0868,2708301.2999999998],[1637385300000,1.0869,1.088,1.0843,1.0845,1120246.0],[1637385600000,1.0844,1.0857,1.0821,1.0855,1427580.0],[1637385900000,1.0854,1.0866,1.0851,1.0863,389398.7],[1637386200000,1.0862,1.0882,1.0855,1.0879,1024285.7],[1637386500000,1.0879,1.0883,1.0875,1.0876,497392.0],[1637386800000,1.0876,1.0878,1.0861,1.0873,511895.0],[1637387100000,1.0873,1.0874,1.0851,1.0856,1142755.2],[1637387400000,1.0855,1.0873,1.0852,1.0863,901715.7],[1637387700000,1.0862,1.0867,1.0842,1.0863,692900.3],[1637388000000,1.0864,1.0871,1.0847,1.0852,561651.4],[1637388300000,1.0853,1.0861,1.0843,1.0856,676985.3],[1637388600000,1.0855,1.0862,1.0848,1.0861,742870.5],[1637388900000,1.0862,1.0875,1.0854,1.0867,841786.8],[1637389200000,1.0868,1.092,1.0867,1.0915,2294600.3999999999],[1637389500000,1.0914,1.0917,1.0883,1.0893,1009314.8],[1637389800000,1.0894,1.0926,1.0892,1.0917,1368490.8999999999],[1637390100000,1.0916,1.0916,1.0886,1.0894,1084968.3],[1637390400000,1.0895,1.0896,1.0879,1.088,932017.3],[1637390700000,1.088,1.0883,1.0865,1.0871,767340.1],[1637391000000,1.0871,1.0897,1.0859,1.0889,1704183.8],[1637391300000,1.089,1.0897,1.0883,1.0892,455040.6],[1637391600000,1.0893,1.0895,1.0882,1.0884,393659.6],[1637391900000,1.0885,1.0895,1.0881,1.0893,567088.8],[1637392200000,1.0893,1.091,1.0892,1.0902,717566.6],[1637392500000,1.0903,1.0903,1.0883,1.0889,774337.1],[1637392800000,1.0888,1.0889,1.087,1.0871,449758.3],[1637393100000,1.0871,1.088,1.0863,1.087,933884.1],[1637393400000,1.0871,1.0879,1.0866,1.0866,596192.9],[1637393700000,1.0866,1.0886,1.086,1.0883,1050754.8],[1637394000000,1.0883,1.0886,1.0874,1.0881,509677.1],[1637394300000,1.0881,1.0882,1.0853,1.0863,1169936.7],[1637394600000,1.0863,1.0864,1.0847,1.0849,578612.8],[1637394900000,1.0849,1.086,1.0846,1.0856,1144627.1000000001],[1637395200000,1.0857,1.0866,1.085,1.0854,989275.4],[1637395500000,1.0855,1.0863,1.0779,1.0808,10505885.3000000007],[1637395800000,1.0808,1.0839,1.0807,1.0821,3208775.7000000002],[1637396100000,1.0821,1.0831,1.0806,1.083,1995404.0],[1637396400000,1.083,1.0846,1.0829,1.0841,681952.6],[1637396700000,1.084,1.0855,1.084,1.0845,794261.4],[1637397000000,1.0845,1.0862,1.0842,1.0855,888880.8],[1637397300000,1.0854,1.0864,1.0834,1.0862,1091271.2],[1637397600000,1.0863,1.0893,1.0858,1.0877,1279769.0],[1637397900000,1.0876,1.0881,1.0855,1.086,793110.0],[1637398200000,1.0859,1.0874,1.0859,1.0867,485470.1],[1637398500000,1.0866,1.0867,1.0837,1.0838,887626.4],[1637398800000,1.0838,1.0863,1.0838,1.0862,1245719.2],[1637399100000,1.0862,1.0884,1.0859,1.0883,974988.0],[1637399400000,1.0884,1.0888,1.0877,1.0886,1003591.0],[1637399700000,1.0885,1.0886,1.0861,1.0864,992947.0],[1637400000000,1.0864,1.0881,1.0863,1.087,779589.5],[1637400300000,1.087,1.0874,1.0844,1.0847,1269541.6000000001],[1637400600000,1.0846,1.0849,1.0831,1.0835,915033.7],[1637400900000,1.0835,1.0865,1.0834,1.0857,1087950.8999999999],[1637401200000,1.0857,1.0877,1.0856,1.0873,537262.9],[1637401500000,1.0873,1.0877,1.0862,1.0877,701827.7],[1637401800000,1.0876,1.0877,1.086,1.0863,755964.2],[1637402100000,1.0863,1.088,1.086,1.0864,926433.9],[1637402400000,1.0863,1.0864,1.084,1.0856,1366293.6000000001],[1637402700000,1.0855,1.0866,1.0843,1.086,796402.5],[1637403000000,1.086,1.0871,1.086,1.0868,381267.2],[1637403300000,1.0868,1.0887,1.0865,1.0879,1003275.7],[1637403600000,1.0878,1.0894,1.0877,1.0884,1017682.3],[1637403900000,1.0884,1.0896,1.0874,1.0895,879450.2],[1637404200000,1.0895,1.0907,1.0866,1.0875,1907528.3],[1637404500000,1.0874,1.0876,1.0861,1.0873,839201.7],[1637404800000,1.0872,1.089,1.0871,1.0878,682662.9],[1637405100000,1.0878,1.0886,1.0867,1.0868,559829.6],[1637405400000,1.0868,1.0871,1.0852,1.0865,1215875.8999999999],[1637405700000,1.0864,1.0879,1.0864,1.0873,937912.4],[1637406000000,1.0874,1.0892,1.0874,1.0878,944114.4],[1637406300000,1.0878,1.0903,1.0878,1.0902,979827.1],[1637406600000,1.0903,1.0904,1.0892,1.0898,805613.7],[1637406900000,1.0898,1.0901,1.0868,1.0881,1123460.1000000001],[1637407200000,1.0881,1.0904,1.0879,1.09,888838.0],[1637407500000,1.09,1.0905,1.0885,1.0897,750959.0],[1637407800000,1.0897,1.0901,1.0888,1.089,886774.2],[1637408100000,1.089,1.0969,1.089,1.0966,5718003.2999999998],[1637408400000,1.0967,1.0986,1.0956,1.0982,2973321.8999999999],[1637408700000,1.0982,1.0988,1.0968,1.0974,2032980.7],[1637409000000,1.0974,1.0976,1.0929,1.0966,2923037.3999999999],[1637409300000,1.0966,1.098,1.0954,1.0971,1428928.1000000001],[1637409600000,1.0971,1.1024,1.0967,1.1005,3556082.2999999998],[1637409900000,1.1004,1.1009,1.0937,1.095,3633304.7000000002],[1637410200000,1.095,1.0986,1.095,1.0969,2066445.8],[1637410500000,1.0968,1.0979,1.0949,1.0966,2075631.8999999999],[1637410800000,1.0965,1.0969,1.094,1.0948,1400437.5],[1637411100000,1.0949,1.0953,1.094,1.0947,723203.3],[1637411400000,1.0947,1.0964,1.0926,1.0946,1518842.8999999999],[1637411700000,1.0945,1.0951,1.0936,1.0938,702839.0],[1637412000000,1.0938,1.0938,1.091,1.0922,1820219.7],[1637412300000,1.0922,1.0929,1.09,1.0926,1242401.1000000001],[1637412600000,1.0926,1.0937,1.092,1.0932,945032.3],[1637412900000,1.0931,1.0941,1.0929,1.093,783040.7],[1637413200000,1.0931,1.0943,1.0928,1.0933,865950.4],[1637413500000,1.0933,1.0954,1.0928,1.0952,972369.5],[1637413800000,1.0952,1.0957,1.0934,1.0936,986854.5],[1637414100000,1.0936,1.0938,1.091,1.0915,1628016.7],[1637414400000,1.0916,1.0918,1.0886,1.0903,2689959.2000000002],[1637414700000,1.0902,1.0919,1.0901,1.091,873543.9],[1637415000000,1.091,1.0911,1.0896,1.09,1140434.0],[1637415300000,1.09,1.0901,1.0874,1.0877,1993566.8],[1637415600000,1.0877,1.0905,1.0874,1.0904,1408563.1000000001],[1637415900000,1.0904,1.0906,1.0877,1.088,1280882.3999999999],[1637416200000,1.088,1.0881,1.0862,1.0879,1721945.6000000001],[1637416500000,1.0879,1.0891,1.0875,1.088,878254.6],[1637416800000,1.0881,1.0903,1.0881,1.0884,1015559.4],[1637417100000,1.0883,1.0901,1.0867,1.0896,1573042.3],[1637417400000,1.0896,1.0915,1.0892,1.0901,1649532.8999999999],[1637417700000,1.0902,1.0921,1.0901,1.0918,780278.3],[1637418000000,1.0918,1.0929,1.0899,1.0909,1178830.8],[1637418300000,1.091,1.091,1.0871,1.0874,1673685.5],[1637418600000,1.0873,1.0878,1.0856,1.0876,1594742.1000000001],[1637418900000,1.0875,1.0903,1.0875,1.0882,1185215.8],[1637419200000,1.0882,1.0889,1.0873,1.0882,901834.1],[1637419500000,1.0881,1.0908,1.0881,1.0905,1190476.0],[1637419800000,1.0904,1.0909,1.088,1.0885,1333521.1000000001],[1637420100000,1.0885,1.0889,1.0868,1.0875,1204363.2],[1637420400000,1.0874,1.0882,1.0774,1.0774,6897278.2000000002],[1637420700000,1.0774,1.0798,1.0666,1.0734,18737028.1000000015],[1637421000000,1.0734,1.0744,1.0649,1.0653,8081248.0999999996],[1637421300000,1.0653,1.0713,1.0634,1.0682,6168610.9000000004],[1637421600000,1.0683,1.0738,1.0682,1.0723,3102912.2999999998],[1637421900000,1.0722,1.0724,1.0685,1.0696,3068162.8999999999],[1637422200000,1.0697,1.0742,1.0689,1.0726,3058278.2000000002],[1637422500000,1.0726,1.0727,1.068,1.0681,2896892.3999999999],[1637422800000,1.0682,1.069,1.06,1.0648,9484606.5],[1637423100000,1.0648,1.0683,1.0627,1.0658,6391215.2000000002],[1637423400000,1.0657,1.0662,1.0626,1.0636,2885611.7000000002],[1637423700000,1.0636,1.0687,1.0633,1.0657,3151402.8999999999],[1637424000000,1.0656,1.0731,1.0638,1.0703,7003005.7999999998],[1637424300000,1.0703,1.0714,1.0679,1.0682,2512787.7000000002],[1637424600000,1.0681,1.0698,1.067,1.0675,2116040.2999999998],[1637424900000,1.0675,1.0703,1.0622,1.0636,3607023.0],[1637425200000,1.0637,1.067,1.0619,1.0668,3870316.7999999998],[1637425500000,1.0667,1.0702,1.0661,1.0695,2504189.2000000002],[1637425800000,1.0695,1.0747,1.0684,1.0725,3386024.2000000002],[1637426100000,1.0724,1.0724,1.0703,1.0714,1934522.6000000001],[1637426400000,1.0715,1.0734,1.0709,1.0727,1171828.0],[1637426700000,1.0726,1.0744,1.0714,1.0737,1507495.8999999999],[1637427000000,1.0736,1.0737,1.0711,1.0714,1028025.1],[1637427300000,1.0715,1.0741,1.0714,1.0731,1073486.7],[1637427600000,1.0731,1.0749,1.0726,1.073,1826134.2],[1637427900000,1.073,1.0742,1.0712,1.0738,918052.8],[1637428200000,1.0738,1.0738,1.0719,1.0722,988718.0],[1637428500000,1.0723,1.0731,1.0703,1.0721,1311910.8],[1637428800000,1.0721,1.0723,1.0694,1.0699,1087906.5],[1637429100000,1.07,1.0711,1.0687,1.071,1325384.2],[1637429400000,1.071,1.0723,1.0698,1.0707,1655250.3],[1637429700000,1.0708,1.0709,1.0681,1.0691,1158388.3999999999],[1637430000000,1.0692,1.0713,1.0683,1.0711,1145710.2],[1637430300000,1.071,1.0734,1.0704,1.0705,1603998.2],[1637430600000,1.0705,1.0736,1.0704,1.0731,1422780.1000000001],[1637430900000,1.073,1.0733,1.0717,1.0729,804557.5],[1637431200000,1.0729,1.0733,1.0701,1.0711,2708513.8999999999],[1637431500000,1.0712,1.0724,1.0685,1.0696,2337731.0],[1637431800000,1.0696,1.0715,1.069,1.0712,1218239.6000000001],[1637432100000,1.0712,1.078,1.0709,1.077,3115678.5],[1637432400000,1.0771,1.0901,1.0765,1.089,10331195.0999999996],[1637432700000,1.089,1.0947,1.0885,1.0938,7902295.5999999996],[1637433000000,1.0939,1.0958,1.0874,1.0882,5300217.7999999998],[1637433300000,1.0882,1.0917,1.0865,1.0902,3014248.7000000002],[1637433600000,1.0901,1.0956,1.0895,1.0956,4186690.7999999998],[1637433900000,1.0956,1.0981,1.0931,1.0948,5519539.0999999996],[1637434200000,1.0948,1.0974,1.0927,1.0966,3683840.6000000001],[1637434500000,1.0965,1.0969,1.0928,1.094,1721444.3999999999],[1637434800000,1.0941,1.0959,1.092,1.0933,3065797.7999999998],[1637435100000,1.0933,1.0952,1.093,1.0933,2150903.7000000002],[1637435400000,1.0934,1.0955,1.0929,1.094,2857204.1000000001],[1637435700000,1.094,1.0953,1.0926,1.0932,2070352.5],[1637436000000,1.0933,1.0948,1.0924,1.0925,1837116.3999999999],[1637436300000,1.0925,1.0928,1.0894,1.0921,2155167.2000000002],[1637436600000,1.0921,1.0929,1.0906,1.0925,1278498.3],[1637436900000,1.0924,1.0939,1.0919,1.093,1043067.9],[1637437200000,1.0931,1.0933,1.0914,1.0921,912194.9],[1637437500000,1.0921,1.0921,1.0897,1.0907,1311876.8999999999],[1637437800000,1.0907,1.0913,1.0896,1.0897,1085619.1000000001],[1637438100000,1.0898,1.0913,1.0895,1.0913,1220068.3999999999],[1637438400000,1.0912,1.093,1.0901,1.0902,2883167.7000000002],[1637438700000,1.0902,1.0922,1.0901,1.0916,1082536.0],[1637439000000,1.0917,1.0922,1.0897,1.0898,1207431.3999999999],[1637439300000,1.0897,1.0908,1.0892,1.0894,818445.8],[1637439600000,1.0893,1.0904,1.0884,1.0902,963286.2],[1637439900000,1.0903,1.0915,1.0893,1.0902,1062387.6000000001],[1637440200000,1.0903,1.0909,1.089,1.0895,818111.2],[1637440500000,1.0895,1.0906,1.0894,1.0904,423345.4],[1637440800000,1.0905,1.0908,1.0899,1.0903,589835.3],[1637441100000,1.0904,1.091,1.0891,1.0908,1313067.3],[1637441400000,1.0908,1.0918,1.0905,1.0908,627179.8],[1637441700000,1.0909,1.0932,1.0908,1.0926,1093904.2],[1637442000000,1.0926,1.0927,1.09,1.0902,1134073.1000000001],[1637442300000,1.0902,1.0902,1.0886,1.0899,967459.1],[1637442600000,1.0899,1.0922,1.0897,1.0919,1200459.6000000001],[1637442900000,1.0919,1.0925,1.0902,1.0903,577084.6],[1637443200000,1.0904,1.0916,1.0901,1.0908,516103.0],[1637443500000,1.0907,1.0924,1.0907,1.0915,1033097.5],[1637443800000,1.0916,1.0937,1.0911,1.0924,1445591.8999999999],[1637444100000,1.0924,1.0939,1.0917,1.0939,730517.6],[1637444400000,1.0939,1.0949,1.0928,1.0934,1642349.3],[1637444700000,1.0934,1.0946,1.0934,1.0939,586339.8],[1637445000000,1.0939,1.095,1.0927,1.0927,597216.7],[1637445300000,1.0927,1.0936,1.0922,1.0929,1007741.2],[1637445600000,1.0929,1.0946,1.0927,1.0935,868797.3],[1637445900000,1.0937,1.0938,1.0916,1.0926,802146.7],[1637446200000,1.0925,1.0934,1.0916,1.0919,857952.7],[1637446500000,1.0919,1.0939,1.0919,1.0935,438778.0],[1637446800000,1.0935,1.0938,1.0917,1.0935,1225414.7],[1637447100000,1.0936,1.0947,1.0935,1.0939,586802.1],[1637447400000,1.0939,1.0941,1.0936,1.0941,349812.6],[1637447700000,1.094,1.0948,1.0936,1.0945,664782.5],[1637448000000,1.0946,1.0963,1.0945,1.0958,1088906.8999999999],[1637448300000,1.0958,1.0966,1.0951,1.0953,876719.8],[1637448600000,1.0953,1.0954,1.0945,1.0948,509822.1],[1637448900000,1.0948,1.0969,1.0942,1.0961,1505118.8999999999],[1637449200000,1.096,1.097,1.0957,1.0965,1148066.1000000001],[1637449500000,1.0966,1.098,1.0948,1.0976,1567379.7],[1637449800000,1.0976,1.098,1.095,1.0956,998647.3],[1637450100000,1.0956,1.0969,1.0952,1.0953,750595.9],[1637450400000,1.0954,1.0956,1.0927,1.0935,1621045.8],[1637450700000,1.0935,1.0952,1.0928,1.0937,925914.1],[1637451000000,1.0937,1.0961,1.0937,1.0953,695201.5],[1637451300000,1.0954,1.0972,1.0953,1.0968,676331.4],[1637451600000,1.0969,1.0977,1.0961,1.097,770746.1],[1637451900000,1.0971,1.098,1.0962,1.0971,784673.1],[1637452200000,1.097,1.0987,1.0961,1.0978,1375433.6000000001],[1637452500000,1.0978,1.0978,1.0956,1.0976,1412024.6000000001],[1637452800000,1.0975,1.0988,1.0951,1.0962,1990264.8999999999],[1637453100000,1.0962,1.0969,1.0946,1.095,780066.8],[1637453400000,1.095,1.0977,1.0937,1.0975,1258294.2],[1637453700000,1.0975,1.0981,1.0924,1.0931,1243792.3],[1637454000000,1.093,1.0956,1.0922,1.094,1261851.6000000001],[1637454300000,1.094,1.0941,1.0915,1.0924,999709.6],[1637454600000,1.0924,1.0926,1.0906,1.0917,1183609.0],[1637454900000,1.0917,1.0926,1.0906,1.0925,997068.5],[1637455200000,1.0924,1.094,1.0923,1.0929,969486.3],[1637455500000,1.0929,1.0935,1.0911,1.0917,781625.8],[1637455800000,1.0917,1.0924,1.0903,1.0912,721801.4],[1637456100000,1.0912,1.0918,1.0901,1.0903,1098562.5],[1637456400000,1.0903,1.0935,1.0903,1.0924,1184347.7],[1637456700000,1.0924,1.0927,1.0911,1.0911,581214.6],[1637457000000,1.0911,1.0932,1.0911,1.0926,726532.9],[1637457300000,1.0925,1.093,1.0911,1.0911,719980.4],[1637457600000,1.0911,1.0918,1.0904,1.0915,453159.2],[1637457900000,1.0914,1.0918,1.0909,1.0909,370430.5],[1637458200000,1.091,1.0911,1.0886,1.09,2121856.8999999999],[1637458500000,1.09,1.0915,1.0898,1.0904,700277.1],[1637458800000,1.0903,1.0907,1.0891,1.0903,473211.3],[1637459100000,1.0903,1.0921,1.087,1.0874,1230450.3],[1637459400000,1.0875,1.0888,1.086,1.0875,1717497.0],[1637459700000,1.0875,1.0894,1.0873,1.0888,784000.5],[1637460000000,1.0888,1.0894,1.0875,1.0891,767362.7],[1637460300000,1.0891,1.0898,1.0882,1.0889,409101.5],[1637460600000,1.089,1.089,1.0863,1.0865,799687.8],[1637460900000,1.0865,1.0875,1.0864,1.0875,478476.7],[1637461200000,1.0874,1.0876,1.0848,1.0852,1712651.7],[1637461500000,1.0852,1.0864,1.0833,1.0835,1532113.7],[1637461800000,1.0835,1.085,1.0783,1.0805,5003637.0999999996],[1637462100000,1.0805,1.0815,1.0732,1.0741,5105530.0],[1637462400000,1.0741,1.0777,1.074,1.0766,2968626.1000000001],[1637462700000,1.0766,1.0789,1.0764,1.0787,1551175.6000000001],[1637463000000,1.0788,1.0791,1.0777,1.0783,1160593.5],[1637463300000,1.0783,1.0811,1.0763,1.0802,1934711.2],[1637463600000,1.0803,1.0809,1.0774,1.0787,1737260.7],[1637463900000,1.0787,1.0824,1.0786,1.0811,1311801.0],[1637464200000,1.081,1.0835,1.0808,1.0819,1937098.3],[1637464500000,1.0819,1.0819,1.0793,1.0804,1190680.3999999999],[1637464800000,1.0803,1.0817,1.0795,1.0795,1084963.5],[1637465100000,1.0795,1.0809,1.0783,1.0796,1058532.3999999999],[1637465400000,1.0796,1.0803,1.0787,1.0791,653388.2],[1637465700000,1.0792,1.0808,1.0786,1.0786,934467.7],[1637466000000,1.0786,1.079,1.0771,1.0773,922674.1],[1637466300000,1.0773,1.078,1.0765,1.0775,748529.9],[1637466600000,1.0775,1.0781,1.0743,1.0776,1872877.2],[1637466900000,1.0776,1.0779,1.0751,1.077,910629.8],[1637467200000,1.0769,1.0791,1.0751,1.0791,1420191.3],[1637467500000,1.0791,1.0798,1.0777,1.0792,1197974.1000000001],[1637467800000,1.0792,1.0819,1.0792,1.0802,1130051.8],[1637468100000,1.0802,1.0825,1.0794,1.0803,1201338.2],[1637468400000,1.0803,1.0814,1.0799,1.0803,480717.8],[1637468700000,1.0804,1.081,1.0788,1.0788,1022164.1],[1637469000000,1.0788,1.0803,1.0783,1.0801,758983.6],[1637469300000,1.08,1.08,1.0775,1.0786,825612.3],[1637469600000,1.0785,1.0788,1.0775,1.0786,511487.8],[1637469900000,1.0786,1.0787,1.0767,1.0775,987343.4],[1637470200000,1.0776,1.0777,1.0759,1.0767,718581.7],[1637470500000,1.0767,1.0773,1.0758,1.0767,547054.9],[1637470800000,1.0768,1.078,1.0758,1.0771,547032.8],[1637471100000,1.0772,1.079,1.0765,1.077,660939.3],[1637471400000,1.0769,1.0786,1.0766,1.078,856206.1],[1637471700000,1.078,1.0792,1.0776,1.0777,476814.3],[1637472000000,1.0777,1.0786,1.0777,1.0781,335511.9],[1637472300000,1.078,1.079,1.0777,1.0784,391906.0],[1637472600000,1.0785,1.0823,1.0782,1.0821,1189955.7],[1637472900000,1.082,1.0825,1.081,1.0821,876731.4],[1637473200000,1.0822,1.0832,1.0809,1.0823,696226.2],[1637473500000,1.0822,1.0838,1.0819,1.0832,853775.1],[1637473800000,1.0832,1.0843,1.0832,1.0841,696519.2],[1637474100000,1.0841,1.0857,1.0825,1.0853,1808459.5],[1637474400000,1.0853,1.0854,1.0835,1.0837,1168583.8],[1637474700000,1.0837,1.0839,1.0821,1.0832,783244.2],[1637475000000,1.0832,1.0833,1.0821,1.0828,452212.4],[1637475300000,1.0828,1.0838,1.0825,1.0833,301469.1],[1637475600000,1.0834,1.0835,1.0812,1.0814,464090.7],[1637475900000,1.0815,1.0815,1.0802,1.0805,767040.1],[1637476200000,1.0805,1.0843,1.0805,1.0838,883357.9],[1637476500000,1.0838,1.0839,1.0804,1.0808,579035.1],[1637476800000,1.0808,1.0819,1.0795,1.0806,797439.9],[1637477100000,1.0805,1.0825,1.0801,1.0823,825426.5],[1637477400000,1.0822,1.0825,1.081,1.0812,547369.3],[1637477700000,1.0813,1.085,1.0812,1.0847,1108606.1000000001],[1637478000000,1.0847,1.0903,1.0831,1.0831,4807307.0],[1637478300000,1.0831,1.0847,1.0828,1.0843,650309.2],[1637478600000,1.0843,1.0844,1.0829,1.0834,771278.2],[1637478900000,1.0835,1.0835,1.0811,1.0824,793377.3],[1637479200000,1.0824,1.084,1.0822,1.0828,513116.3],[1637479500000,1.0828,1.0842,1.0821,1.0827,919236.9],[1637479800000,1.0827,1.0847,1.0827,1.0843,549662.5],[1637480100000,1.0842,1.0845,1.0825,1.0829,707142.8],[1637480400000,1.0829,1.0839,1.0818,1.0831,522142.3],[1637480700000,1.0832,1.0837,1.0813,1.0813,552156.2],[1637481000000,1.0813,1.0821,1.081,1.0813,532391.4],[1637481300000,1.0812,1.0813,1.0795,1.0803,834204.0],[1637481600000,1.0804,1.0813,1.079,1.0793,978148.0],[1637481900000,1.0793,1.0797,1.0765,1.077,1781373.8999999999],[1637482200000,1.077,1.0782,1.0742,1.0778,2554242.6000000001],[1637482500000,1.0778,1.0781,1.0743,1.075,1286432.1000000001],[1637482800000,1.0749,1.0774,1.0742,1.0769,1188115.7],[1637483100000,1.0769,1.0776,1.0744,1.0758,1387133.6000000001],[1637483400000,1.0759,1.0776,1.0748,1.0767,1571797.6000000001],[1637483700000,1.0768,1.0796,1.0767,1.0787,1558711.2],[1637484000000,1.0787,1.079,1.077,1.0776,1283710.6000000001],[1637484300000,1.0776,1.0776,1.0741,1.0742,1409473.3999999999],[1637484600000,1.0741,1.0753,1.0705,1.071,2226765.7999999998],[1637484900000,1.071,1.0738,1.07,1.0723,2707738.7000000002],[1637485200000,1.0722,1.0743,1.0711,1.074,1182262.2],[1637485500000,1.0741,1.0745,1.0728,1.0736,788009.3],[1637485800000,1.0736,1.0761,1.0733,1.0749,1058760.1000000001],[1637486100000,1.0749,1.0759,1.0745,1.0752,1119925.5],[1637486400000,1.0751,1.0762,1.0742,1.0749,910704.1],[1637486700000,1.0749,1.0765,1.0737,1.0738,1493216.8999999999],[1637487000000,1.0738,1.0756,1.0729,1.0741,1005417.0],[1637487300000,1.0741,1.0755,1.0728,1.0753,829939.0],[1637487600000,1.0752,1.0765,1.074,1.0744,809630.2],[1637487900000,1.0744,1.0759,1.0738,1.0746,717923.0],[1637488200000,1.0746,1.0765,1.0745,1.0756,820078.8],[1637488500000,1.0756,1.0783,1.0747,1.0779,1040966.2],[1637488800000,1.0779,1.0786,1.0775,1.0778,1053125.2],[1637489100000,1.0779,1.0783,1.0769,1.0779,811140.6],[1637489400000,1.0778,1.0779,1.0758,1.0762,650018.8],[1637489700000,1.0761,1.0793,1.0758,1.0788,1671629.0],[1637490000000,1.0788,1.0788,1.0768,1.0772,866999.2],[1637490300000,1.0773,1.0789,1.0768,1.0768,732410.1],[1637490600000,1.0768,1.0771,1.0737,1.0751,1686388.0],[1637490900000,1.075,1.0755,1.074,1.074,631987.9],[1637491200000,1.074,1.0743,1.0701,1.0724,2901798.5],[1637491500000,1.0724,1.0754,1.0715,1.0747,1143065.3],[1637491800000,1.0746,1.0766,1.0744,1.076,1246851.5],[1637492100000,1.076,1.0762,1.0745,1.0756,800906.1],[1637492400000,1.0756,1.0809,1.0745,1.0783,4955742.4000000004],[1637492700000,1.0782,1.0809,1.0774,1.079,2248110.1000000001],[1637493000000,1.079,1.0792,1.0767,1.0769,1823132.5],[1637493300000,1.077,1.0771,1.0708,1.0715,3368298.1000000001],[1637493600000,1.0715,1.0716,1.0652,1.071,6452206.2999999998],[1637493900000,1.0709,1.0711,1.0678,1.0694,1547093.3],[1637494200000,1.0694,1.0719,1.0685,1.0713,1943760.0],[1637494500000,1.0712,1.073,1.0704,1.0714,995341.1],[1637494800000,1.0714,1.0715,1.069,1.0714,1160709.3],[1637495100000,1.0714,1.073,1.0709,1.0721,1085871.3999999999],[1637495400000,1.0721,1.0731,1.0716,1.0723,958377.4],[1637495700000,1.0724,1.0734,1.0722,1.0726,442414.5],[1637496000000,1.0727,1.0727,1.07,1.0713,1616334.3999999999],[1637496300000,1.0713,1.0721,1.0711,1.0721,590388.2],[1637496600000,1.0721,1.0765,1.0721,1.0759,2206257.2000000002],[1637496900000,1.0759,1.0776,1.0743,1.0759,1760184.6000000001],[1637497200000,1.0759,1.0767,1.0749,1.0753,997043.0],[1637497500000,1.0754,1.0763,1.0748,1.0759,945988.8],[1637497800000,1.0759,1.0771,1.0737,1.074,1312225.7],[1637498100000,1.0741,1.0745,1.0724,1.0733,896465.1],[1637498400000,1.0734,1.0734,1.0714,1.0731,1280526.7],[1637498700000,1.073,1.0752,1.0727,1.0729,1084723.7],[1637499000000,1.073,1.0755,1.0729,1.0752,948844.9],[1637499300000,1.0751,1.0752,1.0732,1.0736,761256.9],[1637499600000,1.0736,1.0745,1.0728,1.073,724334.7],[1637499900000,1.0729,1.074,1.0724,1.0725,410581.6],[1637500200000,1.0725,1.0738,1.0714,1.0723,825709.7],[1637500500000,1.0724,1.0741,1.0722,1.0731,541109.7],[1637500800000,1.0731,1.0741,1.0729,1.0736,602113.7],[1637501100000,1.0736,1.0738,1.0718,1.072,772796.4],[1637501400000,1.072,1.0728,1.0706,1.0707,639585.1],[1637501700000,1.0708,1.0712,1.0672,1.0699,3931510.2999999998],[1637502000000,1.0699,1.0701,1.0681,1.0694,846705.8],[1637502300000,1.0693,1.0715,1.0688,1.0702,1041782.8],[1637502600000,1.0702,1.071,1.0685,1.069,1044235.0],[1637502900000,1.069,1.0695,1.0663,1.0678,2842689.8999999999],[1637503200000,1.0678,1.0679,1.0638,1.0676,3331142.2000000002],[1637503500000,1.0677,1.0677,1.0658,1.0661,1509472.6000000001],[1637503800000,1.066,1.0694,1.066,1.0686,1594643.8999999999],[1637504100000,1.0686,1.0694,1.0671,1.0673,934328.6],[1637504400000,1.0673,1.0698,1.0673,1.0696,907421.8],[1637504700000,1.0697,1.0698,1.0678,1.0685,700061.7],[1637505000000,1.0685,1.0712,1.0669,1.0709,1578725.6000000001],[1637505300000,1.0709,1.0729,1.0705,1.0724,3502201.0],[1637505600000,1.0724,1.074,1.0716,1.0733,1390219.3],[1637505900000,1.0735,1.0745,1.0717,1.0722,1348645.0],[1637506200000,1.0721,1.0741,1.0711,1.0736,1267274.8999999999],[1637506500000,1.0736,1.0741,1.07,1.0701,2498196.3999999999],[1637506800000,1.07,1.0705,1.0685,1.0697,1205914.8999999999],[1637507100000,1.0696,1.0728,1.0696,1.0728,996615.6],[1637507400000,1.0728,1.0728,1.0704,1.0713,863754.8],[1637507700000,1.0713,1.0734,1.0709,1.0734,1053392.6000000001],[1637508000000,1.0734,1.074,1.0723,1.074,733229.2],[1637508300000,1.0739,1.0794,1.0739,1.079,3367813.3999999999],[1637508600000,1.079,1.0793,1.0768,1.0782,1781674.3999999999],[1637508900000,1.0783,1.0788,1.0775,1.0783,1179503.3999999999],[1637509200000,1.0783,1.0818,1.0782,1.08,2515741.7999999998],[1637509500000,1.0801,1.0805,1.0786,1.0795,1321868.2],[1637509800000,1.0795,1.0807,1.0791,1.0801,1287931.0],[1637510100000,1.0801,1.0804,1.0787,1.0788,1565686.1000000001],[1637510400000,1.0787,1.0814,1.0786,1.0805,1763473.7],[1637510700000,1.0806,1.0838,1.0805,1.0823,2995209.7999999998],[1637511000000,1.0824,1.0826,1.0807,1.0809,1521309.6000000001],[1637511300000,1.081,1.0812,1.0772,1.0793,1840075.8],[1637511600000,1.0792,1.0798,1.0765,1.0776,1690043.6000000001],[1637511900000,1.0775,1.078,1.0754,1.0755,1063176.8999999999],[1637512200000,1.0756,1.077,1.0754,1.0769,1187972.8],[1637512500000,1.0769,1.0769,1.0749,1.0762,1593978.7],[1637512800000,1.0763,1.079,1.0762,1.0786,1059333.8999999999],[1637513100000,1.0786,1.083,1.0784,1.0823,3005583.8999999999],[1637513400000,1.0823,1.0853,1.0811,1.0848,3877599.2999999998],[1637513700000,1.0849,1.0864,1.0839,1.0854,3246257.0],[1637514000000,1.0854,1.0867,1.0818,1.0824,5822200.0999999996],[1637514300000,1.0825,1.084,1.08,1.0817,3095709.1000000001],[1637514600000,1.0818,1.0839,1.0813,1.0828,1452589.8],[1637514900000,1.0829,1.0847,1.0824,1.084,973392.1],[1637515200000,1.0841,1.0855,1.0832,1.0842,1350230.5],[1637515500000,1.0843,1.0855,1.0839,1.0839,1418790.7],[1637515800000,1.084,1.0849,1.082,1.0831,1278012.6000000001],[1637516100000,1.083,1.0845,1.0809,1.0812,1221282.8999999999],[1637516400000,1.0811,1.0828,1.081,1.0814,946701.2],[1637516700000,1.0813,1.0816,1.0799,1.08,1185232.1000000001],[1637517000000,1.0801,1.0811,1.0797,1.0805,814544.5],[1637517300000,1.0804,1.081,1.0797,1.0802,743002.3],[1637517600000,1.0804,1.081,1.0783,1.0784,1149454.1000000001],[1637517900000,1.0783,1.0805,1.0771,1.0796,1361561.5],[1637518200000,1.0797,1.0807,1.0791,1.0806,835282.1],[1637518500000,1.0807,1.0809,1.0793,1.0799,712053.8],[1637518800000,1.0799,1.0808,1.0778,1.0779,664907.9],[1637519100000,1.0779,1.0792,1.0765,1.0779,1317777.8],[1637519400000,1.078,1.0784,1.077,1.0783,414788.8],[1637519700000,1.0783,1.0799,1.0783,1.0786,447598.1],[1637520000000,1.0786,1.0794,1.0785,1.0792,348289.5],[1637520300000,1.0791,1.081,1.0781,1.0785,1278199.5],[1637520600000,1.0786,1.0802,1.0785,1.0801,636711.5],[1637520900000,1.0801,1.0808,1.0789,1.0801,701731.7],[1637521200000,1.0801,1.0817,1.08,1.0813,845379.6],[1637521500000,1.0812,1.0815,1.0778,1.0778,887534.6],[1637521800000,1.0777,1.0792,1.0775,1.0776,598421.4],[1637522100000,1.0776,1.0786,1.0744,1.0748,2436814.7000000002],[1637522400000,1.0748,1.0755,1.0627,1.0673,11657709.5999999996],[1637522700000,1.0674,1.0714,1.0674,1.0711,2419596.8999999999],[1637523000000,1.0712,1.0724,1.0709,1.0711,1822021.5],[1637523300000,1.071,1.0717,1.0692,1.0705,1204982.3999999999],[1637523600000,1.0706,1.0709,1.0688,1.0701,947123.0],[1637523900000,1.0701,1.0722,1.0678,1.0681,1683728.5],[1637524200000,1.0682,1.0691,1.0661,1.0682,1654263.7],[1637524500000,1.0683,1.07,1.0675,1.068,814100.6],[1637524800000,1.0681,1.071,1.068,1.0697,1127817.3],[1637525100000,1.0696,1.0714,1.0691,1.0708,1297767.2],[1637525400000,1.0707,1.0711,1.0695,1.0699,880530.2],[1637525700000,1.07,1.071,1.0696,1.0706,639251.1],[1637526000000,1.0706,1.0722,1.07,1.0711,1272377.8999999999],[1637526300000,1.071,1.0715,1.07,1.0715,766472.4],[1637526600000,1.0714,1.0723,1.0704,1.0719,912265.7],[1637526900000,1.0719,1.0742,1.0719,1.0727,1148654.3999999999],[1637527200000,1.0728,1.0741,1.0726,1.0734,1033655.7],[1637527500000,1.0734,1.0739,1.0727,1.0735,580060.4],[1637527800000,1.0734,1.0764,1.0734,1.0759,956301.3],[1637528100000,1.0759,1.0774,1.0755,1.076,1718106.0],[1637528400000,1.0759,1.0772,1.0743,1.0746,1695191.3999999999],[1637528700000,1.0747,1.076,1.0744,1.0753,754169.2],[1637529000000,1.0753,1.0753,1.0728,1.074,1454014.0],[1637529300000,1.074,1.0761,1.0738,1.0754,789771.2],[1637529600000,1.0753,1.077,1.0751,1.0768,708803.5],[1637529900000,1.0767,1.0781,1.0761,1.0775,953198.1],[1637530200000,1.0775,1.0784,1.0765,1.0771,1015064.0],[1637530500000,1.077,1.0777,1.0744,1.0751,1236336.5],[1637530800000,1.0752,1.0776,1.0751,1.0773,596424.3],[1637531100000,1.0773,1.0773,1.0753,1.0755,883756.5],[1637531400000,1.0755,1.0763,1.0736,1.0739,1046835.3],[1637531700000,1.0739,1.0753,1.0738,1.0752,485188.2],[1637532000000,1.0751,1.0759,1.073,1.074,1138527.0],[1637532300000,1.0741,1.0744,1.0732,1.0735,749520.8],[1637532600000,1.0736,1.0756,1.0735,1.0752,763636.5],[1637532900000,1.0753,1.0762,1.0753,1.0758,369235.1],[1637533200000,1.0759,1.0762,1.0736,1.0742,820142.4],[1637533500000,1.0742,1.0745,1.0721,1.0734,876413.1],[1637533800000,1.0733,1.0737,1.0711,1.0713,765844.7]] \ No newline at end of file diff --git a/user_data/strategies/.gitkeep b/user_data/strategies/.gitkeep deleted file mode 100644 index e69de29bb..000000000