Merge pull request #4127 from freqtrade/new_release

New release 2020.12
This commit is contained in:
Matthias 2020-12-29 07:07:54 +01:00 committed by GitHub
commit f97e810429
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
117 changed files with 3767 additions and 1139 deletions

View File

@ -14,12 +14,109 @@ on:
- cron: '0 5 * * 4' - cron: '0 5 * * 4'
jobs: jobs:
build: build_linux:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
strategy: strategy:
matrix: matrix:
os: [ ubuntu-18.04, ubuntu-20.04, macos-latest ] os: [ ubuntu-18.04, ubuntu-20.04 ]
python-version: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Cache_dependencies
uses: actions/cache@v2
id: cache
with:
path: ~/dependencies/
key: ${{ runner.os }}-dependencies
- name: pip cache (linux)
uses: actions/cache@v2
if: startsWith(matrix.os, 'ubuntu')
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
run: |
python -m pip install --upgrade pip
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
run: |
pytest --random-order --cov=freqtrade --cov-config=.coveragerc
if: matrix.python-version != '3.9'
- name: Tests incl. ccxt compatibility tests
run: |
pytest --random-order --cov=freqtrade --cov-config=.coveragerc --longrun
if: matrix.python-version == '3.9'
- name: Coveralls
if: (startsWith(matrix.os, 'ubuntu-20') && matrix.python-version == '3.8')
env:
# Coveralls token. Not used as secret due to github not providing secrets to forked repositories
COVERALLS_REPO_TOKEN: 6D1m0xupS3FgutfuGao8keFf9Hc0FpIXu
run: |
# Allow failure for coveralls
coveralls -v || true
- name: Backtesting
run: |
cp config.json.example config.json
freqtrade create-userdir --userdir user_data
freqtrade backtesting --datadir tests/testdata --strategy SampleStrategy
- name: Hyperopt
run: |
cp config.json.example config.json
freqtrade create-userdir --userdir user_data
freqtrade hyperopt --datadir tests/testdata -e 5 --strategy SampleStrategy --hyperopt SampleHyperOpt --hyperopt-loss SharpeHyperOptLossDaily --print-all
- name: Flake8
run: |
flake8
- name: Sort imports (isort)
run: |
isort --check .
- name: Mypy
run: |
mypy freqtrade scripts
- name: Slack Notification
uses: homoluctus/slatify@v1.8.0
if: failure() && ( github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false)
with:
type: ${{ job.status }}
job_name: '*Freqtrade CI ${{ matrix.os }}*'
mention: 'here'
mention_if: 'failure'
channel: '#notifications'
url: ${{ secrets.SLACK_WEBHOOK }}
build_macos:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ macos-latest ]
python-version: [3.7, 3.8] python-version: [3.7, 3.8]
steps: steps:
@ -31,21 +128,14 @@ jobs:
python-version: ${{ matrix.python-version }} python-version: ${{ matrix.python-version }}
- name: Cache_dependencies - name: Cache_dependencies
uses: actions/cache@v1 uses: actions/cache@v2
id: cache id: cache
with: with:
path: ~/dependencies/ path: ~/dependencies/
key: ${{ runner.os }}-dependencies key: ${{ runner.os }}-dependencies
- name: pip cache (linux)
uses: actions/cache@preview
if: startsWith(matrix.os, 'ubuntu')
with:
path: ~/.cache/pip
key: test-${{ matrix.os }}-${{ matrix.python-version }}-pip
- name: pip cache (macOS) - name: pip cache (macOS)
uses: actions/cache@preview uses: actions/cache@v2
if: startsWith(matrix.os, 'macOS') if: startsWith(matrix.os, 'macOS')
with: with:
path: ~/Library/Caches/pip path: ~/Library/Caches/pip
@ -113,6 +203,7 @@ jobs:
channel: '#notifications' channel: '#notifications'
url: ${{ secrets.SLACK_WEBHOOK }} url: ${{ secrets.SLACK_WEBHOOK }}
build_windows: build_windows:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
@ -215,7 +306,7 @@ jobs:
# Notify on slack only once - when CI completes (and after deploy) in case it's successfull # Notify on slack only once - when CI completes (and after deploy) in case it's successfull
notify-complete: notify-complete:
needs: [ build, build_windows, docs_check ] needs: [ build_linux, build_macos, build_windows, docs_check ]
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
steps: steps:
- name: Slack Notification - name: Slack Notification
@ -228,8 +319,9 @@ jobs:
url: ${{ secrets.SLACK_WEBHOOK }} url: ${{ secrets.SLACK_WEBHOOK }}
deploy: deploy:
needs: [ build, build_windows, docs_check ] needs: [ build_linux, build_macos, build_windows, docs_check ]
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
if: (github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'release') && github.repository == 'freqtrade/freqtrade' if: (github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'release') && github.repository == 'freqtrade/freqtrade'
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2

View File

@ -4,5 +4,5 @@ build:
image: latest image: latest
python: python:
version: 3.6 version: 3.8
setup_py_install: false setup_py_install: false

View File

@ -1,9 +1,9 @@
os: os:
- linux - linux
dist: xenial dist: bionic
language: python language: python
python: python:
- 3.6 - 3.8
services: services:
- docker - docker
env: env:

View File

