Merge branch 'freqtrade:develop' into develop
This commit is contained in:
commit
85ccba9b14
@ -11,12 +11,14 @@
|
|||||||
"mounts": [
|
"mounts": [
|
||||||
"source=freqtrade-bashhistory,target=/home/ftuser/commandhistory,type=volume"
|
"source=freqtrade-bashhistory,target=/home/ftuser/commandhistory,type=volume"
|
||||||
],
|
],
|
||||||
|
"workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/freqtrade,type=bind,consistency=cached",
|
||||||
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
|
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
|
||||||
"remoteUser": "ftuser",
|
"remoteUser": "ftuser",
|
||||||
|
|
||||||
|
"onCreateCommand": "pip install --user -e .",
|
||||||
"postCreateCommand": "freqtrade create-userdir --userdir user_data/",
|
"postCreateCommand": "freqtrade create-userdir --userdir user_data/",
|
||||||
|
|
||||||
"workspaceFolder": "/freqtrade/",
|
"workspaceFolder": "/workspaces/freqtrade",
|
||||||
|
|
||||||
"settings": {
|
"settings": {
|
||||||
"terminal.integrated.shell.linux": "/bin/bash",
|
"terminal.integrated.shell.linux": "/bin/bash",
|
||||||
|
67
.github/workflows/ci.yml
vendored
67
.github/workflows/ci.yml
vendored
@ -66,12 +66,6 @@ jobs:
|
|||||||
- name: Tests
|
- name: Tests
|
||||||
run: |
|
run: |
|
||||||
pytest --random-order --cov=freqtrade --cov-config=.coveragerc
|
pytest --random-order --cov=freqtrade --cov-config=.coveragerc
|
||||||
if: matrix.python-version != '3.9' || matrix.os != 'ubuntu-22.04'
|
|
||||||
|
|
||||||
- name: Tests incl. ccxt compatibility tests
|
|
||||||
run: |
|
|
||||||
pytest --random-order --cov=freqtrade --cov-config=.coveragerc --longrun
|
|
||||||
if: matrix.python-version == '3.9' && matrix.os == 'ubuntu-22.04'
|
|
||||||
|
|
||||||
- name: Coveralls
|
- name: Coveralls
|
||||||
if: (runner.os == 'Linux' && matrix.python-version == '3.10' && matrix.os == 'ubuntu-22.04')
|
if: (runner.os == 'Linux' && matrix.python-version == '3.10' && matrix.os == 'ubuntu-22.04')
|
||||||
@ -310,9 +304,64 @@ jobs:
|
|||||||
details: Freqtrade doc test failed!
|
details: Freqtrade doc test failed!
|
||||||
webhookUrl: ${{ secrets.DISCORD_WEBHOOK }}
|
webhookUrl: ${{ secrets.DISCORD_WEBHOOK }}
|
||||||
|
|
||||||
|
|
||||||
|
build_linux_online:
|
||||||
|
# Run pytest with "live" checks
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
# permissions:
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: "3.9"
|
||||||
|
|
||||||
|
- name: Cache_dependencies
|
||||||
|
uses: actions/cache@v3
|
||||||
|
id: cache
|
||||||
|
with:
|
||||||
|
path: ~/dependencies/
|
||||||
|
key: ${{ runner.os }}-dependencies
|
||||||
|
|
||||||
|
- name: pip cache (linux)
|
||||||
|
uses: actions/cache@v3
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
with:
|
||||||
|
path: ~/.cache/pip
|
||||||
|
key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip
|
||||||
|
|
||||||
|
- name: TA binary *nix
|
||||||
|
if: steps.cache.outputs.cache-hit != 'true'
|
||||||
|
run: |
|
||||||
|
cd build_helpers && ./install_ta-lib.sh ${HOME}/dependencies/; cd ..
|
||||||
|
|
||||||
|
- name: Installation - *nix
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip wheel
|
||||||
|
export LD_LIBRARY_PATH=${HOME}/dependencies/lib:$LD_LIBRARY_PATH
|
||||||
|
export TA_LIBRARY_PATH=${HOME}/dependencies/lib
|
||||||
|
export TA_INCLUDE_PATH=${HOME}/dependencies/include
|
||||||
|
pip install -r requirements-dev.txt
|
||||||
|
pip install -e .
|
||||||
|
|
||||||
|
- name: Tests incl. ccxt compatibility tests
|
||||||
|
run: |
|
||||||
|
pytest --random-order --cov=freqtrade --cov-config=.coveragerc --longrun
|
||||||
|
|
||||||
|
|
||||||
# Notify only once - when CI completes (and after deploy) in case it's successfull
|
# Notify only once - when CI completes (and after deploy) in case it's successfull
|
||||||
notify-complete:
|
notify-complete:
|
||||||
needs: [ build_linux, build_macos, build_windows, docs_check, mypy_version_check, pre-commit ]
|
needs: [
|
||||||
|
build_linux,
|
||||||
|
build_macos,
|
||||||
|
build_windows,
|
||||||
|
docs_check,
|
||||||
|
mypy_version_check,
|
||||||
|
pre-commit,
|
||||||
|
build_linux_online
|
||||||
|
]
|
||||||
runs-on: ubuntu-22.04
|
runs-on: ubuntu-22.04
|
||||||
# Discord notification can't handle schedule events
|
# Discord notification can't handle schedule events
|
||||||
if: (github.event_name != 'schedule')
|
if: (github.event_name != 'schedule')
|
||||||
@ -361,7 +410,7 @@ jobs:
|
|||||||
python setup.py sdist bdist_wheel
|
python setup.py sdist bdist_wheel
|
||||||
|
|
||||||
- name: Publish to PyPI (Test)
|
- name: Publish to PyPI (Test)
|
||||||
uses: pypa/gh-action-pypi-publish@v1.5.1
|
uses: pypa/gh-action-pypi-publish@v1.6.1
|
||||||
if: (github.event_name == 'release')
|
if: (github.event_name == 'release')
|
||||||
with:
|
with:
|
||||||
user: __token__
|
user: __token__
|
||||||
@ -369,7 +418,7 @@ jobs:
|
|||||||
repository_url: https://test.pypi.org/legacy/
|
repository_url: https://test.pypi.org/legacy/
|
||||||
|
|
||||||
- name: Publish to PyPI
|
- name: Publish to PyPI
|
||||||
uses: pypa/gh-action-pypi-publish@v1.5.1
|
uses: pypa/gh-action-pypi-publish@v1.6.1
|
||||||
if: (github.event_name == 'release')
|
if: (github.event_name == 'release')
|
||||||
with:
|
with:
|
||||||
user: __token__
|
user: __token__
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -109,7 +109,6 @@ target/
|
|||||||
!*.gitkeep
|
!*.gitkeep
|
||||||
!config_examples/config_binance.example.json
|
!config_examples/config_binance.example.json
|
||||||
!config_examples/config_bittrex.example.json
|
!config_examples/config_bittrex.example.json
|
||||||
!config_examples/config_ftx.example.json
|
|
||||||
!config_examples/config_full.example.json
|
!config_examples/config_full.example.json
|
||||||
!config_examples/config_kraken.example.json
|
!config_examples/config_kraken.example.json
|
||||||
!config_examples/config_freqai.example.json
|
!config_examples/config_freqai.example.json
|
||||||
|
@ -15,9 +15,9 @@ repos:
|
|||||||
additional_dependencies:
|
additional_dependencies:
|
||||||
- types-cachetools==5.2.1
|
- types-cachetools==5.2.1
|
||||||
- types-filelock==3.2.7
|
- types-filelock==3.2.7
|
||||||
- types-requests==2.28.11.2
|
- types-requests==2.28.11.5
|
||||||
- types-tabulate==0.9.0.0
|
- types-tabulate==0.9.0.0
|
||||||
- types-python-dateutil==2.8.19.2
|
- types-python-dateutil==2.8.19.4
|
||||||
# stages: [push]
|
# stages: [push]
|
||||||
|
|
||||||
- repo: https://github.com/pycqa/isort
|
- repo: https://github.com/pycqa/isort
|
||||||
|
@ -28,7 +28,6 @@ Please read the [exchange specific notes](docs/exchanges.md) to learn about even
|
|||||||
|
|
||||||
- [X] [Binance](https://www.binance.com/)
|
- [X] [Binance](https://www.binance.com/)
|
||||||
- [X] [Bittrex](https://bittrex.com/)
|
- [X] [Bittrex](https://bittrex.com/)
|
||||||
- [X] [FTX](https://ftx.com/#a=2258149)
|
|
||||||
- [X] [Gate.io](https://www.gate.io/ref/6266643)
|
- [X] [Gate.io](https://www.gate.io/ref/6266643)
|
||||||
- [X] [Huobi](http://huobi.com/)
|
- [X] [Huobi](http://huobi.com/)
|
||||||
- [X] [Kraken](https://kraken.com/)
|
- [X] [Kraken](https://kraken.com/)
|
||||||
@ -39,7 +38,7 @@ Please read the [exchange specific notes](docs/exchanges.md) to learn about even
|
|||||||
|
|
||||||
- [X] [Binance](https://www.binance.com/)
|
- [X] [Binance](https://www.binance.com/)
|
||||||
- [X] [Gate.io](https://www.gate.io/ref/6266643)
|
- [X] [Gate.io](https://www.gate.io/ref/6266643)
|
||||||
- [X] [OKX](https://okx.com/).
|
- [X] [OKX](https://okx.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.
|
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.
|
||||||
|
|
||||||
|
@ -7,11 +7,13 @@ export DOCKER_BUILDKIT=1
|
|||||||
TAG=$(echo "${BRANCH_NAME}" | sed -e "s/\//_/g")
|
TAG=$(echo "${BRANCH_NAME}" | sed -e "s/\//_/g")
|
||||||
TAG_PLOT=${TAG}_plot
|
TAG_PLOT=${TAG}_plot
|
||||||
TAG_FREQAI=${TAG}_freqai
|
TAG_FREQAI=${TAG}_freqai
|
||||||
|
TAG_FREQAI_RL=${TAG_FREQAI}rl
|
||||||
TAG_PI="${TAG}_pi"
|
TAG_PI="${TAG}_pi"
|
||||||
|
|
||||||
TAG_ARM=${TAG}_arm
|
TAG_ARM=${TAG}_arm
|
||||||
TAG_PLOT_ARM=${TAG_PLOT}_arm
|
TAG_PLOT_ARM=${TAG_PLOT}_arm
|
||||||
TAG_FREQAI_ARM=${TAG_FREQAI}_arm
|
TAG_FREQAI_ARM=${TAG_FREQAI}_arm
|
||||||
|
TAG_FREQAI_RL_ARM=${TAG_FREQAI_RL}_arm
|
||||||
CACHE_IMAGE=freqtradeorg/freqtrade_cache
|
CACHE_IMAGE=freqtradeorg/freqtrade_cache
|
||||||
|
|
||||||
echo "Running for ${TAG}"
|
echo "Running for ${TAG}"
|
||||||
@ -41,9 +43,11 @@ 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_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_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 .
|
||||||
|
|
||||||
docker tag freqtrade:$TAG_PLOT_ARM ${CACHE_IMAGE}:$TAG_PLOT_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_ARM ${CACHE_IMAGE}:$TAG_FREQAI_ARM
|
||||||
|
docker tag freqtrade:$TAG_FREQAI_RL_ARM ${CACHE_IMAGE}:$TAG_FREQAI_RL_ARM
|
||||||
|
|
||||||
# Run backtest
|
# Run backtest
|
||||||
docker run --rm -v $(pwd)/config_examples/config_bittrex.example.json:/freqtrade/config.json:ro -v $(pwd)/tests:/tests freqtrade:${TAG_ARM} backtesting --datadir /tests/testdata --strategy-path /tests/strategy/strats/ --strategy StrategyTestV3
|
docker run --rm -v $(pwd)/config_examples/config_bittrex.example.json:/freqtrade/config.json:ro -v $(pwd)/tests:/tests freqtrade:${TAG_ARM} backtesting --datadir /tests/testdata --strategy-path /tests/strategy/strats/ --strategy StrategyTestV3
|
||||||
@ -58,6 +62,7 @@ docker images
|
|||||||
# docker push ${IMAGE_NAME}
|
# docker push ${IMAGE_NAME}
|
||||||
docker push ${CACHE_IMAGE}:$TAG_PLOT_ARM
|
docker push ${CACHE_IMAGE}:$TAG_PLOT_ARM
|
||||||
docker push ${CACHE_IMAGE}:$TAG_FREQAI_ARM
|
docker push ${CACHE_IMAGE}:$TAG_FREQAI_ARM
|
||||||
|
docker push ${CACHE_IMAGE}:$TAG_FREQAI_RL_ARM
|
||||||
docker push ${CACHE_IMAGE}:$TAG_ARM
|
docker push ${CACHE_IMAGE}:$TAG_ARM
|
||||||
|
|
||||||
# Create multi-arch image
|
# Create multi-arch image
|
||||||
@ -74,6 +79,9 @@ 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_ARM} ${CACHE_IMAGE}:${TAG_FREQAI}
|
||||||
docker manifest push -p ${IMAGE_NAME}:${TAG_FREQAI}
|
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 push -p ${IMAGE_NAME}:${TAG_FREQAI_RL}
|
||||||
|
|
||||||
# Tag as latest for develop builds
|
# Tag as latest for develop builds
|
||||||
if [ "${TAG}" = "develop" ]; then
|
if [ "${TAG}" = "develop" ]; then
|
||||||
docker manifest create ${IMAGE_NAME}:latest ${CACHE_IMAGE}:${TAG_ARM} ${IMAGE_NAME}:${TAG_PI} ${CACHE_IMAGE}:${TAG}
|
docker manifest create ${IMAGE_NAME}:latest ${CACHE_IMAGE}:${TAG_ARM} ${IMAGE_NAME}:${TAG_PI} ${CACHE_IMAGE}:${TAG}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
TAG=$(echo "${BRANCH_NAME}" | sed -e "s/\//_/g")
|
TAG=$(echo "${BRANCH_NAME}" | sed -e "s/\//_/g")
|
||||||
TAG_PLOT=${TAG}_plot
|
TAG_PLOT=${TAG}_plot
|
||||||
TAG_FREQAI=${TAG}_freqai
|
TAG_FREQAI=${TAG}_freqai
|
||||||
|
TAG_FREQAI_RL=${TAG_FREQAI}rl
|
||||||
TAG_PI="${TAG}_pi"
|
TAG_PI="${TAG}_pi"
|
||||||
|
|
||||||
PI_PLATFORM="linux/arm/v7"
|
PI_PLATFORM="linux/arm/v7"
|
||||||
@ -51,9 +52,11 @@ docker tag freqtrade:$TAG ${CACHE_IMAGE}:$TAG
|
|||||||
|
|
||||||
docker build --cache-from freqtrade:${TAG} --build-arg sourceimage=${CACHE_IMAGE} --build-arg sourcetag=${TAG} -t freqtrade:${TAG_PLOT} -f docker/Dockerfile.plot .
|
docker build --cache-from freqtrade:${TAG} --build-arg sourceimage=${CACHE_IMAGE} --build-arg sourcetag=${TAG} -t freqtrade:${TAG_PLOT} -f docker/Dockerfile.plot .
|
||||||
docker build --cache-from freqtrade:${TAG} --build-arg sourceimage=${CACHE_IMAGE} --build-arg sourcetag=${TAG} -t freqtrade:${TAG_FREQAI} -f docker/Dockerfile.freqai .
|
docker build --cache-from freqtrade:${TAG} --build-arg sourceimage=${CACHE_IMAGE} --build-arg sourcetag=${TAG} -t freqtrade:${TAG_FREQAI} -f docker/Dockerfile.freqai .
|
||||||
|
docker build --cache-from freqtrade:${TAG_FREQAI} --build-arg sourceimage=${CACHE_IMAGE} --build-arg sourcetag=${TAG_FREQAI} -t freqtrade:${TAG_FREQAI_RL} -f docker/Dockerfile.freqai_rl .
|
||||||
|
|
||||||
docker tag freqtrade:$TAG_PLOT ${CACHE_IMAGE}:$TAG_PLOT
|
docker tag freqtrade:$TAG_PLOT ${CACHE_IMAGE}:$TAG_PLOT
|
||||||
docker tag freqtrade:$TAG_FREQAI ${CACHE_IMAGE}:$TAG_FREQAI
|
docker tag freqtrade:$TAG_FREQAI ${CACHE_IMAGE}:$TAG_FREQAI
|
||||||
|
docker tag freqtrade:$TAG_FREQAI_RL ${CACHE_IMAGE}:$TAG_FREQAI_RL
|
||||||
|
|
||||||
# Run backtest
|
# Run backtest
|
||||||
docker run --rm -v $(pwd)/config_examples/config_bittrex.example.json:/freqtrade/config.json:ro -v $(pwd)/tests:/tests freqtrade:${TAG} backtesting --datadir /tests/testdata --strategy-path /tests/strategy/strats/ --strategy StrategyTestV3
|
docker run --rm -v $(pwd)/config_examples/config_bittrex.example.json:/freqtrade/config.json:ro -v $(pwd)/tests:/tests freqtrade:${TAG} backtesting --datadir /tests/testdata --strategy-path /tests/strategy/strats/ --strategy StrategyTestV3
|
||||||
@ -68,6 +71,7 @@ docker images
|
|||||||
docker push ${CACHE_IMAGE}
|
docker push ${CACHE_IMAGE}
|
||||||
docker push ${CACHE_IMAGE}:$TAG_PLOT
|
docker push ${CACHE_IMAGE}:$TAG_PLOT
|
||||||
docker push ${CACHE_IMAGE}:$TAG_FREQAI
|
docker push ${CACHE_IMAGE}:$TAG_FREQAI
|
||||||
|
docker push ${CACHE_IMAGE}:$TAG_FREQAI_RL
|
||||||
docker push ${CACHE_IMAGE}:$TAG
|
docker push ${CACHE_IMAGE}:$TAG
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,96 +0,0 @@
|
|||||||
{
|
|
||||||
"max_open_trades": 3,
|
|
||||||
"stake_currency": "USD",
|
|
||||||
"stake_amount": 50,
|
|
||||||
"tradable_balance_ratio": 0.99,
|
|
||||||
"fiat_display_currency": "USD",
|
|
||||||
"timeframe": "5m",
|
|
||||||
"dry_run": true,
|
|
||||||
"cancel_open_orders_on_exit": false,
|
|
||||||
"unfilledtimeout": {
|
|
||||||
"entry": 10,
|
|
||||||
"exit": 10,
|
|
||||||
"exit_timeout_count": 0,
|
|
||||||
"unit": "minutes"
|
|
||||||
},
|
|
||||||
"entry_pricing": {
|
|
||||||
"price_side": "same",
|
|
||||||
"use_order_book": true,
|
|
||||||
"order_book_top": 1,
|
|
||||||
"price_last_balance": 0.0,
|
|
||||||
"check_depth_of_market": {
|
|
||||||
"enabled": false,
|
|
||||||
"bids_to_ask_delta": 1
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"exit_pricing": {
|
|
||||||
"price_side": "same",
|
|
||||||
"use_order_book": true,
|
|
||||||
"order_book_top": 1
|
|
||||||
},
|
|
||||||
"exchange": {
|
|
||||||
"name": "ftx",
|
|
||||||
"key": "your_exchange_key",
|
|
||||||
"secret": "your_exchange_secret",
|
|
||||||
"ccxt_config": {},
|
|
||||||
"ccxt_async_config": {},
|
|
||||||
"pair_whitelist": [
|
|
||||||
"BTC/USD",
|
|
||||||
"ETH/USD",
|
|
||||||
"BNB/USD",
|
|
||||||
"USDT/USD",
|
|
||||||
"LTC/USD",
|
|
||||||
"SRM/USD",
|
|
||||||
"SXP/USD",
|
|
||||||
"XRP/USD",
|
|
||||||
"DOGE/USD",
|
|
||||||
"1INCH/USD",
|
|
||||||
"CHZ/USD",
|
|
||||||
"MATIC/USD",
|
|
||||||
"LINK/USD",
|
|
||||||
"OXY/USD",
|
|
||||||
"SUSHI/USD"
|
|
||||||
],
|
|
||||||
"pair_blacklist": [
|
|
||||||
"FTT/USD"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"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",
|
|
||||||
"chat_id": "your_telegram_chat_id"
|
|
||||||
},
|
|
||||||
"api_server": {
|
|
||||||
"enabled": false,
|
|
||||||
"listen_ip_address": "127.0.0.1",
|
|
||||||
"listen_port": 8080,
|
|
||||||
"verbosity": "error",
|
|
||||||
"jwt_secret_key": "somethingrandom",
|
|
||||||
"CORS_origins": [],
|
|
||||||
"username": "freqtrader",
|
|
||||||
"password": "SuperSecurePassword"
|
|
||||||
},
|
|
||||||
"bot_name": "freqtrade",
|
|
||||||
"initial_state": "running",
|
|
||||||
"force_entry_enable": false,
|
|
||||||
"internals": {
|
|
||||||
"process_throttle_secs": 5
|
|
||||||
}
|
|
||||||
}
|
|
@ -204,6 +204,7 @@
|
|||||||
"strategy_path": "user_data/strategies/",
|
"strategy_path": "user_data/strategies/",
|
||||||
"recursive_strategy_search": false,
|
"recursive_strategy_search": false,
|
||||||
"add_config_files": [],
|
"add_config_files": [],
|
||||||
|
"reduce_df_footprint": false,
|
||||||
"dataformat_ohlcv": "json",
|
"dataformat_ohlcv": "json",
|
||||||
"dataformat_trades": "jsongz"
|
"dataformat_trades": "jsongz"
|
||||||
}
|
}
|
||||||
|
8
docker/Dockerfile.freqai_rl
Normal file
8
docker/Dockerfile.freqai_rl
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
ARG sourceimage=freqtradeorg/freqtrade
|
||||||
|
ARG sourcetag=develop_freqai
|
||||||
|
FROM ${sourceimage}:${sourcetag}
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
COPY requirements-freqai.txt requirements-freqai-rl.txt /freqtrade/
|
||||||
|
|
||||||
|
RUN pip install -r requirements-freqai-rl.txt --user --no-cache-dir
|
@ -100,3 +100,17 @@ freqtrade backtesting-analysis -c <config.json> --analysis-groups 0 2 --enter-re
|
|||||||
The indicators have to be present in your strategy's main DataFrame (either for your main
|
The indicators have to be present in your strategy's main DataFrame (either for your main
|
||||||
timeframe or for informative timeframes) otherwise they will simply be ignored in the script
|
timeframe or for informative timeframes) otherwise they will simply be ignored in the script
|
||||||
output.
|
output.
|
||||||
|
|
||||||
|
### Filtering the trade output by date
|
||||||
|
|
||||||
|
To show only trades between dates within your backtested timerange, supply the usual `timerange` option in `YYYYMMDD-[YYYYMMDD]` format:
|
||||||
|
|
||||||
|
```
|
||||||
|
--timerange : Timerange to filter output trades, start date inclusive, end date exclusive. e.g. 20220101-20221231
|
||||||
|
```
|
||||||
|
|
||||||
|
For example, if your backtest timerange was `20220101-20221231` but you only want to output trades in January:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
freqtrade backtesting-analysis -c <config.json> --timerange 20220101-20220201
|
||||||
|
```
|
||||||
|
@ -583,7 +583,8 @@ To utilize this, you can append `--timeframe-detail 5m` to your regular backtest
|
|||||||
freqtrade backtesting --strategy AwesomeStrategy --timeframe 1h --timeframe-detail 5m
|
freqtrade backtesting --strategy AwesomeStrategy --timeframe 1h --timeframe-detail 5m
|
||||||
```
|
```
|
||||||
|
|
||||||
This will load 1h data as well as 5m data for the timeframe. The strategy will be analyzed with the 1h timeframe - and for every "open trade candle" (candles where a trade is open) the 5m data will be used to simulate intra-candle movements.
|
This will load 1h data as well as 5m data for the timeframe. The strategy will be analyzed with the 1h timeframe, and Entry orders will only be placed at the main timeframe, however Order fills and exit signals will be evaluated at the 5m candle, simulating intra-candle movements.
|
||||||
|
|
||||||
All callback functions (`custom_exit()`, `custom_stoploss()`, ... ) will be running for each 5m candle once the trade is opened (so 12 times in the above example of 1h timeframe, and 5m detailed timeframe).
|
All callback functions (`custom_exit()`, `custom_stoploss()`, ... ) will be running for each 5m candle once the trade is opened (so 12 times in the above example of 1h timeframe, and 5m detailed timeframe).
|
||||||
|
|
||||||
`--timeframe-detail` must be smaller than the original timeframe, otherwise backtesting will fail to start.
|
`--timeframe-detail` must be smaller than the original timeframe, otherwise backtesting will fail to start.
|
||||||
|
@ -253,6 +253,7 @@ Mandatory parameters are marked as **Required**, which means that they are requi
|
|||||||
| `add_config_files` | Additional config files. These files will be loaded and merged with the current config file. The files are resolved relative to the initial file.<br> *Defaults to `[]`*. <br> **Datatype:** List of strings
|
| `add_config_files` | Additional config files. These files will be loaded and merged with the current config file. The files are resolved relative to the initial file.<br> *Defaults to `[]`*. <br> **Datatype:** List of strings
|
||||||
| `dataformat_ohlcv` | Data format to use to store historical candle (OHLCV) data. <br> *Defaults to `json`*. <br> **Datatype:** String
|
| `dataformat_ohlcv` | Data format to use to store historical candle (OHLCV) data. <br> *Defaults to `json`*. <br> **Datatype:** String
|
||||||
| `dataformat_trades` | Data format to use to store historical trades data. <br> *Defaults to `jsongz`*. <br> **Datatype:** String
|
| `dataformat_trades` | Data format to use to store historical trades data. <br> *Defaults to `jsongz`*. <br> **Datatype:** String
|
||||||
|
| `reduce_df_footprint` | Recast all numeric columns to float32/int32, with the objective of reducing ram/disk usage (and decreasing train/inference timing in FreqAI). (Currently only affects FreqAI use-cases) <br> **Datatype:** Boolean. <br> Default: `False`.
|
||||||
|
|
||||||
### Parameters in the strategy
|
### Parameters in the strategy
|
||||||
|
|
||||||
@ -552,7 +553,7 @@ The possible values are: `GTC` (default), `FOK` or `IOC`.
|
|||||||
```
|
```
|
||||||
|
|
||||||
!!! Warning
|
!!! Warning
|
||||||
This is ongoing work. For now, it is supported only for binance, gate, ftx and kucoin.
|
This is ongoing work. For now, it is supported only for binance, gate and kucoin.
|
||||||
Please don't change the default value unless you know what you are doing and have researched the impact of using different values for your particular exchange.
|
Please don't change the default value unless you know what you are doing and have researched the impact of using different values for your particular exchange.
|
||||||
|
|
||||||
### What values can be used for fiat_display_currency?
|
### What values can be used for fiat_display_currency?
|
||||||
@ -664,6 +665,7 @@ You should also make sure to read the [Exchanges](exchanges.md) section of the d
|
|||||||
### Using proxy with Freqtrade
|
### 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.
|
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.
|
||||||
|
|
||||||
``` bash
|
``` bash
|
||||||
export HTTP_PROXY="http://addr:port"
|
export HTTP_PROXY="http://addr:port"
|
||||||
@ -671,11 +673,13 @@ export HTTPS_PROXY="http://addr:port"
|
|||||||
freqtrade
|
freqtrade
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Proxy just exchange requests
|
#### Proxy exchange requests
|
||||||
|
|
||||||
To use a proxy just for exchange connections (skips/ignores telegram and coingecko) - you can also define the proxies as part of the ccxt configuration.
|
To use a proxy for exchange connections - you will have to define the proxies as part of the ccxt configuration.
|
||||||
|
|
||||||
``` json
|
``` json
|
||||||
|
{
|
||||||
|
"exchange": {
|
||||||
"ccxt_config": {
|
"ccxt_config": {
|
||||||
"aiohttp_proxy": "http://addr:port",
|
"aiohttp_proxy": "http://addr:port",
|
||||||
"proxies": {
|
"proxies": {
|
||||||
@ -683,6 +687,7 @@ To use a proxy just for exchange connections (skips/ignores telegram and coingec
|
|||||||
"https": "http://addr:port"
|
"https": "http://addr:port"
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Next step
|
## Next step
|
||||||
|
@ -83,7 +83,7 @@ from pathlib import Path
|
|||||||
project_root = "somedir/freqtrade"
|
project_root = "somedir/freqtrade"
|
||||||
i=0
|
i=0
|
||||||
try:
|
try:
|
||||||
os.chdirdir(project_root)
|
os.chdir(project_root)
|
||||||
assert Path('LICENSE').is_file()
|
assert Path('LICENSE').is_file()
|
||||||
except:
|
except:
|
||||||
while i<4 and (not Path('LICENSE').is_file()):
|
while i<4 and (not Path('LICENSE').is_file()):
|
||||||
|
@ -177,13 +177,13 @@ freqtrade download-data --exchange binance --pairs ETH/USDT XRP/USDT BTC/USDT --
|
|||||||
|
|
||||||
### Data format
|
### Data format
|
||||||
|
|
||||||
Freqtrade currently supports 3 data-formats for both OHLCV and trades data:
|
Freqtrade currently supports the following data-formats:
|
||||||
|
|
||||||
* `json` - plain "text" json files
|
* `json` - plain "text" json files
|
||||||
* `jsongz` - a gzip-zipped version of json files
|
* `jsongz` - a gzip-zipped version of json files
|
||||||
* `hdf5` - a high performance datastore
|
* `hdf5` - a high performance datastore
|
||||||
* `feather` - a dataformat based on Apache Arrow
|
* `feather` - a dataformat based on Apache Arrow (OHLCV only)
|
||||||
* `parquet` - columnar datastore
|
* `parquet` - columnar datastore (OHLCV only)
|
||||||
|
|
||||||
By default, OHLCV data is stored as `json` data, while trades data is stored as `jsongz` data.
|
By default, OHLCV data is stored as `json` data, while trades data is stored as `jsongz` data.
|
||||||
|
|
||||||
|
@ -49,6 +49,13 @@ For more information about the [Remote container extension](https://code.visuals
|
|||||||
New code should be covered by basic unittests. Depending on the complexity of the feature, Reviewers may request more in-depth unittests.
|
New code should be covered by basic unittests. Depending on the complexity of the feature, Reviewers may request more in-depth unittests.
|
||||||
If necessary, the Freqtrade team can assist and give guidance with writing good tests (however please don't expect anyone to write the tests for you).
|
If necessary, the Freqtrade team can assist and give guidance with writing good tests (however please don't expect anyone to write the tests for you).
|
||||||
|
|
||||||
|
#### How to run tests
|
||||||
|
|
||||||
|
Use `pytest` in root folder to run all available testcases and confirm your local environment is setup correctly
|
||||||
|
|
||||||
|
!!! Note "feature branches"
|
||||||
|
Tests are expected to pass on the `develop` and `stable` branches. Other branches may be work in progress with tests not working yet.
|
||||||
|
|
||||||
#### Checking log content in tests
|
#### Checking log content in tests
|
||||||
|
|
||||||
Freqtrade uses 2 main methods to check log content in tests, `log_has()` and `log_has_re()` (to check using regex, in case of dynamic log-messages).
|
Freqtrade uses 2 main methods to check log content in tests, `log_has()` and `log_has_re()` (to check using regex, in case of dynamic log-messages).
|
||||||
@ -434,6 +441,11 @@ To keep the release-log short, best wrap the full git changelog into a collapsib
|
|||||||
</details>
|
</details>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### FreqUI release
|
||||||
|
|
||||||
|
If FreqUI has been updated substantially, make sure to create a release before merging the release branch.
|
||||||
|
Make sure that freqUI CI on the release is finished and passed before merging the release.
|
||||||
|
|
||||||
### Create github release / tag
|
### Create github release / tag
|
||||||
|
|
||||||
Once the PR against stable is merged (best right after merging):
|
Once the PR against stable is merged (best right after merging):
|
||||||
|
@ -54,6 +54,9 @@ This configuration enables kraken, as well as rate-limiting to avoid bans from t
|
|||||||
|
|
||||||
## Binance
|
## Binance
|
||||||
|
|
||||||
|
!!! Warning "Server location and geo-ip restrictions"
|
||||||
|
Please be aware that binance restrict api access regarding the server country. The currents and non exhaustive countries blocked are United States, Malaysia (Singapour), Ontario (Canada). Please go to [binance terms > b. Eligibility](https://www.binance.com/en/terms) to find up to date list.
|
||||||
|
|
||||||
Binance supports [time_in_force](configuration.md#understand-order_time_in_force).
|
Binance supports [time_in_force](configuration.md#understand-order_time_in_force).
|
||||||
|
|
||||||
!!! Tip "Stoploss on Exchange"
|
!!! Tip "Stoploss on Exchange"
|
||||||
@ -173,26 +176,6 @@ res = [p for p, x in lm.items() if 'US' in x['info']['prohibitedIn']]
|
|||||||
print(res)
|
print(res)
|
||||||
```
|
```
|
||||||
|
|
||||||
## FTX
|
|
||||||
|
|
||||||
!!! Tip "Stoploss on Exchange"
|
|
||||||
FTX supports `stoploss_on_exchange` and can use both stop-loss-market and stop-loss-limit orders. It provides great advantages, so we recommend to benefit from it.
|
|
||||||
You can use either `"limit"` or `"market"` in the `order_types.stoploss` configuration setting to decide which type of stoploss shall be used.
|
|
||||||
|
|
||||||
### Using subaccounts
|
|
||||||
|
|
||||||
To use subaccounts with FTX, you need to edit the configuration and add the following:
|
|
||||||
|
|
||||||
``` json
|
|
||||||
"exchange": {
|
|
||||||
"ccxt_config": {
|
|
||||||
"headers": {
|
|
||||||
"FTX-SUBACCOUNT": "name"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Kucoin
|
## Kucoin
|
||||||
|
|
||||||
Kucoin requires a passphrase for each api key, you will therefore need to add this key into the configuration so your exchange section looks as follows:
|
Kucoin requires a passphrase for each api key, you will therefore need to add this key into the configuration so your exchange section looks as follows:
|
||||||
|
@ -4,9 +4,11 @@ The table below will list all configuration parameters available for FreqAI. Som
|
|||||||
|
|
||||||
Mandatory parameters are marked as **Required** and have to be set in one of the suggested ways.
|
Mandatory parameters are marked as **Required** and have to be set in one of the suggested ways.
|
||||||
|
|
||||||
|
### General configuration parameters
|
||||||
|
|
||||||
| Parameter | Description |
|
| Parameter | Description |
|
||||||
|------------|-------------|
|
|------------|-------------|
|
||||||
| | **General configuration parameters**
|
| | **General configuration parameters within the `config.freqai` tree**
|
||||||
| `freqai` | **Required.** <br> The parent dictionary containing all the parameters for controlling FreqAI. <br> **Datatype:** Dictionary.
|
| `freqai` | **Required.** <br> The parent dictionary containing all the parameters for controlling FreqAI. <br> **Datatype:** Dictionary.
|
||||||
| `train_period_days` | **Required.** <br> Number of days to use for the training data (width of the sliding window). <br> **Datatype:** Positive integer.
|
| `train_period_days` | **Required.** <br> Number of days to use for the training data (width of the sliding window). <br> **Datatype:** Positive integer.
|
||||||
| `backtest_period_days` | **Required.** <br> Number of days to inference from the trained model before sliding the `train_period_days` window defined above, and retraining the model during backtesting (more info [here](freqai-running.md#backtesting)). This can be fractional days, but beware that the provided `timerange` will be divided by this number to yield the number of trainings necessary to complete the backtest. <br> **Datatype:** Float.
|
| `backtest_period_days` | **Required.** <br> Number of days to inference from the trained model before sliding the `train_period_days` window defined above, and retraining the model during backtesting (more info [here](freqai-running.md#backtesting)). This can be fractional days, but beware that the provided `timerange` will be divided by this number to yield the number of trainings necessary to complete the backtest. <br> **Datatype:** Float.
|
||||||
@ -19,7 +21,13 @@ Mandatory parameters are marked as **Required** and have to be set in one of the
|
|||||||
| `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. <br> **Datatype:** Boolean. <br> Default: `False`.
|
| `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. <br> **Datatype:** Boolean. <br> 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)). <br> **Datatype:** Boolean. <br> 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)). <br> **Datatype:** Boolean. <br> Default: `False`.
|
||||||
| `write_metrics_to_disk` | Collect train timings, inference timings and cpu usage in json file. <br> **Datatype:** Boolean. <br> Default: `False`
|
| `write_metrics_to_disk` | Collect train timings, inference timings and cpu usage in json file. <br> **Datatype:** Boolean. <br> Default: `False`
|
||||||
| | **Feature parameters**
|
| `data_kitchen_thread_count` | <br> 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) <br> **Datatype:** Positive integer.
|
||||||
|
|
||||||
|
### Feature parameters
|
||||||
|
|
||||||
|
| Parameter | Description |
|
||||||
|
|------------|-------------|
|
||||||
|
| | **Feature parameters within the `freqai.feature_parameters` sub dictionary**
|
||||||
| `feature_parameters` | A dictionary containing the parameters used to engineer the feature set. Details and examples are shown [here](freqai-feature-engineering.md). <br> **Datatype:** Dictionary.
|
| `feature_parameters` | A dictionary containing the parameters used to engineer the feature set. Details and examples are shown [here](freqai-feature-engineering.md). <br> **Datatype:** Dictionary.
|
||||||
| `include_timeframes` | A list of timeframes that all indicators in `populate_any_indicators` will be created for. The list is added as features to the base indicators dataset. <br> **Datatype:** List of timeframes (strings).
|
| `include_timeframes` | A list of timeframes that all indicators in `populate_any_indicators` will be created for. The list is added as features to the base indicators dataset. <br> **Datatype:** List of timeframes (strings).
|
||||||
| `include_corr_pairlist` | A list of correlated coins that FreqAI will add as additional features to all `pair_whitelist` coins. All indicators set in `populate_any_indicators` during feature engineering (see details [here](freqai-feature-engineering.md)) will be created for each correlated coin. The correlated coins features are added to the base indicators dataset. <br> **Datatype:** List of assets (strings).
|
| `include_corr_pairlist` | A list of correlated coins that FreqAI will add as additional features to all `pair_whitelist` coins. All indicators set in `populate_any_indicators` during feature engineering (see details [here](freqai-feature-engineering.md)) will be created for each correlated coin. The correlated coins features are added to the base indicators dataset. <br> **Datatype:** List of assets (strings).
|
||||||
@ -29,7 +37,7 @@ Mandatory parameters are marked as **Required** and have to be set in one of the
|
|||||||
| `indicator_max_period_candles` | **No longer used (#7325)**. Replaced by `startup_candle_count` which is set in the [strategy](freqai-configuration.md#building-a-freqai-strategy). `startup_candle_count` is timeframe independent and defines the maximum *period* used in `populate_any_indicators()` for indicator creation. FreqAI uses this parameter together with the maximum timeframe in `include_time_frames` to calculate how many data points to download such that the first data point does not include a NaN. <br> **Datatype:** Positive integer.
|
| `indicator_max_period_candles` | **No longer used (#7325)**. Replaced by `startup_candle_count` which is set in the [strategy](freqai-configuration.md#building-a-freqai-strategy). `startup_candle_count` is timeframe independent and defines the maximum *period* used in `populate_any_indicators()` for indicator creation. FreqAI uses this parameter together with the maximum timeframe in `include_time_frames` to calculate how many data points to download such that the first data point does not include a NaN. <br> **Datatype:** Positive integer.
|
||||||
| `indicator_periods_candles` | Time periods to calculate indicators for. The indicators are added to the base indicator dataset. <br> **Datatype:** List of positive integers.
|
| `indicator_periods_candles` | Time periods to calculate indicators for. The indicators are added to the base indicator dataset. <br> **Datatype:** List of positive integers.
|
||||||
| `principal_component_analysis` | Automatically reduce the dimensionality of the data set using Principal Component Analysis. See details about how it works [here](#reducing-data-dimensionality-with-principal-component-analysis) <br> **Datatype:** Boolean. <br> Default: `False`.
|
| `principal_component_analysis` | Automatically reduce the dimensionality of the data set using Principal Component Analysis. See details about how it works [here](#reducing-data-dimensionality-with-principal-component-analysis) <br> **Datatype:** Boolean. <br> Default: `False`.
|
||||||
| `plot_feature_importances` | Create a feature importance plot for each model for the top/bottom `plot_feature_importances` number of features. <br> **Datatype:** Integer. <br> Default: `0`.
|
| `plot_feature_importances` | Create a feature importance plot for each model for the top/bottom `plot_feature_importances` number of features. Plot is stored in `user_data/models/<identifier>/sub-train-<COIN>_<timestamp>.html`. <br> **Datatype:** Integer. <br> Default: `0`.
|
||||||
| `DI_threshold` | Activates the use of the Dissimilarity Index for outlier detection when set to > 0. See details about how it works [here](freqai-feature-engineering.md#identifying-outliers-with-the-dissimilarity-index-di). <br> **Datatype:** Positive float (typically < 1).
|
| `DI_threshold` | Activates the use of the Dissimilarity Index for outlier detection when set to > 0. See details about how it works [here](freqai-feature-engineering.md#identifying-outliers-with-the-dissimilarity-index-di). <br> **Datatype:** Positive float (typically < 1).
|
||||||
| `use_SVM_to_remove_outliers` | Train a support vector machine to detect and remove outliers from the training dataset, as well as from incoming data points. See details about how it works [here](freqai-feature-engineering.md#identifying-outliers-using-a-support-vector-machine-svm). <br> **Datatype:** Boolean.
|
| `use_SVM_to_remove_outliers` | Train a support vector machine to detect and remove outliers from the training dataset, as well as from incoming data points. See details about how it works [here](freqai-feature-engineering.md#identifying-outliers-using-a-support-vector-machine-svm). <br> **Datatype:** Boolean.
|
||||||
| `svm_params` | All parameters available in Sklearn's `SGDOneClassSVM()`. See details about some select parameters [here](freqai-feature-engineering.md#identifying-outliers-using-a-support-vector-machine-svm). <br> **Datatype:** Dictionary.
|
| `svm_params` | All parameters available in Sklearn's `SGDOneClassSVM()`. See details about some select parameters [here](freqai-feature-engineering.md#identifying-outliers-using-a-support-vector-machine-svm). <br> **Datatype:** Dictionary.
|
||||||
@ -38,15 +46,49 @@ 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). <br> **Datatype:** Integer. <br> Default: `0`.
|
| `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). <br> **Datatype:** Integer. <br> 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. <br> **Datatype:** Float. <br> Default: `30`.
|
| `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. <br> **Datatype:** Float. <br> 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. <br> **Datatype:** Boolean. <br> Default: `False` (no reversal).
|
| `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. <br> **Datatype:** Boolean. <br> Default: `False` (no reversal).
|
||||||
| | **Data split parameters**
|
|
||||||
|
### 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). <br> **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). <br> **Datatype:** Dictionary.
|
||||||
| `test_size` | The fraction of data that should be used for testing instead of training. <br> **Datatype:** Positive float < 1.
|
| `test_size` | The fraction of data that should be used for testing instead of training. <br> **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`. <br> **Datatype:** Boolean. <br> Defaut: `False`.
|
| `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`. <br> **Datatype:** Boolean. <br> Defaut: `False`.
|
||||||
| | **Model training parameters**
|
|
||||||
|
### Model training parameters
|
||||||
|
|
||||||
|
| Parameter | Description |
|
||||||
|
|------------|-------------|
|
||||||
|
| | **Model training parameters within the `freqai.model_training_parameters` sub dictionary**
|
||||||
| `model_training_parameters` | A flexible dictionary that includes all parameters available by the selected model library. For example, if you use `LightGBMRegressor`, this dictionary can contain any parameter available by the `LightGBMRegressor` [here](https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMRegressor.html) (external website). If you select a different model, this dictionary can contain any parameter from that model. A list of the currently available models can be found [here](freqai-configuration.md#using-different-prediction-models). <br> **Datatype:** Dictionary.
|
| `model_training_parameters` | A flexible dictionary that includes all parameters available by the selected model library. For example, if you use `LightGBMRegressor`, this dictionary can contain any parameter available by the `LightGBMRegressor` [here](https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMRegressor.html) (external website). If you select a different model, this dictionary can contain any parameter from that model. A list of the currently available models can be found [here](freqai-configuration.md#using-different-prediction-models). <br> **Datatype:** Dictionary.
|
||||||
| `n_estimators` | The number of boosted trees to fit in the training of the model. <br> **Datatype:** Integer.
|
| `n_estimators` | The number of boosted trees to fit in the training of the model. <br> **Datatype:** Integer.
|
||||||
| `learning_rate` | Boosting learning rate during training of the model. <br> **Datatype:** Float.
|
| `learning_rate` | Boosting learning rate during training of the model. <br> **Datatype:** Float.
|
||||||
| `n_jobs`, `thread_count`, `task_type` | Set the number of threads for parallel processing and the `task_type` (`gpu` or `cpu`). Different model libraries use different parameter names. <br> **Datatype:** Float.
|
| `n_jobs`, `thread_count`, `task_type` | Set the number of threads for parallel processing and the `task_type` (`gpu` or `cpu`). Different model libraries use different parameter names. <br> **Datatype:** Float.
|
||||||
|
|
||||||
|
### Reinforcement Learning parameters
|
||||||
|
|
||||||
|
| Parameter | Description |
|
||||||
|
|------------|-------------|
|
||||||
|
| | **Reinforcement Learning Parameters within the `freqai.rl_config` sub dictionary**
|
||||||
|
| `rl_config` | A dictionary containing the control parameters for a Reinforcement Learning model. <br> **Datatype:** Dictionary.
|
||||||
|
| `train_cycles` | Training time steps will be set based on the `train_cycles * number of training data points. <br> **Datatype:** Integer.
|
||||||
|
| `cpu_count` | Number of processors to dedicate to the Reinforcement Learning training process. <br> **Datatype:** int.
|
||||||
|
| `max_trade_duration_candles`| Guides the agent training to keep trades below desired length. Example usage shown in `prediction_models/ReinforcementLearner.py` within the customizable `calculate_reward()` function. <br> **Datatype:** int.
|
||||||
|
| `model_type` | Model string from stable_baselines3 or SBcontrib. Available strings include: `'TRPO', 'ARS', 'RecurrentPPO', 'MaskablePPO', 'PPO', 'A2C', 'DQN'`. User should ensure that `model_training_parameters` match those available to the corresponding stable_baselines3 model by visiting their documentaiton. [PPO doc](https://stable-baselines3.readthedocs.io/en/master/modules/ppo.html) (external website) <br> **Datatype:** string.
|
||||||
|
| `policy_type` | One of the available policy types from stable_baselines3 <br> **Datatype:** string.
|
||||||
|
| `max_training_drawdown_pct` | The maximum drawdown that the agent is allowed to experience during training. <br> **Datatype:** float. <br> Default: 0.8
|
||||||
|
| `cpu_count` | Number of threads/cpus to dedicate to the Reinforcement Learning training process (depending on if `ReinforcementLearning_multiproc` is selected or not). Recommended to leave this untouched, by default, this value is set to the total number of physical cores minus 1. <br> **Datatype:** int.
|
||||||
|
| `model_reward_parameters` | Parameters used inside the customizable `calculate_reward()` function in `ReinforcementLearner.py` <br> **Datatype:** int.
|
||||||
|
| `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. <br> **Datatype:** bool. <br> 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: `[<shared layers>, dict(vf=[<non-shared value network layers>], pi=[<non-shared policy network layers>])]`. 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. <br> **Datatype:** bool. <br> Default: `False`.
|
||||||
|
|
||||||
|
### Additional parameters
|
||||||
|
|
||||||
|
| Parameter | Description |
|
||||||
|
|------------|-------------|
|
||||||
| | **Extraneous parameters**
|
| | **Extraneous parameters**
|
||||||
| `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. <br> **Datatype:** Boolean. <br> 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. <br> **Datatype:** Boolean. <br> Default: `False`.
|
||||||
| `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. <br> **Datatype:** Integer. <br> Default: `2`.
|
| `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. <br> **Datatype:** Integer. <br> 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). <br> **Datatype:** Boolean. <br> Default: `False`.
|
||||||
|
260
docs/freqai-reinforcement-learning.md
Normal file
260
docs/freqai-reinforcement-learning.md
Normal file
@ -0,0 +1,260 @@
|
|||||||
|
# Reinforcement Learning
|
||||||
|
|
||||||
|
!!! Note "Installation size"
|
||||||
|
Reinforcement learning dependencies include large packages such as `torch`, which should be explicitly requested during `./setup.sh -i` by answering "y" to the question "Do you also want dependencies for freqai-rl (~700mb additional space required) [y/N]?".
|
||||||
|
Users who prefer docker should ensure they use the docker image appended with `_freqairl`.
|
||||||
|
|
||||||
|
## Background and terminology
|
||||||
|
|
||||||
|
### What is RL and why does FreqAI need it?
|
||||||
|
|
||||||
|
Reinforcement learning involves two important components, the *agent* and the training *environment*. During agent training, the agent moves through historical data candle by candle, always making 1 of a set of actions: Long entry, long exit, short entry, short exit, neutral). During this training process, the environment tracks the performance of these actions and rewards the agent according to a custom user made `calculate_reward()` (here we offer a default reward for users to build on if they wish [details here](#creating-a-custom-reward-function)). The reward is used to train weights in a neural network.
|
||||||
|
|
||||||
|
A second important component of the FreqAI RL implementation is the use of *state* information. State information is fed into the network at each step, including current profit, current position, and current trade duration. These are used to train the agent in the training environment, and to reinforce the agent in dry/live (this functionality is not available in backtesting). *FreqAI + Freqtrade is a perfect match for this reinforcing mechanism since this information is readily available in live deployments.*
|
||||||
|
|
||||||
|
Reinforcement learning is a natural progression for FreqAI, since it adds a new layer of adaptivity and market reactivity that Classifiers and Regressors cannot match. However, Classifiers and Regressors have strengths that RL does not have such as robust predictions. Improperly trained RL agents may find "cheats" and "tricks" to maximize reward without actually winning any trades. For this reason, RL is more complex and demands a higher level of understanding than typical Classifiers and Regressors.
|
||||||
|
|
||||||
|
### The RL interface
|
||||||
|
|
||||||
|
With the current framework, we aim to expose the training environment via the common "prediction model" file, which is a user inherited `BaseReinforcementLearner` object (e.g. `freqai/prediction_models/ReinforcementLearner`). Inside this user class, the RL environment is available and customized via `MyRLEnv` as [shown below](#creating-a-custom-reward-function).
|
||||||
|
|
||||||
|
We envision the majority of users focusing their effort on creative design of the `calculate_reward()` function [details here](#creating-a-custom-reward-function), while leaving the rest of the environment untouched. Other users may not touch the environment at all, and they will only play with the configuration settings and the powerful feature engineering that already exists in FreqAI. Meanwhile, we enable advanced users to create their own model classes entirely.
|
||||||
|
|
||||||
|
The framework is built on stable_baselines3 (torch) and OpenAI gym for the base environment class. But generally speaking, the model class is well isolated. Thus, the addition of competing libraries can be easily integrated into the existing framework. For the environment, it is inheriting from `gym.env` which means that it is necessary to write an entirely new environment in order to switch to a different library.
|
||||||
|
|
||||||
|
### 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.
|
||||||
|
|
||||||
|
## Running Reinforcement Learning
|
||||||
|
|
||||||
|
Setting up and running a Reinforcement Learning model is the same as running a Regressor or Classifier. The same two flags, `--freqaimodel` and `--strategy`, must be defined on the command line:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
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 `populate_any_indicators` as a typical Regressor:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def populate_any_indicators(
|
||||||
|
self, pair, df, tf, informative=None, set_generalized_indicators=False
|
||||||
|
):
|
||||||
|
|
||||||
|
if informative is None:
|
||||||
|
informative = self.dp.get_pair_dataframe(pair, tf)
|
||||||
|
|
||||||
|
# first loop is automatically duplicating indicators for time periods
|
||||||
|
for t in self.freqai_info["feature_parameters"]["indicator_periods_candles"]:
|
||||||
|
|
||||||
|
t = int(t)
|
||||||
|
informative[f"%-{pair}rsi-period_{t}"] = ta.RSI(informative, timeperiod=t)
|
||||||
|
informative[f"%-{pair}mfi-period_{t}"] = ta.MFI(informative, timeperiod=t)
|
||||||
|
informative[f"%-{pair}adx-period_{t}"] = ta.ADX(informative, window=t)
|
||||||
|
|
||||||
|
# The following raw price values are necessary for RL models
|
||||||
|
informative[f"%-{pair}raw_close"] = informative["close"]
|
||||||
|
informative[f"%-{pair}raw_open"] = informative["open"]
|
||||||
|
informative[f"%-{pair}raw_high"] = informative["high"]
|
||||||
|
informative[f"%-{pair}raw_low"] = informative["low"]
|
||||||
|
|
||||||
|
indicators = [col for col in informative if col.startswith("%")]
|
||||||
|
# This loop duplicates and shifts all indicators to add a sense of recency to data
|
||||||
|
for n in range(self.freqai_info["feature_parameters"]["include_shifted_candles"] + 1):
|
||||||
|
if n == 0:
|
||||||
|
continue
|
||||||
|
informative_shift = informative[indicators].shift(n)
|
||||||
|
informative_shift = informative_shift.add_suffix("_shift-" + str(n))
|
||||||
|
informative = pd.concat((informative, informative_shift), axis=1)
|
||||||
|
|
||||||
|
df = merge_informative_pair(df, informative, self.config["timeframe"], tf, ffill=True)
|
||||||
|
skip_columns = [
|
||||||
|
(s + "_" + tf) for s in ["date", "open", "high", "low", "close", "volume"]
|
||||||
|
]
|
||||||
|
df = df.drop(columns=skip_columns)
|
||||||
|
|
||||||
|
# Add generalized indicators here (because in live, it will call this
|
||||||
|
# function to populate indicators during training). Notice how we ensure not to
|
||||||
|
# add them multiple times
|
||||||
|
if set_generalized_indicators:
|
||||||
|
|
||||||
|
# For RL, there are no direct targets to set. This is filler (neutral)
|
||||||
|
# until the agent sends an action.
|
||||||
|
df["&-action"] = 0
|
||||||
|
|
||||||
|
return df
|
||||||
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
# The following features are necessary for RL models
|
||||||
|
informative[f"%-{pair}raw_close"] = informative["close"]
|
||||||
|
informative[f"%-{pair}raw_open"] = informative["open"]
|
||||||
|
informative[f"%-{pair}raw_high"] = informative["high"]
|
||||||
|
informative[f"%-{pair}raw_low"] = informative["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.
|
||||||
|
|
||||||
|
After users realize there are no labels to set, they will soon understand that the agent is making its "own" entry and exit decisions. This makes strategy construction rather simple. The entry and exit signals come from the agent in the form of an integer - which are used directly to decide entries and exits in the strategy:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
|
||||||
|
enter_long_conditions = [df["do_predict"] == 1, df["&-action"] == 1]
|
||||||
|
|
||||||
|
if enter_long_conditions:
|
||||||
|
df.loc[
|
||||||
|
reduce(lambda x, y: x & y, enter_long_conditions), ["enter_long", "enter_tag"]
|
||||||
|
] = (1, "long")
|
||||||
|
|
||||||
|
enter_short_conditions = [df["do_predict"] == 1, df["&-action"] == 3]
|
||||||
|
|
||||||
|
if enter_short_conditions:
|
||||||
|
df.loc[
|
||||||
|
reduce(lambda x, y: x & y, enter_short_conditions), ["enter_short", "enter_tag"]
|
||||||
|
] = (1, "short")
|
||||||
|
|
||||||
|
return df
|
||||||
|
|
||||||
|
def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
exit_long_conditions = [df["do_predict"] == 1, df["&-action"] == 2]
|
||||||
|
if exit_long_conditions:
|
||||||
|
df.loc[reduce(lambda x, y: x & y, exit_long_conditions), "exit_long"] = 1
|
||||||
|
|
||||||
|
exit_short_conditions = [df["do_predict"] == 1, df["&-action"] == 4]
|
||||||
|
if exit_short_conditions:
|
||||||
|
df.loc[reduce(lambda x, y: x & y, exit_short_conditions), "exit_short"] = 1
|
||||||
|
|
||||||
|
return df
|
||||||
|
```
|
||||||
|
|
||||||
|
It is important to consider that `&-action` depends on which environment they choose to use. The example above shows 5 actions, where 0 is neutral, 1 is enter long, 2 is exit long, 3 is enter short and 4 is exit short.
|
||||||
|
|
||||||
|
## Configuring the Reinforcement Learner
|
||||||
|
|
||||||
|
In order to configure the `Reinforcement Learner` the following dictionary must exist in the `freqai` config:
|
||||||
|
|
||||||
|
```json
|
||||||
|
"rl_config": {
|
||||||
|
"train_cycles": 25,
|
||||||
|
"add_state_info": true,
|
||||||
|
"max_trade_duration_candles": 300,
|
||||||
|
"max_training_drawdown_pct": 0.02,
|
||||||
|
"cpu_count": 8,
|
||||||
|
"model_type": "PPO",
|
||||||
|
"policy_type": "MlpPolicy",
|
||||||
|
"model_reward_parameters": {
|
||||||
|
"rr": 1,
|
||||||
|
"profit_aim": 0.025
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Parameter details can be found [here](freqai-parameter-table.md), but in general the `train_cycles` decides how many times the agent should cycle through the candle data in its artificial environment to train weights in the model. `model_type` is a string which selects one of the available models in [stable_baselines](https://stable-baselines3.readthedocs.io/en/master/)(external link).
|
||||||
|
|
||||||
|
!!! Note
|
||||||
|
If you would like to experiment with `continual_learning`, then you should set that value to `true` in the main `freqai` configuration dictionary. This will tell the Reinforcement Learning library to continue training new models from the final state of previous models, instead of retraining new models from scratch each time a retrain is initiated.
|
||||||
|
|
||||||
|
!!! Note
|
||||||
|
Remember that the general `model_training_parameters` dictionary should contain all the model hyperparameter customizations for the particular `model_type`. For example, `PPO` parameters can be found [here](https://stable-baselines3.readthedocs.io/en/master/modules/ppo.html).
|
||||||
|
|
||||||
|
## Creating a custom reward function
|
||||||
|
|
||||||
|
As you begin to modify the strategy and the prediction model, you will quickly realize some important differences between the Reinforcement Learner and the Regressors/Classifiers. Firstly, the strategy does not set a target value (no labels!). Instead, you set the `calculate_reward()` function inside the `MyRLEnv` class (see below). A default `calculate_reward()` is provided inside `prediction_models/ReinforcementLearner.py` to demonstrate the necessary building blocks for creating rewards, but users are encouraged to create their own custom reinforcement learning model class (see below) and save it to `user_data/freqaimodels`. It is inside the `calculate_reward()` where creative theories about the market can be expressed. For example, you can reward your agent when it makes a winning trade, and penalize the agent when it makes a losing trade. Or perhaps, you wish to reward the agent for entering trades, and penalize the agent for sitting in trades too long. Below we show examples of how these rewards are all calculated:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from freqtrade.freqai.prediction_models.ReinforcementLearner import ReinforcementLearner
|
||||||
|
from freqtrade.freqai.RL.Base5ActionRLEnv import Actions, Base5ActionRLEnv, Positions
|
||||||
|
|
||||||
|
|
||||||
|
class MyCoolRLModel(ReinforcementLearner):
|
||||||
|
"""
|
||||||
|
User created RL prediction model.
|
||||||
|
|
||||||
|
Save this file to `freqtrade/user_data/freqaimodels`
|
||||||
|
|
||||||
|
then use it with:
|
||||||
|
|
||||||
|
freqtrade trade --freqaimodel MyCoolRLModel --config config.json --strategy SomeCoolStrat
|
||||||
|
|
||||||
|
Here the users can override any of the functions
|
||||||
|
available in the `IFreqaiModel` inheritance tree. Most importantly for RL, this
|
||||||
|
is where the user overrides `MyRLEnv` (see below), to define custom
|
||||||
|
`calculate_reward()` function, or to override any other parts of the environment.
|
||||||
|
|
||||||
|
This class also allows users to override any other part of the IFreqaiModel tree.
|
||||||
|
For example, the user can override `def fit()` or `def train()` or `def predict()`
|
||||||
|
to take fine-tuned control over these processes.
|
||||||
|
|
||||||
|
Another common override may be `def data_cleaning_predict()` where the user can
|
||||||
|
take fine-tuned control over the data handling pipeline.
|
||||||
|
"""
|
||||||
|
class MyRLEnv(Base5ActionRLEnv):
|
||||||
|
"""
|
||||||
|
User made custom environment. This class inherits from BaseEnvironment and gym.env.
|
||||||
|
Users can override any functions from those parent classes. Here is an example
|
||||||
|
of a user customized `calculate_reward()` function.
|
||||||
|
"""
|
||||||
|
def calculate_reward(self, action: int) -> float:
|
||||||
|
# first, penalize if the action is not valid
|
||||||
|
if not self._is_valid(action):
|
||||||
|
return -2
|
||||||
|
pnl = self.get_unrealized_profit()
|
||||||
|
|
||||||
|
factor = 100
|
||||||
|
# reward agent for entering trades
|
||||||
|
if action in (Actions.Long_enter.value, Actions.Short_enter.value) \
|
||||||
|
and self._position == Positions.Neutral:
|
||||||
|
return 25
|
||||||
|
# discourage agent from not entering trades
|
||||||
|
if action == Actions.Neutral.value and self._position == Positions.Neutral:
|
||||||
|
return -1
|
||||||
|
max_trade_duration = self.rl_config.get('max_trade_duration_candles', 300)
|
||||||
|
trade_duration = self._current_tick - self._last_trade_tick
|
||||||
|
if trade_duration <= max_trade_duration:
|
||||||
|
factor *= 1.5
|
||||||
|
elif trade_duration > max_trade_duration:
|
||||||
|
factor *= 0.5
|
||||||
|
# discourage sitting in position
|
||||||
|
if self._position in (Positions.Short, Positions.Long) and \
|
||||||
|
action == Actions.Neutral.value:
|
||||||
|
return -1 * trade_duration / max_trade_duration
|
||||||
|
# close long
|
||||||
|
if action == Actions.Long_exit.value and self._position == Positions.Long:
|
||||||
|
if pnl > self.profit_aim * self.rr:
|
||||||
|
factor *= self.rl_config['model_reward_parameters'].get('win_reward_factor', 2)
|
||||||
|
return float(pnl * factor)
|
||||||
|
# close short
|
||||||
|
if action == Actions.Short_exit.value and self._position == Positions.Short:
|
||||||
|
if pnl > self.profit_aim * self.rr:
|
||||||
|
factor *= self.rl_config['model_reward_parameters'].get('win_reward_factor', 2)
|
||||||
|
return float(pnl * factor)
|
||||||
|
return 0.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using Tensorboard
|
||||||
|
|
||||||
|
Reinforcement Learning models benefit from tracking training metrics. FreqAI has integrated Tensorboard to allow users to track training and evaluation performance across all coins and across all retrainings. Tensorboard is activated via the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd freqtrade
|
||||||
|
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 to view the output in their browser at 127.0.0.1:6006 (6006 is the default port used by Tensorboard).
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### Choosing a base environment
|
||||||
|
|
||||||
|
FreqAI provides two base environments, `Base4ActionEnvironment` and `Base5ActionEnvironment`. As the names imply, the environments are customized for agents that can select from 4 or 5 actions. In the `Base4ActionEnvironment`, the agent can enter long, enter short, hold neutral, or exit position. Meanwhile, in the `Base5ActionEnvironment`, the agent has the same actions as Base4, but instead of a single exit action, it separates exit long and exit short. The main changes stemming from the environment selection include:
|
||||||
|
|
||||||
|
* the actions available in the `calculate_reward`
|
||||||
|
* the actions consumed by the user strategy
|
||||||
|
|
||||||
|
Both of the FreqAI provided environments inherit from an action/position agnostic environment object called the `BaseEnvironment`, which contains all shared logic. The architecture is designed to be easily customized. The simplest customization is the `calculate_reward()` (see details [here](#creating-a-custom-reward-function)). However, the customizations can be further extended into any of the functions inside the environment. You can do this by simply overriding those functions inside your `MyRLEnv` in the prediction model file. Or for more advanced customizations, it is encouraged to create an entirely new environment inherited from `BaseEnvironment`.
|
||||||
|
|
||||||
|
!!! Note
|
||||||
|
FreqAI does not provide by default, a long-only training environment. However, creating one should be as simple as copy-pasting one of the built in environments and removing the `short` actions (and all associated references to those).
|
@ -73,12 +73,19 @@ Backtesting mode requires [downloading the necessary data](#downloading-data-to-
|
|||||||
|
|
||||||
To allow for tweaking your strategy (**not** the features!), FreqAI will automatically save the predictions during backtesting so that they can be reused for future backtests and live runs using the same `identifier` model. This provides a performance enhancement geared towards enabling **high-level hyperopting** of entry/exit criteria.
|
To allow for tweaking your strategy (**not** the features!), FreqAI will automatically save the predictions during backtesting so that they can be reused for future backtests and live runs using the same `identifier` model. This provides a performance enhancement geared towards enabling **high-level hyperopting** of entry/exit criteria.
|
||||||
|
|
||||||
An additional directory called `predictions`, which contains all the predictions stored in `hdf` format, will be created in the `unique-id` folder.
|
An additional directory called `backtesting_predictions`, which contains all the predictions stored in `hdf` format, will be created in the `unique-id` folder.
|
||||||
|
|
||||||
To change your **features**, you **must** set a new `identifier` in the config to signal to FreqAI to train new models.
|
To change your **features**, you **must** set a new `identifier` in the config to signal to FreqAI to train new models.
|
||||||
|
|
||||||
To save the models generated during a particular backtest so that you can start a live deployment from one of them instead of training a new model, you must set `save_backtest_models` to `True` in the config.
|
To save the models generated during a particular backtest so that you can start a live deployment from one of them instead of training a new model, you must set `save_backtest_models` to `True` in the config.
|
||||||
|
|
||||||
|
### Backtest live collected predictions
|
||||||
|
|
||||||
|
FreqAI allow you to reuse live historic predictions through the backtest parameter `--freqai-backtest-live-models`. This can be useful when you want to reuse predictions generated in dry/run for comparison or other study.
|
||||||
|
|
||||||
|
The `--timerange` parameter must not be informed, as it will be automatically calculated through the data in the historic predictions file.
|
||||||
|
|
||||||
|
|
||||||
### Downloading data to cover the full backtest period
|
### Downloading data to cover the full backtest period
|
||||||
|
|
||||||
For live/dry deployments, FreqAI will download the necessary data automatically. However, to use backtesting functionality, you need to download the necessary data using `download-data` (details [here](data-download.md#data-downloading)). You need to pay careful attention to understanding how much *additional* data needs to be downloaded to ensure that there is a sufficient amount of training data *before* the start of the backtesting time range. The amount of additional data can be roughly estimated by moving the start date of the time range backwards by `train_period_days` and the `startup_candle_count` (see the [parameter table](freqai-parameter-table.md) for detailed descriptions of these parameters) from the beginning of the desired backtesting time range.
|
For live/dry deployments, FreqAI will download the necessary data automatically. However, to use backtesting functionality, you need to download the necessary data using `download-data` (details [here](data-download.md#data-downloading)). You need to pay careful attention to understanding how much *additional* data needs to be downloaded to ensure that there is a sufficient amount of training data *before* the start of the backtesting time range. The amount of additional data can be roughly estimated by moving the start date of the time range backwards by `train_period_days` and the `startup_candle_count` (see the [parameter table](freqai-parameter-table.md) for detailed descriptions of these parameters) from the beginning of the desired backtesting time range.
|
||||||
|
@ -268,7 +268,7 @@ This option is disabled by default, and will only apply if set to > 0.
|
|||||||
The `max_value` setting removes pairs where the minimum value change is above a specified value.
|
The `max_value` setting removes pairs where the minimum value change is above a specified value.
|
||||||
This is useful when an exchange has unbalanced limits. For example, if step-size = 1 (so you can only buy 1, or 2, or 3, but not 1.1 Coins) - and the price is pretty high (like 20\$) as the coin has risen sharply since the last limit adaption.
|
This is useful when an exchange has unbalanced limits. For example, if step-size = 1 (so you can only buy 1, or 2, or 3, but not 1.1 Coins) - and the price is pretty high (like 20\$) as the coin has risen sharply since the last limit adaption.
|
||||||
As a result of the above, you can only buy for 20\$, or 40\$ - but not for 25\$.
|
As a result of the above, you can only buy for 20\$, or 40\$ - but not for 25\$.
|
||||||
On exchanges that deduct fees from the receiving currency (e.g. FTX) - this can result in high value coins / amounts that are unsellable as the amount is slightly below the limit.
|
On exchanges that deduct fees from the receiving currency (e.g. binance) - this can result in high value coins / amounts that are unsellable as the amount is slightly below the limit.
|
||||||
|
|
||||||
The `low_price_ratio` setting removes pairs where a raise of 1 price unit (pip) is above the `low_price_ratio` ratio.
|
The `low_price_ratio` setting removes pairs where a raise of 1 price unit (pip) is above the `low_price_ratio` ratio.
|
||||||
This option is disabled by default, and will only apply if set to > 0.
|
This option is disabled by default, and will only apply if set to > 0.
|
||||||
|
@ -32,7 +32,7 @@ Freqtrade is a free and open source crypto trading bot written in Python. It is
|
|||||||
- Run: Test your strategy with simulated money (Dry-Run mode) or deploy it with real money (Live-Trade mode).
|
- Run: Test your strategy with simulated money (Dry-Run mode) or deploy it with real money (Live-Trade mode).
|
||||||
- Run using Edge (optional module): The concept is to find the best historical [trade expectancy](edge.md#expectancy) by markets based on variation of the stop-loss and then allow/reject markets to trade. The sizing of the trade is based on a risk of a percentage of your capital.
|
- Run using Edge (optional module): The concept is to find the best historical [trade expectancy](edge.md#expectancy) by markets based on variation of the stop-loss and then allow/reject markets to trade. The sizing of the trade is based on a risk of a percentage of your capital.
|
||||||
- Control/Monitor: Use Telegram or a WebUI (start/stop the bot, show profit/loss, daily summary, current open trades results, etc.).
|
- Control/Monitor: Use Telegram or a WebUI (start/stop the bot, show profit/loss, daily summary, current open trades results, etc.).
|
||||||
- Analyse: Further analysis can be performed on either Backtesting data or Freqtrade trading history (SQL database), including automated standard plots, and methods to load the data into [interactive environments](data-analysis.md).
|
- Analyze: Further analysis can be performed on either Backtesting data or Freqtrade trading history (SQL database), including automated standard plots, and methods to load the data into [interactive environments](data-analysis.md).
|
||||||
|
|
||||||
## Supported exchange marketplaces
|
## Supported exchange marketplaces
|
||||||
|
|
||||||
@ -40,7 +40,6 @@ Please read the [exchange specific notes](exchanges.md) to learn about eventual,
|
|||||||
|
|
||||||
- [X] [Binance](https://www.binance.com/)
|
- [X] [Binance](https://www.binance.com/)
|
||||||
- [X] [Bittrex](https://bittrex.com/)
|
- [X] [Bittrex](https://bittrex.com/)
|
||||||
- [X] [FTX](https://ftx.com/#a=2258149)
|
|
||||||
- [X] [Gate.io](https://www.gate.io/ref/6266643)
|
- [X] [Gate.io](https://www.gate.io/ref/6266643)
|
||||||
- [X] [Huobi](http://huobi.com/)
|
- [X] [Huobi](http://huobi.com/)
|
||||||
- [X] [Kraken](https://kraken.com/)
|
- [X] [Kraken](https://kraken.com/)
|
||||||
@ -51,7 +50,7 @@ Please read the [exchange specific notes](exchanges.md) to learn about eventual,
|
|||||||
|
|
||||||
- [X] [Binance](https://www.binance.com/)
|
- [X] [Binance](https://www.binance.com/)
|
||||||
- [X] [Gate.io](https://www.gate.io/ref/6266643)
|
- [X] [Gate.io](https://www.gate.io/ref/6266643)
|
||||||
- [X] [OKX](https://okx.com/).
|
- [X] [OKX](https://okx.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.
|
Please make sure to read the [exchange specific notes](exchanges.md), as well as the [trading with leverage](leverage.md) documentation before diving in.
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ Enable subscribing to an instance by adding the `external_message_consumer` sect
|
|||||||
"name": "default", // This can be any name you'd like, default is "default"
|
"name": "default", // This can be any name you'd like, default is "default"
|
||||||
"host": "127.0.0.1", // The host from your producer's api_server config
|
"host": "127.0.0.1", // The host from your producer's api_server config
|
||||||
"port": 8080, // The port from your producer's api_server config
|
"port": 8080, // The port from your producer's api_server config
|
||||||
|
"secure": false, // Use a secure websockets connection, default false
|
||||||
"ws_token": "sercet_Ws_t0ken" // The ws_token from your producer's api_server config
|
"ws_token": "sercet_Ws_t0ken" // The ws_token from your producer's api_server config
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -42,6 +43,7 @@ Enable subscribing to an instance by adding the `external_message_consumer` sect
|
|||||||
| `producers.name` | **Required.** Name of this producer. This name must be used in calls to `get_producer_pairs()` and `get_producer_df()` if more than one producer is used.<br> **Datatype:** string
|
| `producers.name` | **Required.** Name of this producer. This name must be used in calls to `get_producer_pairs()` and `get_producer_df()` if more than one producer is used.<br> **Datatype:** string
|
||||||
| `producers.host` | **Required.** The hostname or IP address from your producer.<br> **Datatype:** string
|
| `producers.host` | **Required.** The hostname or IP address from your producer.<br> **Datatype:** string
|
||||||
| `producers.port` | **Required.** The port matching the above host.<br> **Datatype:** string
|
| `producers.port` | **Required.** The port matching the above host.<br> **Datatype:** string
|
||||||
|
| `producers.secure` | **Optional.** Use ssl in websockets connection. Default False.<br> **Datatype:** string
|
||||||
| `producers.ws_token` | **Required.** `ws_token` as configured on the producer.<br> **Datatype:** string
|
| `producers.ws_token` | **Required.** `ws_token` as configured on the producer.<br> **Datatype:** string
|
||||||
| | **Optional settings**
|
| | **Optional settings**
|
||||||
| `wait_timeout` | Timeout until we ping again if no message is received. <br>*Defaults to `300`.*<br> **Datatype:** Integer - in seconds.
|
| `wait_timeout` | Timeout until we ping again if no message is received. <br>*Defaults to `300`.*<br> **Datatype:** Integer - in seconds.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
markdown==3.3.7
|
markdown==3.3.7
|
||||||
mkdocs==1.4.1
|
mkdocs==1.4.2
|
||||||
mkdocs-material==8.5.7
|
mkdocs-material==8.5.11
|
||||||
mdx_truly_sane_lists==1.3
|
mdx_truly_sane_lists==1.3
|
||||||
pymdown-extensions==9.7
|
pymdown-extensions==9.9
|
||||||
jinja2==3.1.2
|
jinja2==3.1.2
|
||||||
|
@ -389,6 +389,44 @@ Now anytime those types of RPC messages are sent in the bot, you will receive th
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Reverse Proxy setup
|
||||||
|
|
||||||
|
When using [Nginx](https://nginx.org/en/docs/), the following configuration is required for WebSockets to work (Note this configuration is incomplete, it's missing some information and can not be used as is):
|
||||||
|
|
||||||
|
Please make sure to replace `<freqtrade_listen_ip>` (and the subsequent port) with the IP and Port matching your configuration/setup.
|
||||||
|
|
||||||
|
```
|
||||||
|
http {
|
||||||
|
map $http_upgrade $connection_upgrade {
|
||||||
|
default upgrade;
|
||||||
|
'' close;
|
||||||
|
}
|
||||||
|
|
||||||
|
#...
|
||||||
|
|
||||||
|
server {
|
||||||
|
#...
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_pass http://<freqtrade_listen_ip>:8080;
|
||||||
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
|
proxy_set_header Connection $connection_upgrade;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
To properly configure your reverse proxy (securely), please consult it's documentation for proxying websockets.
|
||||||
|
|
||||||
|
- **Traefik**: Traefik supports websockets out of the box, see the [documentation](https://doc.traefik.io/traefik/)
|
||||||
|
- **Caddy**: Caddy v2 supports websockets out of the box, see the [documentation](https://caddyserver.com/docs/v2-upgrade#proxy)
|
||||||
|
|
||||||
|
!!! Tip "SSL certificates"
|
||||||
|
You can use tools like certbot to setup ssl certificates to access your bot's UI through encrypted connection by using any fo the above reverse proxies.
|
||||||
|
While this will protect your data in transit, we do not recommend to run the freqtrade API outside of your private network (VPN, SSH tunnel).
|
||||||
|
|
||||||
### OpenAPI interface
|
### OpenAPI interface
|
||||||
|
|
||||||
To enable the builtin openAPI interface (Swagger UI), specify `"enable_openapi": true` in the api_server configuration.
|
To enable the builtin openAPI interface (Swagger UI), specify `"enable_openapi": true` in the api_server configuration.
|
||||||
|
@ -24,7 +24,7 @@ These modes can be configured with these values:
|
|||||||
```
|
```
|
||||||
|
|
||||||
!!! Note
|
!!! Note
|
||||||
Stoploss on exchange is only supported for Binance (stop-loss-limit), Huobi (stop-limit), Kraken (stop-loss-market, stop-loss-limit), FTX (stop limit and stop-market) 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), Gateio (stop-limit), and Kucoin (stop-limit and stop-market) as of now.
|
||||||
<ins>Do not set too low/tight stoploss value if using stop loss on exchange!</ins>
|
<ins>Do not set too low/tight stoploss value if using stop loss on exchange!</ins>
|
||||||
If set to low/tight then you have greater risk of missing fill on the order and stoploss will not work.
|
If set to low/tight then you have greater risk of missing fill on the order and stoploss will not work.
|
||||||
|
|
||||||
|
@ -446,15 +446,17 @@ A full sample can be found [in the DataProvider section](#complete-data-provider
|
|||||||
|
|
||||||
??? Note "Alternative candle types"
|
??? Note "Alternative candle types"
|
||||||
Informative_pairs can also provide a 3rd tuple element defining the candle type explicitly.
|
Informative_pairs can also provide a 3rd tuple element defining the candle type explicitly.
|
||||||
Availability of alternative candle-types will depend on the trading-mode and the exchange. Details about this can be found in the exchange documentation.
|
Availability of alternative candle-types will depend on the trading-mode and the exchange.
|
||||||
|
In general, spot pairs cannot be used in futures markets, and futures candles can't be used as informative pairs for spot bots.
|
||||||
|
Details about this may vary, if they do, this can be found in the exchange documentation.
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
def informative_pairs(self):
|
def informative_pairs(self):
|
||||||
return [
|
return [
|
||||||
("ETH/USDT", "5m", ""), # Uses default candletype, depends on trading_mode
|
("ETH/USDT", "5m", ""), # Uses default candletype, depends on trading_mode (recommended)
|
||||||
("ETH/USDT", "5m", "spot"), # Forces usage of spot candles
|
("ETH/USDT", "5m", "spot"), # Forces usage of spot candles (only valid for bots running on spot markets).
|
||||||
("BTC/TUSD", "15m", "futures"), # Uses futures candles
|
("BTC/TUSD", "15m", "futures"), # Uses futures candles (only bots with `trading_mode=futures`)
|
||||||
("BTC/TUSD", "15m", "mark"), # Uses mark candles
|
("BTC/TUSD", "15m", "mark"), # Uses mark candles (only bots with `trading_mode=futures`)
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
***
|
***
|
||||||
@ -723,7 +725,7 @@ if self.dp.runmode.value in ('live', 'dry_run'):
|
|||||||
|
|
||||||
!!! Warning
|
!!! Warning
|
||||||
Although the ticker data structure is a part of the ccxt Unified Interface, the values returned by this method can
|
Although the ticker data structure is a part of the ccxt Unified Interface, the values returned by this method can
|
||||||
vary for different exchanges. For instance, many exchanges do not return `vwap` values, the FTX exchange
|
vary for different exchanges. For instance, many exchanges do not return `vwap` values, some exchanges
|
||||||
does not always fills in the `last` field (so it can be None), etc. So you need to carefully verify the ticker
|
does not always fills in the `last` field (so it can be None), etc. So you need to carefully verify the ticker
|
||||||
data returned from the exchange and add appropriate error handling / defaults.
|
data returned from the exchange and add appropriate error handling / defaults.
|
||||||
|
|
||||||
|
@ -232,7 +232,7 @@ graph = generate_candlestick_graph(pair=pair,
|
|||||||
# Show graph inline
|
# Show graph inline
|
||||||
# graph.show()
|
# graph.show()
|
||||||
|
|
||||||
# Render graph in a seperate window
|
# Render graph in a separate window
|
||||||
graph.show(renderer="browser")
|
graph.show(renderer="browser")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -263,7 +263,6 @@ equos True missing opt: fetchTicker, fetchTickers
|
|||||||
eterbase True
|
eterbase True
|
||||||
fcoin True missing opt: fetchMyTrades, fetchTickers
|
fcoin True missing opt: fetchMyTrades, fetchTickers
|
||||||
fcoinjp True missing opt: fetchMyTrades, fetchTickers
|
fcoinjp True missing opt: fetchMyTrades, fetchTickers
|
||||||
ftx True
|
|
||||||
gateio True
|
gateio True
|
||||||
gemini True
|
gemini True
|
||||||
gopax True
|
gopax True
|
||||||
@ -369,7 +368,6 @@ fcoin True missing opt: fetchMyTrades, fetchTickers
|
|||||||
fcoinjp True missing opt: fetchMyTrades, fetchTickers
|
fcoinjp True missing opt: fetchMyTrades, fetchTickers
|
||||||
flowbtc False missing: fetchOrder, fetchOHLCV
|
flowbtc False missing: fetchOrder, fetchOHLCV
|
||||||
foxbit False missing: fetchOrder, fetchOHLCV
|
foxbit False missing: fetchOrder, fetchOHLCV
|
||||||
ftx True
|
|
||||||
gateio True
|
gateio True
|
||||||
gemini True
|
gemini True
|
||||||
gopax True
|
gopax True
|
||||||
@ -724,6 +722,7 @@ usage: freqtrade backtesting-analysis [-h] [-v] [--logfile FILE] [-V]
|
|||||||
[--enter-reason-list ENTER_REASON_LIST [ENTER_REASON_LIST ...]]
|
[--enter-reason-list ENTER_REASON_LIST [ENTER_REASON_LIST ...]]
|
||||||
[--exit-reason-list EXIT_REASON_LIST [EXIT_REASON_LIST ...]]
|
[--exit-reason-list EXIT_REASON_LIST [EXIT_REASON_LIST ...]]
|
||||||
[--indicator-list INDICATOR_LIST [INDICATOR_LIST ...]]
|
[--indicator-list INDICATOR_LIST [INDICATOR_LIST ...]]
|
||||||
|
[--timerange YYYYMMDD-[YYYYMMDD]]
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
@ -746,6 +745,10 @@ optional arguments:
|
|||||||
--indicator-list INDICATOR_LIST [INDICATOR_LIST ...]
|
--indicator-list INDICATOR_LIST [INDICATOR_LIST ...]
|
||||||
Comma separated list of indicators to analyse. e.g.
|
Comma separated list of indicators to analyse. e.g.
|
||||||
'close,rsi,bb_lowerband,profit_abs'
|
'close,rsi,bb_lowerband,profit_abs'
|
||||||
|
--timerange YYYYMMDD-[YYYYMMDD]
|
||||||
|
Timerange to filter trades for analysis,
|
||||||
|
start inclusive, end exclusive. e.g.
|
||||||
|
20220101-20220201
|
||||||
|
|
||||||
Common arguments:
|
Common arguments:
|
||||||
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
|
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
""" Freqtrade bot """
|
""" Freqtrade bot """
|
||||||
__version__ = '2022.11.dev'
|
__version__ = '2022.12.dev'
|
||||||
|
|
||||||
if 'dev' in __version__:
|
if 'dev' in __version__:
|
||||||
try:
|
try:
|
||||||
|
@ -60,10 +60,4 @@ def start_analysis_entries_exits(args: Dict[str, Any]) -> None:
|
|||||||
|
|
||||||
logger.info('Starting freqtrade in analysis mode')
|
logger.info('Starting freqtrade in analysis mode')
|
||||||
|
|
||||||
process_entry_exit_reasons(config['exportfilename'],
|
process_entry_exit_reasons(config)
|
||||||
config['exchange']['pair_whitelist'],
|
|
||||||
config['analysis_groups'],
|
|
||||||
config['enter_reason_list'],
|
|
||||||
config['exit_reason_list'],
|
|
||||||
config['indicator_list']
|
|
||||||
)
|
|
||||||
|
@ -25,7 +25,8 @@ ARGS_COMMON_OPTIMIZE = ["timeframe", "timerange", "dataformat_ohlcv",
|
|||||||
ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_positions",
|
ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_positions",
|
||||||
"enable_protections", "dry_run_wallet", "timeframe_detail",
|
"enable_protections", "dry_run_wallet", "timeframe_detail",
|
||||||
"strategy_list", "export", "exportfilename",
|
"strategy_list", "export", "exportfilename",
|
||||||
"backtest_breakdown", "backtest_cache"]
|
"backtest_breakdown", "backtest_cache",
|
||||||
|
"freqai_backtest_live_models"]
|
||||||
|
|
||||||
ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path",
|
ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path",
|
||||||
"position_stacking", "use_max_market_positions",
|
"position_stacking", "use_max_market_positions",
|
||||||
@ -105,7 +106,7 @@ ARGS_HYPEROPT_SHOW = ["hyperopt_list_best", "hyperopt_list_profitable", "hyperop
|
|||||||
"disableparamexport", "backtest_breakdown"]
|
"disableparamexport", "backtest_breakdown"]
|
||||||
|
|
||||||
ARGS_ANALYZE_ENTRIES_EXITS = ["exportfilename", "analysis_groups", "enter_reason_list",
|
ARGS_ANALYZE_ENTRIES_EXITS = ["exportfilename", "analysis_groups", "enter_reason_list",
|
||||||
"exit_reason_list", "indicator_list"]
|
"exit_reason_list", "indicator_list", "timerange"]
|
||||||
|
|
||||||
NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes",
|
NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes",
|
||||||
"list-markets", "list-pairs", "list-strategies", "list-freqaimodels",
|
"list-markets", "list-pairs", "list-strategies", "list-freqaimodels",
|
||||||
|
@ -108,7 +108,6 @@ def ask_user_config() -> Dict[str, Any]:
|
|||||||
"binance",
|
"binance",
|
||||||
"binanceus",
|
"binanceus",
|
||||||
"bittrex",
|
"bittrex",
|
||||||
"ftx",
|
|
||||||
"gateio",
|
"gateio",
|
||||||
"huobi",
|
"huobi",
|
||||||
"kraken",
|
"kraken",
|
||||||
|
@ -668,4 +668,9 @@ AVAILABLE_CLI_OPTIONS = {
|
|||||||
help='Specify additional lookup path for freqaimodels.',
|
help='Specify additional lookup path for freqaimodels.',
|
||||||
metavar='PATH',
|
metavar='PATH',
|
||||||
),
|
),
|
||||||
|
"freqai_backtest_live_models": Arg(
|
||||||
|
'--freqai-backtest-live-models',
|
||||||
|
help='Run backtest with ready models.',
|
||||||
|
action='store_true'
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
@ -86,6 +86,7 @@ def validate_config_consistency(conf: Dict[str, Any], preliminary: bool = False)
|
|||||||
_validate_unlimited_amount(conf)
|
_validate_unlimited_amount(conf)
|
||||||
_validate_ask_orderbook(conf)
|
_validate_ask_orderbook(conf)
|
||||||
_validate_freqai_hyperopt(conf)
|
_validate_freqai_hyperopt(conf)
|
||||||
|
_validate_freqai_backtest(conf)
|
||||||
_validate_freqai_include_timeframes(conf)
|
_validate_freqai_include_timeframes(conf)
|
||||||
_validate_consumers(conf)
|
_validate_consumers(conf)
|
||||||
validate_migrated_strategy_settings(conf)
|
validate_migrated_strategy_settings(conf)
|
||||||
@ -355,6 +356,26 @@ def _validate_freqai_include_timeframes(conf: Dict[str, Any]) -> None:
|
|||||||
f"`include_timeframes`.Offending include-timeframes: {', '.join(offending_lines)}")
|
f"`include_timeframes`.Offending include-timeframes: {', '.join(offending_lines)}")
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_freqai_backtest(conf: Dict[str, Any]) -> None:
|
||||||
|
if conf.get('runmode', RunMode.OTHER) == RunMode.BACKTEST:
|
||||||
|
freqai_enabled = conf.get('freqai', {}).get('enabled', False)
|
||||||
|
timerange = conf.get('timerange')
|
||||||
|
freqai_backtest_live_models = conf.get('freqai_backtest_live_models', False)
|
||||||
|
if freqai_backtest_live_models and freqai_enabled and timerange:
|
||||||
|
raise OperationalException(
|
||||||
|
'Using timerange parameter is not supported with '
|
||||||
|
'--freqai-backtest-live-models parameter.')
|
||||||
|
|
||||||
|
if freqai_backtest_live_models and not freqai_enabled:
|
||||||
|
raise OperationalException(
|
||||||
|
'Using --freqai-backtest-live-models parameter is only '
|
||||||
|
'supported with a FreqAI strategy.')
|
||||||
|
|
||||||
|
if freqai_enabled and not freqai_backtest_live_models and not timerange:
|
||||||
|
raise OperationalException(
|
||||||
|
'Please pass --timerange if you intend to use FreqAI for backtesting.')
|
||||||
|
|
||||||
|
|
||||||
def _validate_consumers(conf: Dict[str, Any]) -> None:
|
def _validate_consumers(conf: Dict[str, Any]) -> None:
|
||||||
emc_conf = conf.get('external_message_consumer', {})
|
emc_conf = conf.get('external_message_consumer', {})
|
||||||
if emc_conf.get('enabled', False):
|
if emc_conf.get('enabled', False):
|
||||||
|
@ -279,6 +279,9 @@ class Configuration:
|
|||||||
self._args_to_config(config, argname='disableparamexport',
|
self._args_to_config(config, argname='disableparamexport',
|
||||||
logstring='Parameter --disableparamexport detected: {} ...')
|
logstring='Parameter --disableparamexport detected: {} ...')
|
||||||
|
|
||||||
|
self._args_to_config(config, argname='freqai_backtest_live_models',
|
||||||
|
logstring='Parameter --freqai-backtest-live-models detected ...')
|
||||||
|
|
||||||
# Edge section:
|
# Edge section:
|
||||||
if 'stoploss_range' in self.args and self.args["stoploss_range"]:
|
if 'stoploss_range' in self.args and self.args["stoploss_range"]:
|
||||||
txt_range = eval(self.args["stoploss_range"])
|
txt_range = eval(self.args["stoploss_range"])
|
||||||
@ -459,6 +462,9 @@ class Configuration:
|
|||||||
self._args_to_config(config, argname='indicator_list',
|
self._args_to_config(config, argname='indicator_list',
|
||||||
logstring='Analysis indicator list: {}')
|
logstring='Analysis indicator list: {}')
|
||||||
|
|
||||||
|
self._args_to_config(config, argname='timerange',
|
||||||
|
logstring='Filter trades by timerange: {}')
|
||||||
|
|
||||||
def _process_runmode(self, config: Config) -> None:
|
def _process_runmode(self, config: Config) -> None:
|
||||||
|
|
||||||
self._args_to_config(config, argname='dry_run',
|
self._args_to_config(config, argname='dry_run',
|
||||||
|
@ -3,11 +3,12 @@ This module contains the argument manager class
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import arrow
|
import arrow
|
||||||
|
|
||||||
|
from freqtrade.constants import DATETIME_PRINT_FORMAT
|
||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
|
|
||||||
|
|
||||||
@ -29,6 +30,52 @@ class TimeRange:
|
|||||||
self.startts: int = startts
|
self.startts: int = startts
|
||||||
self.stopts: int = stopts
|
self.stopts: int = stopts
|
||||||
|
|
||||||
|
@property
|
||||||
|
def startdt(self) -> Optional[datetime]:
|
||||||
|
if self.startts:
|
||||||
|
return datetime.fromtimestamp(self.startts, tz=timezone.utc)
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def stopdt(self) -> Optional[datetime]:
|
||||||
|
if self.stopts:
|
||||||
|
return datetime.fromtimestamp(self.stopts, tz=timezone.utc)
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def timerange_str(self) -> str:
|
||||||
|
"""
|
||||||
|
Returns a string representation of the timerange as used by parse_timerange.
|
||||||
|
Follows the format yyyymmdd-yyyymmdd - leaving out the parts that are not set.
|
||||||
|
"""
|
||||||
|
start = ''
|
||||||
|
stop = ''
|
||||||
|
if startdt := self.startdt:
|
||||||
|
start = startdt.strftime('%Y%m%d')
|
||||||
|
if stopdt := self.stopdt:
|
||||||
|
stop = stopdt.strftime('%Y%m%d')
|
||||||
|
return f"{start}-{stop}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def start_fmt(self) -> str:
|
||||||
|
"""
|
||||||
|
Returns a string representation of the start date
|
||||||
|
"""
|
||||||
|
val = 'unbounded'
|
||||||
|
if (startdt := self.startdt) is not None:
|
||||||
|
val = startdt.strftime(DATETIME_PRINT_FORMAT)
|
||||||
|
return val
|
||||||
|
|
||||||
|
@property
|
||||||
|
def stop_fmt(self) -> str:
|
||||||
|
"""
|
||||||
|
Returns a string representation of the stop date
|
||||||
|
"""
|
||||||
|
val = 'unbounded'
|
||||||
|
if (stopdt := self.stopdt) is not None:
|
||||||
|
val = stopdt.strftime(DATETIME_PRINT_FORMAT)
|
||||||
|
return val
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
"""Override the default Equals behavior"""
|
"""Override the default Equals behavior"""
|
||||||
return (self.starttype == other.starttype and self.stoptype == other.stoptype
|
return (self.starttype == other.starttype and self.stoptype == other.stoptype
|
||||||
|
@ -159,6 +159,7 @@ CONF_SCHEMA = {
|
|||||||
'ignore_buying_expired_candle_after': {'type': 'number'},
|
'ignore_buying_expired_candle_after': {'type': 'number'},
|
||||||
'trading_mode': {'type': 'string', 'enum': TRADING_MODES},
|
'trading_mode': {'type': 'string', 'enum': TRADING_MODES},
|
||||||
'margin_mode': {'type': 'string', 'enum': MARGIN_MODES},
|
'margin_mode': {'type': 'string', 'enum': MARGIN_MODES},
|
||||||
|
'reduce_df_footprint': {'type': 'boolean', 'default': False},
|
||||||
'liquidation_buffer': {'type': 'number', 'minimum': 0.0, 'maximum': 0.99},
|
'liquidation_buffer': {'type': 'number', 'minimum': 0.0, 'maximum': 0.99},
|
||||||
'backtest_breakdown': {
|
'backtest_breakdown': {
|
||||||
'type': 'array',
|
'type': 'array',
|
||||||
@ -511,6 +512,7 @@ CONF_SCHEMA = {
|
|||||||
'minimum': 0,
|
'minimum': 0,
|
||||||
'maximum': 65535
|
'maximum': 65535
|
||||||
},
|
},
|
||||||
|
'secure': {'type': 'boolean', 'default': False},
|
||||||
'ws_token': {'type': 'string'},
|
'ws_token': {'type': 'string'},
|
||||||
},
|
},
|
||||||
'required': ['name', 'host', 'ws_token']
|
'required': ['name', 'host', 'ws_token']
|
||||||
@ -542,7 +544,7 @@ CONF_SCHEMA = {
|
|||||||
"keras": {"type": "boolean", "default": False},
|
"keras": {"type": "boolean", "default": False},
|
||||||
"write_metrics_to_disk": {"type": "boolean", "default": False},
|
"write_metrics_to_disk": {"type": "boolean", "default": False},
|
||||||
"purge_old_models": {"type": "boolean", "default": True},
|
"purge_old_models": {"type": "boolean", "default": True},
|
||||||
"conv_width": {"type": "integer", "default": 2},
|
"conv_width": {"type": "integer", "default": 1},
|
||||||
"train_period_days": {"type": "integer", "default": 0},
|
"train_period_days": {"type": "integer", "default": 0},
|
||||||
"backtest_period_days": {"type": "number", "default": 7},
|
"backtest_period_days": {"type": "number", "default": 7},
|
||||||
"identifier": {"type": "string", "default": "example"},
|
"identifier": {"type": "string", "default": "example"},
|
||||||
@ -576,9 +578,27 @@ CONF_SCHEMA = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"model_training_parameters": {
|
"model_training_parameters": {
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"rl_config": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"n_estimators": {"type": "integer", "default": 1000}
|
"train_cycles": {"type": "integer"},
|
||||||
|
"max_trade_duration_candles": {"type": "integer"},
|
||||||
|
"add_state_info": {"type": "boolean", "default": False},
|
||||||
|
"max_training_drawdown_pct": {"type": "number", "default": 0.02},
|
||||||
|
"cpu_count": {"type": "integer", "default": 1},
|
||||||
|
"model_type": {"type": "string", "default": "PPO"},
|
||||||
|
"policy_type": {"type": "string", "default": "MlpPolicy"},
|
||||||
|
"net_arch": {"type": "array", "default": [128, 128]},
|
||||||
|
"randomize_startinng_position": {"type": "boolean", "default": False},
|
||||||
|
"model_reward_parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"rr": {"type": "number", "default": 1},
|
||||||
|
"profit_aim": {"type": "number", "default": 0.025}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -26,7 +26,7 @@ BT_DATA_COLUMNS = ['pair', 'stake_amount', 'amount', 'open_date', 'close_date',
|
|||||||
'profit_ratio', 'profit_abs', 'exit_reason',
|
'profit_ratio', 'profit_abs', 'exit_reason',
|
||||||
'initial_stop_loss_abs', 'initial_stop_loss_ratio', 'stop_loss_abs',
|
'initial_stop_loss_abs', 'initial_stop_loss_ratio', 'stop_loss_abs',
|
||||||
'stop_loss_ratio', 'min_rate', 'max_rate', 'is_open', 'enter_tag',
|
'stop_loss_ratio', 'min_rate', 'max_rate', 'is_open', 'enter_tag',
|
||||||
'is_short', 'open_timestamp', 'close_timestamp', 'orders'
|
'leverage', 'is_short', 'open_timestamp', 'close_timestamp', 'orders'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -280,6 +280,8 @@ def load_backtest_data(filename: Union[Path, str], strategy: Optional[str] = Non
|
|||||||
# Compatibility support for pre short Columns
|
# Compatibility support for pre short Columns
|
||||||
if 'is_short' not in df.columns:
|
if 'is_short' not in df.columns:
|
||||||
df['is_short'] = 0
|
df['is_short'] = 0
|
||||||
|
if 'leverage' not in df.columns:
|
||||||
|
df['leverage'] = 1.0
|
||||||
if 'enter_tag' not in df.columns:
|
if 'enter_tag' not in df.columns:
|
||||||
df['enter_tag'] = df['buy_tag']
|
df['enter_tag'] = df['buy_tag']
|
||||||
df = df.drop(['buy_tag'], axis=1)
|
df = df.drop(['buy_tag'], axis=1)
|
||||||
|
@ -3,10 +3,10 @@ Functions to convert data from one format to another
|
|||||||
"""
|
"""
|
||||||
import itertools
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, timezone
|
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from pandas import DataFrame, to_datetime
|
from pandas import DataFrame, to_datetime
|
||||||
|
|
||||||
@ -176,11 +176,9 @@ def trim_dataframe(df: DataFrame, timerange, df_date_col: str = 'date',
|
|||||||
df = df.iloc[startup_candles:, :]
|
df = df.iloc[startup_candles:, :]
|
||||||
else:
|
else:
|
||||||
if timerange.starttype == 'date':
|
if timerange.starttype == 'date':
|
||||||
start = datetime.fromtimestamp(timerange.startts, tz=timezone.utc)
|
df = df.loc[df[df_date_col] >= timerange.startdt, :]
|
||||||
df = df.loc[df[df_date_col] >= start, :]
|
|
||||||
if timerange.stoptype == 'date':
|
if timerange.stoptype == 'date':
|
||||||
stop = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc)
|
df = df.loc[df[df_date_col] <= timerange.stopdt, :]
|
||||||
df = df.loc[df[df_date_col] <= stop, :]
|
|
||||||
return df
|
return df
|
||||||
|
|
||||||
|
|
||||||
@ -356,3 +354,29 @@ def convert_ohlcv_format(
|
|||||||
if erase and convert_from != convert_to:
|
if erase and convert_from != convert_to:
|
||||||
logger.info(f"Deleting source data for {pair} / {timeframe}")
|
logger.info(f"Deleting source data for {pair} / {timeframe}")
|
||||||
src.ohlcv_purge(pair=pair, timeframe=timeframe, candle_type=candle_type)
|
src.ohlcv_purge(pair=pair, timeframe=timeframe, candle_type=candle_type)
|
||||||
|
|
||||||
|
|
||||||
|
def reduce_dataframe_footprint(df: DataFrame) -> DataFrame:
|
||||||
|
"""
|
||||||
|
Ensure all values are float32 in the incoming dataframe.
|
||||||
|
:param df: Dataframe to be converted to float/int 32s
|
||||||
|
:return: Dataframe converted to float/int 32s
|
||||||
|
"""
|
||||||
|
|
||||||
|
logger.debug(f"Memory usage of dataframe is "
|
||||||
|
f"{df.memory_usage().sum() / 1024**2:.2f} MB")
|
||||||
|
|
||||||
|
df_dtypes = df.dtypes
|
||||||
|
for column, dtype in df_dtypes.items():
|
||||||
|
if column in ['open', 'high', 'low', 'close', 'volume']:
|
||||||
|
continue
|
||||||
|
if dtype == np.float64:
|
||||||
|
df_dtypes[column] = np.float32
|
||||||
|
elif dtype == np.int64:
|
||||||
|
df_dtypes[column] = np.int32
|
||||||
|
df = df.astype(df_dtypes)
|
||||||
|
|
||||||
|
logger.debug(f"Memory usage after optimization is: "
|
||||||
|
f"{df.memory_usage().sum() / 1024**2:.2f} MB")
|
||||||
|
|
||||||
|
return df
|
||||||
|
@ -104,13 +104,15 @@ class DataProvider:
|
|||||||
def _emit_df(
|
def _emit_df(
|
||||||
self,
|
self,
|
||||||
pair_key: PairWithTimeframe,
|
pair_key: PairWithTimeframe,
|
||||||
dataframe: DataFrame
|
dataframe: DataFrame,
|
||||||
|
new_candle: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Send this dataframe as an ANALYZED_DF message to RPC
|
Send this dataframe as an ANALYZED_DF message to RPC
|
||||||
|
|
||||||
:param pair_key: PairWithTimeframe tuple
|
:param pair_key: PairWithTimeframe tuple
|
||||||
:param data: Tuple containing the DataFrame and the datetime it was cached
|
:param dataframe: Dataframe to emit
|
||||||
|
:param new_candle: This is a new candle
|
||||||
"""
|
"""
|
||||||
if self.__rpc:
|
if self.__rpc:
|
||||||
self.__rpc.send_msg(
|
self.__rpc.send_msg(
|
||||||
@ -123,6 +125,11 @@ class DataProvider:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
if new_candle:
|
||||||
|
self.__rpc.send_msg({
|
||||||
|
'type': RPCMessageType.NEW_CANDLE,
|
||||||
|
'data': pair_key,
|
||||||
|
})
|
||||||
|
|
||||||
def _add_external_df(
|
def _add_external_df(
|
||||||
self,
|
self,
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Optional
|
|
||||||
|
|
||||||
import joblib
|
import joblib
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
|
|
||||||
|
from freqtrade.configuration import TimeRange
|
||||||
|
from freqtrade.constants import Config
|
||||||
from freqtrade.data.btanalysis import (get_latest_backtest_filename, load_backtest_data,
|
from freqtrade.data.btanalysis import (get_latest_backtest_filename, load_backtest_data,
|
||||||
load_backtest_stats)
|
load_backtest_stats)
|
||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
@ -152,37 +153,55 @@ def _do_group_table_output(bigdf, glist):
|
|||||||
logger.warning("Invalid group mask specified.")
|
logger.warning("Invalid group mask specified.")
|
||||||
|
|
||||||
|
|
||||||
def _print_results(analysed_trades, stratname, analysis_groups,
|
def _select_rows_within_dates(df, timerange=None, df_date_col: str = 'date'):
|
||||||
enter_reason_list, exit_reason_list,
|
if timerange:
|
||||||
indicator_list, columns=None):
|
if timerange.starttype == 'date':
|
||||||
if columns is None:
|
df = df.loc[(df[df_date_col] >= timerange.startdt)]
|
||||||
columns = ['pair', 'open_date', 'close_date', 'profit_abs', 'enter_reason', 'exit_reason']
|
if timerange.stoptype == 'date':
|
||||||
|
df = df.loc[(df[df_date_col] < timerange.stopdt)]
|
||||||
|
return df
|
||||||
|
|
||||||
bigdf = pd.DataFrame()
|
|
||||||
for pair, trades in analysed_trades[stratname].items():
|
|
||||||
bigdf = pd.concat([bigdf, trades], ignore_index=True)
|
|
||||||
|
|
||||||
if bigdf.shape[0] > 0 and ('enter_reason' in bigdf.columns):
|
|
||||||
if analysis_groups:
|
|
||||||
_do_group_table_output(bigdf, analysis_groups)
|
|
||||||
|
|
||||||
|
def _select_rows_by_tags(df, enter_reason_list, exit_reason_list):
|
||||||
if enter_reason_list and "all" not in enter_reason_list:
|
if enter_reason_list and "all" not in enter_reason_list:
|
||||||
bigdf = bigdf.loc[(bigdf['enter_reason'].isin(enter_reason_list))]
|
df = df.loc[(df['enter_reason'].isin(enter_reason_list))]
|
||||||
|
|
||||||
if exit_reason_list and "all" not in exit_reason_list:
|
if exit_reason_list and "all" not in exit_reason_list:
|
||||||
bigdf = bigdf.loc[(bigdf['exit_reason'].isin(exit_reason_list))]
|
df = df.loc[(df['exit_reason'].isin(exit_reason_list))]
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_results(analysed_trades, stratname,
|
||||||
|
enter_reason_list, exit_reason_list,
|
||||||
|
timerange=None):
|
||||||
|
res_df = pd.DataFrame()
|
||||||
|
for pair, trades in analysed_trades[stratname].items():
|
||||||
|
res_df = pd.concat([res_df, trades], ignore_index=True)
|
||||||
|
|
||||||
|
res_df = _select_rows_within_dates(res_df, timerange)
|
||||||
|
|
||||||
|
if res_df is not None and res_df.shape[0] > 0 and ('enter_reason' in res_df.columns):
|
||||||
|
res_df = _select_rows_by_tags(res_df, enter_reason_list, exit_reason_list)
|
||||||
|
|
||||||
|
return res_df
|
||||||
|
|
||||||
|
|
||||||
|
def print_results(res_df, analysis_groups, indicator_list):
|
||||||
|
if res_df.shape[0] > 0:
|
||||||
|
if analysis_groups:
|
||||||
|
_do_group_table_output(res_df, analysis_groups)
|
||||||
|
|
||||||
if "all" in indicator_list:
|
if "all" in indicator_list:
|
||||||
print(bigdf)
|
print(res_df)
|
||||||
elif indicator_list is not None:
|
elif indicator_list is not None:
|
||||||
available_inds = []
|
available_inds = []
|
||||||
for ind in indicator_list:
|
for ind in indicator_list:
|
||||||
if ind in bigdf:
|
if ind in res_df:
|
||||||
available_inds.append(ind)
|
available_inds.append(ind)
|
||||||
ilist = ["pair", "enter_reason", "exit_reason"] + available_inds
|
ilist = ["pair", "enter_reason", "exit_reason"] + available_inds
|
||||||
_print_table(bigdf[ilist], sortcols=['exit_reason'], show_index=False)
|
_print_table(res_df[ilist], sortcols=['exit_reason'], show_index=False)
|
||||||
else:
|
else:
|
||||||
print("\\_ No trades to show")
|
print("\\No trades to show")
|
||||||
|
|
||||||
|
|
||||||
def _print_table(df, sortcols=None, show_index=False):
|
def _print_table(df, sortcols=None, show_index=False):
|
||||||
@ -201,26 +220,33 @@ def _print_table(df, sortcols=None, show_index=False):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def process_entry_exit_reasons(backtest_dir: Path,
|
def process_entry_exit_reasons(config: Config):
|
||||||
pairlist: List[str],
|
|
||||||
analysis_groups: Optional[List[str]] = ["0", "1", "2"],
|
|
||||||
enter_reason_list: Optional[List[str]] = ["all"],
|
|
||||||
exit_reason_list: Optional[List[str]] = ["all"],
|
|
||||||
indicator_list: Optional[List[str]] = []):
|
|
||||||
try:
|
try:
|
||||||
backtest_stats = load_backtest_stats(backtest_dir)
|
analysis_groups = config.get('analysis_groups', [])
|
||||||
|
enter_reason_list = config.get('enter_reason_list', ["all"])
|
||||||
|
exit_reason_list = config.get('exit_reason_list', ["all"])
|
||||||
|
indicator_list = config.get('indicator_list', [])
|
||||||
|
|
||||||
|
timerange = TimeRange.parse_timerange(None if config.get(
|
||||||
|
'timerange') is None else str(config.get('timerange')))
|
||||||
|
|
||||||
|
backtest_stats = load_backtest_stats(config['exportfilename'])
|
||||||
|
|
||||||
for strategy_name, results in backtest_stats['strategy'].items():
|
for strategy_name, results in backtest_stats['strategy'].items():
|
||||||
trades = load_backtest_data(backtest_dir, strategy_name)
|
trades = load_backtest_data(config['exportfilename'], strategy_name)
|
||||||
|
|
||||||
if not trades.empty:
|
if not trades.empty:
|
||||||
signal_candles = _load_signal_candles(backtest_dir)
|
signal_candles = _load_signal_candles(config['exportfilename'])
|
||||||
analysed_trades_dict = _process_candles_and_indicators(pairlist, strategy_name,
|
analysed_trades_dict = _process_candles_and_indicators(
|
||||||
|
config['exchange']['pair_whitelist'], strategy_name,
|
||||||
trades, signal_candles)
|
trades, signal_candles)
|
||||||
_print_results(analysed_trades_dict,
|
|
||||||
strategy_name,
|
res_df = prepare_results(analysed_trades_dict, strategy_name,
|
||||||
|
enter_reason_list, exit_reason_list,
|
||||||
|
timerange=timerange)
|
||||||
|
|
||||||
|
print_results(res_df,
|
||||||
analysis_groups,
|
analysis_groups,
|
||||||
enter_reason_list,
|
|
||||||
exit_reason_list,
|
|
||||||
indicator_list)
|
indicator_list)
|
||||||
|
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import operator
|
import operator
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Optional, Tuple
|
from typing import Dict, List, Optional, Tuple
|
||||||
|
|
||||||
@ -160,9 +160,9 @@ def _load_cached_data_for_updating(
|
|||||||
end = None
|
end = None
|
||||||
if timerange:
|
if timerange:
|
||||||
if timerange.starttype == 'date':
|
if timerange.starttype == 'date':
|
||||||
start = datetime.fromtimestamp(timerange.startts, tz=timezone.utc)
|
start = timerange.startdt
|
||||||
if timerange.stoptype == 'date':
|
if timerange.stoptype == 'date':
|
||||||
end = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc)
|
end = timerange.stopdt
|
||||||
|
|
||||||
# Intentionally don't pass timerange in - since we need to load the full dataset.
|
# Intentionally don't pass timerange in - since we need to load the full dataset.
|
||||||
data = data_handler.ohlcv_load(pair, timeframe=timeframe,
|
data = data_handler.ohlcv_load(pair, timeframe=timeframe,
|
||||||
|
@ -102,6 +102,11 @@ class IDataHandler(ABC):
|
|||||||
:return: (min, max)
|
:return: (min, max)
|
||||||
"""
|
"""
|
||||||
data = self._ohlcv_load(pair, timeframe, None, candle_type)
|
data = self._ohlcv_load(pair, timeframe, None, candle_type)
|
||||||
|
if data.empty:
|
||||||
|
return (
|
||||||
|
datetime.fromtimestamp(0, tz=timezone.utc),
|
||||||
|
datetime.fromtimestamp(0, tz=timezone.utc)
|
||||||
|
)
|
||||||
return data.iloc[0]['date'].to_pydatetime(), data.iloc[-1]['date'].to_pydatetime()
|
return data.iloc[0]['date'].to_pydatetime(), data.iloc[-1]['date'].to_pydatetime()
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@ -361,13 +366,11 @@ class IDataHandler(ABC):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if timerange.starttype == 'date':
|
if timerange.starttype == 'date':
|
||||||
start = datetime.fromtimestamp(timerange.startts, tz=timezone.utc)
|
if pairdata.iloc[0]['date'] > timerange.startdt:
|
||||||
if pairdata.iloc[0]['date'] > start:
|
|
||||||
logger.warning(f"{pair}, {candle_type}, {timeframe}, "
|
logger.warning(f"{pair}, {candle_type}, {timeframe}, "
|
||||||
f"data starts at {pairdata.iloc[0]['date']:%Y-%m-%d %H:%M:%S}")
|
f"data starts at {pairdata.iloc[0]['date']:%Y-%m-%d %H:%M:%S}")
|
||||||
if timerange.stoptype == 'date':
|
if timerange.stoptype == 'date':
|
||||||
stop = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc)
|
if pairdata.iloc[-1]['date'] < timerange.stopdt:
|
||||||
if pairdata.iloc[-1]['date'] < stop:
|
|
||||||
logger.warning(f"{pair}, {candle_type}, {timeframe}, "
|
logger.warning(f"{pair}, {candle_type}, {timeframe}, "
|
||||||
f"data ends at {pairdata.iloc[-1]['date']:%Y-%m-%d %H:%M:%S}")
|
f"data ends at {pairdata.iloc[-1]['date']:%Y-%m-%d %H:%M:%S}")
|
||||||
|
|
||||||
|
@ -392,7 +392,7 @@ class Edge:
|
|||||||
# Returning a list of pairs in order of "expectancy"
|
# Returning a list of pairs in order of "expectancy"
|
||||||
return final
|
return final
|
||||||
|
|
||||||
def _find_trades_for_stoploss_range(self, df, pair, stoploss_range):
|
def _find_trades_for_stoploss_range(self, df, pair: str, stoploss_range) -> list:
|
||||||
buy_column = df['enter_long'].values
|
buy_column = df['enter_long'].values
|
||||||
sell_column = df['exit_long'].values
|
sell_column = df['exit_long'].values
|
||||||
date_column = df['date'].values
|
date_column = df['date'].values
|
||||||
@ -407,7 +407,7 @@ class Edge:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def _detect_next_stop_or_sell_point(self, buy_column, sell_column, date_column,
|
def _detect_next_stop_or_sell_point(self, buy_column, sell_column, date_column,
|
||||||
ohlc_columns, stoploss, pair):
|
ohlc_columns, stoploss, pair: str):
|
||||||
"""
|
"""
|
||||||
Iterate through ohlc_columns in order to find the next trade
|
Iterate through ohlc_columns in order to find the next trade
|
||||||
Next trade opens from the first buy signal noticed to
|
Next trade opens from the first buy signal noticed to
|
||||||
|
@ -6,7 +6,7 @@ from freqtrade.enums.exittype import ExitType
|
|||||||
from freqtrade.enums.hyperoptstate import HyperoptState
|
from freqtrade.enums.hyperoptstate import HyperoptState
|
||||||
from freqtrade.enums.marginmode import MarginMode
|
from freqtrade.enums.marginmode import MarginMode
|
||||||
from freqtrade.enums.ordertypevalue import OrderTypeValues
|
from freqtrade.enums.ordertypevalue import OrderTypeValues
|
||||||
from freqtrade.enums.rpcmessagetype import RPCMessageType, RPCRequestType
|
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.runmode import NON_UTIL_MODES, OPTIMIZE_MODES, TRADING_MODES, RunMode
|
||||||
from freqtrade.enums.signaltype import SignalDirection, SignalTagType, SignalType
|
from freqtrade.enums.signaltype import SignalDirection, SignalTagType, SignalType
|
||||||
from freqtrade.enums.state import State
|
from freqtrade.enums.state import State
|
||||||
|
@ -21,6 +21,7 @@ class RPCMessageType(str, Enum):
|
|||||||
|
|
||||||
WHITELIST = 'whitelist'
|
WHITELIST = 'whitelist'
|
||||||
ANALYZED_DF = 'analyzed_df'
|
ANALYZED_DF = 'analyzed_df'
|
||||||
|
NEW_CANDLE = 'new_candle'
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.value
|
return self.value
|
||||||
@ -35,3 +36,6 @@ class RPCRequestType(str, Enum):
|
|||||||
|
|
||||||
WHITELIST = 'whitelist'
|
WHITELIST = 'whitelist'
|
||||||
ANALYZED_DF = 'analyzed_df'
|
ANALYZED_DF = 'analyzed_df'
|
||||||
|
|
||||||
|
|
||||||
|
NO_ECHO_MESSAGES = (RPCMessageType.ANALYZED_DF, RPCMessageType.WHITELIST, RPCMessageType.NEW_CANDLE)
|
||||||
|
@ -18,7 +18,6 @@ from freqtrade.exchange.exchange_utils import (amount_to_contract_precision, amo
|
|||||||
timeframe_to_next_date, timeframe_to_prev_date,
|
timeframe_to_next_date, timeframe_to_prev_date,
|
||||||
timeframe_to_seconds, validate_exchange,
|
timeframe_to_seconds, validate_exchange,
|
||||||
validate_exchanges)
|
validate_exchanges)
|
||||||
from freqtrade.exchange.ftx import Ftx
|
|
||||||
from freqtrade.exchange.gateio import Gateio
|
from freqtrade.exchange.gateio import Gateio
|
||||||
from freqtrade.exchange.hitbtc import Hitbtc
|
from freqtrade.exchange.hitbtc import Hitbtc
|
||||||
from freqtrade.exchange.huobi import Huobi
|
from freqtrade.exchange.huobi import Huobi
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -20,8 +20,12 @@ class Bybit(Exchange):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
_ft_has: Dict = {
|
_ft_has: Dict = {
|
||||||
"ohlcv_candle_limit": 200,
|
"ohlcv_candle_limit": 1000,
|
||||||
"ccxt_futures_name": "linear"
|
"ccxt_futures_name": "linear",
|
||||||
|
"ohlcv_has_history": False,
|
||||||
|
}
|
||||||
|
_ft_has_futures: Dict = {
|
||||||
|
"ohlcv_has_history": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
_supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [
|
_supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [
|
||||||
|
@ -52,7 +52,6 @@ MAP_EXCHANGE_CHILDCLASS = {
|
|||||||
SUPPORTED_EXCHANGES = [
|
SUPPORTED_EXCHANGES = [
|
||||||
'binance',
|
'binance',
|
||||||
'bittrex',
|
'bittrex',
|
||||||
'ftx',
|
|
||||||
'gateio',
|
'gateio',
|
||||||
'huobi',
|
'huobi',
|
||||||
'kraken',
|
'kraken',
|
||||||
|
@ -1689,6 +1689,17 @@ class Exchange:
|
|||||||
@retrier
|
@retrier
|
||||||
def get_fee(self, symbol: str, type: str = '', side: str = '', amount: float = 1,
|
def get_fee(self, symbol: str, type: str = '', side: str = '', amount: float = 1,
|
||||||
price: float = 1, taker_or_maker: MakerTaker = 'maker') -> float:
|
price: float = 1, taker_or_maker: MakerTaker = 'maker') -> float:
|
||||||
|
"""
|
||||||
|
Retrieve fee from exchange
|
||||||
|
:param symbol: Pair
|
||||||
|
:param type: Type of order (market, limit, ...)
|
||||||
|
:param side: Side of order (buy, sell)
|
||||||
|
:param amount: Amount of order
|
||||||
|
:param price: Price of order
|
||||||
|
:param taker_or_maker: 'maker' or 'taker' (ignored if "type" is provided)
|
||||||
|
"""
|
||||||
|
if type and type == 'market':
|
||||||
|
taker_or_maker = 'taker'
|
||||||
try:
|
try:
|
||||||
if self._config['dry_run'] and self._config.get('fee', None) is not None:
|
if self._config['dry_run'] and self._config.get('fee', None) is not None:
|
||||||
return self._config['fee']
|
return self._config['fee']
|
||||||
|
@ -1,178 +0,0 @@
|
|||||||
""" FTX exchange subclass """
|
|
||||||
import logging
|
|
||||||
from typing import Any, Dict, List, Optional, Tuple
|
|
||||||
|
|
||||||
import ccxt
|
|
||||||
|
|
||||||
from freqtrade.constants import BuySell
|
|
||||||
from freqtrade.enums import MarginMode, TradingMode
|
|
||||||
from freqtrade.exceptions import (DDosProtection, InsufficientFundsError, InvalidOrderException,
|
|
||||||
OperationalException, TemporaryError)
|
|
||||||
from freqtrade.exchange import Exchange
|
|
||||||
from freqtrade.exchange.common import API_FETCH_ORDER_RETRY_COUNT, retrier
|
|
||||||
from freqtrade.misc import safe_value_fallback2
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class Ftx(Exchange):
|
|
||||||
|
|
||||||
_ft_has: Dict = {
|
|
||||||
"order_time_in_force": ['GTC', 'IOC', 'PO'],
|
|
||||||
"stoploss_on_exchange": True,
|
|
||||||
"ohlcv_candle_limit": 1500,
|
|
||||||
"ohlcv_require_since": True,
|
|
||||||
"ohlcv_volume_currency": "quote",
|
|
||||||
"mark_ohlcv_price": "index",
|
|
||||||
"mark_ohlcv_timeframe": "1h",
|
|
||||||
}
|
|
||||||
|
|
||||||
_supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [
|
|
||||||
# TradingMode.SPOT always supported and not required in this list
|
|
||||||
# (TradingMode.MARGIN, MarginMode.CROSS),
|
|
||||||
# (TradingMode.FUTURES, MarginMode.CROSS)
|
|
||||||
]
|
|
||||||
|
|
||||||
def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool:
|
|
||||||
"""
|
|
||||||
Verify stop_loss against stoploss-order value (limit or price)
|
|
||||||
Returns True if adjustment is necessary.
|
|
||||||
"""
|
|
||||||
return order['type'] == 'stop' and (
|
|
||||||
side == "sell" and stop_loss > float(order['price']) or
|
|
||||||
side == "buy" and stop_loss < float(order['price'])
|
|
||||||
)
|
|
||||||
|
|
||||||
@retrier(retries=0)
|
|
||||||
def stoploss(self, pair: str, amount: float, stop_price: float,
|
|
||||||
order_types: Dict, side: BuySell, leverage: float) -> Dict:
|
|
||||||
"""
|
|
||||||
Creates a stoploss order.
|
|
||||||
depending on order_types.stoploss configuration, uses 'market' or limit order.
|
|
||||||
|
|
||||||
Limit orders are defined by having orderPrice set, otherwise a market order is used.
|
|
||||||
"""
|
|
||||||
limit_price_pct = order_types.get('stoploss_on_exchange_limit_ratio', 0.99)
|
|
||||||
if side == "sell":
|
|
||||||
limit_rate = stop_price * limit_price_pct
|
|
||||||
else:
|
|
||||||
limit_rate = stop_price * (2 - limit_price_pct)
|
|
||||||
|
|
||||||
ordertype = "stop"
|
|
||||||
|
|
||||||
stop_price = self.price_to_precision(pair, stop_price)
|
|
||||||
|
|
||||||
if self._config['dry_run']:
|
|
||||||
dry_order = self.create_dry_run_order(
|
|
||||||
pair, ordertype, side, amount, stop_price, leverage, stop_loss=True)
|
|
||||||
return dry_order
|
|
||||||
|
|
||||||
try:
|
|
||||||
params = self._params.copy()
|
|
||||||
if order_types.get('stoploss', 'market') == 'limit':
|
|
||||||
# set orderPrice to place limit order, otherwise it's a market order
|
|
||||||
params['orderPrice'] = limit_rate
|
|
||||||
if self.trading_mode == TradingMode.FUTURES:
|
|
||||||
params.update({'reduceOnly': True})
|
|
||||||
|
|
||||||
params['stopPrice'] = stop_price
|
|
||||||
amount = self.amount_to_precision(pair, amount)
|
|
||||||
|
|
||||||
self._lev_prep(pair, leverage, side)
|
|
||||||
order = self._api.create_order(symbol=pair, type=ordertype, side=side,
|
|
||||||
amount=amount, params=params)
|
|
||||||
self._log_exchange_response('create_stoploss_order', order)
|
|
||||||
logger.info('stoploss order added for %s. '
|
|
||||||
'stop price: %s.', pair, stop_price)
|
|
||||||
return order
|
|
||||||
except ccxt.InsufficientFunds as e:
|
|
||||||
raise InsufficientFundsError(
|
|
||||||
f'Insufficient funds to create {ordertype} {side} order on market {pair}. '
|
|
||||||
f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. '
|
|
||||||
f'Message: {e}') from e
|
|
||||||
except ccxt.InvalidOrder as e:
|
|
||||||
raise InvalidOrderException(
|
|
||||||
f'Could not create {ordertype} {side} order on market {pair}. '
|
|
||||||
f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. '
|
|
||||||
f'Message: {e}') from e
|
|
||||||
except ccxt.DDoSProtection as e:
|
|
||||||
raise DDosProtection(e) from e
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
|
||||||
raise TemporaryError(
|
|
||||||
f'Could not place {side} order due to {e.__class__.__name__}. Message: {e}') from e
|
|
||||||
except ccxt.BaseError as e:
|
|
||||||
raise OperationalException(e) from e
|
|
||||||
|
|
||||||
@retrier(retries=API_FETCH_ORDER_RETRY_COUNT)
|
|
||||||
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:
|
|
||||||
orders = self._api.fetch_orders(pair, None, params={'type': 'stop'})
|
|
||||||
|
|
||||||
order = [order for order in orders if order['id'] == order_id]
|
|
||||||
self._log_exchange_response('fetch_stoploss_order', order)
|
|
||||||
if len(order) == 1:
|
|
||||||
if order[0].get('status') == 'closed':
|
|
||||||
# Trigger order was triggered ...
|
|
||||||
real_order_id: Optional[str] = order[0].get('info', {}).get('orderId')
|
|
||||||
# OrderId may be None for stoploss-market orders
|
|
||||||
# So we need to get it through the endpoint
|
|
||||||
# /conditional_orders/{conditional_order_id}/triggers
|
|
||||||
if not real_order_id:
|
|
||||||
res = self._api.privateGetConditionalOrdersConditionalOrderIdTriggers(
|
|
||||||
params={'conditional_order_id': order_id})
|
|
||||||
self._log_exchange_response('fetch_stoploss_order2', res)
|
|
||||||
real_order_id = res['result'][0]['orderId'] if res.get(
|
|
||||||
'result', []) else None
|
|
||||||
|
|
||||||
if real_order_id:
|
|
||||||
order1 = self._api.fetch_order(real_order_id, pair)
|
|
||||||
self._log_exchange_response('fetch_stoploss_order1', order1)
|
|
||||||
# Fake type to stop - as this was really a stop order.
|
|
||||||
order1['id_stop'] = order1['id']
|
|
||||||
order1['id'] = order_id
|
|
||||||
order1['type'] = 'stop'
|
|
||||||
order1['status_stop'] = 'triggered'
|
|
||||||
return order1
|
|
||||||
|
|
||||||
return order[0]
|
|
||||||
else:
|
|
||||||
raise InvalidOrderException(f"Could not get stoploss order for id {order_id}")
|
|
||||||
|
|
||||||
except ccxt.InvalidOrder as e:
|
|
||||||
raise InvalidOrderException(
|
|
||||||
f'Tried to get an invalid order (id: {order_id}). Message: {e}') from e
|
|
||||||
except ccxt.DDoSProtection as e:
|
|
||||||
raise DDosProtection(e) from e
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
|
||||||
raise TemporaryError(
|
|
||||||
f'Could not get order due to {e.__class__.__name__}. Message: {e}') from e
|
|
||||||
except ccxt.BaseError as e:
|
|
||||||
raise OperationalException(e) from e
|
|
||||||
|
|
||||||
@retrier
|
|
||||||
def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict:
|
|
||||||
if self._config['dry_run']:
|
|
||||||
return {}
|
|
||||||
try:
|
|
||||||
order = self._api.cancel_order(order_id, pair, params={'type': 'stop'})
|
|
||||||
self._log_exchange_response('cancel_stoploss_order', order)
|
|
||||||
return order
|
|
||||||
except ccxt.InvalidOrder as e:
|
|
||||||
raise InvalidOrderException(
|
|
||||||
f'Could not cancel order. Message: {e}') from e
|
|
||||||
except ccxt.DDoSProtection as e:
|
|
||||||
raise DDosProtection(e) from e
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
|
||||||
raise TemporaryError(
|
|
||||||
f'Could not cancel order due to {e.__class__.__name__}. Message: {e}') from e
|
|
||||||
except ccxt.BaseError as e:
|
|
||||||
raise OperationalException(e) from e
|
|
||||||
|
|
||||||
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']
|
|
@ -218,3 +218,19 @@ class Kraken(Exchange):
|
|||||||
fees = sum(df['open_fund'] * df['open_mark'] * amount * time_in_ratio)
|
fees = sum(df['open_fund'] * df['open_mark'] * amount * time_in_ratio)
|
||||||
|
|
||||||
return fees if is_short else -fees
|
return fees if is_short else -fees
|
||||||
|
|
||||||
|
def _trades_contracts_to_amount(self, trades: List) -> List:
|
||||||
|
"""
|
||||||
|
Fix "last" id issue for kraken data downloads
|
||||||
|
This whole override can probably be removed once the following
|
||||||
|
issue is closed in ccxt: https://github.com/ccxt/ccxt/issues/15827
|
||||||
|
"""
|
||||||
|
super()._trades_contracts_to_amount(trades)
|
||||||
|
if (
|
||||||
|
len(trades) > 0
|
||||||
|
and isinstance(trades[-1].get('info'), list)
|
||||||
|
and len(trades[-1].get('info', [])) > 7
|
||||||
|
):
|
||||||
|
|
||||||
|
trades[-1]['id'] = trades[-1].get('info', [])[-1]
|
||||||
|
return trades
|
||||||
|
141
freqtrade/freqai/RL/Base4ActionRLEnv.py
Normal file
141
freqtrade/freqai/RL/Base4ActionRLEnv.py
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
import logging
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
from gym import spaces
|
||||||
|
|
||||||
|
from freqtrade.freqai.RL.BaseEnvironment import BaseEnvironment, Positions
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Actions(Enum):
|
||||||
|
Neutral = 0
|
||||||
|
Exit = 1
|
||||||
|
Long_enter = 2
|
||||||
|
Short_enter = 3
|
||||||
|
|
||||||
|
|
||||||
|
class Base4ActionRLEnv(BaseEnvironment):
|
||||||
|
"""
|
||||||
|
Base class for a 4 action environment
|
||||||
|
"""
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
self.actions = Actions
|
||||||
|
|
||||||
|
def set_action_space(self):
|
||||||
|
self.action_space = spaces.Discrete(len(Actions))
|
||||||
|
|
||||||
|
def step(self, action: int):
|
||||||
|
"""
|
||||||
|
Logic for a single step (incrementing one candle in time)
|
||||||
|
by the agent
|
||||||
|
:param: action: int = the action type that the agent plans
|
||||||
|
to take for the current step.
|
||||||
|
:returns:
|
||||||
|
observation = current state of environment
|
||||||
|
step_reward = the reward from `calculate_reward()`
|
||||||
|
_done = if the agent "died" or if the candles finished
|
||||||
|
info = dict passed back to openai gym lib
|
||||||
|
"""
|
||||||
|
self._done = False
|
||||||
|
self._current_tick += 1
|
||||||
|
|
||||||
|
if self._current_tick == self._end_tick:
|
||||||
|
self._done = True
|
||||||
|
|
||||||
|
self._update_unrealized_total_profit()
|
||||||
|
|
||||||
|
step_reward = self.calculate_reward(action)
|
||||||
|
self.total_reward += step_reward
|
||||||
|
|
||||||
|
trade_type = None
|
||||||
|
if self.is_tradesignal(action):
|
||||||
|
"""
|
||||||
|
Action: Neutral, position: Long -> Close Long
|
||||||
|
Action: Neutral, position: Short -> Close Short
|
||||||
|
|
||||||
|
Action: Long, position: Neutral -> Open Long
|
||||||
|
Action: Long, position: Short -> Close Short and Open Long
|
||||||
|
|
||||||
|
Action: Short, position: Neutral -> Open Short
|
||||||
|
Action: Short, position: Long -> Close Long and Open Short
|
||||||
|
"""
|
||||||
|
|
||||||
|
if action == Actions.Neutral.value:
|
||||||
|
self._position = Positions.Neutral
|
||||||
|
trade_type = "neutral"
|
||||||
|
self._last_trade_tick = None
|
||||||
|
elif action == Actions.Long_enter.value:
|
||||||
|
self._position = Positions.Long
|
||||||
|
trade_type = "long"
|
||||||
|
self._last_trade_tick = self._current_tick
|
||||||
|
elif action == Actions.Short_enter.value:
|
||||||
|
self._position = Positions.Short
|
||||||
|
trade_type = "short"
|
||||||
|
self._last_trade_tick = self._current_tick
|
||||||
|
elif action == Actions.Exit.value:
|
||||||
|
self._update_total_profit()
|
||||||
|
self._position = Positions.Neutral
|
||||||
|
trade_type = "neutral"
|
||||||
|
self._last_trade_tick = None
|
||||||
|
else:
|
||||||
|
print("case not defined")
|
||||||
|
|
||||||
|
if trade_type is not None:
|
||||||
|
self.trade_history.append(
|
||||||
|
{'price': self.current_price(), 'index': self._current_tick,
|
||||||
|
'type': trade_type})
|
||||||
|
|
||||||
|
if self._total_profit < 1 - self.rl_config.get('max_training_drawdown_pct', 0.8):
|
||||||
|
self._done = True
|
||||||
|
|
||||||
|
self._position_history.append(self._position)
|
||||||
|
|
||||||
|
info = dict(
|
||||||
|
tick=self._current_tick,
|
||||||
|
action=action,
|
||||||
|
total_reward=self.total_reward,
|
||||||
|
total_profit=self._total_profit,
|
||||||
|
position=self._position.value,
|
||||||
|
trade_duration=self.get_trade_duration(),
|
||||||
|
current_profit_pct=self.get_unrealized_profit()
|
||||||
|
)
|
||||||
|
|
||||||
|
observation = self._get_observation()
|
||||||
|
|
||||||
|
self._update_history(info)
|
||||||
|
|
||||||
|
return observation, step_reward, self._done, info
|
||||||
|
|
||||||
|
def is_tradesignal(self, action: int) -> bool:
|
||||||
|
"""
|
||||||
|
Determine if the signal is a trade signal
|
||||||
|
e.g.: agent wants a Actions.Long_exit while it is in a Positions.short
|
||||||
|
"""
|
||||||
|
return not ((action == Actions.Neutral.value and self._position == Positions.Neutral) or
|
||||||
|
(action == Actions.Neutral.value and self._position == Positions.Short) or
|
||||||
|
(action == Actions.Neutral.value and self._position == Positions.Long) or
|
||||||
|
(action == Actions.Short_enter.value and self._position == Positions.Short) or
|
||||||
|
(action == Actions.Short_enter.value and self._position == Positions.Long) or
|
||||||
|
(action == Actions.Exit.value and self._position == Positions.Neutral) or
|
||||||
|
(action == Actions.Long_enter.value and self._position == Positions.Long) or
|
||||||
|
(action == Actions.Long_enter.value and self._position == Positions.Short))
|
||||||
|
|
||||||
|
def _is_valid(self, action: int) -> bool:
|
||||||
|
"""
|
||||||
|
Determine if the signal is valid.
|
||||||
|
e.g.: agent wants a Actions.Long_exit while it is in a Positions.short
|
||||||
|
"""
|
||||||
|
# Agent should only try to exit if it is in position
|
||||||
|
if action == Actions.Exit.value:
|
||||||
|
if self._position not in (Positions.Short, Positions.Long):
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Agent should only try to enter if it is not in position
|
||||||
|
if action in (Actions.Short_enter.value, Actions.Long_enter.value):
|
||||||
|
if self._position != Positions.Neutral:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
151
freqtrade/freqai/RL/Base5ActionRLEnv.py
Normal file
151
freqtrade/freqai/RL/Base5ActionRLEnv.py
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
import logging
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
from gym import spaces
|
||||||
|
|
||||||
|
from freqtrade.freqai.RL.BaseEnvironment import BaseEnvironment, Positions
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Actions(Enum):
|
||||||
|
Neutral = 0
|
||||||
|
Long_enter = 1
|
||||||
|
Long_exit = 2
|
||||||
|
Short_enter = 3
|
||||||
|
Short_exit = 4
|
||||||
|
|
||||||
|
|
||||||
|
class Base5ActionRLEnv(BaseEnvironment):
|
||||||
|
"""
|
||||||
|
Base class for a 5 action environment
|
||||||
|
"""
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
self.actions = Actions
|
||||||
|
|
||||||
|
def set_action_space(self):
|
||||||
|
self.action_space = spaces.Discrete(len(Actions))
|
||||||
|
|
||||||
|
def step(self, action: int):
|
||||||
|
"""
|
||||||
|
Logic for a single step (incrementing one candle in time)
|
||||||
|
by the agent
|
||||||
|
:param: action: int = the action type that the agent plans
|
||||||
|
to take for the current step.
|
||||||
|
:returns:
|
||||||
|
observation = current state of environment
|
||||||
|
step_reward = the reward from `calculate_reward()`
|
||||||
|
_done = if the agent "died" or if the candles finished
|
||||||
|
info = dict passed back to openai gym lib
|
||||||
|
"""
|
||||||
|
self._done = False
|
||||||
|
self._current_tick += 1
|
||||||
|
|
||||||
|
if self._current_tick == self._end_tick:
|
||||||
|
self._done = True
|
||||||
|
|
||||||
|
self._update_unrealized_total_profit()
|
||||||
|
step_reward = self.calculate_reward(action)
|
||||||
|
self.total_reward += step_reward
|
||||||
|
|
||||||
|
trade_type = None
|
||||||
|
if self.is_tradesignal(action):
|
||||||
|
"""
|
||||||
|
Action: Neutral, position: Long -> Close Long
|
||||||
|
Action: Neutral, position: Short -> Close Short
|
||||||
|
|
||||||
|
Action: Long, position: Neutral -> Open Long
|
||||||
|
Action: Long, position: Short -> Close Short and Open Long
|
||||||
|
|
||||||
|
Action: Short, position: Neutral -> Open Short
|
||||||
|
Action: Short, position: Long -> Close Long and Open Short
|
||||||
|
"""
|
||||||
|
|
||||||
|
if action == Actions.Neutral.value:
|
||||||
|
self._position = Positions.Neutral
|
||||||
|
trade_type = "neutral"
|
||||||
|
self._last_trade_tick = None
|
||||||
|
elif action == Actions.Long_enter.value:
|
||||||
|
self._position = Positions.Long
|
||||||
|
trade_type = "long"
|
||||||
|
self._last_trade_tick = self._current_tick
|
||||||
|
elif action == Actions.Short_enter.value:
|
||||||
|
self._position = Positions.Short
|
||||||
|
trade_type = "short"
|
||||||
|
self._last_trade_tick = self._current_tick
|
||||||
|
elif action == Actions.Long_exit.value:
|
||||||
|
self._update_total_profit()
|
||||||
|
self._position = Positions.Neutral
|
||||||
|
trade_type = "neutral"
|
||||||
|
self._last_trade_tick = None
|
||||||
|
elif action == Actions.Short_exit.value:
|
||||||
|
self._update_total_profit()
|
||||||
|
self._position = Positions.Neutral
|
||||||
|
trade_type = "neutral"
|
||||||
|
self._last_trade_tick = None
|
||||||
|
else:
|
||||||
|
print("case not defined")
|
||||||
|
|
||||||
|
if trade_type is not None:
|
||||||
|
self.trade_history.append(
|
||||||
|
{'price': self.current_price(), 'index': self._current_tick,
|
||||||
|
'type': trade_type})
|
||||||
|
|
||||||
|
if (self._total_profit < self.max_drawdown or
|
||||||
|
self._total_unrealized_profit < self.max_drawdown):
|
||||||
|
self._done = True
|
||||||
|
|
||||||
|
self._position_history.append(self._position)
|
||||||
|
|
||||||
|
info = dict(
|
||||||
|
tick=self._current_tick,
|
||||||
|
action=action,
|
||||||
|
total_reward=self.total_reward,
|
||||||
|
total_profit=self._total_profit,
|
||||||
|
position=self._position.value,
|
||||||
|
trade_duration=self.get_trade_duration(),
|
||||||
|
current_profit_pct=self.get_unrealized_profit()
|
||||||
|
)
|
||||||
|
|
||||||
|
observation = self._get_observation()
|
||||||
|
|
||||||
|
self._update_history(info)
|
||||||
|
|
||||||
|
return observation, step_reward, self._done, info
|
||||||
|
|
||||||
|
def is_tradesignal(self, action: int) -> bool:
|
||||||
|
"""
|
||||||
|
Determine if the signal is a trade signal
|
||||||
|
e.g.: agent wants a Actions.Long_exit while it is in a Positions.short
|
||||||
|
"""
|
||||||
|
return not ((action == Actions.Neutral.value and self._position == Positions.Neutral) or
|
||||||
|
(action == Actions.Neutral.value and self._position == Positions.Short) or
|
||||||
|
(action == Actions.Neutral.value and self._position == Positions.Long) or
|
||||||
|
(action == Actions.Short_enter.value and self._position == Positions.Short) or
|
||||||
|
(action == Actions.Short_enter.value and self._position == Positions.Long) or
|
||||||
|
(action == Actions.Short_exit.value and self._position == Positions.Long) or
|
||||||
|
(action == Actions.Short_exit.value and self._position == Positions.Neutral) or
|
||||||
|
(action == Actions.Long_enter.value and self._position == Positions.Long) or
|
||||||
|
(action == Actions.Long_enter.value and self._position == Positions.Short) or
|
||||||
|
(action == Actions.Long_exit.value and self._position == Positions.Short) or
|
||||||
|
(action == Actions.Long_exit.value and self._position == Positions.Neutral))
|
||||||
|
|
||||||
|
def _is_valid(self, action: int) -> bool:
|
||||||
|
# trade signal
|
||||||
|
"""
|
||||||
|
Determine if the signal is valid.
|
||||||
|
e.g.: agent wants a Actions.Long_exit while it is in a Positions.short
|
||||||
|
"""
|
||||||
|
# Agent should only try to exit if it is in position
|
||||||
|
if action in (Actions.Short_exit.value, Actions.Long_exit.value):
|
||||||
|
if self._position not in (Positions.Short, Positions.Long):
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Agent should only try to enter if it is not in position
|
||||||
|
if action in (Actions.Short_enter.value, Actions.Long_enter.value):
|
||||||
|
if self._position != Positions.Neutral:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
349
freqtrade/freqai/RL/BaseEnvironment.py
Normal file
349
freqtrade/freqai/RL/BaseEnvironment.py
Normal file
@ -0,0 +1,349 @@
|
|||||||
|
import logging
|
||||||
|
import random
|
||||||
|
from abc import abstractmethod
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Optional, Type
|
||||||
|
|
||||||
|
import gym
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
from gym import spaces
|
||||||
|
from gym.utils import seeding
|
||||||
|
from pandas import DataFrame
|
||||||
|
|
||||||
|
from freqtrade.data.dataprovider import DataProvider
|
||||||
|
from freqtrade.enums import RunMode
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseActions(Enum):
|
||||||
|
"""
|
||||||
|
Default action space, mostly used for type handling.
|
||||||
|
"""
|
||||||
|
Neutral = 0
|
||||||
|
Long_enter = 1
|
||||||
|
Long_exit = 2
|
||||||
|
Short_enter = 3
|
||||||
|
Short_exit = 4
|
||||||
|
|
||||||
|
|
||||||
|
class Positions(Enum):
|
||||||
|
Short = 0
|
||||||
|
Long = 1
|
||||||
|
Neutral = 0.5
|
||||||
|
|
||||||
|
def opposite(self):
|
||||||
|
return Positions.Short if self == Positions.Long else Positions.Long
|
||||||
|
|
||||||
|
|
||||||
|
class BaseEnvironment(gym.Env):
|
||||||
|
"""
|
||||||
|
Base class for environments. This class is agnostic to action count.
|
||||||
|
Inherited classes customize this to include varying action counts/types,
|
||||||
|
See RL/Base5ActionRLEnv.py and RL/Base4ActionRLEnv.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
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 = {},
|
||||||
|
dp: Optional[DataProvider] = None):
|
||||||
|
"""
|
||||||
|
Initializes the training/eval environment.
|
||||||
|
:param df: dataframe of features
|
||||||
|
:param prices: dataframe of prices to be used in the training environment
|
||||||
|
:param window_size: size of window (temporal) to pass to the agent
|
||||||
|
:param reward_kwargs: extra config settings assigned by user in `rl_config`
|
||||||
|
:param starting_point: start at edge of window or not
|
||||||
|
:param id: string id of the environment (used in backend for multiprocessed env)
|
||||||
|
:param seed: Sets the seed of the environment higher in the gym.Env object
|
||||||
|
:param config: Typical user configuration file
|
||||||
|
:param dp: dataprovider from freqtrade
|
||||||
|
"""
|
||||||
|
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.seed(seed)
|
||||||
|
self.reset_env(df, prices, window_size, reward_kwargs, starting_point)
|
||||||
|
self.max_drawdown = 1 - self.rl_config.get('max_training_drawdown_pct', 0.8)
|
||||||
|
self.compound_trades = config['stake_amount'] == 'unlimited'
|
||||||
|
if self.config.get('fee', None) is not None:
|
||||||
|
self.fee = self.config['fee']
|
||||||
|
elif dp is not None:
|
||||||
|
self.fee = dp._exchange.get_fee(symbol=dp.current_whitelist()[0]) # type: ignore
|
||||||
|
else:
|
||||||
|
self.fee = 0.0015
|
||||||
|
|
||||||
|
# set here to default 5Ac, but all children envs can override this
|
||||||
|
self.actions: Type[Enum] = BaseActions
|
||||||
|
self.custom_info: dict = {}
|
||||||
|
self.live: bool = False
|
||||||
|
if dp:
|
||||||
|
self.live = dp.runmode in (RunMode.DRY_RUN, RunMode.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.")
|
||||||
|
|
||||||
|
def reset_env(self, df: DataFrame, prices: DataFrame, window_size: int,
|
||||||
|
reward_kwargs: dict, starting_point=True):
|
||||||
|
"""
|
||||||
|
Resets the environment when the agent fails (in our case, if the drawdown
|
||||||
|
exceeds the user set max_training_drawdown_pct)
|
||||||
|
:param df: dataframe of features
|
||||||
|
:param prices: dataframe of prices to be used in the training environment
|
||||||
|
:param window_size: size of window (temporal) to pass to the agent
|
||||||
|
: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"]
|
||||||
|
|
||||||
|
# # spaces
|
||||||
|
if self.add_state_info:
|
||||||
|
self.total_features = self.signal_features.shape[1] + 3
|
||||||
|
else:
|
||||||
|
self.total_features = self.signal_features.shape[1]
|
||||||
|
self.shape = (window_size, self.total_features)
|
||||||
|
self.set_action_space()
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-1, high=1, shape=self.shape, dtype=np.float32)
|
||||||
|
|
||||||
|
# episode
|
||||||
|
self._start_tick: int = self.window_size
|
||||||
|
self._end_tick: int = len(self.prices) - 1
|
||||||
|
self._done: bool = False
|
||||||
|
self._current_tick: int = self._start_tick
|
||||||
|
self._last_trade_tick: Optional[int] = None
|
||||||
|
self._position = Positions.Neutral
|
||||||
|
self._position_history: list = [None]
|
||||||
|
self.total_reward: float = 0
|
||||||
|
self._total_profit: float = 1
|
||||||
|
self._total_unrealized_profit: float = 1
|
||||||
|
self.history: dict = {}
|
||||||
|
self.trade_history: list = []
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def set_action_space(self):
|
||||||
|
"""
|
||||||
|
Unique to the environment action count. Must be inherited.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def seed(self, seed: int = 1):
|
||||||
|
self.np_random, seed = seeding.np_random(seed)
|
||||||
|
return [seed]
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
"""
|
||||||
|
Reset is called at the beginning of every episode
|
||||||
|
"""
|
||||||
|
# custom_info is used for episodic reports and tensorboard logging
|
||||||
|
self.custom_info["Invalid"] = 0
|
||||||
|
self.custom_info["Hold"] = 0
|
||||||
|
self.custom_info["Unknown"] = 0
|
||||||
|
self.custom_info["pnl_factor"] = 0
|
||||||
|
self.custom_info["duration_factor"] = 0
|
||||||
|
self.custom_info["reward_exit"] = 0
|
||||||
|
self.custom_info["reward_hold"] = 0
|
||||||
|
for action in self.actions:
|
||||||
|
self.custom_info[f"{action.name}"] = 0
|
||||||
|
|
||||||
|
self._done = False
|
||||||
|
|
||||||
|
if self.starting_point is True:
|
||||||
|
if self.rl_config.get('randomize_starting_position', False):
|
||||||
|
length_of_data = int(self._end_tick / 4)
|
||||||
|
start_tick = random.randint(self.window_size + 1, length_of_data)
|
||||||
|
self._start_tick = start_tick
|
||||||
|
self._position_history = (self._start_tick * [None]) + [self._position]
|
||||||
|
else:
|
||||||
|
self._position_history = (self.window_size * [None]) + [self._position]
|
||||||
|
|
||||||
|
self._current_tick = self._start_tick
|
||||||
|
self._last_trade_tick = None
|
||||||
|
self._position = Positions.Neutral
|
||||||
|
|
||||||
|
self.total_reward = 0.
|
||||||
|
self._total_profit = 1. # unit
|
||||||
|
self.history = {}
|
||||||
|
self.trade_history = []
|
||||||
|
self.portfolio_log_returns = np.zeros(len(self.prices))
|
||||||
|
|
||||||
|
self._profits = [(self._start_tick, 1)]
|
||||||
|
self.close_trade_profit = []
|
||||||
|
self._total_unrealized_profit = 1
|
||||||
|
|
||||||
|
return self._get_observation()
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def step(self, action: int):
|
||||||
|
"""
|
||||||
|
Step depeneds on action types, this must be inherited.
|
||||||
|
"""
|
||||||
|
return
|
||||||
|
|
||||||
|
def _get_observation(self):
|
||||||
|
"""
|
||||||
|
This may or may not be independent of action types, user can inherit
|
||||||
|
this in their custom "MyRLEnv"
|
||||||
|
"""
|
||||||
|
features_window = self.signal_features[(
|
||||||
|
self._current_tick - self.window_size):self._current_tick]
|
||||||
|
if self.add_state_info and self.live:
|
||||||
|
features_and_state = DataFrame(np.zeros((len(features_window), 3)),
|
||||||
|
columns=['current_profit_pct',
|
||||||
|
'position',
|
||||||
|
'trade_duration'],
|
||||||
|
index=features_window.index)
|
||||||
|
|
||||||
|
features_and_state['current_profit_pct'] = self.get_unrealized_profit()
|
||||||
|
features_and_state['position'] = self._position.value
|
||||||
|
features_and_state['trade_duration'] = self.get_trade_duration()
|
||||||
|
features_and_state = pd.concat([features_window, features_and_state], axis=1)
|
||||||
|
return features_and_state
|
||||||
|
else:
|
||||||
|
return features_window
|
||||||
|
|
||||||
|
def get_trade_duration(self):
|
||||||
|
"""
|
||||||
|
Get the trade duration if the agent is in a trade
|
||||||
|
"""
|
||||||
|
if self._last_trade_tick is None:
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return self._current_tick - self._last_trade_tick
|
||||||
|
|
||||||
|
def get_unrealized_profit(self):
|
||||||
|
"""
|
||||||
|
Get the unrealized profit if the agent is in a trade
|
||||||
|
"""
|
||||||
|
if self._last_trade_tick is None:
|
||||||
|
return 0.
|
||||||
|
|
||||||
|
if self._position == Positions.Neutral:
|
||||||
|
return 0.
|
||||||
|
elif self._position == Positions.Short:
|
||||||
|
current_price = self.add_entry_fee(self.prices.iloc[self._current_tick].open)
|
||||||
|
last_trade_price = self.add_exit_fee(self.prices.iloc[self._last_trade_tick].open)
|
||||||
|
return (last_trade_price - current_price) / last_trade_price
|
||||||
|
elif self._position == Positions.Long:
|
||||||
|
current_price = self.add_exit_fee(self.prices.iloc[self._current_tick].open)
|
||||||
|
last_trade_price = self.add_entry_fee(self.prices.iloc[self._last_trade_tick].open)
|
||||||
|
return (current_price - last_trade_price) / last_trade_price
|
||||||
|
else:
|
||||||
|
return 0.
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def is_tradesignal(self, action: int) -> bool:
|
||||||
|
"""
|
||||||
|
Determine if the signal is a trade signal. This is
|
||||||
|
unique to the actions in the environment, and therefore must be
|
||||||
|
inherited.
|
||||||
|
"""
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _is_valid(self, action: int) -> bool:
|
||||||
|
"""
|
||||||
|
Determine if the signal is valid.This is
|
||||||
|
unique to the actions in the environment, and therefore must be
|
||||||
|
inherited.
|
||||||
|
"""
|
||||||
|
return True
|
||||||
|
|
||||||
|
def add_entry_fee(self, price):
|
||||||
|
return price * (1 + self.fee)
|
||||||
|
|
||||||
|
def add_exit_fee(self, price):
|
||||||
|
return price / (1 + self.fee)
|
||||||
|
|
||||||
|
def _update_history(self, info):
|
||||||
|
if not self.history:
|
||||||
|
self.history = {key: [] for key in info.keys()}
|
||||||
|
|
||||||
|
for key, value in info.items():
|
||||||
|
self.history[key].append(value)
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def calculate_reward(self, action: int) -> float:
|
||||||
|
"""
|
||||||
|
An example reward function. This is the one function that users will likely
|
||||||
|
wish to inject their own creativity into.
|
||||||
|
:param action: int = The action made by the agent for the current candle.
|
||||||
|
:return:
|
||||||
|
float = the reward to give to the agent for current step (used for optimization
|
||||||
|
of weights in NN)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _update_unrealized_total_profit(self):
|
||||||
|
"""
|
||||||
|
Update the unrealized total profit incase of episode end.
|
||||||
|
"""
|
||||||
|
if self._position in (Positions.Long, Positions.Short):
|
||||||
|
pnl = self.get_unrealized_profit()
|
||||||
|
if self.compound_trades:
|
||||||
|
# assumes unit stake and compounding
|
||||||
|
unrl_profit = self._total_profit * (1 + pnl)
|
||||||
|
else:
|
||||||
|
# assumes unit stake and no compounding
|
||||||
|
unrl_profit = self._total_profit + pnl
|
||||||
|
self._total_unrealized_profit = unrl_profit
|
||||||
|
|
||||||
|
def _update_total_profit(self):
|
||||||
|
pnl = self.get_unrealized_profit()
|
||||||
|
if self.compound_trades:
|
||||||
|
# assumes unit stake and compounding
|
||||||
|
self._total_profit = self._total_profit * (1 + pnl)
|
||||||
|
else:
|
||||||
|
# assumes unit stake and no compounding
|
||||||
|
self._total_profit += pnl
|
||||||
|
|
||||||
|
def current_price(self) -> float:
|
||||||
|
return self.prices.iloc[self._current_tick].open
|
||||||
|
|
||||||
|
def get_actions(self) -> Type[Enum]:
|
||||||
|
"""
|
||||||
|
Used by SubprocVecEnv to get actions from
|
||||||
|
initialized env for tensorboard callback
|
||||||
|
"""
|
||||||
|
return self.actions
|
||||||
|
|
||||||
|
# Keeping around incase we want to start building more complex environment
|
||||||
|
# templates in the future.
|
||||||
|
# def most_recent_return(self):
|
||||||
|
# """
|
||||||
|
# Calculate the tick to tick return if in a trade.
|
||||||
|
# Return is generated from rising prices in Long
|
||||||
|
# and falling prices in Short positions.
|
||||||
|
# The actions Sell/Buy or Hold during a Long position trigger the sell/buy-fee.
|
||||||
|
# """
|
||||||
|
# # Long positions
|
||||||
|
# if self._position == Positions.Long:
|
||||||
|
# current_price = self.prices.iloc[self._current_tick].open
|
||||||
|
# previous_price = self.prices.iloc[self._current_tick - 1].open
|
||||||
|
|
||||||
|
# if (self._position_history[self._current_tick - 1] == Positions.Short
|
||||||
|
# or self._position_history[self._current_tick - 1] == Positions.Neutral):
|
||||||
|
# previous_price = self.add_entry_fee(previous_price)
|
||||||
|
|
||||||
|
# return np.log(current_price) - np.log(previous_price)
|
||||||
|
|
||||||
|
# # Short positions
|
||||||
|
# if self._position == Positions.Short:
|
||||||
|
# current_price = self.prices.iloc[self._current_tick].open
|
||||||
|
# previous_price = self.prices.iloc[self._current_tick - 1].open
|
||||||
|
# if (self._position_history[self._current_tick - 1] == Positions.Long
|
||||||
|
# or self._position_history[self._current_tick - 1] == Positions.Neutral):
|
||||||
|
# previous_price = self.add_exit_fee(previous_price)
|
||||||
|
|
||||||
|
# return np.log(previous_price) - np.log(current_price)
|
||||||
|
|
||||||
|
# return 0
|
||||||
|
|
||||||
|
# def update_portfolio_log_returns(self, action):
|
||||||
|
# self.portfolio_log_returns[self._current_tick] = self.most_recent_return(action)
|
406
freqtrade/freqai/RL/BaseReinforcementLearningModel.py
Normal file
406
freqtrade/freqai/RL/BaseReinforcementLearningModel.py
Normal file
@ -0,0 +1,406 @@
|
|||||||
|
import importlib
|
||||||
|
import logging
|
||||||
|
from abc import abstractmethod
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Callable, Dict, Optional, Tuple, Type, Union
|
||||||
|
|
||||||
|
import gym
|
||||||
|
import numpy as np
|
||||||
|
import numpy.typing as npt
|
||||||
|
import pandas as pd
|
||||||
|
import torch as th
|
||||||
|
import torch.multiprocessing
|
||||||
|
from pandas import DataFrame
|
||||||
|
from stable_baselines3.common.callbacks import EvalCallback
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.utils import set_random_seed
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv
|
||||||
|
|
||||||
|
from freqtrade.exceptions import OperationalException
|
||||||
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||||
|
from freqtrade.freqai.freqai_interface import IFreqaiModel
|
||||||
|
from freqtrade.freqai.RL.Base5ActionRLEnv import Actions, Base5ActionRLEnv
|
||||||
|
from freqtrade.freqai.RL.BaseEnvironment import BaseActions, Positions
|
||||||
|
from freqtrade.freqai.RL.TensorboardCallback import TensorboardCallback
|
||||||
|
from freqtrade.persistence import Trade
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
torch.multiprocessing.set_sharing_strategy('file_system')
|
||||||
|
|
||||||
|
SB3_MODELS = ['PPO', 'A2C', 'DQN']
|
||||||
|
SB3_CONTRIB_MODELS = ['TRPO', 'ARS', 'RecurrentPPO', 'MaskablePPO']
|
||||||
|
|
||||||
|
|
||||||
|
class BaseReinforcementLearningModel(IFreqaiModel):
|
||||||
|
"""
|
||||||
|
User created Reinforcement Learning Model prediction class
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, **kwargs) -> None:
|
||||||
|
super().__init__(config=kwargs['config'])
|
||||||
|
self.max_threads = min(self.freqai_info['rl_config'].get(
|
||||||
|
'cpu_count', 1), max(int(self.max_system_threads / 2), 1))
|
||||||
|
th.set_num_threads(self.max_threads)
|
||||||
|
self.reward_params = self.freqai_info['rl_config']['model_reward_parameters']
|
||||||
|
self.train_env: Union[SubprocVecEnv, Type[gym.Env]] = gym.Env()
|
||||||
|
self.eval_env: Union[SubprocVecEnv, Type[gym.Env]] = gym.Env()
|
||||||
|
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.continual_learning = self.freqai_info.get('continual_learning', False)
|
||||||
|
if self.model_type in SB3_MODELS:
|
||||||
|
import_str = 'stable_baselines3'
|
||||||
|
elif self.model_type in SB3_CONTRIB_MODELS:
|
||||||
|
import_str = 'sb3_contrib'
|
||||||
|
else:
|
||||||
|
raise OperationalException(f'{self.model_type} not available in stable_baselines3 or '
|
||||||
|
f'sb3_contrib. please choose one of {SB3_MODELS} or '
|
||||||
|
f'{SB3_CONTRIB_MODELS}')
|
||||||
|
|
||||||
|
mod = importlib.import_module(import_str, self.model_type)
|
||||||
|
self.MODELCLASS = getattr(mod, self.model_type)
|
||||||
|
self.policy_type = self.freqai_info['rl_config']['policy_type']
|
||||||
|
self.unset_outlier_removal()
|
||||||
|
self.net_arch = self.rl_config.get('net_arch', [128, 128])
|
||||||
|
self.dd.model_type = import_str
|
||||||
|
self.tensorboard_callback: TensorboardCallback = \
|
||||||
|
TensorboardCallback(verbose=1, actions=BaseActions)
|
||||||
|
|
||||||
|
def unset_outlier_removal(self):
|
||||||
|
"""
|
||||||
|
If user has activated any function that may remove training points, this
|
||||||
|
function will set them to false and warn them
|
||||||
|
"""
|
||||||
|
if self.ft_params.get('use_SVM_to_remove_outliers', False):
|
||||||
|
self.ft_params.update({'use_SVM_to_remove_outliers': False})
|
||||||
|
logger.warning('User tried to use SVM with RL. Deactivating SVM.')
|
||||||
|
if self.ft_params.get('use_DBSCAN_to_remove_outliers', False):
|
||||||
|
self.ft_params.update({'use_DBSCAN_to_remove_outliers': False})
|
||||||
|
logger.warning('User tried to use DBSCAN with RL. Deactivating DBSCAN.')
|
||||||
|
if self.freqai_info['data_split_parameters'].get('shuffle', False):
|
||||||
|
self.freqai_info['data_split_parameters'].update({'shuffle': False})
|
||||||
|
logger.warning('User tried to shuffle training data. Setting shuffle to False')
|
||||||
|
|
||||||
|
def train(
|
||||||
|
self, unfiltered_df: DataFrame, pair: str, dk: FreqaiDataKitchen, **kwargs
|
||||||
|
) -> Any:
|
||||||
|
"""
|
||||||
|
Filter the training data and train a model to it. Train makes heavy use of the datakitchen
|
||||||
|
for storing, saving, loading, and analyzing the data.
|
||||||
|
:param unfiltered_df: Full dataframe for the current training period
|
||||||
|
:param metadata: pair metadata from strategy.
|
||||||
|
:returns:
|
||||||
|
:model: Trained model which can be used to inference (self.predict)
|
||||||
|
"""
|
||||||
|
|
||||||
|
logger.info("--------------------Starting training " f"{pair} --------------------")
|
||||||
|
|
||||||
|
features_filtered, labels_filtered = dk.filter_features(
|
||||||
|
unfiltered_df,
|
||||||
|
dk.training_features_list,
|
||||||
|
dk.label_list,
|
||||||
|
training_filter=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
data_dictionary: Dict[str, Any] = dk.make_train_test_datasets(
|
||||||
|
features_filtered, labels_filtered)
|
||||||
|
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
|
||||||
|
self.data_cleaning_train(dk)
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f'Training model on {len(dk.data_dictionary["train_features"].columns)}'
|
||||||
|
f' features and {len(data_dictionary["train_features"])} data points'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.set_train_and_eval_environments(data_dictionary, prices_train, prices_test, dk)
|
||||||
|
|
||||||
|
model = self.fit(data_dictionary, dk)
|
||||||
|
|
||||||
|
logger.info(f"--------------------done training {pair}--------------------")
|
||||||
|
|
||||||
|
return model
|
||||||
|
|
||||||
|
def set_train_and_eval_environments(self, data_dictionary: Dict[str, DataFrame],
|
||||||
|
prices_train: DataFrame, prices_test: DataFrame,
|
||||||
|
dk: FreqaiDataKitchen):
|
||||||
|
"""
|
||||||
|
User can override this if they are using a custom MyRLEnv
|
||||||
|
:param data_dictionary: dict = common data dictionary containing train and test
|
||||||
|
features/labels/weights.
|
||||||
|
:param prices_train/test: DataFrame = dataframe comprised of the prices to be used in the
|
||||||
|
environment during training or testing
|
||||||
|
:param dk: FreqaiDataKitchen = the datakitchen for the current pair
|
||||||
|
"""
|
||||||
|
train_df = data_dictionary["train_features"]
|
||||||
|
test_df = data_dictionary["test_features"]
|
||||||
|
|
||||||
|
self.train_env = self.MyRLEnv(df=train_df,
|
||||||
|
prices=prices_train,
|
||||||
|
window_size=self.CONV_WIDTH,
|
||||||
|
reward_kwargs=self.reward_params,
|
||||||
|
config=self.config,
|
||||||
|
dp=self.data_provider)
|
||||||
|
self.eval_env = Monitor(self.MyRLEnv(df=test_df,
|
||||||
|
prices=prices_test,
|
||||||
|
window_size=self.CONV_WIDTH,
|
||||||
|
reward_kwargs=self.reward_params,
|
||||||
|
config=self.config,
|
||||||
|
dp=self.data_provider))
|
||||||
|
self.eval_callback = EvalCallback(self.eval_env, deterministic=True,
|
||||||
|
render=False, eval_freq=len(train_df),
|
||||||
|
best_model_save_path=str(dk.data_path))
|
||||||
|
|
||||||
|
actions = self.train_env.get_actions()
|
||||||
|
self.tensorboard_callback = TensorboardCallback(verbose=1, actions=actions)
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def fit(self, data_dictionary: Dict[str, Any], dk: FreqaiDataKitchen, **kwargs):
|
||||||
|
"""
|
||||||
|
Agent customizations and abstract Reinforcement Learning customizations
|
||||||
|
go in here. Abstract method, so this function must be overridden by
|
||||||
|
user class.
|
||||||
|
"""
|
||||||
|
return
|
||||||
|
|
||||||
|
def get_state_info(self, pair: str) -> Tuple[float, float, int]:
|
||||||
|
"""
|
||||||
|
State info during dry/live (not backtesting) which is fed back
|
||||||
|
into the model.
|
||||||
|
:param pair: str = COIN/STAKE to get the environment information for
|
||||||
|
:return:
|
||||||
|
:market_side: float = representing short, long, or neutral for
|
||||||
|
pair
|
||||||
|
:current_profit: float = unrealized profit of the current trade
|
||||||
|
:trade_duration: int = the number of candles that the trade has
|
||||||
|
been open for
|
||||||
|
"""
|
||||||
|
open_trades = Trade.get_trades_proxy(is_open=True)
|
||||||
|
market_side = 0.5
|
||||||
|
current_profit: float = 0
|
||||||
|
trade_duration = 0
|
||||||
|
for trade in open_trades:
|
||||||
|
if trade.pair == pair:
|
||||||
|
if self.data_provider._exchange is None: # type: ignore
|
||||||
|
logger.error('No exchange available.')
|
||||||
|
return 0, 0, 0
|
||||||
|
else:
|
||||||
|
current_rate = self.data_provider._exchange.get_rate( # type: ignore
|
||||||
|
pair, refresh=False, side="exit", is_short=trade.is_short)
|
||||||
|
|
||||||
|
now = datetime.now(timezone.utc).timestamp()
|
||||||
|
trade_duration = int((now - trade.open_date_utc.timestamp()) / self.base_tf_seconds)
|
||||||
|
current_profit = trade.calc_profit_ratio(current_rate)
|
||||||
|
if trade.is_short:
|
||||||
|
market_side = 0
|
||||||
|
else:
|
||||||
|
market_side = 1
|
||||||
|
|
||||||
|
return market_side, current_profit, int(trade_duration)
|
||||||
|
|
||||||
|
def predict(
|
||||||
|
self, unfiltered_df: DataFrame, dk: FreqaiDataKitchen, **kwargs
|
||||||
|
) -> Tuple[DataFrame, npt.NDArray[np.int_]]:
|
||||||
|
"""
|
||||||
|
Filter the prediction features data and predict with it.
|
||||||
|
:param unfiltered_dataframe: Full dataframe for the current backtest period.
|
||||||
|
:return:
|
||||||
|
:pred_df: dataframe containing the predictions
|
||||||
|
:do_predict: np.array of 1s and 0s to indicate places where freqai needed to remove
|
||||||
|
data (NaNs) or felt uncertain about data (PCA and DI index)
|
||||||
|
"""
|
||||||
|
|
||||||
|
dk.find_features(unfiltered_df)
|
||||||
|
filtered_dataframe, _ = dk.filter_features(
|
||||||
|
unfiltered_df, dk.training_features_list, training_filter=False
|
||||||
|
)
|
||||||
|
filtered_dataframe = dk.normalize_data_from_metadata(filtered_dataframe)
|
||||||
|
dk.data_dictionary["prediction_features"] = filtered_dataframe
|
||||||
|
|
||||||
|
# optional additional data cleaning/analysis
|
||||||
|
self.data_cleaning_predict(dk)
|
||||||
|
|
||||||
|
pred_df = self.rl_model_predict(
|
||||||
|
dk.data_dictionary["prediction_features"], dk, self.model)
|
||||||
|
pred_df.fillna(0, inplace=True)
|
||||||
|
|
||||||
|
return (pred_df, dk.do_predict)
|
||||||
|
|
||||||
|
def rl_model_predict(self, dataframe: DataFrame,
|
||||||
|
dk: FreqaiDataKitchen, model: Any) -> DataFrame:
|
||||||
|
"""
|
||||||
|
A helper function to make predictions in the Reinforcement learning module.
|
||||||
|
:param dataframe: DataFrame = the dataframe of features to make the predictions on
|
||||||
|
:param dk: FreqaiDatakitchen = data kitchen for the current pair
|
||||||
|
:param model: Any = the trained model used to inference the features.
|
||||||
|
"""
|
||||||
|
output = pd.DataFrame(np.zeros(len(dataframe)), columns=dk.label_list)
|
||||||
|
|
||||||
|
def _predict(window):
|
||||||
|
observations = dataframe.iloc[window.index]
|
||||||
|
if self.live and self.rl_config.get('add_state_info', False):
|
||||||
|
market_side, current_profit, trade_duration = self.get_state_info(dk.pair)
|
||||||
|
observations['current_profit_pct'] = current_profit
|
||||||
|
observations['position'] = market_side
|
||||||
|
observations['trade_duration'] = trade_duration
|
||||||
|
res, _ = model.predict(observations, deterministic=True)
|
||||||
|
return res
|
||||||
|
|
||||||
|
output = output.rolling(window=self.CONV_WIDTH).apply(_predict)
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
def build_ohlc_price_dataframes(self, data_dictionary: dict,
|
||||||
|
pair: str, dk: FreqaiDataKitchen) -> Tuple[DataFrame,
|
||||||
|
DataFrame]:
|
||||||
|
"""
|
||||||
|
Builds the train prices and test prices for the environment.
|
||||||
|
"""
|
||||||
|
|
||||||
|
pair = pair.replace(':', '')
|
||||||
|
train_df = data_dictionary["train_features"]
|
||||||
|
test_df = data_dictionary["test_features"]
|
||||||
|
|
||||||
|
# price data for model training and evaluation
|
||||||
|
tf = self.config['timeframe']
|
||||||
|
ohlc_list = [f'%-{pair}raw_open_{tf}', f'%-{pair}raw_low_{tf}',
|
||||||
|
f'%-{pair}raw_high_{tf}', f'%-{pair}raw_close_{tf}']
|
||||||
|
rename_dict = {f'%-{pair}raw_open_{tf}': 'open', f'%-{pair}raw_low_{tf}': 'low',
|
||||||
|
f'%-{pair}raw_high_{tf}': ' high', f'%-{pair}raw_close_{tf}': 'close'}
|
||||||
|
|
||||||
|
prices_train = train_df.filter(ohlc_list, axis=1)
|
||||||
|
if prices_train.empty:
|
||||||
|
raise OperationalException('Reinforcement learning module didnt find the raw prices '
|
||||||
|
'assigned in populate_any_indicators. Please assign them '
|
||||||
|
'with:\n'
|
||||||
|
'informative[f"%-{pair}raw_close"] = informative["close"]\n'
|
||||||
|
'informative[f"%-{pair}raw_open"] = informative["open"]\n'
|
||||||
|
'informative[f"%-{pair}raw_high"] = informative["high"]\n'
|
||||||
|
'informative[f"%-{pair}raw_low"] = informative["low"]\n')
|
||||||
|
prices_train.rename(columns=rename_dict, inplace=True)
|
||||||
|
prices_train.reset_index(drop=True)
|
||||||
|
|
||||||
|
prices_test = test_df.filter(ohlc_list, axis=1)
|
||||||
|
prices_test.rename(columns=rename_dict, inplace=True)
|
||||||
|
prices_test.reset_index(drop=True)
|
||||||
|
|
||||||
|
return prices_train, prices_test
|
||||||
|
|
||||||
|
def load_model_from_disk(self, dk: FreqaiDataKitchen) -> Any:
|
||||||
|
"""
|
||||||
|
Can be used by user if they are trying to limit_ram_usage *and*
|
||||||
|
perform continual learning.
|
||||||
|
For now, this is unused.
|
||||||
|
"""
|
||||||
|
exists = Path(dk.data_path / f"{dk.model_filename}_model").is_file()
|
||||||
|
if exists:
|
||||||
|
model = self.MODELCLASS.load(dk.data_path / f"{dk.model_filename}_model")
|
||||||
|
else:
|
||||||
|
logger.info('No model file on disk to continue learning from.')
|
||||||
|
|
||||||
|
return model
|
||||||
|
|
||||||
|
def _on_stop(self):
|
||||||
|
"""
|
||||||
|
Hook called on bot shutdown. Close SubprocVecEnv subprocesses for clean shutdown.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if self.train_env:
|
||||||
|
self.train_env.close()
|
||||||
|
|
||||||
|
if self.eval_env:
|
||||||
|
self.eval_env.close()
|
||||||
|
|
||||||
|
# Nested class which can be overridden by user to customize further
|
||||||
|
class MyRLEnv(Base5ActionRLEnv):
|
||||||
|
"""
|
||||||
|
User can override any function in BaseRLEnv and gym.Env. Here the user
|
||||||
|
sets a custom reward based on profit and trade duration.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def calculate_reward(self, action: int) -> float:
|
||||||
|
"""
|
||||||
|
An example reward function. This is the one function that users will likely
|
||||||
|
wish to inject their own creativity into.
|
||||||
|
:param action: int = The action made by the agent for the current candle.
|
||||||
|
:return:
|
||||||
|
float = the reward to give to the agent for current step (used for optimization
|
||||||
|
of weights in NN)
|
||||||
|
"""
|
||||||
|
# first, penalize if the action is not valid
|
||||||
|
if not self._is_valid(action):
|
||||||
|
return -2
|
||||||
|
|
||||||
|
pnl = self.get_unrealized_profit()
|
||||||
|
factor = 100.
|
||||||
|
|
||||||
|
# reward agent for entering trades
|
||||||
|
if (action in (Actions.Long_enter.value, Actions.Short_enter.value)
|
||||||
|
and self._position == Positions.Neutral):
|
||||||
|
return 25
|
||||||
|
# discourage agent from not entering trades
|
||||||
|
if action == Actions.Neutral.value and self._position == Positions.Neutral:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
max_trade_duration = self.rl_config.get('max_trade_duration_candles', 300)
|
||||||
|
if self._last_trade_tick:
|
||||||
|
trade_duration = self._current_tick - self._last_trade_tick
|
||||||
|
else:
|
||||||
|
trade_duration = 0
|
||||||
|
|
||||||
|
if trade_duration <= max_trade_duration:
|
||||||
|
factor *= 1.5
|
||||||
|
elif trade_duration > max_trade_duration:
|
||||||
|
factor *= 0.5
|
||||||
|
|
||||||
|
# discourage sitting in position
|
||||||
|
if (self._position in (Positions.Short, Positions.Long) and
|
||||||
|
action == Actions.Neutral.value):
|
||||||
|
return -1 * trade_duration / max_trade_duration
|
||||||
|
|
||||||
|
# close long
|
||||||
|
if action == Actions.Long_exit.value and self._position == Positions.Long:
|
||||||
|
if pnl > self.profit_aim * self.rr:
|
||||||
|
factor *= self.rl_config['model_reward_parameters'].get('win_reward_factor', 2)
|
||||||
|
return float(pnl * factor)
|
||||||
|
|
||||||
|
# close short
|
||||||
|
if action == Actions.Short_exit.value and self._position == Positions.Short:
|
||||||
|
if pnl > self.profit_aim * self.rr:
|
||||||
|
factor *= self.rl_config['model_reward_parameters'].get('win_reward_factor', 2)
|
||||||
|
return float(pnl * factor)
|
||||||
|
|
||||||
|
return 0.
|
||||||
|
|
||||||
|
|
||||||
|
def make_env(MyRLEnv: Type[gym.Env], env_id: str, rank: int,
|
||||||
|
seed: int, train_df: DataFrame, price: DataFrame,
|
||||||
|
reward_params: Dict[str, int], window_size: int, monitor: bool = False,
|
||||||
|
config: Dict[str, Any] = {}) -> Callable:
|
||||||
|
"""
|
||||||
|
Utility function for multiprocessed env.
|
||||||
|
|
||||||
|
:param env_id: (str) the environment ID
|
||||||
|
:param num_env: (int) the number of environment you wish to have in subprocesses
|
||||||
|
:param seed: (int) the inital seed for RNG
|
||||||
|
:param rank: (int) index of the subprocess
|
||||||
|
:return: (Callable)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _init() -> gym.Env:
|
||||||
|
|
||||||
|
env = MyRLEnv(df=train_df, prices=price, window_size=window_size,
|
||||||
|
reward_kwargs=reward_params, id=env_id, seed=seed + rank, config=config)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
set_random_seed(seed)
|
||||||
|
return _init
|
60
freqtrade/freqai/RL/TensorboardCallback.py
Normal file
60
freqtrade/freqai/RL/TensorboardCallback.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
from enum import Enum
|
||||||
|
from typing import Any, Dict, Type, Union
|
||||||
|
|
||||||
|
from stable_baselines3.common.callbacks import BaseCallback
|
||||||
|
from stable_baselines3.common.logger import HParam
|
||||||
|
|
||||||
|
from freqtrade.freqai.RL.BaseEnvironment import BaseActions, BaseEnvironment
|
||||||
|
|
||||||
|
|
||||||
|
class TensorboardCallback(BaseCallback):
|
||||||
|
"""
|
||||||
|
Custom callback for plotting additional values in tensorboard and
|
||||||
|
episodic summary reports.
|
||||||
|
"""
|
||||||
|
def __init__(self, verbose=1, actions: Type[Enum] = BaseActions):
|
||||||
|
super(TensorboardCallback, self).__init__(verbose)
|
||||||
|
self.model: Any = None
|
||||||
|
self.logger = None # type: Any
|
||||||
|
self.training_env: BaseEnvironment = None # type: ignore
|
||||||
|
self.actions: Type[Enum] = actions
|
||||||
|
|
||||||
|
def _on_training_start(self) -> None:
|
||||||
|
hparam_dict = {
|
||||||
|
"algorithm": self.model.__class__.__name__,
|
||||||
|
"learning_rate": self.model.learning_rate,
|
||||||
|
# "gamma": self.model.gamma,
|
||||||
|
# "gae_lambda": self.model.gae_lambda,
|
||||||
|
# "batch_size": self.model.batch_size,
|
||||||
|
# "n_steps": self.model.n_steps,
|
||||||
|
}
|
||||||
|
metric_dict: Dict[str, Union[float, int]] = {
|
||||||
|
"eval/mean_reward": 0,
|
||||||
|
"rollout/ep_rew_mean": 0,
|
||||||
|
"rollout/ep_len_mean": 0,
|
||||||
|
"train/value_loss": 0,
|
||||||
|
"train/explained_variance": 0,
|
||||||
|
}
|
||||||
|
self.logger.record(
|
||||||
|
"hparams",
|
||||||
|
HParam(hparam_dict, metric_dict),
|
||||||
|
exclude=("stdout", "log", "json", "csv"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def _on_step(self) -> bool:
|
||||||
|
custom_info = self.training_env.get_attr("custom_info")[0]
|
||||||
|
self.logger.record("_state/position", self.locals["infos"][0]["position"])
|
||||||
|
self.logger.record("_state/trade_duration", self.locals["infos"][0]["trade_duration"])
|
||||||
|
self.logger.record("_state/current_profit_pct", self.locals["infos"]
|
||||||
|
[0]["current_profit_pct"])
|
||||||
|
self.logger.record("_reward/total_profit", self.locals["infos"][0]["total_profit"])
|
||||||
|
self.logger.record("_reward/total_reward", self.locals["infos"][0]["total_reward"])
|
||||||
|
self.logger.record_mean("_reward/mean_trade_duration", self.locals["infos"]
|
||||||
|
[0]["trade_duration"])
|
||||||
|
self.logger.record("_actions/action", self.locals["infos"][0]["action"])
|
||||||
|
self.logger.record("_actions/_Invalid", custom_info["Invalid"])
|
||||||
|
self.logger.record("_actions/_Unknown", custom_info["Unknown"])
|
||||||
|
self.logger.record("_actions/Hold", custom_info["Hold"])
|
||||||
|
for action in self.actions:
|
||||||
|
self.logger.record(f"_actions/{action.name}", custom_info[action.name])
|
||||||
|
return True
|
0
freqtrade/freqai/RL/__init__.py
Normal file
0
freqtrade/freqai/RL/__init__.py
Normal file
93
freqtrade/freqai/base_models/FreqaiMultiOutputClassifier.py
Normal file
93
freqtrade/freqai/base_models/FreqaiMultiOutputClassifier.py
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
import numpy as np
|
||||||
|
from joblib import Parallel
|
||||||
|
from sklearn.base import is_classifier
|
||||||
|
from sklearn.multioutput import MultiOutputClassifier, _fit_estimator
|
||||||
|
from sklearn.utils.fixes import delayed
|
||||||
|
from sklearn.utils.multiclass import check_classification_targets
|
||||||
|
from sklearn.utils.validation import has_fit_parameter
|
||||||
|
|
||||||
|
from freqtrade.exceptions import OperationalException
|
||||||
|
|
||||||
|
|
||||||
|
class FreqaiMultiOutputClassifier(MultiOutputClassifier):
|
||||||
|
|
||||||
|
def fit(self, X, y, sample_weight=None, fit_params=None):
|
||||||
|
"""Fit the model to data, separately for each output variable.
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
X : {array-like, sparse matrix} of shape (n_samples, n_features)
|
||||||
|
The input data.
|
||||||
|
y : {array-like, sparse matrix} of shape (n_samples, n_outputs)
|
||||||
|
Multi-output targets. An indicator matrix turns on multilabel
|
||||||
|
estimation.
|
||||||
|
sample_weight : array-like of shape (n_samples,), default=None
|
||||||
|
Sample weights. If `None`, then samples are equally weighted.
|
||||||
|
Only supported if the underlying classifier supports sample
|
||||||
|
weights.
|
||||||
|
fit_params : A list of dicts for the fit_params
|
||||||
|
Parameters passed to the ``estimator.fit`` method of each step.
|
||||||
|
Each dict may contain same or different values (e.g. different
|
||||||
|
eval_sets or init_models)
|
||||||
|
.. versionadded:: 0.23
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
self : object
|
||||||
|
Returns a fitted instance.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not hasattr(self.estimator, "fit"):
|
||||||
|
raise ValueError("The base estimator should implement a fit method")
|
||||||
|
|
||||||
|
y = self._validate_data(X="no_validation", y=y, multi_output=True)
|
||||||
|
|
||||||
|
if is_classifier(self):
|
||||||
|
check_classification_targets(y)
|
||||||
|
|
||||||
|
if y.ndim == 1:
|
||||||
|
raise ValueError(
|
||||||
|
"y must have at least two dimensions for "
|
||||||
|
"multi-output regression but has only one."
|
||||||
|
)
|
||||||
|
|
||||||
|
if sample_weight is not None and not has_fit_parameter(
|
||||||
|
self.estimator, "sample_weight"
|
||||||
|
):
|
||||||
|
raise ValueError("Underlying estimator does not support sample weights.")
|
||||||
|
|
||||||
|
if not fit_params:
|
||||||
|
fit_params = [None] * y.shape[1]
|
||||||
|
|
||||||
|
self.estimators_ = Parallel(n_jobs=self.n_jobs)(
|
||||||
|
delayed(_fit_estimator)(
|
||||||
|
self.estimator, X, y[:, i], sample_weight, **fit_params[i]
|
||||||
|
)
|
||||||
|
for i in range(y.shape[1])
|
||||||
|
)
|
||||||
|
|
||||||
|
self.classes_ = []
|
||||||
|
for estimator in self.estimators_:
|
||||||
|
self.classes_.extend(estimator.classes_)
|
||||||
|
if len(set(self.classes_)) != len(self.classes_):
|
||||||
|
raise OperationalException(f"Class labels must be unique across targets: "
|
||||||
|
f"{self.classes_}")
|
||||||
|
|
||||||
|
if hasattr(self.estimators_[0], "n_features_in_"):
|
||||||
|
self.n_features_in_ = self.estimators_[0].n_features_in_
|
||||||
|
if hasattr(self.estimators_[0], "feature_names_in_"):
|
||||||
|
self.feature_names_in_ = self.estimators_[0].feature_names_in_
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
|
def predict_proba(self, X):
|
||||||
|
"""
|
||||||
|
Get predict_proba and stack arrays horizontally
|
||||||
|
"""
|
||||||
|
results = np.hstack(super().predict_proba(X))
|
||||||
|
return np.squeeze(results)
|
||||||
|
|
||||||
|
def predict(self, X):
|
||||||
|
"""
|
||||||
|
Get predict and squeeze into 2D array
|
||||||
|
"""
|
||||||
|
results = super().predict(X)
|
||||||
|
return np.squeeze(results)
|
@ -1,9 +1,10 @@
|
|||||||
import collections
|
import collections
|
||||||
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import threading
|
import threading
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, Tuple, TypedDict
|
from typing import Any, Dict, Tuple, TypedDict
|
||||||
|
|
||||||
@ -81,12 +82,14 @@ class FreqaiDataDrawer:
|
|||||||
self.historic_predictions_bkp_path = Path(
|
self.historic_predictions_bkp_path = Path(
|
||||||
self.full_path / "historic_predictions.backup.pkl")
|
self.full_path / "historic_predictions.backup.pkl")
|
||||||
self.pair_dictionary_path = Path(self.full_path / "pair_dictionary.json")
|
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.metric_tracker_path = Path(self.full_path / "metric_tracker.json")
|
||||||
self.follow_mode = follow_mode
|
self.follow_mode = follow_mode
|
||||||
if follow_mode:
|
if follow_mode:
|
||||||
self.create_follower_dict()
|
self.create_follower_dict()
|
||||||
self.load_drawer_from_disk()
|
self.load_drawer_from_disk()
|
||||||
self.load_historic_predictions_from_disk()
|
self.load_historic_predictions_from_disk()
|
||||||
|
self.metric_tracker: Dict[str, Dict[str, Dict[str, list]]] = {}
|
||||||
self.load_metric_tracker_from_disk()
|
self.load_metric_tracker_from_disk()
|
||||||
self.training_queue: Dict[str, int] = {}
|
self.training_queue: Dict[str, int] = {}
|
||||||
self.history_lock = threading.Lock()
|
self.history_lock = threading.Lock()
|
||||||
@ -97,7 +100,7 @@ class FreqaiDataDrawer:
|
|||||||
self.empty_pair_dict: pair_info = {
|
self.empty_pair_dict: pair_info = {
|
||||||
"model_filename": "", "trained_timestamp": 0,
|
"model_filename": "", "trained_timestamp": 0,
|
||||||
"data_path": "", "extras": {}}
|
"data_path": "", "extras": {}}
|
||||||
self.metric_tracker: Dict[str, Dict[str, Dict[str, list]]] = {}
|
self.model_type = self.freqai_info.get('model_save_type', 'joblib')
|
||||||
|
|
||||||
def update_metric_tracker(self, metric: str, value: float, pair: str) -> None:
|
def update_metric_tracker(self, metric: str, value: float, pair: str) -> None:
|
||||||
"""
|
"""
|
||||||
@ -125,6 +128,17 @@ class FreqaiDataDrawer:
|
|||||||
self.update_metric_tracker('cpu_load5min', load5 / cpus, pair)
|
self.update_metric_tracker('cpu_load5min', load5 / cpus, pair)
|
||||||
self.update_metric_tracker('cpu_load15min', load15 / cpus, pair)
|
self.update_metric_tracker('cpu_load15min', load15 / cpus, pair)
|
||||||
|
|
||||||
|
def load_global_metadata_from_disk(self):
|
||||||
|
"""
|
||||||
|
Locate and load a previously saved global metadata in present model folder.
|
||||||
|
"""
|
||||||
|
exists = self.global_metadata_path.is_file()
|
||||||
|
if exists:
|
||||||
|
with open(self.global_metadata_path, "r") as fp:
|
||||||
|
metatada_dict = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE)
|
||||||
|
return metatada_dict
|
||||||
|
return {}
|
||||||
|
|
||||||
def load_drawer_from_disk(self):
|
def load_drawer_from_disk(self):
|
||||||
"""
|
"""
|
||||||
Locate and load a previously saved data drawer full of all pair model metadata in
|
Locate and load a previously saved data drawer full of all pair model metadata in
|
||||||
@ -153,6 +167,7 @@ class FreqaiDataDrawer:
|
|||||||
if exists:
|
if exists:
|
||||||
with open(self.metric_tracker_path, "r") as fp:
|
with open(self.metric_tracker_path, "r") as fp:
|
||||||
self.metric_tracker = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE)
|
self.metric_tracker = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE)
|
||||||
|
logger.info("Loading existing metric tracker from disk.")
|
||||||
else:
|
else:
|
||||||
logger.info("Could not find existing metric tracker, starting from scratch")
|
logger.info("Could not find existing metric tracker, starting from scratch")
|
||||||
|
|
||||||
@ -224,6 +239,15 @@ class FreqaiDataDrawer:
|
|||||||
rapidjson.dump(self.follower_dict, fp, default=self.np_encoder,
|
rapidjson.dump(self.follower_dict, fp, default=self.np_encoder,
|
||||||
number_mode=rapidjson.NM_NATIVE)
|
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:
|
||||||
|
rapidjson.dump(metadata, fp, default=self.np_encoder,
|
||||||
|
number_mode=rapidjson.NM_NATIVE)
|
||||||
|
|
||||||
def create_follower_dict(self):
|
def create_follower_dict(self):
|
||||||
"""
|
"""
|
||||||
Create or dictionary for each follower to maintain unique persistent prediction targets
|
Create or dictionary for each follower to maintain unique persistent prediction targets
|
||||||
@ -475,10 +499,12 @@ class FreqaiDataDrawer:
|
|||||||
save_path = Path(dk.data_path)
|
save_path = Path(dk.data_path)
|
||||||
|
|
||||||
# Save the trained model
|
# Save the trained model
|
||||||
if not dk.keras:
|
if self.model_type == 'joblib':
|
||||||
dump(model, save_path / f"{dk.model_filename}_model.joblib")
|
dump(model, save_path / f"{dk.model_filename}_model.joblib")
|
||||||
else:
|
elif self.model_type == 'keras':
|
||||||
model.save(save_path / f"{dk.model_filename}_model.h5")
|
model.save(save_path / f"{dk.model_filename}_model.h5")
|
||||||
|
elif 'stable_baselines' in self.model_type or 'sb3_contrib' == self.model_type:
|
||||||
|
model.save(save_path / f"{dk.model_filename}_model.zip")
|
||||||
|
|
||||||
if dk.svm_model is not None:
|
if dk.svm_model is not None:
|
||||||
dump(dk.svm_model, save_path / f"{dk.model_filename}_svm_model.joblib")
|
dump(dk.svm_model, save_path / f"{dk.model_filename}_svm_model.joblib")
|
||||||
@ -505,11 +531,10 @@ class FreqaiDataDrawer:
|
|||||||
dk.pca, open(dk.data_path / f"{dk.model_filename}_pca_object.pkl", "wb")
|
dk.pca, open(dk.data_path / f"{dk.model_filename}_pca_object.pkl", "wb")
|
||||||
)
|
)
|
||||||
|
|
||||||
# if self.live:
|
|
||||||
# store as much in ram as possible to increase performance
|
|
||||||
self.model_dictionary[coin] = model
|
self.model_dictionary[coin] = model
|
||||||
self.pair_dict[coin]["model_filename"] = dk.model_filename
|
self.pair_dict[coin]["model_filename"] = dk.model_filename
|
||||||
self.pair_dict[coin]["data_path"] = str(dk.data_path)
|
self.pair_dict[coin]["data_path"] = str(dk.data_path)
|
||||||
|
|
||||||
if coin not in self.meta_data_dictionary:
|
if coin not in self.meta_data_dictionary:
|
||||||
self.meta_data_dictionary[coin] = {}
|
self.meta_data_dictionary[coin] = {}
|
||||||
self.meta_data_dictionary[coin]["train_df"] = dk.data_dictionary["train_features"]
|
self.meta_data_dictionary[coin]["train_df"] = dk.data_dictionary["train_features"]
|
||||||
@ -541,14 +566,6 @@ class FreqaiDataDrawer:
|
|||||||
if dk.live:
|
if dk.live:
|
||||||
dk.model_filename = self.pair_dict[coin]["model_filename"]
|
dk.model_filename = self.pair_dict[coin]["model_filename"]
|
||||||
dk.data_path = Path(self.pair_dict[coin]["data_path"])
|
dk.data_path = Path(self.pair_dict[coin]["data_path"])
|
||||||
if self.freqai_info.get("follow_mode", False):
|
|
||||||
# follower can be on a different system which is rsynced from the leader:
|
|
||||||
dk.data_path = Path(
|
|
||||||
self.config["user_data_dir"]
|
|
||||||
/ "models"
|
|
||||||
/ dk.data_path.parts[-2]
|
|
||||||
/ dk.data_path.parts[-1]
|
|
||||||
)
|
|
||||||
|
|
||||||
if coin in self.meta_data_dictionary:
|
if coin in self.meta_data_dictionary:
|
||||||
dk.data = self.meta_data_dictionary[coin]["meta_data"]
|
dk.data = self.meta_data_dictionary[coin]["meta_data"]
|
||||||
@ -567,12 +584,16 @@ class FreqaiDataDrawer:
|
|||||||
# try to access model in memory instead of loading object from disk to save time
|
# try to access model in memory instead of loading object from disk to save time
|
||||||
if dk.live and coin in self.model_dictionary:
|
if dk.live and coin in self.model_dictionary:
|
||||||
model = self.model_dictionary[coin]
|
model = self.model_dictionary[coin]
|
||||||
elif not dk.keras:
|
elif self.model_type == 'joblib':
|
||||||
model = load(dk.data_path / f"{dk.model_filename}_model.joblib")
|
model = load(dk.data_path / f"{dk.model_filename}_model.joblib")
|
||||||
else:
|
elif self.model_type == 'keras':
|
||||||
from tensorflow import keras
|
from tensorflow import keras
|
||||||
|
|
||||||
model = keras.models.load_model(dk.data_path / f"{dk.model_filename}_model.h5")
|
model = keras.models.load_model(dk.data_path / f"{dk.model_filename}_model.h5")
|
||||||
|
elif 'stable_baselines' in self.model_type or 'sb3_contrib' == self.model_type:
|
||||||
|
mod = importlib.import_module(
|
||||||
|
self.model_type, self.freqai_info['rl_config']['model_type'])
|
||||||
|
MODELCLASS = getattr(mod, self.freqai_info['rl_config']['model_type'])
|
||||||
|
model = MODELCLASS.load(dk.data_path / f"{dk.model_filename}_model")
|
||||||
|
|
||||||
if Path(dk.data_path / f"{dk.model_filename}_svm_model.joblib").is_file():
|
if Path(dk.data_path / f"{dk.model_filename}_svm_model.joblib").is_file():
|
||||||
dk.svm_model = load(dk.data_path / f"{dk.model_filename}_svm_model.joblib")
|
dk.svm_model = load(dk.data_path / f"{dk.model_filename}_svm_model.joblib")
|
||||||
@ -582,6 +603,10 @@ class FreqaiDataDrawer:
|
|||||||
f"Unable to load model, ensure model exists at " f"{dk.data_path} "
|
f"Unable to load model, ensure model exists at " f"{dk.data_path} "
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# load it into ram if it was loaded from disk
|
||||||
|
if coin not in self.model_dictionary:
|
||||||
|
self.model_dictionary[coin] = model
|
||||||
|
|
||||||
if self.config["freqai"]["feature_parameters"]["principal_component_analysis"]:
|
if self.config["freqai"]["feature_parameters"]["principal_component_analysis"]:
|
||||||
dk.pca = cloudpickle.load(
|
dk.pca = cloudpickle.load(
|
||||||
open(dk.data_path / f"{dk.model_filename}_pca_object.pkl", "rb")
|
open(dk.data_path / f"{dk.model_filename}_pca_object.pkl", "rb")
|
||||||
@ -636,6 +661,8 @@ class FreqaiDataDrawer:
|
|||||||
axis=0,
|
axis=0,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.current_candle = history_data[dk.pair][self.config['timeframe']].iloc[-1]['date']
|
||||||
|
|
||||||
def load_all_pair_histories(self, timerange: TimeRange, dk: FreqaiDataKitchen) -> None:
|
def load_all_pair_histories(self, timerange: TimeRange, dk: FreqaiDataKitchen) -> None:
|
||||||
"""
|
"""
|
||||||
Load pair histories for all whitelist and corr_pairlist pairs.
|
Load pair histories for all whitelist and corr_pairlist pairs.
|
||||||
@ -690,3 +717,31 @@ class FreqaiDataDrawer:
|
|||||||
).reset_index(drop=True)
|
).reset_index(drop=True)
|
||||||
|
|
||||||
return corr_dataframes, base_dataframes
|
return corr_dataframes, base_dataframes
|
||||||
|
|
||||||
|
def get_timerange_from_live_historic_predictions(self) -> TimeRange:
|
||||||
|
"""
|
||||||
|
Returns timerange information based on historic predictions file
|
||||||
|
:return: timerange calculated from saved live data
|
||||||
|
"""
|
||||||
|
if not self.historic_predictions_path.is_file():
|
||||||
|
raise OperationalException(
|
||||||
|
'Historic predictions not found. Historic predictions data is required '
|
||||||
|
'to run backtest with the freqai-backtest-live-models option '
|
||||||
|
)
|
||||||
|
|
||||||
|
self.load_historic_predictions_from_disk()
|
||||||
|
|
||||||
|
all_pairs_end_dates = []
|
||||||
|
for pair in self.historic_predictions:
|
||||||
|
pair_historic_data = self.historic_predictions[pair]
|
||||||
|
all_pairs_end_dates.append(pair_historic_data.date_pred.max())
|
||||||
|
|
||||||
|
global_metadata = self.load_global_metadata_from_disk()
|
||||||
|
start_date = datetime.fromtimestamp(int(global_metadata["start_dry_live_date"]))
|
||||||
|
end_date = max(all_pairs_end_dates)
|
||||||
|
# add 1 day to string timerange to ensure BT module will load all dataframe data
|
||||||
|
end_date = end_date + timedelta(days=1)
|
||||||
|
backtesting_timerange = TimeRange(
|
||||||
|
'date', 'date', int(start_date.timestamp()), int(end_date.timestamp())
|
||||||
|
)
|
||||||
|
return backtesting_timerange
|
||||||
|
@ -9,6 +9,7 @@ from typing import Any, Dict, List, Tuple
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import numpy.typing as npt
|
import numpy.typing as npt
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
import psutil
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
from scipy import stats
|
from scipy import stats
|
||||||
from sklearn import linear_model
|
from sklearn import linear_model
|
||||||
@ -19,6 +20,7 @@ from sklearn.neighbors import NearestNeighbors
|
|||||||
|
|
||||||
from freqtrade.configuration import TimeRange
|
from freqtrade.configuration import TimeRange
|
||||||
from freqtrade.constants import Config
|
from freqtrade.constants import Config
|
||||||
|
from freqtrade.data.converter import reduce_dataframe_footprint
|
||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
from freqtrade.exchange import timeframe_to_seconds
|
from freqtrade.exchange import timeframe_to_seconds
|
||||||
from freqtrade.strategy.interface import IStrategy
|
from freqtrade.strategy.interface import IStrategy
|
||||||
@ -80,14 +82,15 @@ class FreqaiDataKitchen:
|
|||||||
self.svm_model: linear_model.SGDOneClassSVM = None
|
self.svm_model: linear_model.SGDOneClassSVM = None
|
||||||
self.keras: bool = self.freqai_config.get("keras", False)
|
self.keras: bool = self.freqai_config.get("keras", False)
|
||||||
self.set_all_pairs()
|
self.set_all_pairs()
|
||||||
|
self.backtest_live_models = config.get("freqai_backtest_live_models", False)
|
||||||
|
|
||||||
if not self.live:
|
if not self.live:
|
||||||
if not self.config["timerange"]:
|
self.full_path = self.get_full_models_path(self.config)
|
||||||
raise OperationalException(
|
|
||||||
'Please pass --timerange if you intend to use FreqAI for backtesting.')
|
if not self.backtest_live_models:
|
||||||
self.full_timerange = self.create_fulltimerange(
|
self.full_timerange = self.create_fulltimerange(
|
||||||
self.config["timerange"], self.freqai_config.get("train_period_days", 0)
|
self.config["timerange"], self.freqai_config.get("train_period_days", 0)
|
||||||
)
|
)
|
||||||
|
|
||||||
(self.training_timeranges, self.backtesting_timeranges) = self.split_timerange(
|
(self.training_timeranges, self.backtesting_timeranges) = self.split_timerange(
|
||||||
self.full_timerange,
|
self.full_timerange,
|
||||||
config["freqai"]["train_period_days"],
|
config["freqai"]["train_period_days"],
|
||||||
@ -95,10 +98,14 @@ class FreqaiDataKitchen:
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.data['extra_returns_per_train'] = self.freqai_config.get('extra_returns_per_train', {})
|
self.data['extra_returns_per_train'] = self.freqai_config.get('extra_returns_per_train', {})
|
||||||
self.thread_count = self.freqai_config.get("data_kitchen_thread_count", -1)
|
if not self.freqai_config.get("data_kitchen_thread_count", 0):
|
||||||
|
self.thread_count = max(int(psutil.cpu_count() * 2 - 2), 1)
|
||||||
|
else:
|
||||||
|
self.thread_count = self.freqai_config["data_kitchen_thread_count"]
|
||||||
self.train_dates: DataFrame = pd.DataFrame()
|
self.train_dates: DataFrame = pd.DataFrame()
|
||||||
self.unique_classes: Dict[str, list] = {}
|
self.unique_classes: Dict[str, list] = {}
|
||||||
self.unique_class_list: list = []
|
self.unique_class_list: list = []
|
||||||
|
self.backtest_live_models_data: Dict[str, Any] = {}
|
||||||
|
|
||||||
def set_paths(
|
def set_paths(
|
||||||
self,
|
self,
|
||||||
@ -110,10 +117,7 @@ class FreqaiDataKitchen:
|
|||||||
:param metadata: dict = strategy furnished pair metadata
|
:param metadata: dict = strategy furnished pair metadata
|
||||||
:param trained_timestamp: int = timestamp of most recent training
|
:param trained_timestamp: int = timestamp of most recent training
|
||||||
"""
|
"""
|
||||||
self.full_path = Path(
|
self.full_path = self.get_full_models_path(self.config)
|
||||||
self.config["user_data_dir"] / "models" / str(self.freqai_config.get("identifier"))
|
|
||||||
)
|
|
||||||
|
|
||||||
self.data_path = Path(
|
self.data_path = Path(
|
||||||
self.full_path
|
self.full_path
|
||||||
/ f"sub-train-{pair.split('/')[0]}_{trained_timestamp}"
|
/ f"sub-train-{pair.split('/')[0]}_{trained_timestamp}"
|
||||||
@ -244,7 +248,7 @@ class FreqaiDataKitchen:
|
|||||||
self.data["filter_drop_index_training"] = drop_index
|
self.data["filter_drop_index_training"] = drop_index
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if len(self.data['constant_features_list']):
|
if 'constant_features_list' in self.data and len(self.data['constant_features_list']):
|
||||||
filtered_df = self.check_pred_labels(filtered_df)
|
filtered_df = self.check_pred_labels(filtered_df)
|
||||||
# we are backtesting so we need to preserve row number to send back to strategy,
|
# we are backtesting so we need to preserve row number to send back to strategy,
|
||||||
# so now we use do_predict to avoid any prediction based on a NaN
|
# so now we use do_predict to avoid any prediction based on a NaN
|
||||||
@ -428,9 +432,7 @@ class FreqaiDataKitchen:
|
|||||||
timerange_train.stopts = timerange_train.startts + train_period_days
|
timerange_train.stopts = timerange_train.startts + train_period_days
|
||||||
|
|
||||||
first = False
|
first = False
|
||||||
start = datetime.fromtimestamp(timerange_train.startts, tz=timezone.utc)
|
tr_training_list.append(timerange_train.timerange_str)
|
||||||
stop = datetime.fromtimestamp(timerange_train.stopts, tz=timezone.utc)
|
|
||||||
tr_training_list.append(start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d"))
|
|
||||||
tr_training_list_timerange.append(copy.deepcopy(timerange_train))
|
tr_training_list_timerange.append(copy.deepcopy(timerange_train))
|
||||||
|
|
||||||
# associated backtest period
|
# associated backtest period
|
||||||
@ -442,9 +444,7 @@ class FreqaiDataKitchen:
|
|||||||
if timerange_backtest.stopts > config_timerange.stopts:
|
if timerange_backtest.stopts > config_timerange.stopts:
|
||||||
timerange_backtest.stopts = config_timerange.stopts
|
timerange_backtest.stopts = config_timerange.stopts
|
||||||
|
|
||||||
start = datetime.fromtimestamp(timerange_backtest.startts, tz=timezone.utc)
|
tr_backtesting_list.append(timerange_backtest.timerange_str)
|
||||||
stop = datetime.fromtimestamp(timerange_backtest.stopts, tz=timezone.utc)
|
|
||||||
tr_backtesting_list.append(start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d"))
|
|
||||||
tr_backtesting_list_timerange.append(copy.deepcopy(timerange_backtest))
|
tr_backtesting_list_timerange.append(copy.deepcopy(timerange_backtest))
|
||||||
|
|
||||||
# ensure we are predicting on exactly same amount of data as requested by user defined
|
# ensure we are predicting on exactly same amount of data as requested by user defined
|
||||||
@ -462,12 +462,10 @@ class FreqaiDataKitchen:
|
|||||||
:param df: Dataframe containing all candles to run the entire backtest. Here
|
:param df: Dataframe containing all candles to run the entire backtest. Here
|
||||||
it is sliced down to just the present training period.
|
it is sliced down to just the present training period.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
start = datetime.fromtimestamp(timerange.startts, tz=timezone.utc)
|
|
||||||
stop = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc)
|
|
||||||
df = df.loc[df["date"] >= start, :]
|
|
||||||
if not self.live:
|
if not self.live:
|
||||||
df = df.loc[df["date"] < stop, :]
|
df = df.loc[(df["date"] >= timerange.startdt) & (df["date"] < timerange.stopdt), :]
|
||||||
|
else:
|
||||||
|
df = df.loc[df["date"] >= timerange.startdt, :]
|
||||||
|
|
||||||
return df
|
return df
|
||||||
|
|
||||||
@ -952,7 +950,8 @@ class FreqaiDataKitchen:
|
|||||||
return weights
|
return weights
|
||||||
|
|
||||||
def get_predictions_to_append(self, predictions: DataFrame,
|
def get_predictions_to_append(self, predictions: DataFrame,
|
||||||
do_predict: npt.ArrayLike) -> DataFrame:
|
do_predict: npt.ArrayLike,
|
||||||
|
dataframe_backtest: DataFrame) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
Get backtest prediction from current backtest period
|
Get backtest prediction from current backtest period
|
||||||
"""
|
"""
|
||||||
@ -962,7 +961,9 @@ class FreqaiDataKitchen:
|
|||||||
append_df[label] = predictions[label]
|
append_df[label] = predictions[label]
|
||||||
if append_df[label].dtype == object:
|
if append_df[label].dtype == object:
|
||||||
continue
|
continue
|
||||||
|
if "labels_mean" in self.data:
|
||||||
append_df[f"{label}_mean"] = self.data["labels_mean"][label]
|
append_df[f"{label}_mean"] = self.data["labels_mean"][label]
|
||||||
|
if "labels_std" in self.data:
|
||||||
append_df[f"{label}_std"] = self.data["labels_std"][label]
|
append_df[f"{label}_std"] = self.data["labels_std"][label]
|
||||||
|
|
||||||
for extra_col in self.data["extra_returns_per_train"]:
|
for extra_col in self.data["extra_returns_per_train"]:
|
||||||
@ -972,7 +973,9 @@ class FreqaiDataKitchen:
|
|||||||
if self.freqai_config["feature_parameters"].get("DI_threshold", 0) > 0:
|
if self.freqai_config["feature_parameters"].get("DI_threshold", 0) > 0:
|
||||||
append_df["DI_values"] = self.DI_values
|
append_df["DI_values"] = self.DI_values
|
||||||
|
|
||||||
return append_df
|
dataframe_backtest.reset_index(drop=True, inplace=True)
|
||||||
|
merged_df = pd.concat([dataframe_backtest["date"], append_df], axis=1)
|
||||||
|
return merged_df
|
||||||
|
|
||||||
def append_predictions(self, append_df: DataFrame) -> None:
|
def append_predictions(self, append_df: DataFrame) -> None:
|
||||||
"""
|
"""
|
||||||
@ -982,23 +985,18 @@ class FreqaiDataKitchen:
|
|||||||
if self.full_df.empty:
|
if self.full_df.empty:
|
||||||
self.full_df = append_df
|
self.full_df = append_df
|
||||||
else:
|
else:
|
||||||
self.full_df = pd.concat([self.full_df, append_df], axis=0)
|
self.full_df = pd.concat([self.full_df, append_df], axis=0, ignore_index=True)
|
||||||
|
|
||||||
def fill_predictions(self, dataframe):
|
def fill_predictions(self, dataframe):
|
||||||
"""
|
"""
|
||||||
Back fill values to before the backtesting range so that the dataframe matches size
|
Back fill values to before the backtesting range so that the dataframe matches size
|
||||||
when it goes back to the strategy. These rows are not included in the backtest.
|
when it goes back to the strategy. These rows are not included in the backtest.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
len_filler = len(dataframe) - len(self.full_df.index) # startup_candle_count
|
|
||||||
filler_df = pd.DataFrame(
|
|
||||||
np.zeros((len_filler, len(self.full_df.columns))), columns=self.full_df.columns
|
|
||||||
)
|
|
||||||
|
|
||||||
self.full_df = pd.concat([filler_df, self.full_df], axis=0, ignore_index=True)
|
|
||||||
|
|
||||||
to_keep = [col for col in dataframe.columns if not col.startswith("&")]
|
to_keep = [col for col in dataframe.columns if not col.startswith("&")]
|
||||||
self.return_dataframe = pd.concat([dataframe[to_keep], self.full_df], axis=1)
|
self.return_dataframe = pd.merge(dataframe[to_keep],
|
||||||
|
self.full_df, how='left', on='date')
|
||||||
|
self.return_dataframe[self.full_df.columns] = (
|
||||||
|
self.return_dataframe[self.full_df.columns].fillna(value=0))
|
||||||
self.full_df = DataFrame()
|
self.full_df = DataFrame()
|
||||||
|
|
||||||
return
|
return
|
||||||
@ -1028,14 +1026,7 @@ class FreqaiDataKitchen:
|
|||||||
backtest_timerange.startts = (
|
backtest_timerange.startts = (
|
||||||
backtest_timerange.startts - backtest_period_days * SECONDS_IN_DAY
|
backtest_timerange.startts - backtest_period_days * SECONDS_IN_DAY
|
||||||
)
|
)
|
||||||
start = datetime.fromtimestamp(backtest_timerange.startts, tz=timezone.utc)
|
full_timerange = backtest_timerange.timerange_str
|
||||||
stop = datetime.fromtimestamp(backtest_timerange.stopts, tz=timezone.utc)
|
|
||||||
full_timerange = start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d")
|
|
||||||
|
|
||||||
self.full_path = Path(
|
|
||||||
self.config["user_data_dir"] / "models" / f"{self.freqai_config['identifier']}"
|
|
||||||
)
|
|
||||||
|
|
||||||
config_path = Path(self.config["config_files"][0])
|
config_path = Path(self.config["config_files"][0])
|
||||||
|
|
||||||
if not self.full_path.is_dir():
|
if not self.full_path.is_dir():
|
||||||
@ -1118,15 +1109,15 @@ class FreqaiDataKitchen:
|
|||||||
|
|
||||||
return retrain, trained_timerange, data_load_timerange
|
return retrain, trained_timerange, data_load_timerange
|
||||||
|
|
||||||
def set_new_model_names(self, pair: str, trained_timerange: TimeRange):
|
def set_new_model_names(self, pair: str, timestamp_id: int):
|
||||||
|
|
||||||
coin, _ = pair.split("/")
|
coin, _ = pair.split("/")
|
||||||
self.data_path = Path(
|
self.data_path = Path(
|
||||||
self.full_path
|
self.full_path
|
||||||
/ f"sub-train-{pair.split('/')[0]}_{int(trained_timerange.stopts)}"
|
/ f"sub-train-{pair.split('/')[0]}_{timestamp_id}"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.model_filename = f"cb_{coin.lower()}_{int(trained_timerange.stopts)}"
|
self.model_filename = f"cb_{coin.lower()}_{timestamp_id}"
|
||||||
|
|
||||||
def set_all_pairs(self) -> None:
|
def set_all_pairs(self) -> None:
|
||||||
|
|
||||||
@ -1153,9 +1144,11 @@ class FreqaiDataKitchen:
|
|||||||
pairs = self.freqai_config["feature_parameters"].get("include_corr_pairlist", [])
|
pairs = self.freqai_config["feature_parameters"].get("include_corr_pairlist", [])
|
||||||
|
|
||||||
for pair in pairs:
|
for pair in pairs:
|
||||||
|
pair = pair.replace(':', '') # lightgbm doesnt like colons
|
||||||
valid_strs = [f"%-{pair}", f"%{pair}", f"%_{pair}"]
|
valid_strs = [f"%-{pair}", f"%{pair}", f"%_{pair}"]
|
||||||
pair_cols = [col for col in dataframe.columns if
|
pair_cols = [col for col in dataframe.columns if
|
||||||
any(substr in col for substr in valid_strs)]
|
any(substr in col for substr in valid_strs)]
|
||||||
|
if pair_cols:
|
||||||
pair_cols.insert(0, 'date')
|
pair_cols.insert(0, 'date')
|
||||||
corr_dataframes[pair] = dataframe.filter(pair_cols, axis=1)
|
corr_dataframes[pair] = dataframe.filter(pair_cols, axis=1)
|
||||||
|
|
||||||
@ -1175,8 +1168,9 @@ class FreqaiDataKitchen:
|
|||||||
ready for training
|
ready for training
|
||||||
"""
|
"""
|
||||||
pairs = self.freqai_config["feature_parameters"].get("include_corr_pairlist", [])
|
pairs = self.freqai_config["feature_parameters"].get("include_corr_pairlist", [])
|
||||||
|
current_pair = current_pair.replace(':', '')
|
||||||
for pair in pairs:
|
for pair in pairs:
|
||||||
|
pair = pair.replace(':', '') # lightgbm doesnt work with colons
|
||||||
if current_pair != pair:
|
if current_pair != pair:
|
||||||
dataframe = dataframe.merge(corr_dataframes[pair], how='left', on='date')
|
dataframe = dataframe.merge(corr_dataframes[pair], how='left', on='date')
|
||||||
|
|
||||||
@ -1246,6 +1240,11 @@ class FreqaiDataKitchen:
|
|||||||
|
|
||||||
self.get_unique_classes_from_labels(dataframe)
|
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
|
return dataframe
|
||||||
|
|
||||||
def fit_labels(self) -> None:
|
def fit_labels(self) -> None:
|
||||||
@ -1294,53 +1293,77 @@ class FreqaiDataKitchen:
|
|||||||
self, append_df: DataFrame
|
self, append_df: DataFrame
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Save prediction dataframe from backtesting to h5 file format
|
Save prediction dataframe from backtesting to feather file format
|
||||||
:param append_df: dataframe for backtesting period
|
:param append_df: dataframe for backtesting period
|
||||||
"""
|
"""
|
||||||
full_predictions_folder = Path(self.full_path / self.backtest_predictions_folder)
|
full_predictions_folder = Path(self.full_path / self.backtest_predictions_folder)
|
||||||
if not full_predictions_folder.is_dir():
|
if not full_predictions_folder.is_dir():
|
||||||
full_predictions_folder.mkdir(parents=True, exist_ok=True)
|
full_predictions_folder.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
append_df.to_hdf(self.backtesting_results_path, key='append_df', mode='w')
|
append_df.to_feather(self.backtesting_results_path)
|
||||||
|
|
||||||
def get_backtesting_prediction(
|
def get_backtesting_prediction(
|
||||||
self
|
self
|
||||||
) -> DataFrame:
|
) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
Get prediction dataframe from h5 file format
|
Get prediction dataframe from feather file format
|
||||||
"""
|
"""
|
||||||
append_df = pd.read_hdf(self.backtesting_results_path)
|
append_df = pd.read_feather(self.backtesting_results_path)
|
||||||
return append_df
|
return append_df
|
||||||
|
|
||||||
def check_if_backtest_prediction_is_valid(
|
def check_if_backtest_prediction_is_valid(
|
||||||
self,
|
self,
|
||||||
length_backtesting_dataframe: int
|
len_backtest_df: int
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
Check if a backtesting prediction already exists and if the predictions
|
Check if a backtesting prediction already exists and if the predictions
|
||||||
to append has the same size of backtesting dataframe slice
|
to append have the same size as the backtesting dataframe slice
|
||||||
:param length_backtesting_dataframe: Length of backtesting dataframe slice
|
:param length_backtesting_dataframe: Length of backtesting dataframe slice
|
||||||
:return:
|
:return:
|
||||||
:boolean: whether the prediction file is valid.
|
:boolean: whether the prediction file is valid.
|
||||||
"""
|
"""
|
||||||
path_to_predictionfile = Path(self.full_path /
|
path_to_predictionfile = Path(self.full_path /
|
||||||
self.backtest_predictions_folder /
|
self.backtest_predictions_folder /
|
||||||
f"{self.model_filename}_prediction.h5")
|
f"{self.model_filename}_prediction.feather")
|
||||||
self.backtesting_results_path = path_to_predictionfile
|
self.backtesting_results_path = path_to_predictionfile
|
||||||
|
|
||||||
file_exists = path_to_predictionfile.is_file()
|
file_exists = path_to_predictionfile.is_file()
|
||||||
|
|
||||||
if file_exists:
|
if file_exists:
|
||||||
append_df = self.get_backtesting_prediction()
|
append_df = self.get_backtesting_prediction()
|
||||||
if len(append_df) == length_backtesting_dataframe:
|
if len(append_df) == len_backtest_df and 'date' in append_df:
|
||||||
logger.info(f"Found backtesting prediction file at {path_to_predictionfile}")
|
logger.info(f"Found backtesting prediction file at {path_to_predictionfile}")
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
logger.info("A new backtesting prediction file is required. "
|
logger.info("A new backtesting prediction file is required. "
|
||||||
"(Number of predictions is different from dataframe length).")
|
"(Number of predictions is different from dataframe length or "
|
||||||
|
"old prediction file version).")
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Could not find backtesting prediction file at {path_to_predictionfile}"
|
f"Could not find backtesting prediction file at {path_to_predictionfile}"
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def get_full_models_path(self, config: Config) -> Path:
|
||||||
|
"""
|
||||||
|
Returns default FreqAI model path
|
||||||
|
:param config: Configuration dictionary
|
||||||
|
"""
|
||||||
|
freqai_config: Dict[str, Any] = config["freqai"]
|
||||||
|
return Path(
|
||||||
|
config["user_data_dir"] / "models" / str(freqai_config.get("identifier"))
|
||||||
|
)
|
||||||
|
|
||||||
|
def remove_special_chars_from_feature_names(self, dataframe: pd.DataFrame) -> pd.DataFrame:
|
||||||
|
"""
|
||||||
|
Remove all special characters from feature strings (:)
|
||||||
|
:param dataframe: the dataframe that just finished indicator population. (unfiltered)
|
||||||
|
:return: dataframe with cleaned featrue names
|
||||||
|
"""
|
||||||
|
|
||||||
|
spec_chars = [':']
|
||||||
|
for c in spec_chars:
|
||||||
|
dataframe.columns = dataframe.columns.str.replace(c, "")
|
||||||
|
|
||||||
|
return dataframe
|
||||||
|
@ -5,15 +5,17 @@ from abc import ABC, abstractmethod
|
|||||||
from collections import deque
|
from collections import deque
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, List, Literal, Tuple
|
from typing import Any, Dict, List, Literal, Optional, Tuple
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
import psutil
|
||||||
from numpy.typing import NDArray
|
from numpy.typing import NDArray
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
|
|
||||||
from freqtrade.configuration import TimeRange
|
from freqtrade.configuration import TimeRange
|
||||||
from freqtrade.constants import DATETIME_PRINT_FORMAT, Config
|
from freqtrade.constants import Config
|
||||||
|
from freqtrade.data.dataprovider import DataProvider
|
||||||
from freqtrade.enums import RunMode
|
from freqtrade.enums import RunMode
|
||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
from freqtrade.exchange import timeframe_to_seconds
|
from freqtrade.exchange import timeframe_to_seconds
|
||||||
@ -67,7 +69,11 @@ class IFreqaiModel(ABC):
|
|||||||
self.save_backtest_models: bool = self.freqai_info.get("save_backtest_models", True)
|
self.save_backtest_models: bool = self.freqai_info.get("save_backtest_models", True)
|
||||||
if self.save_backtest_models:
|
if self.save_backtest_models:
|
||||||
logger.info('Backtesting module configured to save all 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, self.follow_mode)
|
||||||
|
# set current candle to arbitrary historical date
|
||||||
|
self.current_candle: datetime = datetime.fromtimestamp(637887600, tz=timezone.utc)
|
||||||
|
self.dd.current_candle = self.current_candle
|
||||||
self.scanning = False
|
self.scanning = False
|
||||||
self.ft_params = self.freqai_info["feature_parameters"]
|
self.ft_params = self.freqai_info["feature_parameters"]
|
||||||
self.corr_pairlist: List[str] = self.ft_params.get("include_corr_pairlist", [])
|
self.corr_pairlist: List[str] = self.ft_params.get("include_corr_pairlist", [])
|
||||||
@ -75,7 +81,7 @@ class IFreqaiModel(ABC):
|
|||||||
if self.keras and self.ft_params.get("DI_threshold", 0):
|
if self.keras and self.ft_params.get("DI_threshold", 0):
|
||||||
self.ft_params["DI_threshold"] = 0
|
self.ft_params["DI_threshold"] = 0
|
||||||
logger.warning("DI threshold is not configured for Keras models yet. Deactivating.")
|
logger.warning("DI threshold is not configured for Keras models yet. Deactivating.")
|
||||||
self.CONV_WIDTH = self.freqai_info.get("conv_width", 2)
|
self.CONV_WIDTH = self.freqai_info.get('conv_width', 1)
|
||||||
if self.ft_params.get("inlier_metric_window", 0):
|
if self.ft_params.get("inlier_metric_window", 0):
|
||||||
self.CONV_WIDTH = self.ft_params.get("inlier_metric_window", 0) * 2
|
self.CONV_WIDTH = self.ft_params.get("inlier_metric_window", 0) * 2
|
||||||
self.pair_it = 0
|
self.pair_it = 0
|
||||||
@ -93,9 +99,11 @@ class IFreqaiModel(ABC):
|
|||||||
# get_corr_dataframes is controlling the caching of corr_dataframes
|
# get_corr_dataframes is controlling the caching of corr_dataframes
|
||||||
# for improved performance. Careful with this boolean.
|
# for improved performance. Careful with this boolean.
|
||||||
self.get_corr_dataframes: bool = True
|
self.get_corr_dataframes: bool = True
|
||||||
|
|
||||||
self._threads: List[threading.Thread] = []
|
self._threads: List[threading.Thread] = []
|
||||||
self._stop_event = threading.Event()
|
self._stop_event = threading.Event()
|
||||||
|
self.metadata: Dict[str, Any] = self.dd.load_global_metadata_from_disk()
|
||||||
|
self.data_provider: Optional[DataProvider] = None
|
||||||
|
self.max_system_threads = max(int(psutil.cpu_count() * 2 - 2), 1)
|
||||||
|
|
||||||
record_params(config, self.full_path)
|
record_params(config, self.full_path)
|
||||||
|
|
||||||
@ -124,11 +132,13 @@ class IFreqaiModel(ABC):
|
|||||||
|
|
||||||
self.live = strategy.dp.runmode in (RunMode.DRY_RUN, RunMode.LIVE)
|
self.live = strategy.dp.runmode in (RunMode.DRY_RUN, RunMode.LIVE)
|
||||||
self.dd.set_pair_dict_info(metadata)
|
self.dd.set_pair_dict_info(metadata)
|
||||||
|
self.data_provider = strategy.dp
|
||||||
|
|
||||||
if self.live:
|
if self.live:
|
||||||
self.inference_timer('start')
|
self.inference_timer('start')
|
||||||
self.dk = FreqaiDataKitchen(self.config, self.live, metadata["pair"])
|
self.dk = FreqaiDataKitchen(self.config, self.live, metadata["pair"])
|
||||||
dk = self.start_live(dataframe, metadata, strategy, self.dk)
|
dk = self.start_live(dataframe, metadata, strategy, self.dk)
|
||||||
|
dataframe = dk.remove_features_from_df(dk.return_dataframe)
|
||||||
|
|
||||||
# For backtesting, each pair enters and then gets trained for each window along the
|
# For backtesting, each pair enters and then gets trained for each window along the
|
||||||
# sliding window defined by "train_period_days" (training window) and "live_retrain_hours"
|
# sliding window defined by "train_period_days" (training window) and "live_retrain_hours"
|
||||||
@ -137,16 +147,24 @@ class IFreqaiModel(ABC):
|
|||||||
# the concatenated results for the full backtesting period back to the strategy.
|
# the concatenated results for the full backtesting period back to the strategy.
|
||||||
elif not self.follow_mode:
|
elif not self.follow_mode:
|
||||||
self.dk = FreqaiDataKitchen(self.config, self.live, metadata["pair"])
|
self.dk = FreqaiDataKitchen(self.config, self.live, metadata["pair"])
|
||||||
logger.info(f"Training {len(self.dk.training_timeranges)} timeranges")
|
|
||||||
dataframe = self.dk.use_strategy_to_populate_indicators(
|
dataframe = self.dk.use_strategy_to_populate_indicators(
|
||||||
strategy, prediction_dataframe=dataframe, pair=metadata["pair"]
|
strategy, prediction_dataframe=dataframe, pair=metadata["pair"]
|
||||||
)
|
)
|
||||||
|
if not self.config.get("freqai_backtest_live_models", False):
|
||||||
|
logger.info(f"Training {len(self.dk.training_timeranges)} timeranges")
|
||||||
dk = self.start_backtesting(dataframe, metadata, self.dk)
|
dk = self.start_backtesting(dataframe, metadata, self.dk)
|
||||||
|
|
||||||
dataframe = dk.remove_features_from_df(dk.return_dataframe)
|
dataframe = dk.remove_features_from_df(dk.return_dataframe)
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
"Backtesting using historic predictions (live models)")
|
||||||
|
dk = self.start_backtesting_from_historic_predictions(
|
||||||
|
dataframe, metadata, self.dk)
|
||||||
|
dataframe = dk.return_dataframe
|
||||||
|
|
||||||
self.clean_up()
|
self.clean_up()
|
||||||
if self.live:
|
if self.live:
|
||||||
self.inference_timer('stop', metadata["pair"])
|
self.inference_timer('stop', metadata["pair"])
|
||||||
|
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def clean_up(self):
|
def clean_up(self):
|
||||||
@ -158,6 +176,13 @@ class IFreqaiModel(ABC):
|
|||||||
self.model = None
|
self.model = None
|
||||||
self.dk = None
|
self.dk = None
|
||||||
|
|
||||||
|
def _on_stop(self):
|
||||||
|
"""
|
||||||
|
Callback for Subclasses to override to include logic for shutting down resources
|
||||||
|
when SIGINT is sent.
|
||||||
|
"""
|
||||||
|
return
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
"""
|
"""
|
||||||
Cleans up threads on Shutdown, set stop event. Join threads to wait
|
Cleans up threads on Shutdown, set stop event. Join threads to wait
|
||||||
@ -166,6 +191,9 @@ class IFreqaiModel(ABC):
|
|||||||
logger.info("Stopping FreqAI")
|
logger.info("Stopping FreqAI")
|
||||||
self._stop_event.set()
|
self._stop_event.set()
|
||||||
|
|
||||||
|
self.data_provider = None
|
||||||
|
self._on_stop()
|
||||||
|
|
||||||
logger.info("Waiting on Training iteration")
|
logger.info("Waiting on Training iteration")
|
||||||
for _thread in self._threads:
|
for _thread in self._threads:
|
||||||
_thread.join()
|
_thread.join()
|
||||||
@ -254,40 +282,37 @@ class IFreqaiModel(ABC):
|
|||||||
train_it += 1
|
train_it += 1
|
||||||
total_trains = len(dk.backtesting_timeranges)
|
total_trains = len(dk.backtesting_timeranges)
|
||||||
self.training_timerange = tr_train
|
self.training_timerange = tr_train
|
||||||
dataframe_train = dk.slice_dataframe(tr_train, dataframe)
|
len_backtest_df = len(dataframe.loc[(dataframe["date"] >= tr_backtest.startdt) & (
|
||||||
dataframe_backtest = dk.slice_dataframe(tr_backtest, dataframe)
|
dataframe["date"] < tr_backtest.stopdt), :])
|
||||||
|
|
||||||
trained_timestamp = tr_train
|
if not self.ensure_data_exists(len_backtest_df, tr_backtest, pair):
|
||||||
tr_train_startts_str = datetime.fromtimestamp(
|
continue
|
||||||
tr_train.startts,
|
|
||||||
tz=timezone.utc).strftime(DATETIME_PRINT_FORMAT)
|
|
||||||
tr_train_stopts_str = datetime.fromtimestamp(
|
|
||||||
tr_train.stopts,
|
|
||||||
tz=timezone.utc).strftime(DATETIME_PRINT_FORMAT)
|
|
||||||
logger.info(
|
|
||||||
f"Training {pair}, {self.pair_it}/{self.total_pairs} pairs"
|
|
||||||
f" from {tr_train_startts_str} to {tr_train_stopts_str}, {train_it}/{total_trains} "
|
|
||||||
"trains"
|
|
||||||
)
|
|
||||||
|
|
||||||
trained_timestamp_int = int(trained_timestamp.stopts)
|
self.log_backtesting_progress(tr_train, pair, train_it, total_trains)
|
||||||
dk.set_paths(pair, trained_timestamp_int)
|
|
||||||
|
|
||||||
dk.set_new_model_names(pair, trained_timestamp)
|
timestamp_model_id = int(tr_train.stopts)
|
||||||
|
if dk.backtest_live_models:
|
||||||
|
timestamp_model_id = int(tr_backtest.startts)
|
||||||
|
|
||||||
if dk.check_if_backtest_prediction_is_valid(len(dataframe_backtest)):
|
dk.set_paths(pair, timestamp_model_id)
|
||||||
|
|
||||||
|
dk.set_new_model_names(pair, timestamp_model_id)
|
||||||
|
|
||||||
|
if dk.check_if_backtest_prediction_is_valid(len_backtest_df):
|
||||||
self.dd.load_metadata(dk)
|
self.dd.load_metadata(dk)
|
||||||
dk.find_features(dataframe_train)
|
dk.find_features(dataframe)
|
||||||
self.check_if_feature_list_matches_strategy(dk)
|
self.check_if_feature_list_matches_strategy(dk)
|
||||||
append_df = dk.get_backtesting_prediction()
|
append_df = dk.get_backtesting_prediction()
|
||||||
dk.append_predictions(append_df)
|
dk.append_predictions(append_df)
|
||||||
else:
|
else:
|
||||||
|
dataframe_train = dk.slice_dataframe(tr_train, dataframe)
|
||||||
|
dataframe_backtest = dk.slice_dataframe(tr_backtest, dataframe)
|
||||||
if not self.model_exists(dk):
|
if not self.model_exists(dk):
|
||||||
dk.find_features(dataframe_train)
|
dk.find_features(dataframe_train)
|
||||||
dk.find_labels(dataframe_train)
|
dk.find_labels(dataframe_train)
|
||||||
self.model = self.train(dataframe_train, pair, dk)
|
self.model = self.train(dataframe_train, pair, dk)
|
||||||
self.dd.pair_dict[pair]["trained_timestamp"] = int(
|
self.dd.pair_dict[pair]["trained_timestamp"] = int(
|
||||||
trained_timestamp.stopts)
|
tr_train.stopts)
|
||||||
if self.plot_features:
|
if self.plot_features:
|
||||||
plot_feature_importance(self.model, pair, dk, self.plot_features)
|
plot_feature_importance(self.model, pair, dk, self.plot_features)
|
||||||
if self.save_backtest_models:
|
if self.save_backtest_models:
|
||||||
@ -300,10 +325,11 @@ class IFreqaiModel(ABC):
|
|||||||
self.model = self.dd.load_data(pair, dk)
|
self.model = self.dd.load_data(pair, dk)
|
||||||
|
|
||||||
pred_df, do_preds = self.predict(dataframe_backtest, dk)
|
pred_df, do_preds = self.predict(dataframe_backtest, dk)
|
||||||
append_df = dk.get_predictions_to_append(pred_df, do_preds)
|
append_df = dk.get_predictions_to_append(pred_df, do_preds, dataframe_backtest)
|
||||||
dk.append_predictions(append_df)
|
dk.append_predictions(append_df)
|
||||||
dk.save_backtesting_prediction(append_df)
|
dk.save_backtesting_prediction(append_df)
|
||||||
|
|
||||||
|
self.backtesting_fit_live_predictions(dk)
|
||||||
dk.fill_predictions(dataframe)
|
dk.fill_predictions(dataframe)
|
||||||
|
|
||||||
return dk
|
return dk
|
||||||
@ -339,6 +365,7 @@ class IFreqaiModel(ABC):
|
|||||||
if self.dd.historic_data:
|
if self.dd.historic_data:
|
||||||
self.dd.update_historic_data(strategy, dk)
|
self.dd.update_historic_data(strategy, dk)
|
||||||
logger.debug(f'Updating historic data on pair {metadata["pair"]}')
|
logger.debug(f'Updating historic data on pair {metadata["pair"]}')
|
||||||
|
self.track_current_candle()
|
||||||
|
|
||||||
if not self.follow_mode:
|
if not self.follow_mode:
|
||||||
|
|
||||||
@ -576,7 +603,7 @@ class IFreqaiModel(ABC):
|
|||||||
model = self.train(unfiltered_dataframe, pair, dk)
|
model = self.train(unfiltered_dataframe, pair, dk)
|
||||||
|
|
||||||
self.dd.pair_dict[pair]["trained_timestamp"] = new_trained_timerange.stopts
|
self.dd.pair_dict[pair]["trained_timestamp"] = new_trained_timerange.stopts
|
||||||
dk.set_new_model_names(pair, new_trained_timerange)
|
dk.set_new_model_names(pair, new_trained_timerange.stopts)
|
||||||
self.dd.save_data(model, pair, dk)
|
self.dd.save_data(model, pair, dk)
|
||||||
|
|
||||||
if self.plot_features:
|
if self.plot_features:
|
||||||
@ -615,6 +642,8 @@ class IFreqaiModel(ABC):
|
|||||||
self.dd.historic_predictions[pair] = pred_df
|
self.dd.historic_predictions[pair] = pred_df
|
||||||
hist_preds_df = self.dd.historic_predictions[pair]
|
hist_preds_df = self.dd.historic_predictions[pair]
|
||||||
|
|
||||||
|
self.set_start_dry_live_date(strat_df)
|
||||||
|
|
||||||
for label in hist_preds_df.columns:
|
for label in hist_preds_df.columns:
|
||||||
if hist_preds_df[label].dtype == object:
|
if hist_preds_df[label].dtype == object:
|
||||||
continue
|
continue
|
||||||
@ -627,7 +656,7 @@ class IFreqaiModel(ABC):
|
|||||||
hist_preds_df['DI_values'] = 0
|
hist_preds_df['DI_values'] = 0
|
||||||
|
|
||||||
for return_str in dk.data['extra_returns_per_train']:
|
for return_str in dk.data['extra_returns_per_train']:
|
||||||
hist_preds_df[return_str] = 0
|
hist_preds_df[return_str] = dk.data['extra_returns_per_train'][return_str]
|
||||||
|
|
||||||
hist_preds_df['close_price'] = strat_df['close']
|
hist_preds_df['close_price'] = strat_df['close']
|
||||||
hist_preds_df['date_pred'] = strat_df['date']
|
hist_preds_df['date_pred'] = strat_df['date']
|
||||||
@ -655,7 +684,8 @@ class IFreqaiModel(ABC):
|
|||||||
for label in full_labels:
|
for label in full_labels:
|
||||||
if self.dd.historic_predictions[dk.pair][label].dtype == object:
|
if self.dd.historic_predictions[dk.pair][label].dtype == object:
|
||||||
continue
|
continue
|
||||||
f = spy.stats.norm.fit(self.dd.historic_predictions[dk.pair][label].tail(num_candles))
|
f = spy.stats.norm.fit(
|
||||||
|
self.dd.historic_predictions[dk.pair][label].tail(num_candles))
|
||||||
dk.data["labels_mean"][label], dk.data["labels_std"][label] = f[0], f[1]
|
dk.data["labels_mean"][label], dk.data["labels_std"][label] = f[0], f[1]
|
||||||
|
|
||||||
return
|
return
|
||||||
@ -683,8 +713,6 @@ class IFreqaiModel(ABC):
|
|||||||
" avoid blinding open trades and degrading performance.")
|
" avoid blinding open trades and degrading performance.")
|
||||||
self.pair_it = 0
|
self.pair_it = 0
|
||||||
self.inference_time = 0
|
self.inference_time = 0
|
||||||
if self.corr_pairlist:
|
|
||||||
self.get_corr_dataframes = True
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def train_timer(self, do: Literal['start', 'stop'] = 'start', pair: str = ''):
|
def train_timer(self, do: Literal['start', 'stop'] = 'start', pair: str = ''):
|
||||||
@ -760,12 +788,132 @@ class IFreqaiModel(ABC):
|
|||||||
"is included in the column names when you are creating features "
|
"is included in the column names when you are creating features "
|
||||||
"in `populate_any_indicators()`.")
|
"in `populate_any_indicators()`.")
|
||||||
self.get_corr_dataframes = not bool(self.corr_dataframes)
|
self.get_corr_dataframes = not bool(self.corr_dataframes)
|
||||||
else:
|
elif self.corr_dataframes:
|
||||||
dataframe = dk.attach_corr_pair_columns(
|
dataframe = dk.attach_corr_pair_columns(
|
||||||
dataframe, self.corr_dataframes, dk.pair)
|
dataframe, self.corr_dataframes, dk.pair)
|
||||||
|
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
|
def track_current_candle(self):
|
||||||
|
"""
|
||||||
|
Checks if the latest candle appended by the datadrawer is
|
||||||
|
equivalent to the latest candle seen by FreqAI. If not, it
|
||||||
|
asks to refresh the cached corr_dfs, and resets the pair
|
||||||
|
counter.
|
||||||
|
"""
|
||||||
|
if self.dd.current_candle > self.current_candle:
|
||||||
|
self.get_corr_dataframes = True
|
||||||
|
self.pair_it = 1
|
||||||
|
self.current_candle = self.dd.current_candle
|
||||||
|
|
||||||
|
def ensure_data_exists(self, len_dataframe_backtest: int,
|
||||||
|
tr_backtest: TimeRange, pair: str) -> bool:
|
||||||
|
"""
|
||||||
|
Check if the dataframe is empty, if not, report useful information to user.
|
||||||
|
:param len_dataframe_backtest: the len of backtesting dataframe
|
||||||
|
:param tr_backtest: current backtesting timerange.
|
||||||
|
:param pair: current pair
|
||||||
|
:return: if the data exists or not
|
||||||
|
"""
|
||||||
|
if self.config.get("freqai_backtest_live_models", False) and len_dataframe_backtest == 0:
|
||||||
|
logger.info(f"No data found for pair {pair} from "
|
||||||
|
f"from { tr_backtest.start_fmt} to {tr_backtest.stop_fmt}. "
|
||||||
|
"Probably more than one training within the same candle period.")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def log_backtesting_progress(self, tr_train: TimeRange, pair: str,
|
||||||
|
train_it: int, total_trains: int):
|
||||||
|
"""
|
||||||
|
Log the backtesting progress so user knows how many pairs have been trained and
|
||||||
|
how many more pairs/trains remain.
|
||||||
|
:param tr_train: the training timerange
|
||||||
|
:param train_it: the train iteration for the current pair (the sliding window progress)
|
||||||
|
:param pair: the current pair
|
||||||
|
:param total_trains: total trains (total number of slides for the sliding window)
|
||||||
|
"""
|
||||||
|
if not self.config.get("freqai_backtest_live_models", False):
|
||||||
|
logger.info(
|
||||||
|
f"Training {pair}, {self.pair_it}/{self.total_pairs} pairs"
|
||||||
|
f" from {tr_train.start_fmt} "
|
||||||
|
f"to {tr_train.stop_fmt}, {train_it}/{total_trains} "
|
||||||
|
"trains"
|
||||||
|
)
|
||||||
|
|
||||||
|
def backtesting_fit_live_predictions(self, dk: FreqaiDataKitchen):
|
||||||
|
"""
|
||||||
|
Apply fit_live_predictions function in backtesting with a dummy historic_predictions
|
||||||
|
The loop is required to simulate dry/live operation, as it is not possible to predict
|
||||||
|
the type of logic implemented by the user.
|
||||||
|
:param dk: datakitchen object
|
||||||
|
"""
|
||||||
|
fit_live_predictions_candles = self.freqai_info.get("fit_live_predictions_candles", 0)
|
||||||
|
if fit_live_predictions_candles:
|
||||||
|
logger.info("Applying fit_live_predictions in backtesting")
|
||||||
|
label_columns = [col for col in dk.full_df.columns if (
|
||||||
|
col.startswith("&") and
|
||||||
|
not (col.startswith("&") and col.endswith("_mean")) and
|
||||||
|
not (col.startswith("&") and col.endswith("_std")) and
|
||||||
|
col not in self.dk.data["extra_returns_per_train"])
|
||||||
|
]
|
||||||
|
|
||||||
|
for index in range(len(dk.full_df)):
|
||||||
|
if index >= fit_live_predictions_candles:
|
||||||
|
self.dd.historic_predictions[self.dk.pair] = (
|
||||||
|
dk.full_df.iloc[index - fit_live_predictions_candles:index])
|
||||||
|
self.fit_live_predictions(self.dk, self.dk.pair)
|
||||||
|
for label in label_columns:
|
||||||
|
if dk.full_df[label].dtype == object:
|
||||||
|
continue
|
||||||
|
if "labels_mean" in self.dk.data:
|
||||||
|
dk.full_df.at[index, f"{label}_mean"] = (
|
||||||
|
self.dk.data["labels_mean"][label])
|
||||||
|
if "labels_std" in self.dk.data:
|
||||||
|
dk.full_df.at[index, f"{label}_std"] = self.dk.data["labels_std"][label]
|
||||||
|
|
||||||
|
for extra_col in self.dk.data["extra_returns_per_train"]:
|
||||||
|
dk.full_df.at[index, f"{extra_col}"] = (
|
||||||
|
self.dk.data["extra_returns_per_train"][extra_col])
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
def update_metadata(self, metadata: Dict[str, Any]):
|
||||||
|
"""
|
||||||
|
Update global metadata and save the updated json file
|
||||||
|
:param metadata: new global metadata dict
|
||||||
|
"""
|
||||||
|
self.dd.save_global_metadata_to_disk(metadata)
|
||||||
|
self.metadata = metadata
|
||||||
|
|
||||||
|
def set_start_dry_live_date(self, live_dataframe: DataFrame):
|
||||||
|
key_name = "start_dry_live_date"
|
||||||
|
if key_name not in self.metadata:
|
||||||
|
metadata = self.metadata
|
||||||
|
metadata[key_name] = int(
|
||||||
|
pd.to_datetime(live_dataframe.tail(1)["date"].values[0]).timestamp())
|
||||||
|
self.update_metadata(metadata)
|
||||||
|
|
||||||
|
def start_backtesting_from_historic_predictions(
|
||||||
|
self, dataframe: DataFrame, metadata: dict, dk: FreqaiDataKitchen
|
||||||
|
) -> FreqaiDataKitchen:
|
||||||
|
"""
|
||||||
|
:param dataframe: DataFrame = strategy passed dataframe
|
||||||
|
:param metadata: Dict = pair metadata
|
||||||
|
:param dk: FreqaiDataKitchen = Data management/analysis tool associated to present pair only
|
||||||
|
:return:
|
||||||
|
FreqaiDataKitchen = Data management/analysis tool associated to present pair only
|
||||||
|
"""
|
||||||
|
pair = metadata["pair"]
|
||||||
|
dk.return_dataframe = dataframe
|
||||||
|
saved_dataframe = self.dd.historic_predictions[pair]
|
||||||
|
columns_to_drop = list(set(saved_dataframe.columns).intersection(
|
||||||
|
dk.return_dataframe.columns))
|
||||||
|
dk.return_dataframe = dk.return_dataframe.drop(columns=list(columns_to_drop))
|
||||||
|
dk.return_dataframe = pd.merge(
|
||||||
|
dk.return_dataframe, saved_dataframe, how='left', left_on='date', right_on="date_pred")
|
||||||
|
# dk.return_dataframe = dk.return_dataframe[saved_dataframe.columns].fillna(0)
|
||||||
|
return dk
|
||||||
|
|
||||||
# Following methods which are overridden by user made prediction models.
|
# Following methods which are overridden by user made prediction models.
|
||||||
# See freqai/prediction_models/CatboostPredictionModel.py for an example.
|
# See freqai/prediction_models/CatboostPredictionModel.py for an example.
|
||||||
|
|
||||||
|
@ -0,0 +1,74 @@
|
|||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from catboost import CatBoostClassifier, Pool
|
||||||
|
|
||||||
|
from freqtrade.freqai.base_models.BaseClassifierModel import BaseClassifierModel
|
||||||
|
from freqtrade.freqai.base_models.FreqaiMultiOutputClassifier import FreqaiMultiOutputClassifier
|
||||||
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class CatboostClassifierMultiTarget(BaseClassifierModel):
|
||||||
|
"""
|
||||||
|
User created prediction model. The class needs to override three necessary
|
||||||
|
functions, predict(), train(), fit(). The class inherits ModelHandler which
|
||||||
|
has its own DataHandler where data is held, saved, loaded, and managed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def fit(self, data_dictionary: Dict, dk: FreqaiDataKitchen, **kwargs) -> Any:
|
||||||
|
"""
|
||||||
|
User sets up the training and test data to fit their desired model here
|
||||||
|
:param data_dictionary: the dictionary constructed by DataHandler to hold
|
||||||
|
all the training and test data/labels.
|
||||||
|
"""
|
||||||
|
|
||||||
|
cbc = CatBoostClassifier(
|
||||||
|
allow_writing_files=True,
|
||||||
|
loss_function='MultiClass',
|
||||||
|
train_dir=Path(dk.data_path),
|
||||||
|
**self.model_training_parameters,
|
||||||
|
)
|
||||||
|
|
||||||
|
X = data_dictionary["train_features"]
|
||||||
|
y = data_dictionary["train_labels"]
|
||||||
|
|
||||||
|
sample_weight = data_dictionary["train_weights"]
|
||||||
|
|
||||||
|
eval_sets = [None] * y.shape[1]
|
||||||
|
|
||||||
|
if self.freqai_info.get('data_split_parameters', {}).get('test_size', 0.1) != 0:
|
||||||
|
eval_sets = [None] * data_dictionary['test_labels'].shape[1]
|
||||||
|
|
||||||
|
for i in range(data_dictionary['test_labels'].shape[1]):
|
||||||
|
eval_sets[i] = Pool(
|
||||||
|
data=data_dictionary["test_features"],
|
||||||
|
label=data_dictionary["test_labels"].iloc[:, i],
|
||||||
|
weight=data_dictionary["test_weights"],
|
||||||
|
)
|
||||||
|
|
||||||
|
init_model = self.get_init_model(dk.pair)
|
||||||
|
|
||||||
|
if init_model:
|
||||||
|
init_models = init_model.estimators_
|
||||||
|
else:
|
||||||
|
init_models = [None] * y.shape[1]
|
||||||
|
|
||||||
|
fit_params = []
|
||||||
|
for i in range(len(eval_sets)):
|
||||||
|
fit_params.append({
|
||||||
|
'eval_set': eval_sets[i], 'init_model': init_models[i],
|
||||||
|
'log_cout': sys.stdout, 'log_cerr': sys.stderr,
|
||||||
|
})
|
||||||
|
|
||||||
|
model = FreqaiMultiOutputClassifier(estimator=cbc)
|
||||||
|
thread_training = self.freqai_info.get('multitarget_parallel_training', False)
|
||||||
|
if thread_training:
|
||||||
|
model.n_jobs = y.shape[1]
|
||||||
|
model.fit(X=X, y=y, sample_weight=sample_weight, fit_params=fit_params)
|
||||||
|
|
||||||
|
return model
|
@ -0,0 +1,64 @@
|
|||||||
|
import logging
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from lightgbm import LGBMClassifier
|
||||||
|
|
||||||
|
from freqtrade.freqai.base_models.BaseClassifierModel import BaseClassifierModel
|
||||||
|
from freqtrade.freqai.base_models.FreqaiMultiOutputClassifier import FreqaiMultiOutputClassifier
|
||||||
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class LightGBMClassifierMultiTarget(BaseClassifierModel):
|
||||||
|
"""
|
||||||
|
User created prediction model. The class needs to override three necessary
|
||||||
|
functions, predict(), train(), fit(). The class inherits ModelHandler which
|
||||||
|
has its own DataHandler where data is held, saved, loaded, and managed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def fit(self, data_dictionary: Dict, dk: FreqaiDataKitchen, **kwargs) -> Any:
|
||||||
|
"""
|
||||||
|
User sets up the training and test data to fit their desired model here
|
||||||
|
:param data_dictionary: the dictionary constructed by DataHandler to hold
|
||||||
|
all the training and test data/labels.
|
||||||
|
"""
|
||||||
|
|
||||||
|
lgb = LGBMClassifier(**self.model_training_parameters)
|
||||||
|
|
||||||
|
X = data_dictionary["train_features"]
|
||||||
|
y = data_dictionary["train_labels"]
|
||||||
|
sample_weight = data_dictionary["train_weights"]
|
||||||
|
|
||||||
|
eval_weights = None
|
||||||
|
eval_sets = [None] * y.shape[1]
|
||||||
|
|
||||||
|
if self.freqai_info.get('data_split_parameters', {}).get('test_size', 0.1) != 0:
|
||||||
|
eval_weights = [data_dictionary["test_weights"]]
|
||||||
|
eval_sets = [(None, None)] * data_dictionary['test_labels'].shape[1] # type: ignore
|
||||||
|
for i in range(data_dictionary['test_labels'].shape[1]):
|
||||||
|
eval_sets[i] = ( # type: ignore
|
||||||
|
data_dictionary["test_features"],
|
||||||
|
data_dictionary["test_labels"].iloc[:, i]
|
||||||
|
)
|
||||||
|
|
||||||
|
init_model = self.get_init_model(dk.pair)
|
||||||
|
if init_model:
|
||||||
|
init_models = init_model.estimators_
|
||||||
|
else:
|
||||||
|
init_models = [None] * y.shape[1]
|
||||||
|
|
||||||
|
fit_params = []
|
||||||
|
for i in range(len(eval_sets)):
|
||||||
|
fit_params.append(
|
||||||
|
{'eval_set': eval_sets[i], 'eval_sample_weight': eval_weights,
|
||||||
|
'init_model': init_models[i]})
|
||||||
|
|
||||||
|
model = FreqaiMultiOutputClassifier(estimator=lgb)
|
||||||
|
thread_training = self.freqai_info.get('multitarget_parallel_training', False)
|
||||||
|
if thread_training:
|
||||||
|
model.n_jobs = y.shape[1]
|
||||||
|
model.fit(X=X, y=y, sample_weight=sample_weight, fit_params=fit_params)
|
||||||
|
|
||||||
|
return model
|
152
freqtrade/freqai/prediction_models/ReinforcementLearner.py
Normal file
152
freqtrade/freqai/prediction_models/ReinforcementLearner.py
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
import torch as th
|
||||||
|
|
||||||
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||||
|
from freqtrade.freqai.RL.Base5ActionRLEnv import Actions, Base5ActionRLEnv, Positions
|
||||||
|
from freqtrade.freqai.RL.BaseReinforcementLearningModel import BaseReinforcementLearningModel
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class ReinforcementLearner(BaseReinforcementLearningModel):
|
||||||
|
"""
|
||||||
|
Reinforcement Learning Model prediction model.
|
||||||
|
|
||||||
|
Users can inherit from this class to make their own RL model with custom
|
||||||
|
environment/training controls. Define the file as follows:
|
||||||
|
|
||||||
|
```
|
||||||
|
from freqtrade.freqai.prediction_models.ReinforcementLearner import ReinforcementLearner
|
||||||
|
|
||||||
|
class MyCoolRLModel(ReinforcementLearner):
|
||||||
|
```
|
||||||
|
|
||||||
|
Save the file to `user_data/freqaimodels`, then run it with:
|
||||||
|
|
||||||
|
freqtrade trade --freqaimodel MyCoolRLModel --config config.json --strategy SomeCoolStrat
|
||||||
|
|
||||||
|
Here the users can override any of the functions
|
||||||
|
available in the `IFreqaiModel` inheritance tree. Most importantly for RL, this
|
||||||
|
is where the user overrides `MyRLEnv` (see below), to define custom
|
||||||
|
`calculate_reward()` function, or to override any other parts of the environment.
|
||||||
|
|
||||||
|
This class also allows users to override any other part of the IFreqaiModel tree.
|
||||||
|
For example, the user can override `def fit()` or `def train()` or `def predict()`
|
||||||
|
to take fine-tuned control over these processes.
|
||||||
|
|
||||||
|
Another common override may be `def data_cleaning_predict()` where the user can
|
||||||
|
take fine-tuned control over the data handling pipeline.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def fit(self, data_dictionary: Dict[str, Any], dk: FreqaiDataKitchen, **kwargs):
|
||||||
|
"""
|
||||||
|
User customizable fit method
|
||||||
|
:param data_dictionary: dict = common data dictionary containing all train/test
|
||||||
|
features/labels/weights.
|
||||||
|
:param dk: FreqaiDatakitchen = data kitchen for current pair.
|
||||||
|
:return:
|
||||||
|
model Any = trained model to be used for inference in dry/live/backtesting
|
||||||
|
"""
|
||||||
|
train_df = data_dictionary["train_features"]
|
||||||
|
total_timesteps = self.freqai_info["rl_config"]["train_cycles"] * len(train_df)
|
||||||
|
|
||||||
|
policy_kwargs = dict(activation_fn=th.nn.ReLU,
|
||||||
|
net_arch=self.net_arch)
|
||||||
|
|
||||||
|
if dk.pair not in self.dd.model_dictionary or not self.continual_learning:
|
||||||
|
model = self.MODELCLASS(self.policy_type, self.train_env, policy_kwargs=policy_kwargs,
|
||||||
|
tensorboard_log=Path(
|
||||||
|
dk.full_path / "tensorboard" / dk.pair.split('/')[0]),
|
||||||
|
**self.freqai_info['model_training_parameters']
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.info('Continual training activated - starting training from previously '
|
||||||
|
'trained agent.')
|
||||||
|
model = self.dd.model_dictionary[dk.pair]
|
||||||
|
model.set_env(self.train_env)
|
||||||
|
|
||||||
|
model.learn(
|
||||||
|
total_timesteps=int(total_timesteps),
|
||||||
|
callback=[self.eval_callback, self.tensorboard_callback]
|
||||||
|
)
|
||||||
|
|
||||||
|
if Path(dk.data_path / "best_model.zip").is_file():
|
||||||
|
logger.info('Callback found a best model.')
|
||||||
|
best_model = self.MODELCLASS.load(dk.data_path / "best_model")
|
||||||
|
return best_model
|
||||||
|
|
||||||
|
logger.info('Couldnt find best model, using final model instead.')
|
||||||
|
|
||||||
|
return model
|
||||||
|
|
||||||
|
class MyRLEnv(Base5ActionRLEnv):
|
||||||
|
"""
|
||||||
|
User can override any function in BaseRLEnv and gym.Env. Here the user
|
||||||
|
sets a custom reward based on profit and trade duration.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def calculate_reward(self, action: int) -> float:
|
||||||
|
"""
|
||||||
|
An example reward function. This is the one function that users will likely
|
||||||
|
wish to inject their own creativity into.
|
||||||
|
:param action: int = The action made by the agent for the current candle.
|
||||||
|
:return:
|
||||||
|
float = the reward to give to the agent for current step (used for optimization
|
||||||
|
of weights in NN)
|
||||||
|
"""
|
||||||
|
# first, penalize if the action is not valid
|
||||||
|
if not self._is_valid(action):
|
||||||
|
self.custom_info["Invalid"] += 1
|
||||||
|
return -2
|
||||||
|
|
||||||
|
pnl = self.get_unrealized_profit()
|
||||||
|
factor = 100.
|
||||||
|
|
||||||
|
# reward agent for entering trades
|
||||||
|
if (action == Actions.Long_enter.value
|
||||||
|
and self._position == Positions.Neutral):
|
||||||
|
self.custom_info[f"{Actions.Long_enter.name}"] += 1
|
||||||
|
return 25
|
||||||
|
if (action == Actions.Short_enter.value
|
||||||
|
and self._position == Positions.Neutral):
|
||||||
|
self.custom_info[f"{Actions.Short_enter.name}"] += 1
|
||||||
|
return 25
|
||||||
|
# discourage agent from not entering trades
|
||||||
|
if action == Actions.Neutral.value and self._position == Positions.Neutral:
|
||||||
|
self.custom_info[f"{Actions.Neutral.name}"] += 1
|
||||||
|
return -1
|
||||||
|
|
||||||
|
max_trade_duration = self.rl_config.get('max_trade_duration_candles', 300)
|
||||||
|
trade_duration = self._current_tick - self._last_trade_tick # type: ignore
|
||||||
|
|
||||||
|
if trade_duration <= max_trade_duration:
|
||||||
|
factor *= 1.5
|
||||||
|
elif trade_duration > max_trade_duration:
|
||||||
|
factor *= 0.5
|
||||||
|
|
||||||
|
# discourage sitting in position
|
||||||
|
if (self._position in (Positions.Short, Positions.Long) and
|
||||||
|
action == Actions.Neutral.value):
|
||||||
|
self.custom_info["Hold"] += 1
|
||||||
|
return -1 * trade_duration / max_trade_duration
|
||||||
|
|
||||||
|
# close long
|
||||||
|
if action == Actions.Long_exit.value and self._position == Positions.Long:
|
||||||
|
if pnl > self.profit_aim * self.rr:
|
||||||
|
factor *= self.rl_config['model_reward_parameters'].get('win_reward_factor', 2)
|
||||||
|
self.custom_info[f"{Actions.Long_exit.name}"] += 1
|
||||||
|
return float(pnl * factor)
|
||||||
|
|
||||||
|
# close short
|
||||||
|
if action == Actions.Short_exit.value and self._position == Positions.Short:
|
||||||
|
if pnl > self.profit_aim * self.rr:
|
||||||
|
factor *= self.rl_config['model_reward_parameters'].get('win_reward_factor', 2)
|
||||||
|
self.custom_info[f"{Actions.Short_exit.name}"] += 1
|
||||||
|
return float(pnl * factor)
|
||||||
|
|
||||||
|
self.custom_info["Unknown"] += 1
|
||||||
|
return 0.
|
@ -0,0 +1,54 @@
|
|||||||
|
import logging
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from pandas import DataFrame
|
||||||
|
from stable_baselines3.common.callbacks import EvalCallback
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv
|
||||||
|
|
||||||
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||||
|
from freqtrade.freqai.prediction_models.ReinforcementLearner import ReinforcementLearner
|
||||||
|
from freqtrade.freqai.RL.BaseReinforcementLearningModel import make_env
|
||||||
|
from freqtrade.freqai.RL.TensorboardCallback import TensorboardCallback
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class ReinforcementLearner_multiproc(ReinforcementLearner):
|
||||||
|
"""
|
||||||
|
Demonstration of how to build vectorized environments
|
||||||
|
"""
|
||||||
|
|
||||||
|
def set_train_and_eval_environments(self, data_dictionary: Dict[str, Any],
|
||||||
|
prices_train: DataFrame, prices_test: DataFrame,
|
||||||
|
dk: FreqaiDataKitchen):
|
||||||
|
"""
|
||||||
|
User can override this if they are using a custom MyRLEnv
|
||||||
|
:param data_dictionary: dict = common data dictionary containing train and test
|
||||||
|
features/labels/weights.
|
||||||
|
:param prices_train/test: DataFrame = dataframe comprised of the prices to be used in
|
||||||
|
the environment during training
|
||||||
|
or testing
|
||||||
|
:param dk: FreqaiDataKitchen = the datakitchen for the current pair
|
||||||
|
"""
|
||||||
|
train_df = data_dictionary["train_features"]
|
||||||
|
test_df = data_dictionary["test_features"]
|
||||||
|
|
||||||
|
env_id = "train_env"
|
||||||
|
self.train_env = SubprocVecEnv([make_env(self.MyRLEnv, env_id, i, 1, train_df, prices_train,
|
||||||
|
self.reward_params, self.CONV_WIDTH, monitor=True,
|
||||||
|
config=self.config) for i
|
||||||
|
in range(self.max_threads)])
|
||||||
|
|
||||||
|
eval_env_id = 'eval_env'
|
||||||
|
self.eval_env = SubprocVecEnv([make_env(self.MyRLEnv, eval_env_id, i, 1,
|
||||||
|
test_df, prices_test,
|
||||||
|
self.reward_params, self.CONV_WIDTH, monitor=True,
|
||||||
|
config=self.config) for i
|
||||||
|
in range(self.max_threads)])
|
||||||
|
self.eval_callback = EvalCallback(self.eval_env, deterministic=True,
|
||||||
|
render=False, eval_freq=len(train_df),
|
||||||
|
best_model_save_path=str(dk.data_path))
|
||||||
|
|
||||||
|
actions = self.train_env.env_method("get_actions")[0]
|
||||||
|
self.tensorboard_callback = TensorboardCallback(verbose=1, actions=actions)
|
@ -14,6 +14,7 @@ from freqtrade.data.history.history_utils import refresh_backtest_ohlcv_data
|
|||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
from freqtrade.exchange import timeframe_to_seconds
|
from freqtrade.exchange import timeframe_to_seconds
|
||||||
from freqtrade.exchange.exchange import market_is_active
|
from freqtrade.exchange.exchange import market_is_active
|
||||||
|
from freqtrade.freqai.data_drawer import FreqaiDataDrawer
|
||||||
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||||
from freqtrade.plugins.pairlist.pairlist_helpers import dynamic_expand_pairlist
|
from freqtrade.plugins.pairlist.pairlist_helpers import dynamic_expand_pairlist
|
||||||
|
|
||||||
@ -218,3 +219,17 @@ def record_params(config: Dict[str, Any], full_path: Path) -> None:
|
|||||||
default=str,
|
default=str,
|
||||||
number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN
|
number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_timerange_backtest_live_models(config: Config) -> str:
|
||||||
|
"""
|
||||||
|
Returns a formated timerange for backtest live/ready models
|
||||||
|
:param config: Configuration dictionary
|
||||||
|
|
||||||
|
:return: a string timerange (format example: '20220801-20220822')
|
||||||
|
"""
|
||||||
|
dk = FreqaiDataKitchen(config)
|
||||||
|
models_path = dk.get_full_models_path(config)
|
||||||
|
dd = FreqaiDataDrawer(models_path, config)
|
||||||
|
timerange = dd.get_timerange_from_live_historic_predictions()
|
||||||
|
return timerange.timerange_str
|
||||||
|
@ -191,10 +191,10 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
# Check whether markets have to be reloaded and reload them when it's needed
|
# Check whether markets have to be reloaded and reload them when it's needed
|
||||||
self.exchange.reload_markets()
|
self.exchange.reload_markets()
|
||||||
|
|
||||||
self.update_closed_trades_without_assigned_fees()
|
self.update_trades_without_assigned_fees()
|
||||||
|
|
||||||
# Query trades from persistence layer
|
# Query trades from persistence layer
|
||||||
trades = Trade.get_open_trades()
|
trades: List[Trade] = Trade.get_open_trades()
|
||||||
|
|
||||||
self.active_pair_whitelist = self._refresh_active_whitelist(trades)
|
self.active_pair_whitelist = self._refresh_active_whitelist(trades)
|
||||||
|
|
||||||
@ -354,7 +354,7 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
if self.trading_mode == TradingMode.FUTURES:
|
if self.trading_mode == TradingMode.FUTURES:
|
||||||
self._schedule.run_pending()
|
self._schedule.run_pending()
|
||||||
|
|
||||||
def update_closed_trades_without_assigned_fees(self):
|
def update_trades_without_assigned_fees(self) -> None:
|
||||||
"""
|
"""
|
||||||
Update closed trades without close fees assigned.
|
Update closed trades without close fees assigned.
|
||||||
Only acts when Orders are in the database, otherwise the last order-id is unknown.
|
Only acts when Orders are in the database, otherwise the last order-id is unknown.
|
||||||
@ -379,8 +379,9 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
stoploss_order=order.ft_order_side == 'stoploss',
|
stoploss_order=order.ft_order_side == 'stoploss',
|
||||||
send_msg=False)
|
send_msg=False)
|
||||||
|
|
||||||
trades: List[Trade] = Trade.get_open_trades_without_assigned_fees()
|
trades = Trade.get_open_trades_without_assigned_fees()
|
||||||
for trade in trades:
|
for trade in trades:
|
||||||
|
with self._exit_lock:
|
||||||
if trade.is_open and not trade.fee_updated(trade.entry_side):
|
if trade.is_open and not trade.fee_updated(trade.entry_side):
|
||||||
order = trade.select_order(trade.entry_side, False)
|
order = trade.select_order(trade.entry_side, False)
|
||||||
open_order = trade.select_order(trade.entry_side, True)
|
open_order = trade.select_order(trade.entry_side, True)
|
||||||
@ -826,6 +827,8 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
co = self.exchange.cancel_stoploss_order_with_result(
|
co = self.exchange.cancel_stoploss_order_with_result(
|
||||||
trade.stoploss_order_id, trade.pair, trade.amount)
|
trade.stoploss_order_id, trade.pair, trade.amount)
|
||||||
trade.update_order(co)
|
trade.update_order(co)
|
||||||
|
# Reset stoploss order id.
|
||||||
|
trade.stoploss_order_id = None
|
||||||
except InvalidOrderException:
|
except InvalidOrderException:
|
||||||
logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id}")
|
logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id}")
|
||||||
return trade
|
return trade
|
||||||
@ -982,7 +985,7 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
# SELL / exit positions / close trades logic and methods
|
# SELL / exit positions / close trades logic and methods
|
||||||
#
|
#
|
||||||
|
|
||||||
def exit_positions(self, trades: List[Any]) -> int:
|
def exit_positions(self, trades: List[Trade]) -> int:
|
||||||
"""
|
"""
|
||||||
Tries to execute exit orders for open trades (positions)
|
Tries to execute exit orders for open trades (positions)
|
||||||
"""
|
"""
|
||||||
@ -1010,7 +1013,7 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
|
|
||||||
def handle_trade(self, trade: Trade) -> bool:
|
def handle_trade(self, trade: Trade) -> bool:
|
||||||
"""
|
"""
|
||||||
Sells/exits_short the current pair if the threshold is reached and updates the trade record.
|
Exits the current pair if the threshold is reached and updates the trade record.
|
||||||
:return: True if trade has been sold/exited_short, False otherwise
|
:return: True if trade has been sold/exited_short, False otherwise
|
||||||
"""
|
"""
|
||||||
if not trade.is_open:
|
if not trade.is_open:
|
||||||
@ -1133,10 +1136,8 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
trade.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value
|
trade.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value
|
||||||
self.update_trade_state(trade, trade.stoploss_order_id, stoploss_order,
|
self.update_trade_state(trade, trade.stoploss_order_id, stoploss_order,
|
||||||
stoploss_order=True)
|
stoploss_order=True)
|
||||||
# Lock pair for one candle to prevent immediate rebuys
|
|
||||||
self.strategy.lock_pair(trade.pair, datetime.now(timezone.utc),
|
|
||||||
reason='Auto lock')
|
|
||||||
self._notify_exit(trade, "stoploss", True)
|
self._notify_exit(trade, "stoploss", True)
|
||||||
|
self.handle_protections(trade.pair, trade.trade_direction)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if trade.open_order_id or not trade.is_open:
|
if trade.open_order_id or not trade.is_open:
|
||||||
@ -1150,7 +1151,7 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
stoploss = (
|
stoploss = (
|
||||||
self.edge.stoploss(pair=trade.pair)
|
self.edge.stoploss(pair=trade.pair)
|
||||||
if self.edge else
|
if self.edge else
|
||||||
self.strategy.stoploss / trade.leverage
|
trade.stop_loss_pct / trade.leverage
|
||||||
)
|
)
|
||||||
if trade.is_short:
|
if trade.is_short:
|
||||||
stop_price = trade.open_rate * (1 - stoploss)
|
stop_price = trade.open_rate * (1 - stoploss)
|
||||||
@ -1169,7 +1170,6 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
if self.create_stoploss_order(trade=trade, stop_price=trade.stoploss_or_liquidation):
|
if self.create_stoploss_order(trade=trade, stop_price=trade.stoploss_or_liquidation):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
trade.stoploss_order_id = None
|
|
||||||
logger.warning('Stoploss order was cancelled, but unable to recreate one.')
|
logger.warning('Stoploss order was cancelled, but unable to recreate one.')
|
||||||
|
|
||||||
# Finally we check if stoploss on exchange should be moved up because of trailing.
|
# Finally we check if stoploss on exchange should be moved up because of trailing.
|
||||||
@ -1595,11 +1595,6 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
trade.close_rate_requested = limit
|
trade.close_rate_requested = limit
|
||||||
trade.exit_reason = exit_reason
|
trade.exit_reason = exit_reason
|
||||||
|
|
||||||
if not sub_trade_amt:
|
|
||||||
# Lock pair for one candle to prevent immediate re-trading
|
|
||||||
self.strategy.lock_pair(trade.pair, datetime.now(timezone.utc),
|
|
||||||
reason='Auto lock')
|
|
||||||
|
|
||||||
self._notify_exit(trade, order_type, sub_trade=bool(sub_trade_amt), order=order_obj)
|
self._notify_exit(trade, order_type, sub_trade=bool(sub_trade_amt), order=order_obj)
|
||||||
# In case of market sell orders the order can be closed immediately
|
# In case of market sell orders the order can be closed immediately
|
||||||
if order.get('status', 'unknown') in ('closed', 'expired'):
|
if order.get('status', 'unknown') in ('closed', 'expired'):
|
||||||
@ -1809,6 +1804,8 @@ class FreqtradeBot(LoggingMixin):
|
|||||||
self._notify_enter(trade, order, fill=True, sub_trade=sub_trade)
|
self._notify_enter(trade, order, fill=True, sub_trade=sub_trade)
|
||||||
|
|
||||||
def handle_protections(self, pair: str, side: LongShort) -> None:
|
def handle_protections(self, pair: str, side: LongShort) -> None:
|
||||||
|
# Lock pair for one candle to prevent immediate rebuys
|
||||||
|
self.strategy.lock_pair(pair, datetime.now(timezone.utc), reason='Auto lock')
|
||||||
prot_trig = self.protections.stop_per_pair(pair, side=side)
|
prot_trig = self.protections.stop_per_pair(pair, side=side)
|
||||||
if prot_trig:
|
if prot_trig:
|
||||||
msg = {'type': RPCMessageType.PROTECTION_TRIGGER, }
|
msg = {'type': RPCMessageType.PROTECTION_TRIGGER, }
|
||||||
|
@ -35,9 +35,5 @@ def interest(
|
|||||||
elif exchange_name == "kraken":
|
elif exchange_name == "kraken":
|
||||||
# Rounded based on https://kraken-fees-calculator.github.io/
|
# Rounded based on https://kraken-fees-calculator.github.io/
|
||||||
return borrowed * rate * (one + FtPrecise(ceil(hours / four)))
|
return borrowed * rate * (one + FtPrecise(ceil(hours / four)))
|
||||||
elif exchange_name == "ftx":
|
|
||||||
# As Explained under #Interest rates section in
|
|
||||||
# https://help.ftx.com/hc/en-us/articles/360053007671-Spot-Margin-Trading-Explainer
|
|
||||||
return borrowed * rate * FtPrecise(ceil(hours)) / twenty_four
|
|
||||||
else:
|
else:
|
||||||
raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade")
|
raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade")
|
||||||
|
@ -7,6 +7,8 @@ import logging
|
|||||||
import sys
|
import sys
|
||||||
from typing import Any, List
|
from typing import Any, List
|
||||||
|
|
||||||
|
from freqtrade.util.gc_setup import gc_set_threshold
|
||||||
|
|
||||||
|
|
||||||
# check min. python version
|
# check min. python version
|
||||||
if sys.version_info < (3, 8): # pragma: no cover
|
if sys.version_info < (3, 8): # pragma: no cover
|
||||||
@ -36,6 +38,7 @@ def main(sysargv: List[str] = None) -> None:
|
|||||||
# Call subcommand.
|
# Call subcommand.
|
||||||
if 'func' in args:
|
if 'func' in args:
|
||||||
logger.info(f'freqtrade {__version__}')
|
logger.info(f'freqtrade {__version__}')
|
||||||
|
gc_set_threshold()
|
||||||
return_code = args['func'](args)
|
return_code = args['func'](args)
|
||||||
else:
|
else:
|
||||||
# No subcommand was issued.
|
# No subcommand was issued.
|
||||||
|
@ -10,7 +10,8 @@ from typing import Any, Dict, Iterator, List, Mapping, Union
|
|||||||
from typing.io import IO
|
from typing.io import IO
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import pandas
|
import orjson
|
||||||
|
import pandas as pd
|
||||||
import rapidjson
|
import rapidjson
|
||||||
|
|
||||||
from freqtrade.constants import DECIMAL_PER_COIN_FALLBACK, DECIMALS_PER_COIN
|
from freqtrade.constants import DECIMAL_PER_COIN_FALLBACK, DECIMALS_PER_COIN
|
||||||
@ -256,29 +257,37 @@ def parse_db_uri_for_logging(uri: str):
|
|||||||
return parsed_db_uri.geturl().replace(f':{pwd}@', ':*****@')
|
return parsed_db_uri.geturl().replace(f':{pwd}@', ':*****@')
|
||||||
|
|
||||||
|
|
||||||
def dataframe_to_json(dataframe: pandas.DataFrame) -> str:
|
def dataframe_to_json(dataframe: pd.DataFrame) -> str:
|
||||||
"""
|
"""
|
||||||
Serialize a DataFrame for transmission over the wire using JSON
|
Serialize a DataFrame for transmission over the wire using JSON
|
||||||
:param dataframe: A pandas DataFrame
|
:param dataframe: A pandas DataFrame
|
||||||
:returns: A JSON string of the pandas DataFrame
|
:returns: A JSON string of the pandas DataFrame
|
||||||
"""
|
"""
|
||||||
return dataframe.to_json(orient='split')
|
# https://github.com/pandas-dev/pandas/issues/24889
|
||||||
|
# https://github.com/pandas-dev/pandas/issues/40443
|
||||||
|
# We need to convert to a dict to avoid mem leak
|
||||||
|
def default(z):
|
||||||
|
if isinstance(z, pd.Timestamp):
|
||||||
|
return z.timestamp() * 1e3
|
||||||
|
raise TypeError
|
||||||
|
|
||||||
|
return str(orjson.dumps(dataframe.to_dict(orient='split'), default=default), 'utf-8')
|
||||||
|
|
||||||
|
|
||||||
def json_to_dataframe(data: str) -> pandas.DataFrame:
|
def json_to_dataframe(data: str) -> pd.DataFrame:
|
||||||
"""
|
"""
|
||||||
Deserialize JSON into a DataFrame
|
Deserialize JSON into a DataFrame
|
||||||
:param data: A JSON string
|
:param data: A JSON string
|
||||||
:returns: A pandas DataFrame from the JSON string
|
:returns: A pandas DataFrame from the JSON string
|
||||||
"""
|
"""
|
||||||
dataframe = pandas.read_json(data, orient='split')
|
dataframe = pd.read_json(data, orient='split')
|
||||||
if 'date' in dataframe.columns:
|
if 'date' in dataframe.columns:
|
||||||
dataframe['date'] = pandas.to_datetime(dataframe['date'], unit='ms', utc=True)
|
dataframe['date'] = pd.to_datetime(dataframe['date'], unit='ms', utc=True)
|
||||||
|
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
|
|
||||||
def remove_entry_exit_signals(dataframe: pandas.DataFrame):
|
def remove_entry_exit_signals(dataframe: pd.DataFrame):
|
||||||
"""
|
"""
|
||||||
Remove Entry and Exit signals from a DataFrame
|
Remove Entry and Exit signals from a DataFrame
|
||||||
|
|
||||||
|
@ -134,6 +134,10 @@ class Backtesting:
|
|||||||
self.fee = self.exchange.get_fee(symbol=self.pairlists.whitelist[0])
|
self.fee = self.exchange.get_fee(symbol=self.pairlists.whitelist[0])
|
||||||
self.precision_mode = self.exchange.precisionMode
|
self.precision_mode = self.exchange.precisionMode
|
||||||
|
|
||||||
|
if self.config.get('freqai_backtest_live_models', False):
|
||||||
|
from freqtrade.freqai.utils import get_timerange_backtest_live_models
|
||||||
|
self.config['timerange'] = get_timerange_backtest_live_models(self.config)
|
||||||
|
|
||||||
self.timerange = TimeRange.parse_timerange(
|
self.timerange = TimeRange.parse_timerange(
|
||||||
None if self.config.get('timerange') is None else str(self.config.get('timerange')))
|
None if self.config.get('timerange') is None else str(self.config.get('timerange')))
|
||||||
|
|
||||||
@ -162,7 +166,7 @@ class Backtesting:
|
|||||||
PairLocks.use_db = True
|
PairLocks.use_db = True
|
||||||
Trade.use_db = True
|
Trade.use_db = True
|
||||||
|
|
||||||
def init_backtest_detail(self):
|
def init_backtest_detail(self) -> None:
|
||||||
# Load detail timeframe if specified
|
# Load detail timeframe if specified
|
||||||
self.timeframe_detail = str(self.config.get('timeframe_detail', ''))
|
self.timeframe_detail = str(self.config.get('timeframe_detail', ''))
|
||||||
if self.timeframe_detail:
|
if self.timeframe_detail:
|
||||||
@ -688,10 +692,11 @@ class Backtesting:
|
|||||||
trade.orders.append(order)
|
trade.orders.append(order)
|
||||||
return trade
|
return trade
|
||||||
|
|
||||||
def _get_exit_trade_entry(self, trade: LocalTrade, row: Tuple) -> Optional[LocalTrade]:
|
def _get_exit_trade_entry(
|
||||||
|
self, trade: LocalTrade, row: Tuple, is_first: bool) -> Optional[LocalTrade]:
|
||||||
exit_candle_time: datetime = row[DATE_IDX].to_pydatetime()
|
exit_candle_time: datetime = row[DATE_IDX].to_pydatetime()
|
||||||
|
|
||||||
if self.trading_mode == TradingMode.FUTURES:
|
if is_first and self.trading_mode == TradingMode.FUTURES:
|
||||||
trade.funding_fees = self.exchange.calculate_funding_fees(
|
trade.funding_fees = self.exchange.calculate_funding_fees(
|
||||||
self.futures_data[trade.pair],
|
self.futures_data[trade.pair],
|
||||||
amount=trade.amount,
|
amount=trade.amount,
|
||||||
@ -700,31 +705,6 @@ class Backtesting:
|
|||||||
close_date=exit_candle_time,
|
close_date=exit_candle_time,
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.timeframe_detail and trade.pair in self.detail_data:
|
|
||||||
exit_candle_end = exit_candle_time + timedelta(minutes=self.timeframe_min)
|
|
||||||
|
|
||||||
detail_data = self.detail_data[trade.pair]
|
|
||||||
detail_data = detail_data.loc[
|
|
||||||
(detail_data['date'] >= exit_candle_time) &
|
|
||||||
(detail_data['date'] < exit_candle_end)
|
|
||||||
].copy()
|
|
||||||
if len(detail_data) == 0:
|
|
||||||
# Fall back to "regular" data if no detail data was found for this candle
|
|
||||||
return self._get_exit_trade_entry_for_candle(trade, row)
|
|
||||||
detail_data.loc[:, 'enter_long'] = row[LONG_IDX]
|
|
||||||
detail_data.loc[:, 'exit_long'] = row[ELONG_IDX]
|
|
||||||
detail_data.loc[:, 'enter_short'] = row[SHORT_IDX]
|
|
||||||
detail_data.loc[:, 'exit_short'] = row[ESHORT_IDX]
|
|
||||||
detail_data.loc[:, 'enter_tag'] = row[ENTER_TAG_IDX]
|
|
||||||
detail_data.loc[:, 'exit_tag'] = row[EXIT_TAG_IDX]
|
|
||||||
for det_row in detail_data[HEADERS].values.tolist():
|
|
||||||
res = self._get_exit_trade_entry_for_candle(trade, det_row)
|
|
||||||
if res:
|
|
||||||
return res
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
else:
|
|
||||||
return self._get_exit_trade_entry_for_candle(trade, row)
|
return self._get_exit_trade_entry_for_candle(trade, row)
|
||||||
|
|
||||||
def get_valid_price_and_stake(
|
def get_valid_price_and_stake(
|
||||||
@ -1070,7 +1050,7 @@ class Backtesting:
|
|||||||
|
|
||||||
def backtest_loop(
|
def backtest_loop(
|
||||||
self, row: Tuple, pair: str, current_time: datetime, end_date: datetime,
|
self, row: Tuple, pair: str, current_time: datetime, end_date: datetime,
|
||||||
max_open_trades: int, open_trade_count_start: int) -> int:
|
max_open_trades: int, open_trade_count_start: int, is_first: bool = True) -> int:
|
||||||
"""
|
"""
|
||||||
NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized.
|
NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized.
|
||||||
|
|
||||||
@ -1088,9 +1068,11 @@ class Backtesting:
|
|||||||
# without positionstacking, we can only have one open trade per pair.
|
# without positionstacking, we can only have one open trade per pair.
|
||||||
# max_open_trades must be respected
|
# max_open_trades must be respected
|
||||||
# don't open on the last row
|
# don't open on the last row
|
||||||
|
# We only open trades on the main candle, not on detail candles
|
||||||
trade_dir = self.check_for_trade_entry(row)
|
trade_dir = self.check_for_trade_entry(row)
|
||||||
if (
|
if (
|
||||||
(self._position_stacking or len(LocalTrade.bt_trades_open_pp[pair]) == 0)
|
(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(max_open_trades, open_trade_count_start)
|
||||||
and current_time != end_date
|
and current_time != end_date
|
||||||
and trade_dir is not None
|
and trade_dir is not None
|
||||||
@ -1116,7 +1098,7 @@ class Backtesting:
|
|||||||
|
|
||||||
# 4. Create exit orders (if any)
|
# 4. Create exit orders (if any)
|
||||||
if not trade.open_order_id:
|
if not trade.open_order_id:
|
||||||
self._get_exit_trade_entry(trade, row) # Place exit order if necessary
|
self._get_exit_trade_entry(trade, row, is_first) # Place exit order if necessary
|
||||||
|
|
||||||
# 5. Process exit orders.
|
# 5. Process exit orders.
|
||||||
order = trade.select_order(trade.exit_side, is_open=True)
|
order = trade.select_order(trade.exit_side, is_open=True)
|
||||||
@ -1167,7 +1149,6 @@ class Backtesting:
|
|||||||
|
|
||||||
self.progress.init_step(BacktestState.BACKTEST, int(
|
self.progress.init_step(BacktestState.BACKTEST, int(
|
||||||
(end_date - start_date) / timedelta(minutes=self.timeframe_min)))
|
(end_date - start_date) / timedelta(minutes=self.timeframe_min)))
|
||||||
|
|
||||||
# Loop timerange and get candle for each pair at that point in time
|
# Loop timerange and get candle for each pair at that point in time
|
||||||
while current_time <= end_date:
|
while current_time <= end_date:
|
||||||
open_trade_count_start = LocalTrade.bt_open_open_trade_count
|
open_trade_count_start = LocalTrade.bt_open_open_trade_count
|
||||||
@ -1181,7 +1162,35 @@ class Backtesting:
|
|||||||
row_index += 1
|
row_index += 1
|
||||||
indexes[pair] = row_index
|
indexes[pair] = row_index
|
||||||
self.dataprovider._set_dataframe_max_index(row_index)
|
self.dataprovider._set_dataframe_max_index(row_index)
|
||||||
|
current_detail_time: datetime = row[DATE_IDX].to_pydatetime()
|
||||||
|
if self.timeframe_detail and pair in self.detail_data:
|
||||||
|
exit_candle_end = current_detail_time + timedelta(minutes=self.timeframe_min)
|
||||||
|
|
||||||
|
detail_data = self.detail_data[pair]
|
||||||
|
detail_data = detail_data.loc[
|
||||||
|
(detail_data['date'] >= current_detail_time) &
|
||||||
|
(detail_data['date'] < exit_candle_end)
|
||||||
|
].copy()
|
||||||
|
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,
|
||||||
|
open_trade_count_start)
|
||||||
|
detail_data.loc[:, 'enter_long'] = row[LONG_IDX]
|
||||||
|
detail_data.loc[:, 'exit_long'] = row[ELONG_IDX]
|
||||||
|
detail_data.loc[:, 'enter_short'] = row[SHORT_IDX]
|
||||||
|
detail_data.loc[:, 'exit_short'] = row[ESHORT_IDX]
|
||||||
|
detail_data.loc[:, 'enter_tag'] = row[ENTER_TAG_IDX]
|
||||||
|
detail_data.loc[:, 'exit_tag'] = row[EXIT_TAG_IDX]
|
||||||
|
is_first = True
|
||||||
|
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,
|
||||||
|
open_trade_count_start, is_first)
|
||||||
|
current_time_det += timedelta(minutes=self.timeframe_detail_min)
|
||||||
|
is_first = False
|
||||||
|
else:
|
||||||
open_trade_count_start = self.backtest_loop(
|
open_trade_count_start = self.backtest_loop(
|
||||||
row, pair, current_time, end_date, max_open_trades, open_trade_count_start)
|
row, pair, current_time, end_date, max_open_trades, open_trade_count_start)
|
||||||
|
|
||||||
@ -1282,8 +1291,7 @@ class Backtesting:
|
|||||||
def _get_min_cached_backtest_date(self):
|
def _get_min_cached_backtest_date(self):
|
||||||
min_backtest_date = None
|
min_backtest_date = None
|
||||||
backtest_cache_age = self.config.get('backtest_cache', constants.BACKTEST_CACHE_DEFAULT)
|
backtest_cache_age = self.config.get('backtest_cache', constants.BACKTEST_CACHE_DEFAULT)
|
||||||
if self.timerange.stopts == 0 or datetime.fromtimestamp(
|
if self.timerange.stopts == 0 or self.timerange.stopdt > datetime.now(tz=timezone.utc):
|
||||||
self.timerange.stopts, tz=timezone.utc) > datetime.now(tz=timezone.utc):
|
|
||||||
logger.warning('Backtest result caching disabled due to use of open-ended timerange.')
|
logger.warning('Backtest result caching disabled due to use of open-ended timerange.')
|
||||||
elif backtest_cache_age == 'day':
|
elif backtest_cache_age == 'day':
|
||||||
min_backtest_date = datetime.now(tz=timezone.utc) - timedelta(days=1)
|
min_backtest_date = datetime.now(tz=timezone.utc) - timedelta(days=1)
|
||||||
|
@ -17,6 +17,7 @@ from freqtrade.enums import HyperoptState
|
|||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
from freqtrade.misc import deep_merge_dicts, round_coin_value, round_dict, safe_value_fallback2
|
from freqtrade.misc import deep_merge_dicts, round_coin_value, round_dict, safe_value_fallback2
|
||||||
from freqtrade.optimize.hyperopt_epoch_filters import hyperopt_filter_epochs
|
from freqtrade.optimize.hyperopt_epoch_filters import hyperopt_filter_epochs
|
||||||
|
from freqtrade.optimize.optimize_reports import generate_wins_draws_losses
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -325,8 +326,10 @@ class HyperoptTools():
|
|||||||
|
|
||||||
# New mode, using backtest result for metrics
|
# New mode, using backtest result for metrics
|
||||||
trials['results_metrics.winsdrawslosses'] = trials.apply(
|
trials['results_metrics.winsdrawslosses'] = trials.apply(
|
||||||
lambda x: f"{x['results_metrics.wins']} {x['results_metrics.draws']:>4} "
|
lambda x: generate_wins_draws_losses(
|
||||||
f"{x['results_metrics.losses']:>4}", axis=1)
|
x['results_metrics.wins'], x['results_metrics.draws'],
|
||||||
|
x['results_metrics.losses']
|
||||||
|
), axis=1)
|
||||||
|
|
||||||
trials = trials[['Best', 'current_epoch', 'results_metrics.total_trades',
|
trials = trials[['Best', 'current_epoch', 'results_metrics.total_trades',
|
||||||
'results_metrics.winsdrawslosses',
|
'results_metrics.winsdrawslosses',
|
||||||
@ -337,7 +340,7 @@ class HyperoptTools():
|
|||||||
'loss', 'is_initial_point', 'is_random', 'is_best']]
|
'loss', 'is_initial_point', 'is_random', 'is_best']]
|
||||||
|
|
||||||
trials.columns = [
|
trials.columns = [
|
||||||
'Best', 'Epoch', 'Trades', ' Win Draw Loss', 'Avg profit',
|
'Best', 'Epoch', 'Trades', ' Win Draw Loss Win%', 'Avg profit',
|
||||||
'Total profit', 'Profit', 'Avg duration', 'max_drawdown', 'max_drawdown_account',
|
'Total profit', 'Profit', 'Avg duration', 'max_drawdown', 'max_drawdown_account',
|
||||||
'max_drawdown_abs', 'Objective', 'is_initial_point', 'is_random', 'is_best'
|
'max_drawdown_abs', 'Objective', 'is_initial_point', 'is_random', 'is_best'
|
||||||
]
|
]
|
||||||
@ -467,9 +470,9 @@ class HyperoptTools():
|
|||||||
|
|
||||||
base_metrics = ['Best', 'current_epoch', 'results_metrics.total_trades',
|
base_metrics = ['Best', 'current_epoch', 'results_metrics.total_trades',
|
||||||
'results_metrics.profit_mean', 'results_metrics.profit_median',
|
'results_metrics.profit_mean', 'results_metrics.profit_median',
|
||||||
'results_metrics.profit_total',
|
'results_metrics.profit_total', 'Stake currency',
|
||||||
'Stake currency',
|
|
||||||
'results_metrics.profit_total_abs', 'results_metrics.holding_avg',
|
'results_metrics.profit_total_abs', 'results_metrics.holding_avg',
|
||||||
|
'results_metrics.trade_count_long', 'results_metrics.trade_count_short',
|
||||||
'loss', 'is_initial_point', 'is_best']
|
'loss', 'is_initial_point', 'is_best']
|
||||||
perc_multi = 100
|
perc_multi = 100
|
||||||
|
|
||||||
@ -477,7 +480,9 @@ class HyperoptTools():
|
|||||||
trials = trials[base_metrics + param_metrics]
|
trials = trials[base_metrics + param_metrics]
|
||||||
|
|
||||||
base_columns = ['Best', 'Epoch', 'Trades', 'Avg profit', 'Median profit', 'Total profit',
|
base_columns = ['Best', 'Epoch', 'Trades', 'Avg profit', 'Median profit', 'Total profit',
|
||||||
'Stake currency', 'Profit', 'Avg duration', 'Objective',
|
'Stake currency', 'Profit', 'Avg duration',
|
||||||
|
'Trade count long', 'Trade count short',
|
||||||
|
'Objective',
|
||||||
'is_initial_point', 'is_best']
|
'is_initial_point', 'is_best']
|
||||||
param_columns = list(results[0]['params_dict'].keys())
|
param_columns = list(results[0]['params_dict'].keys())
|
||||||
trials.columns = base_columns + param_columns
|
trials.columns = base_columns + param_columns
|
||||||
|
@ -86,7 +86,7 @@ def _get_line_header(first_column: str, stake_currency: str,
|
|||||||
'Win Draw Loss Win%']
|
'Win Draw Loss Win%']
|
||||||
|
|
||||||
|
|
||||||
def _generate_wins_draws_losses(wins, draws, losses):
|
def generate_wins_draws_losses(wins, draws, losses):
|
||||||
if wins > 0 and losses == 0:
|
if wins > 0 and losses == 0:
|
||||||
wl_ratio = '100'
|
wl_ratio = '100'
|
||||||
elif wins == 0:
|
elif wins == 0:
|
||||||
@ -600,7 +600,7 @@ def text_table_bt_results(pair_results: List[Dict[str, Any]], stake_currency: st
|
|||||||
output = [[
|
output = [[
|
||||||
t['key'], t['trades'], t['profit_mean_pct'], t['profit_sum_pct'], t['profit_total_abs'],
|
t['key'], t['trades'], t['profit_mean_pct'], t['profit_sum_pct'], t['profit_total_abs'],
|
||||||
t['profit_total_pct'], t['duration_avg'],
|
t['profit_total_pct'], t['duration_avg'],
|
||||||
_generate_wins_draws_losses(t['wins'], t['draws'], t['losses'])
|
generate_wins_draws_losses(t['wins'], t['draws'], t['losses'])
|
||||||
] for t in pair_results]
|
] for t in pair_results]
|
||||||
# Ignore type as floatfmt does allow tuples but mypy does not know that
|
# Ignore type as floatfmt does allow tuples but mypy does not know that
|
||||||
return tabulate(output, headers=headers,
|
return tabulate(output, headers=headers,
|
||||||
@ -626,7 +626,7 @@ def text_table_exit_reason(exit_reason_stats: List[Dict[str, Any]], stake_curren
|
|||||||
|
|
||||||
output = [[
|
output = [[
|
||||||
t.get('exit_reason', t.get('sell_reason')), t['trades'],
|
t.get('exit_reason', t.get('sell_reason')), t['trades'],
|
||||||
_generate_wins_draws_losses(t['wins'], t['draws'], t['losses']),
|
generate_wins_draws_losses(t['wins'], t['draws'], t['losses']),
|
||||||
t['profit_mean_pct'], t['profit_sum_pct'],
|
t['profit_mean_pct'], t['profit_sum_pct'],
|
||||||
round_coin_value(t['profit_total_abs'], stake_currency, False),
|
round_coin_value(t['profit_total_abs'], stake_currency, False),
|
||||||
t['profit_total_pct'],
|
t['profit_total_pct'],
|
||||||
@ -656,7 +656,7 @@ def text_table_tags(tag_type: str, tag_results: List[Dict[str, Any]], stake_curr
|
|||||||
t['profit_total_abs'],
|
t['profit_total_abs'],
|
||||||
t['profit_total_pct'],
|
t['profit_total_pct'],
|
||||||
t['duration_avg'],
|
t['duration_avg'],
|
||||||
_generate_wins_draws_losses(
|
generate_wins_draws_losses(
|
||||||
t['wins'],
|
t['wins'],
|
||||||
t['draws'],
|
t['draws'],
|
||||||
t['losses'])] for t in tag_results]
|
t['losses'])] for t in tag_results]
|
||||||
@ -715,7 +715,7 @@ def text_table_strategy(strategy_results, stake_currency: str) -> str:
|
|||||||
output = [[
|
output = [[
|
||||||
t['key'], t['trades'], t['profit_mean_pct'], t['profit_sum_pct'], t['profit_total_abs'],
|
t['key'], t['trades'], t['profit_mean_pct'], t['profit_sum_pct'], t['profit_total_abs'],
|
||||||
t['profit_total_pct'], t['duration_avg'],
|
t['profit_total_pct'], t['duration_avg'],
|
||||||
_generate_wins_draws_losses(t['wins'], t['draws'], t['losses']), drawdown]
|
generate_wins_draws_losses(t['wins'], t['draws'], t['losses']), drawdown]
|
||||||
for t, drawdown in zip(strategy_results, drawdown)]
|
for t, drawdown in zip(strategy_results, drawdown)]
|
||||||
# Ignore type as floatfmt does allow tuples but mypy does not know that
|
# Ignore type as floatfmt does allow tuples but mypy does not know that
|
||||||
return tabulate(output, headers=headers,
|
return tabulate(output, headers=headers,
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from sqlalchemy import inspect, select, text, tuple_, update
|
from sqlalchemy import inspect, select, text, tuple_, update
|
||||||
|
|
||||||
@ -31,9 +31,9 @@ def get_backup_name(tabs: List[str], backup_prefix: str):
|
|||||||
return table_back_name
|
return table_back_name
|
||||||
|
|
||||||
|
|
||||||
def get_last_sequence_ids(engine, trade_back_name, order_back_name):
|
def get_last_sequence_ids(engine, trade_back_name: str, order_back_name: str):
|
||||||
order_id: int = None
|
order_id: Optional[int] = None
|
||||||
trade_id: int = None
|
trade_id: Optional[int] = None
|
||||||
|
|
||||||
if engine.name == 'postgresql':
|
if engine.name == 'postgresql':
|
||||||
with engine.begin() as connection:
|
with engine.begin() as connection:
|
||||||
|
@ -87,7 +87,7 @@ class PairLocks():
|
|||||||
Get the lock that expires the latest for the pair given.
|
Get the lock that expires the latest for the pair given.
|
||||||
"""
|
"""
|
||||||
locks = PairLocks.get_pair_locks(pair, now, side=side)
|
locks = PairLocks.get_pair_locks(pair, now, side=side)
|
||||||
locks = sorted(locks, key=lambda l: l.lock_end_time, reverse=True)
|
locks = sorted(locks, key=lambda lock: lock.lock_end_time, reverse=True)
|
||||||
return locks[0] if locks else None
|
return locks[0] if locks else None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -90,6 +90,13 @@ class Order(_DECL_BASE):
|
|||||||
def safe_filled(self) -> float:
|
def safe_filled(self) -> float:
|
||||||
return self.filled if self.filled is not None else self.amount or 0.0
|
return self.filled if self.filled is not None else self.amount or 0.0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def safe_remaining(self) -> float:
|
||||||
|
return (
|
||||||
|
self.remaining if self.remaining is not None else
|
||||||
|
self.amount - (self.filled or 0.0)
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def safe_fee_base(self) -> float:
|
def safe_fee_base(self) -> float:
|
||||||
return self.ft_fee_base or 0.0
|
return self.ft_fee_base or 0.0
|
||||||
@ -667,7 +674,7 @@ class LocalTrade():
|
|||||||
self.close(order.safe_price)
|
self.close(order.safe_price)
|
||||||
else:
|
else:
|
||||||
self.recalc_trade_from_orders()
|
self.recalc_trade_from_orders()
|
||||||
elif order.ft_order_side == 'stoploss':
|
elif order.ft_order_side == 'stoploss' and order.status not in ('canceled', 'open'):
|
||||||
self.stoploss_order_id = None
|
self.stoploss_order_id = None
|
||||||
self.close_rate_requested = self.stop_loss
|
self.close_rate_requested = self.stop_loss
|
||||||
self.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value
|
self.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value
|
||||||
@ -1144,7 +1151,8 @@ class Trade(_DECL_BASE, LocalTrade):
|
|||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
|
||||||
orders = relationship("Order", order_by="Order.id", cascade="all, delete-orphan", lazy="joined")
|
orders = relationship("Order", order_by="Order.id", cascade="all, delete-orphan",
|
||||||
|
lazy="selectin", innerjoin=True)
|
||||||
|
|
||||||
exchange = Column(String(25), nullable=False)
|
exchange = Column(String(25), nullable=False)
|
||||||
pair = Column(String(25), nullable=False, index=True)
|
pair = Column(String(25), nullable=False, index=True)
|
||||||
|
@ -81,8 +81,6 @@ async def validate_ws_token(
|
|||||||
except HTTPException:
|
except HTTPException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# No checks passed, deny the connection
|
|
||||||
logger.debug("Denying websocket request.")
|
|
||||||
# If it doesn't match, close the websocket connection
|
# If it doesn't match, close the websocket connection
|
||||||
await ws.close(code=status.WS_1008_POLICY_VIOLATION)
|
await ws.close(code=status.WS_1008_POLICY_VIOLATION)
|
||||||
|
|
||||||
|
@ -37,7 +37,8 @@ logger = logging.getLogger(__name__)
|
|||||||
# 2.16: Additional daily metrics
|
# 2.16: Additional daily metrics
|
||||||
# 2.17: Forceentry - leverage, partial force_exit
|
# 2.17: Forceentry - leverage, partial force_exit
|
||||||
# 2.20: Add websocket endpoints
|
# 2.20: Add websocket endpoints
|
||||||
API_VERSION = 2.20
|
# 2.21: Add new_candle messagetype
|
||||||
|
API_VERSION = 2.21
|
||||||
|
|
||||||
# Public API, requires no auth.
|
# Public API, requires no auth.
|
||||||
router_public = APIRouter()
|
router_public = APIRouter()
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, WebSocketDisconnect
|
from fastapi import APIRouter, Depends
|
||||||
from fastapi.websockets import WebSocket, WebSocketState
|
from fastapi.websockets import WebSocket
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
from websockets.exceptions import WebSocketException
|
|
||||||
|
|
||||||
from freqtrade.enums import RPCMessageType, RPCRequestType
|
from freqtrade.enums import RPCMessageType, RPCRequestType
|
||||||
from freqtrade.rpc.api_server.api_auth import validate_ws_token
|
from freqtrade.rpc.api_server.api_auth import validate_ws_token
|
||||||
from freqtrade.rpc.api_server.deps import get_channel_manager, get_rpc
|
from freqtrade.rpc.api_server.deps import get_message_stream, get_rpc
|
||||||
from freqtrade.rpc.api_server.ws import WebSocketChannel
|
from freqtrade.rpc.api_server.ws.channel import WebSocketChannel, create_channel
|
||||||
from freqtrade.rpc.api_server.ws.channel import ChannelManager
|
from freqtrade.rpc.api_server.ws.message_stream import MessageStream
|
||||||
from freqtrade.rpc.api_server.ws_schemas import (WSAnalyzedDFMessage, WSMessageSchema,
|
from freqtrade.rpc.api_server.ws_schemas import (WSAnalyzedDFMessage, WSMessageSchema,
|
||||||
WSRequestSchema, WSWhitelistMessage)
|
WSRequestSchema, WSWhitelistMessage)
|
||||||
from freqtrade.rpc.rpc import RPC
|
from freqtrade.rpc.rpc import RPC
|
||||||
@ -22,23 +22,35 @@ logger = logging.getLogger(__name__)
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
async def is_websocket_alive(ws: WebSocket) -> bool:
|
async def channel_reader(channel: WebSocketChannel, rpc: RPC):
|
||||||
"""
|
"""
|
||||||
Check if a FastAPI Websocket is still open
|
Iterate over the messages from the channel and process the request
|
||||||
"""
|
"""
|
||||||
if (
|
async for message in channel:
|
||||||
ws.application_state == WebSocketState.CONNECTED and
|
await _process_consumer_request(message, channel, rpc)
|
||||||
ws.client_state == WebSocketState.CONNECTED
|
|
||||||
):
|
|
||||||
return True
|
async def channel_broadcaster(channel: WebSocketChannel, message_stream: MessageStream):
|
||||||
return False
|
"""
|
||||||
|
Iterate over messages in the message stream and send them
|
||||||
|
"""
|
||||||
|
async for message, ts in message_stream:
|
||||||
|
if channel.subscribed_to(message.get('type')):
|
||||||
|
# Log a warning if this channel is behind
|
||||||
|
# on the message stream by a lot
|
||||||
|
if (time.time() - ts) > 60:
|
||||||
|
logger.warning(f"Channel {channel} is behind MessageStream by 1 minute,"
|
||||||
|
" this can cause a memory leak if you see this message"
|
||||||
|
" often, consider reducing pair list size or amount of"
|
||||||
|
" consumers.")
|
||||||
|
|
||||||
|
await channel.send(message, timeout=True)
|
||||||
|
|
||||||
|
|
||||||
async def _process_consumer_request(
|
async def _process_consumer_request(
|
||||||
request: Dict[str, Any],
|
request: Dict[str, Any],
|
||||||
channel: WebSocketChannel,
|
channel: WebSocketChannel,
|
||||||
rpc: RPC,
|
rpc: RPC
|
||||||
channel_manager: ChannelManager
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Validate and handle a request from a websocket consumer
|
Validate and handle a request from a websocket consumer
|
||||||
@ -74,68 +86,29 @@ async def _process_consumer_request(
|
|||||||
|
|
||||||
# Format response
|
# Format response
|
||||||
response = WSWhitelistMessage(data=whitelist)
|
response = WSWhitelistMessage(data=whitelist)
|
||||||
# Send it back
|
await channel.send(response.dict(exclude_none=True))
|
||||||
await channel_manager.send_direct(channel, response.dict(exclude_none=True))
|
|
||||||
|
|
||||||
elif type == RPCRequestType.ANALYZED_DF:
|
elif type == RPCRequestType.ANALYZED_DF:
|
||||||
limit = None
|
|
||||||
|
|
||||||
if data:
|
|
||||||
# Limit the amount of candles per dataframe to 'limit' or 1500
|
# Limit the amount of candles per dataframe to 'limit' or 1500
|
||||||
limit = max(data.get('limit', 1500), 1500)
|
limit = min(data.get('limit', 1500), 1500) if data else None
|
||||||
|
|
||||||
# They requested the full historical analyzed dataframes
|
# For every pair in the generator, send a separate message
|
||||||
analyzed_df = rpc._ws_request_analyzed_df(limit)
|
for message in rpc._ws_request_analyzed_df(limit):
|
||||||
|
# Format response
|
||||||
# For every dataframe, send as a separate message
|
|
||||||
for _, message in analyzed_df.items():
|
|
||||||
response = WSAnalyzedDFMessage(data=message)
|
response = WSAnalyzedDFMessage(data=message)
|
||||||
await channel_manager.send_direct(channel, response.dict(exclude_none=True))
|
await channel.send(response.dict(exclude_none=True))
|
||||||
|
|
||||||
|
|
||||||
@router.websocket("/message/ws")
|
@router.websocket("/message/ws")
|
||||||
async def message_endpoint(
|
async def message_endpoint(
|
||||||
ws: WebSocket,
|
websocket: WebSocket,
|
||||||
|
token: str = Depends(validate_ws_token),
|
||||||
rpc: RPC = Depends(get_rpc),
|
rpc: RPC = Depends(get_rpc),
|
||||||
channel_manager=Depends(get_channel_manager),
|
message_stream: MessageStream = Depends(get_message_stream)
|
||||||
token: str = Depends(validate_ws_token)
|
|
||||||
):
|
):
|
||||||
"""
|
if token:
|
||||||
Message WebSocket endpoint, facilitates sending RPC messages
|
async with create_channel(websocket) as channel:
|
||||||
"""
|
await channel.run_channel_tasks(
|
||||||
try:
|
channel_reader(channel, rpc),
|
||||||
channel = await channel_manager.on_connect(ws)
|
channel_broadcaster(channel, message_stream)
|
||||||
if await is_websocket_alive(ws):
|
)
|
||||||
|
|
||||||
logger.info(f"Consumer connected - {channel}")
|
|
||||||
|
|
||||||
# Keep connection open until explicitly closed, and process requests
|
|
||||||
try:
|
|
||||||
while not channel.is_closed():
|
|
||||||
request = await channel.recv()
|
|
||||||
|
|
||||||
# Process the request here
|
|
||||||
await _process_consumer_request(request, channel, rpc, channel_manager)
|
|
||||||
|
|
||||||
except (WebSocketDisconnect, WebSocketException):
|
|
||||||
# Handle client disconnects
|
|
||||||
logger.info(f"Consumer disconnected - {channel}")
|
|
||||||
except RuntimeError:
|
|
||||||
# Handle cases like -
|
|
||||||
# RuntimeError('Cannot call "send" once a closed message has been sent')
|
|
||||||
pass
|
|
||||||
except Exception as e:
|
|
||||||
logger.info(f"Consumer connection failed - {channel}: {e}")
|
|
||||||
logger.debug(e, exc_info=e)
|
|
||||||
|
|
||||||
except RuntimeError:
|
|
||||||
# WebSocket was closed
|
|
||||||
# Do nothing
|
|
||||||
pass
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to serve - {ws.client}")
|
|
||||||
# Log tracebacks to keep track of what errors are happening
|
|
||||||
logger.exception(e)
|
|
||||||
finally:
|
|
||||||
if channel:
|
|
||||||
await channel_manager.on_disconnect(ws)
|
|
||||||
|
@ -41,8 +41,8 @@ def get_exchange(config=Depends(get_config)):
|
|||||||
return ApiServer._exchange
|
return ApiServer._exchange
|
||||||
|
|
||||||
|
|
||||||
def get_channel_manager():
|
def get_message_stream():
|
||||||
return ApiServer._ws_channel_manager
|
return ApiServer._message_stream
|
||||||
|
|
||||||
|
|
||||||
def is_webserver_mode(config=Depends(get_config)):
|
def is_webserver_mode(config=Depends(get_config)):
|
||||||
|
@ -1,22 +1,17 @@
|
|||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
from threading import Thread
|
from typing import Any, Dict, Optional
|
||||||
from typing import Any, Dict
|
|
||||||
|
|
||||||
import orjson
|
import orjson
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi import Depends, FastAPI
|
from fastapi import Depends, FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
# Look into alternatives
|
|
||||||
from janus import Queue as ThreadedQueue
|
|
||||||
from starlette.responses import JSONResponse
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
from freqtrade.constants import Config
|
from freqtrade.constants import Config
|
||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
from freqtrade.rpc.api_server.uvicorn_threaded import UvicornServer
|
from freqtrade.rpc.api_server.uvicorn_threaded import UvicornServer
|
||||||
from freqtrade.rpc.api_server.ws import ChannelManager
|
from freqtrade.rpc.api_server.ws.message_stream import MessageStream
|
||||||
from freqtrade.rpc.api_server.ws_schemas import WSMessageSchemaType
|
|
||||||
from freqtrade.rpc.rpc import RPC, RPCException, RPCHandler
|
from freqtrade.rpc.rpc import RPC, RPCException, RPCHandler
|
||||||
|
|
||||||
|
|
||||||
@ -50,10 +45,8 @@ class ApiServer(RPCHandler):
|
|||||||
_config: Config = {}
|
_config: Config = {}
|
||||||
# Exchange - only available in webserver mode.
|
# Exchange - only available in webserver mode.
|
||||||
_exchange = None
|
_exchange = None
|
||||||
# websocket message queue stuff
|
# websocket message stuff
|
||||||
_ws_channel_manager = None
|
_message_stream: Optional[MessageStream] = None
|
||||||
_ws_thread = None
|
|
||||||
_ws_loop = None
|
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -71,15 +64,11 @@ class ApiServer(RPCHandler):
|
|||||||
return
|
return
|
||||||
self._standalone: bool = standalone
|
self._standalone: bool = standalone
|
||||||
self._server = None
|
self._server = None
|
||||||
self._ws_queue = None
|
|
||||||
self._ws_background_task = None
|
|
||||||
|
|
||||||
ApiServer.__initialized = True
|
ApiServer.__initialized = True
|
||||||
|
|
||||||
api_config = self._config['api_server']
|
api_config = self._config['api_server']
|
||||||
|
|
||||||
ApiServer._ws_channel_manager = ChannelManager()
|
|
||||||
|
|
||||||
self.app = FastAPI(title="Freqtrade API",
|
self.app = FastAPI(title="Freqtrade API",
|
||||||
docs_url='/docs' if api_config.get('enable_openapi', False) else None,
|
docs_url='/docs' if api_config.get('enable_openapi', False) else None,
|
||||||
redoc_url=None,
|
redoc_url=None,
|
||||||
@ -105,21 +94,9 @@ class ApiServer(RPCHandler):
|
|||||||
del ApiServer._rpc
|
del ApiServer._rpc
|
||||||
if self._server and not self._standalone:
|
if self._server and not self._standalone:
|
||||||
logger.info("Stopping API Server")
|
logger.info("Stopping API Server")
|
||||||
|
# self._server.force_exit, self._server.should_exit = True, True
|
||||||
self._server.cleanup()
|
self._server.cleanup()
|
||||||
|
|
||||||
if self._ws_thread and self._ws_loop:
|
|
||||||
logger.info("Stopping API Server background tasks")
|
|
||||||
|
|
||||||
if self._ws_background_task:
|
|
||||||
# Cancel the queue task
|
|
||||||
self._ws_background_task.cancel()
|
|
||||||
|
|
||||||
self._ws_thread.join()
|
|
||||||
|
|
||||||
self._ws_thread = None
|
|
||||||
self._ws_loop = None
|
|
||||||
self._ws_background_task = None
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def shutdown(cls):
|
def shutdown(cls):
|
||||||
cls.__initialized = False
|
cls.__initialized = False
|
||||||
@ -129,9 +106,11 @@ class ApiServer(RPCHandler):
|
|||||||
cls._rpc = None
|
cls._rpc = None
|
||||||
|
|
||||||
def send_msg(self, msg: Dict[str, Any]) -> None:
|
def send_msg(self, msg: Dict[str, Any]) -> None:
|
||||||
if self._ws_queue:
|
"""
|
||||||
sync_q = self._ws_queue.sync_q
|
Publish the message to the message stream
|
||||||
sync_q.put(msg)
|
"""
|
||||||
|
if ApiServer._message_stream:
|
||||||
|
ApiServer._message_stream.publish(msg)
|
||||||
|
|
||||||
def handle_rpc_exception(self, request, exc):
|
def handle_rpc_exception(self, request, exc):
|
||||||
logger.exception(f"API Error calling: {exc}")
|
logger.exception(f"API Error calling: {exc}")
|
||||||
@ -170,50 +149,30 @@ class ApiServer(RPCHandler):
|
|||||||
)
|
)
|
||||||
|
|
||||||
app.add_exception_handler(RPCException, self.handle_rpc_exception)
|
app.add_exception_handler(RPCException, self.handle_rpc_exception)
|
||||||
|
app.add_event_handler(
|
||||||
|
event_type="startup",
|
||||||
|
func=self._api_startup_event
|
||||||
|
)
|
||||||
|
app.add_event_handler(
|
||||||
|
event_type="shutdown",
|
||||||
|
func=self._api_shutdown_event
|
||||||
|
)
|
||||||
|
|
||||||
def start_message_queue(self):
|
async def _api_startup_event(self):
|
||||||
if self._ws_thread:
|
"""
|
||||||
return
|
Creates the MessageStream class on startup
|
||||||
|
so it has access to the same event loop
|
||||||
|
as uvicorn
|
||||||
|
"""
|
||||||
|
if not ApiServer._message_stream:
|
||||||
|
ApiServer._message_stream = MessageStream()
|
||||||
|
|
||||||
# Create a new loop, as it'll be just for the background thread
|
async def _api_shutdown_event(self):
|
||||||
self._ws_loop = asyncio.new_event_loop()
|
"""
|
||||||
|
Removes the MessageStream class on shutdown
|
||||||
# Start the thread
|
"""
|
||||||
self._ws_thread = Thread(target=self._ws_loop.run_forever)
|
if ApiServer._message_stream:
|
||||||
self._ws_thread.start()
|
ApiServer._message_stream = None
|
||||||
|
|
||||||
# Finally, submit the coro to the thread
|
|
||||||
self._ws_background_task = asyncio.run_coroutine_threadsafe(
|
|
||||||
self._broadcast_queue_data(), loop=self._ws_loop)
|
|
||||||
|
|
||||||
async def _broadcast_queue_data(self):
|
|
||||||
# Instantiate the queue in this coroutine so it's attached to our loop
|
|
||||||
self._ws_queue = ThreadedQueue()
|
|
||||||
async_queue = self._ws_queue.async_q
|
|
||||||
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
logger.debug("Getting queue messages...")
|
|
||||||
# Get data from queue
|
|
||||||
message: WSMessageSchemaType = await async_queue.get()
|
|
||||||
logger.debug(f"Found message of type: {message.get('type')}")
|
|
||||||
async_queue.task_done()
|
|
||||||
# Broadcast it
|
|
||||||
await self._ws_channel_manager.broadcast(message)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# For testing, shouldn't happen when stable
|
|
||||||
except Exception as e:
|
|
||||||
logger.exception(f"Exception happened in background task: {e}")
|
|
||||||
|
|
||||||
finally:
|
|
||||||
# Disconnect channels and stop the loop on cancel
|
|
||||||
await self._ws_channel_manager.disconnect_all()
|
|
||||||
self._ws_loop.stop()
|
|
||||||
# Avoid adding more items to the queue if they aren't
|
|
||||||
# going to get broadcasted.
|
|
||||||
self._ws_queue = None
|
|
||||||
|
|
||||||
def start_api(self):
|
def start_api(self):
|
||||||
"""
|
"""
|
||||||
@ -253,7 +212,6 @@ class ApiServer(RPCHandler):
|
|||||||
if self._standalone:
|
if self._standalone:
|
||||||
self._server.run()
|
self._server.run()
|
||||||
else:
|
else:
|
||||||
self.start_message_queue()
|
|
||||||
self._server.run_in_thread()
|
self._server.run_in_thread()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Api server failed to start.")
|
logger.exception("Api server failed to start.")
|
||||||
|
@ -3,4 +3,5 @@
|
|||||||
from freqtrade.rpc.api_server.ws.types import WebSocketType
|
from freqtrade.rpc.api_server.ws.types import WebSocketType
|
||||||
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
|
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
|
||||||
from freqtrade.rpc.api_server.ws.serializer import HybridJSONWebSocketSerializer
|
from freqtrade.rpc.api_server.ws.serializer import HybridJSONWebSocketSerializer
|
||||||
from freqtrade.rpc.api_server.ws.channel import ChannelManager, WebSocketChannel
|
from freqtrade.rpc.api_server.ws.channel import WebSocketChannel
|
||||||
|
from freqtrade.rpc.api_server.ws.message_stream import MessageStream
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from threading import RLock
|
from collections import deque
|
||||||
from typing import Any, Dict, List, Optional, Type, Union
|
from contextlib import asynccontextmanager
|
||||||
|
from typing import Any, AsyncIterator, Deque, Dict, List, Optional, Type, Union
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from fastapi import WebSocket as FastAPIWebSocket
|
from fastapi import WebSocketDisconnect
|
||||||
|
from websockets.exceptions import ConnectionClosed
|
||||||
|
|
||||||
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
|
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
|
||||||
from freqtrade.rpc.api_server.ws.serializer import (HybridJSONWebSocketSerializer,
|
from freqtrade.rpc.api_server.ws.serializer import (HybridJSONWebSocketSerializer,
|
||||||
@ -21,36 +23,30 @@ class WebSocketChannel:
|
|||||||
"""
|
"""
|
||||||
Object to help facilitate managing a websocket connection
|
Object to help facilitate managing a websocket connection
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
websocket: WebSocketType,
|
websocket: WebSocketType,
|
||||||
channel_id: Optional[str] = None,
|
channel_id: Optional[str] = None,
|
||||||
drain_timeout: int = 3,
|
|
||||||
throttle: float = 0.01,
|
|
||||||
serializer_cls: Type[WebSocketSerializer] = HybridJSONWebSocketSerializer
|
serializer_cls: Type[WebSocketSerializer] = HybridJSONWebSocketSerializer
|
||||||
):
|
):
|
||||||
|
|
||||||
self.channel_id = channel_id if channel_id else uuid4().hex[:8]
|
self.channel_id = channel_id if channel_id else uuid4().hex[:8]
|
||||||
|
|
||||||
# The WebSocket object
|
|
||||||
self._websocket = WebSocketProxy(websocket)
|
self._websocket = WebSocketProxy(websocket)
|
||||||
# The Serializing class for the WebSocket object
|
|
||||||
self._serializer_cls = serializer_cls
|
|
||||||
|
|
||||||
self.drain_timeout = drain_timeout
|
|
||||||
self.throttle = throttle
|
|
||||||
|
|
||||||
self._subscriptions: List[str] = []
|
|
||||||
# 32 is the size of the receiving queue in websockets package
|
|
||||||
self.queue: asyncio.Queue[Dict[str, Any]] = asyncio.Queue(maxsize=32)
|
|
||||||
self._relay_task = asyncio.create_task(self.relay())
|
|
||||||
|
|
||||||
# Internal event to signify a closed websocket
|
# Internal event to signify a closed websocket
|
||||||
self._closed = asyncio.Event()
|
self._closed = asyncio.Event()
|
||||||
|
# The async tasks created for the channel
|
||||||
|
self._channel_tasks: List[asyncio.Task] = []
|
||||||
|
|
||||||
|
# Deque for average send times
|
||||||
|
self._send_times: Deque[float] = deque([], maxlen=10)
|
||||||
|
# High limit defaults to 3 to start
|
||||||
|
self._send_high_limit = 3
|
||||||
|
|
||||||
|
# The subscribed message types
|
||||||
|
self._subscriptions: List[str] = []
|
||||||
|
|
||||||
# Wrap the WebSocket in the Serializing class
|
# Wrap the WebSocket in the Serializing class
|
||||||
self._wrapped_ws = self._serializer_cls(self._websocket)
|
self._wrapped_ws = serializer_cls(self._websocket)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"WebSocketChannel({self.channel_id}, {self.remote_addr})"
|
return f"WebSocketChannel({self.channel_id}, {self.remote_addr})"
|
||||||
@ -63,40 +59,58 @@ class WebSocketChannel:
|
|||||||
def remote_addr(self):
|
def remote_addr(self):
|
||||||
return self._websocket.remote_addr
|
return self._websocket.remote_addr
|
||||||
|
|
||||||
async def _send(self, data):
|
@property
|
||||||
"""
|
def avg_send_time(self):
|
||||||
Send data on the wrapped websocket
|
return sum(self._send_times) / len(self._send_times)
|
||||||
"""
|
|
||||||
await self._wrapped_ws.send(data)
|
|
||||||
|
|
||||||
async def send(self, data) -> bool:
|
def _calc_send_limit(self):
|
||||||
"""
|
"""
|
||||||
Add the data to the queue to be sent.
|
Calculate the send high limit for this channel
|
||||||
:returns: True if data added to queue, False otherwise
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# This block only runs if the queue is full, it will wait
|
# Only update if we have enough data
|
||||||
# until self.drain_timeout for the relay to drain the outgoing queue
|
if len(self._send_times) == self._send_times.maxlen:
|
||||||
# We can't use asyncio.wait_for here because the queue may have been created with a
|
# At least 1s or twice the average of send times, with a
|
||||||
# different eventloop
|
# maximum of 3 seconds per message
|
||||||
start = time.time()
|
self._send_high_limit = min(max(self.avg_send_time * 2, 1), 3)
|
||||||
while self.queue.full():
|
|
||||||
await asyncio.sleep(1)
|
|
||||||
if (time.time() - start) > self.drain_timeout:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# If for some reason the queue is still full, just return False
|
async def send(
|
||||||
|
self,
|
||||||
|
message: Union[WSMessageSchemaType, Dict[str, Any]],
|
||||||
|
timeout: bool = False
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Send a message on the wrapped websocket. If the sending
|
||||||
|
takes too long, it will raise a TimeoutError and
|
||||||
|
disconnect the connection.
|
||||||
|
|
||||||
|
:param message: The message to send
|
||||||
|
:param timeout: Enforce send high limit, defaults to False
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
self.queue.put_nowait(data)
|
_ = time.time()
|
||||||
except asyncio.QueueFull:
|
# If the send times out, it will raise
|
||||||
return False
|
# a TimeoutError and bubble up to the
|
||||||
|
# message_endpoint to close the connection
|
||||||
|
await asyncio.wait_for(
|
||||||
|
self._wrapped_ws.send(message),
|
||||||
|
timeout=self._send_high_limit if timeout else None
|
||||||
|
)
|
||||||
|
total_time = time.time() - _
|
||||||
|
self._send_times.append(total_time)
|
||||||
|
|
||||||
# If we got here everything is ok
|
self._calc_send_limit()
|
||||||
return True
|
except asyncio.TimeoutError:
|
||||||
|
logger.info(f"Connection for {self} timed out, disconnecting")
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Explicitly give control back to event loop as
|
||||||
|
# websockets.send does not
|
||||||
|
await asyncio.sleep(0.01)
|
||||||
|
|
||||||
async def recv(self):
|
async def recv(self):
|
||||||
"""
|
"""
|
||||||
Receive data on the wrapped websocket
|
Receive a message on the wrapped websocket
|
||||||
"""
|
"""
|
||||||
return await self._wrapped_ws.recv()
|
return await self._wrapped_ws.recv()
|
||||||
|
|
||||||
@ -106,18 +120,28 @@ class WebSocketChannel:
|
|||||||
"""
|
"""
|
||||||
return await self._websocket.ping()
|
return await self._websocket.ping()
|
||||||
|
|
||||||
|
async def accept(self):
|
||||||
|
"""
|
||||||
|
Accept the underlying websocket connection,
|
||||||
|
if the connection has been closed before we can
|
||||||
|
accept, just close the channel.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return await self._websocket.accept()
|
||||||
|
except RuntimeError:
|
||||||
|
await self.close()
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
"""
|
"""
|
||||||
Close the WebSocketChannel
|
Close the WebSocketChannel
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
|
||||||
await self.raw_websocket.close()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self._closed.set()
|
self._closed.set()
|
||||||
self._relay_task.cancel()
|
|
||||||
|
try:
|
||||||
|
await self._websocket.close()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
def is_closed(self) -> bool:
|
def is_closed(self) -> bool:
|
||||||
"""
|
"""
|
||||||
@ -141,99 +165,76 @@ class WebSocketChannel:
|
|||||||
"""
|
"""
|
||||||
return message_type in self._subscriptions
|
return message_type in self._subscriptions
|
||||||
|
|
||||||
async def relay(self):
|
async def run_channel_tasks(self, *tasks, **kwargs):
|
||||||
"""
|
"""
|
||||||
Relay messages from the channel's queue and send them out. This is started
|
Create and await on the channel tasks unless an exception
|
||||||
as a task.
|
was raised, then cancel them all.
|
||||||
|
|
||||||
|
:params *tasks: All coros or tasks to be run concurrently
|
||||||
|
:param **kwargs: Any extra kwargs to pass to gather
|
||||||
"""
|
"""
|
||||||
while not self._closed.is_set():
|
|
||||||
message = await self.queue.get()
|
if not self.is_closed():
|
||||||
|
# Wrap the coros into tasks if they aren't already
|
||||||
|
self._channel_tasks = [
|
||||||
|
task if isinstance(task, asyncio.Task) else asyncio.create_task(task)
|
||||||
|
for task in tasks
|
||||||
|
]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await self._send(message)
|
return await asyncio.gather(*self._channel_tasks, **kwargs)
|
||||||
self.queue.task_done()
|
except Exception:
|
||||||
|
# If an exception occurred, cancel the rest of the tasks
|
||||||
|
await self.cancel_channel_tasks()
|
||||||
|
|
||||||
# Limit messages per sec.
|
async def cancel_channel_tasks(self):
|
||||||
# Could cause problems with queue size if too low, and
|
|
||||||
# problems with network traffik if too high.
|
|
||||||
# 0.01 = 100/s
|
|
||||||
await asyncio.sleep(self.throttle)
|
|
||||||
except RuntimeError:
|
|
||||||
# The connection was closed, just exit the task
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
class ChannelManager:
|
|
||||||
def __init__(self):
|
|
||||||
self.channels = dict()
|
|
||||||
self._lock = RLock() # Re-entrant Lock
|
|
||||||
|
|
||||||
async def on_connect(self, websocket: WebSocketType):
|
|
||||||
"""
|
"""
|
||||||
Wrap websocket connection into Channel and add to list
|
Cancel and wait on all channel tasks
|
||||||
|
|
||||||
:param websocket: The WebSocket object to attach to the Channel
|
|
||||||
"""
|
"""
|
||||||
if isinstance(websocket, FastAPIWebSocket):
|
for task in self._channel_tasks:
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
# Wait for tasks to finish cancelling
|
||||||
try:
|
try:
|
||||||
await websocket.accept()
|
await task
|
||||||
except RuntimeError:
|
except (
|
||||||
# The connection was closed before we could accept it
|
asyncio.CancelledError,
|
||||||
return
|
asyncio.TimeoutError,
|
||||||
|
WebSocketDisconnect,
|
||||||
|
ConnectionClosed,
|
||||||
|
RuntimeError
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
except Exception as e:
|
||||||
|
logger.info(f"Encountered unknown exception: {e}", exc_info=e)
|
||||||
|
|
||||||
ws_channel = WebSocketChannel(websocket)
|
self._channel_tasks = []
|
||||||
|
|
||||||
with self._lock:
|
async def __aiter__(self):
|
||||||
self.channels[websocket] = ws_channel
|
|
||||||
|
|
||||||
return ws_channel
|
|
||||||
|
|
||||||
async def on_disconnect(self, websocket: WebSocketType):
|
|
||||||
"""
|
"""
|
||||||
Call close on the channel if it's not, and remove from channel list
|
Generator for received messages
|
||||||
|
|
||||||
:param websocket: The WebSocket objet attached to the Channel
|
|
||||||
"""
|
"""
|
||||||
with self._lock:
|
# We can not catch any errors here as websocket.recv is
|
||||||
channel = self.channels.get(websocket)
|
# the first to catch any disconnects and bubble it up
|
||||||
if channel:
|
# so the connection is garbage collected right away
|
||||||
logger.info(f"Disconnecting channel {channel}")
|
while not self.is_closed():
|
||||||
if not channel.is_closed():
|
yield await self.recv()
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def create_channel(
|
||||||
|
websocket: WebSocketType,
|
||||||
|
**kwargs
|
||||||
|
) -> AsyncIterator[WebSocketChannel]:
|
||||||
|
"""
|
||||||
|
Context manager for safely opening and closing a WebSocketChannel
|
||||||
|
"""
|
||||||
|
channel = WebSocketChannel(websocket, **kwargs)
|
||||||
|
try:
|
||||||
|
await channel.accept()
|
||||||
|
logger.info(f"Connected to channel - {channel}")
|
||||||
|
|
||||||
|
yield channel
|
||||||
|
finally:
|
||||||
await channel.close()
|
await channel.close()
|
||||||
|
logger.info(f"Disconnected from channel - {channel}")
|
||||||
del self.channels[websocket]
|
|
||||||
|
|
||||||
async def disconnect_all(self):
|
|
||||||
"""
|
|
||||||
Disconnect all Channels
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
for websocket in self.channels.copy().keys():
|
|
||||||
await self.on_disconnect(websocket)
|
|
||||||
|
|
||||||
async def broadcast(self, message: WSMessageSchemaType):
|
|
||||||
"""
|
|
||||||
Broadcast a message on all Channels
|
|
||||||
|
|
||||||
:param message: The message to send
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
for channel in self.channels.copy().values():
|
|
||||||
if channel.subscribed_to(message.get('type')):
|
|
||||||
await self.send_direct(channel, message)
|
|
||||||
|
|
||||||
async def send_direct(
|
|
||||||
self, channel: WebSocketChannel, message: Union[WSMessageSchemaType, Dict[str, Any]]):
|
|
||||||
"""
|
|
||||||
Send a message directly through direct_channel only
|
|
||||||
|
|
||||||
:param direct_channel: The WebSocketChannel object to send the message through
|
|
||||||
:param message: The message to send
|
|
||||||
"""
|
|
||||||
if not await channel.send(message):
|
|
||||||
await self.on_disconnect(channel.raw_websocket)
|
|
||||||
|
|
||||||
def has_channels(self):
|
|
||||||
"""
|
|
||||||
Flag for more than 0 channels
|
|
||||||
"""
|
|
||||||
return len(self.channels) > 0
|
|
||||||
|
31
freqtrade/rpc/api_server/ws/message_stream.py
Normal file
31
freqtrade/rpc/api_server/ws/message_stream.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import asyncio
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class MessageStream:
|
||||||
|
"""
|
||||||
|
A message stream for consumers to subscribe to,
|
||||||
|
and for producers to publish to.
|
||||||
|
"""
|
||||||
|
def __init__(self):
|
||||||
|
self._loop = asyncio.get_running_loop()
|
||||||
|
self._waiter = self._loop.create_future()
|
||||||
|
|
||||||
|
def publish(self, message):
|
||||||
|
"""
|
||||||
|
Publish a message to this MessageStream
|
||||||
|
|
||||||
|
:param message: The message to publish
|
||||||
|
"""
|
||||||
|
waiter, self._waiter = self._waiter, self._loop.create_future()
|
||||||
|
waiter.set_result((message, time.time(), self._waiter))
|
||||||
|
|
||||||
|
async def __aiter__(self):
|
||||||
|
"""
|
||||||
|
Iterate over the messages in the message stream
|
||||||
|
"""
|
||||||
|
waiter = self._waiter
|
||||||
|
while True:
|
||||||
|
# Shield the future from being cancelled by a task waiting on it
|
||||||
|
message, ts, waiter = await asyncio.shield(waiter)
|
||||||
|
yield message, ts
|
@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
from typing import Any, Dict, Union
|
||||||
|
|
||||||
import orjson
|
import orjson
|
||||||
import rapidjson
|
import rapidjson
|
||||||
@ -7,6 +8,7 @@ from pandas import DataFrame
|
|||||||
|
|
||||||
from freqtrade.misc import dataframe_to_json, json_to_dataframe
|
from freqtrade.misc import dataframe_to_json, json_to_dataframe
|
||||||
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
|
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
|
||||||
|
from freqtrade.rpc.api_server.ws_schemas import WSMessageSchemaType
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -24,17 +26,13 @@ class WebSocketSerializer(ABC):
|
|||||||
def _deserialize(self, data):
|
def _deserialize(self, data):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def send(self, data: bytes):
|
async def send(self, data: Union[WSMessageSchemaType, Dict[str, Any]]):
|
||||||
await self._websocket.send(self._serialize(data))
|
await self._websocket.send(self._serialize(data))
|
||||||
|
|
||||||
async def recv(self) -> bytes:
|
async def recv(self) -> bytes:
|
||||||
data = await self._websocket.recv()
|
data = await self._websocket.recv()
|
||||||
|
|
||||||
return self._deserialize(data)
|
return self._deserialize(data)
|
||||||
|
|
||||||
async def close(self, code: int = 1000):
|
|
||||||
await self._websocket.close(code)
|
|
||||||
|
|
||||||
|
|
||||||
class HybridJSONWebSocketSerializer(WebSocketSerializer):
|
class HybridJSONWebSocketSerializer(WebSocketSerializer):
|
||||||
def _serialize(self, data) -> str:
|
def _serialize(self, data) -> str:
|
||||||
|
@ -31,6 +31,7 @@ class Producer(TypedDict):
|
|||||||
name: str
|
name: str
|
||||||
host: str
|
host: str
|
||||||
port: int
|
port: int
|
||||||
|
secure: bool
|
||||||
ws_token: str
|
ws_token: str
|
||||||
|
|
||||||
|
|
||||||
@ -180,7 +181,8 @@ class ExternalMessageConsumer:
|
|||||||
host, port = producer['host'], producer['port']
|
host, port = producer['host'], producer['port']
|
||||||
token = producer['ws_token']
|
token = producer['ws_token']
|
||||||
name = producer['name']
|
name = producer['name']
|
||||||
ws_url = f"ws://{host}:{port}/api/v1/message/ws?token={token}"
|
scheme = 'wss' if producer.get('secure', False) else 'ws'
|
||||||
|
ws_url = f"{scheme}://{host}:{port}/api/v1/message/ws?token={token}"
|
||||||
|
|
||||||
# This will raise InvalidURI if the url is bad
|
# This will raise InvalidURI if the url is bad
|
||||||
async with websockets.connect(
|
async with websockets.connect(
|
||||||
|
@ -5,7 +5,7 @@ import logging
|
|||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from datetime import date, datetime, timedelta, timezone
|
from datetime import date, datetime, timedelta, timezone
|
||||||
from math import isnan
|
from math import isnan
|
||||||
from typing import Any, Dict, List, Optional, Tuple, Union
|
from typing import Any, Dict, Generator, List, Optional, Tuple, Union
|
||||||
|
|
||||||
import arrow
|
import arrow
|
||||||
import psutil
|
import psutil
|
||||||
@ -218,9 +218,10 @@ class RPC:
|
|||||||
stoploss_current_dist_pct=round(stoploss_current_dist_ratio * 100, 2),
|
stoploss_current_dist_pct=round(stoploss_current_dist_ratio * 100, 2),
|
||||||
stoploss_entry_dist=stoploss_entry_dist,
|
stoploss_entry_dist=stoploss_entry_dist,
|
||||||
stoploss_entry_dist_ratio=round(stoploss_entry_dist_ratio, 8),
|
stoploss_entry_dist_ratio=round(stoploss_entry_dist_ratio, 8),
|
||||||
open_order='({} {} rem={:.8f})'.format(
|
open_order=(
|
||||||
order.order_type, order.side, order.remaining
|
f'({order.order_type} {order.side} rem={order.safe_remaining:.8f})' if
|
||||||
) if order else None,
|
order else None
|
||||||
|
),
|
||||||
))
|
))
|
||||||
results.append(trade_dict)
|
results.append(trade_dict)
|
||||||
return results
|
return results
|
||||||
@ -739,6 +740,24 @@ class RPC:
|
|||||||
self._freqtrade.wallets.update()
|
self._freqtrade.wallets.update()
|
||||||
return {'result': f'Created sell order for trade {trade_id}.'}
|
return {'result': f'Created sell order for trade {trade_id}.'}
|
||||||
|
|
||||||
|
def _force_entry_validations(self, pair: str, order_side: SignalDirection):
|
||||||
|
if not self._freqtrade.config.get('force_entry_enable', False):
|
||||||
|
raise RPCException('Force_entry not enabled.')
|
||||||
|
|
||||||
|
if self._freqtrade.state != State.RUNNING:
|
||||||
|
raise RPCException('trader is not running')
|
||||||
|
|
||||||
|
if order_side == SignalDirection.SHORT and self._freqtrade.trading_mode == TradingMode.SPOT:
|
||||||
|
raise RPCException("Can't go short on Spot markets.")
|
||||||
|
|
||||||
|
if pair not in self._freqtrade.exchange.get_markets(tradable_only=True):
|
||||||
|
raise RPCException('Symbol does not exist or market is not active.')
|
||||||
|
# Check if pair quote currency equals to the stake currency.
|
||||||
|
stake_currency = self._freqtrade.config.get('stake_currency')
|
||||||
|
if not self._freqtrade.exchange.get_pair_quote_currency(pair) == stake_currency:
|
||||||
|
raise RPCException(
|
||||||
|
f'Wrong pair selected. Only pairs with stake-currency {stake_currency} allowed.')
|
||||||
|
|
||||||
def _rpc_force_entry(self, pair: str, price: Optional[float], *,
|
def _rpc_force_entry(self, pair: str, price: Optional[float], *,
|
||||||
order_type: Optional[str] = None,
|
order_type: Optional[str] = None,
|
||||||
order_side: SignalDirection = SignalDirection.LONG,
|
order_side: SignalDirection = SignalDirection.LONG,
|
||||||
@ -749,21 +768,8 @@ class RPC:
|
|||||||
Handler for forcebuy <asset> <price>
|
Handler for forcebuy <asset> <price>
|
||||||
Buys a pair trade at the given or current price
|
Buys a pair trade at the given or current price
|
||||||
"""
|
"""
|
||||||
|
self._force_entry_validations(pair, order_side)
|
||||||
|
|
||||||
if not self._freqtrade.config.get('force_entry_enable', False):
|
|
||||||
raise RPCException('Force_entry not enabled.')
|
|
||||||
|
|
||||||
if self._freqtrade.state != State.RUNNING:
|
|
||||||
raise RPCException('trader is not running')
|
|
||||||
|
|
||||||
if order_side == SignalDirection.SHORT and self._freqtrade.trading_mode == TradingMode.SPOT:
|
|
||||||
raise RPCException("Can't go short on Spot markets.")
|
|
||||||
|
|
||||||
# Check if pair quote currency equals to the stake currency.
|
|
||||||
stake_currency = self._freqtrade.config.get('stake_currency')
|
|
||||||
if not self._freqtrade.exchange.get_pair_quote_currency(pair) == stake_currency:
|
|
||||||
raise RPCException(
|
|
||||||
f'Wrong pair selected. Only pairs with stake-currency {stake_currency} allowed.')
|
|
||||||
# check if valid pair
|
# check if valid pair
|
||||||
|
|
||||||
# check if pair already has an open pair
|
# check if pair already has an open pair
|
||||||
@ -773,6 +779,9 @@ class RPC:
|
|||||||
is_short = trade.is_short
|
is_short = trade.is_short
|
||||||
if not self._freqtrade.strategy.position_adjustment_enable:
|
if not self._freqtrade.strategy.position_adjustment_enable:
|
||||||
raise RPCException(f'position for {pair} already open - id: {trade.id}')
|
raise RPCException(f'position for {pair} already open - id: {trade.id}')
|
||||||
|
if trade.open_order_id is not None:
|
||||||
|
raise RPCException(f'position for {pair} already open - id: {trade.id} '
|
||||||
|
f'and has open order {trade.open_order_id}')
|
||||||
else:
|
else:
|
||||||
if Trade.get_open_trade_count() >= self._config['max_open_trades']:
|
if Trade.get_open_trade_count() >= self._config['max_open_trades']:
|
||||||
raise RPCException("Maximum number of trades is reached.")
|
raise RPCException("Maximum number of trades is reached.")
|
||||||
@ -785,6 +794,7 @@ class RPC:
|
|||||||
if not order_type:
|
if not order_type:
|
||||||
order_type = self._freqtrade.strategy.order_types.get(
|
order_type = self._freqtrade.strategy.order_types.get(
|
||||||
'force_entry', self._freqtrade.strategy.order_types['entry'])
|
'force_entry', self._freqtrade.strategy.order_types['entry'])
|
||||||
|
with self._freqtrade._exit_lock:
|
||||||
if self._freqtrade.execute_entry(pair, stake_amount, price,
|
if self._freqtrade.execute_entry(pair, stake_amount, price,
|
||||||
ordertype=order_type, trade=trade,
|
ordertype=order_type, trade=trade,
|
||||||
is_short=is_short,
|
is_short=is_short,
|
||||||
@ -1063,23 +1073,20 @@ class RPC:
|
|||||||
self,
|
self,
|
||||||
pairlist: List[str],
|
pairlist: List[str],
|
||||||
limit: Optional[int]
|
limit: Optional[int]
|
||||||
) -> Dict[str, Any]:
|
) -> Generator[Dict[str, Any], None, None]:
|
||||||
""" Get the analysed dataframes of each pair in the pairlist """
|
""" Get the analysed dataframes of each pair in the pairlist """
|
||||||
timeframe = self._freqtrade.config['timeframe']
|
timeframe = self._freqtrade.config['timeframe']
|
||||||
candle_type = self._freqtrade.config.get('candle_type_def', CandleType.SPOT)
|
candle_type = self._freqtrade.config.get('candle_type_def', CandleType.SPOT)
|
||||||
_data = {}
|
|
||||||
|
|
||||||
for pair in pairlist:
|
for pair in pairlist:
|
||||||
dataframe, last_analyzed = self.__rpc_analysed_dataframe_raw(pair, timeframe, limit)
|
dataframe, last_analyzed = self.__rpc_analysed_dataframe_raw(pair, timeframe, limit)
|
||||||
|
|
||||||
_data[pair] = {
|
yield {
|
||||||
"key": (pair, timeframe, candle_type),
|
"key": (pair, timeframe, candle_type),
|
||||||
"df": dataframe,
|
"df": dataframe,
|
||||||
"la": last_analyzed
|
"la": last_analyzed
|
||||||
}
|
}
|
||||||
|
|
||||||
return _data
|
|
||||||
|
|
||||||
def _ws_request_analyzed_df(self, limit: Optional[int]):
|
def _ws_request_analyzed_df(self, limit: Optional[int]):
|
||||||
""" Historical Analyzed Dataframes for WebSocket """
|
""" Historical Analyzed Dataframes for WebSocket """
|
||||||
whitelist = self._freqtrade.active_pair_whitelist
|
whitelist = self._freqtrade.active_pair_whitelist
|
||||||
|
@ -6,7 +6,7 @@ from collections import deque
|
|||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
from freqtrade.constants import Config
|
from freqtrade.constants import Config
|
||||||
from freqtrade.enums import RPCMessageType
|
from freqtrade.enums import NO_ECHO_MESSAGES, RPCMessageType
|
||||||
from freqtrade.rpc import RPC, RPCHandler
|
from freqtrade.rpc import RPC, RPCHandler
|
||||||
|
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ class RPCManager:
|
|||||||
'status': 'stopping bot'
|
'status': 'stopping bot'
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
if msg.get('type') not in (RPCMessageType.ANALYZED_DF, RPCMessageType.WHITELIST):
|
if msg.get('type') not in NO_ECHO_MESSAGES:
|
||||||
logger.info('Sending rpc message: %s', msg)
|
logger.info('Sending rpc message: %s', msg)
|
||||||
if 'pair' in msg:
|
if 'pair' in msg:
|
||||||
msg.update({
|
msg.update({
|
||||||
|
@ -79,6 +79,8 @@ def authorized_only(command_handler: Callable[..., None]) -> Callable[..., Any]:
|
|||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
return command_handler(self, *args, **kwargs)
|
return command_handler(self, *args, **kwargs)
|
||||||
|
except RPCException as e:
|
||||||
|
self._send_msg(str(e))
|
||||||
except BaseException:
|
except BaseException:
|
||||||
logger.exception('Exception occurred within Telegram module')
|
logger.exception('Exception occurred within Telegram module')
|
||||||
|
|
||||||
@ -538,8 +540,6 @@ class Telegram(RPCHandler):
|
|||||||
handler for `/status` and `/status <id>`.
|
handler for `/status` and `/status <id>`.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
|
|
||||||
# Check if there's at least one numerical ID provided.
|
# Check if there's at least one numerical ID provided.
|
||||||
# If so, try to get only these trades.
|
# If so, try to get only these trades.
|
||||||
trade_ids = []
|
trade_ids = []
|
||||||
@ -602,9 +602,6 @@ class Telegram(RPCHandler):
|
|||||||
lines.extend(lines_detail if lines_detail else "")
|
lines.extend(lines_detail if lines_detail else "")
|
||||||
self.__send_status_msg(lines, r)
|
self.__send_status_msg(lines, r)
|
||||||
|
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
def __send_status_msg(self, lines: List[str], r: Dict[str, Any]) -> None:
|
def __send_status_msg(self, lines: List[str], r: Dict[str, Any]) -> None:
|
||||||
"""
|
"""
|
||||||
Send status message.
|
Send status message.
|
||||||
@ -630,7 +627,6 @@ class Telegram(RPCHandler):
|
|||||||
:param update: message update
|
:param update: message update
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
fiat_currency = self._config.get('fiat_display_currency', '')
|
fiat_currency = self._config.get('fiat_display_currency', '')
|
||||||
statlist, head, fiat_profit_sum = self._rpc._rpc_status_table(
|
statlist, head, fiat_profit_sum = self._rpc._rpc_status_table(
|
||||||
self._config['stake_currency'], fiat_currency)
|
self._config['stake_currency'], fiat_currency)
|
||||||
@ -659,8 +655,6 @@ class Telegram(RPCHandler):
|
|||||||
self._send_msg(f"<pre>{message}</pre>", parse_mode=ParseMode.HTML,
|
self._send_msg(f"<pre>{message}</pre>", parse_mode=ParseMode.HTML,
|
||||||
reload_able=True, callback_path="update_status_table",
|
reload_able=True, callback_path="update_status_table",
|
||||||
query=update.callback_query)
|
query=update.callback_query)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _timeunit_stats(self, update: Update, context: CallbackContext, unit: str) -> None:
|
def _timeunit_stats(self, update: Update, context: CallbackContext, unit: str) -> None:
|
||||||
@ -686,7 +680,6 @@ class Telegram(RPCHandler):
|
|||||||
timescale = int(context.args[0]) if context.args else val.default
|
timescale = int(context.args[0]) if context.args else val.default
|
||||||
except (TypeError, ValueError, IndexError):
|
except (TypeError, ValueError, IndexError):
|
||||||
timescale = val.default
|
timescale = val.default
|
||||||
try:
|
|
||||||
stats = self._rpc._rpc_timeunit_profit(
|
stats = self._rpc._rpc_timeunit_profit(
|
||||||
timescale,
|
timescale,
|
||||||
stake_cur,
|
stake_cur,
|
||||||
@ -713,8 +706,6 @@ class Telegram(RPCHandler):
|
|||||||
)
|
)
|
||||||
self._send_msg(message, parse_mode=ParseMode.HTML, reload_able=True,
|
self._send_msg(message, parse_mode=ParseMode.HTML, reload_able=True,
|
||||||
callback_path=val.callback, query=update.callback_query)
|
callback_path=val.callback, query=update.callback_query)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _daily(self, update: Update, context: CallbackContext) -> None:
|
def _daily(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -878,7 +869,6 @@ class Telegram(RPCHandler):
|
|||||||
@authorized_only
|
@authorized_only
|
||||||
def _balance(self, update: Update, context: CallbackContext) -> None:
|
def _balance(self, update: Update, context: CallbackContext) -> None:
|
||||||
""" Handler for /balance """
|
""" Handler for /balance """
|
||||||
try:
|
|
||||||
result = self._rpc._rpc_balance(self._config['stake_currency'],
|
result = self._rpc._rpc_balance(self._config['stake_currency'],
|
||||||
self._config.get('fiat_display_currency', ''))
|
self._config.get('fiat_display_currency', ''))
|
||||||
|
|
||||||
@ -949,8 +939,6 @@ class Telegram(RPCHandler):
|
|||||||
f"{fiat_val}\n")
|
f"{fiat_val}\n")
|
||||||
self._send_msg(output, reload_able=True, callback_path="update_balance",
|
self._send_msg(output, reload_able=True, callback_path="update_balance",
|
||||||
query=update.callback_query)
|
query=update.callback_query)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _start(self, update: Update, context: CallbackContext) -> None:
|
def _start(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1061,7 +1049,8 @@ class Telegram(RPCHandler):
|
|||||||
try:
|
try:
|
||||||
self._rpc._rpc_force_entry(pair, price, order_side=order_side)
|
self._rpc._rpc_force_entry(pair, price, order_side=order_side)
|
||||||
except RPCException as e:
|
except RPCException as e:
|
||||||
self._send_msg(str(e))
|
logger.exception("Forcebuy error!")
|
||||||
|
self._send_msg(str(e), ParseMode.HTML)
|
||||||
|
|
||||||
def _force_enter_inline(self, update: Update, _: CallbackContext) -> None:
|
def _force_enter_inline(self, update: Update, _: CallbackContext) -> None:
|
||||||
if update.callback_query:
|
if update.callback_query:
|
||||||
@ -1124,7 +1113,6 @@ class Telegram(RPCHandler):
|
|||||||
nrecent = int(context.args[0]) if context.args else 10
|
nrecent = int(context.args[0]) if context.args else 10
|
||||||
except (TypeError, ValueError, IndexError):
|
except (TypeError, ValueError, IndexError):
|
||||||
nrecent = 10
|
nrecent = 10
|
||||||
try:
|
|
||||||
trades = self._rpc._rpc_trade_history(
|
trades = self._rpc._rpc_trade_history(
|
||||||
nrecent
|
nrecent
|
||||||
)
|
)
|
||||||
@ -1142,8 +1130,6 @@ class Telegram(RPCHandler):
|
|||||||
message = (f"<b>{min(trades['trades_count'], nrecent)} recent trades</b>:\n"
|
message = (f"<b>{min(trades['trades_count'], nrecent)} recent trades</b>:\n"
|
||||||
+ (f"<pre>{trades_tab}</pre>" if trades['trades_count'] > 0 else ''))
|
+ (f"<pre>{trades_tab}</pre>" if trades['trades_count'] > 0 else ''))
|
||||||
self._send_msg(message, parse_mode=ParseMode.HTML)
|
self._send_msg(message, parse_mode=ParseMode.HTML)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _delete_trade(self, update: Update, context: CallbackContext) -> None:
|
def _delete_trade(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1154,7 +1140,6 @@ class Telegram(RPCHandler):
|
|||||||
:param update: message update
|
:param update: message update
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
if not context.args or len(context.args) == 0:
|
if not context.args or len(context.args) == 0:
|
||||||
raise RPCException("Trade-id not set.")
|
raise RPCException("Trade-id not set.")
|
||||||
trade_id = int(context.args[0])
|
trade_id = int(context.args[0])
|
||||||
@ -1164,9 +1149,6 @@ class Telegram(RPCHandler):
|
|||||||
'Please make sure to take care of this asset on the exchange manually.'
|
'Please make sure to take care of this asset on the exchange manually.'
|
||||||
))
|
))
|
||||||
|
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _performance(self, update: Update, context: CallbackContext) -> None:
|
def _performance(self, update: Update, context: CallbackContext) -> None:
|
||||||
"""
|
"""
|
||||||
@ -1176,7 +1158,6 @@ class Telegram(RPCHandler):
|
|||||||
:param update: message update
|
:param update: message update
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
trades = self._rpc._rpc_performance()
|
trades = self._rpc._rpc_performance()
|
||||||
output = "<b>Performance:</b>\n"
|
output = "<b>Performance:</b>\n"
|
||||||
for i, trade in enumerate(trades):
|
for i, trade in enumerate(trades):
|
||||||
@ -1195,8 +1176,6 @@ class Telegram(RPCHandler):
|
|||||||
self._send_msg(output, parse_mode=ParseMode.HTML,
|
self._send_msg(output, parse_mode=ParseMode.HTML,
|
||||||
reload_able=True, callback_path="update_performance",
|
reload_able=True, callback_path="update_performance",
|
||||||
query=update.callback_query)
|
query=update.callback_query)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _enter_tag_performance(self, update: Update, context: CallbackContext) -> None:
|
def _enter_tag_performance(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1207,7 +1186,6 @@ class Telegram(RPCHandler):
|
|||||||
:param update: message update
|
:param update: message update
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
pair = None
|
pair = None
|
||||||
if context.args and isinstance(context.args[0], str):
|
if context.args and isinstance(context.args[0], str):
|
||||||
pair = context.args[0]
|
pair = context.args[0]
|
||||||
@ -1230,8 +1208,6 @@ class Telegram(RPCHandler):
|
|||||||
self._send_msg(output, parse_mode=ParseMode.HTML,
|
self._send_msg(output, parse_mode=ParseMode.HTML,
|
||||||
reload_able=True, callback_path="update_enter_tag_performance",
|
reload_able=True, callback_path="update_enter_tag_performance",
|
||||||
query=update.callback_query)
|
query=update.callback_query)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _exit_reason_performance(self, update: Update, context: CallbackContext) -> None:
|
def _exit_reason_performance(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1242,7 +1218,6 @@ class Telegram(RPCHandler):
|
|||||||
:param update: message update
|
:param update: message update
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
pair = None
|
pair = None
|
||||||
if context.args and isinstance(context.args[0], str):
|
if context.args and isinstance(context.args[0], str):
|
||||||
pair = context.args[0]
|
pair = context.args[0]
|
||||||
@ -1265,8 +1240,6 @@ class Telegram(RPCHandler):
|
|||||||
self._send_msg(output, parse_mode=ParseMode.HTML,
|
self._send_msg(output, parse_mode=ParseMode.HTML,
|
||||||
reload_able=True, callback_path="update_exit_reason_performance",
|
reload_able=True, callback_path="update_exit_reason_performance",
|
||||||
query=update.callback_query)
|
query=update.callback_query)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _mix_tag_performance(self, update: Update, context: CallbackContext) -> None:
|
def _mix_tag_performance(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1277,7 +1250,6 @@ class Telegram(RPCHandler):
|
|||||||
:param update: message update
|
:param update: message update
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
pair = None
|
pair = None
|
||||||
if context.args and isinstance(context.args[0], str):
|
if context.args and isinstance(context.args[0], str):
|
||||||
pair = context.args[0]
|
pair = context.args[0]
|
||||||
@ -1300,8 +1272,6 @@ class Telegram(RPCHandler):
|
|||||||
self._send_msg(output, parse_mode=ParseMode.HTML,
|
self._send_msg(output, parse_mode=ParseMode.HTML,
|
||||||
reload_able=True, callback_path="update_mix_tag_performance",
|
reload_able=True, callback_path="update_mix_tag_performance",
|
||||||
query=update.callback_query)
|
query=update.callback_query)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _count(self, update: Update, context: CallbackContext) -> None:
|
def _count(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1312,7 +1282,6 @@ class Telegram(RPCHandler):
|
|||||||
:param update: message update
|
:param update: message update
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
counts = self._rpc._rpc_count()
|
counts = self._rpc._rpc_count()
|
||||||
message = tabulate({k: [v] for k, v in counts.items()},
|
message = tabulate({k: [v] for k, v in counts.items()},
|
||||||
headers=['current', 'max', 'total stake'],
|
headers=['current', 'max', 'total stake'],
|
||||||
@ -1322,8 +1291,6 @@ class Telegram(RPCHandler):
|
|||||||
self._send_msg(message, parse_mode=ParseMode.HTML,
|
self._send_msg(message, parse_mode=ParseMode.HTML,
|
||||||
reload_able=True, callback_path="update_count",
|
reload_able=True, callback_path="update_count",
|
||||||
query=update.callback_query)
|
query=update.callback_query)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _locks(self, update: Update, context: CallbackContext) -> None:
|
def _locks(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1371,7 +1338,6 @@ class Telegram(RPCHandler):
|
|||||||
Handler for /whitelist
|
Handler for /whitelist
|
||||||
Shows the currently active whitelist
|
Shows the currently active whitelist
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
whitelist = self._rpc._rpc_whitelist()
|
whitelist = self._rpc._rpc_whitelist()
|
||||||
|
|
||||||
if context.args:
|
if context.args:
|
||||||
@ -1385,8 +1351,6 @@ class Telegram(RPCHandler):
|
|||||||
|
|
||||||
logger.debug(message)
|
logger.debug(message)
|
||||||
self._send_msg(message)
|
self._send_msg(message)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _blacklist(self, update: Update, context: CallbackContext) -> None:
|
def _blacklist(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1423,7 +1387,6 @@ class Telegram(RPCHandler):
|
|||||||
Handler for /logs
|
Handler for /logs
|
||||||
Shows the latest logs
|
Shows the latest logs
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
try:
|
try:
|
||||||
limit = int(context.args[0]) if context.args else 10
|
limit = int(context.args[0]) if context.args else 10
|
||||||
except (TypeError, ValueError, IndexError):
|
except (TypeError, ValueError, IndexError):
|
||||||
@ -1446,8 +1409,6 @@ class Telegram(RPCHandler):
|
|||||||
|
|
||||||
if msgs:
|
if msgs:
|
||||||
self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN_V2)
|
self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN_V2)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _edge(self, update: Update, context: CallbackContext) -> None:
|
def _edge(self, update: Update, context: CallbackContext) -> None:
|
||||||
@ -1455,7 +1416,6 @@ class Telegram(RPCHandler):
|
|||||||
Handler for /edge
|
Handler for /edge
|
||||||
Shows information related to Edge
|
Shows information related to Edge
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
edge_pairs = self._rpc._rpc_edge()
|
edge_pairs = self._rpc._rpc_edge()
|
||||||
if not edge_pairs:
|
if not edge_pairs:
|
||||||
message = '<b>Edge only validated following pairs:</b>'
|
message = '<b>Edge only validated following pairs:</b>'
|
||||||
@ -1468,9 +1428,6 @@ class Telegram(RPCHandler):
|
|||||||
|
|
||||||
self._send_msg(message, parse_mode=ParseMode.HTML)
|
self._send_msg(message, parse_mode=ParseMode.HTML)
|
||||||
|
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _help(self, update: Update, context: CallbackContext) -> None:
|
def _help(self, update: Update, context: CallbackContext) -> None:
|
||||||
"""
|
"""
|
||||||
@ -1550,12 +1507,9 @@ class Telegram(RPCHandler):
|
|||||||
Handler for /health
|
Handler for /health
|
||||||
Shows the last process timestamp
|
Shows the last process timestamp
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
health = self._rpc._health()
|
health = self._rpc._health()
|
||||||
message = f"Last process: `{health['last_process_loc']}`"
|
message = f"Last process: `{health['last_process_loc']}`"
|
||||||
self._send_msg(message)
|
self._send_msg(message)
|
||||||
except RPCException as e:
|
|
||||||
self._send_msg(str(e))
|
|
||||||
|
|
||||||
@authorized_only
|
@authorized_only
|
||||||
def _version(self, update: Update, context: CallbackContext) -> None:
|
def _version(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
@ -68,6 +68,7 @@ class Webhook(RPCHandler):
|
|||||||
RPCMessageType.PROTECTION_TRIGGER_GLOBAL,
|
RPCMessageType.PROTECTION_TRIGGER_GLOBAL,
|
||||||
RPCMessageType.WHITELIST,
|
RPCMessageType.WHITELIST,
|
||||||
RPCMessageType.ANALYZED_DF,
|
RPCMessageType.ANALYZED_DF,
|
||||||
|
RPCMessageType.NEW_CANDLE,
|
||||||
RPCMessageType.STRATEGY_MSG):
|
RPCMessageType.STRATEGY_MSG):
|
||||||
# Don't fail for non-implemented types
|
# Don't fail for non-implemented types
|
||||||
return None
|
return None
|
||||||
|
@ -739,10 +739,10 @@ class IStrategy(ABC, HyperStrategyMixin):
|
|||||||
"""
|
"""
|
||||||
pair = str(metadata.get('pair'))
|
pair = str(metadata.get('pair'))
|
||||||
|
|
||||||
|
new_candle = self._last_candle_seen_per_pair.get(pair, None) != dataframe.iloc[-1]['date']
|
||||||
# Test if seen this pair and last candle before.
|
# Test if seen this pair and last candle before.
|
||||||
# always run if process_only_new_candles is set to false
|
# always run if process_only_new_candles is set to false
|
||||||
if (not self.process_only_new_candles or
|
if not self.process_only_new_candles or new_candle:
|
||||||
self._last_candle_seen_per_pair.get(pair, None) != dataframe.iloc[-1]['date']):
|
|
||||||
|
|
||||||
# Defs that only make change on new candle data.
|
# Defs that only make change on new candle data.
|
||||||
dataframe = self.analyze_ticker(dataframe, metadata)
|
dataframe = self.analyze_ticker(dataframe, metadata)
|
||||||
@ -751,7 +751,7 @@ class IStrategy(ABC, HyperStrategyMixin):
|
|||||||
|
|
||||||
candle_type = self.config.get('candle_type_def', CandleType.SPOT)
|
candle_type = self.config.get('candle_type_def', CandleType.SPOT)
|
||||||
self.dp._set_cached_df(pair, self.timeframe, dataframe, candle_type=candle_type)
|
self.dp._set_cached_df(pair, self.timeframe, dataframe, candle_type=candle_type)
|
||||||
self.dp._emit_df((pair, self.timeframe, candle_type), dataframe)
|
self.dp._emit_df((pair, self.timeframe, candle_type), dataframe, new_candle)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.debug("Skipping TA Analysis for already analyzed candle")
|
logger.debug("Skipping TA Analysis for already analyzed candle")
|
||||||
|
@ -19,7 +19,7 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
|||||||
|
|
||||||
Launching this strategy would be:
|
Launching this strategy would be:
|
||||||
|
|
||||||
freqtrade trade --strategy FreqaiExampleHyridStrategy --strategy-path freqtrade/templates
|
freqtrade trade --strategy FreqaiExampleHybridStrategy --strategy-path freqtrade/templates
|
||||||
--freqaimodel CatboostClassifier --config config_examples/config_freqai.example.json
|
--freqaimodel CatboostClassifier --config config_examples/config_freqai.example.json
|
||||||
|
|
||||||
or the user simply adds this to their config:
|
or the user simply adds this to their config:
|
||||||
@ -86,7 +86,7 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
|||||||
process_only_new_candles = True
|
process_only_new_candles = True
|
||||||
stoploss = -0.05
|
stoploss = -0.05
|
||||||
use_exit_signal = True
|
use_exit_signal = True
|
||||||
startup_candle_count: int = 300
|
startup_candle_count: int = 30
|
||||||
can_short = True
|
can_short = True
|
||||||
|
|
||||||
# Hyperoptable parameters
|
# Hyperoptable parameters
|
||||||
|
@ -328,7 +328,7 @@
|
|||||||
"# Show graph inline\n",
|
"# Show graph inline\n",
|
||||||
"# graph.show()\n",
|
"# graph.show()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Render graph in a seperate window\n",
|
"# Render graph in a separate window\n",
|
||||||
"graph.show(renderer=\"browser\")\n"
|
"graph.show(renderer=\"browser\")\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
18
freqtrade/util/gc_setup.py
Normal file
18
freqtrade/util/gc_setup.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import gc
|
||||||
|
import logging
|
||||||
|
import platform
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def gc_set_threshold():
|
||||||
|
"""
|
||||||
|
Reduce number of GC runs to improve performance (explanation video)
|
||||||
|
https://www.youtube.com/watch?v=p4Sn6UcFTOU
|
||||||
|
|
||||||
|
"""
|
||||||
|
if platform.python_implementation() == "CPython":
|
||||||
|
# allocs, g1, g2 = gc.get_threshold()
|
||||||
|
gc.set_threshold(50_000, 500, 1000)
|
||||||
|
logger.debug("Adjusting python allocations to reduce GC runs")
|
@ -150,14 +150,20 @@ class Worker:
|
|||||||
if timeframe:
|
if timeframe:
|
||||||
next_tf = timeframe_to_next_date(timeframe)
|
next_tf = timeframe_to_next_date(timeframe)
|
||||||
# Maximum throttling should be until new candle arrives
|
# Maximum throttling should be until new candle arrives
|
||||||
# Offset of 0.2s is added to ensure a new candle has been issued.
|
# Offset is added to ensure a new candle has been issued.
|
||||||
next_tf_with_offset = next_tf.timestamp() - time.time() + timeframe_offset
|
next_tft = next_tf.timestamp() - time.time()
|
||||||
|
next_tf_with_offset = next_tft + timeframe_offset
|
||||||
|
if next_tft < sleep_duration and sleep_duration < next_tf_with_offset:
|
||||||
|
# Avoid hitting a new loop between the new candle and the candle with offset
|
||||||
|
sleep_duration = next_tf_with_offset
|
||||||
sleep_duration = min(sleep_duration, next_tf_with_offset)
|
sleep_duration = min(sleep_duration, next_tf_with_offset)
|
||||||
sleep_duration = max(sleep_duration, 0.0)
|
sleep_duration = max(sleep_duration, 0.0)
|
||||||
# next_iter = datetime.now(timezone.utc) + timedelta(seconds=sleep_duration)
|
# next_iter = datetime.now(timezone.utc) + timedelta(seconds=sleep_duration)
|
||||||
|
|
||||||
logger.debug(f"Throttling with '{func.__name__}()': sleep for {sleep_duration:.2f} s, "
|
logger.debug(f"Throttling with '{func.__name__}()': sleep for {sleep_duration:.2f} s, "
|
||||||
f"last iteration took {time_passed:.2f} s.")
|
f"last iteration took {time_passed:.2f} s."
|
||||||
|
# f"next: {next_iter}"
|
||||||
|
)
|
||||||
self._sleep(sleep_duration)
|
self._sleep(sleep_duration)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ nav:
|
|||||||
- Parameter table: freqai-parameter-table.md
|
- Parameter table: freqai-parameter-table.md
|
||||||
- Feature engineering: freqai-feature-engineering.md
|
- Feature engineering: freqai-feature-engineering.md
|
||||||
- Running FreqAI: freqai-running.md
|
- Running FreqAI: freqai-running.md
|
||||||
|
- Reinforcement Learning: freqai-reinforcement-learning.md
|
||||||
- Developer guide: freqai-developers.md
|
- Developer guide: freqai-developers.md
|
||||||
- Short / Leverage: leverage.md
|
- Short / Leverage: leverage.md
|
||||||
- Utility Sub-commands: utils.md
|
- Utility Sub-commands: utils.md
|
||||||
|
@ -30,6 +30,8 @@ asyncio_mode = "auto"
|
|||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
ignore_missing_imports = true
|
ignore_missing_imports = true
|
||||||
|
namespace_packages = false
|
||||||
|
implicit_optional = true
|
||||||
warn_unused_ignores = true
|
warn_unused_ignores = true
|
||||||
exclude = [
|
exclude = [
|
||||||
'^build_helpers\.py$'
|
'^build_helpers\.py$'
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user