@ -12,7 +12,7 @@ Few pointers for contributions:
- New features need to contain unit tests, must conform to PEP8 (max-line-length = 100) and should be documented with the introduction PR. - New features need to contain unit tests, must conform to PEP8 (max-line-length = 100) and should be documented with the introduction PR.
- PR's can be declared as `[WIP]` - which signify Work in Progress Pull Requests (which are not finished). - PR's can be declared as `[WIP]` - which signify Work in Progress Pull Requests (which are not finished).
If you are unsure, discuss the feature on our [discord server](https://discord.gg/MA9v74M), on [Slack](https://join.slack.com/t/highfrequencybot/shared_invite/zt-jaut7r4m-Y17k4x5mcQES9a9swKuxbg) or in a [issue](https://github.com/freqtrade/freqtrade/issues) before a PR. If you are unsure, discuss the feature on our [discord server](https://discord.gg/MA9v74M), on [Slack](https://join.slack.com/t/highfrequencybot/shared_invite/zt-k9o2v5ut-jX8Mc4CwNM8CDc2Dyg96YA) or in a [issue](https://github.com/freqtrade/freqtrade/issues) before a PR.
## Getting started ## Getting started

View File

@ -1,24 +1,41 @@
FROM python:3.8.6-slim-buster FROM python:3.8.6-slim-buster as base
RUN apt-get update \ # Setup env
&& apt-get -y install curl build-essential libssl-dev sqlite3 \ ENV LANG C.UTF-8
&& apt-get clean \ ENV LC_ALL C.UTF-8
&& pip install --upgrade pip ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1
ENV PATH=/root/.local/bin:$PATH
# Prepare environment # Prepare environment
RUN mkdir /freqtrade RUN mkdir /freqtrade
WORKDIR /freqtrade WORKDIR /freqtrade
# Install dependencies
FROM base as python-deps
RUN apt-get update \
&& apt-get -y install curl build-essential libssl-dev git \
&& apt-get clean \
&& pip install --upgrade pip
# Install TA-lib # Install TA-lib
COPY build_helpers/* /tmp/ COPY build_helpers/* /tmp/
RUN cd /tmp && /tmp/install_ta-lib.sh && rm -r /tmp/*ta-lib* RUN cd /tmp && /tmp/install_ta-lib.sh && rm -r /tmp/*ta-lib*
ENV LD_LIBRARY_PATH /usr/local/lib ENV LD_LIBRARY_PATH /usr/local/lib
# Install dependencies # Install dependencies
COPY requirements.txt requirements-hyperopt.txt /freqtrade/ COPY requirements.txt requirements-hyperopt.txt /freqtrade/
RUN pip install numpy --no-cache-dir \ RUN pip install --user --no-cache-dir numpy \
&& pip install -r requirements-hyperopt.txt --no-cache-dir && pip install --user --no-cache-dir -r requirements-hyperopt.txt
# Copy dependencies to runtime-image
FROM base as runtime-image
COPY --from=python-deps /usr/local/lib /usr/local/lib
ENV LD_LIBRARY_PATH /usr/local/lib
COPY --from=python-deps /root/.local /root/.local
# Install and execute # Install and execute
COPY . /freqtrade/ COPY . /freqtrade/

View File

@ -1,25 +1,43 @@
FROM --platform=linux/arm/v7 python:3.7.7-slim-buster FROM --platform=linux/arm/v7 python:3.7.9-slim-buster as base
RUN apt-get update \ # Setup env
&& apt-get -y install curl build-essential libssl-dev libffi-dev libatlas3-base libgfortran5 sqlite3 \ ENV LANG C.UTF-8
&& apt-get clean \ ENV LC_ALL C.UTF-8
&& pip install --upgrade pip \ ENV PYTHONDONTWRITEBYTECODE 1
&& echo "[global]\nextra-index-url=https://www.piwheels.org/simple" > /etc/pip.conf ENV PYTHONFAULTHANDLER 1
ENV PATH=/root/.local/bin:$PATH
# Prepare environment # Prepare environment
RUN mkdir /freqtrade RUN mkdir /freqtrade
WORKDIR /freqtrade WORKDIR /freqtrade
RUN apt-get update \
&& apt-get -y install libatlas3-base curl sqlite3 \
&& apt-get clean
# Install dependencies
FROM base as python-deps
RUN apt-get -y install build-essential libssl-dev libffi-dev libgfortran5 \
&& apt-get clean \
&& pip install --upgrade pip \
&& echo "[global]\nextra-index-url=https://www.piwheels.org/simple" > /etc/pip.conf
# Install TA-lib # Install TA-lib
COPY build_helpers/* /tmp/ COPY build_helpers/* /tmp/
RUN cd /tmp && /tmp/install_ta-lib.sh && rm -r /tmp/*ta-lib* RUN cd /tmp && /tmp/install_ta-lib.sh && rm -r /tmp/*ta-lib*
ENV LD_LIBRARY_PATH /usr/local/lib ENV LD_LIBRARY_PATH /usr/local/lib
# Install dependencies # Install dependencies
COPY requirements.txt /freqtrade/ COPY requirements.txt /freqtrade/
RUN pip install numpy --no-cache-dir \ RUN pip install --user --no-cache-dir numpy \
&& pip install -r requirements.txt --no-cache-dir && pip install --user --no-cache-dir -r requirements.txt
# Copy dependencies to runtime-image
FROM base as runtime-image
COPY --from=python-deps /usr/local/lib /usr/local/lib
ENV LD_LIBRARY_PATH /usr/local/lib
COPY --from=python-deps /root/.local /root/.local
# Install and execute # Install and execute
COPY . /freqtrade/ COPY . /freqtrade/

View File

@ -37,7 +37,7 @@ Please find the complete documentation on our [website](https://www.freqtrade.io
## Features ## Features
- [x] **Based on Python 3.6+**: For botting on any operating system - Windows, macOS and Linux. - [x] **Based on Python 3.7+**: For botting on any operating system - Windows, macOS and Linux.
- [x] **Persistence**: Persistence is achieved through sqlite. - [x] **Persistence**: Persistence is achieved through sqlite.
- [x] **Dry-run**: Run the bot without playing money. - [x] **Dry-run**: Run the bot without playing money.
- [x] **Backtesting**: Run a simulation of your buy/sell strategy. - [x] **Backtesting**: Run a simulation of your buy/sell strategy.
@ -138,7 +138,7 @@ For any questions not covered by the documentation or for further information ab
Please check out our [discord server](https://discord.gg/MA9v74M). Please check out our [discord server](https://discord.gg/MA9v74M).
You can also join our [Slack channel](https://join.slack.com/t/highfrequencybot/shared_invite/zt-jaut7r4m-Y17k4x5mcQES9a9swKuxbg). You can also join our [Slack channel](https://join.slack.com/t/highfrequencybot/shared_invite/zt-k9o2v5ut-jX8Mc4CwNM8CDc2Dyg96YA).
### [Bugs / Issues](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue) ### [Bugs / Issues](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue)
@ -169,7 +169,7 @@ to understand the requirements before sending your pull-requests.
Coding is not a necessity to contribute - maybe start with improving our documentation? Coding is not a necessity to contribute - maybe start with improving our documentation?
Issues labeled [good first issue](https://github.com/freqtrade/freqtrade/labels/good%20first%20issue) can be good first contributions, and will help get you familiar with the codebase. Issues labeled [good first issue](https://github.com/freqtrade/freqtrade/labels/good%20first%20issue) can be good first contributions, and will help get you familiar with the codebase.
**Note** before starting any major new feature work, *please open an issue describing what you are planning to do* or talk to us on [discord](https://discord.gg/MA9v74M) or [Slack](https://join.slack.com/t/highfrequencybot/shared_invite/zt-jaut7r4m-Y17k4x5mcQES9a9swKuxbg). This will ensure that interested parties can give valuable feedback on the feature, and let others know that you are working on it. **Note** before starting any major new feature work, *please open an issue describing what you are planning to do* or talk to us on [discord](https://discord.gg/MA9v74M) or [Slack](https://join.slack.com/t/highfrequencybot/shared_invite/zt-k9o2v5ut-jX8Mc4CwNM8CDc2Dyg96YA). This will ensure that interested parties can give valuable feedback on the feature, and let others know that you are working on it.
**Important:** Always create your PR against the `develop` branch, not `stable`. **Important:** Always create your PR against the `develop` branch, not `stable`.
@ -187,7 +187,7 @@ To run this bot we recommend you a cloud instance with a minimum of:
### Software requirements ### Software requirements
- [Python 3.6.x](http://docs.python-guide.org/en/latest/starting/installation/) - [Python 3.7.x](http://docs.python-guide.org/en/latest/starting/installation/)
- [pip](https://pip.pypa.io/en/stable/installing/) - [pip](https://pip.pypa.io/en/stable/installing/)
- [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) - [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- [TA-Lib](https://mrjbq7.github.io/ta-lib/install.html) - [TA-Lib](https://mrjbq7.github.io/ta-lib/install.html)

View File

@ -75,6 +75,33 @@
"refresh_period": 1440 "refresh_period": 1440
} }
], ],
"protections": [
{
"method": "StoplossGuard",
"lookback_period_candles": 60,
"trade_limit": 4,
"stop_duration_candles": 60,
"only_per_pair": false
},
{
"method": "CooldownPeriod",
"stop_duration_candles": 20
},
{
"method": "MaxDrawdown",
"lookback_period_candles": 200,
"trade_limit": 20,
"stop_duration_candles": 10,
"max_allowed_drawdown": 0.2
},
{
"method": "LowProfitPairs",
"lookback_period_candles": 360,
"trade_limit": 1,
"stop_duration_candles": 2,
"required_profit": 0.02
}
],
"exchange": { "exchange": {
"name": "bittrex", "name": "bittrex",
"sandbox": false, "sandbox": false,

View File

@ -9,7 +9,7 @@ services:
# Build step - only needed when additional dependencies are needed # Build step - only needed when additional dependencies are needed
# build: # build:
# context: . # context: .
# dockerfile: "./Dockerfile.technical" # dockerfile: "./docker/Dockerfile.technical"
restart: unless-stopped restart: unless-stopped
container_name: freqtrade container_name: freqtrade
volumes: volumes:

View File

@ -77,7 +77,7 @@ Currently, the arguments are:
* `results`: DataFrame containing the result * `results`: DataFrame containing the result
The following columns are available in results (corresponds to the output-file of backtesting when used with `--export trades`): The following columns are available in results (corresponds to the output-file of backtesting when used with `--export trades`):
`pair, profit_percent, profit_abs, open_time, close_time, open_index, close_index, trade_duration, open_at_end, open_rate, close_rate, sell_reason` `pair, profit_percent, profit_abs, open_date, open_rate, open_fee, close_date, close_rate, close_fee, amount, trade_duration, open_at_end, sell_reason`
* `trade_count`: Amount of trades (identical to `len(results)`) * `trade_count`: Amount of trades (identical to `len(results)`)
* `min_date`: Start date of the hyperopting TimeFrame * `min_date`: Start date of the hyperopting TimeFrame
* `min_date`: End date of the hyperopting TimeFrame * `min_date`: End date of the hyperopting TimeFrame

View File

@ -165,10 +165,13 @@ A backtesting result will look like that:
| Max open trades | 3 | | Max open trades | 3 |
| | | | | |
| Total trades | 429 | | Total trades | 429 |
| First trade | 2019-01-01 18:30:00 |
| First trade Pair | EOS/USDT |
| Total Profit % | 152.41% | | Total Profit % | 152.41% |
| Trades per day | 3.575 | | Trades per day | 3.575 |
| | |
| Best Pair | LSK/BTC 26.26% |
| Worst Pair | ZEC/BTC -10.18% |
| Best Trade | LSK/BTC 4.25% |
| Worst Trade | ZEC/BTC -10.25% |
| Best day | 25.27% | | Best day | 25.27% |
| Worst day | -30.67% | | Worst day | -30.67% |
| Avg. Duration Winners | 4:23:00 | | Avg. Duration Winners | 4:23:00 |
@ -238,10 +241,13 @@ It contains some useful key metrics about performance of your strategy on backte
| Max open trades | 3 | | Max open trades | 3 |
| | | | | |
| Total trades | 429 | | Total trades | 429 |
| First trade | 2019-01-01 18:30:00 |
| First trade Pair | EOS/USDT |
| Total Profit % | 152.41% | | Total Profit % | 152.41% |
| Trades per day | 3.575 | | Trades per day | 3.575 |
| | |
| Best Pair | LSK/BTC 26.26% |
| Worst Pair | ZEC/BTC -10.18% |
| Best Trade | LSK/BTC 4.25% |
| Worst Trade | ZEC/BTC -10.25% |
| Best day | 25.27% | | Best day | 25.27% |
| Worst day | -30.67% | | Worst day | -30.67% |
| Avg. Duration Winners | 4:23:00 | | Avg. Duration Winners | 4:23:00 |
@ -258,10 +264,10 @@ It contains some useful key metrics about performance of your strategy on backte
- `Backtesting from` / `Backtesting to`: Backtesting range (usually defined with the `--timerange` option). - `Backtesting from` / `Backtesting to`: Backtesting range (usually defined with the `--timerange` option).
- `Max open trades`: Setting of `max_open_trades` (or `--max-open-trades`) - to clearly see settings for this. - `Max open trades`: Setting of `max_open_trades` (or `--max-open-trades`) - to clearly see settings for this.
- `Total trades`: Identical to the total trades of the backtest output table. - `Total trades`: Identical to the total trades of the backtest output table.
- `First trade`: First trade entered.
- `First trade pair`: Which pair was part of the first trade.
- `Total Profit %`: Total profit per stake amount. Aligned to the TOTAL column of the first table. - `Total Profit %`: Total profit per stake amount. Aligned to the TOTAL column of the first table.
- `Trades per day`: Total trades divided by the backtesting duration in days (this will give you information about how many trades to expect from the strategy). - `Trades per day`: Total trades divided by the backtesting duration in days (this will give you information about how many trades to expect from the strategy).
- `Best Pair` / `Worst Pair`: Best and worst performing pair, and it's corresponding `Cum Profit %`.
- `Best Trade` / `Worst Trade`: Biggest winning trade and biggest losing trade
- `Best day` / `Worst day`: Best and worst day based on daily profit. - `Best day` / `Worst day`: Best and worst day based on daily profit.
- `Avg. Duration Winners` / `Avg. Duration Loser`: Average durations for winning and losing trades. - `Avg. Duration Winners` / `Avg. Duration Loser`: Average durations for winning and losing trades.
- `Max Drawdown`: Maximum drawdown experienced. For example, the value of 50% means that from highest to subsequent lowest point, a 50% drop was experienced). - `Max Drawdown`: Maximum drawdown experienced. For example, the value of 50% means that from highest to subsequent lowest point, a 50% drop was experienced).
@ -273,18 +279,24 @@ It contains some useful key metrics about performance of your strategy on backte
Since backtesting lacks some detailed information about what happens within a candle, it needs to take a few assumptions: Since backtesting lacks some detailed information about what happens within a candle, it needs to take a few assumptions:
- Buys happen at open-price - Buys happen at open-price
- Sell signal sells happen at open-price of the following candle - Sell-signal sells happen at open-price of the consecutive candle
- Low happens before high for stoploss, protecting capital first - Sell-signal is favored over Stoploss, because sell-signals are assumed to trigger on candle's open
- ROI - ROI
- sells are compared to high - but the ROI value is used (e.g. ROI = 2%, high=5% - so the sell will be at 2%) - sells are compared to high - but the ROI value is used (e.g. ROI = 2%, high=5% - so the sell will be at 2%)
- sells are never "below the candle", so a ROI of 2% may result in a sell at 2.4% if low was at 2.4% profit - sells are never "below the candle", so a ROI of 2% may result in a sell at 2.4% if low was at 2.4% profit
- Forcesells caused by `<N>=-1` ROI entries use low as sell value, unless N falls on the candle open (e.g. `120: -1` for 1h candles) - Forcesells caused by `<N>=-1` ROI entries use low as sell value, unless N falls on the candle open (e.g. `120: -1` for 1h candles)
- Stoploss sells happen exactly at stoploss price, even if low was lower - Stoploss sells happen exactly at stoploss price, even if low was lower, but the loss will be `2 * fees` higher than the stoploss price
- Stoploss is evaluated before ROI within one candle. So you can often see more trades with the `stoploss` sell reason comparing to the results obtained with the same strategy in the Dry Run/Live Trade modes
- Low happens before high for stoploss, protecting capital first
- Trailing stoploss - Trailing stoploss
- High happens first - adjusting stoploss - High happens first - adjusting stoploss
- Low uses the adjusted stoploss (so sells with large high-low difference are backtested correctly) - Low uses the adjusted stoploss (so sells with large high-low difference are backtested correctly)
- ROI applies before trailing-stop, ensuring profits are "top-capped" at ROI if both ROI and trailing stop applies
- Sell-reason does not explain if a trade was positive or negative, just what triggered the sell (this can look odd if negative ROI values are used) - Sell-reason does not explain if a trade was positive or negative, just what triggered the sell (this can look odd if negative ROI values are used)
- Stoploss (and trailing stoploss) is evaluated before ROI within one candle. So you can often see more trades with the `stoploss` and/or `trailing_stop` sell reason comparing to the results obtained with the same strategy in the Dry Run/Live Trade modes. - Evaluation sequence (if multiple signals happen on the same candle)
- ROI (if not stoploss)
- Sell-signal
- Stoploss
Taking these assumptions, backtesting tries to mirror real trading as closely as possible. However, backtesting will **never** replace running a strategy in dry-run mode. Taking these assumptions, backtesting tries to mirror real trading as closely as possible. However, backtesting will **never** replace running a strategy in dry-run mode.
Also, keep in mind that past results don't guarantee future success. Also, keep in mind that past results don't guarantee future success.

View File

@ -213,9 +213,11 @@ Backtesting also uses the config specified via `-c/--config`.
usage: freqtrade backtesting [-h] [-v] [--logfile FILE] [-V] [-c PATH] usage: freqtrade backtesting [-h] [-v] [--logfile FILE] [-V] [-c PATH]
[-d PATH] [--userdir PATH] [-s NAME] [-d PATH] [--userdir PATH] [-s NAME]
[--strategy-path PATH] [-i TIMEFRAME] [--strategy-path PATH] [-i TIMEFRAME]
[--timerange TIMERANGE] [--max-open-trades INT] [--timerange TIMERANGE]
[--data-format-ohlcv {json,jsongz,hdf5}]
[--max-open-trades INT]
[--stake-amount STAKE_AMOUNT] [--fee FLOAT] [--stake-amount STAKE_AMOUNT] [--fee FLOAT]
[--eps] [--dmmp] [--eps] [--dmmp] [--enable-protections]
[--strategy-list STRATEGY_LIST [STRATEGY_LIST ...]] [--strategy-list STRATEGY_LIST [STRATEGY_LIST ...]]
[--export EXPORT] [--export-filename PATH] [--export EXPORT] [--export-filename PATH]
@ -226,6 +228,9 @@ optional arguments:
`1d`). `1d`).
--timerange TIMERANGE --timerange TIMERANGE
Specify what timerange of data to use. Specify what timerange of data to use.
--data-format-ohlcv {json,jsongz,hdf5}
Storage format for downloaded candle (OHLCV) data.
(default: `None`).
--max-open-trades INT --max-open-trades INT
Override the value of the `max_open_trades` Override the value of the `max_open_trades`
configuration setting. configuration setting.
@ -241,6 +246,10 @@ optional arguments:
Disable applying `max_open_trades` during backtest Disable applying `max_open_trades` during backtest
(same as setting `max_open_trades` to a very high (same as setting `max_open_trades` to a very high
number). number).
--enable-protections, --enableprotections
Enable protections for backtesting.Will slow
backtesting down by a considerable amount, but will
include configured protections
--strategy-list STRATEGY_LIST [STRATEGY_LIST ...] --strategy-list STRATEGY_LIST [STRATEGY_LIST ...]
Provide a space-separated list of strategies to Provide a space-separated list of strategies to
backtest. Please note that ticker-interval needs to be backtest. Please note that ticker-interval needs to be
@ -296,13 +305,14 @@ to find optimal parameter values for your strategy.
usage: freqtrade hyperopt [-h] [-v] [--logfile FILE] [-V] [-c PATH] [-d PATH] usage: freqtrade hyperopt [-h] [-v] [--logfile FILE] [-V] [-c PATH] [-d PATH]
[--userdir PATH] [-s NAME] [--strategy-path PATH] [--userdir PATH] [-s NAME] [--strategy-path PATH]
[-i TIMEFRAME] [--timerange TIMERANGE] [-i TIMEFRAME] [--timerange TIMERANGE]
[--data-format-ohlcv {json,jsongz,hdf5}]
[--max-open-trades INT] [--max-open-trades INT]
[--stake-amount STAKE_AMOUNT] [--fee FLOAT] [--stake-amount STAKE_AMOUNT] [--fee FLOAT]
[--hyperopt NAME] [--hyperopt-path PATH] [--eps] [--hyperopt NAME] [--hyperopt-path PATH] [--eps]
[-e INT] [--dmmp] [--enable-protections] [-e INT]
[--spaces {all,buy,sell,roi,stoploss,trailing,default} [{all,buy,sell,roi,stoploss,trailing,default} ...]] [--spaces {all,buy,sell,roi,stoploss,trailing,default} [{all,buy,sell,roi,stoploss,trailing,default} ...]]
[--dmmp] [--print-all] [--no-color] [--print-json] [--print-all] [--no-color] [--print-json] [-j JOBS]
[-j JOBS] [--random-state INT] [--min-trades INT] [--random-state INT] [--min-trades INT]
[--hyperopt-loss NAME] [--hyperopt-loss NAME]
optional arguments: optional arguments:
@ -312,6 +322,9 @@ optional arguments:
`1d`). `1d`).
--timerange TIMERANGE --timerange TIMERANGE
Specify what timerange of data to use. Specify what timerange of data to use.
--data-format-ohlcv {json,jsongz,hdf5}
Storage format for downloaded candle (OHLCV) data.
(default: `None`).
--max-open-trades INT --max-open-trades INT
Override the value of the `max_open_trades` Override the value of the `max_open_trades`
configuration setting. configuration setting.
@ -327,14 +340,18 @@ optional arguments:
--eps, --enable-position-stacking --eps, --enable-position-stacking
Allow buying the same pair multiple times (position Allow buying the same pair multiple times (position
stacking). stacking).
-e INT, --epochs INT Specify number of epochs (default: 100).
--spaces {all,buy,sell,roi,stoploss,trailing,default} [{all,buy,sell,roi,stoploss,trailing,default} ...]
Specify which parameters to hyperopt. Space-separated
list.
--dmmp, --disable-max-market-positions --dmmp, --disable-max-market-positions
Disable applying `max_open_trades` during backtest Disable applying `max_open_trades` during backtest
(same as setting `max_open_trades` to a very high (same as setting `max_open_trades` to a very high
number). number).
--enable-protections, --enableprotections
Enable protections for backtesting.Will slow
backtesting down by a considerable amount, but will
include configured protections
-e INT, --epochs INT Specify number of epochs (default: 100).
--spaces {all,buy,sell,roi,stoploss,trailing,default} [{all,buy,sell,roi,stoploss,trailing,default} ...]
Specify which parameters to hyperopt. Space-separated
list.
--print-all Print all results, not only the best ones. --print-all Print all results, not only the best ones.
--no-color Disable colorization of hyperopt results. May be --no-color Disable colorization of hyperopt results. May be
useful if you are redirecting output to a file. useful if you are redirecting output to a file.
@ -353,10 +370,10 @@ optional arguments:
class (IHyperOptLoss). Different functions can class (IHyperOptLoss). Different functions can
generate completely different results, since the generate completely different results, since the
target for optimization is different. Built-in target for optimization is different. Built-in
Hyperopt-loss-functions are: ShortTradeDurHyperOptLoss, Hyperopt-loss-functions are:
OnlyProfitHyperOptLoss, SharpeHyperOptLoss, ShortTradeDurHyperOptLoss, OnlyProfitHyperOptLoss,
SharpeHyperOptLossDaily, SortinoHyperOptLoss, SharpeHyperOptLoss, SharpeHyperOptLossDaily,
SortinoHyperOptLossDaily. SortinoHyperOptLoss, SortinoHyperOptLossDaily
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).

View File

@ -91,6 +91,7 @@ Mandatory parameters are marked as **Required**, which means that they are requi
| `edge.*` | Please refer to [edge configuration document](edge.md) for detailed explanation. | `edge.*` | Please refer to [edge configuration document](edge.md) for detailed explanation.
| `experimental.block_bad_exchanges` | Block exchanges known to not work with freqtrade. Leave on default unless you want to test if that exchange works now. <br>*Defaults to `true`.* <br> **Datatype:** Boolean | `experimental.block_bad_exchanges` | Block exchanges known to not work with freqtrade. Leave on default unless you want to test if that exchange works now. <br>*Defaults to `true`.* <br> **Datatype:** Boolean
| `pairlists` | Define one or more pairlists to be used. [More information below](#pairlists-and-pairlist-handlers). <br>*Defaults to `StaticPairList`.* <br> **Datatype:** List of Dicts | `pairlists` | Define one or more pairlists to be used. [More information below](#pairlists-and-pairlist-handlers). <br>*Defaults to `StaticPairList`.* <br> **Datatype:** List of Dicts
| `protections` | Define one or more protections to be used. [More information below](#protections). <br> **Datatype:** List of Dicts
| `telegram.enabled` | Enable the usage of Telegram. <br> **Datatype:** Boolean | `telegram.enabled` | Enable the usage of Telegram. <br> **Datatype:** Boolean
| `telegram.token` | Your Telegram bot token. Only required if `telegram.enabled` is `true`. <br>**Keep it in secret, do not disclose publicly.** <br> **Datatype:** String | `telegram.token` | Your Telegram bot token. Only required if `telegram.enabled` is `true`. <br>**Keep it in secret, do not disclose publicly.** <br> **Datatype:** String
| `telegram.chat_id` | Your personal Telegram account id. Only required if `telegram.enabled` is `true`. <br>**Keep it in secret, do not disclose publicly.** <br> **Datatype:** String | `telegram.chat_id` | Your personal Telegram account id. Only required if `telegram.enabled` is `true`. <br>**Keep it in secret, do not disclose publicly.** <br> **Datatype:** String
@ -575,6 +576,7 @@ Assuming both buy and sell are using market orders, a configuration similar to t
Obviously, if only one side is using limit orders, different pricing combinations can be used. Obviously, if only one side is using limit orders, different pricing combinations can be used.
--8<-- "includes/pairlists.md" --8<-- "includes/pairlists.md"
--8<-- "includes/protections.md"
## Switch to Dry-run mode ## Switch to Dry-run mode

View File

@ -8,7 +8,7 @@ If no additional parameter is specified, freqtrade will download data for `"1m"`
Exchange and pairs will come from `config.json` (if specified using `-c/--config`). Exchange and pairs will come from `config.json` (if specified using `-c/--config`).
Otherwise `--exchange` becomes mandatory. Otherwise `--exchange` becomes mandatory.
You can use a relative timerange (`--days 20`) or an absolute starting point (`--timerange 20200101`). For incremental downloads, the relative approach should be used. You can use a relative timerange (`--days 20`) or an absolute starting point (`--timerange 20200101-`). For incremental downloads, the relative approach should be used.
!!! Tip "Tip: Updating existing data" !!! Tip "Tip: Updating existing data"
If you already have backtesting data available in your data-directory and would like to refresh this data up to today, use `--days xx` with a number slightly higher than the missing number of days. Freqtrade will keep the available data and only download the missing data. If you already have backtesting data available in your data-directory and would like to refresh this data up to today, use `--days xx` with a number slightly higher than the missing number of days. Freqtrade will keep the available data and only download the missing data.

View File

@ -2,7 +2,7 @@
This page is intended for developers of Freqtrade, people who want to contribute to the Freqtrade codebase or documentation, or people who want to understand the source code of the application they're running. This page is intended for developers of Freqtrade, people who want to contribute to the Freqtrade codebase or documentation, or people who want to understand the source code of the application they're running.
All contributions, bug reports, bug fixes, documentation improvements, enhancements and ideas are welcome. We [track issues](https://github.com/freqtrade/freqtrade/issues) on [GitHub](https://github.com) and also have a dev channel on [discord](https://discord.gg/MA9v74M) or [slack](https://join.slack.com/t/highfrequencybot/shared_invite/zt-jaut7r4m-Y17k4x5mcQES9a9swKuxbg) where you can ask questions. All contributions, bug reports, bug fixes, documentation improvements, enhancements and ideas are welcome. We [track issues](https://github.com/freqtrade/freqtrade/issues) on [GitHub](https://github.com) and also have a dev channel on [discord](https://discord.gg/MA9v74M) or [slack](https://join.slack.com/t/highfrequencybot/shared_invite/zt-k9o2v5ut-jX8Mc4CwNM8CDc2Dyg96YA) where you can ask questions.
## Documentation ## Documentation
@ -94,7 +94,9 @@ Below is an outline of exception inheritance hierarchy:
+---+ StrategyError +---+ StrategyError
``` ```
## Modules ---
## Plugins
### Pairlists ### Pairlists
@ -119,6 +121,9 @@ The base-class provides an instance of the exchange (`self._exchange`) the pairl
self._pairlist_pos = pairlist_pos self._pairlist_pos = pairlist_pos
``` ```
!!! Tip
Don't forget to register your pairlist in `constants.py` under the variable `AVAILABLE_PAIRLISTS` - otherwise it will not be selectable.
Now, let's step through the methods which require actions: Now, let's step through the methods which require actions:
#### Pairlist configuration #### Pairlist configuration
@ -170,6 +175,66 @@ In `VolumePairList`, this implements different methods of sorting, does early va
return pairs return pairs
``` ```
### Protections
Best read the [Protection documentation](configuration.md#protections) to understand protections.
This Guide is directed towards Developers who want to develop a new protection.
No protection should use datetime directly, but use the provided `date_now` variable for date calculations. This preserves the ability to backtest protections.
!!! Tip "Writing a new Protection"
Best copy one of the existing Protections to have a good example.
Don't forget to register your protection in `constants.py` under the variable `AVAILABLE_PROTECTIONS` - otherwise it will not be selectable.
#### Implementation of a new protection
All Protection implementations must have `IProtection` as parent class.
For that reason, they must implement the following methods:
* `short_desc()`
* `global_stop()`
* `stop_per_pair()`.
`global_stop()` and `stop_per_pair()` must return a ProtectionReturn tuple, which consists of:
* lock pair - boolean
* lock until - datetime - until when should the pair be locked (will be rounded up to the next new candle)
* reason - string, used for logging and storage in the database
The `until` portion should be calculated using the provided `calculate_lock_end()` method.
All Protections should use `"stop_duration"` / `"stop_duration_candles"` to define how long a a pair (or all pairs) should be locked.
The content of this is made available as `self._stop_duration` to the each Protection.
If your protection requires a look-back period, please use `"lookback_period"` / `"lockback_period_candles"` to keep all protections aligned.
#### Global vs. local stops
Protections can have 2 different ways to stop trading for a limited :
* Per pair (local)
* For all Pairs (globally)
##### Protections - per pair
Protections that implement the per pair approach must set `has_local_stop=True`.
The method `stop_per_pair()` will be called whenever a trade closed (sell order completed).
##### Protections - global protection
These Protections should do their evaluation across all pairs, and consequently will also lock all pairs from trading (called a global PairLock).
Global protection must set `has_global_stop=True` to be evaluated for global stops.
The method `global_stop()` will be called whenever a trade closed (sell order completed).
##### Protections - calculating lock end time
Protections should calculate the lock end time based on the last trade it considers.
This avoids re-locking should the lookback-period be longer than the actual lock period.
The `IProtection` parent class provides a helper method for this in `calculate_lock_end()`.
---
## Implement a new Exchange (WIP) ## Implement a new Exchange (WIP)
!!! Note !!! Note
@ -177,6 +242,9 @@ In `VolumePairList`, this implements different methods of sorting, does early va
Most exchanges supported by CCXT should work out of the box. Most exchanges supported by CCXT should work out of the box.
To quickly test the public endpoints of an exchange, add a configuration for your exchange to `test_ccxt_compat.py` and run these tests with `pytest --longrun tests/exchange/test_ccxt_compat.py`.
Completing these tests successfully a good basis point (it's a requirement, actually), however these won't guarantee correct exchange functioning, as this only tests public endpoints, but no private endpoint (like generate order or similar).
### Stoploss On Exchange ### Stoploss On Exchange
Check if the new exchange supports Stoploss on Exchange orders through their API. Check if the new exchange supports Stoploss on Exchange orders through their API.

View File

@ -23,8 +23,8 @@ The Edge Positioning module seeks to improve a strategy's winning probability an
We raise the following question[^1]: We raise the following question[^1]:
!!! Question "Which trade is a better option?" !!! Question "Which trade is a better option?"
a) A trade with 80% of chance of losing $100 and 20% chance of winning $200<br/> a) A trade with 80% of chance of losing 100\$ and 20% chance of winning 200\$<br/>
b) A trade with 100% of chance of losing $30 b) A trade with 100% of chance of losing 30\$
???+ Info "Answer" ???+ Info "Answer"
The expected value of *a)* is smaller than the expected value of *b)*.<br/> The expected value of *a)* is smaller than the expected value of *b)*.<br/>
@ -34,8 +34,8 @@ We raise the following question[^1]:
Another way to look at it is to ask a similar question: Another way to look at it is to ask a similar question:
!!! Question "Which trade is a better option?" !!! Question "Which trade is a better option?"
a) A trade with 80% of chance of winning 100 and 20% chance of losing $200<br/> a) A trade with 80% of chance of winning 100\$ and 20% chance of losing 200\$<br/>
b) A trade with 100% of chance of winning $30 b) A trade with 100% of chance of winning 30\$
Edge positioning tries to answer the hard questions about risk/reward and position size automatically, seeking to minimizes the chances of losing of a given strategy. Edge positioning tries to answer the hard questions about risk/reward and position size automatically, seeking to minimizes the chances of losing of a given strategy.
@ -82,7 +82,7 @@ Risk Reward Ratio ($R$) is a formula used to measure the expected gains of a giv
$$ R = \frac{\text{potential_profit}}{\text{potential_loss}} $$ $$ R = \frac{\text{potential_profit}}{\text{potential_loss}} $$
???+ Example "Worked example of $R$ calculation" ???+ Example "Worked example of $R$ calculation"
Let's say that you think that the price of *stonecoin* today is $10.0. You believe that, because they will start mining stonecoin, it will go up to $15.0 tomorrow. There is the risk that the stone is too hard, and the GPUs can't mine it, so the price might go to $0 tomorrow. You are planning to invest $100, which will give you 10 shares (100 / 10). Let's say that you think that the price of *stonecoin* today is 10.0\$. You believe that, because they will start mining stonecoin, it will go up to 15.0\$ tomorrow. There is the risk that the stone is too hard, and the GPUs can't mine it, so the price might go to 0\$ tomorrow. You are planning to invest 100\$, which will give you 10 shares (100 / 10).
Your potential profit is calculated as: Your potential profit is calculated as:
@ -92,9 +92,9 @@ $$ R = \frac{\text{potential_profit}}{\text{potential_loss}} $$
&= 50 &= 50
\end{aligned}$ \end{aligned}$
Since the price might go to $0, the $100 dollars invested could turn into 0. Since the price might go to 0\$, the 100\$ dollars invested could turn into 0.
We do however use a stoploss of 15% - so in the worst case, we'll sell 15% below entry price (or at 8.5$). We do however use a stoploss of 15% - so in the worst case, we'll sell 15% below entry price (or at 8.5$\).
$\begin{aligned} $\begin{aligned}
\text{potential_loss} &= (\text{entry_price} - \text{stoploss}) * \frac{\text{investment}}{\text{entry_price}} \\ \text{potential_loss} &= (\text{entry_price} - \text{stoploss}) * \frac{\text{investment}}{\text{entry_price}} \\
@ -109,7 +109,7 @@ $$ R = \frac{\text{potential_profit}}{\text{potential_loss}} $$
&= \frac{50}{15}\\ &= \frac{50}{15}\\
&= 3.33 &= 3.33
\end{aligned}$<br> \end{aligned}$<br>
What it effectively means is that the strategy have the potential to make 3.33$ for each $1 invested. What it effectively means is that the strategy have the potential to make 3.33\$ for each 1\$ invested.
On a long horizon, that is, on many trades, we can calculate the risk reward by dividing the strategy' average profit on winning trades by the strategy' average loss on losing trades. We can calculate the average profit, $\mu_{win}$, as follows: On a long horizon, that is, on many trades, we can calculate the risk reward by dividing the strategy' average profit on winning trades by the strategy' average loss on losing trades. We can calculate the average profit, $\mu_{win}$, as follows:
@ -141,7 +141,7 @@ $$E = R * W - L$$
$E = R * W - L = 5 * 0.28 - 0.72 = 0.68$ $E = R * W - L = 5 * 0.28 - 0.72 = 0.68$
<br> <br>
The expectancy worked out in the example above means that, on average, this strategy' trades will return 1.68 times the size of its losses. Said another way, the strategy makes $1.68 for every $1 it loses, on average. The expectancy worked out in the example above means that, on average, this strategy' trades will return 1.68 times the size of its losses. Said another way, the strategy makes 1.68\$ for every 1\$ it loses, on average.
This is important for two reasons: First, it may seem obvious, but you know right away that you have a positive return. Second, you now have a number you can compare to other candidate systems to make decisions about which ones you employ. This is important for two reasons: First, it may seem obvious, but you know right away that you have a positive return. Second, you now have a number you can compare to other candidate systems to make decisions about which ones you employ.
@ -222,7 +222,7 @@ Edge module has following configuration options:
| `stoploss_range_max` | Maximum stoploss. <br>*Defaults to `-0.10`.* <br> **Datatype:** Float | `stoploss_range_max` | Maximum stoploss. <br>*Defaults to `-0.10`.* <br> **Datatype:** Float
| `stoploss_range_step` | As an example if this is set to -0.01 then Edge will test the strategy for `[-0.01, -0,02, -0,03 ..., -0.09, -0.10]` ranges. <br> **Note** than having a smaller step means having a bigger range which could lead to slow calculation. <br> If you set this parameter to -0.001, you then slow down the Edge calculation by a factor of 10. <br>*Defaults to `-0.001`.* <br> **Datatype:** Float | `stoploss_range_step` | As an example if this is set to -0.01 then Edge will test the strategy for `[-0.01, -0,02, -0,03 ..., -0.09, -0.10]` ranges. <br> **Note** than having a smaller step means having a bigger range which could lead to slow calculation. <br> If you set this parameter to -0.001, you then slow down the Edge calculation by a factor of 10. <br>*Defaults to `-0.001`.* <br> **Datatype:** Float
| `minimum_winrate` | It filters out pairs which don't have at least minimum_winrate. <br>This comes handy if you want to be conservative and don't comprise win rate in favour of risk reward ratio. <br>*Defaults to `0.60`.* <br> **Datatype:** Float | `minimum_winrate` | It filters out pairs which don't have at least minimum_winrate. <br>This comes handy if you want to be conservative and don't comprise win rate in favour of risk reward ratio. <br>*Defaults to `0.60`.* <br> **Datatype:** Float
| `minimum_expectancy` | It filters out pairs which have the expectancy lower than this number. <br>Having an expectancy of 0.20 means if you put 10$ on a trade you expect a 12$ return. <br>*Defaults to `0.20`.* <br> **Datatype:** Float | `minimum_expectancy` | It filters out pairs which have the expectancy lower than this number. <br>Having an expectancy of 0.20 means if you put 10\$ on a trade you expect a 12\$ return. <br>*Defaults to `0.20`.* <br> **Datatype:** Float
| `min_trade_number` | When calculating *W*, *R* and *E* (expectancy) against historical data, you always want to have a minimum number of trades. The more this number is the more Edge is reliable. <br>Having a win rate of 100% on a single trade doesn't mean anything at all. But having a win rate of 70% over past 100 trades means clearly something. <br>*Defaults to `10` (it is highly recommended not to decrease this number).* <br> **Datatype:** Integer | `min_trade_number` | When calculating *W*, *R* and *E* (expectancy) against historical data, you always want to have a minimum number of trades. The more this number is the more Edge is reliable. <br>Having a win rate of 100% on a single trade doesn't mean anything at all. But having a win rate of 70% over past 100 trades means clearly something. <br>*Defaults to `10` (it is highly recommended not to decrease this number).* <br> **Datatype:** Integer
| `max_trade_duration_minute` | Edge will filter out trades with long duration. If a trade is profitable after 1 month, it is hard to evaluate the strategy based on it. But if most of trades are profitable and they have maximum duration of 30 minutes, then it is clearly a good sign.<br>**NOTICE:** While configuring this value, you should take into consideration your timeframe. As an example filtering out trades having duration less than one day for a strategy which has 4h interval does not make sense. Default value is set assuming your strategy interval is relatively small (1m or 5m, etc.).<br>*Defaults to `1440` (one day).* <br> **Datatype:** Integer | `max_trade_duration_minute` | Edge will filter out trades with long duration. If a trade is profitable after 1 month, it is hard to evaluate the strategy based on it. But if most of trades are profitable and they have maximum duration of 30 minutes, then it is clearly a good sign.<br>**NOTICE:** While configuring this value, you should take into consideration your timeframe. As an example filtering out trades having duration less than one day for a strategy which has 4h interval does not make sense. Default value is set assuming your strategy interval is relatively small (1m or 5m, etc.).<br>*Defaults to `1440` (one day).* <br> **Datatype:** Integer
| `remove_pumps` | Edge will remove sudden pumps in a given market while going through historical data. However, given that pumps happen very often in crypto markets, we recommend you keep this off.<br>*Defaults to `false`.* <br> **Datatype:** Boolean | `remove_pumps` | Edge will remove sudden pumps in a given market while going through historical data. However, given that pumps happen very often in crypto markets, we recommend you keep this off.<br>*Defaults to `false`.* <br> **Datatype:** Boolean

View File

@ -2,30 +2,30 @@
## Beginner Tips & Tricks ## Beginner Tips & Tricks
* When you work with your strategy & hyperopt file you should use a proper code editor like vscode or Pycharm. A good code editor will provide syntax highlighting as well as line numbers, making it easy to find syntax errors (most likely, pointed out by Freqtrade during startup). * When you work with your strategy & hyperopt file you should use a proper code editor like VSCode or PyCharm. A good code editor will provide syntax highlighting as well as line numbers, making it easy to find syntax errors (most likely pointed out by Freqtrade during startup).
## Freqtrade common issues ## Freqtrade common issues
### The bot does not start ### The bot does not start
Running the bot with `freqtrade trade --config config.json` does show the output `freqtrade: command not found`. Running the bot with `freqtrade trade --config config.json` shows the output `freqtrade: command not found`.
This could have the following reasons: This could be caused by the following reasons:
* The virtual environment is not active * The virtual environment is not active.
* run `source .env/bin/activate` to activate the virtual environment * Run `source .env/bin/activate` to activate the virtual environment.
* The installation did not work correctly. * The installation did not work correctly.
* Please check the [Installation documentation](installation.md). * Please check the [Installation documentation](installation.md).
### I have waited 5 minutes, why hasn't the bot made any trades yet?! ### I have waited 5 minutes, why hasn't the bot made any trades yet?
* Depending on the buy strategy, the amount of whitelisted coins, the * Depending on the buy strategy, the amount of whitelisted coins, the
situation of the market etc, it can take up to hours to find good entry situation of the market etc, it can take up to hours to find a good entry
position for a trade. Be patient! position for a trade. Be patient!
* Or it may because of a configuration error? Best check the logs, it's usually telling you if the bot is simply not getting buy signals (only heartbeat messages), or if there is something wrong (errors / exceptions in the log). * It may be because of a configuration error. It's best to check the logs, they usually tell you if the bot is simply not getting buy signals (only heartbeat messages), or if there is something wrong (errors / exceptions in the log).
### I have made 12 trades already, why is my total profit negative?! ### I have made 12 trades already, why is my total profit negative?
I understand your disappointment but unfortunately 12 trades is just I understand your disappointment but unfortunately 12 trades is just
not enough to say anything. If you run backtesting, you can see that our not enough to say anything. If you run backtesting, you can see that our
@ -36,11 +36,9 @@ of course constantly aim to improve the bot but it will _always_ be a
gamble, which should leave you with modest wins on monthly basis but gamble, which should leave you with modest wins on monthly basis but
you can't say much from few trades. you can't say much from few trades.
### Id like to change the stake amount. Can I just stop the bot with /stop and then change the config.json and run it again? ### Id like to make changes to the config. Can I do that without having to kill the bot?
Not quite. Trades are persisted to a database but the configuration is Yes. You can edit your config, use the `/stop` command in Telegram, followed by `/reload_config` and the bot will run with the new config.
currently only read when the bot is killed and restarted. `/stop` more
like pauses. You can stop your bot, adjust settings and start it again.
### I want to improve the bot with a new strategy ### I want to improve the bot with a new strategy
@ -49,7 +47,7 @@ the tutorial [here|Testing-new-strategies-with-Hyperopt](bot-usage.md#hyperopt-c
### Is there a setting to only SELL the coins being held and not perform anymore BUYS? ### Is there a setting to only SELL the coins being held and not perform anymore BUYS?
You can use the `/forcesell all` command from Telegram. You can use the `/stopbuy` command in Telegram to prevent future buys, followed by `/forcesell all` (sell all open trades).
### I want to run multiple bots on the same machine ### I want to run multiple bots on the same machine
@ -59,7 +57,7 @@ Please look at the [advanced setup documentation Page](advanced-setup.md#running
This message is just a warning that the latest candles had missing candles in them. This message is just a warning that the latest candles had missing candles in them.
Depending on the exchange, this can indicate that the pair didn't have a trade for the timeframe you are using - and the exchange does only return candles with volume. Depending on the exchange, this can indicate that the pair didn't have a trade for the timeframe you are using - and the exchange does only return candles with volume.
On low volume pairs, this is a rather common occurance. On low volume pairs, this is a rather common occurrence.
If this happens for all pairs in the pairlist, this might indicate a recent exchange downtime. Please check your exchange's public channels for details. If this happens for all pairs in the pairlist, this might indicate a recent exchange downtime. Please check your exchange's public channels for details.
@ -73,7 +71,7 @@ Read [the Bittrex section about restricted markets](exchanges.md#restricted-mark
### I'm getting the "Exchange Bittrex does not support market orders." message and cannot run my strategy ### I'm getting the "Exchange Bittrex does not support market orders." message and cannot run my strategy
As the message says, Bittrex does not support market orders and you have one of the [order types](configuration.md/#understand-order_types) set to "market". Probably your strategy was written with other exchanges in mind and sets "market" orders for "stoploss" orders, which is correct and preferable for most of the exchanges supporting market orders (but not for Bittrex). As the message says, Bittrex does not support market orders and you have one of the [order types](configuration.md/#understand-order_types) set to "market". Your strategy was probably written with other exchanges in mind and sets "market" orders for "stoploss" orders, which is correct and preferable for most of the exchanges supporting market orders (but not for Bittrex).
To fix it for Bittrex, redefine order types in the strategy to use "limit" instead of "market": To fix it for Bittrex, redefine order types in the strategy to use "limit" instead of "market":
@ -85,7 +83,7 @@ To fix it for Bittrex, redefine order types in the strategy to use "limit" inste
} }
``` ```
Same fix should be done in the configuration file, if order types are defined in your custom config rather than in the strategy. The same fix should be applied in the configuration file, if order types are defined in your custom config rather than in the strategy.
### How do I search the bot logs for something? ### How do I search the bot logs for something?
@ -127,10 +125,10 @@ On Windows, the `--logfile` option is also supported by Freqtrade and you can us
## Hyperopt module ## Hyperopt module
### How many epoch do I need to get a good Hyperopt result? ### How many epochs do I need to get a good Hyperopt result?
Per default Hyperopt called without the `-e`/`--epochs` command line option will only Per default Hyperopt called without the `-e`/`--epochs` command line option will only
run 100 epochs, means 100 evals of your triggers, guards, ... Too few run 100 epochs, means 100 evaluations of your triggers, guards, ... Too few
to find a great result (unless if you are very lucky), so you probably to find a great result (unless if you are very lucky), so you probably
have to run it for 10.000 or more. But it will take an eternity to have to run it for 10.000 or more. But it will take an eternity to
compute. compute.
@ -140,25 +138,25 @@ Since hyperopt uses Bayesian search, running for too many epochs may not produce
It's therefore recommended to run between 500-1000 epochs over and over until you hit at least 10.000 epochs in total (or are satisfied with the result). You can best judge by looking at the results - if the bot keeps discovering better strategies, it's best to keep on going. It's therefore recommended to run between 500-1000 epochs over and over until you hit at least 10.000 epochs in total (or are satisfied with the result). You can best judge by looking at the results - if the bot keeps discovering better strategies, it's best to keep on going.
```bash ```bash
freqtrade hyperopt --hyperop SampleHyperopt --hyperopt-loss SharpeHyperOptLossDaily --strategy SampleStrategy -e 1000 freqtrade hyperopt --hyperopt SampleHyperopt --hyperopt-loss SharpeHyperOptLossDaily --strategy SampleStrategy -e 1000
``` ```
### Why does it take a long time to run hyperopt? ### Why does it take a long time to run hyperopt?
* Discovering a great strategy with Hyperopt takes time. Study www.freqtrade.io, the Freqtrade Documentation page, join the Freqtrade [Slack community](https://join.slack.com/t/highfrequencybot/shared_invite/zt-jaut7r4m-Y17k4x5mcQES9a9swKuxbg) - or the Freqtrade [discord community](https://discord.gg/X89cVG). While you patiently wait for the most advanced, free crypto bot in the world, to hand you a possible golden strategy specially designed just for you. * Discovering a great strategy with Hyperopt takes time. Study www.freqtrade.io, the Freqtrade Documentation page, join the Freqtrade [Slack community](https://join.slack.com/t/highfrequencybot/shared_invite/zt-k9o2v5ut-jX8Mc4CwNM8CDc2Dyg96YA) - or the Freqtrade [discord community](https://discord.gg/X89cVG). While you patiently wait for the most advanced, free crypto bot in the world, to hand you a possible golden strategy specially designed just for you.
* If you wonder why it can take from 20 minutes to days to do 1000 epochs here are some answers: * If you wonder why it can take from 20 minutes to days to do 1000 epochs here are some answers:
This answer was written during the release 0.15.1, when we had: This answer was written during the release 0.15.1, when we had:
- 8 triggers * 8 triggers
- 9 guards: let's say we evaluate even 10 values from each * 9 guards: let's say we evaluate even 10 values from each
- 1 stoploss calculation: let's say we want 10 values from that too to be evaluated * 1 stoploss calculation: let's say we want 10 values from that too to be evaluated
The following calculation is still very rough and not very precise The following calculation is still very rough and not very precise
but it will give the idea. With only these triggers and guards there is but it will give the idea. With only these triggers and guards there is
already 8\*10^9\*10 evaluations. A roughly total of 80 billion evals. already 8\*10^9\*10 evaluations. A roughly total of 80 billion evaluations.
Did you run 100 000 evals? Congrats, you've done roughly 1 / 100 000 th Did you run 100 000 evaluations? Congrats, you've done roughly 1 / 100 000 th
of the search space, assuming that the bot never tests the same parameters more than once. of the search space, assuming that the bot never tests the same parameters more than once.
* The time it takes to run 1000 hyperopt epochs depends on things like: The available cpu, hard-disk, ram, timeframe, timerange, indicator settings, indicator count, amount of coins that hyperopt test strategies on and the resulting trade count - which can be 650 trades in a year or 10.0000 trades depending if the strategy aims for big profits by trading rarely or for many low profit trades. * The time it takes to run 1000 hyperopt epochs depends on things like: The available cpu, hard-disk, ram, timeframe, timerange, indicator settings, indicator count, amount of coins that hyperopt test strategies on and the resulting trade count - which can be 650 trades in a year or 10.0000 trades depending if the strategy aims for big profits by trading rarely or for many low profit trades.

View File

@ -64,9 +64,9 @@ Depending on the space you want to optimize, only some of the below are required
Optional in hyperopt - can also be loaded from a strategy (recommended): Optional in hyperopt - can also be loaded from a strategy (recommended):
* copy `populate_indicators` from your strategy - otherwise default-strategy will be used * `populate_indicators` - fallback to create indicators
* copy `populate_buy_trend` from your strategy - otherwise default-strategy will be used * `populate_buy_trend` - fallback if not optimizing for buy space. should come from strategy
* copy `populate_sell_trend` from your strategy - otherwise default-strategy will be used * `populate_sell_trend` - fallback if not optimizing for sell space. should come from strategy
!!! Note !!! Note
You always have to provide a strategy to Hyperopt, even if your custom Hyperopt class contains all methods. You always have to provide a strategy to Hyperopt, even if your custom Hyperopt class contains all methods.
@ -104,7 +104,7 @@ This command will create a new hyperopt file from a template, allowing you to ge
There are two places you need to change in your hyperopt file to add a new buy hyperopt for testing: There are two places you need to change in your hyperopt file to add a new buy hyperopt for testing:
* Inside `indicator_space()` - the parameters hyperopt shall be optimizing. * Inside `indicator_space()` - the parameters hyperopt shall be optimizing.
* Inside `populate_buy_trend()` - applying the parameters. * Within `buy_strategy_generator()` - populate the nested `populate_buy_trend()` to apply the parameters.
There you have two different types of indicators: 1. `guards` and 2. `triggers`. There you have two different types of indicators: 1. `guards` and 2. `triggers`.
@ -128,7 +128,7 @@ Similar to the buy-signal above, sell-signals can also be optimized.
Place the corresponding settings into the following methods Place the corresponding settings into the following methods
* Inside `sell_indicator_space()` - the parameters hyperopt shall be optimizing. * Inside `sell_indicator_space()` - the parameters hyperopt shall be optimizing.
* Inside `populate_sell_trend()` - applying the parameters. * Within `sell_strategy_generator()` - populate the nested method `populate_sell_trend()` to apply the parameters.
The configuration and rules are the same than for buy signals. The configuration and rules are the same than for buy signals.
To avoid naming collisions in the search-space, please prefix all sell-spaces with `sell-`. To avoid naming collisions in the search-space, please prefix all sell-spaces with `sell-`.
@ -173,6 +173,11 @@ one we call `trigger` and use it to decide which buy trigger we want to use.
So let's write the buy strategy using these values: So let's write the buy strategy using these values:
```python ```python
@staticmethod
def buy_strategy_generator(params: Dict[str, Any]) -> Callable:
"""
Define the buy strategy parameters to be used by Hyperopt.
"""
def populate_buy_trend(dataframe: DataFrame) -> DataFrame: def populate_buy_trend(dataframe: DataFrame) -> DataFrame:
conditions = [] conditions = []
# GUARDS AND TRENDS # GUARDS AND TRENDS

View File

@ -15,6 +15,7 @@ Inactive markets are always removed from the resulting pairlist. Explicitly blac
* [`StaticPairList`](#static-pair-list) (default, if not configured differently) * [`StaticPairList`](#static-pair-list) (default, if not configured differently)
* [`VolumePairList`](#volume-pair-list) * [`VolumePairList`](#volume-pair-list)
* [`AgeFilter`](#agefilter) * [`AgeFilter`](#agefilter)
* [`PerformanceFilter`](#performancefilter)
* [`PrecisionFilter`](#precisionfilter) * [`PrecisionFilter`](#precisionfilter)
* [`PriceFilter`](#pricefilter) * [`PriceFilter`](#pricefilter)
* [`ShuffleFilter`](#shufflefilter) * [`ShuffleFilter`](#shufflefilter)
@ -64,6 +65,9 @@ The `refresh_period` setting allows to define the period (in seconds), at which
}], }],
``` ```
!!! Note
`VolumePairList` does not support backtesting mode.
#### AgeFilter #### AgeFilter
Removes pairs that have been listed on the exchange for less than `min_days_listed` days (defaults to `10`). Removes pairs that have been listed on the exchange for less than `min_days_listed` days (defaults to `10`).
@ -74,6 +78,18 @@ be caught out buying before the pair has finished dropping in price.
This filter allows freqtrade to ignore pairs until they have been listed for at least `min_days_listed` days. This filter allows freqtrade to ignore pairs until they have been listed for at least `min_days_listed` days.
#### PerformanceFilter
Sorts pairs by past trade performance, as follows:
1. Positive performance.
2. No closed trades yet.
3. Negative performance.
Trade count is used as a tie breaker.
!!! Note
`PerformanceFilter` does not support backtesting mode.
#### PrecisionFilter #### PrecisionFilter
Filters low-value coins which would not allow setting stoplosses. Filters low-value coins which would not allow setting stoplosses.

View File

@ -0,0 +1,169 @@
## Protections
!!! Warning "Beta feature"
This feature is still in it's testing phase. Should you notice something you think is wrong please let us know via Discord, Slack or via Github Issue.
Protections will protect your strategy from unexpected events and market conditions by temporarily stop trading for either one pair, or for all pairs.
All protection end times are rounded up to the next candle to avoid sudden, unexpected intra-candle buys.
!!! Note
Not all Protections will work for all strategies, and parameters will need to be tuned for your strategy to improve performance.
!!! Tip
Each Protection can be configured multiple times with different parameters, to allow different levels of protection (short-term / long-term).
!!! Note "Backtesting"
Protections are supported by backtesting and hyperopt, but must be explicitly enabled by using the `--enable-protections` flag.
### Available Protections
* [`StoplossGuard`](#stoploss-guard) Stop trading if a certain amount of stoploss occurred within a certain time window.
* [`MaxDrawdown`](#maxdrawdown) Stop trading if max-drawdown is reached.
* [`LowProfitPairs`](#low-profit-pairs) Lock pairs with low profits
* [`CooldownPeriod`](#cooldown-period) Don't enter a trade right after selling a trade.
### Common settings to all Protections
| Parameter| Description |
|------------|-------------|
| `method` | Protection name to use. <br> **Datatype:** String, selected from [available Protections](#available-protections)
| `stop_duration_candles` | For how many candles should the lock be set? <br> **Datatype:** Positive integer (in candles)
| `stop_duration` | how many minutes should protections be locked. <br>Cannot be used together with `stop_duration_candles`. <br> **Datatype:** Float (in minutes)
| `lookback_period_candles` | Only trades that completed within the last `lookback_period_candles` candles will be considered. This setting may be ignored by some Protections. <br> **Datatype:** Positive integer (in candles).
| `lookback_period` | Only trades that completed after `current_time - lookback_period` will be considered. <br>Cannot be used together with `lookback_period_candles`. <br>This setting may be ignored by some Protections. <br> **Datatype:** Float (in minutes)
| `trade_limit` | Number of trades required at minimum (not used by all Protections). <br> **Datatype:** Positive integer
!!! Note "Durations"
Durations (`stop_duration*` and `lookback_period*` can be defined in either minutes or candles).
For more flexibility when testing different timeframes, all below examples will use the "candle" definition.
#### Stoploss Guard
`StoplossGuard` selects all trades within `lookback_period`, and determines if the amount of trades that resulted in stoploss are above `trade_limit` - in which case trading will stop for `stop_duration`.
This applies across all pairs, unless `only_per_pair` is set to true, which will then only look at one pair at a time.
The below example stops trading for all pairs for 4 candles after the last trade if the bot hit stoploss 4 times within the last 24 candles.
```json
"protections": [
{
"method": "StoplossGuard",
"lookback_period_candles": 24,
"trade_limit": 4,
"stop_duration_candles": 4,
"only_per_pair": false
}
],
```
!!! Note
`StoplossGuard` considers all trades with the results `"stop_loss"` and `"trailing_stop_loss"` if the resulting profit was negative.
`trade_limit` and `lookback_period` will need to be tuned for your strategy.
#### MaxDrawdown
`MaxDrawdown` uses all trades within `lookback_period` (in minutes) to determine the maximum drawdown. If the drawdown is below `max_allowed_drawdown`, trading will stop for `stop_duration` (in minutes) after the last trade - assuming that the bot needs some time to let markets recover.
The below sample stops trading for 12 candles if max-drawdown is > 20% considering all trades within the last 48 candles.
```json
"protections": [
{
"method": "MaxDrawdown",
"lookback_period_candles": 48,
"trade_limit": 20,
"stop_duration_candles": 12,
"max_allowed_drawdown": 0.2
},
],
```
#### Low Profit Pairs
`LowProfitPairs` uses all trades for a pair within `lookback_period` (in minutes) to determine the overall profit ratio.
If that ratio is below `required_profit`, that pair will be locked for `stop_duration` (in minutes).
The below example will stop trading a pair for 60 minutes if the pair does not have a required profit of 2% (and a minimum of 2 trades) within the last 6 candles.
```json
"protections": [
{
"method": "LowProfitPairs",
"lookback_period_candles": 6,
"trade_limit": 2,
"stop_duration": 60,
"required_profit": 0.02
}
],
```
#### Cooldown Period
`CooldownPeriod` locks a pair for `stop_duration` (in minutes) after selling, avoiding a re-entry for this pair for `stop_duration` minutes.
The below example will stop trading a pair for 2 candles after closing a trade, allowing this pair to "cool down".
```json
"protections": [
{
"method": "CooldownPeriod",
"stop_duration_candles": 2
}
],
```
!!! Note
This Protection applies only at pair-level, and will never lock all pairs globally.
This Protection does not consider `lookback_period` as it only looks at the latest trade.
### Full example of Protections
All protections can be combined at will, also with different parameters, creating a increasing wall for under-performing pairs.
All protections are evaluated in the sequence they are defined.
The below example assumes a timeframe of 1 hour:
* Locks each pair after selling for an additional 5 candles (`CooldownPeriod`), giving other pairs a chance to get filled.
* Stops trading for 4 hours (`4 * 1h candles`) if the last 2 days (`48 * 1h candles`) had 20 trades, which caused a max-drawdown of more than 20%. (`MaxDrawdown`).
* Stops trading if more than 4 stoploss occur for all pairs within a 1 day (`24 * 1h candles`) limit (`StoplossGuard`).
* Locks all pairs that had 4 Trades within the last 6 hours (`6 * 1h candles`) with a combined profit ratio of below 0.02 (<2%) (`LowProfitPairs`).
* Locks all pairs for 2 candles that had a profit of below 0.01 (<1%) within the last 24h (`24 * 1h candles`), a minimum of 4 trades.
```json
"timeframe": "1h",
"protections": [
{
"method": "CooldownPeriod",
"stop_duration_candles": 5
},
{
"method": "MaxDrawdown",
"lookback_period_candles": 48,
"trade_limit": 20,
"stop_duration_candles": 4,
"max_allowed_drawdown": 0.2
},
{
"method": "StoplossGuard",
"lookback_period_candles": 24,
"trade_limit": 4,
"stop_duration_candles": 2,
"only_per_pair": false
},
{
"method": "LowProfitPairs",
"lookback_period_candles": 6,
"trade_limit": 2,
"stop_duration_candles": 60,
"required_profit": 0.02
},
{
"method": "LowProfitPairs",
"lookback_period_candles": 24,
"trade_limit": 4,
"stop_duration_candles": 2,
"required_profit": 0.01
}
],
```

View File

@ -14,7 +14,7 @@
## Introduction ## Introduction
Freqtrade is a crypto-currency algorithmic trading software developed in python (3.6+) and supported on Windows, macOS and Linux. Freqtrade is a crypto-currency algorithmic trading software developed in python (3.7+) and supported on Windows, macOS and Linux.
!!! Danger "DISCLAIMER" !!! Danger "DISCLAIMER"
This software is for educational purposes only. Do not risk money which you are afraid to lose. USE THE SOFTWARE AT YOUR OWN RISK. THE AUTHORS AND ALL AFFILIATES ASSUME NO RESPONSIBILITY FOR YOUR TRADING RESULTS. This software is for educational purposes only. Do not risk money which you are afraid to lose. USE THE SOFTWARE AT YOUR OWN RISK. THE AUTHORS AND ALL AFFILIATES ASSUME NO RESPONSIBILITY FOR YOUR TRADING RESULTS.
@ -51,7 +51,7 @@ To run this bot we recommend you a linux cloud instance with a minimum of:
Alternatively Alternatively
- Python 3.6.x - Python 3.7+
- pip (pip3) - pip (pip3)
- git - git
- TA-Lib - TA-Lib
@ -65,7 +65,7 @@ For any questions not covered by the documentation or for further information ab
Please check out our [discord server](https://discord.gg/MA9v74M). Please check out our [discord server](https://discord.gg/MA9v74M).
You can also join our [Slack channel](https://join.slack.com/t/highfrequencybot/shared_invite/zt-jaut7r4m-Y17k4x5mcQES9a9swKuxbg). You can also join our [Slack channel](https://join.slack.com/t/highfrequencybot/shared_invite/zt-k9o2v5ut-jX8Mc4CwNM8CDc2Dyg96YA).
## Ready to try? ## Ready to try?

View File

@ -10,7 +10,7 @@ Please consider using the prebuilt [docker images](docker.md) to get started qui
Click each one for install guide: Click each one for install guide:
* [Python >= 3.6.x](http://docs.python-guide.org/en/latest/starting/installation/) * [Python >= 3.7.x](http://docs.python-guide.org/en/latest/starting/installation/)
* [pip](https://pip.pypa.io/en/stable/installing/) * [pip](https://pip.pypa.io/en/stable/installing/)
* [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [virtualenv](https://virtualenv.pypa.io/en/stable/installation.html) (Recommended) * [virtualenv](https://virtualenv.pypa.io/en/stable/installation.html) (Recommended)
@ -34,7 +34,7 @@ The easiest way to install and run Freqtrade is to clone the bot Github reposito
When cloning the repository the default working branch has the name `develop`. This branch contains all last features (can be considered as relatively stable, thanks to automated tests). The `stable` branch contains the code of the last release (done usually once per month on an approximately one week old snapshot of the `develop` branch to prevent packaging bugs, so potentially it's more stable). When cloning the repository the default working branch has the name `develop`. This branch contains all last features (can be considered as relatively stable, thanks to automated tests). The `stable` branch contains the code of the last release (done usually once per month on an approximately one week old snapshot of the `develop` branch to prevent packaging bugs, so potentially it's more stable).
!!! Note !!! Note
Python3.6 or higher and the corresponding `pip` are assumed to be available. The install-script will warn you and stop if that's not the case. `git` is also needed to clone the Freqtrade repository. Python3.7 or higher and the corresponding `pip` are assumed to be available. The install-script will warn you and stop if that's not the case. `git` is also needed to clone the Freqtrade repository.
This can be achieved with the following commands: This can be achieved with the following commands:
@ -63,7 +63,7 @@ usage:
** --install ** ** --install **
With this option, the script will install the bot and most dependencies: With this option, the script will install the bot and most dependencies:
You will need to have git and python3.6+ installed beforehand for this to work. You will need to have git and python3.7+ installed beforehand for this to work.
* Mandatory software as: `ta-lib` * Mandatory software as: `ta-lib`
* Setup your virtualenv under `.env/` * Setup your virtualenv under `.env/`
@ -90,13 +90,13 @@ Each time you open a new terminal, you must run `source .env/bin/activate`.
## Custom Installation ## Custom Installation
We've included/collected install instructions for Ubuntu 16.04, MacOS, and Windows. These are guidelines and your success may vary with other distros. We've included/collected install instructions for Ubuntu, MacOS, and Windows. These are guidelines and your success may vary with other distros.
OS Specific steps are listed first, the [Common](#common) section below is necessary for all systems. OS Specific steps are listed first, the [Common](#common) section below is necessary for all systems.
!!! Note !!! Note
Python3.6 or higher and the corresponding pip are assumed to be available. Python3.7 or higher and the corresponding pip are assumed to be available.
=== "Ubuntu 16.04" === "Ubuntu/Debian"
#### Install necessary dependencies #### Install necessary dependencies
```bash ```bash
@ -105,13 +105,17 @@ OS Specific steps are listed first, the [Common](#common) section below is neces
``` ```
=== "RaspberryPi/Raspbian" === "RaspberryPi/Raspbian"
The following assumes the latest [Raspbian Buster lite image](https://www.raspberrypi.org/downloads/raspbian/) from at least September 2019. The following assumes the latest [Raspbian Buster lite image](https://www.raspberrypi.org/downloads/raspbian/).
This image comes with python3.7 preinstalled, making it easy to get freqtrade up and running. This image comes with python3.7 preinstalled, making it easy to get freqtrade up and running.
Tested using a Raspberry Pi 3 with the Raspbian Buster lite image, all updates applied. Tested using a Raspberry Pi 3 with the Raspbian Buster lite image, all updates applied.
``` bash ``` bash
sudo apt-get install python3-venv libatlas-base-dev sudo apt-get install python3-venv libatlas-base-dev cmake
# Use pywheels.org to speed up installation
sudo echo "[global]\nextra-index-url=https://www.piwheels.org/simple" > tee /etc/pip.conf
git clone https://github.com/freqtrade/freqtrade.git git clone https://github.com/freqtrade/freqtrade.git
cd freqtrade cd freqtrade
@ -120,6 +124,7 @@ OS Specific steps are listed first, the [Common](#common) section below is neces
!!! Note "Installation duration" !!! Note "Installation duration"
Depending on your internet speed and the Raspberry Pi version, installation can take multiple hours to complete. Depending on your internet speed and the Raspberry Pi version, installation can take multiple hours to complete.
Due to this, we recommend to use the prebuild docker-image for Raspberry, by following the [Docker quickstart documentation](docker_quickstart.md)
!!! Note !!! Note
The above does not install hyperopt dependencies. To install these, please use `python3 -m pip install -e .[hyperopt]`. The above does not install hyperopt dependencies. To install these, please use `python3 -m pip install -e .[hyperopt]`.

View File

@ -1,51 +1,48 @@
{#-
This file was automatically generated - do not edit
-#}
{% set site_url = config.site_url | d(nav.homepage.url, true) | url %}
{% if not config.use_directory_urls and site_url[0] == site_url[-1] == "." %}
{% set site_url = site_url ~ "/index.html" %}
{% endif %}
<header class="md-header" data-md-component="header"> <header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid"> <nav class="md-header-nav md-grid" aria-label="{{ lang.t('header.title') }}">
<div class="md-flex"> <a href="{{ site_url }}" title="{{ config.site_name | e }}" class="md-header-nav__button md-logo"
<div class="md-flex__cell md-flex__cell--shrink"> aria-label="{{ config.site_name }}">
<a href="{{ config.site_url | default(nav.homepage.url, true) | url }}" title="{{ config.site_name }}" {% include "partials/logo.html" %}
class="md-header-nav__button md-logo">
{% if config.theme.logo.icon %}
<i class="md-icon">{{ config.theme.logo.icon }}</i>
{% else %}
<img src="{{ config.theme.logo | url }}" width="24" height="24">
{% endif %}
</a> </a>
</div> <label class="md-header-nav__button md-icon" for="__drawer">
<div class="md-flex__cell md-flex__cell--shrink"> {% include ".icons/material/menu" ~ ".svg" %}
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label> </label>
</div> <div class="md-header-nav__title" data-md-component="header-title">
<div class="md-flex__cell md-flex__cell--stretch"> <div class="md-header-nav__ellipsis">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title"> <div class="md-header-nav__topic">
{% block site_name %} <span class="md-ellipsis">
{% if config.site_name == page.title %}
{{ config.site_name }} {{ config.site_name }}
</span>
</div>
<div class="md-header-nav__topic">
<span class="md-ellipsis">
{% if page and page.meta and page.meta.title %}
{{ page.meta.title }}
{% else %} {% else %}
<span class="md-header-nav__topic">
{{ config.site_name }}
</span>
<span class="md-header-nav__topic">
{{ page.title }} {{ page.title }}
</span>
{% endif %} {% endif %}
{% endblock %} </span>
</div>
</div> </div>
</div> </div>
<div class="md-flex__cell md-flex__cell--shrink">
{% block search_box %}
{% if "search" in config["plugins"] %} {% if "search" in config["plugins"] %}
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label> <label class="md-header-nav__button md-icon" for="__search">
{% include ".icons/material/magnify.svg" %}
</label>
{% include "partials/search.html" %} {% include "partials/search.html" %}
{% endif %} {% endif %}
{% endblock %}
</div>
{% if config.repo_url %} {% if config.repo_url %}
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source"> <div class="md-header-nav__source">
{% include "partials/source.html" %} {% include "partials/source.html" %}
</div> </div>
</div>
{% endif %} {% endif %}
</div>
</nav> </nav>
<!-- Place this tag in your head or just before your close body tag. --> <!-- Place this tag in your head or just before your close body tag. -->
<script async defer src="https://buttons.github.io/buttons.js"></script> <script async defer src="https://buttons.github.io/buttons.js"></script>

View File

@ -168,6 +168,7 @@ Additional features when using plot_config include:
* Specify colors per indicator * Specify colors per indicator
* Specify additional subplots * Specify additional subplots
* Specify indicator pairs to fill area in between
The sample plot configuration below specifies fixed colors for the indicators. Otherwise consecutive plots may produce different colorschemes each time, making comparisons difficult. The sample plot configuration below specifies fixed colors for the indicators. Otherwise consecutive plots may produce different colorschemes each time, making comparisons difficult.
It also allows multiple subplots to display both MACD and RSI at the same time. It also allows multiple subplots to display both MACD and RSI at the same time.
@ -183,23 +184,33 @@ Sample configuration with inline comments explaining the process:
'ema50': {'color': '#CCCCCC'}, 'ema50': {'color': '#CCCCCC'},
# By omitting color, a random color is selected. # By omitting color, a random color is selected.
'sar': {}, 'sar': {},
# fill area between senkou_a and senkou_b
'senkou_a': {
'color': 'green', #optional
'fill_to': 'senkou_b',
'fill_label': 'Ichimoku Cloud' #optional,
'fill_color': 'rgba(255,76,46,0.2)', #optional
},
# plot senkou_b, too. Not only the area to it.
'senkou_b': {}
}, },
'subplots': { 'subplots': {
# Create subplot MACD # Create subplot MACD
"MACD": { "MACD": {
'macd': {'color': 'blue'}, 'macd': {'color': 'blue', 'fill_to': 'macdhist'},
'macdsignal': {'color': 'orange'}, 'macdsignal': {'color': 'orange'}
}, },
# Additional subplot RSI # Additional subplot RSI
"RSI": { "RSI": {
'rsi': {'color': 'red'}, 'rsi': {'color': 'red'}
} }
} }
} }
```
```
!!! Note !!! Note
The above configuration assumes that `ema10`, `ema50`, `macd`, `macdsignal` and `rsi` are columns in the DataFrame created by the strategy. The above configuration assumes that `ema10`, `ema50`, `senkou_a`, `senkou_b`,
`macd`, `macdsignal`, `macdhist` and `rsi` are columns in the DataFrame created by the strategy.
## Plot profit ## Plot profit

3
docs/plugins.md Normal file
View File

@ -0,0 +1,3 @@
# Plugins
--8<-- "includes/pairlists.md"
--8<-- "includes/protections.md"

View File

@ -1,3 +1,3 @@
mkdocs-material==6.1.6 mkdocs-material==6.2.3
mdx_truly_sane_lists==1.2 mdx_truly_sane_lists==1.2
pymdown-extensions==8.0.1 pymdown-extensions==8.1

View File

@ -127,6 +127,7 @@ python3 scripts/rest_client.py --config rest_config.json <command> [optional par
| `performance` | Show performance of each finished trade grouped by pair. | `performance` | Show performance of each finished trade grouped by pair.
| `balance` | Show account balance per currency. | `balance` | Show account balance per currency.
| `daily <n>` | Shows profit or loss per day, over the last n days (n defaults to 7). | `daily <n>` | Shows profit or loss per day, over the last n days (n defaults to 7).
| `stats` | Display a summary of profit / loss reasons as well as average holding times.
| `whitelist` | Show the current whitelist. | `whitelist` | Show the current whitelist.
| `blacklist [pair]` | Show the current blacklist, or adds a pair to the blacklist. | `blacklist [pair]` | Show the current blacklist, or adds a pair to the blacklist.
| `edge` | Show validated pairs by Edge if it is enabled. | `edge` | Show validated pairs by Edge if it is enabled.
@ -229,6 +230,9 @@ show_config
start start
Start the bot if it's in the stopped state. Start the bot if it's in the stopped state.
stats
Return the stats report (durations, sell-reasons).
status status
Get the status of open trades. Get the status of open trades.

View File

@ -147,7 +147,7 @@ Let's try to backtest 1 month (January 2019) of 5m candles using an example stra
freqtrade backtesting --timerange 20190101-20190201 --timeframe 5m freqtrade backtesting --timerange 20190101-20190201 --timeframe 5m
``` ```
Assuming `startup_candle_count` is set to 100, backtesting knows it needs 100 candles to generate valid buy signals. It will load data from `20190101 - (100 * 5m)` - which is ~2019-12-31 15:30:00. Assuming `startup_candle_count` is set to 100, backtesting knows it needs 100 candles to generate valid buy signals. It will load data from `20190101 - (100 * 5m)` - which is ~2018-12-31 15:30:00.
If this data is available, indicators will be calculated with this extended timerange. The instable startup period (up to 2019-01-01 00:00:00) will then be removed before starting backtesting. If this data is available, indicators will be calculated with this extended timerange. The instable startup period (up to 2019-01-01 00:00:00) will then be removed before starting backtesting.
!!! Note !!! Note

View File

@ -87,6 +87,41 @@ Example configuration showing the different settings:
}, },
``` ```
## Create a custom keyboard (command shortcut buttons)
Telegram allows us to create a custom keyboard with buttons for commands.
The default custom keyboard looks like this.
```python
[
["/daily", "/profit", "/balance"], # row 1, 3 commands
["/status", "/status table", "/performance"], # row 2, 3 commands
["/count", "/start", "/stop", "/help"] # row 3, 4 commands
]
```
### Usage
You can create your own keyboard in `config.json`:
``` json
"telegram": {
"enabled": true,
"token": "your_telegram_token",
"chat_id": "your_telegram_chat_id",
"keyboard": [
["/daily", "/stats", "/balance", "/profit"],
["/status table", "/performance"],
["/reload_config", "/count", "/logs"]
]
},
```
!!! Note "Supported Commands"
Only the following commands are allowed. Command arguments are not supported!
`/start`, `/stop`, `/status`, `/status table`, `/trades`, `/profit`, `/performance`, `/daily`, `/stats`, `/count`, `/locks`, `/balance`, `/stopbuy`, `/reload_config`, `/show_config`, `/logs`, `/whitelist`, `/blacklist`, `/edge`, `/help`, `/version`
## Telegram commands ## Telegram commands
Per default, the Telegram bot shows predefined commands. Some commands Per default, the Telegram bot shows predefined commands. Some commands
@ -106,6 +141,7 @@ official commands. You can ask at any moment for help with `/help`.
| `/trades [limit]` | List all recently closed trades in a table format. | `/trades [limit]` | List all recently closed trades in a table format.
| `/delete <trade_id>` | Delete a specific trade from the Database. Tries to close open orders. Requires manual handling of this trade on the exchange. | `/delete <trade_id>` | Delete a specific trade from the Database. Tries to close open orders. Requires manual handling of this trade on the exchange.
| `/count` | Displays number of trades used and available | `/count` | Displays number of trades used and available
| `/locks` | Show currently locked pairs.
| `/profit` | Display a summary of your profit/loss from close trades and some stats about your performance | `/profit` | Display a summary of your profit/loss from close trades and some stats about your performance
| `/forcesell <trade_id>` | Instantly sells the given trade (Ignoring `minimum_roi`). | `/forcesell <trade_id>` | Instantly sells the given trade (Ignoring `minimum_roi`).
| `/forcesell all` | Instantly sells all open trades (Ignoring `minimum_roi`). | `/forcesell all` | Instantly sells all open trades (Ignoring `minimum_roi`).
@ -113,6 +149,7 @@ official commands. You can ask at any moment for help with `/help`.
| `/performance` | Show performance of each finished trade grouped by pair | `/performance` | Show performance of each finished trade grouped by pair
| `/balance` | Show account balance per currency | `/balance` | Show account balance per currency
| `/daily <n>` | Shows profit or loss per day, over the last n days (n defaults to 7) | `/daily <n>` | Shows profit or loss per day, over the last n days (n defaults to 7)
| `/stats` | Shows Wins / losses by Sell reason as well as Avg. holding durations for buys and sells
| `/whitelist` | Show the current whitelist | `/whitelist` | Show the current whitelist
| `/blacklist [pair]` | Show the current blacklist, or adds a pair to the blacklist. | `/blacklist [pair]` | Show the current blacklist, or adds a pair to the blacklist.
| `/edge` | Show validated pairs by Edge if it is enabled. | `/edge` | Show validated pairs by Edge if it is enabled.
@ -207,7 +244,7 @@ Return a summary of your profit/loss and performance.
Note that for this to work, `forcebuy_enable` needs to be set to true. Note that for this to work, `forcebuy_enable` needs to be set to true.
[More details](configuration.md/#understand-forcebuy_enable) [More details](configuration.md#understand-forcebuy_enable)
### /performance ### /performance

31
docs/updating.md Normal file
View File

@ -0,0 +1,31 @@
# How to update
To update your freqtrade installation, please use one of the below methods, corresponding to your installation method.
## docker-compose
!!! Note "Legacy installations using the `master` image"
We're switching from master to stable for the release Images - please adjust your docker-file and replace `freqtradeorg/freqtrade:master` with `freqtradeorg/freqtrade:stable`
``` bash
docker-compose pull
docker-compose up -d
```
## Installation via setup script
``` bash
./setup.sh --update
```
!!! Note
Make sure to run this command with your virtual environment disabled!
## Plain native installation
Please ensure that you're also updating dependencies - otherwise things might break without you noticing.
``` bash
git pull
pip install -U -r requirements.txt
```

View File

@ -4,7 +4,7 @@ channels:
- conda-forge - conda-forge
dependencies: dependencies:
# Required for app # Required for app
- python>=3.6 - python>=3.7
- pip - pip
- wheel - wheel
- numpy - numpy

View File

@ -1,5 +1,5 @@
""" Freqtrade bot """ """ Freqtrade bot """
__version__ = '2020.11' __version__ = '2020.12'
if __version__ == 'develop': if __version__ == 'develop':

View File

@ -3,7 +3,7 @@
__main__.py for Freqtrade __main__.py for Freqtrade
To launch Freqtrade as a module To launch Freqtrade as a module
> python -m freqtrade (with Python >= 3.6) > python -m freqtrade (with Python >= 3.7)
""" """
from freqtrade import main from freqtrade import main

View File

@ -20,11 +20,13 @@ ARGS_COMMON_OPTIMIZE = ["timeframe", "timerange", "dataformat_ohlcv",
"max_open_trades", "stake_amount", "fee"] "max_open_trades", "stake_amount", "fee"]
ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_positions", ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_positions",
"enable_protections",
"strategy_list", "export", "exportfilename"] "strategy_list", "export", "exportfilename"]
ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path", ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path",
"position_stacking", "epochs", "spaces", "position_stacking", "use_max_market_positions",
"use_max_market_positions", "print_all", "enable_protections",
"epochs", "spaces", "print_all",
"print_colorized", "print_json", "hyperopt_jobs", "print_colorized", "print_json", "hyperopt_jobs",
"hyperopt_random_state", "hyperopt_min_trades", "hyperopt_random_state", "hyperopt_min_trades",
"hyperopt_loss"] "hyperopt_loss"]
@ -42,7 +44,8 @@ ARGS_LIST_TIMEFRAMES = ["exchange", "print_one_column"]
ARGS_LIST_PAIRS = ["exchange", "print_list", "list_pairs_print_json", "print_one_column", ARGS_LIST_PAIRS = ["exchange", "print_list", "list_pairs_print_json", "print_one_column",
"print_csv", "base_currencies", "quote_currencies", "list_pairs_all"] "print_csv", "base_currencies", "quote_currencies", "list_pairs_all"]
ARGS_TEST_PAIRLIST = ["config", "quote_currencies", "print_one_column", "list_pairs_print_json"] ARGS_TEST_PAIRLIST = ["verbosity", "config", "quote_currencies", "print_one_column",
"list_pairs_print_json"]
ARGS_CREATE_USERDIR = ["user_data_dir", "reset"] ARGS_CREATE_USERDIR = ["user_data_dir", "reset"]

View File

@ -144,6 +144,14 @@ AVAILABLE_CLI_OPTIONS = {
action='store_false', action='store_false',
default=True, default=True,
), ),
"enable_protections": Arg(
'--enable-protections', '--enableprotections',
help='Enable protections for backtesting.'
'Will slow backtesting down by a considerable amount, but will include '
'configured protections',
action='store_true',
default=False,
),
"strategy_list": Arg( "strategy_list": Arg(
'--strategy-list', '--strategy-list',
help='Provide a space-separated list of strategies to backtest. ' help='Provide a space-separated list of strategies to backtest. '

View File

@ -15,7 +15,7 @@ def start_test_pairlist(args: Dict[str, Any]) -> None:
""" """
Test Pairlist configuration Test Pairlist configuration
""" """
from freqtrade.pairlist.pairlistmanager import PairListManager from freqtrade.plugins.pairlistmanager import PairListManager
config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE) config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE)
exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config, validate=False) exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config, validate=False)

View File

@ -74,6 +74,7 @@ def validate_config_consistency(conf: Dict[str, Any]) -> None:
_validate_trailing_stoploss(conf) _validate_trailing_stoploss(conf)
_validate_edge(conf) _validate_edge(conf)
_validate_whitelist(conf) _validate_whitelist(conf)
_validate_protections(conf)
_validate_unlimited_amount(conf) _validate_unlimited_amount(conf)
# validate configuration before returning # validate configuration before returning
@ -155,3 +156,22 @@ def _validate_whitelist(conf: Dict[str, Any]) -> None:
if (pl.get('method') == 'StaticPairList' if (pl.get('method') == 'StaticPairList'
and not conf.get('exchange', {}).get('pair_whitelist')): and not conf.get('exchange', {}).get('pair_whitelist')):
raise OperationalException("StaticPairList requires pair_whitelist to be set.") raise OperationalException("StaticPairList requires pair_whitelist to be set.")
def _validate_protections(conf: Dict[str, Any]) -> None:
"""
Validate protection configuration validity
"""
for prot in conf.get('protections', []):
if ('stop_duration' in prot and 'stop_duration_candles' in prot):
raise OperationalException(
"Protections must specify either `stop_duration` or `stop_duration_candles`.\n"
f"Please fix the protection {prot.get('method')}"
)
if ('lookback_period' in prot and 'lookback_period_candles' in prot):
raise OperationalException(
"Protections must specify either `lookback_period` or `lookback_period_candles`.\n"
f"Please fix the protection {prot.get('method')}"
)

View File

@ -211,6 +211,9 @@ class Configuration:
self._args_to_config(config, argname='position_stacking', self._args_to_config(config, argname='position_stacking',
logstring='Parameter --enable-position-stacking detected ...') logstring='Parameter --enable-position-stacking detected ...')
self._args_to_config(
config, argname='enable_protections',
logstring='Parameter --enable-protections detected, enabling Protections. ...')
# Setting max_open_trades to infinite if -1 # Setting max_open_trades to infinite if -1
if config.get('max_open_trades') == -1: if config.get('max_open_trades') == -1:
config['max_open_trades'] = float('inf') config['max_open_trades'] = float('inf')

View File

@ -26,6 +26,24 @@ def check_conflicting_settings(config: Dict[str, Any],
) )
def process_removed_setting(config: Dict[str, Any],
section1: str, name1: str,
section2: str, name2: str) -> None:
"""
:param section1: Removed section
:param name1: Removed setting name
:param section2: new section for this key
:param name2: new setting name
"""
section1_config = config.get(section1, {})
if name1 in section1_config:
raise OperationalException(
f"Setting `{section1}.{name1}` has been moved to `{section2}.{name2}. "
f"Please delete it from your configuration and use the `{section2}.{name2}` "
"setting instead."
)
def process_deprecated_setting(config: Dict[str, Any], def process_deprecated_setting(config: Dict[str, Any],
section1: str, name1: str, section1: str, name1: str,
section2: str, name2: str) -> None: section2: str, name2: str) -> None:
@ -44,19 +62,18 @@ def process_deprecated_setting(config: Dict[str, Any],
def process_temporary_deprecated_settings(config: Dict[str, Any]) -> None: def process_temporary_deprecated_settings(config: Dict[str, Any]) -> None:
check_conflicting_settings(config, 'ask_strategy', 'use_sell_signal', # Kept for future deprecated / moved settings
'experimental', 'use_sell_signal') # check_conflicting_settings(config, 'ask_strategy', 'use_sell_signal',
check_conflicting_settings(config, 'ask_strategy', 'sell_profit_only', # 'experimental', 'use_sell_signal')
'experimental', 'sell_profit_only') # process_deprecated_setting(config, 'ask_strategy', 'use_sell_signal',
check_conflicting_settings(config, 'ask_strategy', 'ignore_roi_if_buy_signal', # 'experimental', 'use_sell_signal')
'experimental', 'ignore_roi_if_buy_signal')
process_deprecated_setting(config, 'ask_strategy', 'use_sell_signal', process_removed_setting(config, 'experimental', 'use_sell_signal',
'experimental', 'use_sell_signal') 'ask_strategy', 'use_sell_signal')
process_deprecated_setting(config, 'ask_strategy', 'sell_profit_only', process_removed_setting(config, 'experimental', 'sell_profit_only',
'experimental', 'sell_profit_only') 'ask_strategy', 'sell_profit_only')
process_deprecated_setting(config, 'ask_strategy', 'ignore_roi_if_buy_signal', process_removed_setting(config, 'experimental', 'ignore_roi_if_buy_signal',
'experimental', 'ignore_roi_if_buy_signal') 'ask_strategy', 'ignore_roi_if_buy_signal')
if (config.get('edge', {}).get('enabled', False) if (config.get('edge', {}).get('enabled', False)
and 'capital_available_percentage' in config.get('edge', {})): and 'capital_available_percentage' in config.get('edge', {})):

View File

@ -24,8 +24,10 @@ HYPEROPT_LOSS_BUILTIN = ['ShortTradeDurHyperOptLoss', 'OnlyProfitHyperOptLoss',
'SharpeHyperOptLoss', 'SharpeHyperOptLossDaily', 'SharpeHyperOptLoss', 'SharpeHyperOptLossDaily',
'SortinoHyperOptLoss', 'SortinoHyperOptLossDaily'] 'SortinoHyperOptLoss', 'SortinoHyperOptLossDaily']
AVAILABLE_PAIRLISTS = ['StaticPairList', 'VolumePairList', AVAILABLE_PAIRLISTS = ['StaticPairList', 'VolumePairList',
'AgeFilter', 'PrecisionFilter', 'PriceFilter', 'AgeFilter', 'PerformanceFilter', 'PrecisionFilter',
'RangeStabilityFilter', 'ShuffleFilter', 'SpreadFilter'] 'PriceFilter', 'RangeStabilityFilter', 'ShuffleFilter',
'SpreadFilter']
AVAILABLE_PROTECTIONS = ['CooldownPeriod', 'LowProfitPairs', 'MaxDrawdown', 'StoplossGuard']
AVAILABLE_DATAHANDLERS = ['json', 'jsongz', 'hdf5'] AVAILABLE_DATAHANDLERS = ['json', 'jsongz', 'hdf5']
DRY_RUN_WALLET = 1000 DRY_RUN_WALLET = 1000
DATETIME_PRINT_FORMAT = '%Y-%m-%d %H:%M:%S' DATETIME_PRINT_FORMAT = '%Y-%m-%d %H:%M:%S'
@ -182,9 +184,6 @@ CONF_SCHEMA = {
'experimental': { 'experimental': {
'type': 'object', 'type': 'object',
'properties': { 'properties': {
'use_sell_signal': {'type': 'boolean'},
'sell_profit_only': {'type': 'boolean'},
'ignore_roi_if_buy_signal': {'type': 'boolean'},
'block_bad_exchanges': {'type': 'boolean'} 'block_bad_exchanges': {'type': 'boolean'}
} }
}, },
@ -194,7 +193,21 @@ CONF_SCHEMA = {
'type': 'object', 'type': 'object',
'properties': { 'properties': {
'method': {'type': 'string', 'enum': AVAILABLE_PAIRLISTS}, 'method': {'type': 'string', 'enum': AVAILABLE_PAIRLISTS},
'config': {'type': 'object'} },
'required': ['method'],
}
},
'protections': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'method': {'type': 'string', 'enum': AVAILABLE_PROTECTIONS},
'stop_duration': {'type': 'number', 'minimum': 0.0},
'stop_duration_candles': {'type': 'number', 'minimum': 0},
'trade_limit': {'type': 'number', 'minimum': 1},
'lookback_period': {'type': 'number', 'minimum': 1},
'lookback_period_candles': {'type': 'number', 'minimum': 1},
}, },
'required': ['method'], 'required': ['method'],
} }

View File

@ -6,6 +6,7 @@ from freqtrade.exchange.exchange import Exchange
from freqtrade.exchange.bibox import Bibox from freqtrade.exchange.bibox import Bibox
from freqtrade.exchange.binance import Binance from freqtrade.exchange.binance import Binance
from freqtrade.exchange.bittrex import Bittrex from freqtrade.exchange.bittrex import Bittrex
from freqtrade.exchange.bybit import Bybit
from freqtrade.exchange.exchange import (available_exchanges, ccxt_exchanges, from freqtrade.exchange.exchange import (available_exchanges, ccxt_exchanges,
get_exchange_bad_reason, is_exchange_bad, get_exchange_bad_reason, is_exchange_bad,
is_exchange_known_ccxt, is_exchange_officially_supported, is_exchange_known_ccxt, is_exchange_officially_supported,

View File

@ -18,6 +18,7 @@ class Binance(Exchange):
_ft_has: Dict = { _ft_has: Dict = {
"stoploss_on_exchange": True, "stoploss_on_exchange": True,
"order_time_in_force": ['gtc', 'fok', 'ioc'], "order_time_in_force": ['gtc', 'fok', 'ioc'],
"ohlcv_candle_limit": 1000,
"trades_pagination": "id", "trades_pagination": "id",
"trades_pagination_arg": "fromId", "trades_pagination_arg": "fromId",
"l2_limit_range": [5, 10, 20, 50, 100, 500, 1000], "l2_limit_range": [5, 10, 20, 50, 100, 500, 1000],

View File

@ -0,0 +1,24 @@
""" Bybit exchange subclass """
import logging
from typing import Dict
from freqtrade.exchange import Exchange
logger = logging.getLogger(__name__)
class Bybit(Exchange):
"""
Bybit exchange class. Contains adjustments needed for Freqtrade to work
with this exchange.
Please note that this exchange is not included in the list of exchanges
officially supported by the Freqtrade development team. So some features
may still not work as expected.
"""
# fetchCurrencies API point requires authentication for Bybit,
_ft_has: Dict = {
"ohlcv_candle_limit": 200,
}

View File

@ -658,7 +658,8 @@ class Exchange:
@retrier @retrier
def fetch_ticker(self, pair: str) -> dict: def fetch_ticker(self, pair: str) -> dict:
try: try:
if pair not in self._api.markets or not self._api.markets[pair].get('active'): if (pair not in self._api.markets or
self._api.markets[pair].get('active', False) is False):
raise ExchangeError(f"Pair {pair} not available") raise ExchangeError(f"Pair {pair} not available")
data = self._api.fetch_ticker(pair) data = self._api.fetch_ticker(pair)
return data return data
@ -732,13 +733,17 @@ class Exchange:
logger.info("Downloaded data for %s with length %s.", pair, len(data)) logger.info("Downloaded data for %s with length %s.", pair, len(data))
return data return data
def refresh_latest_ohlcv(self, pair_list: ListPairsWithTimeframes) -> List[Tuple[str, List]]: def refresh_latest_ohlcv(self, pair_list: ListPairsWithTimeframes, *,
since_ms: Optional[int] = None, cache: bool = True
) -> Dict[Tuple[str, str], DataFrame]:
""" """
Refresh in-memory OHLCV asynchronously and set `_klines` with the result Refresh in-memory OHLCV asynchronously and set `_klines` with the result
Loops asynchronously over pair_list and downloads all pairs async (semi-parallel). Loops asynchronously over pair_list and downloads all pairs async (semi-parallel).
Only used in the dataprovider.refresh() method. Only used in the dataprovider.refresh() method.
:param pair_list: List of 2 element tuples containing pair, interval to refresh :param pair_list: List of 2 element tuples containing pair, interval to refresh
:return: TODO: return value is only used in the tests, get rid of it :param since_ms: time since when to download, in milliseconds
:param cache: Assign result to _klines. Usefull for one-off downloads like for pairlists
:return: Dict of [{(pair, timeframe): Dataframe}]
""" """
logger.debug("Refreshing candle (OHLCV) data for %d pairs", len(pair_list)) logger.debug("Refreshing candle (OHLCV) data for %d pairs", len(pair_list))
@ -748,7 +753,8 @@ class Exchange:
for pair, timeframe in set(pair_list): for pair, timeframe in set(pair_list):
if (not ((pair, timeframe) in self._klines) if (not ((pair, timeframe) in self._klines)
or self._now_is_time_to_refresh(pair, timeframe)): or self._now_is_time_to_refresh(pair, timeframe)):
input_coroutines.append(self._async_get_candle_history(pair, timeframe)) input_coroutines.append(self._async_get_candle_history(pair, timeframe,
since_ms=since_ms))
else: else:
logger.debug( logger.debug(
"Using cached candle (OHLCV) data for pair %s, timeframe %s ...", "Using cached candle (OHLCV) data for pair %s, timeframe %s ...",
@ -758,6 +764,7 @@ class Exchange:
results = asyncio.get_event_loop().run_until_complete( results = asyncio.get_event_loop().run_until_complete(
asyncio.gather(*input_coroutines, return_exceptions=True)) asyncio.gather(*input_coroutines, return_exceptions=True))
results_df = {}
# handle caching # handle caching
for res in results: for res in results:
if isinstance(res, Exception): if isinstance(res, Exception):
@ -769,11 +776,13 @@ class Exchange:
if ticks: if ticks:
self._pairs_last_refresh_time[(pair, timeframe)] = ticks[-1][0] // 1000 self._pairs_last_refresh_time[(pair, timeframe)] = ticks[-1][0] // 1000
# keeping parsed dataframe in cache # keeping parsed dataframe in cache
self._klines[(pair, timeframe)] = ohlcv_to_dataframe( ohlcv_df = ohlcv_to_dataframe(
ticks, timeframe, pair=pair, fill_missing=True, ticks, timeframe, pair=pair, fill_missing=True,
drop_incomplete=self._ohlcv_partial_candle) drop_incomplete=self._ohlcv_partial_candle)
results_df[(pair, timeframe)] = ohlcv_df
return results if cache:
self._klines[(pair, timeframe)] = ohlcv_df
return results_df
def _now_is_time_to_refresh(self, pair: str, timeframe: str) -> bool: def _now_is_time_to_refresh(self, pair: str, timeframe: str) -> bool:
# Timeframe in seconds # Timeframe in seconds
@ -798,7 +807,8 @@ class Exchange:
) )
data = await self._api_async.fetch_ohlcv(pair, timeframe=timeframe, data = await self._api_async.fetch_ohlcv(pair, timeframe=timeframe,
since=since_ms) since=since_ms,
limit=self._ohlcv_candle_limit)
# Some exchanges sort OHLCV in ASC order and others in DESC. # Some exchanges sort OHLCV in ASC order and others in DESC.
# Ex: Bittrex returns the list of OHLCV in ASC order (oldest first, newest last) # Ex: Bittrex returns the list of OHLCV in ASC order (oldest first, newest last)

View File

@ -18,6 +18,7 @@ class Kraken(Exchange):
_params: Dict = {"trading_agreement": "agree"} _params: Dict = {"trading_agreement": "agree"}
_ft_has: Dict = { _ft_has: Dict = {
"stoploss_on_exchange": True, "stoploss_on_exchange": True,
"ohlcv_candle_limit": 720,
"trades_pagination": "id", "trades_pagination": "id",
"trades_pagination_arg": "since", "trades_pagination_arg": "since",
} }

View File

@ -19,10 +19,12 @@ from freqtrade.data.dataprovider import DataProvider
from freqtrade.edge import Edge from freqtrade.edge import Edge
from freqtrade.exceptions import (DependencyException, ExchangeError, InsufficientFundsError, from freqtrade.exceptions import (DependencyException, ExchangeError, InsufficientFundsError,
InvalidOrderException, PricingError) InvalidOrderException, PricingError)
from freqtrade.exchange import timeframe_to_minutes from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
from freqtrade.misc import safe_value_fallback, safe_value_fallback2 from freqtrade.misc import safe_value_fallback, safe_value_fallback2
from freqtrade.pairlist.pairlistmanager import PairListManager from freqtrade.mixins import LoggingMixin
from freqtrade.persistence import Order, PairLocks, Trade, cleanup_db, init_db from freqtrade.persistence import Order, PairLocks, Trade, cleanup_db, init_db
from freqtrade.plugins.pairlistmanager import PairListManager
from freqtrade.plugins.protectionmanager import ProtectionManager
from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.resolvers import ExchangeResolver, StrategyResolver
from freqtrade.rpc import RPCManager, RPCMessageType from freqtrade.rpc import RPCManager, RPCMessageType
from freqtrade.state import State from freqtrade.state import State
@ -34,7 +36,7 @@ from freqtrade.wallets import Wallets
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class FreqtradeBot: class FreqtradeBot(LoggingMixin):
""" """
Freqtrade is the main class of the bot. Freqtrade is the main class of the bot.
This is from here the bot start its logic. This is from here the bot start its logic.
@ -78,6 +80,8 @@ class FreqtradeBot:
self.dataprovider = DataProvider(self.config, self.exchange, self.pairlists) self.dataprovider = DataProvider(self.config, self.exchange, self.pairlists)
self.protections = ProtectionManager(self.config)
# Attach Dataprovider to Strategy baseclass # Attach Dataprovider to Strategy baseclass
IStrategy.dp = self.dataprovider IStrategy.dp = self.dataprovider
# Attach Wallets to Strategy baseclass # Attach Wallets to Strategy baseclass
@ -101,6 +105,7 @@ class FreqtradeBot:
self.rpc: RPCManager = RPCManager(self) self.rpc: RPCManager = RPCManager(self)
# Protect sell-logic from forcesell and viceversa # Protect sell-logic from forcesell and viceversa
self._sell_lock = Lock() self._sell_lock = Lock()
LoggingMixin.__init__(self, logger, timeframe_to_seconds(self.strategy.timeframe))
def notify_status(self, msg: str) -> None: def notify_status(self, msg: str) -> None:
""" """
@ -132,7 +137,7 @@ class FreqtradeBot:
Called on startup and after reloading the bot - triggers notifications and Called on startup and after reloading the bot - triggers notifications and
performs startup tasks performs startup tasks
""" """
self.rpc.startup_messages(self.config, self.pairlists) self.rpc.startup_messages(self.config, self.pairlists, self.protections)
if not self.edge: if not self.edge:
# Adjust stoploss if it was changed # Adjust stoploss if it was changed
Trade.stoploss_reinitialization(self.strategy.stoploss) Trade.stoploss_reinitialization(self.strategy.stoploss)
@ -358,6 +363,15 @@ class FreqtradeBot:
logger.info("No currency pair in active pair whitelist, " logger.info("No currency pair in active pair whitelist, "
"but checking to sell open trades.") "but checking to sell open trades.")
return trades_created return trades_created
if PairLocks.is_global_lock():
lock = PairLocks.get_pair_longest_lock('*')
if lock:
self.log_once(f"Global pairlock active until "
f"{lock.lock_end_time.strftime(constants.DATETIME_PRINT_FORMAT)}. "
"Not creating new trades.", logger.info)
else:
self.log_once("Global pairlock active. Not creating new trades.", logger.info)
return trades_created
# Create entity and execute trade for each pair from whitelist # Create entity and execute trade for each pair from whitelist
for pair in whitelist: for pair in whitelist:
try: try:
@ -366,8 +380,7 @@ class FreqtradeBot:
logger.warning('Unable to create trade for %s: %s', pair, exception) logger.warning('Unable to create trade for %s: %s', pair, exception)
if not trades_created: if not trades_created:
logger.debug("Found no buy signals for whitelisted currencies. " logger.debug("Found no buy signals for whitelisted currencies. Trying again...")
"Trying again...")
return trades_created return trades_created
@ -519,7 +532,6 @@ class FreqtradeBot:
# reserve some percent defined in config (5% default) + stoploss # reserve some percent defined in config (5% default) + stoploss
amount_reserve_percent = 1.0 - self.config.get('amount_reserve_percent', amount_reserve_percent = 1.0 - self.config.get('amount_reserve_percent',
constants.DEFAULT_AMOUNT_RESERVE_PERCENT) constants.DEFAULT_AMOUNT_RESERVE_PERCENT)
if self.strategy.stoploss is not None:
amount_reserve_percent += self.strategy.stoploss amount_reserve_percent += self.strategy.stoploss
# it should not be more than 50% # it should not be more than 50%
amount_reserve_percent = max(amount_reserve_percent, 0.5) amount_reserve_percent = max(amount_reserve_percent, 0.5)
@ -541,9 +553,15 @@ class FreqtradeBot:
logger.debug(f"create_trade for pair {pair}") logger.debug(f"create_trade for pair {pair}")
analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(pair, self.strategy.timeframe) analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(pair, self.strategy.timeframe)
if self.strategy.is_pair_locked( nowtime = analyzed_df.iloc[-1]['date'] if len(analyzed_df) > 0 else None
pair, analyzed_df.iloc[-1]['date'] if len(analyzed_df) > 0 else None): if self.strategy.is_pair_locked(pair, nowtime):
logger.info(f"Pair {pair} is currently locked.") lock = PairLocks.get_pair_longest_lock(pair, nowtime)
if lock:
self.log_once(f"Pair {pair} is still locked until "
f"{lock.lock_end_time.strftime(constants.DATETIME_PRINT_FORMAT)}.",
logger.info)
else:
self.log_once(f"Pair {pair} is still locked.", logger.info)
return False return False
# get_free_open_trades is checked before create_trade is called # get_free_open_trades is checked before create_trade is called
@ -616,6 +634,9 @@ class FreqtradeBot:
# Calculate price # Calculate price
buy_limit_requested = self.get_buy_rate(pair, True) buy_limit_requested = self.get_buy_rate(pair, True)
if not buy_limit_requested:
raise PricingError('Could not determine buy price.')
min_stake_amount = self._get_min_pair_stake_amount(pair, buy_limit_requested) min_stake_amount = self._get_min_pair_stake_amount(pair, buy_limit_requested)
if min_stake_amount is not None and min_stake_amount > stake_amount: if min_stake_amount is not None and min_stake_amount > stake_amount:
logger.warning( logger.warning(
@ -1393,7 +1414,7 @@ class FreqtradeBot:
abs_tol=constants.MATH_CLOSE_PREC): abs_tol=constants.MATH_CLOSE_PREC):
order['amount'] = new_amount order['amount'] = new_amount
order.pop('filled', None) order.pop('filled', None)
trade.recalc_open_trade_price() trade.recalc_open_trade_value()
except DependencyException as exception: except DependencyException as exception:
logger.warning("Could not update trade amount: %s", exception) logger.warning("Could not update trade amount: %s", exception)
@ -1405,6 +1426,8 @@ class FreqtradeBot:
# Updating wallets when order is closed # Updating wallets when order is closed
if not trade.is_open: if not trade.is_open:
self.protections.stop_per_pair(trade.pair)
self.protections.global_stop()
self.wallets.update() self.wallets.update()
return False return False
@ -1446,7 +1469,10 @@ class FreqtradeBot:
fee_cost, fee_currency, fee_rate = self.exchange.extract_cost_curr_rate(order) fee_cost, fee_currency, fee_rate = self.exchange.extract_cost_curr_rate(order)
logger.info(f"Fee for Trade {trade} [{order.get('side')}]: " logger.info(f"Fee for Trade {trade} [{order.get('side')}]: "
f"{fee_cost:.8g} {fee_currency} - rate: {fee_rate}") f"{fee_cost:.8g} {fee_currency} - rate: {fee_rate}")
if fee_rate is None or fee_rate < 0.02:
# Reject all fees that report as > 2%.
# These are most likely caused by a parsing bug in ccxt
# due to multiple trades (https://github.com/ccxt/ccxt/issues/8025)
trade.update_fee(fee_cost, fee_currency, fee_rate, order.get('side', '')) trade.update_fee(fee_cost, fee_currency, fee_rate, order.get('side', ''))
if trade_base_currency == fee_currency: if trade_base_currency == fee_currency:
# Apply fee to amount # Apply fee to amount

View File

@ -9,8 +9,8 @@ from typing import Any, List
# check min. python version # check min. python version
if sys.version_info < (3, 6): if sys.version_info < (3, 7):
sys.exit("Freqtrade requires Python version >= 3.6") sys.exit("Freqtrade requires Python version >= 3.7")
from freqtrade.commands import Arguments from freqtrade.commands import Arguments
from freqtrade.exceptions import FreqtradeException, OperationalException from freqtrade.exceptions import FreqtradeException, OperationalException

View File

@ -0,0 +1,2 @@
# flake8: noqa: F401
from freqtrade.mixins.logging_mixin import LoggingMixin

View File

@ -0,0 +1,38 @@
from typing import Callable
from cachetools import TTLCache, cached
class LoggingMixin():
"""
Logging Mixin
Shows similar messages only once every `refresh_period`.
"""
# Disable output completely
show_output = True
def __init__(self, logger, refresh_period: int = 3600):
"""
:param refresh_period: in seconds - Show identical messages in this intervals
"""
self.logger = logger
self.refresh_period = refresh_period
self._log_cache: TTLCache = TTLCache(maxsize=1024, ttl=self.refresh_period)
def log_once(self, message: str, logmethod: Callable) -> None:
"""
Logs message - not more often than "refresh_period" to avoid log spamming
Logs the log-message as debug as well to simplify debugging.
:param message: String containing the message to be sent to the function.
:param logmethod: Function that'll be called. Most likely `logger.info`.
:return: None.
"""
@cached(cache=self._log_cache)
def _log_once(message: str):
logmethod(message)
# Log as debug first
self.logger.debug(message)
# Call hidden function.
if self.show_output:
_log_once(message)

View File

@ -18,10 +18,12 @@ from freqtrade.data.converter import trim_dataframe
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
from freqtrade.mixins import LoggingMixin
from freqtrade.optimize.optimize_reports import (generate_backtest_stats, show_backtest_results, from freqtrade.optimize.optimize_reports import (generate_backtest_stats, show_backtest_results,
store_backtest_stats) store_backtest_stats)
from freqtrade.pairlist.pairlistmanager import PairListManager from freqtrade.persistence import PairLocks, Trade
from freqtrade.persistence import Trade from freqtrade.plugins.pairlistmanager import PairListManager
from freqtrade.plugins.protectionmanager import ProtectionManager
from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.resolvers import ExchangeResolver, StrategyResolver
from freqtrade.strategy.interface import IStrategy, SellCheckTuple, SellType from freqtrade.strategy.interface import IStrategy, SellCheckTuple, SellType
@ -67,6 +69,8 @@ class Backtesting:
""" """
def __init__(self, config: Dict[str, Any]) -> None: def __init__(self, config: Dict[str, Any]) -> None:
LoggingMixin.show_output = False
self.config = config self.config = config
# Reset keys for backtesting # Reset keys for backtesting
@ -98,6 +102,8 @@ class Backtesting:
self.pairlists = PairListManager(self.exchange, self.config) self.pairlists = PairListManager(self.exchange, self.config)
if 'VolumePairList' in self.pairlists.name_list: if 'VolumePairList' in self.pairlists.name_list:
raise OperationalException("VolumePairList not allowed for backtesting.") raise OperationalException("VolumePairList not allowed for backtesting.")
if 'PerformanceFilter' in self.pairlists.name_list:
raise OperationalException("PerformanceFilter not allowed for backtesting.")
if len(self.strategylist) > 1 and 'PrecisionFilter' in self.pairlists.name_list: if len(self.strategylist) > 1 and 'PrecisionFilter' in self.pairlists.name_list:
raise OperationalException( raise OperationalException(
@ -115,11 +121,24 @@ class Backtesting:
else: else:
self.fee = self.exchange.get_fee(symbol=self.pairlists.whitelist[0]) self.fee = self.exchange.get_fee(symbol=self.pairlists.whitelist[0])
Trade.use_db = False
Trade.reset_trades()
PairLocks.timeframe = self.config['timeframe']
PairLocks.use_db = False
PairLocks.reset_locks()
if self.config.get('enable_protections', False):
self.protections = ProtectionManager(self.config)
# Get maximum required startup period # Get maximum required startup period
self.required_startup = max([strat.startup_candle_count for strat in self.strategylist]) self.required_startup = max([strat.startup_candle_count for strat in self.strategylist])
# Load one (first) strategy # Load one (first) strategy
self._set_strategy(self.strategylist[0]) self._set_strategy(self.strategylist[0])
def __del__(self):
LoggingMixin.show_output = True
PairLocks.use_db = True
Trade.use_db = True
def _set_strategy(self, strategy): def _set_strategy(self, strategy):
""" """
Load strategy into backtesting Load strategy into backtesting
@ -156,6 +175,17 @@ class Backtesting:
return data, timerange return data, timerange
def prepare_backtest(self, enable_protections):
"""
Backtesting setup method - called once for every call to "backtest()".
"""
PairLocks.use_db = False
Trade.use_db = False
if enable_protections:
# Reset persisted data - used for protections only
PairLocks.reset_locks()
Trade.reset_trades()
def _get_ohlcv_as_lists(self, processed: Dict[str, DataFrame]) -> Dict[str, Tuple]: def _get_ohlcv_as_lists(self, processed: Dict[str, DataFrame]) -> Dict[str, Tuple]:
""" """
Helper function to convert a processed dataframes into lists for performance reasons. Helper function to convert a processed dataframes into lists for performance reasons.
@ -235,6 +265,10 @@ class Backtesting:
trade_dur = int((sell_row[DATE_IDX] - trade.open_date).total_seconds() // 60) trade_dur = int((sell_row[DATE_IDX] - trade.open_date).total_seconds() // 60)
closerate = self._get_close_rate(sell_row, trade, sell, trade_dur) closerate = self._get_close_rate(sell_row, trade, sell, trade_dur)
trade.close_date = sell_row[DATE_IDX]
trade.sell_reason = sell.sell_type
trade.close(closerate, show_msg=False)
return BacktestResult(pair=trade.pair, return BacktestResult(pair=trade.pair,
profit_percent=trade.calc_profit_ratio(rate=closerate), profit_percent=trade.calc_profit_ratio(rate=closerate),
profit_abs=trade.calc_profit(rate=closerate), profit_abs=trade.calc_profit(rate=closerate),
@ -261,6 +295,7 @@ class Backtesting:
if len(open_trades[pair]) > 0: if len(open_trades[pair]) > 0:
for trade in open_trades[pair]: for trade in open_trades[pair]:
sell_row = data[pair][-1] sell_row = data[pair][-1]
trade_entry = BacktestResult(pair=trade.pair, trade_entry = BacktestResult(pair=trade.pair,
profit_percent=trade.calc_profit_ratio( profit_percent=trade.calc_profit_ratio(
rate=sell_row[OPEN_IDX]), rate=sell_row[OPEN_IDX]),
@ -283,7 +318,8 @@ class Backtesting:
def backtest(self, processed: Dict, stake_amount: float, def backtest(self, processed: Dict, stake_amount: float,
start_date: datetime, end_date: datetime, start_date: datetime, end_date: datetime,
max_open_trades: int = 0, position_stacking: bool = False) -> DataFrame: max_open_trades: int = 0, position_stacking: bool = False,
enable_protections: bool = False) -> DataFrame:
""" """
Implement backtesting functionality Implement backtesting functionality
@ -297,6 +333,7 @@ class Backtesting:
:param end_date: backtesting timerange end datetime :param end_date: backtesting timerange end datetime
:param max_open_trades: maximum number of concurrent trades, <= 0 means unlimited :param max_open_trades: maximum number of concurrent trades, <= 0 means unlimited
:param position_stacking: do we allow position stacking? :param position_stacking: do we allow position stacking?
:param enable_protections: Should protections be enabled?
:return: DataFrame with trades (results of backtesting) :return: DataFrame with trades (results of backtesting)
""" """
logger.debug(f"Run backtest, stake_amount: {stake_amount}, " logger.debug(f"Run backtest, stake_amount: {stake_amount}, "
@ -304,6 +341,7 @@ class Backtesting:
f"max_open_trades: {max_open_trades}, position_stacking: {position_stacking}" f"max_open_trades: {max_open_trades}, position_stacking: {position_stacking}"
) )
trades = [] trades = []
self.prepare_backtest(enable_protections)
# Use dict of lists with data for performance # Use dict of lists with data for performance
# (looping lists is a lot faster than pandas DataFrames) # (looping lists is a lot faster than pandas DataFrames)
@ -342,7 +380,8 @@ class Backtesting:
if ((position_stacking or len(open_trades[pair]) == 0) if ((position_stacking or len(open_trades[pair]) == 0)
and (max_open_trades <= 0 or open_trade_count_start < max_open_trades) and (max_open_trades <= 0 or open_trade_count_start < max_open_trades)
and tmp != end_date and tmp != end_date
and row[BUY_IDX] == 1 and row[SELL_IDX] != 1): and row[BUY_IDX] == 1 and row[SELL_IDX] != 1
and not PairLocks.is_pair_locked(pair, row[DATE_IDX])):
# Enter trade # Enter trade
trade = Trade( trade = Trade(
pair=pair, pair=pair,
@ -361,6 +400,7 @@ class Backtesting:
open_trade_count += 1 open_trade_count += 1
# logger.debug(f"{pair} - Backtesting emulates creation of new trade: {trade}.") # logger.debug(f"{pair} - Backtesting emulates creation of new trade: {trade}.")
open_trades[pair].append(trade) open_trades[pair].append(trade)
Trade.trades.append(trade)
for trade in open_trades[pair]: for trade in open_trades[pair]:
# since indexes has been incremented before, we need to go one step back to # since indexes has been incremented before, we need to go one step back to
@ -372,6 +412,9 @@ class Backtesting:
open_trade_count -= 1 open_trade_count -= 1
open_trades[pair].remove(trade) open_trades[pair].remove(trade)
trades.append(trade_entry) trades.append(trade_entry)
if enable_protections:
self.protections.stop_per_pair(pair, row[DATE_IDX])
self.protections.global_stop(tmp)
# Move time one configured time_interval ahead. # Move time one configured time_interval ahead.
tmp += timedelta(minutes=self.timeframe_min) tmp += timedelta(minutes=self.timeframe_min)
@ -427,10 +470,12 @@ class Backtesting:
end_date=max_date.datetime, end_date=max_date.datetime,
max_open_trades=max_open_trades, max_open_trades=max_open_trades,
position_stacking=position_stacking, position_stacking=position_stacking,
enable_protections=self.config.get('enable_protections', False),
) )
all_results[self.strategy.get_strategy_name()] = { all_results[self.strategy.get_strategy_name()] = {
'results': results, 'results': results,
'config': self.strategy.config, 'config': self.strategy.config,
'locks': PairLocks.locks,
} }
stats = generate_backtest_stats(data, all_results, min_date=min_date, max_date=max_date) stats = generate_backtest_stats(data, all_results, min_date=min_date, max_date=max_date)

View File

@ -542,6 +542,8 @@ class Hyperopt:
end_date=max_date.datetime, end_date=max_date.datetime,
max_open_trades=self.max_open_trades, max_open_trades=self.max_open_trades,
position_stacking=self.position_stacking, position_stacking=self.position_stacking,
enable_protections=self.config.get('enable_protections', False),
) )
return self._get_results_dict(backtesting_results, min_date, max_date, return self._get_results_dict(backtesting_results, min_date, max_date,
params_dict, params_details) params_dict, params_details)

View File

@ -58,16 +58,19 @@ def _generate_result_line(result: DataFrame, max_open_trades: int, first_column:
""" """
Generate one result dict, with "first_column" as key. Generate one result dict, with "first_column" as key.
""" """
profit_sum = result['profit_percent'].sum()
profit_total = profit_sum / max_open_trades
return { return {
'key': first_column, 'key': first_column,
'trades': len(result), 'trades': len(result),
'profit_mean': result['profit_percent'].mean() if len(result) > 0 else 0.0, 'profit_mean': result['profit_percent'].mean() if len(result) > 0 else 0.0,
'profit_mean_pct': result['profit_percent'].mean() * 100.0 if len(result) > 0 else 0.0, 'profit_mean_pct': result['profit_percent'].mean() * 100.0 if len(result) > 0 else 0.0,
'profit_sum': result['profit_percent'].sum(), 'profit_sum': profit_sum,
'profit_sum_pct': result['profit_percent'].sum() * 100.0, 'profit_sum_pct': round(profit_sum * 100.0, 2),
'profit_total_abs': result['profit_abs'].sum(), 'profit_total_abs': result['profit_abs'].sum(),
'profit_total': result['profit_percent'].sum() / max_open_trades, 'profit_total': profit_total,
'profit_total_pct': result['profit_percent'].sum() * 100.0 / max_open_trades, 'profit_total_pct': round(profit_total * 100.0, 2),
'duration_avg': str(timedelta( 'duration_avg': str(timedelta(
minutes=round(result['trade_duration'].mean())) minutes=round(result['trade_duration'].mean()))
) if not result.empty else '0:00', ) if not result.empty else '0:00',
@ -122,8 +125,8 @@ def generate_sell_reason_stats(max_open_trades: int, results: DataFrame) -> List
result = results.loc[results['sell_reason'] == reason] result = results.loc[results['sell_reason'] == reason]
profit_mean = result['profit_percent'].mean() profit_mean = result['profit_percent'].mean()
profit_sum = result["profit_percent"].sum() profit_sum = result['profit_percent'].sum()
profit_percent_tot = result['profit_percent'].sum() / max_open_trades profit_total = profit_sum / max_open_trades
tabular_data.append( tabular_data.append(
{ {
@ -137,8 +140,8 @@ def generate_sell_reason_stats(max_open_trades: int, results: DataFrame) -> List
'profit_sum': profit_sum, 'profit_sum': profit_sum,
'profit_sum_pct': round(profit_sum * 100, 2), 'profit_sum_pct': round(profit_sum * 100, 2),
'profit_total_abs': result['profit_abs'].sum(), 'profit_total_abs': result['profit_abs'].sum(),
'profit_total': profit_percent_tot, 'profit_total': profit_total,
'profit_total_pct': round(profit_percent_tot * 100, 2), 'profit_total_pct': round(profit_total * 100, 2),
} }
) )
return tabular_data return tabular_data
@ -253,13 +256,19 @@ def generate_backtest_stats(btdata: Dict[str, DataFrame],
results=results.loc[results['open_at_end']], results=results.loc[results['open_at_end']],
skip_nan=True) skip_nan=True)
daily_stats = generate_daily_stats(results) daily_stats = generate_daily_stats(results)
best_pair = max([pair for pair in pair_results if pair['key'] != 'TOTAL'],
key=lambda x: x['profit_sum']) if len(pair_results) > 1 else None
worst_pair = min([pair for pair in pair_results if pair['key'] != 'TOTAL'],
key=lambda x: x['profit_sum']) if len(pair_results) > 1 else None
results['open_timestamp'] = results['open_date'].astype(int64) // 1e6 results['open_timestamp'] = results['open_date'].astype(int64) // 1e6
results['close_timestamp'] = results['close_date'].astype(int64) // 1e6 results['close_timestamp'] = results['close_date'].astype(int64) // 1e6
backtest_days = (max_date - min_date).days backtest_days = (max_date - min_date).days
strat_stats = { strat_stats = {
'trades': results.to_dict(orient='records'), 'trades': results.to_dict(orient='records'),
'locks': [lock.to_json() for lock in content['locks']],
'best_pair': best_pair,
'worst_pair': worst_pair,
'results_per_pair': pair_results, 'results_per_pair': pair_results,
'sell_reason_summary': sell_reason_stats, 'sell_reason_summary': sell_reason_stats,
'left_open_trades': left_open_results, 'left_open_trades': left_open_results,
@ -392,17 +401,25 @@ def text_table_strategy(strategy_results, stake_currency: str) -> str:
def text_table_add_metrics(strat_results: Dict) -> str: def text_table_add_metrics(strat_results: Dict) -> str:
if len(strat_results['trades']) > 0: if len(strat_results['trades']) > 0:
min_trade = min(strat_results['trades'], key=lambda x: x['open_date']) best_trade = max(strat_results['trades'], key=lambda x: x['profit_percent'])
worst_trade = min(strat_results['trades'], key=lambda x: x['profit_percent'])
metrics = [ metrics = [
('Backtesting from', strat_results['backtest_start'].strftime(DATETIME_PRINT_FORMAT)), ('Backtesting from', strat_results['backtest_start'].strftime(DATETIME_PRINT_FORMAT)),
('Backtesting to', strat_results['backtest_end'].strftime(DATETIME_PRINT_FORMAT)), ('Backtesting to', strat_results['backtest_end'].strftime(DATETIME_PRINT_FORMAT)),
('Max open trades', strat_results['max_open_trades']), ('Max open trades', strat_results['max_open_trades']),
('', ''), # Empty line to improve readability ('', ''), # Empty line to improve readability
('Total trades', strat_results['total_trades']), ('Total trades', strat_results['total_trades']),
('First trade', min_trade['open_date'].strftime(DATETIME_PRINT_FORMAT)),
('First trade Pair', min_trade['pair']),
('Total Profit %', f"{round(strat_results['profit_total'] * 100, 2)}%"), ('Total Profit %', f"{round(strat_results['profit_total'] * 100, 2)}%"),
('Trades per day', strat_results['trades_per_day']), ('Trades per day', strat_results['trades_per_day']),
('', ''), # Empty line to improve readability
('Best Pair', f"{strat_results['best_pair']['key']} "
f"{round(strat_results['best_pair']['profit_sum_pct'], 2)}%"),
('Worst Pair', f"{strat_results['worst_pair']['key']} "
f"{round(strat_results['worst_pair']['profit_sum_pct'], 2)}%"),
('Best trade', f"{best_trade['pair']} {round(best_trade['profit_percent'] * 100, 2)}%"),
('Worst trade', f"{worst_trade['pair']} "
f"{round(worst_trade['profit_percent'] * 100, 2)}%"),
('Best day', f"{round(strat_results['backtest_best_day'] * 100, 2)}%"), ('Best day', f"{round(strat_results['backtest_best_day'] * 100, 2)}%"),
('Worst day', f"{round(strat_results['backtest_worst_day'] * 100, 2)}%"), ('Worst day', f"{round(strat_results['backtest_worst_day'] * 100, 2)}%"),
('Days win/draw/lose', f"{strat_results['winning_days']} / " ('Days win/draw/lose', f"{strat_results['winning_days']} / "

View File

@ -53,11 +53,11 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
else: else:
timeframe = get_column_def(cols, 'timeframe', 'null') timeframe = get_column_def(cols, 'timeframe', 'null')
open_trade_price = get_column_def(cols, 'open_trade_price', open_trade_value = get_column_def(cols, 'open_trade_value',
f'amount * open_rate * (1 + {fee_open})') f'amount * open_rate * (1 + {fee_open})')
close_profit_abs = get_column_def( close_profit_abs = get_column_def(
cols, 'close_profit_abs', cols, 'close_profit_abs',
f"(amount * close_rate * (1 - {fee_close})) - {open_trade_price}") f"(amount * close_rate * (1 - {fee_close})) - {open_trade_value}")
sell_order_status = get_column_def(cols, 'sell_order_status', 'null') sell_order_status = get_column_def(cols, 'sell_order_status', 'null')
amount_requested = get_column_def(cols, 'amount_requested', 'amount') amount_requested = get_column_def(cols, 'amount_requested', 'amount')
@ -79,7 +79,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct, stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct,
stoploss_order_id, stoploss_last_update, stoploss_order_id, stoploss_last_update,
max_rate, min_rate, sell_reason, sell_order_status, strategy, max_rate, min_rate, sell_reason, sell_order_status, strategy,
timeframe, open_trade_price, close_profit_abs timeframe, open_trade_value, close_profit_abs
) )
select id, lower(exchange), select id, lower(exchange),
case case
@ -102,7 +102,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
{max_rate} max_rate, {min_rate} min_rate, {sell_reason} sell_reason, {max_rate} max_rate, {min_rate} min_rate, {sell_reason} sell_reason,
{sell_order_status} sell_order_status, {sell_order_status} sell_order_status,
{strategy} strategy, {timeframe} timeframe, {strategy} strategy, {timeframe} timeframe,
{open_trade_price} open_trade_price, {close_profit_abs} close_profit_abs {open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs
from {table_back_name} from {table_back_name}
""") """)
@ -134,7 +134,7 @@ def check_migrate(engine, decl_base, previous_tables) -> None:
table_back_name = get_backup_name(tabs, 'trades_bak') table_back_name = get_backup_name(tabs, 'trades_bak')
# Check for latest column # Check for latest column
if not has_column(cols, 'amount_requested'): if not has_column(cols, 'open_trade_value'):
logger.info(f'Running database migration for trades - backup: {table_back_name}') logger.info(f'Running database migration for trades - backup: {table_back_name}')
migrate_trades_table(decl_base, inspector, engine, table_back_name, cols) migrate_trades_table(decl_base, inspector, engine, table_back_name, cols)
# Reread columns - the above recreated the table! # Reread columns - the above recreated the table!

View File

@ -202,6 +202,10 @@ class Trade(_DECL_BASE):
""" """
__tablename__ = 'trades' __tablename__ = 'trades'
use_db: bool = True
# Trades container for backtesting
trades: List['Trade'] = []
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
orders = relationship("Order", order_by="Order.id", cascade="all, delete-orphan") orders = relationship("Order", order_by="Order.id", cascade="all, delete-orphan")
@ -217,8 +221,8 @@ class Trade(_DECL_BASE):
fee_close_currency = Column(String, nullable=True) fee_close_currency = Column(String, nullable=True)
open_rate = Column(Float) open_rate = Column(Float)
open_rate_requested = Column(Float) open_rate_requested = Column(Float)
# open_trade_price - calculated via _calc_open_trade_price # open_trade_value - calculated via _calc_open_trade_value
open_trade_price = Column(Float) open_trade_value = Column(Float)
close_rate = Column(Float) close_rate = Column(Float)
close_rate_requested = Column(Float) close_rate_requested = Column(Float)
close_profit = Column(Float) close_profit = Column(Float)
@ -252,7 +256,7 @@ class Trade(_DECL_BASE):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
self.recalc_open_trade_price() self.recalc_open_trade_value()
def __repr__(self): def __repr__(self):
open_since = self.open_date.strftime(DATETIME_PRINT_FORMAT) if self.is_open else 'closed' open_since = self.open_date.strftime(DATETIME_PRINT_FORMAT) if self.is_open else 'closed'
@ -284,7 +288,7 @@ class Trade(_DECL_BASE):
'open_timestamp': int(self.open_date.replace(tzinfo=timezone.utc).timestamp() * 1000), 'open_timestamp': int(self.open_date.replace(tzinfo=timezone.utc).timestamp() * 1000),
'open_rate': self.open_rate, 'open_rate': self.open_rate,
'open_rate_requested': self.open_rate_requested, 'open_rate_requested': self.open_rate_requested,
'open_trade_price': round(self.open_trade_price, 8), 'open_trade_value': round(self.open_trade_value, 8),
'close_date_hum': (arrow.get(self.close_date).humanize() 'close_date_hum': (arrow.get(self.close_date).humanize()
if self.close_date else None), if self.close_date else None),
@ -323,6 +327,14 @@ class Trade(_DECL_BASE):
'open_order_id': self.open_order_id, 'open_order_id': self.open_order_id,
} }
@staticmethod
def reset_trades() -> None:
"""
Resets all trades. Only active for backtesting mode.
"""
if not Trade.use_db:
Trade.trades = []
def adjust_min_max_rates(self, current_price: float) -> None: def adjust_min_max_rates(self, current_price: float) -> None:
""" """
Adjust the max_rate and min_rate. Adjust the max_rate and min_rate.
@ -389,7 +401,7 @@ class Trade(_DECL_BASE):
# Update open rate and actual amount # Update open rate and actual amount
self.open_rate = Decimal(safe_value_fallback(order, 'average', 'price')) self.open_rate = Decimal(safe_value_fallback(order, 'average', 'price'))
self.amount = Decimal(safe_value_fallback(order, 'filled', 'amount')) self.amount = Decimal(safe_value_fallback(order, 'filled', 'amount'))
self.recalc_open_trade_price() self.recalc_open_trade_value()
if self.is_open: if self.is_open:
logger.info(f'{order_type.upper()}_BUY has been fulfilled for {self}.') logger.info(f'{order_type.upper()}_BUY has been fulfilled for {self}.')
self.open_order_id = None self.open_order_id = None
@ -407,7 +419,7 @@ class Trade(_DECL_BASE):
raise ValueError(f'Unknown order type: {order_type}') raise ValueError(f'Unknown order type: {order_type}')
cleanup_db() cleanup_db()
def close(self, rate: float) -> None: def close(self, rate: float, *, show_msg: bool = True) -> None:
""" """
Sets close_rate to the given rate, calculates total profit Sets close_rate to the given rate, calculates total profit
and marks trade as closed and marks trade as closed
@ -419,6 +431,7 @@ class Trade(_DECL_BASE):
self.is_open = False self.is_open = False
self.sell_order_status = 'closed' self.sell_order_status = 'closed'
self.open_order_id = None self.open_order_id = None
if show_msg:
logger.info( logger.info(
'Marking %s as closed as the trade is fulfilled and found no open orders for it.', 'Marking %s as closed as the trade is fulfilled and found no open orders for it.',
self self
@ -464,7 +477,7 @@ class Trade(_DECL_BASE):
Trade.session.delete(self) Trade.session.delete(self)
Trade.session.flush() Trade.session.flush()
def _calc_open_trade_price(self) -> float: def _calc_open_trade_value(self) -> float:
""" """
Calculate the open_rate including open_fee. Calculate the open_rate including open_fee.
:return: Price in of the open trade incl. Fees :return: Price in of the open trade incl. Fees
@ -473,14 +486,14 @@ class Trade(_DECL_BASE):
fees = buy_trade * Decimal(self.fee_open) fees = buy_trade * Decimal(self.fee_open)
return float(buy_trade + fees) return float(buy_trade + fees)
def recalc_open_trade_price(self) -> None: def recalc_open_trade_value(self) -> None:
""" """
Recalculate open_trade_price. Recalculate open_trade_value.
Must be called whenever open_rate or fee_open is changed. Must be called whenever open_rate or fee_open is changed.
""" """
self.open_trade_price = self._calc_open_trade_price() self.open_trade_value = self._calc_open_trade_value()
def calc_close_trade_price(self, rate: Optional[float] = None, def calc_close_trade_value(self, rate: Optional[float] = None,
fee: Optional[float] = None) -> float: fee: Optional[float] = None) -> float:
""" """
Calculate the close_rate including fee Calculate the close_rate including fee
@ -507,11 +520,11 @@ class Trade(_DECL_BASE):
If rate is not set self.close_rate will be used If rate is not set self.close_rate will be used
:return: profit in stake currency as float :return: profit in stake currency as float
""" """
close_trade_price = self.calc_close_trade_price( close_trade_value = self.calc_close_trade_value(
rate=(rate or self.close_rate), rate=(rate or self.close_rate),
fee=(fee or self.fee_close) fee=(fee or self.fee_close)
) )
profit = close_trade_price - self.open_trade_price profit = close_trade_value - self.open_trade_value
return float(f"{profit:.8f}") return float(f"{profit:.8f}")
def calc_profit_ratio(self, rate: Optional[float] = None, def calc_profit_ratio(self, rate: Optional[float] = None,
@ -523,11 +536,11 @@ class Trade(_DECL_BASE):
:param fee: fee to use on the close rate (optional). :param fee: fee to use on the close rate (optional).
:return: profit ratio as float :return: profit ratio as float
""" """
close_trade_price = self.calc_close_trade_price( close_trade_value = self.calc_close_trade_value(
rate=(rate or self.close_rate), rate=(rate or self.close_rate),
fee=(fee or self.fee_close) fee=(fee or self.fee_close)
) )
profit_ratio = (close_trade_price / self.open_trade_price) - 1 profit_ratio = (close_trade_value / self.open_trade_value) - 1
return float(f"{profit_ratio:.8f}") return float(f"{profit_ratio:.8f}")
def select_order(self, order_side: str, is_open: Optional[bool]) -> Optional[Order]: def select_order(self, order_side: str, is_open: Optional[bool]) -> Optional[Order]:
@ -562,6 +575,43 @@ class Trade(_DECL_BASE):
else: else:
return Trade.query return Trade.query
@staticmethod
def get_trades_proxy(*, pair: str = None, is_open: bool = None,
open_date: datetime = None, close_date: datetime = None,
) -> List['Trade']:
"""
Helper function to query Trades.
Returns a List of trades, filtered on the parameters given.
In live mode, converts the filter to a database query and returns all rows
In Backtest mode, uses filters on Trade.trades to get the result.
:return: unsorted List[Trade]
"""
if Trade.use_db:
trade_filter = []
if pair:
trade_filter.append(Trade.pair == pair)
if open_date:
trade_filter.append(Trade.open_date > open_date)
if close_date:
trade_filter.append(Trade.close_date > close_date)
if is_open is not None:
trade_filter.append(Trade.is_open.is_(is_open))
return Trade.get_trades(trade_filter).all()
else:
# Offline mode - without database
sel_trades = [trade for trade in Trade.trades]
if pair:
sel_trades = [trade for trade in sel_trades if trade.pair == pair]
if open_date:
sel_trades = [trade for trade in sel_trades if trade.open_date > open_date]
if close_date:
sel_trades = [trade for trade in sel_trades if trade.close_date
and trade.close_date > close_date]
if is_open is not None:
sel_trades = [trade for trade in sel_trades if trade.is_open == is_open]
return sel_trades
@staticmethod @staticmethod
def get_open_trades() -> List[Any]: def get_open_trades() -> List[Any]:
""" """
@ -688,7 +738,7 @@ class PairLock(_DECL_BASE):
@staticmethod @staticmethod
def query_pair_locks(pair: Optional[str], now: datetime) -> Query: def query_pair_locks(pair: Optional[str], now: datetime) -> Query:
""" """
Get all locks for this pair Get all currently active locks for this pair
:param pair: Pair to check for. Returns all current locks if pair is empty :param pair: Pair to check for. Returns all current locks if pair is empty
:param now: Datetime object (generated via datetime.now(timezone.utc)). :param now: Datetime object (generated via datetime.now(timezone.utc)).
""" """

View File

@ -22,10 +22,27 @@ class PairLocks():
timeframe: str = '' timeframe: str = ''
@staticmethod @staticmethod
def lock_pair(pair: str, until: datetime, reason: str = None) -> None: def reset_locks() -> None:
"""
Resets all locks. Only active for backtesting mode.
"""
if not PairLocks.use_db:
PairLocks.locks = []
@staticmethod
def lock_pair(pair: str, until: datetime, reason: str = None, *, now: datetime = None) -> None:
"""
Create PairLock from now to "until".
Uses database by default, unless PairLocks.use_db is set to False,
in which case a list is maintained.
:param pair: pair to lock. use '*' to lock all pairs
:param until: End time of the lock. Will be rounded up to the next candle.
:param reason: Reason string that will be shown as reason for the lock
:param now: Current timestamp. Used to determine lock start time.
"""
lock = PairLock( lock = PairLock(
pair=pair, pair=pair,
lock_time=datetime.now(timezone.utc), lock_time=now or datetime.now(timezone.utc),
lock_end_time=timeframe_to_next_date(PairLocks.timeframe, until), lock_end_time=timeframe_to_next_date(PairLocks.timeframe, until),
reason=reason, reason=reason,
active=True active=True
@ -57,6 +74,15 @@ class PairLocks():
)] )]
return locks return locks
@staticmethod
def get_pair_longest_lock(pair: str, now: Optional[datetime] = None) -> Optional[PairLock]:
"""
Get the lock that expires the latest for the pair given.
"""
locks = PairLocks.get_pair_locks(pair, now)
locks = sorted(locks, key=lambda l: l.lock_end_time, reverse=True)
return locks[0] if locks else None
@staticmethod @staticmethod
def unlock_pair(pair: str, now: Optional[datetime] = None) -> None: def unlock_pair(pair: str, now: Optional[datetime] = None) -> None:
""" """

View File

@ -263,6 +263,65 @@ def create_plotconfig(indicators1: List[str], indicators2: List[str],
return plot_config return plot_config
def plot_area(fig, row: int, data: pd.DataFrame, indicator_a: str,
indicator_b: str, label: str = "",
fill_color: str = "rgba(0,176,246,0.2)") -> make_subplots:
""" Creates a plot for the area between two traces and adds it to fig.
:param fig: Plot figure to append to
:param row: row number for this plot
:param data: candlestick DataFrame
:param indicator_a: indicator name as populated in stragetie
:param indicator_b: indicator name as populated in stragetie
:param label: label for the filled area
:param fill_color: color to be used for the filled area
:return: fig with added filled_traces plot
"""
if indicator_a in data and indicator_b in data:
# make lines invisible to get the area plotted, only.
line = {'color': 'rgba(255,255,255,0)'}
# TODO: Figure out why scattergl causes problems plotly/plotly.js#2284
trace_a = go.Scatter(x=data.date, y=data[indicator_a],
showlegend=False,
line=line)
trace_b = go.Scatter(x=data.date, y=data[indicator_b], name=label,
fill="tonexty", fillcolor=fill_color,
line=line)
fig.add_trace(trace_a, row, 1)
fig.add_trace(trace_b, row, 1)
return fig
def add_areas(fig, row: int, data: pd.DataFrame, indicators) -> make_subplots:
""" Adds all area plots (specified in plot_config) to fig.
:param fig: Plot figure to append to
:param row: row number for this plot
:param data: candlestick DataFrame
:param indicators: dict with indicators. ie.: plot_config['main_plot'] or
plot_config['subplots'][subplot_label]
:return: fig with added filled_traces plot
"""
for indicator, ind_conf in indicators.items():
if 'fill_to' in ind_conf:
indicator_b = ind_conf['fill_to']
if indicator in data and indicator_b in data:
label = ind_conf.get('fill_label',
f'{indicator}<>{indicator_b}')
fill_color = ind_conf.get('fill_color', 'rgba(0,176,246,0.2)')
fig = plot_area(fig, row, data, indicator, indicator_b,
label=label, fill_color=fill_color)
elif indicator not in data:
logger.info(
'Indicator "%s" ignored. Reason: This indicator is not '
'found in your strategy.', indicator
)
elif indicator_b not in data:
logger.info(
'fill_to: "%s" ignored. Reason: This indicator is not '
'in your strategy.', indicator_b
)
return fig
def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFrame = None, *, def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFrame = None, *,
indicators1: List[str] = [], indicators1: List[str] = [],
indicators2: List[str] = [], indicators2: List[str] = [],
@ -280,7 +339,6 @@ def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFra
:return: Plotly figure :return: Plotly figure
""" """
plot_config = create_plotconfig(indicators1, indicators2, plot_config) plot_config = create_plotconfig(indicators1, indicators2, plot_config)
rows = 2 + len(plot_config['subplots']) rows = 2 + len(plot_config['subplots'])
row_widths = [1 for _ in plot_config['subplots']] row_widths = [1 for _ in plot_config['subplots']]
# Define the graph # Define the graph
@ -346,36 +404,20 @@ def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFra
fig.add_trace(sells, 1, 1) fig.add_trace(sells, 1, 1)
else: else:
logger.warning("No sell-signals found.") logger.warning("No sell-signals found.")
# Add Bollinger Bands
# TODO: Figure out why scattergl causes problems plotly/plotly.js#2284 fig = plot_area(fig, 1, data, 'bb_lowerband', 'bb_upperband',
if 'bb_lowerband' in data and 'bb_upperband' in data: label="Bollinger Band")
bb_lower = go.Scatter( # prevent bb_lower and bb_upper from plotting
x=data.date, try:
y=data.bb_lowerband,
showlegend=False,
line={'color': 'rgba(255,255,255,0)'},
)
bb_upper = go.Scatter(
x=data.date,
y=data.bb_upperband,
name='Bollinger Band',
fill="tonexty",
fillcolor="rgba(0,176,246,0.2)",
line={'color': 'rgba(255,255,255,0)'},
)
fig.add_trace(bb_lower, 1, 1)
fig.add_trace(bb_upper, 1, 1)
if ('bb_upperband' in plot_config['main_plot']
and 'bb_lowerband' in plot_config['main_plot']):
del plot_config['main_plot']['bb_upperband']
del plot_config['main_plot']['bb_lowerband'] del plot_config['main_plot']['bb_lowerband']
del plot_config['main_plot']['bb_upperband']
# Add indicators to main plot except KeyError:
pass
# main plot goes to row 1
fig = add_indicators(fig=fig, row=1, indicators=plot_config['main_plot'], data=data) fig = add_indicators(fig=fig, row=1, indicators=plot_config['main_plot'], data=data)
fig = add_areas(fig, 1, data, plot_config['main_plot'])
fig = plot_trades(fig, trades) fig = plot_trades(fig, trades)
# sub plot: Volume goes to row 2
# Volume goes to row 2
volume = go.Bar( volume = go.Bar(
x=data['date'], x=data['date'],
y=data['volume'], y=data['volume'],
@ -384,13 +426,14 @@ def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFra
marker_line_color='DarkSlateGrey' marker_line_color='DarkSlateGrey'
) )
fig.add_trace(volume, 2, 1) fig.add_trace(volume, 2, 1)
# add each sub plot to a separate row
# Add indicators to separate row for i, label in enumerate(plot_config['subplots']):
for i, name in enumerate(plot_config['subplots']): sub_config = plot_config['subplots'][label]
fig = add_indicators(fig=fig, row=3 + i, row = 3 + i
indicators=plot_config['subplots'][name], fig = add_indicators(fig=fig, row=row, indicators=sub_config,
data=data) data=data)
# fill area between indicators ( 'fill_to': 'other_indicator')
fig = add_areas(fig, row, data, sub_config)
return fig return fig

View File

@ -2,13 +2,15 @@
Minimum age (days listed) pair list filter Minimum age (days listed) pair list filter
""" """
import logging import logging
from typing import Any, Dict from copy import deepcopy
from typing import Any, Dict, List, Optional
import arrow import arrow
from pandas import DataFrame
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.misc import plural from freqtrade.misc import plural
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -40,7 +42,7 @@ class AgeFilter(IPairList):
If no Pairlist requires tickers, an empty Dict is passed If no Pairlist requires tickers, an empty Dict is passed
as tickers argument to filter_pairlist as tickers argument to filter_pairlist
""" """
return True return False
def short_desc(self) -> str: def short_desc(self) -> str:
""" """
@ -49,36 +51,49 @@ class AgeFilter(IPairList):
return (f"{self.name} - Filtering pairs with age less than " return (f"{self.name} - Filtering pairs with age less than "
f"{self._min_days_listed} {plural(self._min_days_listed, 'day')}.") f"{self._min_days_listed} {plural(self._min_days_listed, 'day')}.")
def _validate_pair(self, ticker: Dict) -> bool: def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]:
""" """
Validate age for the ticker :param pairlist: pairlist to filter or sort
:param ticker: ticker dict as returned from ccxt.load_markets() :param tickers: Tickers (from exchange.get_tickers()). May be cached.
:return: True if the pair can stay, False if it should be removed :return: new allowlist
""" """
needed_pairs = [(p, '1d') for p in pairlist if p not in self._symbolsChecked]
# Check symbol in cache if not needed_pairs:
if ticker['symbol'] in self._symbolsChecked: return pairlist
return True
since_ms = int(arrow.utcnow() since_ms = int(arrow.utcnow()
.floor('day') .floor('day')
.shift(days=-self._min_days_listed) .shift(days=-self._min_days_listed - 1)
.float_timestamp) * 1000 .float_timestamp) * 1000
candles = self._exchange.refresh_latest_ohlcv(needed_pairs, since_ms=since_ms, cache=False)
if self._enabled:
for p in deepcopy(pairlist):
daily_candles = candles[(p, '1d')] if (p, '1d') in candles else None
if not self._validate_pair_loc(p, daily_candles):
pairlist.remove(p)
logger.info(f"Validated {len(pairlist)} pairs.")
return pairlist
daily_candles = self._exchange.get_historic_ohlcv(pair=ticker['symbol'], def _validate_pair_loc(self, pair: str, daily_candles: Optional[DataFrame]) -> bool:
timeframe='1d', """
since_ms=since_ms) Validate age for the ticker
:param pair: Pair that's currently validated
:param ticker: ticker dict as returned from ccxt.load_markets()
:return: True if the pair can stay, false if it should be removed
"""
# Check symbol in cache
if pair in self._symbolsChecked:
return True
if daily_candles is not None: if daily_candles is not None:
if len(daily_candles) > self._min_days_listed: if len(daily_candles) > self._min_days_listed:
# We have fetched at least the minimum required number of daily candles # We have fetched at least the minimum required number of daily candles
# Add to cache, store the time we last checked this symbol # Add to cache, store the time we last checked this symbol
self._symbolsChecked[ticker['symbol']] = int(arrow.utcnow().float_timestamp) * 1000 self._symbolsChecked[pair] = int(arrow.utcnow().float_timestamp) * 1000
return True return True
else: else:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, " self.log_once(f"Removed {pair} from whitelist, because age "
f"because age {len(daily_candles)} is less than " f"{len(daily_candles)} is less than {self._min_days_listed} "
f"{self._min_days_listed} " f"{plural(self._min_days_listed, 'day')}", logger.info)
f"{plural(self._min_days_listed, 'day')}")
return False return False
return False return False

View File

@ -6,16 +6,15 @@ from abc import ABC, abstractmethod, abstractproperty
from copy import deepcopy from copy import deepcopy
from typing import Any, Dict, List from typing import Any, Dict, List
from cachetools import TTLCache, cached
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange import market_is_active from freqtrade.exchange import market_is_active
from freqtrade.mixins import LoggingMixin
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class IPairList(ABC): class IPairList(LoggingMixin, ABC):
def __init__(self, exchange, pairlistmanager, def __init__(self, exchange, pairlistmanager,
config: Dict[str, Any], pairlistconfig: Dict[str, Any], config: Dict[str, Any], pairlistconfig: Dict[str, Any],
@ -36,7 +35,7 @@ class IPairList(ABC):
self._pairlist_pos = pairlist_pos self._pairlist_pos = pairlist_pos
self.refresh_period = self._pairlistconfig.get('refresh_period', 1800) self.refresh_period = self._pairlistconfig.get('refresh_period', 1800)
self._last_refresh = 0 self._last_refresh = 0
self._log_cache: TTLCache = TTLCache(maxsize=1024, ttl=self.refresh_period) LoggingMixin.__init__(self, logger, self.refresh_period)
@property @property
def name(self) -> str: def name(self) -> str:
@ -46,24 +45,6 @@ class IPairList(ABC):
""" """
return self.__class__.__name__ return self.__class__.__name__
def log_on_refresh(self, logmethod, message: str) -> None:
"""
Logs message - not more often than "refresh_period" to avoid log spamming
Logs the log-message as debug as well to simplify debugging.
:param logmethod: Function that'll be called. Most likely `logger.info`.
:param message: String containing the message to be sent to the function.
:return: None.
"""
@cached(cache=self._log_cache)
def _log_on_refresh(message: str):
logmethod(message)
# Log as debug first
logger.debug(message)
# Call hidden function.
_log_on_refresh(message)
@abstractproperty @abstractproperty
def needstickers(self) -> bool: def needstickers(self) -> bool:
""" """
@ -79,13 +60,14 @@ class IPairList(ABC):
-> Please overwrite in subclasses -> Please overwrite in subclasses
""" """
def _validate_pair(self, ticker) -> bool: def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool:
""" """
Check one pair against Pairlist Handler's specific conditions. Check one pair against Pairlist Handler's specific conditions.
Either implement it in the Pairlist Handler or override the generic Either implement it in the Pairlist Handler or override the generic
filter_pairlist() method. filter_pairlist() method.
:param pair: Pair that's currently validated
:param ticker: ticker dict as returned from ccxt.load_markets() :param ticker: ticker dict as returned from ccxt.load_markets()
:return: True if the pair can stay, false if it should be removed :return: True if the pair can stay, false if it should be removed
""" """
@ -128,7 +110,7 @@ class IPairList(ABC):
# Copy list since we're modifying this list # Copy list since we're modifying this list
for p in deepcopy(pairlist): for p in deepcopy(pairlist):
# Filter out assets # Filter out assets
if not self._validate_pair(tickers[p]): if not self._validate_pair(p, tickers[p] if p in tickers else {}):
pairlist.remove(p) pairlist.remove(p)
return pairlist return pairlist

View File

@ -0,0 +1,66 @@
"""
Performance pair list filter
"""
import logging
from typing import Any, Dict, List
import pandas as pd
from freqtrade.persistence import Trade
from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__)
class PerformanceFilter(IPairList):
def __init__(self, exchange, pairlistmanager,
config: Dict[str, Any], pairlistconfig: Dict[str, Any],
pairlist_pos: int) -> None:
super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos)
@property
def needstickers(self) -> bool:
"""
Boolean property defining if tickers are necessary.
If no Pairlist requries tickers, an empty List is passed
as tickers argument to filter_pairlist
"""
return False
def short_desc(self) -> str:
"""
Short allowlist method description - used for startup-messages
"""
return f"{self.name} - Sorting pairs by performance."
def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]:
"""
Filters and sorts pairlist and returns the allowlist again.
Called on each bot iteration - please use internal caching if necessary
:param pairlist: pairlist to filter or sort
:param tickers: Tickers (from exchange.get_tickers()). May be cached.
:return: new allowlist
"""
# Get the trading performance for pairs from database
performance = pd.DataFrame(Trade.get_overall_performance())
# Skip performance-based sorting if no performance data is available
if len(performance) == 0:
return pairlist
# Get pairlist from performance dataframe values
list_df = pd.DataFrame({'pair': pairlist})
# Set initial value for pairs with no trades to 0
# Sort the list using:
# - primarily performance (high to low)
# - then count (low to high, so as to favor same performance with fewer trades)
# - then pair name alphametically
sorted_df = list_df.merge(performance, on='pair', how='left')\
.fillna(0).sort_values(by=['count', 'pair'], ascending=True)\
.sort_values(by=['profit'], ascending=False)
pairlist = sorted_df['pair'].tolist()
return pairlist

View File

@ -5,7 +5,7 @@ import logging
from typing import Any, Dict from typing import Any, Dict
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -43,25 +43,25 @@ class PrecisionFilter(IPairList):
""" """
return f"{self.name} - Filtering untradable pairs." return f"{self.name} - Filtering untradable pairs."
def _validate_pair(self, ticker: dict) -> bool: def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool:
""" """
Check if pair has enough room to add a stoploss to avoid "unsellable" buys of very Check if pair has enough room to add a stoploss to avoid "unsellable" buys of very
low value pairs. low value pairs.
:param pair: Pair that's currently validated
:param ticker: ticker dict as returned from ccxt.load_markets() :param ticker: ticker dict as returned from ccxt.load_markets()
:return: True if the pair can stay, False if it should be removed :return: True if the pair can stay, false if it should be removed
""" """
stop_price = ticker['ask'] * self._stoploss stop_price = ticker['ask'] * self._stoploss
# Adjust stop-prices to precision # Adjust stop-prices to precision
sp = self._exchange.price_to_precision(ticker["symbol"], stop_price) sp = self._exchange.price_to_precision(pair, stop_price)
stop_gap_price = self._exchange.price_to_precision(ticker["symbol"], stop_price * 0.99) stop_gap_price = self._exchange.price_to_precision(pair, stop_price * 0.99)
logger.debug(f"{ticker['symbol']} - {sp} : {stop_gap_price}") logger.debug(f"{ticker['symbol']} - {sp} : {stop_gap_price}")
if sp <= stop_gap_price: if sp <= stop_gap_price:
self.log_on_refresh(logger.info, self.log_once(f"Removed {ticker['symbol']} from whitelist, because "
f"Removed {ticker['symbol']} from whitelist, " f"stop price {sp} would be <= stop limit {stop_gap_price}", logger.info)
f"because stop price {sp} would be <= stop limit {stop_gap_price}")
return False return False
return True return True

View File

@ -5,7 +5,7 @@ import logging
from typing import Any, Dict from typing import Any, Dict
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -57,39 +57,40 @@ class PriceFilter(IPairList):
return f"{self.name} - No price filters configured." return f"{self.name} - No price filters configured."
def _validate_pair(self, ticker) -> bool: def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool:
""" """
Check if if one price-step (pip) is > than a certain barrier. Check if if one price-step (pip) is > than a certain barrier.
:param pair: Pair that's currently validated
:param ticker: ticker dict as returned from ccxt.load_markets() :param ticker: ticker dict as returned from ccxt.load_markets()
:return: True if the pair can stay, false if it should be removed :return: True if the pair can stay, false if it should be removed
""" """
if ticker['last'] is None or ticker['last'] == 0: if ticker['last'] is None or ticker['last'] == 0:
self.log_on_refresh(logger.info, self.log_once(f"Removed {pair} from whitelist, because "
f"Removed {ticker['symbol']} from whitelist, because " "ticker['last'] is empty (Usually no trade in the last 24h).",
"ticker['last'] is empty (Usually no trade in the last 24h).") logger.info)
return False return False
# Perform low_price_ratio check. # Perform low_price_ratio check.
if self._low_price_ratio != 0: if self._low_price_ratio != 0:
compare = self._exchange.price_get_one_pip(ticker['symbol'], ticker['last']) compare = self._exchange.price_get_one_pip(pair, ticker['last'])
changeperc = compare / ticker['last'] changeperc = compare / ticker['last']
if changeperc > self._low_price_ratio: if changeperc > self._low_price_ratio:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, " self.log_once(f"Removed {pair} from whitelist, "
f"because 1 unit is {changeperc * 100:.3f}%") f"because 1 unit is {changeperc * 100:.3f}%", logger.info)
return False return False
# Perform min_price check. # Perform min_price check.
if self._min_price != 0: if self._min_price != 0:
if ticker['last'] < self._min_price: if ticker['last'] < self._min_price:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, " self.log_once(f"Removed {pair} from whitelist, "
f"because last price < {self._min_price:.8f}") f"because last price < {self._min_price:.8f}", logger.info)
return False return False
# Perform max_price check. # Perform max_price check.
if self._max_price != 0: if self._max_price != 0:
if ticker['last'] > self._max_price: if ticker['last'] > self._max_price:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, " self.log_once(f"Removed {ticker['symbol']} from whitelist, "
f"because last price > {self._max_price:.8f}") f"because last price > {self._max_price:.8f}", logger.info)
return False return False
return True return True

View File

@ -5,7 +5,7 @@ import logging
import random import random
from typing import Any, Dict, List from typing import Any, Dict, List
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -4,7 +4,7 @@ Spread pair list filter
import logging import logging
from typing import Any, Dict from typing import Any, Dict
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -36,18 +36,19 @@ class SpreadFilter(IPairList):
return (f"{self.name} - Filtering pairs with ask/bid diff above " return (f"{self.name} - Filtering pairs with ask/bid diff above "
f"{self._max_spread_ratio * 100}%.") f"{self._max_spread_ratio * 100}%.")
def _validate_pair(self, ticker: dict) -> bool: def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool:
""" """
Validate spread for the ticker Validate spread for the ticker
:param pair: Pair that's currently validated
:param ticker: ticker dict as returned from ccxt.load_markets() :param ticker: ticker dict as returned from ccxt.load_markets()
:return: True if the pair can stay, False if it should be removed :return: True if the pair can stay, false if it should be removed
""" """
if 'bid' in ticker and 'ask' in ticker: if 'bid' in ticker and 'ask' in ticker:
spread = 1 - ticker['bid'] / ticker['ask'] spread = 1 - ticker['bid'] / ticker['ask']
if spread > self._max_spread_ratio: if spread > self._max_spread_ratio:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, " self.log_once(f"Removed {pair} from whitelist, because spread "
f"because spread {spread * 100:.3f}% >" f"{spread * 100:.3f}% > {self._max_spread_ratio * 100}%",
f"{self._max_spread_ratio * 100}%") logger.info)
return False return False
else: else:
return True return True

View File

@ -7,7 +7,7 @@ import logging
from typing import Any, Dict, List from typing import Any, Dict, List
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -8,7 +8,7 @@ from datetime import datetime
from typing import Any, Dict, List from typing import Any, Dict, List
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -111,6 +111,6 @@ class VolumePairList(IPairList):
# Limit pairlist to the requested number of pairs # Limit pairlist to the requested number of pairs
pairs = pairs[:self._number_pairs] pairs = pairs[:self._number_pairs]
self.log_on_refresh(logger.info, f"Searching {self._number_pairs} pairs: {pairs}") self.log_once(f"Searching {self._number_pairs} pairs: {pairs}", logger.info)
return pairs return pairs

View File

@ -2,14 +2,16 @@
Rate of change pairlist filter Rate of change pairlist filter
""" """
import logging import logging
from typing import Any, Dict from copy import deepcopy
from typing import Any, Dict, List, Optional
import arrow import arrow
from cachetools.ttl import TTLCache from cachetools.ttl import TTLCache
from pandas import DataFrame
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.misc import plural from freqtrade.misc import plural
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -42,7 +44,7 @@ class RangeStabilityFilter(IPairList):
If no Pairlist requires tickers, an empty List is passed If no Pairlist requires tickers, an empty List is passed
as tickers argument to filter_pairlist as tickers argument to filter_pairlist
""" """
return True return False
def short_desc(self) -> str: def short_desc(self) -> str:
""" """
@ -51,25 +53,43 @@ class RangeStabilityFilter(IPairList):
return (f"{self.name} - Filtering pairs with rate of change below " return (f"{self.name} - Filtering pairs with rate of change below "
f"{self._min_rate_of_change} over the last {plural(self._days, 'day')}.") f"{self._min_rate_of_change} over the last {plural(self._days, 'day')}.")
def _validate_pair(self, ticker: Dict) -> bool: def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]:
""" """
Validate trading range Validate trading range
:param ticker: ticker dict as returned from ccxt.load_markets() :param pairlist: pairlist to filter or sort
:return: True if the pair can stay, False if it should be removed :param tickers: Tickers (from exchange.get_tickers()). May be cached.
:return: new allowlist
"""
needed_pairs = [(p, '1d') for p in pairlist if p not in self._pair_cache]
since_ms = int(arrow.utcnow()
.floor('day')
.shift(days=-self._days - 1)
.float_timestamp) * 1000
# Get all candles
candles = {}
if needed_pairs:
candles = self._exchange.refresh_latest_ohlcv(needed_pairs, since_ms=since_ms,
cache=False)
if self._enabled:
for p in deepcopy(pairlist):
daily_candles = candles[(p, '1d')] if (p, '1d') in candles else None
if not self._validate_pair_loc(p, daily_candles):
pairlist.remove(p)
return pairlist
def _validate_pair_loc(self, pair: str, daily_candles: Optional[DataFrame]) -> bool:
"""
Validate trading range
:param pair: Pair that's currently validated
:param ticker: ticker dict as returned from ccxt.load_markets()
:return: True if the pair can stay, false if it should be removed
""" """
pair = ticker['symbol']
# Check symbol in cache # Check symbol in cache
if pair in self._pair_cache: if pair in self._pair_cache:
return self._pair_cache[pair] return self._pair_cache[pair]
since_ms = int(arrow.utcnow()
.floor('day')
.shift(days=-self._days)
.float_timestamp) * 1000
daily_candles = self._exchange.get_historic_ohlcv_as_df(pair=pair,
timeframe='1d',
since_ms=since_ms)
result = False result = False
if daily_candles is not None and not daily_candles.empty: if daily_candles is not None and not daily_candles.empty:
highest_high = daily_candles['high'].max() highest_high = daily_candles['high'].max()
@ -78,11 +98,10 @@ class RangeStabilityFilter(IPairList):
if pct_change >= self._min_rate_of_change: if pct_change >= self._min_rate_of_change:
result = True result = True
else: else:
self.log_on_refresh(logger.info, self.log_once(f"Removed {pair} from whitelist, because rate of change "
f"Removed {pair} from whitelist, " f"over {self._days} {plural(self._days, 'day')} is {pct_change:.3f}, "
f"because rate of change over {plural(self._days, 'day')} is " f"which is below the threshold of {self._min_rate_of_change}.",
f"{pct_change:.3f}, which is below the " logger.info)
f"threshold of {self._min_rate_of_change}.")
result = False result = False
self._pair_cache[pair] = result self._pair_cache[pair] = result

View File

@ -3,13 +3,13 @@ PairList manager class
""" """
import logging import logging
from copy import deepcopy from copy import deepcopy
from typing import Dict, List from typing import Any, Dict, List
from cachetools import TTLCache, cached from cachetools import TTLCache, cached
from freqtrade.constants import ListPairsWithTimeframes from freqtrade.constants import ListPairsWithTimeframes
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
from freqtrade.resolvers import PairListResolver from freqtrade.resolvers import PairListResolver
@ -26,9 +26,6 @@ class PairListManager():
self._pairlist_handlers: List[IPairList] = [] self._pairlist_handlers: List[IPairList] = []
self._tickers_needed = False self._tickers_needed = False
for pairlist_handler_config in self._config.get('pairlists', None): for pairlist_handler_config in self._config.get('pairlists', None):
if 'method' not in pairlist_handler_config:
logger.warning(f"No method found in {pairlist_handler_config}, ignoring.")
continue
pairlist_handler = PairListResolver.load_pairlist( pairlist_handler = PairListResolver.load_pairlist(
pairlist_handler_config['method'], pairlist_handler_config['method'],
exchange=exchange, exchange=exchange,
@ -100,7 +97,7 @@ class PairListManager():
self._whitelist = pairlist self._whitelist = pairlist
def _prepare_whitelist(self, pairlist: List[str], tickers) -> List[str]: def _prepare_whitelist(self, pairlist: List[str], tickers: Dict[str, Any]) -> List[str]:
""" """
Prepare sanitized pairlist for Pairlist Handlers that use tickers data - remove Prepare sanitized pairlist for Pairlist Handlers that use tickers data - remove
pairs that do not have ticker available pairs that do not have ticker available

View File

@ -0,0 +1,72 @@
"""
Protection manager class
"""
import logging
from datetime import datetime, timezone
from typing import Dict, List, Optional
from freqtrade.persistence import PairLocks
from freqtrade.plugins.protections import IProtection
from freqtrade.resolvers import ProtectionResolver
logger = logging.getLogger(__name__)
class ProtectionManager():
def __init__(self, config: dict) -> None:
self._config = config
self._protection_handlers: List[IProtection] = []
for protection_handler_config in self._config.get('protections', []):
protection_handler = ProtectionResolver.load_protection(
protection_handler_config['method'],
config=config,
protection_config=protection_handler_config,
)
self._protection_handlers.append(protection_handler)
if not self._protection_handlers:
logger.info("No protection Handlers defined.")
@property
def name_list(self) -> List[str]:
"""
Get list of loaded Protection Handler names
"""
return [p.name for p in self._protection_handlers]
def short_desc(self) -> List[Dict]:
"""
List of short_desc for each Pairlist Handler
"""
return [{p.name: p.short_desc()} for p in self._protection_handlers]
def global_stop(self, now: Optional[datetime] = None) -> bool:
if not now:
now = datetime.now(timezone.utc)
result = False
for protection_handler in self._protection_handlers:
if protection_handler.has_global_stop:
result, until, reason = protection_handler.global_stop(now)
# Early stopping - first positive result blocks further trades
if result and until:
if not PairLocks.is_global_lock(until):
PairLocks.lock_pair('*', until, reason, now=now)
result = True
return result
def stop_per_pair(self, pair, now: Optional[datetime] = None) -> bool:
if not now:
now = datetime.now(timezone.utc)
result = False
for protection_handler in self._protection_handlers:
if protection_handler.has_local_stop:
result, until, reason = protection_handler.stop_per_pair(pair, now)
if result and until:
if not PairLocks.is_pair_locked(pair, until):
PairLocks.lock_pair(pair, until, reason, now=now)
result = True
return result

View File

@ -0,0 +1,2 @@
# flake8: noqa: F401
from freqtrade.plugins.protections.iprotection import IProtection, ProtectionReturn

View File

@ -0,0 +1,72 @@
import logging
from datetime import datetime, timedelta
from typing import Any, Dict
from freqtrade.persistence import Trade
from freqtrade.plugins.protections import IProtection, ProtectionReturn
logger = logging.getLogger(__name__)
class CooldownPeriod(IProtection):
has_global_stop: bool = False
has_local_stop: bool = True
def __init__(self, config: Dict[str, Any], protection_config: Dict[str, Any]) -> None:
super().__init__(config, protection_config)
def _reason(self) -> str:
"""
LockReason to use
"""
return (f'Cooldown period for {self.stop_duration_str}.')
def short_desc(self) -> str:
"""
Short method description - used for startup-messages
"""
return (f"{self.name} - Cooldown period of {self.stop_duration_str}.")
def _cooldown_period(self, pair: str, date_now: datetime, ) -> ProtectionReturn:
"""
Get last trade for this pair
"""
look_back_until = date_now - timedelta(minutes=self._stop_duration)
# filters = [
# Trade.is_open.is_(False),
# Trade.close_date > look_back_until,
# Trade.pair == pair,
# ]
# trade = Trade.get_trades(filters).first()
trades = Trade.get_trades_proxy(pair=pair, is_open=False, close_date=look_back_until)
if trades:
# Get latest trade
trade = sorted(trades, key=lambda t: t.close_date)[-1]
self.log_once(f"Cooldown for {pair} for {self.stop_duration_str}.", logger.info)
until = self.calculate_lock_end([trade], self._stop_duration)
return True, until, self._reason()
return False, None, None
def global_stop(self, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for all pairs
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, all pairs will be locked with <reason> until <until>
"""
# Not implemented for cooldown period.
return False, None, None
def stop_per_pair(self, pair: str, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for this pair
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, this pair will be locked with <reason> until <until>
"""
return self._cooldown_period(pair, date_now)

View File

@ -0,0 +1,107 @@
import logging
from abc import ABC, abstractmethod
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional, Tuple
from freqtrade.exchange import timeframe_to_minutes
from freqtrade.misc import plural
from freqtrade.mixins import LoggingMixin
from freqtrade.persistence import Trade
logger = logging.getLogger(__name__)
ProtectionReturn = Tuple[bool, Optional[datetime], Optional[str]]
class IProtection(LoggingMixin, ABC):
# Can globally stop the bot
has_global_stop: bool = False
# Can stop trading for one pair
has_local_stop: bool = False
def __init__(self, config: Dict[str, Any], protection_config: Dict[str, Any]) -> None:
self._config = config
self._protection_config = protection_config
tf_in_min = timeframe_to_minutes(config['timeframe'])
if 'stop_duration_candles' in protection_config:
self._stop_duration_candles = protection_config.get('stop_duration_candles', 1)
self._stop_duration = (tf_in_min * self._stop_duration_candles)
else:
self._stop_duration_candles = None
self._stop_duration = protection_config.get('stop_duration', 60)
if 'lookback_period_candles' in protection_config:
self._lookback_period_candles = protection_config.get('lookback_period_candles', 1)
self._lookback_period = tf_in_min * self._lookback_period_candles
else:
self._lookback_period_candles = None
self._lookback_period = protection_config.get('lookback_period', 60)
LoggingMixin.__init__(self, logger)
@property
def name(self) -> str:
return self.__class__.__name__
@property
def stop_duration_str(self) -> str:
"""
Output configured stop duration in either candles or minutes
"""
if self._stop_duration_candles:
return (f"{self._stop_duration_candles} "
f"{plural(self._stop_duration_candles, 'candle', 'candles')}")
else:
return (f"{self._stop_duration} "
f"{plural(self._stop_duration, 'minute', 'minutes')}")
@property
def lookback_period_str(self) -> str:
"""
Output configured lookback period in either candles or minutes
"""
if self._lookback_period_candles:
return (f"{self._lookback_period_candles} "
f"{plural(self._lookback_period_candles, 'candle', 'candles')}")
else:
return (f"{self._lookback_period} "
f"{plural(self._lookback_period, 'minute', 'minutes')}")
@abstractmethod
def short_desc(self) -> str:
"""
Short method description - used for startup-messages
-> Please overwrite in subclasses
"""
@abstractmethod
def global_stop(self, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for all pairs
This must evaluate to true for the whole period of the "cooldown period".
"""
@abstractmethod
def stop_per_pair(self, pair: str, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for this pair
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, this pair will be locked with <reason> until <until>
"""
@staticmethod
def calculate_lock_end(trades: List[Trade], stop_minutes: int) -> datetime:
"""
Get lock end time
"""
max_date: datetime = max([trade.close_date for trade in trades])
# comming from Database, tzinfo is not set.
if max_date.tzinfo is None:
max_date = max_date.replace(tzinfo=timezone.utc)
until = max_date + timedelta(minutes=stop_minutes)
return until

View File

@ -0,0 +1,83 @@
import logging
from datetime import datetime, timedelta
from typing import Any, Dict
from freqtrade.persistence import Trade
from freqtrade.plugins.protections import IProtection, ProtectionReturn
logger = logging.getLogger(__name__)
class LowProfitPairs(IProtection):
has_global_stop: bool = False
has_local_stop: bool = True
def __init__(self, config: Dict[str, Any], protection_config: Dict[str, Any]) -> None:
super().__init__(config, protection_config)
self._trade_limit = protection_config.get('trade_limit', 1)
self._required_profit = protection_config.get('required_profit', 0.0)
def short_desc(self) -> str:
"""
Short method description - used for startup-messages
"""
return (f"{self.name} - Low Profit Protection, locks pairs with "
f"profit < {self._required_profit} within {self.lookback_period_str}.")
def _reason(self, profit: float) -> str:
"""
LockReason to use
"""
return (f'{profit} < {self._required_profit} in {self.lookback_period_str}, '
f'locking for {self.stop_duration_str}.')
def _low_profit(self, date_now: datetime, pair: str) -> ProtectionReturn:
"""
Evaluate recent trades for pair
"""
look_back_until = date_now - timedelta(minutes=self._lookback_period)
# filters = [
# Trade.is_open.is_(False),
# Trade.close_date > look_back_until,
# ]
# if pair:
# filters.append(Trade.pair == pair)
trades = Trade.get_trades_proxy(pair=pair, is_open=False, close_date=look_back_until)
# trades = Trade.get_trades(filters).all()
if len(trades) < self._trade_limit:
# Not enough trades in the relevant period
return False, None, None
profit = sum(trade.close_profit for trade in trades)
if profit < self._required_profit:
self.log_once(
f"Trading for {pair} stopped due to {profit:.2f} < {self._required_profit} "
f"within {self._lookback_period} minutes.", logger.info)
until = self.calculate_lock_end(trades, self._stop_duration)
return True, until, self._reason(profit)
return False, None, None
def global_stop(self, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for all pairs
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, all pairs will be locked with <reason> until <until>
"""
return False, None, None
def stop_per_pair(self, pair: str, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for this pair
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, this pair will be locked with <reason> until <until>
"""
return self._low_profit(date_now, pair=pair)

View File

@ -0,0 +1,88 @@
import logging
from datetime import datetime, timedelta
from typing import Any, Dict
import pandas as pd
from freqtrade.data.btanalysis import calculate_max_drawdown
from freqtrade.persistence import Trade
from freqtrade.plugins.protections import IProtection, ProtectionReturn
logger = logging.getLogger(__name__)
class MaxDrawdown(IProtection):
has_global_stop: bool = True
has_local_stop: bool = False
def __init__(self, config: Dict[str, Any], protection_config: Dict[str, Any]) -> None:
super().__init__(config, protection_config)
self._trade_limit = protection_config.get('trade_limit', 1)
self._max_allowed_drawdown = protection_config.get('max_allowed_drawdown', 0.0)
# TODO: Implement checks to limit max_drawdown to sensible values
def short_desc(self) -> str:
"""
Short method description - used for startup-messages
"""
return (f"{self.name} - Max drawdown protection, stop trading if drawdown is > "
f"{self._max_allowed_drawdown} within {self.lookback_period_str}.")
def _reason(self, drawdown: float) -> str:
"""
LockReason to use
"""
return (f'{drawdown} > {self._max_allowed_drawdown} in {self.lookback_period_str}, '
f'locking for {self.stop_duration_str}.')
def _max_drawdown(self, date_now: datetime) -> ProtectionReturn:
"""
Evaluate recent trades for drawdown ...
"""
look_back_until = date_now - timedelta(minutes=self._lookback_period)
trades = Trade.get_trades_proxy(is_open=False, close_date=look_back_until)
trades_df = pd.DataFrame([trade.to_json() for trade in trades])
if len(trades) < self._trade_limit:
# Not enough trades in the relevant period
return False, None, None
# Drawdown is always positive
try:
drawdown, _, _ = calculate_max_drawdown(trades_df, value_col='close_profit')
except ValueError:
return False, None, None
if drawdown > self._max_allowed_drawdown:
self.log_once(
f"Trading stopped due to Max Drawdown {drawdown:.2f} < {self._max_allowed_drawdown}"
f" within {self.lookback_period_str}.", logger.info)
until = self.calculate_lock_end(trades, self._stop_duration)
return True, until, self._reason(drawdown)
return False, None, None
def global_stop(self, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for all pairs
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, all pairs will be locked with <reason> until <until>
"""
return self._max_drawdown(date_now)
def stop_per_pair(self, pair: str, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for this pair
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, this pair will be locked with <reason> until <until>
"""
return False, None, None

View File

@ -0,0 +1,86 @@
import logging
from datetime import datetime, timedelta
from typing import Any, Dict
from freqtrade.persistence import Trade
from freqtrade.plugins.protections import IProtection, ProtectionReturn
from freqtrade.strategy.interface import SellType
logger = logging.getLogger(__name__)
class StoplossGuard(IProtection):
has_global_stop: bool = True
has_local_stop: bool = True
def __init__(self, config: Dict[str, Any], protection_config: Dict[str, Any]) -> None:
super().__init__(config, protection_config)
self._trade_limit = protection_config.get('trade_limit', 10)
self._disable_global_stop = protection_config.get('only_per_pair', False)
def short_desc(self) -> str:
"""
Short method description - used for startup-messages
"""
return (f"{self.name} - Frequent Stoploss Guard, {self._trade_limit} stoplosses "
f"within {self.lookback_period_str}.")
def _reason(self) -> str:
"""
LockReason to use
"""
return (f'{self._trade_limit} stoplosses in {self._lookback_period} min, '
f'locking for {self._stop_duration} min.')
def _stoploss_guard(self, date_now: datetime, pair: str = None) -> ProtectionReturn:
"""
Evaluate recent trades
"""
look_back_until = date_now - timedelta(minutes=self._lookback_period)
# filters = [
# Trade.is_open.is_(False),
# Trade.close_date > look_back_until,
# or_(Trade.sell_reason == SellType.STOP_LOSS.value,
# and_(Trade.sell_reason == SellType.TRAILING_STOP_LOSS.value,
# Trade.close_profit < 0))
# ]
# if pair:
# filters.append(Trade.pair == pair)
# trades = Trade.get_trades(filters).all()
trades1 = Trade.get_trades_proxy(pair=pair, is_open=False, close_date=look_back_until)
trades = [trade for trade in trades1 if str(trade.sell_reason) == SellType.STOP_LOSS.value
or (str(trade.sell_reason) == SellType.TRAILING_STOP_LOSS.value
and trade.close_profit < 0)]
if len(trades) > self._trade_limit:
self.log_once(f"Trading stopped due to {self._trade_limit} "
f"stoplosses within {self._lookback_period} minutes.", logger.info)
until = self.calculate_lock_end(trades, self._stop_duration)
return True, until, self._reason()
return False, None, None
def global_stop(self, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for all pairs
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, all pairs will be locked with <reason> until <until>
"""
if self._disable_global_stop:
return False, None, None
return self._stoploss_guard(date_now, None)
def stop_per_pair(self, pair: str, date_now: datetime) -> ProtectionReturn:
"""
Stops trading (position entering) for this pair
This must evaluate to true for the whole period of the "cooldown period".
:return: Tuple of [bool, until, reason].
If true, this pair will be locked with <reason> until <until>
"""
return self._stoploss_guard(date_now, pair)

View File

@ -6,6 +6,7 @@ from freqtrade.resolvers.exchange_resolver import ExchangeResolver
# Don't import HyperoptResolver to avoid loading the whole Optimize tree # Don't import HyperoptResolver to avoid loading the whole Optimize tree
# from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver # from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver
from freqtrade.resolvers.pairlist_resolver import PairListResolver from freqtrade.resolvers.pairlist_resolver import PairListResolver
from freqtrade.resolvers.protection_resolver import ProtectionResolver
from freqtrade.resolvers.strategy_resolver import StrategyResolver from freqtrade.resolvers.strategy_resolver import StrategyResolver

View File

@ -6,7 +6,7 @@ This module load custom pairlists
import logging import logging
from pathlib import Path from pathlib import Path
from freqtrade.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.IPairList import IPairList
from freqtrade.resolvers import IResolver from freqtrade.resolvers import IResolver
@ -20,7 +20,7 @@ class PairListResolver(IResolver):
object_type = IPairList object_type = IPairList
object_type_str = "Pairlist" object_type_str = "Pairlist"
user_subdir = None user_subdir = None
initial_search_path = Path(__file__).parent.parent.joinpath('pairlist').resolve() initial_search_path = Path(__file__).parent.parent.joinpath('plugins/pairlist').resolve()
@staticmethod @staticmethod
def load_pairlist(pairlist_name: str, exchange, pairlistmanager, def load_pairlist(pairlist_name: str, exchange, pairlistmanager,

View File

@ -0,0 +1,37 @@
"""
This module load custom pairlists
"""
import logging
from pathlib import Path
from typing import Dict
from freqtrade.plugins.protections import IProtection
from freqtrade.resolvers import IResolver
logger = logging.getLogger(__name__)
class ProtectionResolver(IResolver):
"""
This class contains all the logic to load custom PairList class
"""
object_type = IProtection
object_type_str = "Protection"
user_subdir = None
initial_search_path = Path(__file__).parent.parent.joinpath('plugins/protections').resolve()
@staticmethod
def load_protection(protection_name: str, config: Dict, protection_config: Dict) -> IProtection:
"""
Load the protection with protection_name
:param protection_name: Classname of the pairlist
:param config: configuration dictionary
:param protection_config: Configuration dedicated to this pairlist
:return: initialized Protection class
"""
return ProtectionResolver.load_object(protection_name, config,
kwargs={'config': config,
'protection_config': protection_config,
},
)

View File

@ -88,9 +88,6 @@ class StrategyResolver(IResolver):
StrategyResolver._override_attribute_helper(strategy, config, StrategyResolver._override_attribute_helper(strategy, config,
attribute, default) attribute, default)
# Assign deprecated variable - to not break users code relying on this.
strategy.ticker_interval = strategy.timeframe
# Loop this list again to have output combined # Loop this list again to have output combined
for attribute, _, subkey in attributes: for attribute, _, subkey in attributes:
if subkey and attribute in config[subkey]: if subkey and attribute in config[subkey]:
@ -98,11 +95,7 @@ class StrategyResolver(IResolver):
elif attribute in config: elif attribute in config:
logger.info("Strategy using %s: %s", attribute, config[attribute]) logger.info("Strategy using %s: %s", attribute, config[attribute])
# Sort and apply type conversions StrategyResolver._normalize_attributes(strategy)
strategy.minimal_roi = OrderedDict(sorted(
{int(key): value for (key, value) in strategy.minimal_roi.items()}.items(),
key=lambda t: t[0]))
strategy.stoploss = float(strategy.stoploss)
StrategyResolver._strategy_sanity_validations(strategy) StrategyResolver._strategy_sanity_validations(strategy)
return strategy return strategy
@ -131,6 +124,24 @@ class StrategyResolver(IResolver):
setattr(strategy, attribute, default) setattr(strategy, attribute, default)
config[attribute] = default config[attribute] = default
@staticmethod
def _normalize_attributes(strategy: IStrategy) -> IStrategy:
"""
Normalize attributes to have the correct type.
"""
# Assign deprecated variable - to not break users code relying on this.
if hasattr(strategy, 'timeframe'):
strategy.ticker_interval = strategy.timeframe
# Sort and apply type conversions
if hasattr(strategy, 'minimal_roi'):
strategy.minimal_roi = OrderedDict(sorted(
{int(key): value for (key, value) in strategy.minimal_roi.items()}.items(),
key=lambda t: t[0]))
if hasattr(strategy, 'stoploss'):
strategy.stoploss = float(strategy.stoploss)
return strategy
@staticmethod @staticmethod
def _strategy_sanity_validations(strategy): def _strategy_sanity_validations(strategy):
if not all(k in strategy.order_types for k in REQUIRED_ORDERTYPES): if not all(k in strategy.order_types for k in REQUIRED_ORDERTYPES):

View File

@ -1,3 +1,3 @@
# flake8: noqa: F401 # flake8: noqa: F401
from .rpc import RPC, RPCException, RPCMessageType from .rpc import RPC, RPCException, RPCHandler, RPCMessageType
from .rpc_manager import RPCManager from .rpc_manager import RPCManager

View File

@ -20,8 +20,7 @@ from freqtrade.__init__ import __version__
from freqtrade.constants import DATETIME_PRINT_FORMAT, USERPATH_STRATEGIES from freqtrade.constants import DATETIME_PRINT_FORMAT, USERPATH_STRATEGIES
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.rpc.fiat_convert import CryptoToFiatConverter from freqtrade.rpc.rpc import RPC, RPCException, RPCHandler
from freqtrade.rpc.rpc import RPC, RPCException
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -79,7 +78,7 @@ def shutdown_session(exception=None):
Trade.session.remove() Trade.session.remove()
class ApiServer(RPC): class ApiServer(RPCHandler):
""" """
This class runs api server and provides rpc.rpc functionality to it This class runs api server and provides rpc.rpc functionality to it
@ -90,15 +89,15 @@ class ApiServer(RPC):
return (safe_str_cmp(username, self._config['api_server'].get('username')) and return (safe_str_cmp(username, self._config['api_server'].get('username')) and
safe_str_cmp(password, self._config['api_server'].get('password'))) safe_str_cmp(password, self._config['api_server'].get('password')))
def __init__(self, freqtrade) -> None: def __init__(self, rpc: RPC, config: Dict[str, Any]) -> None:
""" """
Init the api server, and init the super class RPC Init the api server, and init the super class RPCHandler
:param freqtrade: Instance of a freqtrade bot :param rpc: instance of RPC Helper class
:param config: Configuration object
:return: None :return: None
""" """
super().__init__(freqtrade) super().__init__(rpc, config)
self._config = freqtrade.config
self.app = Flask(__name__) self.app = Flask(__name__)
self._cors = CORS(self.app, self._cors = CORS(self.app,
resources={r"/api/*": { resources={r"/api/*": {
@ -118,9 +117,6 @@ class ApiServer(RPC):
# Register application handling # Register application handling
self.register_rest_rpc_urls() self.register_rest_rpc_urls()
if self._config.get('fiat_display_currency', None):
self._fiat_converter = CryptoToFiatConverter()
thread = threading.Thread(target=self.run, daemon=True) thread = threading.Thread(target=self.run, daemon=True)
thread.start() thread.start()
@ -198,6 +194,8 @@ class ApiServer(RPC):
self.app.add_url_rule(f'{BASE_URI}/logs', 'log', view_func=self._get_logs, methods=['GET']) self.app.add_url_rule(f'{BASE_URI}/logs', 'log', view_func=self._get_logs, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/profit', 'profit', self.app.add_url_rule(f'{BASE_URI}/profit', 'profit',
view_func=self._profit, methods=['GET']) view_func=self._profit, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/stats', 'stats',
view_func=self._stats, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/performance', 'performance', self.app.add_url_rule(f'{BASE_URI}/performance', 'performance',
view_func=self._performance, methods=['GET']) view_func=self._performance, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/status', 'status', self.app.add_url_rule(f'{BASE_URI}/status', 'status',
@ -285,7 +283,7 @@ class ApiServer(RPC):
Handler for /start. Handler for /start.
Starts TradeThread in bot if stopped. Starts TradeThread in bot if stopped.
""" """
msg = self._rpc_start() msg = self._rpc._rpc_start()
return jsonify(msg) return jsonify(msg)
@require_login @require_login
@ -295,7 +293,7 @@ class ApiServer(RPC):
Handler for /stop. Handler for /stop.
Stops TradeThread in bot if running Stops TradeThread in bot if running
""" """
msg = self._rpc_stop() msg = self._rpc._rpc_stop()
return jsonify(msg) return jsonify(msg)
@require_login @require_login
@ -305,7 +303,7 @@ class ApiServer(RPC):
Handler for /stopbuy. Handler for /stopbuy.
Sets max_open_trades to 0 and gracefully sells all open trades Sets max_open_trades to 0 and gracefully sells all open trades
""" """
msg = self._rpc_stopbuy() msg = self._rpc._rpc_stopbuy()
return jsonify(msg) return jsonify(msg)
@rpc_catch_errors @rpc_catch_errors
@ -329,7 +327,7 @@ class ApiServer(RPC):
""" """
Prints the bot's version Prints the bot's version
""" """
return jsonify(RPC._rpc_show_config(self._config, self._freqtrade.state)) return jsonify(RPC._rpc_show_config(self._config, self._rpc._freqtrade.state))
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors
@ -338,7 +336,7 @@ class ApiServer(RPC):
Handler for /reload_config. Handler for /reload_config.
Triggers a config file reload Triggers a config file reload
""" """
msg = self._rpc_reload_config() msg = self._rpc._rpc_reload_config()
return jsonify(msg) return jsonify(msg)
@require_login @require_login
@ -348,7 +346,7 @@ class ApiServer(RPC):
Handler for /count. Handler for /count.
Returns the number of trades running Returns the number of trades running
""" """
msg = self._rpc_count() msg = self._rpc._rpc_count()
return jsonify(msg) return jsonify(msg)
@require_login @require_login
@ -358,7 +356,7 @@ class ApiServer(RPC):
Handler for /locks. Handler for /locks.
Returns the currently active locks. Returns the currently active locks.
""" """
return jsonify(self._rpc_locks()) return jsonify(self._rpc._rpc_locks())
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors
@ -371,7 +369,7 @@ class ApiServer(RPC):
timescale = request.args.get('timescale', 7) timescale = request.args.get('timescale', 7)
timescale = int(timescale) timescale = int(timescale)
stats = self._rpc_daily_profit(timescale, stats = self._rpc._rpc_daily_profit(timescale,
self._config['stake_currency'], self._config['stake_currency'],
self._config.get('fiat_display_currency', '') self._config.get('fiat_display_currency', '')
) )
@ -388,7 +386,7 @@ class ApiServer(RPC):
limit: Only get a certain number of records limit: Only get a certain number of records
""" """
limit = int(request.args.get('limit', 0)) or None limit = int(request.args.get('limit', 0)) or None
return jsonify(self._rpc_get_logs(limit)) return jsonify(RPC._rpc_get_logs(limit))
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors
@ -397,7 +395,7 @@ class ApiServer(RPC):
Returns information related to Edge. Returns information related to Edge.
:return: edge stats :return: edge stats
""" """
stats = self._rpc_edge() stats = self._rpc._rpc_edge()
return jsonify(stats) return jsonify(stats)
@ -411,12 +409,24 @@ class ApiServer(RPC):
:return: stats :return: stats
""" """
stats = self._rpc_trade_statistics(self._config['stake_currency'], stats = self._rpc._rpc_trade_statistics(self._config['stake_currency'],
self._config.get('fiat_display_currency') self._config.get('fiat_display_currency')
) )
return jsonify(stats) return jsonify(stats)
@require_login
@rpc_catch_errors
def _stats(self):
"""
Handler for /stats.
Returns a Object with "durations" and "sell_reasons" as keys.
"""
stats = self._rpc._rpc_stats()
return jsonify(stats)
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors
def _performance(self): def _performance(self):
@ -426,7 +436,7 @@ class ApiServer(RPC):
Returns a cumulative performance statistics Returns a cumulative performance statistics
:return: stats :return: stats
""" """
stats = self._rpc_performance() stats = self._rpc._rpc_performance()
return jsonify(stats) return jsonify(stats)
@ -439,7 +449,7 @@ class ApiServer(RPC):
Returns the current status of the trades in json format Returns the current status of the trades in json format
""" """
try: try:
results = self._rpc_trade_status() results = self._rpc._rpc_trade_status()
return jsonify(results) return jsonify(results)
except RPCException: except RPCException:
return jsonify([]) return jsonify([])
@ -452,7 +462,7 @@ class ApiServer(RPC):
Returns the current status of the trades in json format Returns the current status of the trades in json format
""" """
results = self._rpc_balance(self._config['stake_currency'], results = self._rpc._rpc_balance(self._config['stake_currency'],
self._config.get('fiat_display_currency', '')) self._config.get('fiat_display_currency', ''))
return jsonify(results) return jsonify(results)
@ -465,12 +475,12 @@ class ApiServer(RPC):
Returns the X last trades in json format Returns the X last trades in json format
""" """
limit = int(request.args.get('limit', 0)) limit = int(request.args.get('limit', 0))
results = self._rpc_trade_history(limit) results = self._rpc._rpc_trade_history(limit)
return jsonify(results) return jsonify(results)
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors
def _trades_delete(self, tradeid): def _trades_delete(self, tradeid: int):
""" """
Handler for DELETE /trades/<tradeid> endpoint. Handler for DELETE /trades/<tradeid> endpoint.
Removes the trade from the database (tries to cancel open orders first!) Removes the trade from the database (tries to cancel open orders first!)
@ -478,7 +488,7 @@ class ApiServer(RPC):
param: param:
tradeid: Numeric trade-id assigned to the trade. tradeid: Numeric trade-id assigned to the trade.
""" """
result = self._rpc_delete(tradeid) result = self._rpc._rpc_delete(tradeid)
return jsonify(result) return jsonify(result)
@require_login @require_login
@ -487,7 +497,7 @@ class ApiServer(RPC):
""" """
Handler for /whitelist. Handler for /whitelist.
""" """
results = self._rpc_whitelist() results = self._rpc._rpc_whitelist()
return jsonify(results) return jsonify(results)
@require_login @require_login
@ -497,7 +507,7 @@ class ApiServer(RPC):
Handler for /blacklist. Handler for /blacklist.
""" """
add = request.json.get("blacklist", None) if request.method == 'POST' else None add = request.json.get("blacklist", None) if request.method == 'POST' else None
results = self._rpc_blacklist(add) results = self._rpc._rpc_blacklist(add)
return jsonify(results) return jsonify(results)
@require_login @require_login
@ -510,7 +520,7 @@ class ApiServer(RPC):
price = request.json.get("price", None) price = request.json.get("price", None)
price = float(price) if price is not None else price price = float(price) if price is not None else price
trade = self._rpc_forcebuy(asset, price) trade = self._rpc._rpc_forcebuy(asset, price)
if trade: if trade:
return jsonify(trade.to_json()) return jsonify(trade.to_json())
else: else:
@ -523,7 +533,7 @@ class ApiServer(RPC):
Handler for /forcesell. Handler for /forcesell.
""" """
tradeid = request.json.get("tradeid") tradeid = request.json.get("tradeid")
results = self._rpc_forcesell(tradeid) results = self._rpc._rpc_forcesell(tradeid)
return jsonify(results) return jsonify(results)
@require_login @require_login
@ -545,7 +555,7 @@ class ApiServer(RPC):
if not pair or not timeframe: if not pair or not timeframe:
return self.rest_error("Mandatory parameter missing.", 400) return self.rest_error("Mandatory parameter missing.", 400)
results = self._rpc_analysed_dataframe(pair, timeframe, limit) results = self._rpc._rpc_analysed_dataframe(pair, timeframe, limit)
return jsonify(results) return jsonify(results)
@require_login @require_login
@ -584,7 +594,7 @@ class ApiServer(RPC):
""" """
Handler for /plot_config. Handler for /plot_config.
""" """
return jsonify(self._rpc_plot_config()) return jsonify(self._rpc._rpc_plot_config())
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors

View File

@ -65,20 +65,17 @@ class RPCException(Exception):
} }
class RPC: class RPCHandler:
"""
RPC class can be used to have extra feature, like bot data, and access to DB data
"""
# Bind _fiat_converter if needed in each RPC handler
_fiat_converter: Optional[CryptoToFiatConverter] = None
def __init__(self, freqtrade) -> None: def __init__(self, rpc: 'RPC', config: Dict[str, Any]) -> None:
""" """
Initializes all enabled rpc modules Initializes RPCHandlers
:param freqtrade: Instance of a freqtrade bot :param rpc: instance of RPC Helper class
:param config: Configuration object
:return: None :return: None
""" """
self._freqtrade = freqtrade self._rpc = rpc
self._config: Dict[str, Any] = config
@property @property
def name(self) -> str: def name(self) -> str:
@ -93,6 +90,25 @@ class RPC:
def send_msg(self, msg: Dict[str, str]) -> None: def send_msg(self, msg: Dict[str, str]) -> None:
""" Sends a message to all registered rpc modules """ """ Sends a message to all registered rpc modules """
class RPC:
"""
RPC class can be used to have extra feature, like bot data, and access to DB data
"""
# Bind _fiat_converter if needed
_fiat_converter: Optional[CryptoToFiatConverter] = None
def __init__(self, freqtrade) -> None:
"""
Initializes all enabled rpc modules
:param freqtrade: Instance of a freqtrade bot
:return: None
"""
self._freqtrade = freqtrade
self._config: Dict[str, Any] = freqtrade.config
if self._config.get('fiat_display_currency', None):
self._fiat_converter = CryptoToFiatConverter()
@staticmethod @staticmethod
def _rpc_show_config(config, botstate: State) -> Dict[str, Any]: def _rpc_show_config(config, botstate: State) -> Dict[str, Any]:
""" """
@ -275,6 +291,39 @@ class RPC:
"trades_count": len(output) "trades_count": len(output)
} }
def _rpc_stats(self) -> Dict[str, Any]:
"""
Generate generic stats for trades in database
"""
def trade_win_loss(trade):
if trade.close_profit > 0:
return 'wins'
elif trade.close_profit < 0:
return 'losses'
else:
return 'draws'
trades = trades = Trade.get_trades([Trade.is_open.is_(False)])
# Sell reason
sell_reasons = {}
for trade in trades:
if trade.sell_reason not in sell_reasons:
sell_reasons[trade.sell_reason] = {'wins': 0, 'losses': 0, 'draws': 0}
sell_reasons[trade.sell_reason][trade_win_loss(trade)] += 1
# Duration
dur: Dict[str, List[int]] = {'wins': [], 'draws': [], 'losses': []}
for trade in trades:
if trade.close_date is not None and trade.open_date is not None:
trade_dur = (trade.close_date - trade.open_date).total_seconds()
dur[trade_win_loss(trade)].append(trade_dur)
wins_dur = sum(dur['wins']) / len(dur['wins']) if len(dur['wins']) > 0 else 'N/A'
draws_dur = sum(dur['draws']) / len(dur['draws']) if len(dur['draws']) > 0 else 'N/A'
losses_dur = sum(dur['losses']) / len(dur['losses']) if len(dur['losses']) > 0 else 'N/A'
durations = {'wins': wins_dur, 'draws': draws_dur, 'losses': losses_dur}
return {'sell_reasons': sell_reasons, 'durations': durations}
def _rpc_trade_statistics( def _rpc_trade_statistics(
self, stake_currency: str, fiat_display_currency: str) -> Dict[str, Any]: self, stake_currency: str, fiat_display_currency: str) -> Dict[str, Any]:
""" Returns cumulative profit statistics """ """ Returns cumulative profit statistics """
@ -542,7 +591,7 @@ class RPC:
else: else:
return None return None
def _rpc_delete(self, trade_id: str) -> Dict[str, Union[str, int]]: def _rpc_delete(self, trade_id: int) -> Dict[str, Union[str, int]]:
""" """
Handler for delete <id>. Handler for delete <id>.
Delete the given trade and close eventually existing open orders. Delete the given trade and close eventually existing open orders.
@ -645,7 +694,8 @@ class RPC:
} }
return res return res
def _rpc_get_logs(self, limit: Optional[int]) -> Dict[str, Any]: @staticmethod
def _rpc_get_logs(limit: Optional[int]) -> Dict[str, Any]:
"""Returns the last X logs""" """Returns the last X logs"""
if limit: if limit:
buffer = bufferHandler.buffer[-limit:] buffer = bufferHandler.buffer[-limit:]

View File

@ -4,7 +4,7 @@ This module contains class to manage RPC communications (Telegram, Slack, ...)
import logging import logging
from typing import Any, Dict, List from typing import Any, Dict, List
from freqtrade.rpc import RPC, RPCMessageType from freqtrade.rpc import RPC, RPCHandler, RPCMessageType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -16,25 +16,26 @@ class RPCManager:
""" """
def __init__(self, freqtrade) -> None: def __init__(self, freqtrade) -> None:
""" Initializes all enabled rpc modules """ """ Initializes all enabled rpc modules """
self.registered_modules: List[RPC] = [] self.registered_modules: List[RPCHandler] = []
self._rpc = RPC(freqtrade)
config = freqtrade.config
# Enable telegram # Enable telegram
if freqtrade.config.get('telegram', {}).get('enabled', False): if config.get('telegram', {}).get('enabled', False):
logger.info('Enabling rpc.telegram ...') logger.info('Enabling rpc.telegram ...')
from freqtrade.rpc.telegram import Telegram from freqtrade.rpc.telegram import Telegram
self.registered_modules.append(Telegram(freqtrade)) self.registered_modules.append(Telegram(self._rpc, config))
# Enable Webhook # Enable Webhook
if freqtrade.config.get('webhook', {}).get('enabled', False): if config.get('webhook', {}).get('enabled', False):
logger.info('Enabling rpc.webhook ...') logger.info('Enabling rpc.webhook ...')
from freqtrade.rpc.webhook import Webhook from freqtrade.rpc.webhook import Webhook
self.registered_modules.append(Webhook(freqtrade)) self.registered_modules.append(Webhook(self._rpc, config))
# Enable local rest api server for cmd line control # Enable local rest api server for cmd line control
if freqtrade.config.get('api_server', {}).get('enabled', False): if config.get('api_server', {}).get('enabled', False):
logger.info('Enabling rpc.api_server') logger.info('Enabling rpc.api_server')
from freqtrade.rpc.api_server import ApiServer from freqtrade.rpc.api_server import ApiServer
self.registered_modules.append(ApiServer(freqtrade)) self.registered_modules.append(ApiServer(self._rpc, config))
def cleanup(self) -> None: def cleanup(self) -> None:
""" Stops all enabled rpc modules """ """ Stops all enabled rpc modules """
@ -62,7 +63,7 @@ class RPCManager:
except NotImplementedError: except NotImplementedError:
logger.error(f"Message type '{msg['type']}' not implemented by handler {mod.name}.") logger.error(f"Message type '{msg['type']}' not implemented by handler {mod.name}.")
def startup_messages(self, config: Dict[str, Any], pairlist) -> None: def startup_messages(self, config: Dict[str, Any], pairlist, protections) -> None:
if config['dry_run']: if config['dry_run']:
self.send_msg({ self.send_msg({
'type': RPCMessageType.WARNING_NOTIFICATION, 'type': RPCMessageType.WARNING_NOTIFICATION,
@ -90,3 +91,9 @@ class RPCManager:
'status': f'Searching for {stake_currency} pairs to buy and sell ' 'status': f'Searching for {stake_currency} pairs to buy and sell '
f'based on {pairlist.short_desc()}' f'based on {pairlist.short_desc()}'
}) })
if len(protections.name_list) > 0:
prots = '\n'.join([p for prot in protections.short_desc() for k, p in prot.items()])
self.send_msg({
'type': RPCMessageType.STARTUP_NOTIFICATION,
'status': f'Using Protections: \n{prots}'
})

View File

@ -5,18 +5,20 @@ This module manage Telegram communication
""" """
import json import json
import logging import logging
from typing import Any, Callable, Dict from datetime import timedelta
from itertools import chain
from typing import Any, Callable, Dict, List, Union
import arrow import arrow
from tabulate import tabulate from tabulate import tabulate
from telegram import ParseMode, ReplyKeyboardMarkup, Update from telegram import KeyboardButton, ParseMode, ReplyKeyboardMarkup, Update
from telegram.error import NetworkError, TelegramError from telegram.error import NetworkError, TelegramError
from telegram.ext import CallbackContext, CommandHandler, Updater from telegram.ext import CallbackContext, CommandHandler, Updater
from telegram.utils.helpers import escape_markdown from telegram.utils.helpers import escape_markdown
from freqtrade.__init__ import __version__ from freqtrade.__init__ import __version__
from freqtrade.rpc import RPC, RPCException, RPCMessageType from freqtrade.exceptions import OperationalException
from freqtrade.rpc.fiat_convert import CryptoToFiatConverter from freqtrade.rpc import RPC, RPCException, RPCHandler, RPCMessageType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -60,22 +62,60 @@ def authorized_only(command_handler: Callable[..., None]) -> Callable[..., Any]:
return wrapper return wrapper
class Telegram(RPC): class Telegram(RPCHandler):
""" This class handles all telegram communication """ """ This class handles all telegram communication """
def __init__(self, freqtrade) -> None: def __init__(self, rpc: RPC, config: Dict[str, Any]) -> None:
""" """
Init the Telegram call, and init the super class RPC Init the Telegram call, and init the super class RPCHandler
:param freqtrade: Instance of a freqtrade bot :param rpc: instance of RPC Helper class
:param config: Configuration object
:return: None :return: None
""" """
super().__init__(freqtrade) super().__init__(rpc, config)
self._updater: Updater = None self._updater: Updater
self._config = freqtrade.config self._init_keyboard()
self._init() self._init()
if self._config.get('fiat_display_currency', None):
self._fiat_converter = CryptoToFiatConverter() def _init_keyboard(self) -> None:
"""
Validates the keyboard configuration from telegram config
section.
"""
self._keyboard: List[List[Union[str, KeyboardButton]]] = [
['/daily', '/profit', '/balance'],
['/status', '/status table', '/performance'],
['/count', '/start', '/stop', '/help']
]
# do not allow commands with mandatory arguments and critical cmds
# like /forcesell and /forcebuy
# TODO: DRY! - its not good to list all valid cmds here. But otherwise
# this needs refacoring of the whole telegram module (same
# problem in _help()).
valid_keys: List[str] = ['/start', '/stop', '/status', '/status table',
'/trades', '/profit', '/performance', '/daily',
'/stats', '/count', '/locks', '/balance',
'/stopbuy', '/reload_config', '/show_config',
'/logs', '/whitelist', '/blacklist', '/edge',
'/help', '/version']
# custom keyboard specified in config.json
cust_keyboard = self._config['telegram'].get('keyboard', [])
if cust_keyboard:
# check for valid shortcuts
invalid_keys = [b for b in chain.from_iterable(cust_keyboard)
if b not in valid_keys]
if len(invalid_keys):
err_msg = ('config.telegram.keyboard: Invalid commands for '
f'custom Telegram keyboard: {invalid_keys}'
f'\nvalid commands are: {valid_keys}')
raise OperationalException(err_msg)
else:
self._keyboard = cust_keyboard
logger.info('using custom keyboard from '
f'config.json: {self._keyboard}')
def _init(self) -> None: def _init(self) -> None:
""" """
@ -98,6 +138,7 @@ class Telegram(RPC):
CommandHandler('trades', self._trades), CommandHandler('trades', self._trades),
CommandHandler('delete', self._delete_trade), CommandHandler('delete', self._delete_trade),
CommandHandler('performance', self._performance), CommandHandler('performance', self._performance),
CommandHandler('stats', self._stats),
CommandHandler('daily', self._daily), CommandHandler('daily', self._daily),
CommandHandler('count', self._count), CommandHandler('count', self._count),
CommandHandler('locks', self._locks), CommandHandler('locks', self._locks),
@ -142,8 +183,8 @@ class Telegram(RPC):
return return
if msg['type'] == RPCMessageType.BUY_NOTIFICATION: if msg['type'] == RPCMessageType.BUY_NOTIFICATION:
if self._fiat_converter: if self._rpc._fiat_converter:
msg['stake_amount_fiat'] = self._fiat_converter.convert_amount( msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount(
msg['stake_amount'], msg['stake_currency'], msg['fiat_currency']) msg['stake_amount'], msg['stake_currency'], msg['fiat_currency'])
else: else:
msg['stake_amount_fiat'] = 0 msg['stake_amount_fiat'] = 0
@ -183,8 +224,8 @@ class Telegram(RPC):
# Check if all sell properties are available. # Check if all sell properties are available.
# This might not be the case if the message origin is triggered by /forcesell # This might not be the case if the message origin is triggered by /forcesell
if (all(prop in msg for prop in ['gain', 'fiat_currency', 'stake_currency']) if (all(prop in msg for prop in ['gain', 'fiat_currency', 'stake_currency'])
and self._fiat_converter): and self._rpc._fiat_converter):
msg['profit_fiat'] = self._fiat_converter.convert_amount( msg['profit_fiat'] = self._rpc._fiat_converter.convert_amount(
msg['profit_amount'], msg['stake_currency'], msg['fiat_currency']) msg['profit_amount'], msg['stake_currency'], msg['fiat_currency'])
message += (' `({gain}: {profit_amount:.8f} {stake_currency}' message += (' `({gain}: {profit_amount:.8f} {stake_currency}'
' / {profit_fiat:.3f} {fiat_currency})`').format(**msg) ' / {profit_fiat:.3f} {fiat_currency})`').format(**msg)
@ -231,12 +272,12 @@ class Telegram(RPC):
:return: None :return: None
""" """
if 'table' in context.args: if context.args and 'table' in context.args:
self._status_table(update, context) self._status_table(update, context)
return return
try: try:
results = self._rpc_trade_status() results = self._rpc._rpc_trade_status()
messages = [] messages = []
for r in results: for r in results:
@ -286,8 +327,9 @@ class Telegram(RPC):
:return: None :return: None
""" """
try: try:
statlist, head = self._rpc_status_table(self._config['stake_currency'], statlist, head = self._rpc._rpc_status_table(
self._config.get('fiat_display_currency', '')) self._config['stake_currency'], self._config.get('fiat_display_currency', ''))
message = tabulate(statlist, headers=head, tablefmt='simple') message = tabulate(statlist, headers=head, tablefmt='simple')
self._send_msg(f"<pre>{message}</pre>", parse_mode=ParseMode.HTML) self._send_msg(f"<pre>{message}</pre>", parse_mode=ParseMode.HTML)
except RPCException as e: except RPCException as e:
@ -305,11 +347,11 @@ class Telegram(RPC):
stake_cur = self._config['stake_currency'] stake_cur = self._config['stake_currency']
fiat_disp_cur = self._config.get('fiat_display_currency', '') fiat_disp_cur = self._config.get('fiat_display_currency', '')
try: try:
timescale = int(context.args[0]) timescale = int(context.args[0]) if context.args else 7
except (TypeError, ValueError, IndexError): except (TypeError, ValueError, IndexError):
timescale = 7 timescale = 7
try: try:
stats = self._rpc_daily_profit( stats = self._rpc._rpc_daily_profit(
timescale, timescale,
stake_cur, stake_cur,
fiat_disp_cur fiat_disp_cur
@ -343,7 +385,7 @@ class Telegram(RPC):
stake_cur = self._config['stake_currency'] stake_cur = self._config['stake_currency']
fiat_disp_cur = self._config.get('fiat_display_currency', '') fiat_disp_cur = self._config.get('fiat_display_currency', '')
stats = self._rpc_trade_statistics( stats = self._rpc._rpc_trade_statistics(
stake_cur, stake_cur,
fiat_disp_cur) fiat_disp_cur)
profit_closed_coin = stats['profit_closed_coin'] profit_closed_coin = stats['profit_closed_coin']
@ -388,11 +430,53 @@ class Telegram(RPC):
f"*Best Performing:* `{best_pair}: {best_rate:.2f}%`") f"*Best Performing:* `{best_pair}: {best_rate:.2f}%`")
self._send_msg(markdown_msg) self._send_msg(markdown_msg)
@authorized_only
def _stats(self, update: Update, context: CallbackContext) -> None:
"""
Handler for /stats
Show stats of recent trades
"""
stats = self._rpc._rpc_stats()
reason_map = {
'roi': 'ROI',
'stop_loss': 'Stoploss',
'trailing_stop_loss': 'Trail. Stop',
'stoploss_on_exchange': 'Stoploss',
'sell_signal': 'Sell Signal',
'force_sell': 'Forcesell',
'emergency_sell': 'Emergency Sell',
}
sell_reasons_tabulate = [
[
reason_map.get(reason, reason),
sum(count.values()),
count['wins'],
count['losses']
] for reason, count in stats['sell_reasons'].items()
]
sell_reasons_msg = tabulate(
sell_reasons_tabulate,
headers=['Sell Reason', 'Sells', 'Wins', 'Losses']
)
durations = stats['durations']
duration_msg = tabulate([
['Wins', str(timedelta(seconds=durations['wins']))
if durations['wins'] != 'N/A' else 'N/A'],
['Losses', str(timedelta(seconds=durations['losses']))
if durations['losses'] != 'N/A' else 'N/A']
],
headers=['', 'Avg. Duration']
)
msg = (f"""```\n{sell_reasons_msg}```\n```\n{duration_msg}```""")
self._send_msg(msg, ParseMode.MARKDOWN)
@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: try:
result = self._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', ''))
output = '' output = ''
@ -436,7 +520,7 @@ class Telegram(RPC):
:param update: message update :param update: message update
:return: None :return: None
""" """
msg = self._rpc_start() msg = self._rpc._rpc_start()
self._send_msg('Status: `{status}`'.format(**msg)) self._send_msg('Status: `{status}`'.format(**msg))
@authorized_only @authorized_only
@ -448,7 +532,7 @@ class Telegram(RPC):
:param update: message update :param update: message update
:return: None :return: None
""" """
msg = self._rpc_stop() msg = self._rpc._rpc_stop()
self._send_msg('Status: `{status}`'.format(**msg)) self._send_msg('Status: `{status}`'.format(**msg))
@authorized_only @authorized_only
@ -460,7 +544,7 @@ class Telegram(RPC):
:param update: message update :param update: message update
:return: None :return: None
""" """
msg = self._rpc_reload_config() msg = self._rpc._rpc_reload_config()
self._send_msg('Status: `{status}`'.format(**msg)) self._send_msg('Status: `{status}`'.format(**msg))
@authorized_only @authorized_only
@ -472,7 +556,7 @@ class Telegram(RPC):
:param update: message update :param update: message update
:return: None :return: None
""" """
msg = self._rpc_stopbuy() msg = self._rpc._rpc_stopbuy()
self._send_msg('Status: `{status}`'.format(**msg)) self._send_msg('Status: `{status}`'.format(**msg))
@authorized_only @authorized_only
@ -485,9 +569,12 @@ class Telegram(RPC):
:return: None :return: None
""" """
trade_id = context.args[0] if len(context.args) > 0 else None trade_id = context.args[0] if context.args and len(context.args) > 0 else None
if not trade_id:
self._send_msg("You must specify a trade-id or 'all'.")
return
try: try:
msg = self._rpc_forcesell(trade_id) msg = self._rpc._rpc_forcesell(trade_id)
self._send_msg('Forcesell Result: `{result}`'.format(**msg)) self._send_msg('Forcesell Result: `{result}`'.format(**msg))
except RPCException as e: except RPCException as e:
@ -502,11 +589,11 @@ class Telegram(RPC):
:param update: message update :param update: message update
:return: None :return: None
""" """
if context.args:
pair = context.args[0] pair = context.args[0]
price = float(context.args[1]) if len(context.args) > 1 else None price = float(context.args[1]) if len(context.args) > 1 else None
try: try:
self._rpc_forcebuy(pair, price) self._rpc._rpc_forcebuy(pair, price)
except RPCException as e: except RPCException as e:
self._send_msg(str(e)) self._send_msg(str(e))
@ -521,11 +608,11 @@ class Telegram(RPC):
""" """
stake_cur = self._config['stake_currency'] stake_cur = self._config['stake_currency']
try: try:
nrecent = int(context.args[0]) nrecent = int(context.args[0]) if context.args else 10
except (TypeError, ValueError, IndexError): except (TypeError, ValueError, IndexError):
nrecent = 10 nrecent = 10
try: try:
trades = self._rpc_trade_history( trades = self._rpc._rpc_trade_history(
nrecent nrecent
) )
trades_tab = tabulate( trades_tab = tabulate(
@ -554,10 +641,11 @@ class Telegram(RPC):
:param update: message update :param update: message update
:return: None :return: None
""" """
trade_id = context.args[0] if len(context.args) > 0 else None
try: try:
msg = self._rpc_delete(trade_id) if not context.args or len(context.args) == 0:
raise RPCException("Trade-id not set.")
trade_id = int(context.args[0])
msg = self._rpc._rpc_delete(trade_id)
self._send_msg(( self._send_msg((
'`{result_msg}`\n' '`{result_msg}`\n'
'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.'
@ -576,7 +664,7 @@ class Telegram(RPC):
:return: None :return: None
""" """
try: try:
trades = self._rpc_performance() trades = self._rpc._rpc_performance()
stats = '\n'.join('{index}.\t<code>{pair}\t{profit:.2f}% ({count})</code>'.format( stats = '\n'.join('{index}.\t<code>{pair}\t{profit:.2f}% ({count})</code>'.format(
index=i + 1, index=i + 1,
pair=trade['pair'], pair=trade['pair'],
@ -598,7 +686,7 @@ class Telegram(RPC):
:return: None :return: None
""" """
try: try:
counts = self._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'],
tablefmt='simple') tablefmt='simple')
@ -615,7 +703,7 @@ class Telegram(RPC):
Returns the currently active locks Returns the currently active locks
""" """
try: try:
locks = self._rpc_locks() locks = self._rpc._rpc_locks()
message = tabulate([[ message = tabulate([[
lock['pair'], lock['pair'],
lock['lock_end_time'], lock['lock_end_time'],
@ -635,7 +723,7 @@ class Telegram(RPC):
Shows the currently active whitelist Shows the currently active whitelist
""" """
try: try:
whitelist = self._rpc_whitelist() whitelist = self._rpc._rpc_whitelist()
message = f"Using whitelist `{whitelist['method']}` with {whitelist['length']} pairs\n" message = f"Using whitelist `{whitelist['method']}` with {whitelist['length']} pairs\n"
message += f"`{', '.join(whitelist['whitelist'])}`" message += f"`{', '.join(whitelist['whitelist'])}`"
@ -653,7 +741,7 @@ class Telegram(RPC):
""" """
try: try:
blacklist = self._rpc_blacklist(context.args) blacklist = self._rpc._rpc_blacklist(context.args)
errmsgs = [] errmsgs = []
for pair, error in blacklist['errors'].items(): for pair, error in blacklist['errors'].items():
errmsgs.append(f"Error adding `{pair}` to blacklist: `{error['error_msg']}`") errmsgs.append(f"Error adding `{pair}` to blacklist: `{error['error_msg']}`")
@ -676,10 +764,10 @@ class Telegram(RPC):
""" """
try: try:
try: try:
limit = int(context.args[0]) limit = int(context.args[0]) if context.args else 10
except (TypeError, ValueError, IndexError): except (TypeError, ValueError, IndexError):
limit = 10 limit = 10
logs = self._rpc_get_logs(limit)['logs'] logs = RPC._rpc_get_logs(limit)['logs']
msgs = '' msgs = ''
msg_template = "*{}* {}: {} \\- `{}`" msg_template = "*{}* {}: {} \\- `{}`"
for logrec in logs: for logrec in logs:
@ -707,7 +795,7 @@ class Telegram(RPC):
Shows information related to Edge Shows information related to Edge
""" """
try: try:
edge_pairs = self._rpc_edge() edge_pairs = self._rpc._rpc_edge()
edge_pairs_tab = tabulate(edge_pairs, headers='keys', tablefmt='simple') edge_pairs_tab = tabulate(edge_pairs, headers='keys', tablefmt='simple')
message = f'<b>Edge only validated following pairs:</b>\n<pre>{edge_pairs_tab}</pre>' message = f'<b>Edge only validated following pairs:</b>\n<pre>{edge_pairs_tab}</pre>'
self._send_msg(message, parse_mode=ParseMode.HTML) self._send_msg(message, parse_mode=ParseMode.HTML)
@ -739,6 +827,8 @@ class Telegram(RPC):
"*/delete <trade_id>:* `Instantly delete the given trade in the database`\n" "*/delete <trade_id>:* `Instantly delete the given trade in the database`\n"
"*/performance:* `Show performance of each finished trade grouped by pair`\n" "*/performance:* `Show performance of each finished trade grouped by pair`\n"
"*/daily <n>:* `Shows profit or loss per day, over the last n days`\n" "*/daily <n>:* `Shows profit or loss per day, over the last n days`\n"
"*/stats:* `Shows Wins / losses by Sell reason as well as "
"Avg. holding durationsfor buys and sells.`\n"
"*/count:* `Show number of active trades compared to allowed number of trades`\n" "*/count:* `Show number of active trades compared to allowed number of trades`\n"
"*/locks:* `Show currently locked pairs`\n" "*/locks:* `Show currently locked pairs`\n"
"*/balance:* `Show account balance per currency`\n" "*/balance:* `Show account balance per currency`\n"
@ -775,7 +865,7 @@ class Telegram(RPC):
:param update: message update :param update: message update
:return: None :return: None
""" """
val = RPC._rpc_show_config(self._freqtrade.config, self._freqtrade.state) val = RPC._rpc_show_config(self._config, self._rpc._freqtrade.state)
if val['trailing_stop']: if val['trailing_stop']:
sl_info = ( sl_info = (
@ -802,7 +892,7 @@ class Telegram(RPC):
f"*Current state:* `{val['state']}`" f"*Current state:* `{val['state']}`"
) )
def _send_msg(self, msg: str, parse_mode: ParseMode = ParseMode.MARKDOWN, def _send_msg(self, msg: str, parse_mode: str = ParseMode.MARKDOWN,
disable_notification: bool = False) -> None: disable_notification: bool = False) -> None:
""" """
Send given markdown message Send given markdown message
@ -811,13 +901,7 @@ class Telegram(RPC):
:param parse_mode: telegram parse mode :param parse_mode: telegram parse mode
:return: None :return: None
""" """
reply_markup = ReplyKeyboardMarkup(self._keyboard)
keyboard = [['/daily', '/profit', '/balance'],
['/status', '/status table', '/performance'],
['/count', '/start', '/stop', '/help']]
reply_markup = ReplyKeyboardMarkup(keyboard)
try: try:
try: try:
self._updater.bot.send_message( self._updater.bot.send_message(

View File

@ -6,7 +6,7 @@ from typing import Any, Dict
from requests import RequestException, post from requests import RequestException, post
from freqtrade.rpc import RPC, RPCMessageType from freqtrade.rpc import RPC, RPCHandler, RPCMessageType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -14,18 +14,18 @@ logger = logging.getLogger(__name__)
logger.debug('Included module rpc.webhook ...') logger.debug('Included module rpc.webhook ...')
class Webhook(RPC): class Webhook(RPCHandler):
""" This class handles all webhook communication """ """ This class handles all webhook communication """
def __init__(self, freqtrade) -> None: def __init__(self, rpc: RPC, config: Dict[str, Any]) -> None:
""" """
Init the Webhook class, and init the super class RPC Init the Webhook class, and init the super class RPCHandler
:param freqtrade: Instance of a freqtrade bot :param rpc: instance of RPC Helper class
:param config: Configuration object
:return: None :return: None
""" """
super().__init__(freqtrade) super().__init__(rpc, config)
self._config = freqtrade.config
self._url = self._config['webhook']['url'] self._url = self._config['webhook']['url']
def cleanup(self) -> None: def cleanup(self) -> None:

View File

@ -312,7 +312,7 @@ class IStrategy(ABC):
if not candle_date: if not candle_date:
# Simple call ... # Simple call ...
return PairLocks.is_pair_locked(pair, candle_date) return PairLocks.is_pair_locked(pair)
else: else:
lock_time = timeframe_to_next_date(self.timeframe, candle_date) lock_time = timeframe_to_next_date(self.timeframe, candle_date)
return PairLocks.is_pair_locked(pair, lock_time) return PairLocks.is_pair_locked(pair, lock_time)
@ -476,40 +476,44 @@ class IStrategy(ABC):
current_time=date, current_profit=current_profit, current_time=date, current_profit=current_profit,
force_stoploss=force_stoploss, high=high) force_stoploss=force_stoploss, high=high)
if stoplossflag.sell_flag:
logger.debug(f"{trade.pair} - Stoploss hit. sell_flag=True, "
f"sell_type={stoplossflag.sell_type}")
return stoplossflag
# Set current rate to high for backtesting sell # Set current rate to high for backtesting sell
current_rate = high or rate current_rate = high or rate
current_profit = trade.calc_profit_ratio(current_rate) current_profit = trade.calc_profit_ratio(current_rate)
config_ask_strategy = self.config.get('ask_strategy', {}) config_ask_strategy = self.config.get('ask_strategy', {})
if buy and config_ask_strategy.get('ignore_roi_if_buy_signal', False): # if buy signal and ignore_roi is set, we don't need to evaluate min_roi.
# This one is noisy, commented out roi_reached = (not (buy and config_ask_strategy.get('ignore_roi_if_buy_signal', False))
# logger.debug(f"{trade.pair} - Buy signal still active. sell_flag=False") and self.min_roi_reached(trade=trade, current_profit=current_profit,
return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE) current_time=date))
# Check if minimal roi has been reached and no longer in buy conditions (avoiding a fee) if config_ask_strategy.get('sell_profit_only', False) and trade.calc_profit(rate=rate) <= 0:
if self.min_roi_reached(trade=trade, current_profit=current_profit, current_time=date): # Negative profits and sell_profit_only - ignore sell signal
sell_signal = False
else:
sell_signal = sell and not buy and config_ask_strategy.get('use_sell_signal', True)
# TODO: return here if sell-signal should be favored over ROI
# Start evaluations
# Sequence:
# ROI (if not stoploss)
# Sell-signal
# Stoploss
if roi_reached and stoplossflag.sell_type != SellType.STOP_LOSS:
logger.debug(f"{trade.pair} - Required profit reached. sell_flag=True, " logger.debug(f"{trade.pair} - Required profit reached. sell_flag=True, "
f"sell_type=SellType.ROI") f"sell_type=SellType.ROI")
return SellCheckTuple(sell_flag=True, sell_type=SellType.ROI) return SellCheckTuple(sell_flag=True, sell_type=SellType.ROI)
if config_ask_strategy.get('sell_profit_only', False): if sell_signal:
# This one is noisy, commented out
# logger.debug(f"{trade.pair} - Checking if trade is profitable...")
if trade.calc_profit(rate=rate) <= 0:
# This one is noisy, commented out
# logger.debug(f"{trade.pair} - Trade is not profitable. sell_flag=False")
return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE)
if sell and not buy and config_ask_strategy.get('use_sell_signal', True):
logger.debug(f"{trade.pair} - Sell signal received. sell_flag=True, " logger.debug(f"{trade.pair} - Sell signal received. sell_flag=True, "
f"sell_type=SellType.SELL_SIGNAL") f"sell_type=SellType.SELL_SIGNAL")
return SellCheckTuple(sell_flag=True, sell_type=SellType.SELL_SIGNAL) return SellCheckTuple(sell_flag=True, sell_type=SellType.SELL_SIGNAL)
if stoplossflag.sell_flag:
logger.debug(f"{trade.pair} - Stoploss hit. sell_flag=True, "
f"sell_type={stoplossflag.sell_type}")
return stoplossflag
# This one is noisy, commented out... # This one is noisy, commented out...
# logger.debug(f"{trade.pair} - No sell signal. sell_flag=False") # logger.debug(f"{trade.pair} - No sell signal. sell_flag=False")
return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE) return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE)
@ -547,8 +551,7 @@ class IStrategy(ABC):
# evaluate if the stoploss was hit if stoploss is not on exchange # evaluate if the stoploss was hit if stoploss is not on exchange
# in Dry-Run, this handles stoploss logic as well, as the logic will not be different to # in Dry-Run, this handles stoploss logic as well, as the logic will not be different to
# regular stoploss handling. # regular stoploss handling.
if ((self.stoploss is not None) and if ((trade.stop_loss >= current_rate) and
(trade.stop_loss >= current_rate) and
(not self.order_types.get('stoploss_on_exchange') or self.config['dry_run'])): (not self.order_types.get('stoploss_on_exchange') or self.config['dry_run'])):
sell_type = SellType.STOP_LOSS sell_type = SellType.STOP_LOSS

View File

@ -19,6 +19,7 @@ nav:
- Backtesting: backtesting.md - Backtesting: backtesting.md
- Hyperopt: hyperopt.md - Hyperopt: hyperopt.md
- Edge Positioning: edge.md - Edge Positioning: edge.md
- Plugins: plugins.md
- Utility Subcommands: utils.md - Utility Subcommands: utils.md
- FAQ: faq.md - FAQ: faq.md
- Data Analysis: - Data Analysis:
@ -31,11 +32,13 @@ nav:
- Advanced Strategy: strategy-advanced.md - Advanced Strategy: strategy-advanced.md
- Advanced Hyperopt: advanced-hyperopt.md - Advanced Hyperopt: advanced-hyperopt.md
- Sandbox Testing: sandbox-testing.md - Sandbox Testing: sandbox-testing.md
- Updating Freqtrade: updating.md
- Deprecated Features: deprecated.md - Deprecated Features: deprecated.md
- Contributors Guide: developer.md - Contributors Guide: developer.md
theme: theme:
name: material name: material
logo: 'images/logo.png' logo: 'images/logo.png'
favicon: 'images/logo.png'
custom_dir: 'docs' custom_dir: 'docs'
palette: palette:
primary: 'blue grey' primary: 'blue grey'

View File

@ -6,12 +6,12 @@
coveralls==2.2.0 coveralls==2.2.0
flake8==3.8.4 flake8==3.8.4
flake8-type-annotations==0.1.0 flake8-type-annotations==0.1.0
flake8-tidy-imports==4.1.0 flake8-tidy-imports==4.2.1
mypy==0.790 mypy==0.790
pytest==6.1.2 pytest==6.2.1
pytest-asyncio==0.14.0 pytest-asyncio==0.14.0
pytest-cov==2.10.1 pytest-cov==2.10.1
pytest-mock==3.3.1 pytest-mock==3.4.0
pytest-random-order==1.0.4 pytest-random-order==1.0.4
isort==5.6.4 isort==5.6.4

View File

@ -6,5 +6,5 @@ scipy==1.5.4
scikit-learn==0.23.2 scikit-learn==0.23.2
scikit-optimize==0.8.1 scikit-optimize==0.8.1
filelock==3.0.12 filelock==3.0.12
joblib==0.17.0 joblib==1.0.0
progressbar2==3.53.1 progressbar2==3.53.1

View File

@ -1,5 +1,5 @@
# Include all requirements to run the bot. # Include all requirements to run the bot.
-r requirements.txt -r requirements.txt
plotly==4.12.0 plotly==4.14.1

View File

@ -1,13 +1,13 @@
numpy==1.19.4 numpy==1.19.4
pandas==1.1.4 pandas==1.1.5
ccxt==1.38.13 ccxt==1.39.79
aiohttp==3.7.3 aiohttp==3.7.3
SQLAlchemy==1.3.20 SQLAlchemy==1.3.22
python-telegram-bot==13.0 python-telegram-bot==13.1
arrow==0.17.0 arrow==0.17.0
cachetools==4.1.1 cachetools==4.2.0
requests==2.25.0 requests==2.25.1
urllib3==1.26.2 urllib3==1.26.2
wrapt==1.12.1 wrapt==1.12.1
jsonschema==3.2.0 jsonschema==3.2.0
@ -16,13 +16,13 @@ tabulate==0.8.7
pycoingecko==1.4.0 pycoingecko==1.4.0
jinja2==2.11.2 jinja2==2.11.2
tables==3.6.1 tables==3.6.1
blosc==1.9.2 blosc==1.10.1
# find first, C search in arrays # find first, C search in arrays
py_find_1st==1.1.4 py_find_1st==1.1.4
# Load ticker files 30% faster # Load ticker files 30% faster
python-rapidjson==0.9.4 python-rapidjson==1.0
# Notify systemd # Notify systemd
sdnotify==0.3.2 sdnotify==0.3.2
@ -35,5 +35,5 @@ flask-cors==3.0.9
# Support for colorized terminal output # Support for colorized terminal output
colorama==0.4.4 colorama==0.4.4
# Building config files interactively # Building config files interactively
questionary==1.8.1 questionary==1.9.0
prompt-toolkit==3.0.8 prompt-toolkit==3.0.8

View File

@ -139,6 +139,13 @@ class FtRestClient():
""" """
return self._get("profit") return self._get("profit")
def stats(self):
"""Return the stats report (durations, sell-reasons).
:return: json object
"""
return self._get("stats")
def performance(self): def performance(self):
"""Return the performance of the different coins. """Return the performance of the different coins.

View File

@ -3,9 +3,9 @@ from sys import version_info
from setuptools import setup from setuptools import setup
if version_info.major == 3 and version_info.minor < 6 or \ if version_info.major == 3 and version_info.minor < 7 or \
version_info.major < 3: version_info.major < 3:
print('Your Python interpreter must be 3.6 or greater!') print('Your Python interpreter must be 3.7 or greater!')
exit(1) exit(1)
from pathlib import Path # noqa: E402 from pathlib import Path # noqa: E402
@ -109,7 +109,6 @@ setup(name='freqtrade',
'Environment :: Console', 'Environment :: Console',
'Intended Audience :: Science/Research', 'Intended Audience :: Science/Research',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.8',
'Operating System :: MacOS', 'Operating System :: MacOS',

View File

@ -25,6 +25,14 @@ function check_installed_python() {
return return
fi fi
which python3.9
if [ $? -eq 0 ]; then
echo "using Python 3.9"
PYTHON=python3.9
check_installed_pip
return
fi
which python3.7 which python3.7
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "using Python 3.7" echo "using Python 3.7"
@ -33,16 +41,9 @@ function check_installed_python() {
return return
fi fi
which python3.6
if [ $? -eq 0 ]; then
echo "using Python 3.6"
PYTHON=python3.6
check_installed_pip
return
fi
if [ -z ${PYTHON} ]; then if [ -z ${PYTHON} ]; then
echo "No usable python found. Please make sure to have python3.6 or python3.7 installed" echo "No usable python found. Please make sure to have python3.7 or newer installed"
exit 1 exit 1
fi fi
} }
@ -56,18 +57,45 @@ function updateenv() {
exit 1 exit 1
fi fi
source .env/bin/activate source .env/bin/activate
SYS_ARCH=$(uname -m)
echo "pip install in-progress. Please wait..." echo "pip install in-progress. Please wait..."
${PYTHON} -m pip install --upgrade pip ${PYTHON} -m pip install --upgrade pip
read -p "Do you want to install dependencies for dev [y/N]? " read -p "Do you want to install dependencies for dev [y/N]? "
if [[ $REPLY =~ ^[Yy]$ ]] if [[ $REPLY =~ ^[Yy]$ ]]
then then
${PYTHON} -m pip install --upgrade -r requirements-dev.txt REQUIREMENTS=requirements-dev.txt
else else
${PYTHON} -m pip install --upgrade -r requirements.txt REQUIREMENTS=requirements.txt
echo "Dev dependencies ignored." fi
REQUIREMENTS_HYPEROPT=""
REQUIREMENTS_PLOT=""
read -p "Do you want to install plotting dependencies (plotly) [y/N]? "
if [[ $REPLY =~ ^[Yy]$ ]]
then
REQUIREMENTS_PLOT="-r requirements-plot.txt"
fi
if [ "${SYS_ARCH}" == "armv7l" ]; then
echo "Detected Raspberry, installing cython, skipping hyperopt installation."
${PYTHON} -m pip install --upgrade cython
else
# Is not Raspberry
read -p "Do you want to install hyperopt dependencies [y/N]? "
if [[ $REPLY =~ ^[Yy]$ ]]
then
REQUIREMENTS_HYPEROPT="-r requirements-hyperopt.txt"
fi
fi fi
${PYTHON} -m pip install --upgrade -r ${REQUIREMENTS} ${REQUIREMENTS_HYPEROPT} ${REQUIREMENTS_PLOT}
if [ $? -ne 0 ]; then
echo "Failed installing dependencies"
exit 1
fi
${PYTHON} -m pip install -e . ${PYTHON} -m pip install -e .
if [ $? -ne 0 ]; then
echo "Failed installing Freqtrade"
exit 1
fi
echo "pip install completed" echo "pip install completed"
echo echo
} }
@ -134,11 +162,11 @@ function reset() {
git fetch -a git fetch -a
if [ "1" == $(git branch -vv |grep -c "* develop") ] if [ "1" == $(git branch -vv | grep -c "* develop") ]
then then
echo "- Hard resetting of 'develop' branch." echo "- Hard resetting of 'develop' branch."
git reset --hard origin/develop git reset --hard origin/develop
elif [ "1" == $(git branch -vv |grep -c "* stable") ] elif [ "1" == $(git branch -vv | grep -c "* stable") ]
then then
echo "- Hard resetting of 'stable' branch." echo "- Hard resetting of 'stable' branch."
git reset --hard origin/stable git reset --hard origin/stable
@ -149,7 +177,7 @@ function reset() {
fi fi
if [ -d ".env" ]; then if [ -d ".env" ]; then
echo "- Delete your previous virtual env" echo "- Deleting your previous virtual env"
rm -rf .env rm -rf .env
fi fi
echo echo
@ -253,7 +281,7 @@ function install() {
echo "Run the bot !" echo "Run the bot !"
echo "-------------------------" echo "-------------------------"
echo "You can now use the bot by executing 'source .env/bin/activate; freqtrade <subcommand>'." echo "You can now use the bot by executing 'source .env/bin/activate; freqtrade <subcommand>'."
echo "You can see the list of available bot subcommands by executing 'source .env/bin/activate; freqtrade --help'." echo "You can see the list of available bot sub-commands by executing 'source .env/bin/activate; freqtrade --help'."
echo "You verify that freqtrade is installed successfully by running 'source .env/bin/activate; freqtrade --version'." echo "You verify that freqtrade is installed successfully by running 'source .env/bin/activate; freqtrade --version'."
} }
@ -275,7 +303,7 @@ function help() {
echo " -p,--plot Install dependencies for Plotting scripts." echo " -p,--plot Install dependencies for Plotting scripts."
} }
# Verify if 3.6 or 3.7 is installed # Verify if 3.7 or 3.8 is installed
check_installed_python check_installed_python
case $* in case $* in

View File

@ -33,6 +33,19 @@ logging.getLogger('').setLevel(logging.INFO)
np.seterr(all='raise') np.seterr(all='raise')
def pytest_addoption(parser):
parser.addoption('--longrun', action='store_true', dest="longrun",
default=False, help="Enable long-run tests (ccxt compat)")
def pytest_configure(config):
config.addinivalue_line(
"markers", "longrun: mark test that is running slowly and should not be run regularily"
)
if not config.option.longrun:
setattr(config.option, 'markexpr', 'not longrun')
def log_has(line, logs): def log_has(line, logs):
# caplog mocker returns log as a tuple: ('freqtrade.something', logging.WARNING, 'foobar') # caplog mocker returns log as a tuple: ('freqtrade.something', logging.WARNING, 'foobar')
# and we want to match line against foobar in the tuple # and we want to match line against foobar in the tuple
@ -224,6 +237,10 @@ def init_persistence(default_conf):
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def default_conf(testdatadir): def default_conf(testdatadir):
return get_default_conf(testdatadir)
def get_default_conf(testdatadir):
""" Returns validated configuration suitable for most tests """ """ Returns validated configuration suitable for most tests """
configuration = { configuration = {
"max_open_trades": 1, "max_open_trades": 1,
@ -1084,7 +1101,7 @@ def ohlcv_history_list():
@pytest.fixture @pytest.fixture
def ohlcv_history(ohlcv_history_list): def ohlcv_history(ohlcv_history_list):
return ohlcv_to_dataframe(ohlcv_history_list, "5m", pair="UNITTEST/BTC", return ohlcv_to_dataframe(ohlcv_history_list, "5m", pair="UNITTEST/BTC",
fill_missing=True) fill_missing=True, drop_incomplete=False)
@pytest.fixture @pytest.fixture
@ -1588,16 +1605,7 @@ def fetch_trades_result():
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def trades_for_order2(): def trades_for_order2():
return [{'info': {'id': 34567, return [{'info': {},
'orderId': 123456,
'price': '0.24544100',
'qty': '8.00000000',
'commission': '0.00800000',
'commissionAsset': 'LTC',
'time': 1521663363189,
'isBuyer': True,
'isMaker': False,
'isBestMatch': True},
'timestamp': 1521663363189, 'timestamp': 1521663363189,
'datetime': '2018-03-21T20:16:03.189Z', 'datetime': '2018-03-21T20:16:03.189Z',
'symbol': 'LTC/ETH', 'symbol': 'LTC/ETH',
@ -1609,16 +1617,7 @@ def trades_for_order2():
'cost': 1.963528, 'cost': 1.963528,
'amount': 4.0, 'amount': 4.0,
'fee': {'cost': 0.004, 'currency': 'LTC'}}, 'fee': {'cost': 0.004, 'currency': 'LTC'}},
{'info': {'id': 34567, {'info': {},
'orderId': 123456,
'price': '0.24544100',
'qty': '8.00000000',
'commission': '0.00800000',
'commissionAsset': 'LTC',
'time': 1521663363189,
'isBuyer': True,
'isMaker': False,
'isBestMatch': True},
'timestamp': 1521663363189, 'timestamp': 1521663363189,
'datetime': '2018-03-21T20:16:03.189Z', 'datetime': '2018-03-21T20:16:03.189Z',
'symbol': 'LTC/ETH', 'symbol': 'LTC/ETH',
@ -1632,6 +1631,14 @@ def trades_for_order2():
'fee': {'cost': 0.004, 'currency': 'LTC'}}] 'fee': {'cost': 0.004, 'currency': 'LTC'}}]
@pytest.fixture(scope="function")
def trades_for_order3(trades_for_order2):
# Different fee currencies for each trade
trades_for_order = deepcopy(trades_for_order2)
trades_for_order[0]['fee'] = {'cost': 0.02, 'currency': 'BNB'}
return trades_for_order
@pytest.fixture @pytest.fixture
def buy_order_fee(): def buy_order_fee():
return { return {

View File

@ -1,3 +1,5 @@
from datetime import datetime, timedelta, timezone
from freqtrade.persistence.models import Order, Trade from freqtrade.persistence.models import Order, Trade
@ -82,6 +84,9 @@ def mock_trade_2(fee):
is_open=False, is_open=False,
open_order_id='dry_run_sell_12345', open_order_id='dry_run_sell_12345',
strategy='DefaultStrategy', strategy='DefaultStrategy',
sell_reason='sell_signal',
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=20),
close_date=datetime.now(tz=timezone.utc),
) )
o = Order.parse_from_ccxt_object(mock_order_2(), 'ETC/BTC', 'buy') o = Order.parse_from_ccxt_object(mock_order_2(), 'ETC/BTC', 'buy')
trade.orders.append(o) trade.orders.append(o)
@ -134,6 +139,9 @@ def mock_trade_3(fee):
close_profit=0.01, close_profit=0.01,
exchange='bittrex', exchange='bittrex',
is_open=False, is_open=False,
sell_reason='roi',
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=20),
close_date=datetime.now(tz=timezone.utc),
) )
o = Order.parse_from_ccxt_object(mock_order_3(), 'XRP/BTC', 'buy') o = Order.parse_from_ccxt_object(mock_order_3(), 'XRP/BTC', 'buy')
trade.orders.append(o) trade.orders.append(o)

View File

@ -6,7 +6,7 @@ from pandas import DataFrame
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
from freqtrade.exceptions import ExchangeError, OperationalException from freqtrade.exceptions import ExchangeError, OperationalException
from freqtrade.pairlist.pairlistmanager import PairListManager from freqtrade.plugins.pairlistmanager import PairListManager
from freqtrade.state import RunMode from freqtrade.state import RunMode
from tests.conftest import get_patched_exchange from tests.conftest import get_patched_exchange

View File

@ -0,0 +1,134 @@
"""
Tests in this file do NOT mock network calls, so they are expected to be fluky at times.
However, these tests should give a good idea to determine if a new exchange is
suitable to run with freqtrade.
"""
from pathlib import Path
import pytest
from freqtrade.resolvers.exchange_resolver import ExchangeResolver
from tests.conftest import get_default_conf
# Exchanges that should be tested
EXCHANGES = {
'bittrex': {
'pair': 'BTC/USDT',
'hasQuoteVolume': False,
'timeframe': '5m',
},
'binance': {
'pair': 'BTC/USDT',
'hasQuoteVolume': True,
'timeframe': '5m',
},
'kraken': {
'pair': 'BTC/USDT',
'hasQuoteVolume': True,
'timeframe': '5m',
},
'ftx': {
'pair': 'BTC/USDT',
'hasQuoteVolume': True,
'timeframe': '5m',
}
}
@pytest.fixture(scope="class")
def exchange_conf():
config = get_default_conf((Path(__file__).parent / "testdata").resolve())
config['exchange']['pair_whitelist'] = []
return config
@pytest.fixture(params=EXCHANGES, scope="class")
def exchange(request, exchange_conf):
exchange_conf['exchange']['name'] = request.param
exchange = ExchangeResolver.load_exchange(request.param, exchange_conf, validate=True)
yield exchange, request.param
@pytest.mark.longrun
class TestCCXTExchange():
def test_load_markets(self, exchange):
exchange, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
markets = exchange.markets
assert pair in markets
assert isinstance(markets[pair], dict)
def test_ccxt_fetch_tickers(self, exchange):
exchange, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
tickers = exchange.get_tickers()
assert pair in tickers
assert 'ask' in tickers[pair]
assert tickers[pair]['ask'] is not None
assert 'bid' in tickers[pair]
assert tickers[pair]['bid'] is not None
assert 'quoteVolume' in tickers[pair]
if EXCHANGES[exchangename].get('hasQuoteVolume'):
assert tickers[pair]['quoteVolume'] is not None
def test_ccxt_fetch_ticker(self, exchange):
exchange, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
ticker = exchange.fetch_ticker(pair)
assert 'ask' in ticker
assert ticker['ask'] is not None
assert 'bid' in ticker
assert ticker['bid'] is not None
assert 'quoteVolume' in ticker
if EXCHANGES[exchangename].get('hasQuoteVolume'):
assert ticker['quoteVolume'] is not None
def test_ccxt_fetch_l2_orderbook(self, exchange):
exchange, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
l2 = exchange.fetch_l2_order_book(pair)
assert 'asks' in l2
assert 'bids' in l2
l2_limit_range = exchange._ft_has['l2_limit_range']
for val in [1, 2, 5, 25, 100]:
l2 = exchange.fetch_l2_order_book(pair, val)
if not l2_limit_range or val in l2_limit_range:
assert len(l2['asks']) == val
assert len(l2['bids']) == val
else:
next_limit = exchange.get_next_limit_in_list(val, l2_limit_range)
if next_limit > 200:
# Large orderbook sizes can be a problem for some exchanges (bitrex ...)
assert len(l2['asks']) > 200
assert len(l2['asks']) > 200
else:
assert len(l2['asks']) == next_limit
assert len(l2['asks']) == next_limit
def test_fetch_ohlcv(self, exchange):
exchange, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
timeframe = EXCHANGES[exchangename]['timeframe']
pair_tf = (pair, timeframe)
ohlcv = exchange.refresh_latest_ohlcv([pair_tf])
assert isinstance(ohlcv, dict)
assert len(ohlcv[pair_tf]) == len(exchange.klines(pair_tf))
assert len(exchange.klines(pair_tf)) > 200
# TODO: tests fetch_trades (?)
def test_ccxt_get_fee(self, exchange):
exchange, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
assert 0 < exchange.get_fee(pair, 'limit', 'buy') < 1
assert 0 < exchange.get_fee(pair, 'limit', 'sell') < 1
assert 0 < exchange.get_fee(pair, 'market', 'buy') < 1
assert 0 < exchange.get_fee(pair, 'market', 'sell') < 1

View File

@ -1385,6 +1385,12 @@ def test_refresh_latest_ohlcv(mocker, default_conf, caplog) -> None:
pairs = [('IOTA/ETH', '5m'), ('XRP/ETH', '5m')] pairs = [('IOTA/ETH', '5m'), ('XRP/ETH', '5m')]
# empty dicts # empty dicts
assert not exchange._klines assert not exchange._klines
exchange.refresh_latest_ohlcv(pairs, cache=False)
# No caching
assert not exchange._klines
assert exchange._api_async.fetch_ohlcv.call_count == 2
exchange._api_async.fetch_ohlcv.reset_mock()
exchange.refresh_latest_ohlcv(pairs) exchange.refresh_latest_ohlcv(pairs)
assert log_has(f'Refreshing candle (OHLCV) data for {len(pairs)} pairs', caplog) assert log_has(f'Refreshing candle (OHLCV) data for {len(pairs)} pairs', caplog)
@ -1499,11 +1505,9 @@ def test_refresh_latest_ohlcv_inv_result(default_conf, mocker, caplog):
assert exchange._klines assert exchange._klines
assert exchange._api_async.fetch_ohlcv.call_count == 2 assert exchange._api_async.fetch_ohlcv.call_count == 2
assert type(res) is list assert type(res) is dict
assert len(res) == 2 assert len(res) == 1
# Test that each is in list at least once as order is not guaranteed # Test that each is in list at least once as order is not guaranteed
assert type(res[0]) is tuple or type(res[1]) is tuple
assert type(res[0]) is TypeError or type(res[1]) is TypeError
assert log_has("Error loading ETH/BTC. Result was [[]].", caplog) assert log_has("Error loading ETH/BTC. Result was [[]].", caplog)
assert log_has("Async code raised an exception: TypeError", caplog) assert log_has("Async code raised an exception: TypeError", caplog)
@ -2149,7 +2153,7 @@ def test_get_fee(default_conf, mocker, exchange_name):
def test_stoploss_order_unsupported_exchange(default_conf, mocker): def test_stoploss_order_unsupported_exchange(default_conf, mocker):
exchange = get_patched_exchange(mocker, default_conf, 'bittrex') exchange = get_patched_exchange(mocker, default_conf, id='bittrex')
with pytest.raises(OperationalException, match=r"stoploss is not implemented .*"): with pytest.raises(OperationalException, match=r"stoploss is not implemented .*"):
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})

Some files were not shown because too many files have changed in this diff Show More