diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 195370339..47b9a9279 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: strategy: matrix: os: [ ubuntu-18.04, ubuntu-20.04, ubuntu-22.04 ] - python-version: ["3.8", "3.9", "3.10.6"] + python-version: ["3.8", "3.9", "3.10"] steps: - uses: actions/checkout@v3 @@ -74,7 +74,7 @@ jobs: if: matrix.python-version == '3.9' && matrix.os == 'ubuntu-22.04' - name: Coveralls - if: (runner.os == 'Linux' && matrix.python-version == '3.9') + if: (runner.os == 'Linux' && matrix.python-version == '3.10' && matrix.os == 'ubuntu-22.04') env: # Coveralls token. Not used as secret due to github not providing secrets to forked repositories COVERALLS_REPO_TOKEN: 6D1m0xupS3FgutfuGao8keFf9Hc0FpIXu @@ -121,7 +121,7 @@ jobs: strategy: matrix: os: [ macos-latest ] - python-version: ["3.8", "3.9", "3.10.6"] + python-version: ["3.8", "3.9", "3.10"] steps: - uses: actions/checkout@v3 @@ -205,7 +205,7 @@ jobs: strategy: matrix: os: [ windows-latest ] - python-version: ["3.8", "3.9", "3.10.6"] + python-version: ["3.8", "3.9", "3.10"] steps: - uses: actions/checkout@v3 @@ -441,4 +441,4 @@ jobs: with: severity: info details: Deploy Succeeded! - webhookUrl: ${{ secrets.DISCORD_WEBHOOK }} \ No newline at end of file + webhookUrl: ${{ secrets.DISCORD_WEBHOOK }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2cad0a7d3..ca42402dd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,9 +15,9 @@ repos: additional_dependencies: - types-cachetools==5.2.1 - types-filelock==3.2.7 - - types-requests==2.28.11 - - types-tabulate==0.8.11 - - types-python-dateutil==2.8.19 + - types-requests==2.28.11.2 + - types-tabulate==0.9.0.0 + - types-python-dateutil==2.8.19.2 # stages: [push] - repo: https://github.com/pycqa/isort diff --git a/build_helpers/pyarrow-9.0.0-cp39-cp39-linux_armv7l.whl b/build_helpers/pyarrow-9.0.0-cp39-cp39-linux_armv7l.whl new file mode 100644 index 000000000..221d6561d Binary files /dev/null and b/build_helpers/pyarrow-9.0.0-cp39-cp39-linux_armv7l.whl differ diff --git a/config_examples/config_binance.example.json b/config_examples/config_binance.example.json index 35b9fcd20..3e99bd114 100644 --- a/config_examples/config_binance.example.json +++ b/config_examples/config_binance.example.json @@ -53,7 +53,7 @@ "XTZ/BTC" ], "pair_blacklist": [ - "BNB/BTC" + "BNB/.*" ] }, "pairlists": [ diff --git a/config_examples/config_freqai.example.json b/config_examples/config_freqai.example.json index db8ae7181..5e564a1fc 100644 --- a/config_examples/config_freqai.example.json +++ b/config_examples/config_freqai.example.json @@ -18,13 +18,8 @@ "name": "binance", "key": "", "secret": "", - "ccxt_config": { - "enableRateLimit": true - }, - "ccxt_async_config": { - "enableRateLimit": true, - "rateLimit": 200 - }, + "ccxt_config": {}, + "ccxt_async_config": {}, "pair_whitelist": [ "1INCH/USDT", "ALGO/USDT" diff --git a/docker/Dockerfile.armhf b/docker/Dockerfile.armhf index 73fc681eb..7b663ae6c 100644 --- a/docker/Dockerfile.armhf +++ b/docker/Dockerfile.armhf @@ -11,7 +11,7 @@ ENV FT_APP_ENV="docker" # Prepare environment RUN mkdir /freqtrade \ && apt-get update \ - && apt-get -y install sudo libatlas3-base curl sqlite3 libhdf5-dev \ + && apt-get -y install sudo libatlas3-base curl sqlite3 libhdf5-dev libutf8proc-dev libsnappy-dev \ && apt-get clean \ && useradd -u 1000 -G sudo -U -m ftuser \ && chown ftuser:ftuser /freqtrade \ @@ -37,6 +37,7 @@ ENV LD_LIBRARY_PATH /usr/local/lib COPY --chown=ftuser:ftuser requirements.txt /freqtrade/ USER ftuser RUN pip install --user --no-cache-dir numpy \ + && pip install --user /tmp/pyarrow-*.whl \ && pip install --user --no-cache-dir -r requirements.txt # Copy dependencies to runtime-image diff --git a/docs/advanced-hyperopt.md b/docs/advanced-hyperopt.md index 9933628d1..0dace9985 100644 --- a/docs/advanced-hyperopt.md +++ b/docs/advanced-hyperopt.md @@ -78,6 +78,8 @@ This function needs to return a floating point number (`float`). Smaller numbers To override a pre-defined space (`roi_space`, `generate_roi_table`, `stoploss_space`, `trailing_space`), define a nested class called Hyperopt and define the required spaces as follows: ```python +from freqtrade.optimize.space import Categorical, Dimension, Integer, SKDecimal + class MyAwesomeStrategy(IStrategy): class HyperOpt: # Define a custom stoploss space. @@ -94,6 +96,33 @@ class MyAwesomeStrategy(IStrategy): SKDecimal(0.01, 0.07, decimals=3, name='roi_p2'), SKDecimal(0.01, 0.20, decimals=3, name='roi_p3'), ] + + def generate_roi_table(params: Dict) -> Dict[int, float]: + + roi_table = {} + roi_table[0] = params['roi_p1'] + params['roi_p2'] + params['roi_p3'] + roi_table[params['roi_t3']] = params['roi_p1'] + params['roi_p2'] + roi_table[params['roi_t3'] + params['roi_t2']] = params['roi_p1'] + roi_table[params['roi_t3'] + params['roi_t2'] + params['roi_t1']] = 0 + + return roi_table + + def trailing_space() -> List[Dimension]: + # All parameters here are mandatory, you can only modify their type or the range. + return [ + # Fixed to true, if optimizing trailing_stop we assume to use trailing stop at all times. + Categorical([True], name='trailing_stop'), + + SKDecimal(0.01, 0.35, decimals=3, name='trailing_stop_positive'), + # 'trailing_stop_positive_offset' should be greater than 'trailing_stop_positive', + # so this intermediate parameter is used as the value of the difference between + # them. The value of the 'trailing_stop_positive_offset' is constructed in the + # generate_trailing_params() method. + # This is similar to the hyperspace dimensions used for constructing the ROI tables. + SKDecimal(0.001, 0.1, decimals=3, name='trailing_stop_positive_offset_p1'), + + Categorical([True, False], name='trailing_only_offset_is_reached'), + ] ``` !!! Note diff --git a/docs/backtesting.md b/docs/backtesting.md index f20a53d22..e3cddb7a1 100644 --- a/docs/backtesting.md +++ b/docs/backtesting.md @@ -522,13 +522,13 @@ Since backtesting lacks some detailed information about what happens within a ca - ROI - exits are compared to high - but the ROI value is used (e.g. ROI = 2%, high=5% - so the exit will be at 2%) - exits are never "below the candle", so a ROI of 2% may result in a exit at 2.4% if low was at 2.4% profit - - Forceexits caused by `=-1` ROI entries use low as exit value, unless N falls on the candle open (e.g. `120: -1` for 1h candles) + - Force-exits caused by `=-1` ROI entries use low as exit value, unless N falls on the candle open (e.g. `120: -1` for 1h candles) - Stoploss exits 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` exit 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 is only adjusted if it's below the candle's low (otherwise it would be triggered) - - On trade entry candles that trigger trailing stoploss, the "minimum offset" (`stop_positive_offset`) is assumed (instead of high) - and the stop is calculated from this point + - On trade entry candles that trigger trailing stoploss, the "minimum offset" (`stop_positive_offset`) is assumed (instead of high) - and the stop is calculated from this point. This rule is NOT applicable to custom-stoploss scenarios, since there's no information about the stoploss logic available. - High happens first - adjusting stoploss - Low uses the adjusted stoploss (so exits 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 @@ -546,8 +546,8 @@ In addition to the above assumptions, strategy authors should carefully read the ### Trading limits in backtesting -Exchanges have certain trading limits, like minimum base currency, or minimum stake (quote) currency. -These limits are usually listed in the exchange documentation as "trading rules" or similar. +Exchanges have certain trading limits, like minimum (and maximum) base currency, or minimum/maximum stake (quote) currency. +These limits are usually listed in the exchange documentation as "trading rules" or similar and can be quite different between different pairs. Backtesting (as well as live and dry-run) does honor these limits, and will ensure that a stoploss can be placed below this value - so the value will be slightly higher than what the exchange specifies. Freqtrade has however no information about historic limits. diff --git a/docs/configuration.md b/docs/configuration.md index 556414e21..e773e1878 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -215,16 +215,18 @@ Mandatory parameters are marked as **Required**, which means that they are requi | `telegram.balance_dust_level` | Dust-level (in stake currency) - currencies with a balance below this will not be shown by `/balance`.
**Datatype:** float | `telegram.reload` | Allow "reload" buttons on telegram messages.
*Defaults to `True`.
**Datatype:** boolean | `telegram.notification_settings.*` | Detailed notification settings. Refer to the [telegram documentation](telegram-usage.md) for details.
**Datatype:** dictionary +| `telegram.allow_custom_messages` | Enable the sending of Telegram messages from strategies via the dataprovider.send_msg() function.
**Datatype:** Boolean | | **Webhook** | `webhook.enabled` | Enable usage of Webhook notifications
**Datatype:** Boolean | `webhook.url` | URL for the webhook. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String -| `webhook.webhookentry` | Payload to send on entry. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String -| `webhook.webhookentrycancel` | Payload to send on entry order cancel. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String -| `webhook.webhookentryfill` | Payload to send on entry order filled. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String -| `webhook.webhookexit` | Payload to send on exit. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String -| `webhook.webhookexitcancel` | Payload to send on exit order cancel. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String -| `webhook.webhookexitfill` | Payload to send on exit order filled. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String -| `webhook.webhookstatus` | Payload to send on status calls. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String +| `webhook.entry` | Payload to send on entry. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String +| `webhook.entry_cancel` | Payload to send on entry order cancel. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String +| `webhook.entry_fill` | Payload to send on entry order filled. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String +| `webhook.exit` | Payload to send on exit. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String +| `webhook.exit_cancel` | Payload to send on exit order cancel. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String +| `webhook.exit_fill` | Payload to send on exit order filled. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String +| `webhook.status` | Payload to send on status calls. Only required if `webhook.enabled` is `true`. See the [webhook documentation](webhook-config.md) for more details.
**Datatype:** String +| `webhook.allow_custom_messages` | Enable the sending of Webhook messages from strategies via the dataprovider.send_msg() function.
**Datatype:** Boolean | | **Rest API / FreqUI / Producer-Consumer** | `api_server.enabled` | Enable usage of API Server. See the [API Server documentation](rest-api.md) for more details.
**Datatype:** Boolean | `api_server.listen_ip_address` | Bind IP address. See the [API Server documentation](rest-api.md) for more details.
**Datatype:** IPv4 diff --git a/docs/deprecated.md b/docs/deprecated.md index beceb12ab..3b5b28b81 100644 --- a/docs/deprecated.md +++ b/docs/deprecated.md @@ -66,11 +66,11 @@ We will keep a compatibility layer for 1-2 versions (so both `buy_tag` and `ente #### Naming changes -Webhook terminology changed from "sell" to "exit", and from "buy" to "entry". +Webhook terminology changed from "sell" to "exit", and from "buy" to "entry", removing "webhook" in the process. -* `webhookbuy` -> `webhookentry` -* `webhookbuyfill` -> `webhookentryfill` -* `webhookbuycancel` -> `webhookentrycancel` -* `webhooksell` -> `webhookexit` -* `webhooksellfill` -> `webhookexitfill` -* `webhooksellcancel` -> `webhookexitcancel` +* `webhookbuy`, `webhookentry` -> `entry` +* `webhookbuyfill`, `webhookentryfill` -> `entry_fill` +* `webhookbuycancel`, `webhookentrycancel` -> `entry_cancel` +* `webhooksell`, `webhookexit` -> `exit` +* `webhooksellfill`, `webhookexitfill` -> `exit_fill` +* `webhooksellcancel`, `webhookexitcancel` -> `exit_cancel` diff --git a/docs/faq.md b/docs/faq.md index a72268ef9..bcceaf898 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -102,6 +102,12 @@ If this happens for all pairs in the pairlist, this might indicate a recent exch Irrespectively of the reason, Freqtrade will fill up these candles with "empty" candles, where open, high, low and close are set to the previous candle close - and volume is empty. In a chart, this will look like a `_` - and is aligned with how exchanges usually represent 0 volume candles. +### I'm getting "Price jump between 2 candles detected" + +This message is a warning that the candles had a price jump of > 30%. +This might be a sign that the pair stopped trading, and some token exchange took place (e.g. COCOS in 2021 - where price jumped from 0.0000154 to 0.01621). +This message is often accompanied by ["Missing data fillup"](#im-getting-missing-data-fillup-messages-in-the-log) - as trading on such pairs is often stopped for some time. + ### I'm getting "Outdated history for pair xxx" in the log The bot is trying to tell you that it got an outdated last candle (not the last complete candle). diff --git a/docs/freqai-configuration.md b/docs/freqai-configuration.md index d24c60057..59d72e337 100644 --- a/docs/freqai-configuration.md +++ b/docs/freqai-configuration.md @@ -192,11 +192,11 @@ dataframe["target_roi"] = dataframe["&-s_close_mean"] + dataframe["&-s_close_std dataframe["sell_roi"] = dataframe["&-s_close_mean"] - dataframe["&-s_close_std"] * 1.25 ``` -To consider the population of *historical predictions* for creating the dynamic target instead of information from the training as discussed above, you would set `fit_live_prediction_candles` in the config to the number of historical prediction candles you wish to use to generate target statistics. +To consider the population of *historical predictions* for creating the dynamic target instead of information from the training as discussed above, you would set `fit_live_predictions_candles` in the config to the number of historical prediction candles you wish to use to generate target statistics. ```json "freqai": { - "fit_live_prediction_candles": 300, + "fit_live_predictions_candles": 300, } ``` @@ -204,14 +204,44 @@ If this value is set, FreqAI will initially use the predictions from the trainin ## Using different prediction models -FreqAI has multiple example prediction model libraries that are ready to be used as is via the flag `--freqaimodel`. These libraries include `Catboost`, `LightGBM`, and `XGBoost` regression, classification, and multi-target models, and can be found in `freqai/prediction_models/`. However, it is possible to customize and create your own prediction models using the `IFreqaiModel` class. You are encouraged to inherit `fit()`, `train()`, and `predict()` to let these customize various aspects of the training procedures. +FreqAI has multiple example prediction model libraries that are ready to be used as is via the flag `--freqaimodel`. These libraries include `CatBoost`, `LightGBM`, and `XGBoost` regression, classification, and multi-target models, and can be found in `freqai/prediction_models/`. -### Setting classifier targets +Regression and classification models differ in what targets they predict - a regression model will predict a target of continuous values, for example what price BTC will be at tomorrow, whilst a classifier will predict a target of discrete values, for example if the price of BTC will go up tomorrow or not. This means that you have to specify your targets differently depending on which model type you are using (see details [below](#setting-model-targets)). -FreqAI includes a variety of classifiers, such as the `CatboostClassifier` via the flag `--freqaimodel CatboostClassifier`. If you elects to use a classifier, the classes need to be set using strings. For example: +All of the aforementioned model libraries implement gradient boosted decision tree algorithms. They all work on the principle of ensemble learning, where predictions from multiple simple learners are combined to get a final prediction that is more stable and generalized. The simple learners in this case are decision trees. Gradient boosting refers to the method of learning, where each simple learner is built in sequence - the subsequent learner is used to improve on the error from the previous learner. If you want to learn more about the different model libraries you can find the information in their respective docs: + +* CatBoost: https://catboost.ai/en/docs/ +* LightGBM: https://lightgbm.readthedocs.io/en/v3.3.2/# +* XGBoost: https://xgboost.readthedocs.io/en/stable/# + +There are also numerous online articles describing and comparing the algorithms. Some relatively light-weight examples would be [CatBoost vs. LightGBM vs. XGBoost — Which is the best algorithm?](https://towardsdatascience.com/catboost-vs-lightgbm-vs-xgboost-c80f40662924#:~:text=In%20CatBoost%2C%20symmetric%20trees%2C%20or,the%20same%20depth%20can%20differ.) and [XGBoost, LightGBM or CatBoost — which boosting algorithm should I use?](https://medium.com/riskified-technology/xgboost-lightgbm-or-catboost-which-boosting-algorithm-should-i-use-e7fda7bb36bc). Keep in mind that the performance of each model is highly dependent on the application and so any reported metrics might not be true for your particular use of the model. + +Apart from the models already available in FreqAI, it is also possible to customize and create your own prediction models using the `IFreqaiModel` class. You are encouraged to inherit `fit()`, `train()`, and `predict()` to customize various aspects of the training procedures. You can place custom FreqAI models in `user_data/freqaimodels` - and freqtrade will pick them up from there based on the provided `--freqaimodel` name - which has to correspond to the class name of your custom model. +Make sure to use unique names to avoid overriding built-in models. + +### Setting model targets + +#### Regressors + +If you are using a regressor, you need to specify a target that has continuous values. FreqAI includes a variety of regressors, such as the `CatboostRegressor`via the flag `--freqaimodel CatboostRegressor`. An example of how you could set a regression target for predicting the price 100 candles into the future would be + +```python +df['&s-close_price'] = df['close'].shift(-100) +``` + +If you want to predict multiple targets, you need to define multiple labels using the same syntax as shown above. + +#### Classifiers + +If you are using a classifier, you need to specify a target that has discrete values. FreqAI includes a variety of classifiers, such as the `CatboostClassifier` via the flag `--freqaimodel CatboostClassifier`. If you elects to use a classifier, the classes need to be set using strings. For example, if you want to predict if the price 100 candles into the future goes up or down you would set ```python df['&s-up_or_down'] = np.where( df["close"].shift(-100) > df["close"], 'up', 'down') ``` -Additionally, the example classifier models do not accommodate multiple labels, but they do allow multi-class classification within a single label column. +If you want to predict multiple targets you must specify all labels in the same label column. You could, for example, add the label `same` to define where the price was unchanged by setting + +```python +df['&s-up_or_down'] = np.where( df["close"].shift(-100) > df["close"], 'up', 'down') +df['&s-up_or_down'] = np.where( df["close"].shift(-100) == df["close"], 'same', df['&s-up_or_down']) +``` diff --git a/docs/freqai-parameter-table.md b/docs/freqai-parameter-table.md index ffe1c9d32..f22fd4382 100644 --- a/docs/freqai-parameter-table.md +++ b/docs/freqai-parameter-table.md @@ -37,12 +37,13 @@ Mandatory parameters are marked as **Required** and have to be set in one of the | `noise_standard_deviation` | If set, FreqAI adds noise to the training features with the aim of preventing overfitting. FreqAI generates random deviates from a gaussian distribution with a standard deviation of `noise_standard_deviation` and adds them to all data points. `noise_standard_deviation` should be kept relative to the normalized space, i.e., between -1 and 1. In other words, since data in FreqAI is always normalized to be between -1 and 1, `noise_standard_deviation: 0.05` would result in 32% of the data being randomly increased/decreased by more than 2.5% (i.e., the percent of data falling within the first standard deviation).
**Datatype:** Integer.
Default: `0`. | `outlier_protection_percentage` | Enable to prevent outlier detection methods from discarding too much data. If more than `outlier_protection_percentage` % of points are detected as outliers by the SVM or DBSCAN, FreqAI will log a warning message and ignore outlier detection, i.e., the original dataset will be kept intact. If the outlier protection is triggered, no predictions will be made based on the training dataset.
**Datatype:** Float.
Default: `30`. | `reverse_train_test_order` | Split the feature dataset (see below) and use the latest data split for training and test on historical split of the data. This allows the model to be trained up to the most recent data point, while avoiding overfitting. However, you should be careful to understand the unorthodox nature of this parameter before employing it.
**Datatype:** Boolean.
Default: `False` (no reversal). +| `write_metrics_to_disk` | Collect train timings, inference timings and cpu usage in json file.
**Datatype:** Boolean.
Default: `False` | | **Data split parameters** | `data_split_parameters` | Include any additional parameters available from Scikit-learn `test_train_split()`, which are shown [here](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html) (external website).
**Datatype:** Dictionary. | `test_size` | The fraction of data that should be used for testing instead of training.
**Datatype:** Positive float < 1. | `shuffle` | Shuffle the training data points during training. Typically, to not remove the chronological order of data in time-series forecasting, this is set to `False`.
**Datatype:** Boolean.
Defaut: `False`. | | **Model training parameters** -| `model_training_parameters` | A flexible dictionary that includes all parameters available by the selected model library. For example, if you use `LightGBMRegressor`, this dictionary can contain any parameter available by the `LightGBMRegressor` [here](https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMRegressor.html) (external website). If you select a different model, this dictionary can contain any parameter from that model.
**Datatype:** Dictionary. +| `model_training_parameters` | A flexible dictionary that includes all parameters available by the selected model library. For example, if you use `LightGBMRegressor`, this dictionary can contain any parameter available by the `LightGBMRegressor` [here](https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMRegressor.html) (external website). If you select a different model, this dictionary can contain any parameter from that model. A list of the currently available models can be found [here](freqai-configuration.md#using-different-prediction-models).
**Datatype:** Dictionary. | `n_estimators` | The number of boosted trees to fit in the training of the model.
**Datatype:** Integer. | `learning_rate` | Boosting learning rate during training of the model.
**Datatype:** Float. | `n_jobs`, `thread_count`, `task_type` | Set the number of threads for parallel processing and the `task_type` (`gpu` or `cpu`). Different model libraries use different parameter names.
**Datatype:** Float. diff --git a/docs/freqai-running.md b/docs/freqai-running.md index b8994aed9..8947a02bf 100644 --- a/docs/freqai-running.md +++ b/docs/freqai-running.md @@ -142,15 +142,32 @@ dataframe['outlier'] = np.where(dataframe['DI_values'] > self.di_max.value/10, 1 This specific hyperopt would help you understand the appropriate `DI_values` for your particular parameter space. +## Using Tensorboard + +CatBoost models benefit from tracking training metrics via Tensorboard. You can take advantage of the FreqAI integration to track training and evaluation performance across all coins and across all retrainings. Tensorboard is activated via the following command: + +```bash +cd freqtrade +tensorboard --logdir user_data/models/unique-id +``` + +where `unique-id` is the `identifier` set in the `freqai` configuration file. This command must be run in a separate shell if you wish to view the output in your browser at 127.0.0.1:6060 (6060 is the default port used by Tensorboard). + +![tensorboard](assets/tensorboard.jpg) + ## Setting up a follower You can indicate to the bot that it should not train models, but instead should look for models trained by a leader with a specific `identifier` by defining: ```json "freqai": { + "enabled": true, "follow_mode": true, - "identifier": "example" + "identifier": "example", + "feature_parameters": { + // leader bots feature_parameters inserted here + }, } ``` -In this example, the user has a leader bot with the `"identifier": "example"`. The leader bot is already running or is launched simultaneously with the follower. The follower will load models created by the leader and inference them to obtain predictions instead of training its own models. +In this example, the user has a leader bot with the `"identifier": "example"`. The leader bot is already running or is launched simultaneously with the follower. The follower will load models created by the leader and inference them to obtain predictions instead of training its own models. The user will also need to duplicate the `feature_parameters` parameters from from the leaders freqai configuration file into the freqai section of the followers config. diff --git a/docs/requirements-docs.txt b/docs/requirements-docs.txt index 4ff1780cf..0e1e80e09 100644 --- a/docs/requirements-docs.txt +++ b/docs/requirements-docs.txt @@ -1,6 +1,6 @@ markdown==3.3.7 -mkdocs==1.4.0 -mkdocs-material==8.5.6 +mkdocs==1.4.1 +mkdocs-material==8.5.7 mdx_truly_sane_lists==1.3 -pymdown-extensions==9.6 +pymdown-extensions==9.7 jinja2==3.1.2 diff --git a/docs/stoploss.md b/docs/stoploss.md index 249c40109..a8285cf04 100644 --- a/docs/stoploss.md +++ b/docs/stoploss.md @@ -87,7 +87,7 @@ At this stage the bot contains the following stoploss support modes: 2. Trailing stop loss. 3. Trailing stop loss, custom positive loss. 4. Trailing stop loss only once the trade has reached a certain offset. -5. [Custom stoploss function](strategy-advanced.md#custom-stoploss) +5. [Custom stoploss function](strategy-callbacks.md#custom-stoploss) ### Static Stop Loss diff --git a/docs/strategy-callbacks.md b/docs/strategy-callbacks.md index ea10fc472..230968fb0 100644 --- a/docs/strategy-callbacks.md +++ b/docs/strategy-callbacks.md @@ -159,6 +159,7 @@ The stoploss price can only ever move upwards - if the stoploss value returned f The method must return a stoploss value (float / number) as a percentage of the current price. E.g. If the `current_rate` is 200 USD, then returning `0.02` will set the stoploss price 2% lower, at 196 USD. +During backtesting, `current_rate` (and `current_profit`) are provided against the candle's high (or low for short trades) - while the resulting stoploss is evaluated against the candle's low (or high for short trades). The absolute value of the return value is used (the sign is ignored), so returning `0.05` or `-0.05` have the same result, a stoploss 5% below the current price. diff --git a/docs/strategy-customization.md b/docs/strategy-customization.md index b97bd6d23..f036182e3 100644 --- a/docs/strategy-customization.md +++ b/docs/strategy-customization.md @@ -655,13 +655,13 @@ This is where calling `self.dp.current_whitelist()` comes in handy. # fetch live / historical candle (OHLCV) data for the first informative pair inf_pair, inf_timeframe = self.informative_pairs()[0] informative = self.dp.get_pair_dataframe(pair=inf_pair, - timeframe=inf_timeframe) + timeframe=inf_timeframe) ``` !!! Warning "Warning about backtesting" - Be careful when using dataprovider in backtesting. `historic_ohlcv()` (and `get_pair_dataframe()` - for the backtesting runmode) provides the full time-range in one go, - so please be aware of it and make sure to not "look into the future" to avoid surprises when running in dry/live mode. + In backtesting, `dp.get_pair_dataframe()` behavior differs depending on where it's called. + Within `populate_*()` methods, `dp.get_pair_dataframe()` returns the full timerange. Please make sure to not "look into the future" to avoid surprises when running in dry/live mode. + Within [callbacks](strategy-callbacks.md), you'll get the full timerange up to the current (simulated) candle. ### *get_analyzed_dataframe(pair, timeframe)* @@ -670,13 +670,13 @@ It can also be used in specific callbacks to get the signal that caused the acti ``` python # fetch current dataframe -if self.dp.runmode.value in ('live', 'dry_run'): - dataframe, last_updated = self.dp.get_analyzed_dataframe(pair=metadata['pair'], - timeframe=self.timeframe) +dataframe, last_updated = self.dp.get_analyzed_dataframe(pair=metadata['pair'], + timeframe=self.timeframe) ``` !!! Note "No data available" Returns an empty dataframe if the requested pair was not cached. + You can check for this with `if dataframe.empty:` and handle this case accordingly. This should not happen when using whitelisted pairs. ### *orderbook(pair, maximum)* diff --git a/docs/strategy_migration.md b/docs/strategy_migration.md index ac65abff4..f93efd067 100644 --- a/docs/strategy_migration.md +++ b/docs/strategy_migration.md @@ -43,19 +43,25 @@ Note : `forcesell`, `forcebuy`, `emergencysell` are changed to `force_exit`, `fo * `order_time_in_force` buy -> entry, sell -> exit. * `order_types` buy -> entry, sell -> exit. * `unfilledtimeout` buy -> entry, sell -> exit. + * `ignore_buying_expired_candle_after` -> moved to root level instead of "ask_strategy/exit_pricing" * Terminology changes * Sell reasons changed to reflect the new naming of "exit" instead of sells. Be careful in your strategy if you're using `exit_reason` checks and eventually update your strategy. * `sell_signal` -> `exit_signal` * `custom_sell` -> `custom_exit` * `force_sell` -> `force_exit` * `emergency_sell` -> `emergency_exit` + * Order pricing + * `bid_strategy` -> `entry_pricing` + * `ask_strategy` -> `exit_pricing` + * `ask_last_balance` -> `price_last_balance` + * `bid_last_balance` -> `price_last_balance` * Webhook terminology changed from "sell" to "exit", and from "buy" to entry - * `webhookbuy` -> `webhookentry` - * `webhookbuyfill` -> `webhookentryfill` - * `webhookbuycancel` -> `webhookentrycancel` - * `webhooksell` -> `webhookexit` - * `webhooksellfill` -> `webhookexitfill` - * `webhooksellcancel` -> `webhookexitcancel` + * `webhookbuy` -> `entry` + * `webhookbuyfill` -> `entry_fill` + * `webhookbuycancel` -> `entry_cancel` + * `webhooksell` -> `exit` + * `webhooksellfill` -> `exit_fill` + * `webhooksellcancel` -> `exit_cancel` * Telegram notification settings * `buy` -> `entry` * `buy_fill` -> `entry_fill` @@ -443,6 +449,7 @@ Please refer to the [pricing documentation](configuration.md#prices-used-for-ord "use_order_book": true, "order_book_top": 1, "bid_last_balance": 0.0 + "ignore_buying_expired_candle_after": 120 } } ``` @@ -466,6 +473,7 @@ after: "use_order_book": true, "order_book_top": 1, "price_last_balance": 0.0 - } + }, + "ignore_buying_expired_candle_after": 120 } ``` diff --git a/docs/telegram-usage.md b/docs/telegram-usage.md index 055512f26..db4a309d0 100644 --- a/docs/telegram-usage.md +++ b/docs/telegram-usage.md @@ -77,6 +77,7 @@ Example configuration showing the different settings: "enabled": true, "token": "your_telegram_token", "chat_id": "your_telegram_chat_id", + "allow_custom_messages": true, "notification_settings": { "status": "silent", "warning": "on", @@ -115,6 +116,7 @@ Example configuration showing the different settings: `show_candle` - show candle values as part of entry/exit messages. Only possible values are `"ohlc"` or `"off"`. `balance_dust_level` will define what the `/balance` command takes as "dust" - Currencies with a balance below this will be shown. +`allow_custom_messages` completely disable strategy messages. `reload` allows you to disable reload-buttons on selected messages. ## Create a custom keyboard (command shortcut buttons) diff --git a/docs/utils.md b/docs/utils.md index 174fa0527..ee8793159 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -169,6 +169,43 @@ Example: Search dedicated strategy path. freqtrade list-strategies --strategy-path ~/.freqtrade/strategies/ ``` +## List freqAI models + +Use the `list-freqaimodels` subcommand to see all freqAI models available. + +This subcommand is useful for finding problems in your environment with loading freqAI models: modules with models that contain errors and failed to load are printed in red (LOAD FAILED), while models with duplicate names are printed in yellow (DUPLICATE NAME). + +``` +usage: freqtrade list-freqaimodels [-h] [-v] [--logfile FILE] [-V] [-c PATH] + [-d PATH] [--userdir PATH] + [--freqaimodel-path PATH] [-1] [--no-color] + +optional arguments: + -h, --help show this help message and exit + --freqaimodel-path PATH + Specify additional lookup path for freqaimodels. + -1, --one-column Print output in one column. + --no-color Disable colorization of hyperopt results. May be + useful if you are redirecting output to a file. + +Common arguments: + -v, --verbose Verbose mode (-vv for more, -vvv to get all messages). + --logfile FILE Log to the file specified. Special values are: + 'syslog', 'journald'. See the documentation for more + details. + -V, --version show program's version number and exit + -c PATH, --config PATH + Specify configuration file (default: + `userdir/config.json` or `config.json` whichever + exists). Multiple --config options may be used. Can be + set to `-` to read config from stdin. + -d PATH, --datadir PATH, --data-dir PATH + Path to directory with historical backtesting data. + --userdir PATH, --user-data-dir PATH + Path to userdata directory. + +``` + ## List Exchanges Use the `list-exchanges` subcommand to see the exchanges available for the bot. diff --git a/docs/webhook-config.md b/docs/webhook-config.md index 3677ebe89..00c369919 100644 --- a/docs/webhook-config.md +++ b/docs/webhook-config.md @@ -10,37 +10,37 @@ Sample configuration (tested using IFTTT). "webhook": { "enabled": true, "url": "https://maker.ifttt.com/trigger//with/key//", - "webhookentry": { + "entry": { "value1": "Buying {pair}", "value2": "limit {limit:8f}", "value3": "{stake_amount:8f} {stake_currency}" }, - "webhookentrycancel": { + "entry_cancel": { "value1": "Cancelling Open Buy Order for {pair}", "value2": "limit {limit:8f}", "value3": "{stake_amount:8f} {stake_currency}" }, - "webhookentryfill": { + "entry_fill": { "value1": "Buy Order for {pair} filled", "value2": "at {open_rate:8f}", "value3": "" }, - "webhookexit": { + "exit": { "value1": "Exiting {pair}", "value2": "limit {limit:8f}", "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" }, - "webhookexitcancel": { + "exit_cancel": { "value1": "Cancelling Open Exit Order for {pair}", "value2": "limit {limit:8f}", "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" }, - "webhookexitfill": { + "exit_fill": { "value1": "Exit Order for {pair} filled", "value2": "at {close_rate:8f}.", "value3": "" }, - "webhookstatus": { + "status": { "value1": "Status: {status}", "value2": "", "value3": "" @@ -57,7 +57,7 @@ You can set the POST body format to Form-Encoded (default), JSON-Encoded, or raw "enabled": true, "url": "https://.cloud.mattermost.com/hooks/", "format": "json", - "webhookstatus": { + "status": { "text": "Status: {status}" } }, @@ -88,17 +88,30 @@ Optional parameters are available to enable automatic retries for webhook messag "url": "https://", "retries": 3, "retry_delay": 0.2, - "webhookstatus": { + "status": { "status": "Status: {status}" } }, ``` +Custom messages can be sent to Webhook endpoints via the `self.dp.send_msg()` function from within the strategy. To enable this, set the `allow_custom_messages` option to `true`: + +```json + "webhook": { + "enabled": true, + "url": "https://", + "allow_custom_messages": true, + "strategy_msg": { + "status": "StrategyMessage: {msg}" + } + }, +``` + Different payloads can be configured for different events. Not all fields are necessary, but you should configure at least one of the dicts, otherwise the webhook will never be called. -### Webhookentry +### Entry -The fields in `webhook.webhookentry` are filled when the bot executes a long/short. Parameters are filled using string.format. +The fields in `webhook.entry` are filled when the bot executes a long/short. Parameters are filled using string.format. Possible parameters are: * `trade_id` @@ -118,9 +131,9 @@ Possible parameters are: * `current_rate` * `enter_tag` -### Webhookentrycancel +### Entry cancel -The fields in `webhook.webhookentrycancel` are filled when the bot cancels a long/short order. Parameters are filled using string.format. +The fields in `webhook.entry_cancel` are filled when the bot cancels a long/short order. Parameters are filled using string.format. Possible parameters are: * `trade_id` @@ -139,9 +152,9 @@ Possible parameters are: * `current_rate` * `enter_tag` -### Webhookentryfill +### Entry fill -The fields in `webhook.webhookentryfill` are filled when the bot filled a long/short order. Parameters are filled using string.format. +The fields in `webhook.entry_fill` are filled when the bot filled a long/short order. Parameters are filled using string.format. Possible parameters are: * `trade_id` @@ -160,9 +173,9 @@ Possible parameters are: * `current_rate` * `enter_tag` -### Webhookexit +### Exit -The fields in `webhook.webhookexit` are filled when the bot exits a trade. Parameters are filled using string.format. +The fields in `webhook.exit` are filled when the bot exits a trade. Parameters are filled using string.format. Possible parameters are: * `trade_id` @@ -184,9 +197,9 @@ Possible parameters are: * `open_date` * `close_date` -### Webhookexitfill +### Exit fill -The fields in `webhook.webhookexitfill` are filled when the bot fills a exit order (closes a Trade). Parameters are filled using string.format. +The fields in `webhook.exit_fill` are filled when the bot fills a exit order (closes a Trade). Parameters are filled using string.format. Possible parameters are: * `trade_id` @@ -209,9 +222,9 @@ Possible parameters are: * `open_date` * `close_date` -### Webhookexitcancel +### Exit cancel -The fields in `webhook.webhookexitcancel` are filled when the bot cancels a exit order. Parameters are filled using string.format. +The fields in `webhook.exit_cancel` are filled when the bot cancels a exit order. Parameters are filled using string.format. Possible parameters are: * `trade_id` @@ -234,9 +247,9 @@ Possible parameters are: * `open_date` * `close_date` -### Webhookstatus +### Status -The fields in `webhook.webhookstatus` are used for regular status messages (Started / Stopped / ...). Parameters are filled using string.format. +The fields in `webhook.status` are used for regular status messages (Started / Stopped / ...). Parameters are filled using string.format. The only possible value here is `{status}`. @@ -280,7 +293,6 @@ You can configure this as follows: } ``` - The above represents the default (`exit_fill` and `entry_fill` are optional and will default to the above configuration) - modifications are obviously possible. Available fields correspond to the fields for webhooks and are documented in the corresponding webhook sections. @@ -288,3 +300,13 @@ Available fields correspond to the fields for webhooks and are documented in the The notifications will look as follows by default. ![discord-notification](assets/discord_notification.png) + +Custom messages can be sent from a strategy to Discord endpoints via the dataprovider.send_msg() function. To enable this, set the `allow_custom_messages` option to `true`: + +```json + "discord": { + "enabled": true, + "webhook_url": "https://discord.com/api/webhooks/", + "allow_custom_messages": true, + }, +``` diff --git a/freqtrade/__init__.py b/freqtrade/__init__.py index 1e62266a8..ad80410ee 100644 --- a/freqtrade/__init__.py +++ b/freqtrade/__init__.py @@ -1,5 +1,5 @@ """ Freqtrade bot """ -__version__ = '2022.10.dev' +__version__ = '2022.11.dev' if 'dev' in __version__: try: @@ -16,6 +16,6 @@ if 'dev' in __version__: from pathlib import Path versionfile = Path('./freqtrade_commit') if versionfile.is_file(): - __version__ = f"docker-{versionfile.read_text()[:8]}" + __version__ = f"docker-{__version__}-{versionfile.read_text()[:8]}" except Exception: pass diff --git a/freqtrade/commands/__init__.py b/freqtrade/commands/__init__.py index d93ed1e09..788657cc8 100644 --- a/freqtrade/commands/__init__.py +++ b/freqtrade/commands/__init__.py @@ -15,9 +15,9 @@ from freqtrade.commands.db_commands import start_convert_db from freqtrade.commands.deploy_commands import (start_create_userdir, start_install_ui, start_new_strategy) from freqtrade.commands.hyperopt_commands import start_hyperopt_list, start_hyperopt_show -from freqtrade.commands.list_commands import (start_list_exchanges, start_list_markets, - start_list_strategies, start_list_timeframes, - start_show_trades) +from freqtrade.commands.list_commands import (start_list_exchanges, start_list_freqAI_models, + start_list_markets, start_list_strategies, + start_list_timeframes, start_show_trades) from freqtrade.commands.optimize_commands import (start_backtesting, start_backtesting_show, start_edge, start_hyperopt) from freqtrade.commands.pairlist_commands import start_test_pairlist diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 97d8cc130..57689db0a 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -41,6 +41,8 @@ ARGS_EDGE = ARGS_COMMON_OPTIMIZE + ["stoploss_range"] ARGS_LIST_STRATEGIES = ["strategy_path", "print_one_column", "print_colorized", "recursive_strategy_search"] +ARGS_LIST_FREQAIMODELS = ["freqaimodel_path", "print_one_column", "print_colorized"] + ARGS_LIST_HYPEROPTS = ["hyperopt_path", "print_one_column", "print_colorized"] ARGS_BACKTEST_SHOW = ["exportfilename", "backtest_show_pair_list"] @@ -106,8 +108,8 @@ ARGS_ANALYZE_ENTRIES_EXITS = ["exportfilename", "analysis_groups", "enter_reason "exit_reason_list", "indicator_list"] NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes", - "list-markets", "list-pairs", "list-strategies", "list-data", - "hyperopt-list", "hyperopt-show", "backtest-filter", + "list-markets", "list-pairs", "list-strategies", "list-freqaimodels", + "list-data", "hyperopt-list", "hyperopt-show", "backtest-filter", "plot-dataframe", "plot-profit", "show-trades", "trades-to-ohlcv"] NO_CONF_ALLOWED = ["create-userdir", "list-exchanges", "new-strategy"] @@ -192,10 +194,11 @@ class Arguments: start_create_userdir, start_download_data, start_edge, start_hyperopt, start_hyperopt_list, start_hyperopt_show, start_install_ui, start_list_data, start_list_exchanges, - start_list_markets, start_list_strategies, - start_list_timeframes, start_new_config, start_new_strategy, - start_plot_dataframe, start_plot_profit, start_show_trades, - start_test_pairlist, start_trading, start_webserver) + start_list_freqAI_models, start_list_markets, + start_list_strategies, start_list_timeframes, + start_new_config, start_new_strategy, start_plot_dataframe, + start_plot_profit, start_show_trades, start_test_pairlist, + start_trading, start_webserver) subparsers = self.parser.add_subparsers(dest='command', # Use custom message when no subhandler is added @@ -362,6 +365,15 @@ class Arguments: list_strategies_cmd.set_defaults(func=start_list_strategies) self._build_args(optionlist=ARGS_LIST_STRATEGIES, parser=list_strategies_cmd) + # Add list-freqAI Models subcommand + list_freqaimodels_cmd = subparsers.add_parser( + 'list-freqaimodels', + help='Print available freqAI models.', + parents=[_common_parser], + ) + list_freqaimodels_cmd.set_defaults(func=start_list_freqAI_models) + self._build_args(optionlist=ARGS_LIST_FREQAIMODELS, parser=list_freqaimodels_cmd) + # Add list-timeframes subcommand list_timeframes_cmd = subparsers.add_parser( 'list-timeframes', diff --git a/freqtrade/commands/cli_options.py b/freqtrade/commands/cli_options.py index e50fb86d8..b95a70082 100644 --- a/freqtrade/commands/cli_options.py +++ b/freqtrade/commands/cli_options.py @@ -49,7 +49,7 @@ AVAILABLE_CLI_OPTIONS = { default=0, ), "logfile": Arg( - '--logfile', + '--logfile', '--log-file', help="Log to the file specified. Special values are: 'syslog', 'journald'. " "See the documentation for more details.", metavar='FILE', diff --git a/freqtrade/commands/list_commands.py b/freqtrade/commands/list_commands.py index eb761eeec..4e0623081 100644 --- a/freqtrade/commands/list_commands.py +++ b/freqtrade/commands/list_commands.py @@ -1,7 +1,6 @@ import csv import logging import sys -from pathlib import Path from typing import Any, Dict, List import rapidjson @@ -10,7 +9,6 @@ from colorama import init as colorama_init from tabulate import tabulate from freqtrade.configuration import setup_utils_configuration -from freqtrade.constants import USERPATH_STRATEGIES from freqtrade.enums import RunMode from freqtrade.exceptions import OperationalException from freqtrade.exchange import market_is_active, validate_exchanges @@ -41,7 +39,7 @@ def start_list_exchanges(args: Dict[str, Any]) -> None: print(tabulate(exchanges, headers=['Exchange name', 'Valid', 'reason'])) -def _print_objs_tabular(objs: List, print_colorized: bool, base_dir: Path) -> None: +def _print_objs_tabular(objs: List, print_colorized: bool) -> None: if print_colorized: colorama_init(autoreset=True) red = Fore.RED @@ -55,7 +53,7 @@ def _print_objs_tabular(objs: List, print_colorized: bool, base_dir: Path) -> No names = [s['name'] for s in objs] objs_to_print = [{ 'name': s['name'] if s['name'] else "--", - 'location': s['location'].relative_to(base_dir), + 'location': s['location_rel'], 'status': (red + "LOAD FAILED" + reset if s['class'] is None else "OK" if names.count(s['name']) == 1 else yellow + "DUPLICATE NAME" + reset) @@ -76,9 +74,8 @@ def start_list_strategies(args: Dict[str, Any]) -> None: """ config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) - directory = Path(config.get('strategy_path', config['user_data_dir'] / USERPATH_STRATEGIES)) strategy_objs = StrategyResolver.search_all_objects( - directory, not args['print_one_column'], config.get('recursive_strategy_search', False)) + config, not args['print_one_column'], config.get('recursive_strategy_search', False)) # Sort alphabetically strategy_objs = sorted(strategy_objs, key=lambda x: x['name']) for obj in strategy_objs: @@ -90,7 +87,22 @@ def start_list_strategies(args: Dict[str, Any]) -> None: if args['print_one_column']: print('\n'.join([s['name'] for s in strategy_objs])) else: - _print_objs_tabular(strategy_objs, config.get('print_colorized', False), directory) + _print_objs_tabular(strategy_objs, config.get('print_colorized', False)) + + +def start_list_freqAI_models(args: Dict[str, Any]) -> None: + """ + Print files with FreqAI models custom classes available in the directory + """ + config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) + from freqtrade.resolvers.freqaimodel_resolver import FreqaiModelResolver + model_objs = FreqaiModelResolver.search_all_objects(config, not args['print_one_column']) + # Sort alphabetically + model_objs = sorted(model_objs, key=lambda x: x['name']) + if args['print_one_column']: + print('\n'.join([s['name'] for s in model_objs])) + else: + _print_objs_tabular(model_objs, config.get('print_colorized', False)) def start_list_timeframes(args: Dict[str, Any]) -> None: diff --git a/freqtrade/configuration/config_validation.py b/freqtrade/configuration/config_validation.py index 7055d9551..98f69c030 100644 --- a/freqtrade/configuration/config_validation.py +++ b/freqtrade/configuration/config_validation.py @@ -86,6 +86,7 @@ def validate_config_consistency(conf: Dict[str, Any], preliminary: bool = False) _validate_unlimited_amount(conf) _validate_ask_orderbook(conf) _validate_freqai_hyperopt(conf) + _validate_freqai_include_timeframes(conf) _validate_consumers(conf) validate_migrated_strategy_settings(conf) @@ -334,6 +335,26 @@ def _validate_freqai_hyperopt(conf: Dict[str, Any]) -> None: 'Using analyze-per-epoch parameter is not supported with a FreqAI strategy.') +def _validate_freqai_include_timeframes(conf: Dict[str, Any]) -> None: + freqai_enabled = conf.get('freqai', {}).get('enabled', False) + if freqai_enabled: + main_tf = conf.get('timeframe', '5m') + freqai_include_timeframes = conf.get('freqai', {}).get('feature_parameters', {} + ).get('include_timeframes', []) + + from freqtrade.exchange import timeframe_to_seconds + main_tf_s = timeframe_to_seconds(main_tf) + offending_lines = [] + for tf in freqai_include_timeframes: + tf_s = timeframe_to_seconds(tf) + if tf_s < main_tf_s: + offending_lines.append(tf) + if offending_lines: + raise OperationalException( + f"Main timeframe of {main_tf} must be smaller or equal to FreqAI " + f"`include_timeframes`.Offending include-timeframes: {', '.join(offending_lines)}") + + def _validate_consumers(conf: Dict[str, Any]) -> None: emc_conf = conf.get('external_message_consumer', {}) if emc_conf.get('enabled', False): diff --git a/freqtrade/configuration/directory_operations.py b/freqtrade/configuration/directory_operations.py index f70310ee1..e1313749b 100644 --- a/freqtrade/configuration/directory_operations.py +++ b/freqtrade/configuration/directory_operations.py @@ -3,7 +3,8 @@ import shutil from pathlib import Path from typing import Optional -from freqtrade.constants import USER_DATA_FILES, Config +from freqtrade.constants import (USER_DATA_FILES, USERPATH_FREQAIMODELS, USERPATH_HYPEROPTS, + USERPATH_NOTEBOOKS, USERPATH_STRATEGIES, Config) from freqtrade.exceptions import OperationalException @@ -49,8 +50,8 @@ def create_userdata_dir(directory: str, create_dir: bool = False) -> Path: :param create_dir: Create directory if it does not exist. :return: Path object containing the directory """ - sub_dirs = ["backtest_results", "data", "hyperopts", "hyperopt_results", "logs", - "notebooks", "plot", "strategies", ] + sub_dirs = ["backtest_results", "data", USERPATH_HYPEROPTS, "hyperopt_results", "logs", + USERPATH_NOTEBOOKS, "plot", USERPATH_STRATEGIES, USERPATH_FREQAIMODELS] folder = Path(directory) chown_user_directory(folder) if not folder.is_dir(): diff --git a/freqtrade/constants.py b/freqtrade/constants.py index 4fa3b7481..7078e3d14 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -5,7 +5,7 @@ bot constants """ from typing import Any, Dict, List, Literal, Tuple -from freqtrade.enums import CandleType +from freqtrade.enums import CandleType, RPCMessageType DEFAULT_CONFIG = 'config.json' @@ -282,6 +282,7 @@ CONF_SCHEMA = { 'enabled': {'type': 'boolean'}, 'token': {'type': 'string'}, 'chat_id': {'type': 'string'}, + 'allow_custom_messages': {'type': 'boolean', 'default': True}, 'balance_dust_level': {'type': 'number', 'minimum': 0.0}, 'notification_settings': { 'type': 'object', @@ -344,6 +345,8 @@ CONF_SCHEMA = { 'format': {'type': 'string', 'enum': WEBHOOK_FORMAT_OPTIONS, 'default': 'form'}, 'retries': {'type': 'integer', 'minimum': 0}, 'retry_delay': {'type': 'number', 'minimum': 0}, + **dict([(x, {'type': 'object'}) for x in RPCMessageType]), + # Below -> Deprecated 'webhookentry': {'type': 'object'}, 'webhookentrycancel': {'type': 'object'}, 'webhookentryfill': {'type': 'object'}, @@ -537,6 +540,8 @@ CONF_SCHEMA = { "properties": { "enabled": {"type": "boolean", "default": False}, "keras": {"type": "boolean", "default": False}, + "write_metrics_to_disk": {"type": "boolean", "default": False}, + "purge_old_models": {"type": "boolean", "default": True}, "conv_width": {"type": "integer", "default": 2}, "train_period_days": {"type": "integer", "default": 0}, "backtest_period_days": {"type": "number", "default": 7}, @@ -650,5 +655,6 @@ LongShort = Literal['long', 'short'] EntryExit = Literal['entry', 'exit'] BuySell = Literal['buy', 'sell'] MakerTaker = Literal['maker', 'taker'] +BidAsk = Literal['bid', 'ask'] Config = Dict[str, Any] diff --git a/freqtrade/data/history/idatahandler.py b/freqtrade/data/history/idatahandler.py index 80e29f4c0..cbc3f1a34 100644 --- a/freqtrade/data/history/idatahandler.py +++ b/freqtrade/data/history/idatahandler.py @@ -303,7 +303,7 @@ class IDataHandler(ABC): timerange=timerange_startup, candle_type=candle_type ) - if self._check_empty_df(pairdf, pair, timeframe, candle_type, warn_no_data): + if self._check_empty_df(pairdf, pair, timeframe, candle_type, warn_no_data, True): return pairdf else: enddate = pairdf.iloc[-1]['date'] @@ -323,8 +323,9 @@ class IDataHandler(ABC): self._check_empty_df(pairdf, pair, timeframe, candle_type, warn_no_data) return pairdf - def _check_empty_df(self, pairdf: DataFrame, pair: str, timeframe: str, - candle_type: CandleType, warn_no_data: bool): + def _check_empty_df( + self, pairdf: DataFrame, pair: str, timeframe: str, candle_type: CandleType, + warn_no_data: bool, warn_price: bool = False) -> bool: """ Warn on empty dataframe """ @@ -335,6 +336,20 @@ class IDataHandler(ABC): "Use `freqtrade download-data` to download the data" ) return True + elif warn_price: + candle_price_gap = 0 + if (candle_type in (CandleType.SPOT, CandleType.FUTURES) and + not pairdf.empty + and 'close' in pairdf.columns and 'open' in pairdf.columns): + # Detect gaps between prior close and open + gaps = ((pairdf['open'] - pairdf['close'].shift(1)) / pairdf['close'].shift(1)) + gaps = gaps.dropna() + if len(gaps): + candle_price_gap = max(abs(gaps)) + if candle_price_gap > 0.1: + logger.info(f"Price jump in {pair}, {timeframe}, {candle_type} between two candles " + f"of {candle_price_gap:.2%} detected.") + return False def _validate_pairdata(self, pair, pairdata: DataFrame, timeframe: str, diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 1b5ca11ee..8294838b1 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -9,14 +9,15 @@ from freqtrade.exchange.bitpanda import Bitpanda from freqtrade.exchange.bittrex import Bittrex from freqtrade.exchange.bybit import Bybit from freqtrade.exchange.coinbasepro import Coinbasepro -from freqtrade.exchange.exchange import (amount_to_contract_precision, amount_to_contracts, - amount_to_precision, available_exchanges, ccxt_exchanges, - contracts_to_amount, date_minus_candles, - is_exchange_known_ccxt, market_is_active, - price_to_precision, timeframe_to_minutes, - timeframe_to_msecs, timeframe_to_next_date, - timeframe_to_prev_date, timeframe_to_seconds, - validate_exchange, validate_exchanges) +from freqtrade.exchange.exchange_utils import (amount_to_contract_precision, amount_to_contracts, + amount_to_precision, available_exchanges, + ccxt_exchanges, contracts_to_amount, + date_minus_candles, is_exchange_known_ccxt, + market_is_active, price_to_precision, + timeframe_to_minutes, timeframe_to_msecs, + timeframe_to_next_date, timeframe_to_prev_date, + timeframe_to_seconds, validate_exchange, + validate_exchanges) from freqtrade.exchange.ftx import Ftx from freqtrade.exchange.gateio import Gateio from freqtrade.exchange.hitbtc import Hitbtc diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index a0d4b2d82..b21e64eb2 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -11,6 +11,7 @@ from freqtrade.enums import CandleType, MarginMode, TradingMode from freqtrade.exceptions import DDosProtection, OperationalException, TemporaryError from freqtrade.exchange import Exchange from freqtrade.exchange.common import retrier +from freqtrade.exchange.types import Tickers from freqtrade.misc import deep_merge_dicts, json_load @@ -41,25 +42,7 @@ class Binance(Exchange): (TradingMode.FUTURES, MarginMode.ISOLATED) ] - def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool: - """ - Verify stop_loss against stoploss-order value (limit or price) - Returns True if adjustment is necessary. - :param side: "buy" or "sell" - """ - order_types = ('stop_loss_limit', 'stop', 'stop_market') - - return ( - order.get('stopPrice', None) is None - or ( - order['type'] in order_types - and ( - (side == "sell" and stop_loss > float(order['stopPrice'])) or - (side == "buy" and stop_loss < float(order['stopPrice'])) - ) - )) - - def get_tickers(self, symbols: Optional[List[str]] = None, cached: bool = False) -> Dict: + def get_tickers(self, symbols: Optional[List[str]] = None, cached: bool = False) -> Tickers: tickers = super().get_tickers(symbols=symbols, cached=cached) if self.trading_mode == TradingMode.FUTURES: # Binance's future result has no bid/ask values. diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index cb9cbebbd..7a2b8ce7d 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -8,7 +8,6 @@ import inspect import logging from copy import deepcopy from datetime import datetime, timedelta, timezone -from math import ceil from threading import Lock from typing import Any, Coroutine, Dict, List, Literal, Optional, Tuple, Union @@ -16,28 +15,31 @@ import arrow import ccxt import ccxt.async_support as ccxt_async from cachetools import TTLCache -from ccxt import ROUND_DOWN, ROUND_UP, TICK_SIZE, TRUNCATE, decimal_to_precision +from ccxt import TICK_SIZE from dateutil import parser -from pandas import DataFrame +from pandas import DataFrame, concat -from freqtrade.constants import (DEFAULT_AMOUNT_RESERVE_PERCENT, NON_OPEN_EXCHANGE_STATES, BuySell, - Config, EntryExit, ListPairsWithTimeframes, MakerTaker, +from freqtrade.constants import (DEFAULT_AMOUNT_RESERVE_PERCENT, NON_OPEN_EXCHANGE_STATES, BidAsk, + BuySell, Config, EntryExit, ListPairsWithTimeframes, MakerTaker, PairWithTimeframe) -from freqtrade.data.converter import ohlcv_to_dataframe, trades_dict_to_list +from freqtrade.data.converter import clean_ohlcv_dataframe, ohlcv_to_dataframe, trades_dict_to_list from freqtrade.enums import OPTIMIZE_MODES, CandleType, MarginMode, TradingMode from freqtrade.exceptions import (DDosProtection, ExchangeError, InsufficientFundsError, InvalidOrderException, OperationalException, PricingError, RetryableOrderError, TemporaryError) -from freqtrade.exchange.common import (API_FETCH_ORDER_RETRY_COUNT, BAD_EXCHANGES, - EXCHANGE_HAS_OPTIONAL, EXCHANGE_HAS_REQUIRED, - remove_credentials, retrier, retrier_async) +from freqtrade.exchange.common import (API_FETCH_ORDER_RETRY_COUNT, remove_credentials, retrier, + retrier_async) +from freqtrade.exchange.exchange_utils import (CcxtModuleType, amount_to_contract_precision, + amount_to_contracts, amount_to_precision, + contracts_to_amount, date_minus_candles, + is_exchange_known_ccxt, market_is_active, + price_to_precision, timeframe_to_minutes, + timeframe_to_msecs, timeframe_to_next_date, + timeframe_to_prev_date, timeframe_to_seconds) +from freqtrade.exchange.types import Ticker, Tickers from freqtrade.misc import (chunks, deep_merge_dicts, file_dump_json, file_load_json, safe_value_fallback2) from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist -from freqtrade.util import FtPrecise - - -CcxtModuleType = Any logger = logging.getLogger(__name__) @@ -179,13 +181,14 @@ class Exchange: exchange_config, ccxt_async, ccxt_kwargs=ccxt_async_config) logger.info(f'Using Exchange "{self.name}"') - + self.required_candle_call_count = 1 if validate: # Initial markets load self._load_markets() self.validate_config(config) + self._startup_candle_count: int = config.get('startup_candle_count', 0) self.required_candle_call_count = self.validate_required_startup_candles( - config.get('startup_candle_count', 0), config.get('timeframe', '')) + self._startup_candle_count, config.get('timeframe', '')) # Converts the interval provided in minutes in config to seconds self.markets_refresh_interval: int = exchange_config.get( @@ -408,11 +411,13 @@ class Exchange: else: return DataFrame() - def get_contract_size(self, pair: str) -> float: + def get_contract_size(self, pair: str) -> Optional[float]: if self.trading_mode == TradingMode.FUTURES: - market = self.markets[pair] + market = self.markets.get(pair, {}) contract_size: float = 1.0 - if market['contractSize'] is not None: + if not market: + return None + if market.get('contractSize') is not None: # ccxt has contractSize in markets as string contract_size = float(market['contractSize']) return contract_size @@ -1072,7 +1077,14 @@ class Exchange: Verify stop_loss against stoploss-order value (limit or price) Returns True if adjustment is necessary. """ - raise OperationalException(f"stoploss is not implemented for {self.name}.") + if not self._ft_has.get('stoploss_on_exchange'): + raise OperationalException(f"stoploss is not implemented for {self.name}.") + + return ( + order.get('stopPrice', None) is None + or ((side == "sell" and stop_loss > float(order['stopPrice'])) or + (side == "buy" and stop_loss < float(order['stopPrice']))) + ) def _get_stop_order_type(self, user_order_type) -> Tuple[str, str]: @@ -1102,7 +1114,7 @@ class Exchange: 'In stoploss limit order, stop price should be more than limit price') return limit_rate - def _get_stop_params(self, ordertype: str, stop_price: float) -> Dict: + def _get_stop_params(self, side: BuySell, ordertype: str, stop_price: float) -> Dict: params = self._params.copy() # Verify if stopPrice works for your exchange! params.update({'stopPrice': stop_price}) @@ -1151,7 +1163,8 @@ class Exchange: return dry_order try: - params = self._get_stop_params(ordertype=ordertype, stop_price=stop_price_norm) + params = self._get_stop_params(side=side, ordertype=ordertype, + stop_price=stop_price_norm) if self.trading_mode == TradingMode.FUTURES: params['reduceOnly'] = True @@ -1419,14 +1432,17 @@ class Exchange: raise OperationalException(e) from e @retrier - def get_tickers(self, symbols: Optional[List[str]] = None, cached: bool = False) -> Dict: + def get_tickers(self, symbols: Optional[List[str]] = None, cached: bool = False) -> Tickers: """ :param cached: Allow cached result :return: fetch_tickers result """ + tickers: Tickers + if not self.exchange_has('fetchTickers'): + return {} if cached: with self._cache_lock: - tickers = self._fetch_tickers_cache.get('fetch_tickers') + tickers = self._fetch_tickers_cache.get('fetch_tickers') # type: ignore if tickers: return tickers try: @@ -1449,12 +1465,12 @@ class Exchange: # Pricing info @retrier - def fetch_ticker(self, pair: str) -> dict: + def fetch_ticker(self, pair: str) -> Ticker: try: if (pair not in self.markets or self.markets[pair].get('active', False) is False): raise ExchangeError(f"Pair {pair} not available") - data = self._api.fetch_ticker(pair) + data: Ticker = self._api.fetch_ticker(pair) return data except ccxt.DDoSProtection as e: raise DDosProtection(e) from e @@ -1505,7 +1521,7 @@ class Exchange: except ccxt.BaseError as e: raise OperationalException(e) from e - def _get_price_side(self, side: str, is_short: bool, conf_strategy: Dict) -> str: + def _get_price_side(self, side: str, is_short: bool, conf_strategy: Dict) -> BidAsk: price_side = conf_strategy['price_side'] if price_side in ('same', 'other'): @@ -1524,7 +1540,7 @@ class Exchange: def get_rate(self, pair: str, refresh: bool, side: EntryExit, is_short: bool, - order_book: Optional[dict] = None, ticker: Optional[dict] = None) -> float: + order_book: Optional[dict] = None, ticker: Optional[Ticker] = None) -> float: """ Calculates bid/ask target bid rate - between current ask price and last price @@ -1850,10 +1866,22 @@ class Exchange: return pair, timeframe, candle_type, data def _build_coroutine(self, pair: str, timeframe: str, candle_type: CandleType, - since_ms: Optional[int]) -> Coroutine: + since_ms: Optional[int], cache: bool) -> Coroutine: + not_all_data = cache and self.required_candle_call_count > 1 + if cache and (pair, timeframe, candle_type) in self._klines: + candle_limit = self.ohlcv_candle_limit(timeframe, candle_type) + min_date = date_minus_candles(timeframe, candle_limit - 5).timestamp() + # Check if 1 call can get us updated candles without hole in the data. + if min_date < self._pairs_last_refresh_time.get((pair, timeframe, candle_type), 0): + # Cache can be used - do one-off call. + not_all_data = False + else: + # Time jump detected, evict cache + logger.info( + f"Time jump detected. Evicting cache for {pair}, {timeframe}, {candle_type}") + del self._klines[(pair, timeframe, candle_type)] - if (not since_ms - and (self._ft_has["ohlcv_require_since"] or self.required_candle_call_count > 1)): + if (not since_ms and (self._ft_has["ohlcv_require_since"] or not_all_data)): # Multiple calls for one pair - to get more history one_call = timeframe_to_msecs(timeframe) * self.ohlcv_candle_limit( timeframe, candle_type, since_ms) @@ -1878,10 +1906,8 @@ class Exchange: input_coroutines = [] cached_pairs = [] for pair, timeframe, candle_type in set(pair_list): - if ( - timeframe not in self.timeframes - and candle_type in (CandleType.SPOT, CandleType.FUTURES) - ): + if (timeframe not in self.timeframes + and candle_type in (CandleType.SPOT, CandleType.FUTURES)): logger.warning( f"Cannot download ({pair}, {timeframe}) combination as this timeframe is " f"not available on {self.name}. Available timeframes are " @@ -1890,8 +1916,9 @@ class Exchange: if ((pair, timeframe, candle_type) not in self._klines or not cache or self._now_is_time_to_refresh(pair, timeframe, candle_type)): - input_coroutines.append(self._build_coroutine( - pair, timeframe, candle_type=candle_type, since_ms=since_ms)) + + input_coroutines.append( + self._build_coroutine(pair, timeframe, candle_type, since_ms, cache)) else: logger.debug( @@ -1901,6 +1928,29 @@ class Exchange: return input_coroutines, cached_pairs + def _process_ohlcv_df(self, pair: str, timeframe: str, c_type: CandleType, ticks: List[List], + cache: bool, drop_incomplete: bool) -> DataFrame: + # keeping last candle time as last refreshed time of the pair + if ticks and cache: + self._pairs_last_refresh_time[(pair, timeframe, c_type)] = ticks[-1][0] // 1000 + # keeping parsed dataframe in cache + ohlcv_df = ohlcv_to_dataframe(ticks, timeframe, pair=pair, fill_missing=True, + drop_incomplete=drop_incomplete) + if cache: + if (pair, timeframe, c_type) in self._klines: + old = self._klines[(pair, timeframe, c_type)] + # Reassign so we return the updated, combined df + ohlcv_df = clean_ohlcv_dataframe(concat([old, ohlcv_df], axis=0), timeframe, pair, + fill_missing=True, drop_incomplete=False) + candle_limit = self.ohlcv_candle_limit(timeframe, self._config['candle_type_def']) + # Age out old candles + ohlcv_df = ohlcv_df.tail(candle_limit + self._startup_candle_count) + ohlcv_df = ohlcv_df.reset_index(drop=True) + self._klines[(pair, timeframe, c_type)] = ohlcv_df + else: + self._klines[(pair, timeframe, c_type)] = ohlcv_df + return ohlcv_df + def refresh_latest_ohlcv(self, pair_list: ListPairsWithTimeframes, *, since_ms: Optional[int] = None, cache: bool = True, drop_incomplete: Optional[bool] = None @@ -1937,16 +1987,11 @@ class Exchange: continue # Deconstruct tuple (has 4 elements) pair, timeframe, c_type, ticks = res - # keeping last candle time as last refreshed time of the pair - if ticks: - self._pairs_last_refresh_time[(pair, timeframe, c_type)] = ticks[-1][0] // 1000 - # keeping parsed dataframe in cache - ohlcv_df = ohlcv_to_dataframe( - ticks, timeframe, pair=pair, fill_missing=True, - drop_incomplete=drop_incomplete) + ohlcv_df = self._process_ohlcv_df( + pair, timeframe, c_type, ticks, cache, drop_incomplete) + results_df[(pair, timeframe, c_type)] = ohlcv_df - if cache: - self._klines[(pair, timeframe, c_type)] = ohlcv_df + # Return cached klines for pair, timeframe, c_type in cached_pairs: results_df[(pair, timeframe, c_type)] = self.klines( @@ -1959,11 +2004,8 @@ class Exchange: def _now_is_time_to_refresh(self, pair: str, timeframe: str, candle_type: CandleType) -> bool: # Timeframe in seconds interval_in_sec = timeframe_to_seconds(timeframe) - - return not ( - (self._pairs_last_refresh_time.get((pair, timeframe, candle_type), 0) - + interval_in_sec) >= arrow.utcnow().int_timestamp - ) + plr = self._pairs_last_refresh_time.get((pair, timeframe, candle_type), 0) + interval_in_sec + return plr < arrow.utcnow().int_timestamp @retrier_async async def _async_get_candle_history( @@ -1989,8 +2031,8 @@ class Exchange: candle_limit = self.ohlcv_candle_limit( timeframe, candle_type=candle_type, since_ms=since_ms) - if candle_type != CandleType.SPOT: - params.update({'price': candle_type}) + if candle_type and candle_type != CandleType.SPOT: + params.update({'price': candle_type.value}) if candle_type != CandleType.FUNDING_RATE: data = await self._api_async.fetch_ohlcv( pair, timeframe=timeframe, since=since_ms, @@ -2766,240 +2808,3 @@ class Exchange: # describes the min amt for a tier, and the lowest tier will always go down to 0 else: raise OperationalException(f"Cannot get maintenance ratio using {self.name}") - - -def is_exchange_known_ccxt(exchange_name: str, ccxt_module: CcxtModuleType = None) -> bool: - return exchange_name in ccxt_exchanges(ccxt_module) - - -def ccxt_exchanges(ccxt_module: CcxtModuleType = None) -> List[str]: - """ - Return the list of all exchanges known to ccxt - """ - return ccxt_module.exchanges if ccxt_module is not None else ccxt.exchanges - - -def available_exchanges(ccxt_module: CcxtModuleType = None) -> List[str]: - """ - Return exchanges available to the bot, i.e. non-bad exchanges in the ccxt list - """ - exchanges = ccxt_exchanges(ccxt_module) - return [x for x in exchanges if validate_exchange(x)[0]] - - -def validate_exchange(exchange: str) -> Tuple[bool, str]: - ex_mod = getattr(ccxt, exchange.lower())() - if not ex_mod or not ex_mod.has: - return False, '' - missing = [k for k in EXCHANGE_HAS_REQUIRED if ex_mod.has.get(k) is not True] - if missing: - return False, f"missing: {', '.join(missing)}" - - missing_opt = [k for k in EXCHANGE_HAS_OPTIONAL if not ex_mod.has.get(k)] - - if exchange.lower() in BAD_EXCHANGES: - return False, BAD_EXCHANGES.get(exchange.lower(), '') - if missing_opt: - return True, f"missing opt: {', '.join(missing_opt)}" - - return True, '' - - -def validate_exchanges(all_exchanges: bool) -> List[Tuple[str, bool, str]]: - """ - :return: List of tuples with exchangename, valid, reason. - """ - exchanges = ccxt_exchanges() if all_exchanges else available_exchanges() - exchanges_valid = [ - (e, *validate_exchange(e)) for e in exchanges - ] - return exchanges_valid - - -def timeframe_to_seconds(timeframe: str) -> int: - """ - Translates the timeframe interval value written in the human readable - form ('1m', '5m', '1h', '1d', '1w', etc.) to the number - of seconds for one timeframe interval. - """ - return ccxt.Exchange.parse_timeframe(timeframe) - - -def timeframe_to_minutes(timeframe: str) -> int: - """ - Same as timeframe_to_seconds, but returns minutes. - """ - return ccxt.Exchange.parse_timeframe(timeframe) // 60 - - -def timeframe_to_msecs(timeframe: str) -> int: - """ - Same as timeframe_to_seconds, but returns milliseconds. - """ - return ccxt.Exchange.parse_timeframe(timeframe) * 1000 - - -def timeframe_to_prev_date(timeframe: str, date: datetime = None) -> datetime: - """ - Use Timeframe and determine the candle start date for this date. - Does not round when given a candle start date. - :param timeframe: timeframe in string format (e.g. "5m") - :param date: date to use. Defaults to now(utc) - :returns: date of previous candle (with utc timezone) - """ - if not date: - date = datetime.now(timezone.utc) - - new_timestamp = ccxt.Exchange.round_timeframe(timeframe, date.timestamp() * 1000, - ROUND_DOWN) // 1000 - return datetime.fromtimestamp(new_timestamp, tz=timezone.utc) - - -def timeframe_to_next_date(timeframe: str, date: datetime = None) -> datetime: - """ - Use Timeframe and determine next candle. - :param timeframe: timeframe in string format (e.g. "5m") - :param date: date to use. Defaults to now(utc) - :returns: date of next candle (with utc timezone) - """ - if not date: - date = datetime.now(timezone.utc) - new_timestamp = ccxt.Exchange.round_timeframe(timeframe, date.timestamp() * 1000, - ROUND_UP) // 1000 - return datetime.fromtimestamp(new_timestamp, tz=timezone.utc) - - -def date_minus_candles( - timeframe: str, candle_count: int, date: Optional[datetime] = None) -> datetime: - """ - subtract X candles from a date. - :param timeframe: timeframe in string format (e.g. "5m") - :param candle_count: Amount of candles to subtract. - :param date: date to use. Defaults to now(utc) - - """ - if not date: - date = datetime.now(timezone.utc) - - tf_min = timeframe_to_minutes(timeframe) - new_date = timeframe_to_prev_date(timeframe, date) - timedelta(minutes=tf_min * candle_count) - return new_date - - -def market_is_active(market: Dict) -> bool: - """ - Return True if the market is active. - """ - # "It's active, if the active flag isn't explicitly set to false. If it's missing or - # true then it's true. If it's undefined, then it's most likely true, but not 100% )" - # See https://github.com/ccxt/ccxt/issues/4874, - # https://github.com/ccxt/ccxt/issues/4075#issuecomment-434760520 - return market.get('active', True) is not False - - -def amount_to_contracts(amount: float, contract_size: Optional[float]) -> float: - """ - Convert amount to contracts. - :param amount: amount to convert - :param contract_size: contract size - taken from exchange.get_contract_size(pair) - :return: num-contracts - """ - if contract_size and contract_size != 1: - return float(FtPrecise(amount) / FtPrecise(contract_size)) - else: - return amount - - -def contracts_to_amount(num_contracts: float, contract_size: Optional[float]) -> float: - """ - Takes num-contracts and converts it to contract size - :param num_contracts: number of contracts - :param contract_size: contract size - taken from exchange.get_contract_size(pair) - :return: Amount - """ - - if contract_size and contract_size != 1: - return float(FtPrecise(num_contracts) * FtPrecise(contract_size)) - else: - return num_contracts - - -def amount_to_precision(amount: float, amount_precision: Optional[float], - precisionMode: Optional[int]) -> float: - """ - Returns the amount to buy or sell to a precision the Exchange accepts - Re-implementation of ccxt internal methods - ensuring we can test the result is correct - based on our definitions. - :param amount: amount to truncate - :param amount_precision: amount precision to use. - should be retrieved from markets[pair]['precision']['amount'] - :param precisionMode: precision mode to use. Should be used from precisionMode - one of ccxt's DECIMAL_PLACES, SIGNIFICANT_DIGITS, or TICK_SIZE - :return: truncated amount - """ - if amount_precision is not None and precisionMode is not None: - precision = int(amount_precision) if precisionMode != TICK_SIZE else amount_precision - # precision must be an int for non-ticksize inputs. - amount = float(decimal_to_precision(amount, rounding_mode=TRUNCATE, - precision=precision, - counting_mode=precisionMode, - )) - - return amount - - -def amount_to_contract_precision( - amount, amount_precision: Optional[float], precisionMode: Optional[int], - contract_size: Optional[float]) -> float: - """ - Returns the amount to buy or sell to a precision the Exchange accepts - including calculation to and from contracts. - Re-implementation of ccxt internal methods - ensuring we can test the result is correct - based on our definitions. - :param amount: amount to truncate - :param amount_precision: amount precision to use. - should be retrieved from markets[pair]['precision']['amount'] - :param precisionMode: precision mode to use. Should be used from precisionMode - one of ccxt's DECIMAL_PLACES, SIGNIFICANT_DIGITS, or TICK_SIZE - :param contract_size: contract size - taken from exchange.get_contract_size(pair) - :return: truncated amount - """ - if amount_precision is not None and precisionMode is not None: - contracts = amount_to_contracts(amount, contract_size) - amount_p = amount_to_precision(contracts, amount_precision, precisionMode) - return contracts_to_amount(amount_p, contract_size) - return amount - - -def price_to_precision(price: float, price_precision: Optional[float], - precisionMode: Optional[int]) -> float: - """ - Returns the price rounded up to the precision the Exchange accepts. - Partial Re-implementation of ccxt internal method decimal_to_precision(), - which does not support rounding up - TODO: If ccxt supports ROUND_UP for decimal_to_precision(), we could remove this and - align with amount_to_precision(). - !!! Rounds up - :param price: price to convert - :param price_precision: price precision to use. Used from markets[pair]['precision']['price'] - :param precisionMode: precision mode to use. Should be used from precisionMode - one of ccxt's DECIMAL_PLACES, SIGNIFICANT_DIGITS, or TICK_SIZE - :return: price rounded up to the precision the Exchange accepts - - """ - if price_precision is not None and precisionMode is not None: - # price = float(decimal_to_precision(price, rounding_mode=ROUND, - # precision=price_precision, - # counting_mode=self.precisionMode, - # )) - if precisionMode == TICK_SIZE: - precision = FtPrecise(price_precision) - price_str = FtPrecise(price) - missing = price_str % precision - if not missing == FtPrecise("0"): - price = round(float(str(price_str - missing + precision)), 14) - else: - symbol_prec = price_precision - big_price = price * pow(10, symbol_prec) - price = ceil(big_price) / pow(10, symbol_prec) - return price diff --git a/freqtrade/exchange/exchange_utils.py b/freqtrade/exchange/exchange_utils.py new file mode 100644 index 000000000..cb6333869 --- /dev/null +++ b/freqtrade/exchange/exchange_utils.py @@ -0,0 +1,252 @@ +""" +Exchange support utils +""" +from datetime import datetime, timedelta, timezone +from math import ceil +from typing import Any, Dict, List, Optional, Tuple + +import ccxt +from ccxt import ROUND_DOWN, ROUND_UP, TICK_SIZE, TRUNCATE, decimal_to_precision + +from freqtrade.exchange.common import BAD_EXCHANGES, EXCHANGE_HAS_OPTIONAL, EXCHANGE_HAS_REQUIRED +from freqtrade.util import FtPrecise + + +CcxtModuleType = Any + + +def is_exchange_known_ccxt(exchange_name: str, ccxt_module: CcxtModuleType = None) -> bool: + return exchange_name in ccxt_exchanges(ccxt_module) + + +def ccxt_exchanges(ccxt_module: CcxtModuleType = None) -> List[str]: + """ + Return the list of all exchanges known to ccxt + """ + return ccxt_module.exchanges if ccxt_module is not None else ccxt.exchanges + + +def available_exchanges(ccxt_module: CcxtModuleType = None) -> List[str]: + """ + Return exchanges available to the bot, i.e. non-bad exchanges in the ccxt list + """ + exchanges = ccxt_exchanges(ccxt_module) + return [x for x in exchanges if validate_exchange(x)[0]] + + +def validate_exchange(exchange: str) -> Tuple[bool, str]: + ex_mod = getattr(ccxt, exchange.lower())() + if not ex_mod or not ex_mod.has: + return False, '' + missing = [k for k in EXCHANGE_HAS_REQUIRED if ex_mod.has.get(k) is not True] + if missing: + return False, f"missing: {', '.join(missing)}" + + missing_opt = [k for k in EXCHANGE_HAS_OPTIONAL if not ex_mod.has.get(k)] + + if exchange.lower() in BAD_EXCHANGES: + return False, BAD_EXCHANGES.get(exchange.lower(), '') + if missing_opt: + return True, f"missing opt: {', '.join(missing_opt)}" + + return True, '' + + +def validate_exchanges(all_exchanges: bool) -> List[Tuple[str, bool, str]]: + """ + :return: List of tuples with exchangename, valid, reason. + """ + exchanges = ccxt_exchanges() if all_exchanges else available_exchanges() + exchanges_valid = [ + (e, *validate_exchange(e)) for e in exchanges + ] + return exchanges_valid + + +def timeframe_to_seconds(timeframe: str) -> int: + """ + Translates the timeframe interval value written in the human readable + form ('1m', '5m', '1h', '1d', '1w', etc.) to the number + of seconds for one timeframe interval. + """ + return ccxt.Exchange.parse_timeframe(timeframe) + + +def timeframe_to_minutes(timeframe: str) -> int: + """ + Same as timeframe_to_seconds, but returns minutes. + """ + return ccxt.Exchange.parse_timeframe(timeframe) // 60 + + +def timeframe_to_msecs(timeframe: str) -> int: + """ + Same as timeframe_to_seconds, but returns milliseconds. + """ + return ccxt.Exchange.parse_timeframe(timeframe) * 1000 + + +def timeframe_to_prev_date(timeframe: str, date: datetime = None) -> datetime: + """ + Use Timeframe and determine the candle start date for this date. + Does not round when given a candle start date. + :param timeframe: timeframe in string format (e.g. "5m") + :param date: date to use. Defaults to now(utc) + :returns: date of previous candle (with utc timezone) + """ + if not date: + date = datetime.now(timezone.utc) + + new_timestamp = ccxt.Exchange.round_timeframe(timeframe, date.timestamp() * 1000, + ROUND_DOWN) // 1000 + return datetime.fromtimestamp(new_timestamp, tz=timezone.utc) + + +def timeframe_to_next_date(timeframe: str, date: datetime = None) -> datetime: + """ + Use Timeframe and determine next candle. + :param timeframe: timeframe in string format (e.g. "5m") + :param date: date to use. Defaults to now(utc) + :returns: date of next candle (with utc timezone) + """ + if not date: + date = datetime.now(timezone.utc) + new_timestamp = ccxt.Exchange.round_timeframe(timeframe, date.timestamp() * 1000, + ROUND_UP) // 1000 + return datetime.fromtimestamp(new_timestamp, tz=timezone.utc) + + +def date_minus_candles( + timeframe: str, candle_count: int, date: Optional[datetime] = None) -> datetime: + """ + subtract X candles from a date. + :param timeframe: timeframe in string format (e.g. "5m") + :param candle_count: Amount of candles to subtract. + :param date: date to use. Defaults to now(utc) + + """ + if not date: + date = datetime.now(timezone.utc) + + tf_min = timeframe_to_minutes(timeframe) + new_date = timeframe_to_prev_date(timeframe, date) - timedelta(minutes=tf_min * candle_count) + return new_date + + +def market_is_active(market: Dict) -> bool: + """ + Return True if the market is active. + """ + # "It's active, if the active flag isn't explicitly set to false. If it's missing or + # true then it's true. If it's undefined, then it's most likely true, but not 100% )" + # See https://github.com/ccxt/ccxt/issues/4874, + # https://github.com/ccxt/ccxt/issues/4075#issuecomment-434760520 + return market.get('active', True) is not False + + +def amount_to_contracts(amount: float, contract_size: Optional[float]) -> float: + """ + Convert amount to contracts. + :param amount: amount to convert + :param contract_size: contract size - taken from exchange.get_contract_size(pair) + :return: num-contracts + """ + if contract_size and contract_size != 1: + return float(FtPrecise(amount) / FtPrecise(contract_size)) + else: + return amount + + +def contracts_to_amount(num_contracts: float, contract_size: Optional[float]) -> float: + """ + Takes num-contracts and converts it to contract size + :param num_contracts: number of contracts + :param contract_size: contract size - taken from exchange.get_contract_size(pair) + :return: Amount + """ + + if contract_size and contract_size != 1: + return float(FtPrecise(num_contracts) * FtPrecise(contract_size)) + else: + return num_contracts + + +def amount_to_precision(amount: float, amount_precision: Optional[float], + precisionMode: Optional[int]) -> float: + """ + Returns the amount to buy or sell to a precision the Exchange accepts + Re-implementation of ccxt internal methods - ensuring we can test the result is correct + based on our definitions. + :param amount: amount to truncate + :param amount_precision: amount precision to use. + should be retrieved from markets[pair]['precision']['amount'] + :param precisionMode: precision mode to use. Should be used from precisionMode + one of ccxt's DECIMAL_PLACES, SIGNIFICANT_DIGITS, or TICK_SIZE + :return: truncated amount + """ + if amount_precision is not None and precisionMode is not None: + precision = int(amount_precision) if precisionMode != TICK_SIZE else amount_precision + # precision must be an int for non-ticksize inputs. + amount = float(decimal_to_precision(amount, rounding_mode=TRUNCATE, + precision=precision, + counting_mode=precisionMode, + )) + + return amount + + +def amount_to_contract_precision( + amount, amount_precision: Optional[float], precisionMode: Optional[int], + contract_size: Optional[float]) -> float: + """ + Returns the amount to buy or sell to a precision the Exchange accepts + including calculation to and from contracts. + Re-implementation of ccxt internal methods - ensuring we can test the result is correct + based on our definitions. + :param amount: amount to truncate + :param amount_precision: amount precision to use. + should be retrieved from markets[pair]['precision']['amount'] + :param precisionMode: precision mode to use. Should be used from precisionMode + one of ccxt's DECIMAL_PLACES, SIGNIFICANT_DIGITS, or TICK_SIZE + :param contract_size: contract size - taken from exchange.get_contract_size(pair) + :return: truncated amount + """ + if amount_precision is not None and precisionMode is not None: + contracts = amount_to_contracts(amount, contract_size) + amount_p = amount_to_precision(contracts, amount_precision, precisionMode) + return contracts_to_amount(amount_p, contract_size) + return amount + + +def price_to_precision(price: float, price_precision: Optional[float], + precisionMode: Optional[int]) -> float: + """ + Returns the price rounded up to the precision the Exchange accepts. + Partial Re-implementation of ccxt internal method decimal_to_precision(), + which does not support rounding up + TODO: If ccxt supports ROUND_UP for decimal_to_precision(), we could remove this and + align with amount_to_precision(). + !!! Rounds up + :param price: price to convert + :param price_precision: price precision to use. Used from markets[pair]['precision']['price'] + :param precisionMode: precision mode to use. Should be used from precisionMode + one of ccxt's DECIMAL_PLACES, SIGNIFICANT_DIGITS, or TICK_SIZE + :return: price rounded up to the precision the Exchange accepts + + """ + if price_precision is not None and precisionMode is not None: + # price = float(decimal_to_precision(price, rounding_mode=ROUND, + # precision=price_precision, + # counting_mode=self.precisionMode, + # )) + if precisionMode == TICK_SIZE: + precision = FtPrecise(price_precision) + price_str = FtPrecise(price) + missing = price_str % precision + if not missing == FtPrecise("0"): + price = round(float(str(price_str - missing + precision)), 14) + else: + symbol_prec = price_precision + big_price = price * pow(10, symbol_prec) + price = ceil(big_price) / pow(10, symbol_prec) + return price diff --git a/freqtrade/exchange/gateio.py b/freqtrade/exchange/gateio.py index ab127a036..de178af02 100644 --- a/freqtrade/exchange/gateio.py +++ b/freqtrade/exchange/gateio.py @@ -126,13 +126,3 @@ class Gateio(Exchange): pair=pair, params={'stop': True} ) - - def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool: - """ - Verify stop_loss against stoploss-order value (limit or price) - Returns True if adjustment is necessary. - """ - return (order.get('stopPrice', None) is None or ( - side == "sell" and stop_loss > float(order['stopPrice'])) or - (side == "buy" and stop_loss < float(order['stopPrice'])) - ) diff --git a/freqtrade/exchange/huobi.py b/freqtrade/exchange/huobi.py index 736515dec..fdb6050a3 100644 --- a/freqtrade/exchange/huobi.py +++ b/freqtrade/exchange/huobi.py @@ -2,6 +2,7 @@ import logging from typing import Dict +from freqtrade.constants import BuySell from freqtrade.exchange import Exchange @@ -22,20 +23,7 @@ class Huobi(Exchange): "l2_limit_range_required": False, } - def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool: - """ - Verify stop_loss against stoploss-order value (limit or price) - Returns True if adjustment is necessary. - """ - return ( - order.get('stopPrice', None) is None - or ( - order['type'] == 'stop' - and stop_loss > float(order['stopPrice']) - ) - ) - - def _get_stop_params(self, ordertype: str, stop_price: float) -> Dict: + def _get_stop_params(self, side: BuySell, ordertype: str, stop_price: float) -> Dict: params = self._params.copy() params.update({ diff --git a/freqtrade/exchange/kraken.py b/freqtrade/exchange/kraken.py index 6c9b88eae..f3a9486f2 100644 --- a/freqtrade/exchange/kraken.py +++ b/freqtrade/exchange/kraken.py @@ -12,6 +12,7 @@ from freqtrade.exceptions import (DDosProtection, InsufficientFundsError, Invali OperationalException, TemporaryError) from freqtrade.exchange import Exchange from freqtrade.exchange.common import retrier +from freqtrade.exchange.types import Tickers logger = logging.getLogger(__name__) @@ -45,7 +46,7 @@ class Kraken(Exchange): return (parent_check and market.get('darkpool', False) is False) - def get_tickers(self, symbols: Optional[List[str]] = None, cached: bool = False) -> Dict: + def get_tickers(self, symbols: Optional[List[str]] = None, cached: bool = False) -> Tickers: # Only fetch tickers for current stake currency # Otherwise the request for kraken becomes too large. symbols = list(self.get_markets(quote_currencies=[self._config['stake_currency']])) diff --git a/freqtrade/exchange/kucoin.py b/freqtrade/exchange/kucoin.py index f05fd3f56..6c7d7acfc 100644 --- a/freqtrade/exchange/kucoin.py +++ b/freqtrade/exchange/kucoin.py @@ -2,6 +2,7 @@ import logging from typing import Dict +from freqtrade.constants import BuySell from freqtrade.exchange import Exchange @@ -27,17 +28,7 @@ class Kucoin(Exchange): "ohlcv_candle_limit": 1500, } - def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool: - """ - Verify stop_loss against stoploss-order value (limit or price) - Returns True if adjustment is necessary. - """ - return ( - order.get('stopPrice', None) is None - or stop_loss > float(order['stopPrice']) - ) - - def _get_stop_params(self, ordertype: str, stop_price: float) -> Dict: + def _get_stop_params(self, side: BuySell, ordertype: str, stop_price: float) -> Dict: params = self._params.copy() params.update({ diff --git a/freqtrade/exchange/types.py b/freqtrade/exchange/types.py new file mode 100644 index 000000000..a60b454d4 --- /dev/null +++ b/freqtrade/exchange/types.py @@ -0,0 +1,16 @@ +from typing import Dict, Optional, TypedDict + + +class Ticker(TypedDict): + symbol: str + ask: Optional[float] + askVolume: Optional[float] + bid: Optional[float] + bidVolume: Optional[float] + last: Optional[float] + quoteVolume: Optional[float] + baseVolume: Optional[float] + # Several more - only listing required. + + +Tickers = Dict[str, Ticker] diff --git a/freqtrade/freqai/base_models/BaseClassifierModel.py b/freqtrade/freqai/base_models/BaseClassifierModel.py index 09f1bf98c..17bffa85b 100644 --- a/freqtrade/freqai/base_models/BaseClassifierModel.py +++ b/freqtrade/freqai/base_models/BaseClassifierModel.py @@ -51,7 +51,7 @@ class BaseClassifierModel(IFreqaiModel): f"{end_date} --------------------") # split data into train/test data. data_dictionary = dk.make_train_test_datasets(features_filtered, labels_filtered) - if not self.freqai_info.get("fit_live_predictions", 0) or not self.live: + if not self.freqai_info.get("fit_live_predictions_candles", 0) or not self.live: dk.fit_labels() # normalize all data based on train_dataset only data_dictionary = dk.normalize_data(data_dictionary) @@ -78,7 +78,7 @@ class BaseClassifierModel(IFreqaiModel): ) -> Tuple[DataFrame, npt.NDArray[np.int_]]: """ Filter the prediction features data and predict with it. - :param: unfiltered_df: Full dataframe for the current backtest period. + :param unfiltered_df: Full dataframe for the current backtest period. :return: :pred_df: dataframe containing the predictions :do_predict: np.array of 1s and 0s to indicate places where freqai needed to remove diff --git a/freqtrade/freqai/base_models/BaseRegressionModel.py b/freqtrade/freqai/base_models/BaseRegressionModel.py index 5d89dd356..766579cb6 100644 --- a/freqtrade/freqai/base_models/BaseRegressionModel.py +++ b/freqtrade/freqai/base_models/BaseRegressionModel.py @@ -50,7 +50,7 @@ class BaseRegressionModel(IFreqaiModel): f"{end_date} --------------------") # split data into train/test data. data_dictionary = dk.make_train_test_datasets(features_filtered, labels_filtered) - if not self.freqai_info.get("fit_live_predictions", 0) or not self.live: + if not self.freqai_info.get("fit_live_predictions_candles", 0) or not self.live: dk.fit_labels() # normalize all data based on train_dataset only data_dictionary = dk.normalize_data(data_dictionary) @@ -77,7 +77,7 @@ class BaseRegressionModel(IFreqaiModel): ) -> Tuple[DataFrame, npt.NDArray[np.int_]]: """ Filter the prediction features data and predict with it. - :param: unfiltered_df: Full dataframe for the current backtest period. + :param unfiltered_df: Full dataframe for the current backtest period. :return: :pred_df: dataframe containing the predictions :do_predict: np.array of 1s and 0s to indicate places where freqai needed to remove diff --git a/freqtrade/freqai/base_models/BaseTensorFlowModel.py b/freqtrade/freqai/base_models/BaseTensorFlowModel.py index 00f9d6cba..b41ee0175 100644 --- a/freqtrade/freqai/base_models/BaseTensorFlowModel.py +++ b/freqtrade/freqai/base_models/BaseTensorFlowModel.py @@ -47,7 +47,7 @@ class BaseTensorFlowModel(IFreqaiModel): f"{end_date} --------------------") # split data into train/test data. data_dictionary = dk.make_train_test_datasets(features_filtered, labels_filtered) - if not self.freqai_info.get("fit_live_predictions", 0) or not self.live: + if not self.freqai_info.get("fit_live_predictions_candles", 0) or not self.live: dk.fit_labels() # normalize all data based on train_dataset only data_dictionary = dk.normalize_data(data_dictionary) diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index 22ac9d4c7..a4d5b5d5c 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -1,14 +1,15 @@ import collections -import json import logging import re import shutil import threading +from datetime import datetime, timezone from pathlib import Path from typing import Any, Dict, Tuple, TypedDict import numpy as np import pandas as pd +import psutil import rapidjson from joblib import dump, load from joblib.externals import cloudpickle @@ -65,6 +66,8 @@ class FreqaiDataDrawer: self.pair_dict: Dict[str, pair_info] = {} # dictionary holding all actively inferenced models in memory given a model filename self.model_dictionary: Dict[str, Any] = {} + # all additional metadata that we want to keep in ram + self.meta_data_dictionary: Dict[str, Dict[str, Any]] = {} self.model_return_values: Dict[str, DataFrame] = {} self.historic_data: Dict[str, Dict[str, DataFrame]] = {} self.historic_predictions: Dict[str, DataFrame] = {} @@ -78,19 +81,49 @@ class FreqaiDataDrawer: self.historic_predictions_bkp_path = Path( self.full_path / "historic_predictions.backup.pkl") self.pair_dictionary_path = Path(self.full_path / "pair_dictionary.json") + self.metric_tracker_path = Path(self.full_path / "metric_tracker.json") self.follow_mode = follow_mode if follow_mode: self.create_follower_dict() self.load_drawer_from_disk() self.load_historic_predictions_from_disk() + self.load_metric_tracker_from_disk() self.training_queue: Dict[str, int] = {} self.history_lock = threading.Lock() self.save_lock = threading.Lock() self.pair_dict_lock = threading.Lock() + self.metric_tracker_lock = threading.Lock() self.old_DBSCAN_eps: Dict[str, float] = {} self.empty_pair_dict: pair_info = { "model_filename": "", "trained_timestamp": 0, "data_path": "", "extras": {}} + self.metric_tracker: Dict[str, Dict[str, Dict[str, list]]] = {} + + def update_metric_tracker(self, metric: str, value: float, pair: str) -> None: + """ + General utility for adding and updating custom metrics. Typically used + for adding training performance, train timings, inferenc timings, cpu loads etc. + """ + with self.metric_tracker_lock: + if pair not in self.metric_tracker: + self.metric_tracker[pair] = {} + if metric not in self.metric_tracker[pair]: + self.metric_tracker[pair][metric] = {'timestamp': [], 'value': []} + + timestamp = int(datetime.now(timezone.utc).timestamp()) + self.metric_tracker[pair][metric]['value'].append(value) + self.metric_tracker[pair][metric]['timestamp'].append(timestamp) + + def collect_metrics(self, time_spent: float, pair: str): + """ + Add metrics to the metric tracker dictionary + """ + load1, load5, load15 = psutil.getloadavg() + cpus = psutil.cpu_count() + self.update_metric_tracker('train_time', time_spent, pair) + self.update_metric_tracker('cpu_load1min', load1 / cpus, pair) + self.update_metric_tracker('cpu_load5min', load5 / cpus, pair) + self.update_metric_tracker('cpu_load15min', load15 / cpus, pair) self.limit_ram_use = self.freqai_info.get('limit_ram_usage', False) if 'rl_config' in self.freqai_info: self.model_type = 'stable_baselines' @@ -103,12 +136,12 @@ class FreqaiDataDrawer: """ Locate and load a previously saved data drawer full of all pair model metadata in present model folder. - :return: bool - whether or not the drawer was located + Load any existing metric tracker that may be present. """ exists = self.pair_dictionary_path.is_file() if exists: with open(self.pair_dictionary_path, "r") as fp: - self.pair_dict = json.load(fp) + self.pair_dict = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) elif not self.follow_mode: logger.info("Could not find existing datadrawer, starting from scratch") else: @@ -117,7 +150,18 @@ class FreqaiDataDrawer: "sending null values back to strategy" ) - return exists + def load_metric_tracker_from_disk(self): + """ + Tries to load an existing metrics dictionary if the user + wants to collect metrics. + """ + if self.freqai_info.get('write_metrics_to_disk', False): + exists = self.metric_tracker_path.is_file() + if exists: + with open(self.metric_tracker_path, "r") as fp: + self.metric_tracker = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) + else: + logger.info("Could not find existing metric tracker, starting from scratch") def load_historic_predictions_from_disk(self): """ @@ -153,7 +197,7 @@ class FreqaiDataDrawer: def save_historic_predictions_to_disk(self): """ - Save data drawer full of all pair model metadata in present model folder. + Save historic predictions pickle to disk """ with open(self.historic_predictions_path, "wb") as fp: cloudpickle.dump(self.historic_predictions, fp, protocol=cloudpickle.DEFAULT_PROTOCOL) @@ -161,6 +205,15 @@ class FreqaiDataDrawer: # create a backup shutil.copy(self.historic_predictions_path, self.historic_predictions_bkp_path) + def save_metric_tracker_to_disk(self): + """ + Save metric tracker of all pair metrics collected. + """ + with self.save_lock: + with open(self.metric_tracker_path, 'w') as fp: + rapidjson.dump(self.metric_tracker, fp, default=self.np_encoder, + number_mode=rapidjson.NM_NATIVE) + def save_drawer_to_disk(self): """ Save data drawer full of all pair model metadata in present model folder. @@ -419,9 +472,8 @@ class FreqaiDataDrawer: def save_data(self, model: Any, coin: str, dk: FreqaiDataKitchen) -> None: """ Saves all data associated with a model for a single sub-train time range - :params: - :model: User trained model which can be reused for inferencing to generate - predictions + :param model: User trained model which can be reused for inferencing to generate + predictions """ if not dk.data_path.is_dir(): @@ -466,6 +518,10 @@ class FreqaiDataDrawer: self.model_dictionary[coin] = model self.pair_dict[coin]["model_filename"] = dk.model_filename self.pair_dict[coin]["data_path"] = str(dk.data_path) + if coin not in self.meta_data_dictionary: + self.meta_data_dictionary[coin] = {} + self.meta_data_dictionary[coin]["train_df"] = dk.data_dictionary["train_features"] + self.meta_data_dictionary[coin]["meta_data"] = dk.data self.save_drawer_to_disk() return @@ -476,7 +532,7 @@ class FreqaiDataDrawer: presaved backtesting (prediction file loading). """ with open(dk.data_path / f"{dk.model_filename}_metadata.json", "r") as fp: - dk.data = json.load(fp) + dk.data = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) dk.training_features_list = dk.data["training_features_list"] dk.label_list = dk.data["label_list"] @@ -502,14 +558,19 @@ class FreqaiDataDrawer: / dk.data_path.parts[-1] ) - with open(dk.data_path / f"{dk.model_filename}_metadata.json", "r") as fp: - dk.data = json.load(fp) - dk.training_features_list = dk.data["training_features_list"] - dk.label_list = dk.data["label_list"] + if coin in self.meta_data_dictionary: + dk.data = self.meta_data_dictionary[coin]["meta_data"] + dk.data_dictionary["train_features"] = self.meta_data_dictionary[coin]["train_df"] + else: + with open(dk.data_path / f"{dk.model_filename}_metadata.json", "r") as fp: + dk.data = rapidjson.load(fp, number_mode=rapidjson.NM_NATIVE) - dk.data_dictionary["train_features"] = pd.read_pickle( - dk.data_path / f"{dk.model_filename}_trained_df.pkl" - ) + dk.data_dictionary["train_features"] = pd.read_pickle( + dk.data_path / f"{dk.model_filename}_trained_df.pkl" + ) + + dk.training_features_list = dk.data["training_features_list"] + dk.label_list = dk.data["label_list"] # try to access model in memory instead of loading object from disk to save time if dk.live and coin in self.model_dictionary and not self.limit_ram_use: @@ -549,8 +610,7 @@ class FreqaiDataDrawer: Append new candles to our stores historic data (in memory) so that we do not need to load candle history from disk and we dont need to pinging exchange multiple times for the same candle. - :params: - dataframe: DataFrame = strategy provided dataframe + :param dataframe: DataFrame = strategy provided dataframe """ feat_params = self.freqai_info["feature_parameters"] with self.history_lock: @@ -596,9 +656,8 @@ class FreqaiDataDrawer: """ Load pair histories for all whitelist and corr_pairlist pairs. Only called once upon startup of bot. - :params: - timerange: TimeRange = full timerange required to populate all indicators - for training according to user defined train_period_days + :param timerange: TimeRange = full timerange required to populate all indicators + for training according to user defined train_period_days """ history_data = self.historic_data @@ -621,10 +680,9 @@ class FreqaiDataDrawer: """ Searches through our historic_data in memory and returns the dataframes relevant to the present pair. - :params: - timerange: TimeRange = full timerange required to populate all indicators - for training according to user defined train_period_days - metadata: dict = strategy furnished pair metadata + :param timerange: TimeRange = full timerange required to populate all indicators + for training according to user defined train_period_days + :param metadata: dict = strategy furnished pair metadata """ with self.history_lock: corr_dataframes: Dict[Any, Any] = {} @@ -635,7 +693,8 @@ class FreqaiDataDrawer: ) for tf in self.freqai_info["feature_parameters"].get("include_timeframes"): - base_dataframes[tf] = dk.slice_dataframe(timerange, historic_data[pair][tf]) + base_dataframes[tf] = dk.slice_dataframe( + timerange, historic_data[pair][tf]).reset_index(drop=True) if pairs: for p in pairs: if pair in p: @@ -644,6 +703,6 @@ class FreqaiDataDrawer: corr_dataframes[p] = {} corr_dataframes[p][tf] = dk.slice_dataframe( timerange, historic_data[p][tf] - ) + ).reset_index(drop=True) return corr_dataframes, base_dataframes diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index 3e7f795b2..3dbd47c6e 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -111,9 +111,8 @@ class FreqaiDataKitchen: ) -> None: """ Set the paths to the data for the present coin/botloop - :params: - metadata: dict = strategy furnished pair metadata - trained_timestamp: int = timestamp of most recent training + :param metadata: dict = strategy furnished pair metadata + :param trained_timestamp: int = timestamp of most recent training """ self.full_path = Path( self.config["user_data_dir"] / "models" / str(self.freqai_config.get("identifier")) @@ -133,8 +132,8 @@ class FreqaiDataKitchen: Given the dataframe for the full history for training, split the data into training and test data according to user specified parameters in configuration file. - :filtered_dataframe: cleaned dataframe ready to be split. - :labels: cleaned labels ready to be split. + :param filtered_dataframe: cleaned dataframe ready to be split. + :param labels: cleaned labels ready to be split. """ feat_dict = self.freqai_config["feature_parameters"] @@ -193,13 +192,14 @@ class FreqaiDataKitchen: remove all NaNs. Any row with a NaN is removed from training dataset or replaced with 0s in the prediction dataset. However, prediction dataset do_predict will reflect any row that had a NaN and will shield user from that prediction. - :params: - :unfiltered_df: the full dataframe for the present training period - :training_feature_list: list, the training feature list constructed by - self.build_feature_list() according to user specified parameters in the configuration file. - :labels: the labels for the dataset - :training_filter: boolean which lets the function know if it is training data or - prediction data to be filtered. + + :param unfiltered_df: the full dataframe for the present training period + :param training_feature_list: list, the training feature list constructed by + self.build_feature_list() according to user specified + parameters in the configuration file. + :param labels: the labels for the dataset + :param training_filter: boolean which lets the function know if it is training data or + prediction data to be filtered. :returns: :filtered_df: dataframe cleaned of NaNs and only containing the user requested feature set. @@ -214,7 +214,10 @@ class FreqaiDataKitchen: const_cols = list((filtered_df.nunique() == 1).loc[lambda x: x].index) if const_cols: filtered_df = filtered_df.filter(filtered_df.columns.difference(const_cols)) + self.data['constant_features_list'] = const_cols logger.warning(f"Removed features {const_cols} with constant values.") + else: + self.data['constant_features_list'] = [] # we don't care about total row number (total no. datapoints) in training, we only care # about removing any row with NaNs # if labels has multiple columns (user wants to train multiple modelEs), we detect here @@ -245,6 +248,8 @@ class FreqaiDataKitchen: self.data["filter_drop_index_training"] = drop_index else: + if len(self.data['constant_features_list']): + filtered_df = self.check_pred_labels(filtered_df) # we are backtesting so we need to preserve row number to send back to strategy, # so now we use do_predict to avoid any prediction based on a NaN drop_index = pd.isnull(filtered_df).any(axis=1) @@ -289,8 +294,8 @@ class FreqaiDataKitchen: def normalize_data(self, data_dictionary: Dict) -> Dict[Any, Any]: """ Normalize all data in the data_dictionary according to the training dataset - :params: - :data_dictionary: dictionary containing the cleaned and split training/test data/labels + :param data_dictionary: dictionary containing the cleaned and + split training/test data/labels :returns: :data_dictionary: updated dictionary with standardized values. """ @@ -464,6 +469,22 @@ class FreqaiDataKitchen: return df + def check_pred_labels(self, df_predictions: DataFrame) -> DataFrame: + """ + Check that prediction feature labels match training feature labels. + :param df_predictions: incoming predictions + """ + constant_labels = self.data['constant_features_list'] + df_predictions = df_predictions.filter( + df_predictions.columns.difference(constant_labels) + ) + logger.warning( + f"Removed {len(constant_labels)} features from prediction features, " + f"these were considered constant values during most recent training." + ) + + return df_predictions + def principal_component_analysis(self) -> None: """ Performs Principal Component Analysis on the data for dimensionality reduction @@ -520,8 +541,7 @@ class FreqaiDataKitchen: def pca_transform(self, filtered_dataframe: DataFrame) -> None: """ Use an existing pca transform to transform data into components - :params: - filtered_dataframe: DataFrame = the cleaned dataframe + :param filtered_dataframe: DataFrame = the cleaned dataframe """ pca_components = self.pca.transform(filtered_dataframe) self.data_dictionary["prediction_features"] = pd.DataFrame( @@ -565,8 +585,7 @@ class FreqaiDataKitchen: """ Build/inference a Support Vector Machine to detect outliers in training data and prediction - :params: - predict: bool = If true, inference an existing SVM model, else construct one + :param predict: bool = If true, inference an existing SVM model, else construct one """ if self.keras: @@ -651,11 +670,11 @@ class FreqaiDataKitchen: Use DBSCAN to cluster training data and remove "noisy" data (read outliers). User controls this via the config param `DBSCAN_outlier_pct` which indicates the pct of training data that they want to be considered outliers. - :params: - predict: bool = If False (training), iterate to find the best hyper parameters to match - user requested outlier percent target. If True (prediction), use the parameters - determined from the previous training to estimate if the current prediction point - is an outlier. + :param predict: bool = If False (training), iterate to find the best hyper parameters + to match user requested outlier percent target. + If True (prediction), use the parameters determined from + the previous training to estimate if the current prediction point + is an outlier. """ if predict: @@ -944,6 +963,9 @@ class FreqaiDataKitchen: append_df[f"{label}_mean"] = self.data["labels_mean"][label] append_df[f"{label}_std"] = self.data["labels_std"][label] + for extra_col in self.data["extra_returns_per_train"]: + append_df["{extra_col}"] = self.data["extra_returns_per_train"][extra_col] + append_df["do_predict"] = do_predict if self.freqai_config["feature_parameters"].get("DI_threshold", 0) > 0: append_df["DI_values"] = self.DI_values @@ -1122,15 +1144,13 @@ class FreqaiDataKitchen: prediction_dataframe: DataFrame = pd.DataFrame(), ) -> DataFrame: """ - Use the user defined strategy for populating indicators during - retrain - :params: - strategy: IStrategy = user defined strategy object - corr_dataframes: dict = dict containing the informative pair dataframes - (for user defined timeframes) - base_dataframes: dict = dict containing the current pair dataframes - (for user defined timeframes) - metadata: dict = strategy furnished pair metadata + Use the user defined strategy for populating indicators during retrain + :param strategy: IStrategy = user defined strategy object + :param corr_dataframes: dict = dict containing the informative pair dataframes + (for user defined timeframes) + :param base_dataframes: dict = dict containing the current pair dataframes + (for user defined timeframes) + :param metadata: dict = strategy furnished pair metadata :returns: dataframe: DataFrame = dataframe containing populated indicators """ diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index 931a9d18e..9d4629daf 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -1,5 +1,4 @@ import logging -import shutil import threading import time from abc import ABC, abstractmethod @@ -7,7 +6,7 @@ from collections import deque from datetime import datetime, timezone from pathlib import Path from threading import Lock -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Dict, List, Optional, Literal, Tuple import numpy as np import pandas as pd @@ -22,7 +21,7 @@ from freqtrade.exceptions import OperationalException from freqtrade.exchange import timeframe_to_seconds from freqtrade.freqai.data_drawer import FreqaiDataDrawer from freqtrade.freqai.data_kitchen import FreqaiDataKitchen -from freqtrade.freqai.utils import plot_feature_importance +from freqtrade.freqai.utils import plot_feature_importance, record_params from freqtrade.strategy.interface import IStrategy @@ -62,6 +61,7 @@ class IFreqaiModel(ABC): "data_split_parameters", {}) self.model_training_parameters: Dict[str, Any] = config.get("freqai", {}).get( "model_training_parameters", {}) + self.identifier: str = self.freqai_info.get("identifier", "no_id_provided") self.retrain = False self.first = True self.set_full_path() @@ -70,7 +70,6 @@ class IFreqaiModel(ABC): if self.save_backtest_models: logger.info('Backtesting module configured to save all models.') self.dd = FreqaiDataDrawer(Path(self.full_path), self.config, self.follow_mode) - self.identifier: str = self.freqai_info.get("identifier", "no_id_provided") self.scanning = False self.ft_params = self.freqai_info["feature_parameters"] self.keras: bool = self.freqai_info.get("keras", False) @@ -100,12 +99,13 @@ class IFreqaiModel(ABC): self.strategy: Optional[IStrategy] = None self.max_system_threads = max(int(psutil.cpu_count() * 2 - 2), 1) + record_params(config, self.full_path) + def __getstate__(self): """ Return an empty state to be pickled in hyperopt """ return ({}) - self.strategy: Optional[IStrategy] = None def assert_config(self, config: Config) -> None: @@ -149,7 +149,7 @@ class IFreqaiModel(ABC): dataframe = dk.remove_features_from_df(dk.return_dataframe) self.clean_up() if self.live: - self.inference_timer('stop') + self.inference_timer('stop', metadata["pair"]) return dataframe def clean_up(self): @@ -210,29 +210,31 @@ class IFreqaiModel(ABC): (_, trained_timestamp, _) = self.dd.get_pair_dict_info(pair) dk = FreqaiDataKitchen(self.config, self.live, pair) - dk.set_paths(pair, trained_timestamp) ( retrain, new_trained_timerange, data_load_timerange, ) = dk.check_if_new_training_required(trained_timestamp) - dk.set_paths(pair, new_trained_timerange.stopts) if retrain: self.train_timer('start') + dk.set_paths(pair, new_trained_timerange.stopts) try: self.extract_data_and_train_model( new_trained_timerange, pair, strategy, dk, data_load_timerange ) except Exception as msg: - logger.warning(f'Training {pair} raised exception {msg}, skipping.') + logger.warning(f"Training {pair} raised exception {msg.__class__.__name__}. " + f"Message: {msg}, skipping.") - self.train_timer('stop') + self.train_timer('stop', pair) # only rotate the queue after the first has been trained. self.train_queue.rotate(-1) self.dd.save_historic_predictions_to_disk() + if self.freqai_info.get('write_metrics_to_disk', False): + self.dd.save_metric_tracker_to_disk() def start_backtesting( self, dataframe: DataFrame, metadata: dict, dk: FreqaiDataKitchen @@ -281,9 +283,7 @@ class IFreqaiModel(ABC): ) trained_timestamp_int = int(trained_timestamp.stopts) - dk.data_path = Path( - dk.full_path / f"sub-train-{pair.split('/')[0]}_{trained_timestamp_int}" - ) + dk.set_paths(pair, trained_timestamp_int) dk.set_new_model_names(pair, trained_timestamp) @@ -540,14 +540,13 @@ class IFreqaiModel(ABC): return file_exists def set_full_path(self) -> None: + """ + Creates and sets the full path for the identifier + """ self.full_path = Path( - self.config["user_data_dir"] / "models" / f"{self.freqai_info['identifier']}" + self.config["user_data_dir"] / "models" / f"{self.identifier}" ) self.full_path.mkdir(parents=True, exist_ok=True) - shutil.copy( - self.config["config_files"][0], - Path(self.full_path, Path(self.config["config_files"][0]).name), - ) def extract_data_and_train_model( self, @@ -616,11 +615,11 @@ class IFreqaiModel(ABC): If the user reuses an identifier on a subsequent instance, this function will not be called. In that case, "real" predictions will be appended to the loaded set of historic predictions. - :param: df: DataFrame = the dataframe containing the training feature data - :param: model: Any = A model which was `fit` using a common library such as - catboost or lightgbm - :param: dk: FreqaiDataKitchen = object containing methods for data analysis - :param: pair: str = current pair + :param df: DataFrame = the dataframe containing the training feature data + :param model: Any = A model which was `fit` using a common library such as + catboost or lightgbm + :param dk: FreqaiDataKitchen = object containing methods for data analysis + :param pair: str = current pair """ self.dd.historic_predictions[pair] = pred_df @@ -671,7 +670,7 @@ class IFreqaiModel(ABC): return - def inference_timer(self, do='start'): + def inference_timer(self, do: Literal['start', 'stop'] = 'start', pair: str = ''): """ Timer designed to track the cumulative time spent in FreqAI for one pass through the whitelist. This will check if the time spent is more than 1/4 the time @@ -682,7 +681,10 @@ class IFreqaiModel(ABC): self.begin_time = time.time() elif do == 'stop': end = time.time() - self.inference_time += (end - self.begin_time) + time_spent = (end - self.begin_time) + if self.freqai_info.get('write_metrics_to_disk', False): + self.dd.update_metric_tracker('inference_time', time_spent, pair) + self.inference_time += time_spent if self.pair_it == self.total_pairs: logger.info( f'Total time spent inferencing pairlist {self.inference_time:.2f} seconds') @@ -693,7 +695,7 @@ class IFreqaiModel(ABC): self.inference_time = 0 return - def train_timer(self, do='start'): + def train_timer(self, do: Literal['start', 'stop'] = 'start', pair: str = ''): """ Timer designed to track the cumulative time spent training the full pairlist in FreqAI. @@ -703,7 +705,11 @@ class IFreqaiModel(ABC): self.begin_time_train = time.time() elif do == 'stop': end = time.time() - self.train_time += (end - self.begin_time_train) + time_spent = (end - self.begin_time_train) + if self.freqai_info.get('write_metrics_to_disk', False): + self.dd.collect_metrics(time_spent, pair) + + self.train_time += time_spent if self.pair_it_train == self.total_pairs: logger.info( f'Total time spent training pairlist {self.train_time:.2f} seconds') diff --git a/freqtrade/freqai/prediction_models/CatboostClassifier.py b/freqtrade/freqai/prediction_models/CatboostClassifier.py index 60536e6de..ca1d8ece0 100644 --- a/freqtrade/freqai/prediction_models/CatboostClassifier.py +++ b/freqtrade/freqai/prediction_models/CatboostClassifier.py @@ -1,4 +1,6 @@ import logging +import sys +from pathlib import Path from typing import Any, Dict from catboost import CatBoostClassifier, Pool @@ -20,9 +22,8 @@ class CatboostClassifier(BaseClassifierModel): def fit(self, data_dictionary: Dict, dk: FreqaiDataKitchen, **kwargs) -> Any: """ User sets up the training and test data to fit their desired model here - :params: - :data_dictionary: the dictionary constructed by DataHandler to hold - all the training and test data/labels. + :param data_dictionary: the dictionary constructed by DataHandler to hold + all the training and test data/labels. """ train_data = Pool( @@ -30,15 +31,25 @@ class CatboostClassifier(BaseClassifierModel): label=data_dictionary["train_labels"], weight=data_dictionary["train_weights"], ) + if self.freqai_info.get("data_split_parameters", {}).get("test_size", 0.1) == 0: + test_data = None + else: + test_data = Pool( + data=data_dictionary["test_features"], + label=data_dictionary["test_labels"], + weight=data_dictionary["test_weights"], + ) cbr = CatBoostClassifier( - allow_writing_files=False, + allow_writing_files=True, loss_function='MultiClass', + train_dir=Path(dk.data_path), **self.model_training_parameters, ) init_model = self.get_init_model(dk.pair) - cbr.fit(train_data, init_model=init_model) + cbr.fit(X=train_data, eval_set=test_data, init_model=init_model, + log_cout=sys.stdout, log_cerr=sys.stderr) return cbr diff --git a/freqtrade/freqai/prediction_models/CatboostRegressor.py b/freqtrade/freqai/prediction_models/CatboostRegressor.py index 73cf6c88a..4b17a703b 100644 --- a/freqtrade/freqai/prediction_models/CatboostRegressor.py +++ b/freqtrade/freqai/prediction_models/CatboostRegressor.py @@ -1,4 +1,6 @@ import logging +import sys +from pathlib import Path from typing import Any, Dict from catboost import CatBoostRegressor, Pool @@ -41,10 +43,12 @@ class CatboostRegressor(BaseRegressionModel): init_model = self.get_init_model(dk.pair) model = CatBoostRegressor( - allow_writing_files=False, + allow_writing_files=True, + train_dir=Path(dk.data_path), **self.model_training_parameters, ) - model.fit(X=train_data, eval_set=test_data, init_model=init_model) + model.fit(X=train_data, eval_set=test_data, init_model=init_model, + log_cout=sys.stdout, log_cerr=sys.stderr) return model diff --git a/freqtrade/freqai/prediction_models/CatboostRegressorMultiTarget.py b/freqtrade/freqai/prediction_models/CatboostRegressorMultiTarget.py index 7fa4e293e..976d0b29b 100644 --- a/freqtrade/freqai/prediction_models/CatboostRegressorMultiTarget.py +++ b/freqtrade/freqai/prediction_models/CatboostRegressorMultiTarget.py @@ -1,4 +1,6 @@ import logging +import sys +from pathlib import Path from typing import Any, Dict from catboost import CatBoostRegressor, Pool @@ -26,7 +28,8 @@ class CatboostRegressorMultiTarget(BaseRegressionModel): """ cbr = CatBoostRegressor( - allow_writing_files=False, + allow_writing_files=True, + train_dir=Path(dk.data_path), **self.model_training_parameters, ) @@ -56,8 +59,10 @@ class CatboostRegressorMultiTarget(BaseRegressionModel): fit_params = [] for i in range(len(eval_sets)): - fit_params.append( - {'eval_set': eval_sets[i], 'init_model': init_models[i]}) + fit_params.append({ + 'eval_set': eval_sets[i], 'init_model': init_models[i], + 'log_cout': sys.stdout, 'log_cerr': sys.stderr, + }) model = FreqaiMultiOutputRegressor(estimator=cbr) thread_training = self.freqai_info.get('multitarget_parallel_training', False) diff --git a/freqtrade/freqai/prediction_models/LightGBMClassifier.py b/freqtrade/freqai/prediction_models/LightGBMClassifier.py index 3eec516ba..e467ad3c1 100644 --- a/freqtrade/freqai/prediction_models/LightGBMClassifier.py +++ b/freqtrade/freqai/prediction_models/LightGBMClassifier.py @@ -20,9 +20,8 @@ class LightGBMClassifier(BaseClassifierModel): def fit(self, data_dictionary: Dict, dk: FreqaiDataKitchen, **kwargs) -> Any: """ User sets up the training and test data to fit their desired model here - :params: - :data_dictionary: the dictionary constructed by DataHandler to hold - all the training and test data/labels. + :param data_dictionary: the dictionary constructed by DataHandler to hold + all the training and test data/labels. """ if self.freqai_info.get('data_split_parameters', {}).get('test_size', 0.1) == 0: diff --git a/freqtrade/freqai/prediction_models/XGBoostClassifier.py b/freqtrade/freqai/prediction_models/XGBoostClassifier.py index 8bf5d6281..67c7c7783 100644 --- a/freqtrade/freqai/prediction_models/XGBoostClassifier.py +++ b/freqtrade/freqai/prediction_models/XGBoostClassifier.py @@ -26,9 +26,8 @@ class XGBoostClassifier(BaseClassifierModel): def fit(self, data_dictionary: Dict, dk: FreqaiDataKitchen, **kwargs) -> Any: """ User sets up the training and test data to fit their desired model here - :params: - :data_dictionary: the dictionary constructed by DataHandler to hold - all the training and test data/labels. + :param data_dictionary: the dictionary constructed by DataHandler to hold + all the training and test data/labels. """ X = data_dictionary["train_features"].to_numpy() @@ -65,7 +64,7 @@ class XGBoostClassifier(BaseClassifierModel): ) -> Tuple[DataFrame, npt.NDArray[np.int_]]: """ Filter the prediction features data and predict with it. - :param: unfiltered_df: Full dataframe for the current backtest period. + :param unfiltered_df: Full dataframe for the current backtest period. :return: :pred_df: dataframe containing the predictions :do_predict: np.array of 1s and 0s to indicate places where freqai needed to remove diff --git a/freqtrade/freqai/prediction_models/XGBoostRFClassifier.py b/freqtrade/freqai/prediction_models/XGBoostRFClassifier.py new file mode 100644 index 000000000..470c283ea --- /dev/null +++ b/freqtrade/freqai/prediction_models/XGBoostRFClassifier.py @@ -0,0 +1,84 @@ +import logging +from typing import Any, Dict, Tuple + +import numpy as np +import numpy.typing as npt +import pandas as pd +from pandas import DataFrame +from pandas.api.types import is_integer_dtype +from sklearn.preprocessing import LabelEncoder +from xgboost import XGBRFClassifier + +from freqtrade.freqai.base_models.BaseClassifierModel import BaseClassifierModel +from freqtrade.freqai.data_kitchen import FreqaiDataKitchen + + +logger = logging.getLogger(__name__) + + +class XGBoostRFClassifier(BaseClassifierModel): + """ + User created prediction model. The class needs to override three necessary + functions, predict(), train(), fit(). The class inherits ModelHandler which + has its own DataHandler where data is held, saved, loaded, and managed. + """ + + def fit(self, data_dictionary: Dict, dk: FreqaiDataKitchen, **kwargs) -> Any: + """ + User sets up the training and test data to fit their desired model here + :param data_dictionary: the dictionary constructed by DataHandler to hold + all the training and test data/labels. + """ + + X = data_dictionary["train_features"].to_numpy() + y = data_dictionary["train_labels"].to_numpy()[:, 0] + + le = LabelEncoder() + if not is_integer_dtype(y): + y = pd.Series(le.fit_transform(y), dtype="int64") + + if self.freqai_info.get('data_split_parameters', {}).get('test_size', 0.1) == 0: + eval_set = None + else: + test_features = data_dictionary["test_features"].to_numpy() + test_labels = data_dictionary["test_labels"].to_numpy()[:, 0] + + if not is_integer_dtype(test_labels): + test_labels = pd.Series(le.transform(test_labels), dtype="int64") + + eval_set = [(test_features, test_labels)] + + train_weights = data_dictionary["train_weights"] + + init_model = self.get_init_model(dk.pair) + + model = XGBRFClassifier(**self.model_training_parameters) + + model.fit(X=X, y=y, eval_set=eval_set, sample_weight=train_weights, + xgb_model=init_model) + + return model + + def predict( + self, unfiltered_df: DataFrame, dk: FreqaiDataKitchen, **kwargs + ) -> Tuple[DataFrame, npt.NDArray[np.int_]]: + """ + Filter the prediction features data and predict with it. + :param unfiltered_df: Full dataframe for the current backtest period. + :return: + :pred_df: dataframe containing the predictions + :do_predict: np.array of 1s and 0s to indicate places where freqai needed to remove + data (NaNs) or felt uncertain about data (PCA and DI index) + """ + + (pred_df, dk.do_predict) = super().predict(unfiltered_df, dk, **kwargs) + + le = LabelEncoder() + label = dk.label_list[0] + labels_before = list(dk.data['labels_std'].keys()) + labels_after = le.fit_transform(labels_before).tolist() + pred_df[label] = le.inverse_transform(pred_df[label]) + pred_df = pred_df.rename( + columns={labels_after[i]: labels_before[i] for i in range(len(labels_before))}) + + return (pred_df, dk.do_predict) diff --git a/freqtrade/freqai/prediction_models/XGBoostRFRegressor.py b/freqtrade/freqai/prediction_models/XGBoostRFRegressor.py new file mode 100644 index 000000000..e7cc27f2e --- /dev/null +++ b/freqtrade/freqai/prediction_models/XGBoostRFRegressor.py @@ -0,0 +1,46 @@ +import logging +from typing import Any, Dict + +from xgboost import XGBRFRegressor + +from freqtrade.freqai.base_models.BaseRegressionModel import BaseRegressionModel +from freqtrade.freqai.data_kitchen import FreqaiDataKitchen + + +logger = logging.getLogger(__name__) + + +class XGBoostRFRegressor(BaseRegressionModel): + """ + User created prediction model. The class needs to override three necessary + functions, predict(), train(), fit(). The class inherits ModelHandler which + has its own DataHandler where data is held, saved, loaded, and managed. + """ + + def fit(self, data_dictionary: Dict, dk: FreqaiDataKitchen, **kwargs) -> Any: + """ + User sets up the training and test data to fit their desired model here + :param data_dictionary: the dictionary constructed by DataHandler to hold + all the training and test data/labels. + """ + + X = data_dictionary["train_features"] + y = data_dictionary["train_labels"] + + if self.freqai_info.get("data_split_parameters", {}).get("test_size", 0.1) == 0: + eval_set = None + eval_weights = None + else: + eval_set = [(data_dictionary["test_features"], data_dictionary["test_labels"])] + eval_weights = [data_dictionary['test_weights']] + + sample_weight = data_dictionary["train_weights"] + + xgb_model = self.get_init_model(dk.pair) + + model = XGBRFRegressor(**self.model_training_parameters) + + model.fit(X=X, y=y, sample_weight=sample_weight, eval_set=eval_set, + sample_weight_eval_set=eval_weights, xgb_model=xgb_model) + + return model diff --git a/freqtrade/freqai/prediction_models/XGBoostRegressor.py b/freqtrade/freqai/prediction_models/XGBoostRegressor.py index c9be9ce74..9a280286b 100644 --- a/freqtrade/freqai/prediction_models/XGBoostRegressor.py +++ b/freqtrade/freqai/prediction_models/XGBoostRegressor.py @@ -29,6 +29,7 @@ class XGBoostRegressor(BaseRegressionModel): if self.freqai_info.get("data_split_parameters", {}).get("test_size", 0.1) == 0: eval_set = None + eval_weights = None else: eval_set = [(data_dictionary["test_features"], data_dictionary["test_labels"])] eval_weights = [data_dictionary['test_weights']] diff --git a/freqtrade/freqai/utils.py b/freqtrade/freqai/utils.py index 22bc1e06e..5a2b9b7d6 100644 --- a/freqtrade/freqai/utils.py +++ b/freqtrade/freqai/utils.py @@ -1,9 +1,11 @@ import logging from datetime import datetime, timezone -from typing import Any +from pathlib import Path +from typing import Any, Dict import numpy as np import pandas as pd +import rapidjson from freqtrade.configuration import TimeRange from freqtrade.constants import Config @@ -191,3 +193,28 @@ def plot_feature_importance(model: Any, pair: str, dk: FreqaiDataKitchen, fig.update_layout(title_text=f"Best and worst features by importance {pair}") label = label.replace('&', '').replace('%', '') # escape two FreqAI specific characters store_plot_file(fig, f"{dk.model_filename}-{label}.html", dk.data_path) + + +def record_params(config: Dict[str, Any], full_path: Path) -> None: + """ + Records run params in the full path for reproducibility + """ + params_record_path = full_path / "run_params.json" + + run_params = { + "freqai": config.get('freqai', {}), + "timeframe": config.get('timeframe'), + "stake_amount": config.get('stake_amount'), + "stake_currency": config.get('stake_currency'), + "max_open_trades": config.get('max_open_trades'), + "pairs": config.get('exchange', {}).get('pair_whitelist') + } + + with open(params_record_path, "w") as handle: + rapidjson.dump( + run_params, + handle, + indent=4, + default=str, + number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN + ) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index cd111679c..ea7c2f1f9 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1471,12 +1471,13 @@ class FreqtradeBot(LoggingMixin): ) return cancelled - def _safe_exit_amount(self, pair: str, amount: float) -> float: + def _safe_exit_amount(self, trade: Trade, pair: str, amount: float) -> float: """ Get sellable amount. Should be trade.amount - but will fall back to the available amount if necessary. This should cover cases where get_real_amount() was not able to update the amount for whatever reason. + :param trade: Trade we're working with :param pair: Pair we're trying to sell :param amount: amount we expect to be available :return: amount to sell @@ -1495,6 +1496,7 @@ class FreqtradeBot(LoggingMixin): return amount elif wallet_amount > amount * 0.98: logger.info(f"{pair} - Falling back to wallet-amount {wallet_amount} -> {amount}.") + trade.amount = wallet_amount return wallet_amount else: raise DependencyException( @@ -1553,7 +1555,7 @@ class FreqtradeBot(LoggingMixin): # Emergency sells (default to market!) order_type = self.strategy.order_types.get("emergency_exit", "market") - amount = self._safe_exit_amount(trade.pair, sub_trade_amt or trade.amount) + amount = self._safe_exit_amount(trade, trade.pair, sub_trade_amt or trade.amount) time_in_force = self.strategy.order_time_in_force['exit'] if (exit_check.exit_type != ExitType.LIQUIDATION @@ -1828,7 +1830,7 @@ class FreqtradeBot(LoggingMixin): never in base currency. """ self.wallets.update() - amount_ = amount + amount_ = trade.amount if order_obj.ft_order_side == trade.exit_side or order_obj.ft_order_side == 'stoploss': # check against remaining amount! amount_ = trade.amount - amount diff --git a/freqtrade/misc.py b/freqtrade/misc.py index 56b3fef0e..49d33d46f 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -6,7 +6,7 @@ import logging import re from datetime import datetime from pathlib import Path -from typing import Any, Iterator, List +from typing import Any, Dict, Iterator, List, Mapping, Union from typing.io import IO from urllib.parse import urlparse @@ -186,7 +186,10 @@ def safe_value_fallback(obj: dict, key1: str, key2: str, default_value=None): return default_value -def safe_value_fallback2(dict1: dict, dict2: dict, key1: str, key2: str, default_value=None): +dictMap = Union[Dict[str, Any], Mapping[str, Any]] + + +def safe_value_fallback2(dict1: dictMap, dict2: dictMap, key1: str, key2: str, default_value=None): """ Search a value in dict1, return this if it's not None. Fall back to dict2 - return key2 from dict2 if it's not None. diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 83159dfe4..4d98f1f5a 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -151,6 +151,8 @@ class Backtesting: self.trading_mode: TradingMode = config.get('trading_mode', TradingMode.SPOT) # strategies which define "can_short=True" will fail to load in Spot mode. self._can_short = self.trading_mode != TradingMode.SPOT + self._position_stacking: bool = self.config.get('position_stacking', False) + self.enable_protections: bool = self.config.get('enable_protections', False) self.init_backtest() @@ -617,13 +619,16 @@ class Backtesting: exit_reason = row[EXIT_TAG_IDX] # Custom exit pricing only for exit-signals if order_type == 'limit': - close_rate = strategy_safe_wrapper(self.strategy.custom_exit_price, - default_retval=close_rate)( + rate = strategy_safe_wrapper(self.strategy.custom_exit_price, + default_retval=close_rate)( pair=trade.pair, trade=trade, # type: ignore[arg-type] current_time=exit_candle_time, proposed_rate=close_rate, current_profit=current_profit, exit_tag=exit_reason) + if rate != close_rate: + close_rate = price_to_precision(rate, trade.price_precision, + self.precision_mode) # We can't place orders lower than current low. # freqtrade does not support this in live, and the order would fill immediately if trade.is_short: @@ -660,7 +665,6 @@ class Backtesting: # amount = amount or trade.amount amount = amount_to_contract_precision(amount or trade.amount, trade.amount_precision, self.precision_mode, trade.contract_size) - rate = price_to_precision(close_rate, trade.price_precision, self.precision_mode) order = Order( id=self.order_id_counter, ft_trade_id=trade.id, @@ -674,12 +678,12 @@ class Backtesting: side=trade.exit_side, order_type=order_type, status="open", - price=rate, - average=rate, + price=close_rate, + average=close_rate, amount=amount, filled=0, remaining=amount, - cost=amount * rate, + cost=amount * close_rate, ) trade.orders.append(order) return trade @@ -726,18 +730,21 @@ class Backtesting: def get_valid_price_and_stake( self, pair: str, row: Tuple, propose_rate: float, stake_amount: float, direction: LongShort, current_time: datetime, entry_tag: Optional[str], - trade: Optional[LocalTrade], order_type: str + trade: Optional[LocalTrade], order_type: str, price_precision: Optional[float] ) -> Tuple[float, float, float, float]: if order_type == 'limit': - propose_rate = strategy_safe_wrapper(self.strategy.custom_entry_price, - default_retval=propose_rate)( + new_rate = strategy_safe_wrapper(self.strategy.custom_entry_price, + default_retval=propose_rate)( pair=pair, current_time=current_time, proposed_rate=propose_rate, entry_tag=entry_tag, side=direction, ) # default value is the open rate # We can't place orders higher than current high (otherwise it'd be a stop limit entry) # which freqtrade does not support in live. + if new_rate != propose_rate: + propose_rate = price_to_precision(new_rate, price_precision, + self.precision_mode) if direction == "short": propose_rate = max(propose_rate, row[LOW_IDX]) else: @@ -799,9 +806,11 @@ class Backtesting: pos_adjust = trade is not None and requested_rate is None stake_amount_ = stake_amount or (trade.stake_amount if trade else 0.0) + precision_price = self.exchange.get_precision_price(pair) + propose_rate, stake_amount, leverage, min_stake_amount = self.get_valid_price_and_stake( pair, row, row[OPEN_IDX], stake_amount_, direction, current_time, entry_tag, trade, - order_type + order_type, precision_price, ) # replace proposed rate if another rate was requested @@ -817,8 +826,6 @@ class Backtesting: if stake_amount and (not min_stake_amount or stake_amount > min_stake_amount): self.order_id_counter += 1 base_currency = self.exchange.get_pair_base_currency(pair) - precision_price = self.exchange.get_precision_price(pair) - propose_rate = price_to_precision(propose_rate, precision_price, self.precision_mode) amount_p = (stake_amount / propose_rate) * leverage contract_size = self.exchange.get_contract_size(pair) @@ -914,30 +921,23 @@ class Backtesting: return trade def handle_left_open(self, open_trades: Dict[str, List[LocalTrade]], - data: Dict[str, List[Tuple]]) -> List[LocalTrade]: + data: Dict[str, List[Tuple]]) -> None: """ Handling of left open trades at the end of backtesting """ - trades = [] for pair in open_trades.keys(): - if len(open_trades[pair]) > 0: - for trade in open_trades[pair]: - if trade.open_order_id and trade.nr_of_successful_entries == 0: - # Ignore trade if entry-order did not fill yet - continue - exit_row = data[pair][-1] - self._exit_trade(trade, exit_row, exit_row[OPEN_IDX], trade.amount) - trade.orders[-1].close_bt_order(exit_row[DATE_IDX].to_pydatetime(), trade) + for trade in list(open_trades[pair]): + if trade.open_order_id and trade.nr_of_successful_entries == 0: + # Ignore trade if entry-order did not fill yet + continue + exit_row = data[pair][-1] + self._exit_trade(trade, exit_row, exit_row[OPEN_IDX], trade.amount) + trade.orders[-1].close_bt_order(exit_row[DATE_IDX].to_pydatetime(), trade) - trade.close_date = exit_row[DATE_IDX].to_pydatetime() - trade.exit_reason = ExitType.FORCE_EXIT.value - trade.close(exit_row[OPEN_IDX], show_msg=False) - LocalTrade.close_bt_trade(trade) - # Deepcopy object to have wallets update correctly - trade1 = deepcopy(trade) - trade1.is_open = True - trades.append(trade1) - return trades + trade.close_date = exit_row[DATE_IDX].to_pydatetime() + trade.exit_reason = ExitType.FORCE_EXIT.value + trade.close(exit_row[OPEN_IDX], show_msg=False) + LocalTrade.close_bt_trade(trade) def trade_slot_available(self, max_open_trades: int, open_trade_count: int) -> bool: # Always allow trades when max_open_trades is enabled. @@ -961,9 +961,8 @@ class Backtesting: return 'short' return None - def run_protections( - self, enable_protections, pair: str, current_time: datetime, side: LongShort): - if enable_protections: + def run_protections(self, pair: str, current_time: datetime, side: LongShort): + if self.enable_protections: self.protections.stop_per_pair(pair, current_time, side) self.protections.global_stop(current_time, side) @@ -1069,10 +1068,78 @@ class Backtesting: return None return row - def backtest(self, processed: Dict, # noqa: max-complexity: 13 + def backtest_loop( + self, row: Tuple, pair: str, current_time: datetime, end_date: datetime, + max_open_trades: int, open_trade_count_start: int) -> int: + """ + NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized. + + Backtesting processing for one candle/pair. + """ + for t in list(LocalTrade.bt_trades_open_pp[pair]): + # 1. Manage currently open orders of active trades + if self.manage_open_orders(t, current_time, row): + # Close trade + open_trade_count_start -= 1 + LocalTrade.remove_bt_trade(t) + self.wallets.update() + + # 2. Process entries. + # without positionstacking, we can only have one open trade per pair. + # max_open_trades must be respected + # don't open on the last row + trade_dir = self.check_for_trade_entry(row) + if ( + (self._position_stacking or len(LocalTrade.bt_trades_open_pp[pair]) == 0) + and self.trade_slot_available(max_open_trades, open_trade_count_start) + and current_time != end_date + and trade_dir is not None + and not PairLocks.is_pair_locked(pair, row[DATE_IDX], trade_dir) + ): + trade = self._enter_trade(pair, row, trade_dir) + if trade: + # TODO: hacky workaround to avoid opening > max_open_trades + # This emulates previous behavior - not sure if this is correct + # Prevents entering if the trade-slot was freed in this candle + open_trade_count_start += 1 + # logger.debug(f"{pair} - Emulate creation of new trade: {trade}.") + LocalTrade.add_bt_trade(trade) + self.wallets.update() + + for trade in list(LocalTrade.bt_trades_open_pp[pair]): + # 3. Process entry orders. + order = trade.select_order(trade.entry_side, is_open=True) + if order and self._get_order_filled(order.price, row): + order.close_bt_order(current_time, trade) + trade.open_order_id = None + self.wallets.update() + + # 4. Create exit orders (if any) + if not trade.open_order_id: + self._get_exit_trade_entry(trade, row) # Place exit order if necessary + + # 5. Process exit orders. + order = trade.select_order(trade.exit_side, is_open=True) + if order and self._get_order_filled(order.price, row): + order.close_bt_order(current_time, trade) + trade.open_order_id = None + sub_trade = order.safe_amount_after_fee != trade.amount + if sub_trade: + order.close_bt_order(current_time, trade) + trade.recalc_trade_from_orders() + else: + trade.close_date = current_time + trade.close(order.price, show_msg=False) + + # logger.debug(f"{pair} - Backtesting exit {trade}") + LocalTrade.close_bt_trade(trade) + self.wallets.update() + self.run_protections(pair, current_time, trade.trade_direction) + return open_trade_count_start + + def backtest(self, processed: Dict, start_date: datetime, end_date: datetime, - max_open_trades: int = 0, position_stacking: bool = False, - enable_protections: bool = False) -> Dict[str, Any]: + max_open_trades: int = 0) -> Dict[str, Any]: """ Implement backtesting functionality @@ -1085,12 +1152,9 @@ class Backtesting: :param start_date: backtesting timerange start datetime :param end_date: backtesting timerange end datetime :param max_open_trades: maximum number of concurrent trades, <= 0 means unlimited - :param position_stacking: do we allow position stacking? - :param enable_protections: Should protections be enabled? :return: DataFrame with trades (results of backtesting) """ - trades: List[LocalTrade] = [] - self.prepare_backtest(enable_protections) + self.prepare_backtest(self.enable_protections) # Ensure wallets are uptodate (important for --strategy-list) self.wallets.update() # Use dict of lists with data for performance @@ -1101,15 +1165,12 @@ class Backtesting: indexes: Dict = defaultdict(int) current_time = start_date + timedelta(minutes=self.timeframe_min) - open_trades: Dict[str, List[LocalTrade]] = defaultdict(list) - open_trade_count = 0 - self.progress.init_step(BacktestState.BACKTEST, int( (end_date - start_date) / timedelta(minutes=self.timeframe_min))) # Loop timerange and get candle for each pair at that point in time while current_time <= end_date: - open_trade_count_start = open_trade_count + open_trade_count_start = LocalTrade.bt_open_open_trade_count self.check_abort() for i, pair in enumerate(data): row_index = indexes[pair] @@ -1121,81 +1182,17 @@ class Backtesting: indexes[pair] = row_index self.dataprovider._set_dataframe_max_index(row_index) - for t in list(open_trades[pair]): - # 1. Manage currently open orders of active trades - if self.manage_open_orders(t, current_time, row): - # Close trade - open_trade_count -= 1 - open_trades[pair].remove(t) - LocalTrade.trades_open.remove(t) - self.wallets.update() - - # 2. Process entries. - # without positionstacking, we can only have one open trade per pair. - # max_open_trades must be respected - # don't open on the last row - trade_dir = self.check_for_trade_entry(row) - if ( - (position_stacking or len(open_trades[pair]) == 0) - and self.trade_slot_available(max_open_trades, open_trade_count_start) - and current_time != end_date - and trade_dir is not None - and not PairLocks.is_pair_locked(pair, row[DATE_IDX], trade_dir) - ): - trade = self._enter_trade(pair, row, trade_dir) - if trade: - # TODO: hacky workaround to avoid opening > max_open_trades - # This emulates previous behavior - not sure if this is correct - # Prevents entering if the trade-slot was freed in this candle - open_trade_count_start += 1 - open_trade_count += 1 - # logger.debug(f"{pair} - Emulate creation of new trade: {trade}.") - open_trades[pair].append(trade) - LocalTrade.add_bt_trade(trade) - self.wallets.update() - - for trade in list(open_trades[pair]): - # 3. Process entry orders. - order = trade.select_order(trade.entry_side, is_open=True) - if order and self._get_order_filled(order.price, row): - order.close_bt_order(current_time, trade) - trade.open_order_id = None - self.wallets.update() - - # 4. Create exit orders (if any) - if not trade.open_order_id: - self._get_exit_trade_entry(trade, row) # Place exit order if necessary - - # 5. Process exit orders. - order = trade.select_order(trade.exit_side, is_open=True) - if order and self._get_order_filled(order.price, row): - order.close_bt_order(current_time, trade) - trade.open_order_id = None - sub_trade = order.safe_amount_after_fee != trade.amount - if sub_trade: - order.close_bt_order(current_time, trade) - trade.recalc_trade_from_orders() - else: - trade.close_date = current_time - trade.close(order.price, show_msg=False) - - # logger.debug(f"{pair} - Backtesting exit {trade}") - open_trade_count -= 1 - open_trades[pair].remove(trade) - LocalTrade.close_bt_trade(trade) - trades.append(trade) - self.wallets.update() - self.run_protections( - enable_protections, pair, current_time, trade.trade_direction) + open_trade_count_start = self.backtest_loop( + row, pair, current_time, end_date, max_open_trades, open_trade_count_start) # Move time one configured time_interval ahead. self.progress.increment() current_time += timedelta(minutes=self.timeframe_min) - trades += self.handle_left_open(open_trades, data=data) + self.handle_left_open(LocalTrade.bt_trades_open_pp, data=data) self.wallets.update() - results = trade_list_to_dataframe(trades) + results = trade_list_to_dataframe(LocalTrade.trades) return { 'results': results, 'config': self.strategy.config, @@ -1248,8 +1245,6 @@ class Backtesting: start_date=min_date, end_date=max_date, max_open_trades=max_open_trades, - position_stacking=self.config.get('position_stacking', False), - enable_protections=self.config.get('enable_protections', False), ) backtest_end_time = datetime.now(timezone.utc) results.update({ diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index d93bbbfc1..b459d59f2 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -122,7 +122,6 @@ class Hyperopt: else: logger.debug('Ignoring max_open_trades (--disable-max-market-positions was used) ...') self.max_open_trades = 0 - self.position_stacking = self.config.get('position_stacking', False) if HyperoptTools.has_space(self.config, 'sell'): # Make sure use_exit_signal is enabled @@ -258,6 +257,7 @@ class Hyperopt: logger.debug("Hyperopt has 'protection' space") # Enable Protections if protection space is selected. self.config['enable_protections'] = True + self.backtesting.enable_protections = True self.protection_space = self.custom_hyperopt.protection_space() if HyperoptTools.has_space(self.config, 'buy'): @@ -339,8 +339,6 @@ class Hyperopt: start_date=self.min_date, end_date=self.max_date, max_open_trades=self.max_open_trades, - position_stacking=self.position_stacking, - enable_protections=self.config.get('enable_protections', False), ) backtest_end_time = datetime.now(timezone.utc) bt_results.update({ diff --git a/freqtrade/optimize/hyperopt_tools.py b/freqtrade/optimize/hyperopt_tools.py index 65bdc4db5..393c055c4 100755 --- a/freqtrade/optimize/hyperopt_tools.py +++ b/freqtrade/optimize/hyperopt_tools.py @@ -12,7 +12,7 @@ import tabulate from colorama import Fore, Style from pandas import isna, json_normalize -from freqtrade.constants import FTHYPT_FILEVERSION, USERPATH_STRATEGIES, Config +from freqtrade.constants import FTHYPT_FILEVERSION, Config from freqtrade.enums import HyperoptState from freqtrade.exceptions import OperationalException from freqtrade.misc import deep_merge_dicts, round_coin_value, round_dict, safe_value_fallback2 @@ -50,9 +50,8 @@ class HyperoptTools(): Get Strategy-location (filename) from strategy_name """ from freqtrade.resolvers.strategy_resolver import StrategyResolver - directory = Path(config.get('strategy_path', config['user_data_dir'] / USERPATH_STRATEGIES)) strategy_objs = StrategyResolver.search_all_objects( - directory, False, config.get('recursive_strategy_search', False)) + config, False, config.get('recursive_strategy_search', False)) strategies = [s for s in strategy_objs if s['name'] == strategy_name] if strategies: strategy = strategies[0] diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index 8dafe2e41..c406f866b 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -408,10 +408,10 @@ def generate_strategy_stats(pairlist: List[str], exit_reason_stats = generate_exit_reason_stats(max_open_trades=max_open_trades, results=results) - left_open_results = generate_pair_metrics(pairlist, stake_currency=stake_currency, - starting_balance=start_balance, - results=results.loc[results['is_open']], - skip_nan=True) + left_open_results = generate_pair_metrics( + pairlist, stake_currency=stake_currency, starting_balance=start_balance, + results=results.loc[results['exit_reason'] == 'force_exit'], skip_nan=True) + daily_stats = generate_daily_stats(results) trade_stats = generate_trading_stats(results) best_pair = max([pair for pair in pair_results if pair['key'] != 'TOTAL'], diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 6e421f33e..70c460e89 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -2,6 +2,7 @@ This module contains the class to persist trades into SQLite """ import logging +from collections import defaultdict from datetime import datetime, timedelta, timezone from math import isclose from typing import Any, Dict, List, Optional @@ -255,6 +256,9 @@ class LocalTrade(): # Trades container for backtesting trades: List['LocalTrade'] = [] trades_open: List['LocalTrade'] = [] + # Copy of trades_open - but indexed by pair + bt_trades_open_pp: Dict[str, List['LocalTrade']] = defaultdict(list) + bt_open_open_trade_count: int = 0 total_profit: float = 0 realized_profit: float = 0 @@ -538,6 +542,8 @@ class LocalTrade(): """ LocalTrade.trades = [] LocalTrade.trades_open = [] + LocalTrade.bt_trades_open_pp = defaultdict(list) + LocalTrade.bt_open_open_trade_count = 0 LocalTrade.total_profit = 0 def adjust_min_max_rates(self, current_price: float, current_price_low: float) -> None: @@ -1067,6 +1073,8 @@ class LocalTrade(): @staticmethod def close_bt_trade(trade): LocalTrade.trades_open.remove(trade) + LocalTrade.bt_trades_open_pp[trade.pair].remove(trade) + LocalTrade.bt_open_open_trade_count -= 1 LocalTrade.trades.append(trade) LocalTrade.total_profit += trade.close_profit_abs @@ -1074,9 +1082,17 @@ class LocalTrade(): def add_bt_trade(trade): if trade.is_open: LocalTrade.trades_open.append(trade) + LocalTrade.bt_trades_open_pp[trade.pair].append(trade) + LocalTrade.bt_open_open_trade_count += 1 else: LocalTrade.trades.append(trade) + @staticmethod + def remove_bt_trade(trade): + LocalTrade.trades_open.remove(trade) + LocalTrade.bt_trades_open_pp[trade.pair].remove(trade) + LocalTrade.bt_open_open_trade_count -= 1 + @staticmethod def get_open_trades() -> List[Any]: """ @@ -1092,7 +1108,7 @@ class LocalTrade(): if Trade.use_db: return Trade.query.filter(Trade.is_open.is_(True)).count() else: - return len(LocalTrade.trades_open) + return LocalTrade.bt_open_open_trade_count @staticmethod def stoploss_reinitialization(desired_stoploss): @@ -1504,3 +1520,87 @@ class Trade(_DECL_BASE, LocalTrade): Order.status == 'closed' ).scalar() return trading_volume + + @staticmethod + def from_json(json_str: str) -> 'Trade': + """ + Create a Trade instance from a json string. + + Used for debugging purposes - please keep. + :param json_str: json string to parse + :return: Trade instance + """ + import rapidjson + data = rapidjson.loads(json_str) + trade = Trade( + id=data["trade_id"], + pair=data["pair"], + base_currency=data["base_currency"], + stake_currency=data["quote_currency"], + is_open=data["is_open"], + exchange=data["exchange"], + amount=data["amount"], + amount_requested=data["amount_requested"], + stake_amount=data["stake_amount"], + strategy=data["strategy"], + enter_tag=data["enter_tag"], + timeframe=data["timeframe"], + fee_open=data["fee_open"], + fee_open_cost=data["fee_open_cost"], + fee_open_currency=data["fee_open_currency"], + fee_close=data["fee_close"], + fee_close_cost=data["fee_close_cost"], + fee_close_currency=data["fee_close_currency"], + open_date=datetime.fromtimestamp(data["open_timestamp"] // 1000, tz=timezone.utc), + open_rate=data["open_rate"], + open_rate_requested=data["open_rate_requested"], + open_trade_value=data["open_trade_value"], + close_date=(datetime.fromtimestamp(data["close_timestamp"] // 1000, tz=timezone.utc) + if data["close_timestamp"] else None), + realized_profit=data["realized_profit"], + close_rate=data["close_rate"], + close_rate_requested=data["close_rate_requested"], + close_profit=data["close_profit"], + close_profit_abs=data["close_profit_abs"], + exit_reason=data["exit_reason"], + exit_order_status=data["exit_order_status"], + stop_loss=data["stop_loss_abs"], + stop_loss_pct=data["stop_loss_ratio"], + stoploss_order_id=data["stoploss_order_id"], + stoploss_last_update=(datetime.fromtimestamp(data["stoploss_last_update"] // 1000, + tz=timezone.utc) if data["stoploss_last_update"] else None), + initial_stop_loss=data["initial_stop_loss_abs"], + initial_stop_loss_pct=data["initial_stop_loss_ratio"], + min_rate=data["min_rate"], + max_rate=data["max_rate"], + leverage=data["leverage"], + interest_rate=data["interest_rate"], + liquidation_price=data["liquidation_price"], + is_short=data["is_short"], + trading_mode=data["trading_mode"], + funding_fees=data["funding_fees"], + open_order_id=data["open_order_id"], + ) + for order in data["orders"]: + + order_obj = Order( + amount=order["amount"], + ft_order_side=order["ft_order_side"], + ft_pair=order["pair"], + ft_is_open=order["is_open"], + order_id=order["order_id"], + status=order["status"], + average=order["average"], + cost=order["cost"], + filled=order["filled"], + order_date=datetime.strptime(order["order_date"], DATETIME_PRINT_FORMAT), + order_filled_date=(datetime.fromtimestamp( + order["order_filled_timestamp"] // 1000, tz=timezone.utc) + if order["order_filled_timestamp"] else None), + order_type=order["order_type"], + price=order["price"], + remaining=order["remaining"], + ) + trade.orders.append(order_obj) + + return trade diff --git a/freqtrade/plugins/pairlist/AgeFilter.py b/freqtrade/plugins/pairlist/AgeFilter.py index 70638936a..f9c02e250 100644 --- a/freqtrade/plugins/pairlist/AgeFilter.py +++ b/freqtrade/plugins/pairlist/AgeFilter.py @@ -10,6 +10,7 @@ from pandas import DataFrame from freqtrade.constants import Config, ListPairsWithTimeframes from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Tickers from freqtrade.misc import plural from freqtrade.plugins.pairlist.IPairList import IPairList from freqtrade.util import PeriodicCache @@ -67,10 +68,10 @@ class AgeFilter(IPairList): f"{self._max_days_listed} {plural(self._max_days_listed, 'day')}" ) if self._max_days_listed else '') - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> List[str]: """ :param pairlist: pairlist to filter or sort - :param tickers: Tickers (from exchange.get_tickers()). May be cached. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new allowlist """ needed_pairs: ListPairsWithTimeframes = [ diff --git a/freqtrade/plugins/pairlist/IPairList.py b/freqtrade/plugins/pairlist/IPairList.py index c02ba5ef5..660d6228c 100644 --- a/freqtrade/plugins/pairlist/IPairList.py +++ b/freqtrade/plugins/pairlist/IPairList.py @@ -4,11 +4,12 @@ PairList Handler base class import logging from abc import ABC, abstractmethod, abstractproperty from copy import deepcopy -from typing import Any, Dict, List +from typing import Any, Dict, List, Optional from freqtrade.constants import Config from freqtrade.exceptions import OperationalException from freqtrade.exchange import Exchange, market_is_active +from freqtrade.exchange.types import Ticker, Tickers from freqtrade.mixins import LoggingMixin @@ -61,7 +62,7 @@ class IPairList(LoggingMixin, ABC): -> Please overwrite in subclasses """ - def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool: + def _validate_pair(self, pair: str, ticker: Optional[Ticker]) -> bool: """ Check one pair against Pairlist Handler's specific conditions. @@ -69,12 +70,12 @@ class IPairList(LoggingMixin, ABC): filter_pairlist() method. :param pair: Pair that's currently validated - :param ticker: ticker dict as returned from ccxt.fetch_tickers() + :param ticker: ticker dict as returned from ccxt.fetch_ticker :return: True if the pair can stay, false if it should be removed """ raise NotImplementedError() - def gen_pairlist(self, tickers: Dict) -> List[str]: + def gen_pairlist(self, tickers: Tickers) -> List[str]: """ Generate the pairlist. @@ -85,13 +86,13 @@ class IPairList(LoggingMixin, ABC): it will raise the exception if a Pairlist Handler is used at the first position in the chain. - :param tickers: Tickers (from exchange.get_tickers()). May be cached. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: List of pairs """ raise OperationalException("This Pairlist Handler should not be used " "at the first position in the list of Pairlist Handlers.") - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> List[str]: """ Filters and sorts pairlist and returns the whitelist again. @@ -103,14 +104,14 @@ class IPairList(LoggingMixin, ABC): own filtration. :param pairlist: pairlist to filter or sort - :param tickers: Tickers (from exchange.get_tickers()). May be cached. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new whitelist """ if self._enabled: # Copy list since we're modifying this list for p in deepcopy(pairlist): # Filter out assets - if not self._validate_pair(p, tickers[p] if p in tickers else {}): + if not self._validate_pair(p, tickers[p] if p in tickers else None): pairlist.remove(p) return pairlist diff --git a/freqtrade/plugins/pairlist/OffsetFilter.py b/freqtrade/plugins/pairlist/OffsetFilter.py index 149befdeb..8f21cdd85 100644 --- a/freqtrade/plugins/pairlist/OffsetFilter.py +++ b/freqtrade/plugins/pairlist/OffsetFilter.py @@ -6,6 +6,7 @@ from typing import Any, Dict, List from freqtrade.constants import Config from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Tickers from freqtrade.plugins.pairlist.IPairList import IPairList @@ -42,12 +43,12 @@ class OffsetFilter(IPairList): return f"{self.name} - Taking {self._number_pairs} Pairs, starting from {self._offset}." return f"{self.name} - Offsetting pairs by {self._offset}." - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> List[str]: """ Filters and sorts pairlist and returns the whitelist 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. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new whitelist """ if self._offset > len(pairlist): diff --git a/freqtrade/plugins/pairlist/PerformanceFilter.py b/freqtrade/plugins/pairlist/PerformanceFilter.py index c29b4f337..e7fcac1e4 100644 --- a/freqtrade/plugins/pairlist/PerformanceFilter.py +++ b/freqtrade/plugins/pairlist/PerformanceFilter.py @@ -7,6 +7,7 @@ from typing import Any, Dict, List import pandas as pd from freqtrade.constants import Config +from freqtrade.exchange.types import Tickers from freqtrade.persistence import Trade from freqtrade.plugins.pairlist.IPairList import IPairList @@ -39,12 +40,12 @@ class PerformanceFilter(IPairList): """ return f"{self.name} - Sorting pairs by performance." - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> 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. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new allowlist """ # Get the trading performance for pairs from database diff --git a/freqtrade/plugins/pairlist/PrecisionFilter.py b/freqtrade/plugins/pairlist/PrecisionFilter.py index 8f1c9b839..478eaec20 100644 --- a/freqtrade/plugins/pairlist/PrecisionFilter.py +++ b/freqtrade/plugins/pairlist/PrecisionFilter.py @@ -2,10 +2,11 @@ Precision pair list filter """ import logging -from typing import Any, Dict +from typing import Any, Dict, Optional from freqtrade.constants import Config from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Ticker from freqtrade.plugins.pairlist.IPairList import IPairList @@ -44,15 +45,15 @@ class PrecisionFilter(IPairList): """ return f"{self.name} - Filtering untradable pairs." - def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool: + def _validate_pair(self, pair: str, ticker: Optional[Ticker]) -> bool: """ Check if pair has enough room to add a stoploss to avoid "unsellable" buys of very low value pairs. :param pair: Pair that's currently validated - :param ticker: ticker dict as returned from ccxt.fetch_tickers() + :param ticker: ticker dict as returned from ccxt.fetch_ticker :return: True if the pair can stay, false if it should be removed """ - if ticker.get('last', None) is None: + if not ticker or ticker.get('last', None) is None: self.log_once(f"Removed {pair} from whitelist, because " "ticker['last'] is empty (Usually no trade in the last 24h).", logger.info) diff --git a/freqtrade/plugins/pairlist/PriceFilter.py b/freqtrade/plugins/pairlist/PriceFilter.py index f2952001a..4d23de792 100644 --- a/freqtrade/plugins/pairlist/PriceFilter.py +++ b/freqtrade/plugins/pairlist/PriceFilter.py @@ -2,10 +2,11 @@ Price pair list filter """ import logging -from typing import Any, Dict +from typing import Any, Dict, Optional from freqtrade.constants import Config from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Ticker from freqtrade.plugins.pairlist.IPairList import IPairList @@ -64,14 +65,16 @@ class PriceFilter(IPairList): return f"{self.name} - No price filters configured." - def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool: + def _validate_pair(self, pair: str, ticker: Optional[Ticker]) -> bool: """ 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.fetch_tickers() + :param ticker: ticker dict as returned from ccxt.fetch_ticker :return: True if the pair can stay, false if it should be removed """ - if ticker.get('last', None) is None or ticker.get('last') == 0: + if ticker and 'last' in ticker and ticker['last'] is not None and ticker.get('last') != 0: + price: float = ticker['last'] + else: self.log_once(f"Removed {pair} from whitelist, because " "ticker['last'] is empty (Usually no trade in the last 24h).", logger.info) @@ -79,8 +82,8 @@ class PriceFilter(IPairList): # Perform low_price_ratio check. if self._low_price_ratio != 0: - compare = self._exchange.price_get_one_pip(pair, ticker['last']) - changeperc = compare / ticker['last'] + compare = self._exchange.price_get_one_pip(pair, price) + changeperc = compare / price if changeperc > self._low_price_ratio: self.log_once(f"Removed {pair} from whitelist, " f"because 1 unit is {changeperc:.3%}", logger.info) @@ -88,7 +91,6 @@ class PriceFilter(IPairList): # Perform low_amount check if self._max_value != 0: - price = ticker['last'] market = self._exchange.markets[pair] limits = market['limits'] if (limits['amount']['min'] is not None): @@ -113,14 +115,14 @@ class PriceFilter(IPairList): # Perform min_price check. if self._min_price != 0: - if ticker['last'] < self._min_price: + if price < self._min_price: self.log_once(f"Removed {pair} from whitelist, " f"because last price < {self._min_price:.8f}", logger.info) return False # Perform max_price check. if self._max_price != 0: - if ticker['last'] > self._max_price: + if price > self._max_price: self.log_once(f"Removed {pair} from whitelist, " f"because last price > {self._max_price:.8f}", logger.info) return False diff --git a/freqtrade/plugins/pairlist/ProducerPairList.py b/freqtrade/plugins/pairlist/ProducerPairList.py index 50b674e60..882d49b76 100644 --- a/freqtrade/plugins/pairlist/ProducerPairList.py +++ b/freqtrade/plugins/pairlist/ProducerPairList.py @@ -7,6 +7,7 @@ import logging from typing import Any, Dict, List, Optional from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Tickers from freqtrade.plugins.pairlist.IPairList import IPairList @@ -68,10 +69,10 @@ class ProducerPairList(IPairList): return pairs - def gen_pairlist(self, tickers: Dict) -> List[str]: + def gen_pairlist(self, tickers: Tickers) -> List[str]: """ Generate the pairlist - :param tickers: Tickers (from exchange.get_tickers()). May be cached. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: List of pairs """ pairs = self._filter_pairlist(None) @@ -79,12 +80,12 @@ class ProducerPairList(IPairList): pairs = self._whitelist_for_active_markets(self.verify_whitelist(pairs, logger.info)) return pairs - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> List[str]: """ Filters and sorts pairlist and returns the whitelist 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. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new whitelist """ return self._filter_pairlist(pairlist) diff --git a/freqtrade/plugins/pairlist/ShuffleFilter.py b/freqtrade/plugins/pairlist/ShuffleFilter.py index b6b5fc3c8..1bc114d4e 100644 --- a/freqtrade/plugins/pairlist/ShuffleFilter.py +++ b/freqtrade/plugins/pairlist/ShuffleFilter.py @@ -7,6 +7,7 @@ from typing import Any, Dict, List from freqtrade.constants import Config from freqtrade.enums import RunMode +from freqtrade.exchange.types import Tickers from freqtrade.plugins.pairlist.IPairList import IPairList @@ -47,12 +48,12 @@ class ShuffleFilter(IPairList): return (f"{self.name} - Shuffling pairs" + (f", seed = {self._seed}." if self._seed is not None else ".")) - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> List[str]: """ Filters and sorts pairlist and returns the whitelist 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. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new whitelist """ # Shuffle is done inplace diff --git a/freqtrade/plugins/pairlist/SpreadFilter.py b/freqtrade/plugins/pairlist/SpreadFilter.py index 1f20af305..207328d08 100644 --- a/freqtrade/plugins/pairlist/SpreadFilter.py +++ b/freqtrade/plugins/pairlist/SpreadFilter.py @@ -2,10 +2,10 @@ Spread pair list filter """ import logging -from typing import Any, Dict +from typing import Any, Dict, Optional from freqtrade.constants import Config -from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Ticker from freqtrade.plugins.pairlist.IPairList import IPairList @@ -22,12 +22,6 @@ class SpreadFilter(IPairList): self._max_spread_ratio = pairlistconfig.get('max_spread_ratio', 0.005) self._enabled = self._max_spread_ratio != 0 - if not self._exchange.exchange_has('fetchTickers'): - raise OperationalException( - 'Exchange does not support fetchTickers, therefore SpreadFilter cannot be used.' - 'Please edit your config and restart the bot.' - ) - @property def needstickers(self) -> bool: """ @@ -44,14 +38,14 @@ class SpreadFilter(IPairList): return (f"{self.name} - Filtering pairs with ask/bid diff above " f"{self._max_spread_ratio:.2%}.") - def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool: + def _validate_pair(self, pair: str, ticker: Optional[Ticker]) -> bool: """ Validate spread for the ticker :param pair: Pair that's currently validated - :param ticker: ticker dict as returned from ccxt.fetch_tickers() + :param ticker: ticker dict as returned from ccxt.fetch_ticker :return: True if the pair can stay, false if it should be removed """ - if 'bid' in ticker and 'ask' in ticker and ticker['ask'] and ticker['bid']: + if ticker and 'bid' in ticker and 'ask' in ticker and ticker['ask'] and ticker['bid']: spread = 1 - ticker['bid'] / ticker['ask'] if spread > self._max_spread_ratio: self.log_once(f"Removed {pair} from whitelist, because spread " diff --git a/freqtrade/plugins/pairlist/StaticPairList.py b/freqtrade/plugins/pairlist/StaticPairList.py index 83a0fa0c8..4b1961a53 100644 --- a/freqtrade/plugins/pairlist/StaticPairList.py +++ b/freqtrade/plugins/pairlist/StaticPairList.py @@ -8,6 +8,7 @@ from copy import deepcopy from typing import Any, Dict, List from freqtrade.constants import Config +from freqtrade.exchange.types import Tickers from freqtrade.plugins.pairlist.IPairList import IPairList @@ -39,10 +40,10 @@ class StaticPairList(IPairList): """ return f"{self.name}" - def gen_pairlist(self, tickers: Dict) -> List[str]: + def gen_pairlist(self, tickers: Tickers) -> List[str]: """ Generate the pairlist - :param tickers: Tickers (from exchange.get_tickers()). May be cached. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: List of pairs """ if self._allow_inactive: @@ -53,12 +54,12 @@ class StaticPairList(IPairList): return self._whitelist_for_active_markets( self.verify_whitelist(self._config['exchange']['pair_whitelist'], logger.info)) - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> List[str]: """ Filters and sorts pairlist and returns the whitelist 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. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new whitelist """ pairlist_ = deepcopy(pairlist) diff --git a/freqtrade/plugins/pairlist/VolatilityFilter.py b/freqtrade/plugins/pairlist/VolatilityFilter.py index c9af3a7b3..401a2e86c 100644 --- a/freqtrade/plugins/pairlist/VolatilityFilter.py +++ b/freqtrade/plugins/pairlist/VolatilityFilter.py @@ -13,6 +13,7 @@ from pandas import DataFrame from freqtrade.constants import Config, ListPairsWithTimeframes from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Tickers from freqtrade.misc import plural from freqtrade.plugins.pairlist.IPairList import IPairList @@ -62,11 +63,11 @@ class VolatilityFilter(IPairList): f"{self._min_volatility}-{self._max_volatility} " f" the last {self._days} {plural(self._days, 'day')}.") - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> List[str]: """ Validate trading range :param pairlist: pairlist to filter or sort - :param tickers: Tickers (from exchange.get_tickers()). May be cached. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new allowlist """ needed_pairs: ListPairsWithTimeframes = [ diff --git a/freqtrade/plugins/pairlist/VolumePairList.py b/freqtrade/plugins/pairlist/VolumePairList.py index b290f76aa..ad27a93d8 100644 --- a/freqtrade/plugins/pairlist/VolumePairList.py +++ b/freqtrade/plugins/pairlist/VolumePairList.py @@ -5,13 +5,14 @@ Provides dynamic pair list based on trade volumes """ import logging from datetime import datetime, timedelta, timezone -from typing import Any, Dict, List +from typing import Any, Dict, List, Literal from cachetools import TTLCache from freqtrade.constants import Config, ListPairsWithTimeframes from freqtrade.exceptions import OperationalException from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date +from freqtrade.exchange.types import Tickers from freqtrade.misc import format_ms_time from freqtrade.plugins.pairlist.IPairList import IPairList @@ -36,7 +37,7 @@ class VolumePairList(IPairList): self._stake_currency = config['stake_currency'] self._number_pairs = self._pairlistconfig['number_assets'] - self._sort_key = self._pairlistconfig.get('sort_key', 'quoteVolume') + self._sort_key: Literal['quoteVolume'] = self._pairlistconfig.get('sort_key', 'quoteVolume') self._min_value = self._pairlistconfig.get('min_value', 0) self._refresh_period = self._pairlistconfig.get('refresh_period', 1800) self._pair_cache: TTLCache = TTLCache(maxsize=1, ttl=self._refresh_period) @@ -110,10 +111,10 @@ class VolumePairList(IPairList): """ return f"{self.name} - top {self._pairlistconfig['number_assets']} volume pairs." - def gen_pairlist(self, tickers: Dict) -> List[str]: + def gen_pairlist(self, tickers: Tickers) -> List[str]: """ Generate the pairlist - :param tickers: Tickers (from exchange.get_tickers()). May be cached. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: List of pairs """ # Generate dynamic whitelist @@ -150,7 +151,7 @@ class VolumePairList(IPairList): Filters and sorts pairlist and returns the whitelist 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. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new whitelist """ if self._use_range: diff --git a/freqtrade/plugins/pairlist/pairlist_helpers.py b/freqtrade/plugins/pairlist/pairlist_helpers.py index 9ef3e4614..93d4fc308 100644 --- a/freqtrade/plugins/pairlist/pairlist_helpers.py +++ b/freqtrade/plugins/pairlist/pairlist_helpers.py @@ -12,7 +12,7 @@ def expand_pairlist(wildcardpl: List[str], available_pairs: List[str], :param wildcardpl: List of Pairlists, which may contain regex :param available_pairs: List of all available pairs (`exchange.get_markets().keys()`) :param keep_invalid: If sets to True, drops invalid pairs silently while expanding regexes - :return expanded pairlist, with Regexes from wildcardpl applied to match all available pairs. + :return: expanded pairlist, with Regexes from wildcardpl applied to match all available pairs. :raises: ValueError if a wildcard is invalid (like '*/BTC' - which should be `.*/BTC`) """ result = [] diff --git a/freqtrade/plugins/pairlist/rangestabilityfilter.py b/freqtrade/plugins/pairlist/rangestabilityfilter.py index 1bc7ad48f..546b026cb 100644 --- a/freqtrade/plugins/pairlist/rangestabilityfilter.py +++ b/freqtrade/plugins/pairlist/rangestabilityfilter.py @@ -11,6 +11,7 @@ from pandas import DataFrame from freqtrade.constants import Config, ListPairsWithTimeframes from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Tickers from freqtrade.misc import plural from freqtrade.plugins.pairlist.IPairList import IPairList @@ -60,11 +61,11 @@ class RangeStabilityFilter(IPairList): f"{self._min_rate_of_change}{max_rate_desc} over the " f"last {plural(self._days, 'day')}.") - def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: + def filter_pairlist(self, pairlist: List[str], tickers: Tickers) -> List[str]: """ Validate trading range :param pairlist: pairlist to filter or sort - :param tickers: Tickers (from exchange.get_tickers()). May be cached. + :param tickers: Tickers (from exchange.get_tickers). May be cached. :return: new allowlist """ needed_pairs: ListPairsWithTimeframes = [ diff --git a/freqtrade/plugins/pairlistmanager.py b/freqtrade/plugins/pairlistmanager.py index 5ed319e93..20a264fd8 100644 --- a/freqtrade/plugins/pairlistmanager.py +++ b/freqtrade/plugins/pairlistmanager.py @@ -11,6 +11,7 @@ from freqtrade.constants import Config, ListPairsWithTimeframes from freqtrade.data.dataprovider import DataProvider from freqtrade.enums import CandleType from freqtrade.exceptions import OperationalException +from freqtrade.exchange.types import Tickers from freqtrade.mixins import LoggingMixin from freqtrade.plugins.pairlist.IPairList import IPairList from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist @@ -45,6 +46,15 @@ class PairListManager(LoggingMixin): if not self._pairlist_handlers: raise OperationalException("No Pairlist Handlers defined") + if self._tickers_needed and not self._exchange.exchange_has('fetchTickers'): + invalid = ". ".join([p.name for p in self._pairlist_handlers if p.needstickers]) + + raise OperationalException( + "Exchange does not support fetchTickers, therefore the following pairlists " + "cannot be used. Please edit your config and restart the bot.\n" + f"{invalid}." + ) + refresh_period = config.get('pairlist_refresh_period', 3600) LoggingMixin.__init__(self, logger, refresh_period) @@ -76,7 +86,7 @@ class PairListManager(LoggingMixin): return [{p.name: p.short_desc()} for p in self._pairlist_handlers] @cached(TTLCache(maxsize=1, ttl=1800)) - def _get_cached_tickers(self): + def _get_cached_tickers(self) -> Tickers: return self._exchange.get_tickers() def refresh_pairlist(self) -> None: diff --git a/freqtrade/resolvers/freqaimodel_resolver.py b/freqtrade/resolvers/freqaimodel_resolver.py index aa5228ca1..48c3facac 100644 --- a/freqtrade/resolvers/freqaimodel_resolver.py +++ b/freqtrade/resolvers/freqaimodel_resolver.py @@ -26,6 +26,7 @@ class FreqaiModelResolver(IResolver): initial_search_path = ( Path(__file__).parent.parent.joinpath("freqai/prediction_models").resolve() ) + extra_path = "freqaimodel_path" @staticmethod def load_freqaimodel(config: Config) -> IFreqaiModel: @@ -50,7 +51,6 @@ class FreqaiModelResolver(IResolver): freqaimodel_name, config, kwargs={"config": config}, - extra_dir=config.get("freqaimodel_path"), ) return freqaimodel diff --git a/freqtrade/resolvers/iresolver.py b/freqtrade/resolvers/iresolver.py index 9682e1c2b..0b484394a 100644 --- a/freqtrade/resolvers/iresolver.py +++ b/freqtrade/resolvers/iresolver.py @@ -42,6 +42,8 @@ class IResolver: object_type_str: str user_subdir: Optional[str] = None initial_search_path: Optional[Path] + # Optional config setting containing a path (strategy_path, freqaimodel_path) + extra_path: Optional[str] = None @classmethod def build_search_paths(cls, config: Config, user_subdir: Optional[str] = None, @@ -58,6 +60,9 @@ class IResolver: for dir in extra_dirs: abs_paths.insert(0, Path(dir).resolve()) + if cls.extra_path and (extra := config.get(cls.extra_path)): + abs_paths.insert(0, Path(extra).resolve()) + return abs_paths @classmethod @@ -183,9 +188,35 @@ class IResolver: ) @classmethod - def search_all_objects(cls, directory: Path, enum_failed: bool, + def search_all_objects(cls, config: Config, enum_failed: bool, recursive: bool = False) -> List[Dict[str, Any]]: """ + Searches for valid objects + :param config: Config object + :param enum_failed: If True, will return None for modules which fail. + Otherwise, failing modules are skipped. + :param recursive: Recursively walk directory tree searching for strategies + :return: List of dicts containing 'name', 'class' and 'location' entries + """ + result = [] + + abs_paths = cls.build_search_paths(config, user_subdir=cls.user_subdir) + for path in abs_paths: + result.extend(cls._search_all_objects(path, enum_failed, recursive)) + return result + + @classmethod + def _build_rel_location(cls, directory: Path, entry: Path) -> str: + + builtin = cls.initial_search_path == directory + return f"/{entry.relative_to(directory)}" if builtin else str( + entry.relative_to(directory)) + + @classmethod + def _search_all_objects( + cls, directory: Path, enum_failed: bool, recursive: bool = False, + basedir: Optional[Path] = None) -> List[Dict[str, Any]]: + """ Searches a directory for valid objects :param directory: Path to search :param enum_failed: If True, will return None for modules which fail. @@ -204,7 +235,8 @@ class IResolver: and not entry.name.startswith('__') and not entry.name.startswith('.') ): - objects.extend(cls.search_all_objects(entry, enum_failed, recursive=recursive)) + objects.extend(cls._search_all_objects( + entry, enum_failed, recursive, basedir or directory)) # Only consider python files if entry.suffix != '.py': logger.debug('Ignoring %s', entry) @@ -217,5 +249,6 @@ class IResolver: {'name': obj[0].__name__ if obj is not None else '', 'class': obj[0] if obj is not None else None, 'location': entry, + 'location_rel': cls._build_rel_location(basedir or directory, entry), }) return objects diff --git a/freqtrade/resolvers/strategy_resolver.py b/freqtrade/resolvers/strategy_resolver.py index c574246ac..67df49dcb 100644 --- a/freqtrade/resolvers/strategy_resolver.py +++ b/freqtrade/resolvers/strategy_resolver.py @@ -30,6 +30,7 @@ class StrategyResolver(IResolver): object_type_str = "Strategy" user_subdir = USERPATH_STRATEGIES initial_search_path = None + extra_path = "strategy_path" @staticmethod def load_strategy(config: Config = None) -> IStrategy: diff --git a/freqtrade/rpc/api_server/api_backtest.py b/freqtrade/rpc/api_server/api_backtest.py index c21828fd4..b17636a7d 100644 --- a/freqtrade/rpc/api_server/api_backtest.py +++ b/freqtrade/rpc/api_server/api_backtest.py @@ -89,6 +89,7 @@ async def api_start_backtest(bt_settings: BacktestRequest, background_tasks: Bac lastconfig['enable_protections'] = btconfig.get('enable_protections') lastconfig['dry_run_wallet'] = btconfig.get('dry_run_wallet') + ApiServer._bt.enable_protections = btconfig.get('enable_protections', False) ApiServer._bt.strategylist = [strat] ApiServer._bt.results = {} ApiServer._bt.load_prior_backtest() diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index 135892dc6..c0c9b8f57 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -1,13 +1,11 @@ import logging from copy import deepcopy -from pathlib import Path from typing import List, Optional from fastapi import APIRouter, Depends, Query from fastapi.exceptions import HTTPException from freqtrade import __version__ -from freqtrade.constants import USERPATH_STRATEGIES from freqtrade.data.history import get_datahandler from freqtrade.enums import CandleType, TradingMode from freqtrade.exceptions import OperationalException @@ -253,11 +251,9 @@ def plot_config(rpc: RPC = Depends(get_rpc)): @router.get('/strategies', response_model=StrategyListResponse, tags=['strategy']) def list_strategies(config=Depends(get_config)): - directory = Path(config.get( - 'strategy_path', config['user_data_dir'] / USERPATH_STRATEGIES)) from freqtrade.resolvers.strategy_resolver import StrategyResolver strategies = StrategyResolver.search_all_objects( - directory, False, config.get('recursive_strategy_search', False)) + config, False, config.get('recursive_strategy_search', False)) strategies = sorted(strategies, key=lambda x: x['name']) return {'strategies': [x['name'] for x in strategies]} diff --git a/freqtrade/rpc/api_server/api_ws.py b/freqtrade/rpc/api_server/api_ws.py index f55b2dbd3..b230cbe2b 100644 --- a/freqtrade/rpc/api_server/api_ws.py +++ b/freqtrade/rpc/api_server/api_ws.py @@ -4,11 +4,13 @@ from typing import Any, Dict from fastapi import APIRouter, Depends, WebSocketDisconnect from fastapi.websockets import WebSocket, WebSocketState from pydantic import ValidationError +from websockets.exceptions import WebSocketException from freqtrade.enums import RPCMessageType, RPCRequestType from freqtrade.rpc.api_server.api_auth import validate_ws_token from freqtrade.rpc.api_server.deps import get_channel_manager, get_rpc from freqtrade.rpc.api_server.ws import WebSocketChannel +from freqtrade.rpc.api_server.ws.channel import ChannelManager from freqtrade.rpc.api_server.ws_schemas import (WSAnalyzedDFMessage, WSMessageSchema, WSRequestSchema, WSWhitelistMessage) from freqtrade.rpc.rpc import RPC @@ -35,7 +37,8 @@ async def is_websocket_alive(ws: WebSocket) -> bool: async def _process_consumer_request( request: Dict[str, Any], channel: WebSocketChannel, - rpc: RPC + rpc: RPC, + channel_manager: ChannelManager ): """ Validate and handle a request from a websocket consumer @@ -72,7 +75,7 @@ async def _process_consumer_request( # Format response response = WSWhitelistMessage(data=whitelist) # Send it back - await channel.send(response.dict(exclude_none=True)) + await channel_manager.send_direct(channel, response.dict(exclude_none=True)) elif type == RPCRequestType.ANALYZED_DF: limit = None @@ -87,7 +90,7 @@ async def _process_consumer_request( # For every dataframe, send as a separate message for _, message in analyzed_df.items(): response = WSAnalyzedDFMessage(data=message) - await channel.send(response.dict(exclude_none=True)) + await channel_manager.send_direct(channel, response.dict(exclude_none=True)) @router.websocket("/message/ws") @@ -102,7 +105,6 @@ async def message_endpoint( """ try: channel = await channel_manager.on_connect(ws) - if await is_websocket_alive(ws): logger.info(f"Consumer connected - {channel}") @@ -113,28 +115,33 @@ async def message_endpoint( request = await channel.recv() # Process the request here - await _process_consumer_request(request, channel, rpc) + await _process_consumer_request(request, channel, rpc, channel_manager) - except WebSocketDisconnect: + except (WebSocketDisconnect, WebSocketException): # Handle client disconnects logger.info(f"Consumer disconnected - {channel}") - await channel_manager.on_disconnect(ws) - except Exception as e: - logger.info(f"Consumer connection failed - {channel}") - logger.exception(e) + except RuntimeError: # Handle cases like - # RuntimeError('Cannot call "send" once a closed message has been sent') + pass + except Exception as e: + logger.info(f"Consumer connection failed - {channel}: {e}") + logger.debug(e, exc_info=e) + finally: await channel_manager.on_disconnect(ws) else: + if channel: + await channel_manager.on_disconnect(ws) await ws.close() except RuntimeError: # WebSocket was closed - await channel_manager.on_disconnect(ws) - + # Do nothing + pass except Exception as e: logger.error(f"Failed to serve - {ws.client}") # Log tracebacks to keep track of what errors are happening logger.exception(e) + finally: await channel_manager.on_disconnect(ws) diff --git a/freqtrade/rpc/api_server/webserver.py b/freqtrade/rpc/api_server/webserver.py index 53af91477..1d0192a89 100644 --- a/freqtrade/rpc/api_server/webserver.py +++ b/freqtrade/rpc/api_server/webserver.py @@ -16,6 +16,7 @@ from freqtrade.constants import Config from freqtrade.exceptions import OperationalException from freqtrade.rpc.api_server.uvicorn_threaded import UvicornServer from freqtrade.rpc.api_server.ws import ChannelManager +from freqtrade.rpc.api_server.ws_schemas import WSMessageSchemaType from freqtrade.rpc.rpc import RPC, RPCException, RPCHandler @@ -127,7 +128,7 @@ class ApiServer(RPCHandler): cls._has_rpc = False cls._rpc = None - def send_msg(self, msg: Dict[str, str]) -> None: + def send_msg(self, msg: Dict[str, Any]) -> None: if self._ws_queue: sync_q = self._ws_queue.sync_q sync_q.put(msg) @@ -194,14 +195,10 @@ class ApiServer(RPCHandler): while True: logger.debug("Getting queue messages...") # Get data from queue - message = await async_queue.get() + message: WSMessageSchemaType = await async_queue.get() logger.debug(f"Found message of type: {message.get('type')}") # Broadcast it await self._ws_channel_manager.broadcast(message) - # Limit messages per sec. - # Could cause problems with queue size if too low, and - # problems with network traffik if too high. - await asyncio.sleep(0.001) except asyncio.CancelledError: pass @@ -245,6 +242,7 @@ class ApiServer(RPCHandler): use_colors=False, log_config=None, access_log=True if verbosity != 'error' else False, + ws_ping_interval=None # We do this explicitly ourselves ) try: self._server = UvicornServer(uvconfig) diff --git a/freqtrade/rpc/api_server/ws/channel.py b/freqtrade/rpc/api_server/ws/channel.py index 69a32e266..34f03f0c4 100644 --- a/freqtrade/rpc/api_server/ws/channel.py +++ b/freqtrade/rpc/api_server/ws/channel.py @@ -1,6 +1,7 @@ +import asyncio import logging from threading import RLock -from typing import List, Optional, Type +from typing import Any, Dict, List, Optional, Type, Union from uuid import uuid4 from fastapi import WebSocket as FastAPIWebSocket @@ -9,6 +10,7 @@ from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy from freqtrade.rpc.api_server.ws.serializer import (HybridJSONWebSocketSerializer, WebSocketSerializer) from freqtrade.rpc.api_server.ws.types import WebSocketType +from freqtrade.rpc.api_server.ws_schemas import WSMessageSchemaType logger = logging.getLogger(__name__) @@ -23,6 +25,8 @@ class WebSocketChannel: self, websocket: WebSocketType, channel_id: Optional[str] = None, + drain_timeout: int = 3, + throttle: float = 0.01, serializer_cls: Type[WebSocketSerializer] = HybridJSONWebSocketSerializer ): @@ -33,7 +37,13 @@ class WebSocketChannel: # The Serializing class for the WebSocket object self._serializer_cls = serializer_cls + self.drain_timeout = drain_timeout + self.throttle = throttle + self._subscriptions: List[str] = [] + # 32 is the size of the receiving queue in websockets package + self.queue: asyncio.Queue[Dict[str, Any]] = asyncio.Queue(maxsize=32) + self._relay_task = asyncio.create_task(self.relay()) # Internal event to signify a closed websocket self._closed = False @@ -44,16 +54,34 @@ class WebSocketChannel: def __repr__(self): return f"WebSocketChannel({self.channel_id}, {self.remote_addr})" + @property + def raw_websocket(self): + return self._websocket.raw_websocket + @property def remote_addr(self): return self._websocket.remote_addr - async def send(self, data): + async def _send(self, data): """ Send data on the wrapped websocket """ await self._wrapped_ws.send(data) + async def send(self, data) -> bool: + """ + Add the data to the queue to be sent. + :returns: True if data added to queue, False otherwise + """ + try: + await asyncio.wait_for( + self.queue.put(data), + timeout=self.drain_timeout + ) + return True + except asyncio.TimeoutError: + return False + async def recv(self): """ Receive data on the wrapped websocket @@ -72,6 +100,7 @@ class WebSocketChannel: """ self._closed = True + self._relay_task.cancel() def is_closed(self) -> bool: """ @@ -95,6 +124,26 @@ class WebSocketChannel: """ return message_type in self._subscriptions + async def relay(self): + """ + Relay messages from the channel's queue and send them out. This is started + as a task. + """ + while True: + message = await self.queue.get() + try: + await self._send(message) + self.queue.task_done() + + # Limit messages per sec. + # Could cause problems with queue size if too low, and + # problems with network traffik if too high. + # 0.01 = 100/s + await asyncio.sleep(self.throttle) + except RuntimeError: + # The connection was closed, just exit the task + return + class ChannelManager: def __init__(self): @@ -130,6 +179,7 @@ class ChannelManager: with self._lock: channel = self.channels.get(websocket) if channel: + logger.info(f"Disconnecting channel {channel}") if not channel.is_closed(): await channel.close() @@ -140,36 +190,30 @@ class ChannelManager: Disconnect all Channels """ with self._lock: - for websocket, channel in self.channels.copy().items(): - if not channel.is_closed(): - await channel.close() + for websocket in self.channels.copy().keys(): + await self.on_disconnect(websocket) - self.channels = dict() - - async def broadcast(self, data): + async def broadcast(self, message: WSMessageSchemaType): """ - Broadcast data on all Channels + Broadcast a message on all Channels - :param data: The data to send + :param message: The message to send """ with self._lock: - message_type = data.get('type') - for websocket, channel in self.channels.copy().items(): - try: - if channel.subscribed_to(message_type): - await channel.send(data) - except RuntimeError: - # Handle cannot send after close cases - await self.on_disconnect(websocket) + for channel in self.channels.copy().values(): + if channel.subscribed_to(message.get('type')): + await self.send_direct(channel, message) - async def send_direct(self, channel, data): + async def send_direct( + self, channel: WebSocketChannel, message: Union[WSMessageSchemaType, Dict[str, Any]]): """ - Send data directly through direct_channel only + Send a message directly through direct_channel only - :param direct_channel: The WebSocketChannel object to send data through - :param data: The data to send + :param direct_channel: The WebSocketChannel object to send the message through + :param message: The message to send """ - await channel.send(data) + if not await channel.send(message): + await self.on_disconnect(channel.raw_websocket) def has_channels(self): """ diff --git a/freqtrade/rpc/api_server/ws/proxy.py b/freqtrade/rpc/api_server/ws/proxy.py index 2e5a59f05..ae123dd2d 100644 --- a/freqtrade/rpc/api_server/ws/proxy.py +++ b/freqtrade/rpc/api_server/ws/proxy.py @@ -15,6 +15,10 @@ class WebSocketProxy: def __init__(self, websocket: WebSocketType): self._websocket: Union[FastAPIWebSocket, WebSocket] = websocket + @property + def raw_websocket(self): + return self._websocket + @property def remote_addr(self) -> Tuple[Any, ...]: if isinstance(self._websocket, WebSocket): diff --git a/freqtrade/rpc/api_server/ws_schemas.py b/freqtrade/rpc/api_server/ws_schemas.py index 255226d84..877232213 100644 --- a/freqtrade/rpc/api_server/ws_schemas.py +++ b/freqtrade/rpc/api_server/ws_schemas.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, Optional, TypedDict from pandas import DataFrame from pydantic import BaseModel @@ -18,6 +18,12 @@ class WSRequestSchema(BaseArbitraryModel): data: Optional[Any] = None +class WSMessageSchemaType(TypedDict): + # Type for typing to avoid doing pydantic typechecks. + type: RPCMessageType + data: Optional[Dict[str, Any]] + + class WSMessageSchema(BaseArbitraryModel): type: RPCMessageType data: Optional[Any] = None diff --git a/freqtrade/rpc/discord.py b/freqtrade/rpc/discord.py index c48508300..8be0eab68 100644 --- a/freqtrade/rpc/discord.py +++ b/freqtrade/rpc/discord.py @@ -11,13 +11,12 @@ logger = logging.getLogger(__name__) class Discord(Webhook): def __init__(self, rpc: 'RPC', config: Config): - # super().__init__(rpc, config) + self._config = config self.rpc = rpc - self.config = config self.strategy = config.get('strategy', '') self.timeframe = config.get('timeframe', '') - self._url = self.config['discord']['webhook_url'] + self._url = config['discord']['webhook_url'] self._format = 'json' self._retries = 1 self._retry_delay = 0.1 @@ -31,19 +30,21 @@ class Discord(Webhook): def send_msg(self, msg) -> None: - if msg['type'].value in self.config['discord']: + if msg['type'].value in self._config['discord']: logger.info(f"Sending discord message: {msg}") msg['strategy'] = self.strategy msg['timeframe'] = self.timeframe - fields = self.config['discord'].get(msg['type'].value) + fields = self._config['discord'].get(msg['type'].value) color = 0x0000FF if msg['type'] in (RPCMessageType.EXIT, RPCMessageType.EXIT_FILL): profit_ratio = msg.get('profit_ratio') color = (0x00FF00 if profit_ratio > 0 else 0xFF0000) - + title = msg['type'].value + if 'pair' in msg: + title = f"Trade: {msg['pair']} {msg['type'].value}" embeds = [{ - 'title': f"Trade: {msg['pair']} {msg['type'].value}", + 'title': title, 'color': color, 'fields': [], @@ -51,7 +52,7 @@ class Discord(Webhook): for f in fields: for k, v in f.items(): v = v.format(**msg) - embeds[0]['fields'].append( # type: ignore + embeds[0]['fields'].append( {'name': k, 'value': v, 'inline': True}) # Send the message to discord channel diff --git a/freqtrade/rpc/external_message_consumer.py b/freqtrade/rpc/external_message_consumer.py index f5ba4b490..e86f44c17 100644 --- a/freqtrade/rpc/external_message_consumer.py +++ b/freqtrade/rpc/external_message_consumer.py @@ -62,7 +62,7 @@ class ExternalMessageConsumer: self.enabled = self._emc_config.get('enabled', False) self.producers: List[Producer] = self._emc_config.get('producers', []) - self.wait_timeout = self._emc_config.get('wait_timeout', 300) # in seconds + self.wait_timeout = self._emc_config.get('wait_timeout', 30) # in seconds self.ping_timeout = self._emc_config.get('ping_timeout', 10) # in seconds self.sleep_time = self._emc_config.get('sleep_time', 10) # in seconds @@ -174,6 +174,7 @@ class ExternalMessageConsumer: :param producer: Dictionary containing producer info :param lock: An asyncio Lock """ + channel = None while self._running: try: host, port = producer['host'], producer['port'] @@ -182,7 +183,11 @@ class ExternalMessageConsumer: ws_url = f"ws://{host}:{port}/api/v1/message/ws?token={token}" # This will raise InvalidURI if the url is bad - async with websockets.connect(ws_url, max_size=self.message_size_limit) as ws: + async with websockets.connect( + ws_url, + max_size=self.message_size_limit, + ping_interval=None + ) as ws: channel = WebSocketChannel(ws, channel_id=name) logger.info(f"Producer connection success - {channel}") @@ -224,6 +229,10 @@ class ExternalMessageConsumer: logger.exception(e) continue + finally: + if channel: + await channel.close() + async def _receive_messages( self, channel: WebSocketChannel, @@ -261,6 +270,11 @@ class ExternalMessageConsumer: logger.debug(f"Connection to {channel} still alive...") continue + except (websockets.exceptions.ConnectionClosed): + # Just eat the error and continue reconnecting + logger.warning(f"Disconnection in {channel} - retrying in {self.sleep_time}s") + await asyncio.sleep(self.sleep_time) + break except Exception as e: logger.warning(f"Ping error {channel} - retrying in {self.sleep_time}s") logger.debug(e, exc_info=e) diff --git a/freqtrade/rpc/fiat_convert.py b/freqtrade/rpc/fiat_convert.py index cbe4c0045..24c34af72 100644 --- a/freqtrade/rpc/fiat_convert.py +++ b/freqtrade/rpc/fiat_convert.py @@ -3,8 +3,8 @@ Module that define classes to convert Crypto-currency to FIAT e.g BTC to USD """ -import datetime import logging +from datetime import datetime from typing import Dict, List from cachetools import TTLCache @@ -46,7 +46,9 @@ class CryptoToFiatConverter(LoggingMixin): if CryptoToFiatConverter.__instance is None: CryptoToFiatConverter.__instance = object.__new__(cls) try: - CryptoToFiatConverter._coingekko = CoinGeckoAPI() + # Limit retires to 1 (0 and 1) + # otherwise we risk bot impact if coingecko is down. + CryptoToFiatConverter._coingekko = CoinGeckoAPI(retries=1) except BaseException: CryptoToFiatConverter._coingekko = None return CryptoToFiatConverter.__instance @@ -67,7 +69,7 @@ class CryptoToFiatConverter(LoggingMixin): logger.warning( "Too many requests for CoinGecko API, backing off and trying again later.") # Set backoff timestamp to 60 seconds in the future - self._backoff = datetime.datetime.now().timestamp() + 60 + self._backoff = datetime.now().timestamp() + 60 return # If the request is not a 429 error we want to raise the normal error logger.error( @@ -81,7 +83,7 @@ class CryptoToFiatConverter(LoggingMixin): def _get_gekko_id(self, crypto_symbol): if not self._coinlistings: - if self._backoff <= datetime.datetime.now().timestamp(): + if self._backoff <= datetime.now().timestamp(): self._load_cryptomap() # Still not loaded. if not self._coinlistings: diff --git a/freqtrade/rpc/rpc_manager.py b/freqtrade/rpc/rpc_manager.py index e3b31d225..9c25723b0 100644 --- a/freqtrade/rpc/rpc_manager.py +++ b/freqtrade/rpc/rpc_manager.py @@ -88,10 +88,13 @@ class RPCManager: """ while queue: msg = queue.popleft() - self.send_msg({ - 'type': RPCMessageType.STRATEGY_MSG, - 'msg': msg, - }) + logger.info('Sending rpc strategy_msg: %s', msg) + for mod in self.registered_modules: + if mod._config.get(mod.name, {}).get('allow_custom_messages', False): + mod.send_msg({ + 'type': RPCMessageType.STRATEGY_MSG, + 'msg': msg, + }) def startup_messages(self, config: Config, pairlist, protections) -> None: if config['dry_run']: diff --git a/freqtrade/rpc/webhook.py b/freqtrade/rpc/webhook.py index bb3b3922f..19c4166b3 100644 --- a/freqtrade/rpc/webhook.py +++ b/freqtrade/rpc/webhook.py @@ -3,7 +3,7 @@ This module manages webhook communication """ import logging import time -from typing import Any, Dict +from typing import Any, Dict, Optional from requests import RequestException, post @@ -41,36 +41,44 @@ class Webhook(RPCHandler): """ pass + def _get_value_dict(self, msg: Dict[str, Any]) -> Optional[Dict[str, Any]]: + whconfig = self._config['webhook'] + # Deprecated 2022.10 - only keep generic method. + if msg['type'] in [RPCMessageType.ENTRY]: + valuedict = whconfig.get('webhookentry') + elif msg['type'] in [RPCMessageType.ENTRY_CANCEL]: + valuedict = whconfig.get('webhookentrycancel') + elif msg['type'] in [RPCMessageType.ENTRY_FILL]: + valuedict = whconfig.get('webhookentryfill') + elif msg['type'] == RPCMessageType.EXIT: + valuedict = whconfig.get('webhookexit') + elif msg['type'] == RPCMessageType.EXIT_FILL: + valuedict = whconfig.get('webhookexitfill') + elif msg['type'] == RPCMessageType.EXIT_CANCEL: + valuedict = whconfig.get('webhookexitcancel') + elif msg['type'] in (RPCMessageType.STATUS, + RPCMessageType.STARTUP, + RPCMessageType.WARNING): + valuedict = whconfig.get('webhookstatus') + elif msg['type'].value in whconfig: + # Allow all types ... + valuedict = whconfig.get(msg['type'].value) + elif msg['type'] in ( + RPCMessageType.PROTECTION_TRIGGER, + RPCMessageType.PROTECTION_TRIGGER_GLOBAL, + RPCMessageType.WHITELIST, + RPCMessageType.ANALYZED_DF, + RPCMessageType.STRATEGY_MSG): + # Don't fail for non-implemented types + return None + return valuedict + def send_msg(self, msg: Dict[str, Any]) -> None: """ Send a message to telegram channel """ try: - whconfig = self._config['webhook'] - if msg['type'] in [RPCMessageType.ENTRY]: - valuedict = whconfig.get('webhookentry') - elif msg['type'] in [RPCMessageType.ENTRY_CANCEL]: - valuedict = whconfig.get('webhookentrycancel') - elif msg['type'] in [RPCMessageType.ENTRY_FILL]: - valuedict = whconfig.get('webhookentryfill') - elif msg['type'] == RPCMessageType.EXIT: - valuedict = whconfig.get('webhookexit') - elif msg['type'] == RPCMessageType.EXIT_FILL: - valuedict = whconfig.get('webhookexitfill') - elif msg['type'] == RPCMessageType.EXIT_CANCEL: - valuedict = whconfig.get('webhookexitcancel') - elif msg['type'] in (RPCMessageType.STATUS, - RPCMessageType.STARTUP, - RPCMessageType.WARNING): - valuedict = whconfig.get('webhookstatus') - elif msg['type'] in ( - RPCMessageType.PROTECTION_TRIGGER, - RPCMessageType.PROTECTION_TRIGGER_GLOBAL, - RPCMessageType.WHITELIST, - RPCMessageType.ANALYZED_DF, - RPCMessageType.STRATEGY_MSG): - # Don't fail for non-implemented types - return - else: - raise NotImplementedError('Unknown message type: {}'.format(msg['type'])) + + valuedict = self._get_value_dict(msg) + if not valuedict: logger.info("Message type '%s' not configured for webhooks", msg['type']) return diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 8f803045f..681c5fcbb 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -49,7 +49,7 @@ class IStrategy(ABC, HyperStrategyMixin): _ft_params_from_file: Dict # associated minimal roi - minimal_roi: Dict = {} + minimal_roi: Dict = {"0": 10.0} # associated stoploss stoploss: float @@ -1072,28 +1072,26 @@ class IStrategy(ABC, HyperStrategyMixin): trade.stop_loss > (high or current_rate) ) + # Make sure current_profit is calculated using high for backtesting. + bound = (low if trade.is_short else high) + bound_profit = current_profit if not bound else trade.calc_profit_ratio(bound) if self.use_custom_stoploss and dir_correct: stop_loss_value = strategy_safe_wrapper(self.custom_stoploss, default_retval=None )(pair=trade.pair, trade=trade, current_time=current_time, - current_rate=current_rate, - current_profit=current_profit) + current_rate=(bound or current_rate), + current_profit=bound_profit) # Sanity check - error cases will return None if stop_loss_value: - # logger.info(f"{trade.pair} {stop_loss_value=} {current_profit=}") - trade.adjust_stop_loss(current_rate, stop_loss_value) + # logger.info(f"{trade.pair} {stop_loss_value=} {bound_profit=}") + trade.adjust_stop_loss(bound or current_rate, stop_loss_value) else: logger.warning("CustomStoploss function did not return valid stoploss") - sl_lower_long = (trade.stop_loss < (low or current_rate) and not trade.is_short) - sl_higher_short = (trade.stop_loss > (high or current_rate) and trade.is_short) - if self.trailing_stop and (sl_lower_long or sl_higher_short): + if self.trailing_stop and dir_correct: # trailing stoploss handling sl_offset = self.trailing_stop_positive_offset - # Make sure current_profit is calculated using high for backtesting. - bound = low if trade.is_short else high - bound_profit = current_profit if not bound else trade.calc_profit_ratio(bound) # Don't update stoploss if trailing_only_offset_is_reached is true. if not (self.trailing_only_offset_is_reached and bound_profit < sl_offset): @@ -1101,7 +1099,7 @@ class IStrategy(ABC, HyperStrategyMixin): if self.trailing_stop_positive is not None and bound_profit > sl_offset: stop_loss_value = self.trailing_stop_positive logger.debug(f"{trade.pair} - Using positive stoploss: {stop_loss_value} " - f"offset: {sl_offset:.4g} profit: {current_profit:.2%}") + f"offset: {sl_offset:.4g} profit: {bound_profit:.2%}") trade.adjust_stop_loss(bound or current_rate, stop_loss_value) diff --git a/freqtrade/templates/FreqaiHybridExampleStrategy.py b/freqtrade/templates/FreqaiExampleHybridStrategy.py similarity index 100% rename from freqtrade/templates/FreqaiHybridExampleStrategy.py rename to freqtrade/templates/FreqaiExampleHybridStrategy.py diff --git a/freqtrade/templates/sample_strategy.py b/freqtrade/templates/sample_strategy.py index 1eb3d4256..fd81570fe 100644 --- a/freqtrade/templates/sample_strategy.py +++ b/freqtrade/templates/sample_strategy.py @@ -5,6 +5,7 @@ import numpy as np # noqa import pandas as pd # noqa from pandas import DataFrame +from typing import Optional, Union from freqtrade.strategy import (BooleanParameter, CategoricalParameter, DecimalParameter, IStrategy, IntParameter) diff --git a/freqtrade/worker.py b/freqtrade/worker.py index dea0acc44..a407de0d7 100755 --- a/freqtrade/worker.py +++ b/freqtrade/worker.py @@ -14,6 +14,7 @@ from freqtrade.configuration import Configuration from freqtrade.constants import PROCESS_THROTTLE_SECS, RETRY_TIMEOUT, Config from freqtrade.enums import State from freqtrade.exceptions import OperationalException, TemporaryError +from freqtrade.exchange import timeframe_to_next_date from freqtrade.freqtradebot import FreqtradeBot @@ -35,7 +36,6 @@ class Worker: self._config = config self._init(False) - self.last_throttle_start_time: float = 0 self._heartbeat_msg: float = 0 # Tell systemd that we completed initialization phase @@ -112,7 +112,10 @@ class Worker: # Ping systemd watchdog before throttling self._notify("WATCHDOG=1\nSTATUS=State: RUNNING.") - self._throttle(func=self._process_running, throttle_secs=self._throttle_secs) + # Use an offset of 1s to ensure a new candle has been issued + self._throttle(func=self._process_running, throttle_secs=self._throttle_secs, + timeframe=self._config['timeframe'] if self._config else None, + timeframe_offset=1) if self._heartbeat_interval: now = time.time() @@ -127,24 +130,42 @@ class Worker: return state - def _throttle(self, func: Callable[..., Any], throttle_secs: float, *args, **kwargs) -> Any: + def _throttle(self, func: Callable[..., Any], throttle_secs: float, + timeframe: Optional[str] = None, timeframe_offset: float = 1.0, + *args, **kwargs) -> Any: """ Throttles the given callable that it takes at least `min_secs` to finish execution. :param func: Any callable :param throttle_secs: throttling interation execution time limit in seconds + :param timeframe: ensure iteration is executed at the beginning of the next candle. + :param timeframe_offset: offset in seconds to apply to the next candle time. :return: Any (result of execution of func) """ - self.last_throttle_start_time = time.time() + last_throttle_start_time = time.time() logger.debug("========================================") result = func(*args, **kwargs) - time_passed = time.time() - self.last_throttle_start_time - sleep_duration = max(throttle_secs - time_passed, 0.0) + time_passed = time.time() - last_throttle_start_time + sleep_duration = throttle_secs - time_passed + if timeframe: + next_tf = timeframe_to_next_date(timeframe) + # Maximum throttling should be until new candle arrives + # Offset of 0.2s is added to ensure a new candle has been issued. + next_tf_with_offset = next_tf.timestamp() - time.time() + timeframe_offset + sleep_duration = min(sleep_duration, next_tf_with_offset) + sleep_duration = max(sleep_duration, 0.0) + # next_iter = datetime.now(timezone.utc) + timedelta(seconds=sleep_duration) + logger.debug(f"Throttling with '{func.__name__}()': sleep for {sleep_duration:.2f} s, " f"last iteration took {time_passed:.2f} s.") - time.sleep(sleep_duration) + self._sleep(sleep_duration) return result + @staticmethod + def _sleep(sleep_duration: float) -> None: + """Local sleep method - to improve testability""" + time.sleep(sleep_duration) + def _process_stopped(self) -> None: self.freqtrade.process_stopped() diff --git a/requirements-dev.txt b/requirements-dev.txt index dccd5baba..91a5c060e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -9,23 +9,23 @@ coveralls==3.3.1 flake8==5.0.4 flake8-tidy-imports==4.8.0 -mypy==0.981 +mypy==0.982 pre-commit==2.20.0 pytest==7.1.3 -pytest-asyncio==0.19.0 +pytest-asyncio==0.20.1 pytest-cov==4.0.0 -pytest-mock==3.9.0 +pytest-mock==3.10.0 pytest-random-order==1.0.4 isort==5.10.1 # For datetime mocking time-machine==2.8.2 # Convert jupyter notebooks to markdown documents -nbconvert==7.0.0 +nbconvert==7.2.1 # mypy types types-cachetools==5.2.1 types-filelock==3.2.7 -types-requests==2.28.11 -types-tabulate==0.8.11 -types-python-dateutil==2.8.19 +types-requests==2.28.11.2 +types-tabulate==0.9.0.0 +types-python-dateutil==2.8.19.2 diff --git a/requirements-freqai.txt b/requirements-freqai.txt index cf0d2eb07..201d5be1b 100644 --- a/requirements-freqai.txt +++ b/requirements-freqai.txt @@ -5,5 +5,6 @@ scikit-learn==1.1.2 joblib==1.2.0 catboost==1.1; platform_machine != 'aarch64' -lightgbm==3.3.2 +lightgbm==3.3.3 xgboost==1.6.2 +tensorboard==2.10.1 diff --git a/requirements-hyperopt.txt b/requirements-hyperopt.txt index efa31272a..39d304115 100644 --- a/requirements-hyperopt.txt +++ b/requirements-hyperopt.txt @@ -2,8 +2,8 @@ -r requirements.txt # Required for hyperopt -scipy==1.9.1 +scipy==1.9.3 scikit-learn==1.1.2 scikit-optimize==0.9.0 filelock==3.8.0 -progressbar2==4.0.0 +progressbar2==4.1.1 diff --git a/requirements.txt b/requirements.txt index 4f4b30d0c..78d3dd1cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,14 @@ -numpy==1.23.3 -pandas==1.5.0; platform_machine != 'armv7l' +numpy==1.23.4 +pandas==1.5.1; platform_machine != 'armv7l' # Piwheels doesn't have 1.5.0 yet. pandas==1.4.3; platform_machine == 'armv7l' pandas-ta==0.3.14b -ccxt==1.95.2 +ccxt==2.0.58 # Pin cryptography for now due to rust build errors with piwheels cryptography==38.0.1 aiohttp==3.8.3 -SQLAlchemy==1.4.41 +SQLAlchemy==1.4.42 python-telegram-bot==13.14 arrow==1.2.3 cachetools==4.2.2 @@ -17,7 +17,7 @@ urllib3==1.26.12 jsonschema==4.16.0 TA-Lib==0.4.25 technical==1.3.0 -tabulate==0.8.10 +tabulate==0.9.0 pycoingecko==3.0.0 jinja2==3.1.2 tables==3.7.0 @@ -29,7 +29,7 @@ pyarrow==9.0.0; platform_machine != 'armv7l' py_find_1st==1.1.5 # Load ticker files 30% faster -python-rapidjson==1.8 +python-rapidjson==1.9 # Properly format api responses orjson==3.8.0 @@ -37,10 +37,10 @@ orjson==3.8.0 sdnotify==0.3.2 # API Server -fastapi==0.85.0 -pydantic>=1.8.0 +fastapi==0.85.1 +pydantic==1.10.2 uvicorn==0.18.3 -pyjwt==2.5.0 +pyjwt==2.6.0 aiofiles==22.1.0 psutil==5.9.2 diff --git a/scripts/ws_client.py b/scripts/ws_client.py new file mode 100644 index 000000000..23ad9296d --- /dev/null +++ b/scripts/ws_client.py @@ -0,0 +1,328 @@ +#!/usr/bin/env python3 +""" +Simple command line client for Testing/debugging +a Freqtrade bot's message websocket + +Should not import anything from freqtrade, +so it can be used as a standalone script. +""" +import argparse +import asyncio +import logging +import socket +import sys +import time +from pathlib import Path + +import orjson +import pandas +import rapidjson +import websockets +from dateutil.relativedelta import relativedelta + + +logger = logging.getLogger("WebSocketClient") + + +# --------------------------------------------------------------------------- + +def setup_logging(filename: str): + logging.basicConfig( + level=logging.INFO, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + handlers=[ + logging.FileHandler(filename), + logging.StreamHandler() + ] + ) + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument( + '-c', + '--config', + help='Specify configuration file (default: %(default)s). ', + dest='config', + type=str, + metavar='PATH', + default='config.json' + ) + parser.add_argument( + '-l', + '--logfile', + help='The filename to log to.', + dest='logfile', + type=str, + default='ws_client.log' + ) + + args = parser.parse_args() + return vars(args) + + +def load_config(configfile): + file = Path(configfile) + if file.is_file(): + with file.open("r") as f: + config = rapidjson.load(f, parse_mode=rapidjson.PM_COMMENTS | + rapidjson.PM_TRAILING_COMMAS) + return config + else: + logger.warning(f"Could not load config file {file}.") + sys.exit(1) + + +def readable_timedelta(delta): + """ + Convert a dateutil.relativedelta to a readable format + + :param delta: A dateutil.relativedelta + :returns: The readable time difference string + """ + attrs = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'microseconds'] + return ", ".join([ + '%d %s' % (getattr(delta, attr), attr if getattr(delta, attr) > 0 else attr[:-1]) + for attr in attrs if getattr(delta, attr) + ]) + +# ---------------------------------------------------------------------------- + + +def json_serialize(message): + """ + Serialize a message to JSON using orjson + :param message: The message to serialize + """ + return str(orjson.dumps(message), "utf-8") + + +def json_deserialize(message): + """ + Deserialize JSON to a dict + :param message: The message to deserialize + """ + def json_to_dataframe(data: str) -> pandas.DataFrame: + dataframe = pandas.read_json(data, orient='split') + if 'date' in dataframe.columns: + dataframe['date'] = pandas.to_datetime(dataframe['date'], unit='ms', utc=True) + + return dataframe + + def _json_object_hook(z): + if z.get('__type__') == 'dataframe': + return json_to_dataframe(z.get('__value__')) + return z + + return rapidjson.loads(message, object_hook=_json_object_hook) + + +# --------------------------------------------------------------------------- + + +class ClientProtocol: + logger = logging.getLogger("WebSocketClient.Protocol") + _MESSAGE_COUNT = 0 + _LAST_RECEIVED_AT = 0 # The epoch we received a message most recently + + async def on_connect(self, websocket): + # On connection we have to send our initial requests + initial_requests = [ + { + "type": "subscribe", # The subscribe request should always be first + "data": ["analyzed_df", "whitelist"] # The message types we want + }, + { + "type": "whitelist", + "data": None, + }, + { + "type": "analyzed_df", + "data": {"limit": 1500} + } + ] + + for request in initial_requests: + await websocket.send(json_serialize(request)) + + async def on_message(self, websocket, name, message): + deserialized = json_deserialize(message) + + message_size = sys.getsizeof(message) + message_type = deserialized.get('type') + message_data = deserialized.get('data') + + self.logger.info( + f"Received message of type {message_type} [{message_size} bytes] @ [{name}]" + ) + + time_difference = self._calculate_time_difference() + + if self._MESSAGE_COUNT > 0: + self.logger.info(f"Time since last message: {time_difference}") + + message_handler = getattr(self, f"_handle_{message_type}", None) or self._handle_default + await message_handler(name, message_type, message_data) + + self._MESSAGE_COUNT += 1 + self.logger.info(f"[{self._MESSAGE_COUNT}] total messages..") + self.logger.info("-" * 80) + + def _calculate_time_difference(self): + old_last_received_at = self._LAST_RECEIVED_AT + self._LAST_RECEIVED_AT = time.time() * 1e6 + time_delta = relativedelta(microseconds=(self._LAST_RECEIVED_AT - old_last_received_at)) + + return readable_timedelta(time_delta) + + async def _handle_whitelist(self, name, type, data): + self.logger.info(data) + + async def _handle_analyzed_df(self, name, type, data): + key, la, df = data['key'], data['la'], data['df'] + + if not df.empty: + columns = ", ".join([str(column) for column in df.columns]) + + self.logger.info(key) + self.logger.info(f"Last analyzed datetime: {la}") + self.logger.info(f"Latest candle datetime: {df.iloc[-1]['date']}") + self.logger.info(f"DataFrame length: {len(df)}") + self.logger.info(f"DataFrame columns: {columns}") + else: + self.logger.info("Empty DataFrame") + + async def _handle_default(self, name, type, data): + self.logger.info("Unkown message of type {type} received...") + self.logger.info(data) + + +async def create_client( + host, + port, + token, + name='default', + protocol=ClientProtocol(), + sleep_time=10, + ping_timeout=10, + wait_timeout=30, + **kwargs +): + """ + Create a websocket client and listen for messages + :param host: The host + :param port: The port + :param token: The websocket auth token + :param name: The name of the producer + :param **kwargs: Any extra kwargs passed to websockets.connect + """ + + while 1: + try: + websocket_url = f"ws://{host}:{port}/api/v1/message/ws?token={token}" + logger.info(f"Attempting to connect to {name} @ {host}:{port}") + + async with websockets.connect(websocket_url, **kwargs) as ws: + logger.info("Connection successful...") + await protocol.on_connect(ws) + + # Now listen for messages + while 1: + try: + message = await asyncio.wait_for( + ws.recv(), + timeout=wait_timeout + ) + + await protocol.on_message(ws, name, message) + + except ( + asyncio.TimeoutError, + websockets.exceptions.WebSocketException + ): + # Try pinging + try: + pong = ws.ping() + await asyncio.wait_for( + pong, + timeout=ping_timeout + ) + logger.info("Connection still alive...") + + continue + + except asyncio.TimeoutError: + logger.error(f"Ping timed out, retrying in {sleep_time}s") + await asyncio.sleep(sleep_time) + + break + + except ( + socket.gaierror, + ConnectionRefusedError, + websockets.exceptions.InvalidStatusCode, + websockets.exceptions.InvalidMessage + ) as e: + logger.error(f"Connection Refused - {e} retrying in {sleep_time}s") + await asyncio.sleep(sleep_time) + + continue + + except ( + websockets.exceptions.ConnectionClosedError, + websockets.exceptions.ConnectionClosedOK + ): + # Just keep trying to connect again indefinitely + await asyncio.sleep(sleep_time) + + continue + + except Exception as e: + # An unforseen error has occurred, log and try reconnecting again + logger.error("Unexpected error has occurred:") + logger.exception(e) + + await asyncio.sleep(sleep_time) + continue + + +# --------------------------------------------------------------------------- + + +async def _main(args): + setup_logging(args['logfile']) + config = load_config(args['config']) + + emc_config = config.get('external_message_consumer', {}) + + producers = emc_config.get('producers', []) + producer = producers[0] + + wait_timeout = emc_config.get('wait_timeout', 30) + ping_timeout = emc_config.get('ping_timeout', 10) + sleep_time = emc_config.get('sleep_time', 10) + message_size_limit = (emc_config.get('message_size_limit', 8) << 20) + + await create_client( + producer['host'], + producer['port'], + producer['ws_token'], + producer['name'], + sleep_time=sleep_time, + ping_timeout=ping_timeout, + wait_timeout=wait_timeout, + max_size=message_size_limit, + ping_interval=None + ) + + +def main(): + args = parse_args() + try: + asyncio.run(_main(args)) + except KeyboardInterrupt: + logger.info("Exiting...") + + +if __name__ == "__main__": + main() diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 28515a28a..d3bceb004 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -18,6 +18,7 @@ from freqtrade.commands import (start_backtesting_show, start_convert_data, star from freqtrade.commands.db_commands import start_convert_db from freqtrade.commands.deploy_commands import (clean_ui_subdir, download_and_install_ui, get_ui_download_url, read_ui_version) +from freqtrade.commands.list_commands import start_list_freqAI_models from freqtrade.configuration import setup_utils_configuration from freqtrade.enums import RunMode from freqtrade.exceptions import OperationalException @@ -944,6 +945,34 @@ def test_start_list_strategies(capsys): assert str(Path("broken_strats/broken_futures_strategies.py")) in captured.out +def test_start_list_freqAI_models(capsys): + + args = [ + "list-freqaimodels", + "-1" + ] + pargs = get_args(args) + pargs['config'] = None + start_list_freqAI_models(pargs) + captured = capsys.readouterr() + assert "LightGBMClassifier" in captured.out + assert "LightGBMRegressor" in captured.out + assert "XGBoostRegressor" in captured.out + assert "/LightGBMRegressor.py" not in captured.out + + args = [ + "list-freqaimodels", + ] + pargs = get_args(args) + pargs['config'] = None + start_list_freqAI_models(pargs) + captured = capsys.readouterr() + assert "LightGBMClassifier" in captured.out + assert "LightGBMRegressor" in captured.out + assert "XGBoostRegressor" in captured.out + assert "/LightGBMRegressor.py" in captured.out + + def test_start_test_pairlist(mocker, caplog, tickers, default_conf, capsys): patch_exchange(mocker, mock_markets=True) mocker.patch.multiple('freqtrade.exchange.Exchange', diff --git a/tests/conftest.py b/tests/conftest.py index a9eeb481e..9f71709f1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,7 @@ from unittest.mock import MagicMock, Mock, PropertyMock import arrow import numpy as np +import pandas as pd import pytest from telegram import Chat, Message, Update @@ -19,6 +20,7 @@ from freqtrade.data.converter import ohlcv_to_dataframe from freqtrade.edge import PairInfo from freqtrade.enums import CandleType, MarginMode, RunMode, SignalDirection, TradingMode from freqtrade.exchange import Exchange +from freqtrade.exchange.exchange import timeframe_to_minutes from freqtrade.freqtradebot import FreqtradeBot from freqtrade.persistence import LocalTrade, Order, Trade, init_db from freqtrade.resolvers import ExchangeResolver @@ -82,6 +84,33 @@ def get_args(args): return Arguments(args).get_parsed_arg() +def generate_test_data(timeframe: str, size: int, start: str = '2020-07-05'): + np.random.seed(42) + tf_mins = timeframe_to_minutes(timeframe) + + base = np.random.normal(20, 2, size=size) + + date = pd.date_range(start, periods=size, freq=f'{tf_mins}min', tz='UTC') + df = pd.DataFrame({ + 'date': date, + 'open': base, + 'high': base + np.random.normal(2, 1, size=size), + 'low': base - np.random.normal(2, 1, size=size), + 'close': base + np.random.normal(0, 1, size=size), + 'volume': np.random.normal(200, size=size) + } + ) + df = df.dropna() + return df + + +def generate_test_data_raw(timeframe: str, size: int, start: str = '2020-07-05'): + """ Generates data in the ohlcv format used by ccxt """ + df = generate_test_data(timeframe, size, start) + df['date'] = df.loc[:, 'date'].view(np.int64) // 1000 // 1000 + return list(list(x) for x in zip(*(df[x].values.tolist() for x in df.columns))) + + # Source: https://stackoverflow.com/questions/29881236/how-to-mock-asyncio-coroutines # TODO: This should be replaced with AsyncMock once support for python 3.7 is dropped. def get_mock_coro(return_value=None, side_effect=None): diff --git a/tests/data/test_datahandler.py b/tests/data/test_datahandler.py index 5d6d60f84..67eeda7d0 100644 --- a/tests/data/test_datahandler.py +++ b/tests/data/test_datahandler.py @@ -15,7 +15,7 @@ from freqtrade.data.history.idatahandler import IDataHandler, get_datahandler, g from freqtrade.data.history.jsondatahandler import JsonDataHandler, JsonGzDataHandler from freqtrade.data.history.parquetdatahandler import ParquetDataHandler from freqtrade.enums import CandleType, TradingMode -from tests.conftest import log_has +from tests.conftest import log_has, log_has_re def test_datahandler_ohlcv_get_pairs(testdatadir): @@ -154,6 +154,85 @@ def test_jsondatahandler_ohlcv_load(testdatadir, caplog): assert df.columns.equals(df1.columns) +def test_datahandler__check_empty_df(testdatadir, caplog): + dh = JsonDataHandler(testdatadir) + expected_text = r"Price jump in UNITTEST/USDT, 1h, spot between" + df = DataFrame([ + [ + 1511686200000, # 8:50:00 + 8.794, # open + 8.948, # high + 8.794, # low + 8.88, # close + 2255, # volume (in quote currency) + ], + [ + 1511686500000, # 8:55:00 + 8.88, + 8.942, + 8.88, + 8.893, + 9911, + ], + [ + 1511687100000, # 9:05:00 + 8.891, + 8.893, + 8.875, + 8.877, + 2251 + ], + [ + 1511687400000, # 9:10:00 + 8.877, + 8.883, + 8.895, + 8.817, + 123551 + ] + ], columns=['date', 'open', 'high', 'low', 'close', 'volume']) + + dh._check_empty_df(df, 'UNITTEST/USDT', '1h', CandleType.SPOT, True, True) + assert not log_has_re(expected_text, caplog) + df = DataFrame([ + [ + 1511686200000, # 8:50:00 + 8.794, # open + 8.948, # high + 8.794, # low + 8.88, # close + 2255, # volume (in quote currency) + ], + [ + 1511686500000, # 8:55:00 + 8.88, + 8.942, + 8.88, + 8.893, + 9911, + ], + [ + 1511687100000, # 9:05:00 + 889.1, # Price jump by several decimals + 889.3, + 887.5, + 887.7, + 2251 + ], + [ + 1511687400000, # 9:10:00 + 8.877, + 8.883, + 8.895, + 8.817, + 123551 + ] + ], columns=['date', 'open', 'high', 'low', 'close', 'volume']) + + dh._check_empty_df(df, 'UNITTEST/USDT', '1h', CandleType.SPOT, True, True) + assert log_has_re(expected_text, caplog) + + @pytest.mark.parametrize('datahandler', ['feather', 'parquet']) def test_datahandler_trades_not_supported(datahandler, testdatadir, ): dh = get_datahandler(testdatadir, datahandler) diff --git a/tests/exchange/test_binance.py b/tests/exchange/test_binance.py index ef5cb1240..1fc8b4153 100644 --- a/tests/exchange/test_binance.py +++ b/tests/exchange/test_binance.py @@ -5,7 +5,7 @@ from unittest.mock import MagicMock, PropertyMock import ccxt import pytest -from freqtrade.enums import MarginMode, TradingMode +from freqtrade.enums import CandleType, MarginMode, TradingMode from freqtrade.exceptions import DependencyException, InvalidOrderException, OperationalException from tests.conftest import get_mock_coro, get_patched_exchange, log_has_re from tests.exchange.test_exchange import ccxt_exceptionhandlers @@ -162,9 +162,6 @@ def test_stoploss_adjust_binance(mocker, default_conf, sl1, sl2, sl3, side): } assert exchange.stoploss_adjust(sl1, order, side=side) assert not exchange.stoploss_adjust(sl2, order, side=side) - # Test with invalid order case - order['type'] = 'stop_loss' - assert not exchange.stoploss_adjust(sl3, order, side=side) def test_fill_leverage_tiers_binance(default_conf, mocker): @@ -542,7 +539,7 @@ def test__set_leverage_binance(mocker, default_conf): @pytest.mark.asyncio -@pytest.mark.parametrize('candle_type', ['mark', '']) +@pytest.mark.parametrize('candle_type', [CandleType.MARK, '']) async def test__async_get_historic_ohlcv_binance(default_conf, mocker, caplog, candle_type): ohlcv = [ [ diff --git a/tests/exchange/test_ccxt_compat.py b/tests/exchange/test_ccxt_compat.py index 6798cd2f7..9f9866aba 100644 --- a/tests/exchange/test_ccxt_compat.py +++ b/tests/exchange/test_ccxt_compat.py @@ -56,7 +56,7 @@ EXCHANGES = { 'leverage_in_spot_market': True, }, 'kucoin': { - 'pair': 'BTC/USDT', + 'pair': 'XRP/USDT', 'stake_currency': 'USDT', 'hasQuoteVolume': True, 'timeframe': '5m', @@ -268,9 +268,8 @@ class TestCCXTExchange(): now = datetime.now(timezone.utc) - timedelta(minutes=(timeframe_to_minutes(timeframe) * 2)) assert exchange.klines(pair_tf).iloc[-1]['date'] >= timeframe_to_prev_date(timeframe, now) - def ccxt__async_get_candle_history(self, exchange, exchangename, pair, timeframe): + def ccxt__async_get_candle_history(self, exchange, exchangename, pair, timeframe, candle_type): - candle_type = CandleType.SPOT timeframe_ms = timeframe_to_msecs(timeframe) now = timeframe_to_prev_date( timeframe, datetime.now(timezone.utc)) @@ -302,7 +301,8 @@ class TestCCXTExchange(): return pair = EXCHANGES[exchangename]['pair'] timeframe = EXCHANGES[exchangename]['timeframe'] - self.ccxt__async_get_candle_history(exchange, exchangename, pair, timeframe) + self.ccxt__async_get_candle_history( + exchange, exchangename, pair, timeframe, CandleType.SPOT) def test_ccxt__async_get_candle_history_futures(self, exchange_futures): exchange, exchangename = exchange_futures @@ -311,7 +311,8 @@ class TestCCXTExchange(): return pair = EXCHANGES[exchangename].get('futures_pair', EXCHANGES[exchangename]['pair']) timeframe = EXCHANGES[exchangename]['timeframe'] - self.ccxt__async_get_candle_history(exchange, exchangename, pair, timeframe) + self.ccxt__async_get_candle_history( + exchange, exchangename, pair, timeframe, CandleType.FUTURES) def test_ccxt_fetch_funding_rate_history(self, exchange_futures): exchange, exchangename = exchange_futures diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 37ba2ca97..25ba294a3 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -22,7 +22,8 @@ from freqtrade.exchange.common import (API_FETCH_ORDER_RETRY_COUNT, API_RETRY_CO calculate_backoff, remove_credentials) from freqtrade.exchange.exchange import amount_to_contract_precision from freqtrade.resolvers.exchange_resolver import ExchangeResolver -from tests.conftest import get_mock_coro, get_patched_exchange, log_has, log_has_re, num_log_has_re +from tests.conftest import (generate_test_data_raw, get_mock_coro, get_patched_exchange, log_has, + log_has_re, num_log_has_re) # Make sure to always keep one exchange here which is NOT subclassed!! @@ -1833,6 +1834,7 @@ def test_get_tickers(default_conf, mocker, exchange_name): 'last': 41, } } + mocker.patch('freqtrade.exchange.exchange.Exchange.exchange_has', return_value=True) api_mock.fetch_tickers = MagicMock(return_value=tick) api_mock.fetch_bids_asks = MagicMock(return_value={}) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) @@ -1882,6 +1884,11 @@ def test_get_tickers(default_conf, mocker, exchange_name): assert api_mock.fetch_tickers.call_count == 1 assert api_mock.fetch_bids_asks.call_count == (1 if exchange_name == 'binance' else 0) + api_mock.fetch_tickers.reset_mock() + api_mock.fetch_bids_asks.reset_mock() + mocker.patch('freqtrade.exchange.exchange.Exchange.exchange_has', return_value=False) + assert exchange.get_tickers() == {} + @pytest.mark.parametrize("exchange_name", EXCHANGES) def test_fetch_ticker(default_conf, mocker, exchange_name): @@ -2083,7 +2090,7 @@ async def test__async_get_historic_ohlcv(default_conf, mocker, caplog, exchange_ def test_refresh_latest_ohlcv(mocker, default_conf, caplog, candle_type) -> None: ohlcv = [ [ - (arrow.utcnow().int_timestamp - 1) * 1000, # unix timestamp ms + (arrow.utcnow().shift(minutes=-5).int_timestamp) * 1000, # unix timestamp ms 1, # open 2, # high 3, # low @@ -2140,10 +2147,22 @@ def test_refresh_latest_ohlcv(mocker, default_conf, caplog, candle_type) -> None assert len(res) == len(pairs) assert exchange._api_async.fetch_ohlcv.call_count == 0 - exchange.required_candle_call_count = 1 assert log_has(f"Using cached candle (OHLCV) data for {pairs[0][0]}, " f"{pairs[0][1]}, {candle_type} ...", caplog) + caplog.clear() + # Reset refresh times - must do 2 call per pair as cache is expired + exchange._pairs_last_refresh_time = {} + res = exchange.refresh_latest_ohlcv( + [('IOTA/ETH', '5m', candle_type), ('XRP/ETH', '5m', candle_type)]) + assert len(res) == len(pairs) + + assert exchange._api_async.fetch_ohlcv.call_count == 4 + + # cache - but disabled caching + exchange._api_async.fetch_ohlcv.reset_mock() + exchange.required_candle_call_count = 1 + pairlist = [ ('IOTA/ETH', '5m', candle_type), ('XRP/ETH', '5m', candle_type), @@ -2159,6 +2178,7 @@ def test_refresh_latest_ohlcv(mocker, default_conf, caplog, candle_type) -> None assert exchange._api_async.fetch_ohlcv.call_count == 3 exchange._api_async.fetch_ohlcv.reset_mock() caplog.clear() + # Call with invalid timeframe res = exchange.refresh_latest_ohlcv([('IOTA/ETH', '3m', candle_type)], cache=False) if candle_type != CandleType.MARK: @@ -2169,6 +2189,101 @@ def test_refresh_latest_ohlcv(mocker, default_conf, caplog, candle_type) -> None assert len(res) == 1 +@pytest.mark.parametrize('candle_type', [CandleType.FUTURES, CandleType.MARK, CandleType.SPOT]) +def test_refresh_latest_ohlcv_cache(mocker, default_conf, candle_type, time_machine) -> None: + start = datetime(2021, 8, 1, 0, 0, 0, 0, tzinfo=timezone.utc) + ohlcv = generate_test_data_raw('1h', 100, start.strftime('%Y-%m-%d')) + time_machine.move_to(start + timedelta(hours=99, minutes=30)) + + exchange = get_patched_exchange(mocker, default_conf) + mocker.patch("freqtrade.exchange.Exchange.ohlcv_candle_limit", return_value=100) + assert exchange._startup_candle_count == 0 + + exchange._api_async.fetch_ohlcv = get_mock_coro(ohlcv) + pair1 = ('IOTA/ETH', '1h', candle_type) + pair2 = ('XRP/ETH', '1h', candle_type) + pairs = [pair1, pair2] + + # No caching + assert not exchange._klines + res = exchange.refresh_latest_ohlcv(pairs, cache=False) + assert exchange._api_async.fetch_ohlcv.call_count == 2 + assert len(res) == 2 + assert len(res[pair1]) == 99 + assert len(res[pair2]) == 99 + assert not exchange._klines + exchange._api_async.fetch_ohlcv.reset_mock() + + # With caching + res = exchange.refresh_latest_ohlcv(pairs) + assert exchange._api_async.fetch_ohlcv.call_count == 2 + assert len(res) == 2 + assert len(res[pair1]) == 99 + assert len(res[pair2]) == 99 + assert exchange._klines + assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-1][0] // 1000 + exchange._api_async.fetch_ohlcv.reset_mock() + + # Returned from cache + res = exchange.refresh_latest_ohlcv(pairs) + assert exchange._api_async.fetch_ohlcv.call_count == 0 + assert len(res) == 2 + assert len(res[pair1]) == 99 + assert len(res[pair2]) == 99 + assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-1][0] // 1000 + + # Move time 1 candle further but result didn't change yet + time_machine.move_to(start + timedelta(hours=101)) + res = exchange.refresh_latest_ohlcv(pairs) + assert exchange._api_async.fetch_ohlcv.call_count == 2 + assert len(res) == 2 + assert len(res[pair1]) == 99 + assert len(res[pair2]) == 99 + assert res[pair2].at[0, 'open'] + assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-1][0] // 1000 + refresh_pior = exchange._pairs_last_refresh_time[pair1] + + # New candle on exchange - return 100 candles - but skip one candle so we actually get 2 candles + # in one go + new_startdate = (start + timedelta(hours=2)).strftime('%Y-%m-%d %H:%M') + # mocker.patch("freqtrade.exchange.Exchange.ohlcv_candle_limit", return_value=100) + ohlcv = generate_test_data_raw('1h', 100, new_startdate) + exchange._api_async.fetch_ohlcv = get_mock_coro(ohlcv) + res = exchange.refresh_latest_ohlcv(pairs) + assert exchange._api_async.fetch_ohlcv.call_count == 2 + assert len(res) == 2 + assert len(res[pair1]) == 100 + assert len(res[pair2]) == 100 + # Verify index starts at 0 + assert res[pair2].at[0, 'open'] + assert refresh_pior != exchange._pairs_last_refresh_time[pair1] + + assert exchange._pairs_last_refresh_time[pair1] == ohlcv[-1][0] // 1000 + assert exchange._pairs_last_refresh_time[pair2] == ohlcv[-1][0] // 1000 + exchange._api_async.fetch_ohlcv.reset_mock() + + # Retry same call - from cache + res = exchange.refresh_latest_ohlcv(pairs) + assert exchange._api_async.fetch_ohlcv.call_count == 0 + assert len(res) == 2 + assert len(res[pair1]) == 100 + assert len(res[pair2]) == 100 + assert res[pair2].at[0, 'open'] + + # Move to distant future (so a 1 call would cause a hole in the data) + time_machine.move_to(start + timedelta(hours=2000)) + ohlcv = generate_test_data_raw('1h', 100, start + timedelta(hours=1900)) + exchange._api_async.fetch_ohlcv = get_mock_coro(ohlcv) + res = exchange.refresh_latest_ohlcv(pairs) + + assert exchange._api_async.fetch_ohlcv.call_count == 2 + assert len(res) == 2 + # Cache eviction - new data. + assert len(res[pair1]) == 99 + assert len(res[pair2]) == 99 + assert res[pair2].at[0, 'open'] + + @pytest.mark.asyncio @pytest.mark.parametrize("exchange_name", EXCHANGES) async def test__async_get_candle_history(default_conf, mocker, caplog, exchange_name): @@ -2240,7 +2355,8 @@ async def test__async_kucoin_get_candle_history(default_conf, mocker, caplog): for _ in range(3): with pytest.raises(DDosProtection, match=r'429 Too Many Requests'): await exchange._async_get_candle_history( - "ETH/BTC", "5m", (arrow.utcnow().int_timestamp - 2000) * 1000, count=3) + "ETH/BTC", "5m", CandleType.SPOT, + since_ms=(arrow.utcnow().int_timestamp - 2000) * 1000, count=3) assert num_log_has_re(msg, caplog) == 3 caplog.clear() @@ -2256,7 +2372,8 @@ async def test__async_kucoin_get_candle_history(default_conf, mocker, caplog): for _ in range(3): with pytest.raises(DDosProtection, match=r'429 Too Many Requests'): await exchange._async_get_candle_history( - "ETH/BTC", "5m", (arrow.utcnow().int_timestamp - 2000) * 1000, count=3) + "ETH/BTC", "5m", CandleType.SPOT, + (arrow.utcnow().int_timestamp - 2000) * 1000, count=3) # Expect the "returned exception" message 12 times (4 retries * 3 (loop)) assert num_log_has_re(msg, caplog) == 12 assert num_log_has_re(msg2, caplog) == 9 @@ -4234,9 +4351,10 @@ def test__fetch_and_calculate_funding_fees_datetime_called( ('XLTCUSDT', 1, 'spot'), ('LTC/USD', 1, 'futures'), ('XLTCUSDT', 0.01, 'futures'), - ('ETH/USDT:USDT', 10, 'futures') + ('ETH/USDT:USDT', 10, 'futures'), + ('TORN/USDT:USDT', None, 'futures'), # Don't fail for unavailable pairs. ]) -def est__get_contract_size(mocker, default_conf, pair, expected_size, trading_mode): +def test__get_contract_size(mocker, default_conf, pair, expected_size, trading_mode): api_mock = MagicMock() default_conf['trading_mode'] = trading_mode default_conf['margin_mode'] = 'isolated' diff --git a/tests/exchange/test_huobi.py b/tests/exchange/test_huobi.py index fc7c7cefb..2ce379a47 100644 --- a/tests/exchange/test_huobi.py +++ b/tests/exchange/test_huobi.py @@ -113,5 +113,4 @@ def test_stoploss_adjust_huobi(mocker, default_conf): assert exchange.stoploss_adjust(1501, order, 'sell') assert not exchange.stoploss_adjust(1499, order, 'sell') # Test with invalid order case - order['type'] = 'stop_loss' - assert not exchange.stoploss_adjust(1501, order, 'sell') + assert exchange.stoploss_adjust(1501, order, 'sell') diff --git a/tests/freqai/conftest.py b/tests/freqai/conftest.py index 7f4897439..00efad3a7 100644 --- a/tests/freqai/conftest.py +++ b/tests/freqai/conftest.py @@ -131,6 +131,8 @@ def make_unfiltered_dataframe(mocker, freqai_conf): unfiltered_dataframe = freqai.dk.use_strategy_to_populate_indicators( strategy, corr_dataframes, base_dataframes, freqai.dk.pair ) + for i in range(5): + unfiltered_dataframe[f'constant_{i}'] = i unfiltered_dataframe = freqai.dk.slice_dataframe(new_timerange, unfiltered_dataframe) diff --git a/tests/freqai/test_freqai_backtesting.py b/tests/freqai/test_freqai_backtesting.py index b1881b2f5..5b9d3aefd 100644 --- a/tests/freqai/test_freqai_backtesting.py +++ b/tests/freqai/test_freqai_backtesting.py @@ -26,7 +26,7 @@ def test_freqai_backtest_start_backtest_list(freqai_conf, mocker, testdatadir, c '--config', 'config.json', '--datadir', str(testdatadir), '--strategy-path', str(Path(__file__).parents[1] / 'strategy/strats'), - '--timeframe', '1h', + '--timeframe', '1m', '--strategy-list', CURRENT_TEST_STRATEGY ] args = get_args(args) diff --git a/tests/freqai/test_freqai_datakitchen.py b/tests/freqai/test_freqai_datakitchen.py index f60b29bf1..f1203877e 100644 --- a/tests/freqai/test_freqai_datakitchen.py +++ b/tests/freqai/test_freqai_datakitchen.py @@ -125,7 +125,8 @@ def test_normalize_data(mocker, freqai_conf): freqai = make_data_dictionary(mocker, freqai_conf) data_dict = freqai.dk.data_dictionary freqai.dk.normalize_data(data_dict) - assert len(freqai.dk.data) == 32 + assert any('_max' in entry for entry in freqai.dk.data.keys()) + assert any('_min' in entry for entry in freqai.dk.data.keys()) def test_filter_features(mocker, freqai_conf): diff --git a/tests/freqai/test_freqai_interface.py b/tests/freqai/test_freqai_interface.py index 40a573547..76dcd1c2f 100644 --- a/tests/freqai/test_freqai_interface.py +++ b/tests/freqai/test_freqai_interface.py @@ -30,6 +30,7 @@ def is_mac() -> bool: @pytest.mark.parametrize('model', [ 'LightGBMRegressor', 'XGBoostRegressor', + 'XGBoostRFRegressor', 'CatboostRegressor', 'ReinforcementLearner', 'ReinforcementLearner_multiproc', @@ -69,10 +70,17 @@ def test_extract_data_and_train_model_Standard(mocker, freqai_conf, model): data_load_timerange = TimeRange.parse_timerange("20180125-20180130") new_timerange = TimeRange.parse_timerange("20180127-20180130") + freqai.dk.set_paths('ADA/BTC', None) + freqai.train_timer("start", "ADA/BTC") freqai.extract_data_and_train_model( new_timerange, "ADA/BTC", strategy, freqai.dk, data_load_timerange) + freqai.train_timer("stop", "ADA/BTC") + freqai.dd.save_metric_tracker_to_disk() + freqai.dd.save_drawer_to_disk() + assert Path(freqai.dk.full_path / "metric_tracker.json").is_file() + assert Path(freqai.dk.full_path / "pair_dictionary.json").is_file() assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_model.{model_save_ext}").is_file() assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_metadata.json").is_file() @@ -107,6 +115,7 @@ def test_extract_data_and_train_model_MultiTargets(mocker, freqai_conf, model): data_load_timerange = TimeRange.parse_timerange("20180110-20180130") new_timerange = TimeRange.parse_timerange("20180120-20180130") + freqai.dk.set_paths('ADA/BTC', None) freqai.extract_data_and_train_model( new_timerange, "ADA/BTC", strategy, freqai.dk, data_load_timerange) @@ -125,6 +134,7 @@ def test_extract_data_and_train_model_MultiTargets(mocker, freqai_conf, model): 'LightGBMClassifier', 'CatboostClassifier', 'XGBoostClassifier', + 'XGBoostRFClassifier', ]) def test_extract_data_and_train_model_Classifiers(mocker, freqai_conf, model): if is_arm() and model == 'CatboostClassifier': @@ -148,6 +158,7 @@ def test_extract_data_and_train_model_Classifiers(mocker, freqai_conf, model): data_load_timerange = TimeRange.parse_timerange("20180110-20180130") new_timerange = TimeRange.parse_timerange("20180120-20180130") + freqai.dk.set_paths('ADA/BTC', None) freqai.extract_data_and_train_model(new_timerange, "ADA/BTC", strategy, freqai.dk, data_load_timerange) @@ -172,7 +183,7 @@ def test_extract_data_and_train_model_Classifiers(mocker, freqai_conf, model): ("CatboostClassifier", 6, "freqai_test_classifier") ], ) -def test_start_backtesting(mocker, freqai_conf, model, num_files, strat): +def test_start_backtesting(mocker, freqai_conf, model, num_files, strat, caplog): freqai_conf.get("freqai", {}).update({"save_backtest_models": True}) freqai_conf['runmode'] = RunMode.BACKTEST if is_arm() and "Catboost" in model: @@ -205,6 +216,9 @@ def test_start_backtesting(mocker, freqai_conf, model, num_files, strat): corr_df, base_df = freqai.dd.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC", freqai.dk) df = freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, "LTC/BTC") + for i in range(5): + df[f'%-constant_{i}'] = i + # df.loc[:, f'%-constant_{i}'] = i metadata = {"pair": "LTC/BTC"} freqai.start_backtesting(df, metadata, freqai.dk) @@ -212,6 +226,14 @@ def test_start_backtesting(mocker, freqai_conf, model, num_files, strat): assert len(model_folders) == num_files Trade.use_db = True + assert log_has_re( + "Removed features ", + caplog, + ) + assert log_has_re( + "Removed 5 features from prediction features, ", + caplog, + ) Backtesting.cleanup() shutil.rmtree(Path(freqai.dk.full_path)) @@ -237,6 +259,7 @@ def test_start_backtesting_subdaily_backtest_period(mocker, freqai_conf): metadata = {"pair": "LTC/BTC"} freqai.start_backtesting(df, metadata, freqai.dk) model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()] + assert len(model_folders) == 9 shutil.rmtree(Path(freqai.dk.full_path)) @@ -281,6 +304,7 @@ def test_start_backtesting_from_existing_folder(mocker, freqai_conf, caplog): corr_df, base_df = freqai.dd.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC", freqai.dk) df = freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, "LTC/BTC") + freqai.start_backtesting(df, metadata, freqai.dk) assert log_has_re( @@ -337,6 +361,7 @@ def test_follow_mode(mocker, freqai_conf): freqai.dd.load_all_pair_histories(timerange, freqai.dk) df = strategy.dp.get_pair_dataframe('ADA/BTC', '5m') + freqai.start_live(df, metadata, strategy, freqai.dk) assert len(freqai.dk.return_dataframe.index) == 5702 diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index 907e97fb7..290e08455 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -97,7 +97,6 @@ def _make_backtest_conf(mocker, datadir, conf=None, pair='UNITTEST/BTC'): 'start_date': min_date, 'end_date': max_date, 'max_open_trades': 10, - 'position_stacking': False, } @@ -735,7 +734,6 @@ def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None: start_date=min_date, end_date=max_date, max_open_trades=10, - position_stacking=False, ) results = result['results'] assert not results.empty @@ -799,6 +797,34 @@ def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None: t["close_rate"], 6) < round(ln.iloc[0]["high"], 6)) +def test_backtest_timedout_entry_orders(default_conf, fee, mocker, testdatadir) -> None: + # This strategy intentionally places unfillable orders. + default_conf['strategy'] = 'StrategyTestV3CustomEntryPrice' + default_conf['startup_candle_count'] = 0 + # Cancel unfilled order after 4 minutes on 5m timeframe. + default_conf["unfilledtimeout"] = {"entry": 4} + mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) + mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) + mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=float('inf')) + patch_exchange(mocker) + backtesting = Backtesting(default_conf) + backtesting._set_strategy(backtesting.strategylist[0]) + # Testing dataframe contains 11 candles. Expecting 10 timed out orders. + timerange = TimeRange('date', 'date', 1517227800, 1517231100) + data = history.load_data(datadir=testdatadir, timeframe='5m', pairs=['UNITTEST/BTC'], + timerange=timerange) + min_date, max_date = get_timerange(data) + + result = backtesting.backtest( + processed=deepcopy(data), + start_date=min_date, + end_date=max_date, + max_open_trades=1, + ) + + assert result['timedout_entry_orders'] == 10 + + def test_backtest_1min_timeframe(default_conf, fee, mocker, testdatadir) -> None: default_conf['use_exit_signal'] = False mocker.patch('freqtrade.exchange.Exchange.get_fee', fee) @@ -819,7 +845,6 @@ def test_backtest_1min_timeframe(default_conf, fee, mocker, testdatadir) -> None start_date=min_date, end_date=max_date, max_open_trades=1, - position_stacking=False, ) assert not results['results'].empty assert len(results['results']) == 1 @@ -851,7 +876,6 @@ def test_backtest_trim_no_data_left(default_conf, fee, mocker, testdatadir) -> N start_date=min_date, end_date=max_date, max_open_trades=10, - position_stacking=False, ) @@ -906,7 +930,6 @@ def test_backtest_dataprovider_analyzed_df(default_conf, fee, mocker, testdatadi start_date=min_date, end_date=max_date, max_open_trades=10, - position_stacking=False, ) assert count == 5 @@ -950,8 +973,6 @@ def test_backtest_pricecontours_protections(default_conf, fee, mocker, testdatad start_date=min_date, end_date=max_date, max_open_trades=1, - position_stacking=False, - enable_protections=default_conf.get('enable_protections', False), ) assert len(results['results']) == numres @@ -994,8 +1015,6 @@ def test_backtest_pricecontours(default_conf, fee, mocker, testdatadir, start_date=min_date, end_date=max_date, max_open_trades=1, - position_stacking=False, - enable_protections=default_conf.get('enable_protections', False), ) assert len(results['results']) == expected @@ -1107,7 +1126,6 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) 'start_date': min_date, 'end_date': max_date, 'max_open_trades': 3, - 'position_stacking': False, } results = backtesting.backtest(**backtest_conf) @@ -1130,7 +1148,6 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) 'start_date': min_date, 'end_date': max_date, 'max_open_trades': 1, - 'position_stacking': False, } results = backtesting.backtest(**backtest_conf) assert len(evaluate_result_multi(results['results'], '5m', 1)) == 0 diff --git a/tests/optimize/test_backtesting_adjust_position.py b/tests/optimize/test_backtesting_adjust_position.py index 99c160a40..135ec6b15 100644 --- a/tests/optimize/test_backtesting_adjust_position.py +++ b/tests/optimize/test_backtesting_adjust_position.py @@ -42,7 +42,6 @@ def test_backtest_position_adjustment(default_conf, fee, mocker, testdatadir) -> start_date=min_date, end_date=max_date, max_open_trades=10, - position_stacking=False, ) results = result['results'] assert not results.empty diff --git a/tests/optimize/test_hyperopt.py b/tests/optimize/test_hyperopt.py index 5666ebabc..5bce9f419 100644 --- a/tests/optimize/test_hyperopt.py +++ b/tests/optimize/test_hyperopt.py @@ -336,7 +336,7 @@ def test_start_calls_optimizer(mocker, hyperopt_conf, capsys) -> None: assert hasattr(hyperopt.backtesting.strategy, "advise_entry") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] - assert hasattr(hyperopt, "position_stacking") + assert hasattr(hyperopt.backtesting, "_position_stacking") def test_hyperopt_format_results(hyperopt): @@ -704,7 +704,7 @@ def test_simplified_interface_roi_stoploss(mocker, hyperopt_conf, capsys) -> Non assert hasattr(hyperopt.backtesting.strategy, "advise_entry") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] - assert hasattr(hyperopt, "position_stacking") + assert hasattr(hyperopt.backtesting, "_position_stacking") def test_simplified_interface_all_failed(mocker, hyperopt_conf, caplog) -> None: @@ -778,7 +778,7 @@ def test_simplified_interface_buy(mocker, hyperopt_conf, capsys) -> None: assert hasattr(hyperopt.backtesting.strategy, "advise_entry") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] - assert hasattr(hyperopt, "position_stacking") + assert hasattr(hyperopt.backtesting, "_position_stacking") def test_simplified_interface_sell(mocker, hyperopt_conf, capsys) -> None: @@ -821,7 +821,7 @@ def test_simplified_interface_sell(mocker, hyperopt_conf, capsys) -> None: assert hasattr(hyperopt.backtesting.strategy, "advise_entry") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades'] - assert hasattr(hyperopt, "position_stacking") + assert hasattr(hyperopt.backtesting, "_position_stacking") @pytest.mark.parametrize("space", [ @@ -910,8 +910,9 @@ def test_in_strategy_auto_hyperopt_with_parallel(mocker, hyperopt_conf, tmpdir, }) hyperopt = Hyperopt(hyperopt_conf) hyperopt.backtesting.exchange.get_max_leverage = lambda *x, **xx: 1.0 - hyperopt.backtesting.exchange.get_min_pair_stake_amount = lambda *x, **xx: 1.0 + hyperopt.backtesting.exchange.get_min_pair_stake_amount = lambda *x, **xx: 0.00001 hyperopt.backtesting.exchange.get_max_pair_stake_amount = lambda *x, **xx: 100.0 + hyperopt.backtesting.exchange._markets = get_markets() assert isinstance(hyperopt.custom_hyperopt, HyperOptAuto) assert isinstance(hyperopt.backtesting.strategy.buy_rsi, IntParameter) diff --git a/tests/persistence/__init__.py b/tests/persistence/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_persistence.py b/tests/persistence/test_persistence.py similarity index 99% rename from tests/test_persistence.py rename to tests/persistence/test_persistence.py index e7f218c02..3323dd7c6 100644 --- a/tests/test_persistence.py +++ b/tests/persistence/test_persistence.py @@ -2404,8 +2404,10 @@ def test_Trade_object_idem(): 'get_enter_tag_performance', 'get_mix_tag_performance', 'get_trading_volume', - + 'from_json', ) + EXCLUDES2 = ('trades', 'trades_open', 'bt_trades_open_pp', 'bt_open_open_trade_count', + 'total_profit') # Parent (LocalTrade) should have the same attributes for item in trade: @@ -2416,7 +2418,7 @@ def test_Trade_object_idem(): # Fails if only a column is added without corresponding parent field for item in localtrade: if (not item.startswith('__') - and item not in ('trades', 'trades_open', 'total_profit') + and item not in EXCLUDES2 and type(getattr(LocalTrade, item)) not in (property, FunctionType)): assert item in trade diff --git a/tests/persistence/test_trade_fromjson.py b/tests/persistence/test_trade_fromjson.py new file mode 100644 index 000000000..529008e02 --- /dev/null +++ b/tests/persistence/test_trade_fromjson.py @@ -0,0 +1,181 @@ +from datetime import datetime, timezone + +from freqtrade.persistence.trade_model import Trade + + +def test_trade_fromjson(): + """Test the Trade.from_json() method.""" + trade_string = """{ + "trade_id": 25, + "pair": "ETH/USDT", + "base_currency": "ETH", + "quote_currency": "USDT", + "is_open": false, + "exchange": "binance", + "amount": 407.0, + "amount_requested": 102.92547026, + "stake_amount": 102.7494348, + "strategy": "SampleStrategy55", + "buy_tag": "Strategy2", + "enter_tag": "Strategy2", + "timeframe": 5, + "fee_open": 0.001, + "fee_open_cost": 0.1027494, + "fee_open_currency": "ETH", + "fee_close": 0.001, + "fee_close_cost": 0.1054944, + "fee_close_currency": "USDT", + "open_date": "2022-10-18 09:12:42", + "open_timestamp": 1666084362912, + "open_rate": 0.2518998249562391, + "open_rate_requested": 0.2516, + "open_trade_value": 102.62575199, + "close_date": "2022-10-18 09:45:22", + "close_timestamp": 1666086322208, + "realized_profit": 2.76315361, + "close_rate": 0.2592, + "close_rate_requested": 0.2592, + "close_profit": 0.026865, + "close_profit_pct": 2.69, + "close_profit_abs": 2.76315361, + "trade_duration_s": 1959, + "trade_duration": 32, + "profit_ratio": 0.02686, + "profit_pct": 2.69, + "profit_abs": 2.76315361, + "sell_reason": "no longer good", + "exit_reason": "no longer good", + "exit_order_status": "closed", + "stop_loss_abs": 0.1981, + "stop_loss_ratio": -0.216, + "stop_loss_pct": -21.6, + "stoploss_order_id": null, + "stoploss_last_update": null, + "stoploss_last_update_timestamp": null, + "initial_stop_loss_abs": 0.1981, + "initial_stop_loss_ratio": -0.216, + "initial_stop_loss_pct": -21.6, + "min_rate": 0.2495, + "max_rate": 0.2592, + "leverage": 1.0, + "interest_rate": 0.0, + "liquidation_price": null, + "is_short": false, + "trading_mode": "spot", + "funding_fees": 0.0, + "open_order_id": null, + "orders": [ + { + "amount": 102.0, + "safe_price": 0.2526, + "ft_order_side": "buy", + "order_filled_timestamp": 1666084370887, + "ft_is_entry": true, + "pair": "ETH/USDT", + "order_id": "78404228", + "status": "closed", + "average": 0.2526, + "cost": 25.7652, + "filled": 102.0, + "is_open": false, + "order_date": "2022-10-18 09:12:42", + "order_timestamp": 1666084362684, + "order_filled_date": "2022-10-18 09:12:50", + "order_type": "limit", + "price": 0.2526, + "remaining": 0.0 + }, + { + "amount": 102.0, + "safe_price": 0.2517, + "ft_order_side": "buy", + "order_filled_timestamp": 1666084379056, + "ft_is_entry": true, + "pair": "ETH/USDT", + "order_id": "78405139", + "status": "closed", + "average": 0.2517, + "cost": 25.6734, + "filled": 102.0, + "is_open": false, + "order_date": "2022-10-18 09:12:57", + "order_timestamp": 1666084377681, + "order_filled_date": "2022-10-18 09:12:59", + "order_type": "limit", + "price": 0.2517, + "remaining": 0.0 + }, + { + "amount": 102.0, + "safe_price": 0.2517, + "ft_order_side": "buy", + "order_filled_timestamp": 1666084389644, + "ft_is_entry": true, + "pair": "ETH/USDT", + "order_id": "78405265", + "status": "closed", + "average": 0.2517, + "cost": 25.6734, + "filled": 102.0, + "is_open": false, + "order_date": "2022-10-18 09:13:03", + "order_timestamp": 1666084383295, + "order_filled_date": "2022-10-18 09:13:09", + "order_type": "limit", + "price": 0.2517, + "remaining": 0.0 + }, + { + "amount": 102.0, + "safe_price": 0.2516, + "ft_order_side": "buy", + "order_filled_timestamp": 1666084723521, + "ft_is_entry": true, + "pair": "ETH/USDT", + "order_id": "78405395", + "status": "closed", + "average": 0.2516, + "cost": 25.6632, + "filled": 102.0, + "is_open": false, + "order_date": "2022-10-18 09:13:13", + "order_timestamp": 1666084393920, + "order_filled_date": "2022-10-18 09:18:43", + "order_type": "limit", + "price": 0.2516, + "remaining": 0.0 + }, + { + "amount": 407.0, + "safe_price": 0.2592, + "ft_order_side": "sell", + "order_filled_timestamp": 1666086322198, + "ft_is_entry": false, + "pair": "ETH/USDT", + "order_id": "78432649", + "status": "closed", + "average": 0.2592, + "cost": 105.4944, + "filled": 407.0, + "is_open": false, + "order_date": "2022-10-18 09:45:21", + "order_timestamp": 1666086321435, + "order_filled_date": "2022-10-18 09:45:22", + "order_type": "market", + "price": 0.2592, + "remaining": 0.0 + } + ] + }""" + trade = Trade.from_json(trade_string) + + assert trade.id == 25 + assert trade.pair == 'ETH/USDT' + assert trade.open_date == datetime(2022, 10, 18, 9, 12, 42, tzinfo=timezone.utc) + assert isinstance(trade.open_date, datetime) + assert trade.exit_reason == 'no longer good' + + assert len(trade.orders) == 5 + last_o = trade.orders[-1] + assert last_o.order_filled_date == datetime(2022, 10, 18, 9, 45, 22, tzinfo=timezone.utc) + assert isinstance(last_o.order_date, datetime) diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index c0837755a..711bf140d 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1443,8 +1443,9 @@ def test_api_plot_config(botclient): assert isinstance(rc.json()['subplots'], dict) -def test_api_strategies(botclient): +def test_api_strategies(botclient, tmpdir): ftbot, client = botclient + ftbot.config['user_data_dir'] = Path(tmpdir) rc = client_get(client, f"{BASE_URI}/strategies") @@ -1456,6 +1457,7 @@ def test_api_strategies(botclient): 'InformativeDecoratorTest', 'StrategyTestV2', 'StrategyTestV3', + 'StrategyTestV3CustomEntryPrice', 'StrategyTestV3Futures', 'freqai_rl_test_strat', 'freqai_test_classifier', diff --git a/tests/rpc/test_rpc_manager.py b/tests/rpc/test_rpc_manager.py index d71f38259..21c8b0813 100644 --- a/tests/rpc/test_rpc_manager.py +++ b/tests/rpc/test_rpc_manager.py @@ -99,6 +99,7 @@ def test_send_msg_telegram_error(mocker, default_conf, caplog) -> None: def test_process_msg_queue(mocker, default_conf, caplog) -> None: telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg') + default_conf['telegram']['allow_custom_messages'] = True mocker.patch('freqtrade.rpc.telegram.Telegram._init') freqtradebot = get_patched_freqtradebot(mocker, default_conf) @@ -108,8 +109,8 @@ def test_process_msg_queue(mocker, default_conf, caplog) -> None: queue.append('Test message 2') rpc_manager.process_msg_queue(queue) - assert log_has("Sending rpc message: {'type': strategy_msg, 'msg': 'Test message'}", caplog) - assert log_has("Sending rpc message: {'type': strategy_msg, 'msg': 'Test message 2'}", caplog) + assert log_has("Sending rpc strategy_msg: Test message", caplog) + assert log_has("Sending rpc strategy_msg: Test message 2", caplog) assert telegram_mock.call_count == 2 diff --git a/tests/rpc/test_rpc_webhook.py b/tests/rpc/test_rpc_webhook.py index 3bbb85d54..a8fd0c34b 100644 --- a/tests/rpc/test_rpc_webhook.py +++ b/tests/rpc/test_rpc_webhook.py @@ -3,7 +3,6 @@ from datetime import datetime, timedelta from unittest.mock import MagicMock -import pytest from requests import RequestException from freqtrade.enums import ExitType, RPCMessageType @@ -337,34 +336,18 @@ def test_exception_send_msg(default_conf, mocker, caplog): caplog) default_conf["webhook"] = get_webhook_dict() - default_conf["webhook"]["webhookentry"]["value1"] = "{DEADBEEF:8f}" + default_conf["webhook"]["strategy_msg"] = {"value1": "{DEADBEEF:8f}"} msg_mock = MagicMock() mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) msg = { - 'type': RPCMessageType.ENTRY, - 'exchange': 'Binance', - 'pair': 'ETH/BTC', - 'limit': 0.005, - 'order_type': 'limit', - 'stake_amount': 0.8, - 'stake_amount_fiat': 500, - 'stake_currency': 'BTC', - 'fiat_currency': 'EUR' + 'type': RPCMessageType.STRATEGY_MSG, + 'msg': 'hello world', } webhook.send_msg(msg) assert log_has("Problem calling Webhook. Please check your webhook configuration. " "Exception: 'DEADBEEF'", caplog) - msg_mock = MagicMock() - mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) - msg = { - 'type': 'DEADBEEF', - 'status': 'whatever' - } - with pytest.raises(NotImplementedError): - webhook.send_msg(msg) - # Test no failure for not implemented but known messagetypes for e in RPCMessageType: msg = { diff --git a/tests/strategy/strats/strategy_test_v3_custom_entry_price.py b/tests/strategy/strats/strategy_test_v3_custom_entry_price.py new file mode 100644 index 000000000..872984156 --- /dev/null +++ b/tests/strategy/strats/strategy_test_v3_custom_entry_price.py @@ -0,0 +1,37 @@ +# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement + +from datetime import datetime +from typing import Optional + +from pandas import DataFrame +from strategy_test_v3 import StrategyTestV3 + + +class StrategyTestV3CustomEntryPrice(StrategyTestV3): + """ + Strategy used by tests freqtrade bot. + Please do not modify this strategy, it's intended for internal use only. + Please look at the SampleStrategy in the user_data/strategy directory + or strategy repository https://github.com/freqtrade/freqtrade-strategies + for samples and inspiration. + """ + new_entry_price: float = 0.001 + + def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + return dataframe + + def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + + dataframe.loc[ + dataframe['volume'] > 0, + 'enter_long'] = 1 + + return dataframe + + def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + return dataframe + + def custom_entry_price(self, pair: str, current_time: datetime, proposed_rate: float, + entry_tag: Optional[str], side: str, **kwargs) -> float: + + return self.new_entry_price diff --git a/tests/strategy/test_strategy_helpers.py b/tests/strategy/test_strategy_helpers.py index 8cb990e87..f42f9e681 100644 --- a/tests/strategy/test_strategy_helpers.py +++ b/tests/strategy/test_strategy_helpers.py @@ -5,29 +5,8 @@ import pytest from freqtrade.data.dataprovider import DataProvider from freqtrade.enums import CandleType from freqtrade.resolvers.strategy_resolver import StrategyResolver -from freqtrade.strategy import (merge_informative_pair, stoploss_from_absolute, stoploss_from_open, - timeframe_to_minutes) -from tests.conftest import get_patched_exchange - - -def generate_test_data(timeframe: str, size: int, start: str = '2020-07-05'): - np.random.seed(42) - tf_mins = timeframe_to_minutes(timeframe) - - base = np.random.normal(20, 2, size=size) - - date = pd.date_range(start, periods=size, freq=f'{tf_mins}min', tz='UTC') - df = pd.DataFrame({ - 'date': date, - 'open': base, - 'high': base + np.random.normal(2, 1, size=size), - 'low': base - np.random.normal(2, 1, size=size), - 'close': base + np.random.normal(0, 1, size=size), - 'volume': np.random.normal(200, size=size) - } - ) - df = df.dropna() - return df +from freqtrade.strategy import merge_informative_pair, stoploss_from_absolute, stoploss_from_open +from tests.conftest import generate_test_data, get_patched_exchange def test_merge_informative_pair(): diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 9c1494576..6b831c116 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -32,24 +32,25 @@ def test_search_strategy(): def test_search_all_strategies_no_failed(): directory = Path(__file__).parent / "strats" - strategies = StrategyResolver.search_all_objects(directory, enum_failed=False) + strategies = StrategyResolver._search_all_objects(directory, enum_failed=False) assert isinstance(strategies, list) - assert len(strategies) == 10 + assert len(strategies) == 11 assert isinstance(strategies[0], dict) def test_search_all_strategies_with_failed(): directory = Path(__file__).parent / "strats" - strategies = StrategyResolver.search_all_objects(directory, enum_failed=True) + strategies = StrategyResolver._search_all_objects(directory, enum_failed=True) assert isinstance(strategies, list) - assert len(strategies) == 11 + assert len(strategies) == 12 # with enum_failed=True search_all_objects() shall find 2 good strategies # and 1 which fails to load - assert len([x for x in strategies if x['class'] is not None]) == 10 + assert len([x for x in strategies if x['class'] is not None]) == 11 + assert len([x for x in strategies if x['class'] is None]) == 1 directory = Path(__file__).parent / "strats_nonexistingdir" - strategies = StrategyResolver.search_all_objects(directory, enum_failed=True) + strategies = StrategyResolver._search_all_objects(directory, enum_failed=True) assert len(strategies) == 0 @@ -77,10 +78,9 @@ def test_load_strategy_base64(dataframe_1m, caplog, default_conf): def test_load_strategy_invalid_directory(caplog, default_conf): - default_conf['strategy'] = 'StrategyTestV3' extra_dir = Path.cwd() / 'some/path' - with pytest.raises(OperationalException): - StrategyResolver._load_strategy(CURRENT_TEST_STRATEGY, config=default_conf, + with pytest.raises(OperationalException, match=r"Impossible to load Strategy.*"): + StrategyResolver._load_strategy('StrategyTestV333', config=default_conf, extra_dir=extra_dir) assert log_has_re(r'Path .*' + r'some.*path.*' + r'.* does not exist', caplog) @@ -102,8 +102,8 @@ def test_load_strategy_noname(default_conf): StrategyResolver.load_strategy(default_conf) -@pytest.mark.filterwarnings("ignore:deprecated") -@pytest.mark.parametrize('strategy_name', ['StrategyTestV2']) +@ pytest.mark.filterwarnings("ignore:deprecated") +@ pytest.mark.parametrize('strategy_name', ['StrategyTestV2']) def test_strategy_pre_v3(dataframe_1m, default_conf, strategy_name): default_conf.update({'strategy': strategy_name}) @@ -349,7 +349,7 @@ def test_strategy_override_use_exit_profit_only(caplog, default_conf): assert log_has("Override strategy 'exit_profit_only' with value in config file: True.", caplog) -@pytest.mark.filterwarnings("ignore:deprecated") +@ pytest.mark.filterwarnings("ignore:deprecated") def test_missing_implements(default_conf, caplog): default_location = Path(__file__).parent / "strats" diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 2336e3585..ed3c84b1e 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -1028,6 +1028,31 @@ def test__validate_pricing_rules(default_conf, caplog) -> None: validate_config_consistency(conf) +def test__validate_freqai_include_timeframes(default_conf, caplog) -> None: + conf = deepcopy(default_conf) + conf.update({ + "freqai": { + "enabled": True, + "feature_parameters": { + "include_timeframes": ["1m", "5m"], + "include_corr_pairlist": [], + }, + "data_split_parameters": {}, + "model_training_parameters": {} + } + }) + with pytest.raises(OperationalException, match=r"Main timeframe of .*"): + validate_config_consistency(conf) + # Validation pass + conf.update({'timeframe': '1m'}) + validate_config_consistency(conf) + conf.update({'analyze_per_epoch': True}) + + with pytest.raises(OperationalException, + match=r"Using analyze-per-epoch .* not supported with a FreqAI strategy."): + validate_config_consistency(conf) + + def test__validate_consumers(default_conf, caplog) -> None: conf = deepcopy(default_conf) conf.update({ diff --git a/tests/test_directory_operations.py b/tests/test_directory_operations.py index 905b078f9..8e49aab10 100644 --- a/tests/test_directory_operations.py +++ b/tests/test_directory_operations.py @@ -25,7 +25,7 @@ def test_create_userdata_dir(mocker, default_conf, caplog) -> None: md = mocker.patch.object(Path, 'mkdir', MagicMock()) x = create_userdata_dir('/tmp/bar', create_dir=True) - assert md.call_count == 9 + assert md.call_count == 10 assert md.call_args[1]['parents'] is False assert log_has(f'Created user-data directory: {Path("/tmp/bar")}', caplog) assert isinstance(x, Path) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index c127e3850..a0d38563e 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -3969,15 +3969,17 @@ def test__safe_exit_amount(default_conf_usdt, fee, caplog, mocker, amount_wallet patch_get_signal(freqtrade) if has_err: with pytest.raises(DependencyException, match=r"Not enough amount to exit trade."): - assert freqtrade._safe_exit_amount(trade.pair, trade.amount) + assert freqtrade._safe_exit_amount(trade, trade.pair, trade.amount) else: wallet_update.reset_mock() - assert freqtrade._safe_exit_amount(trade.pair, trade.amount) == amount_wallet + assert trade.amount != amount_wallet + assert freqtrade._safe_exit_amount(trade, trade.pair, trade.amount) == amount_wallet assert log_has_re(r'.*Falling back to wallet-amount.', caplog) + assert trade.amount == amount_wallet assert wallet_update.call_count == 1 caplog.clear() wallet_update.reset_mock() - assert freqtrade._safe_exit_amount(trade.pair, amount_wallet) == amount_wallet + assert freqtrade._safe_exit_amount(trade, trade.pair, amount_wallet) == amount_wallet assert not log_has_re(r'.*Falling back to wallet-amount.', caplog) assert wallet_update.call_count == 1 diff --git a/tests/test_integration.py b/tests/test_integration.py index f2504c23a..01a2801ad 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -420,7 +420,7 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) assert trade.open_order_id is None # Open rate is not adjusted yet assert trade.open_rate == 1.99 - assert trade.stake_amount == 60 + assert pytest.approx(trade.stake_amount) == 60 assert trade.stop_loss_pct == -0.1 assert pytest.approx(trade.stop_loss) == 1.99 * (1 - 0.1 / leverage) assert pytest.approx(trade.initial_stop_loss) == 1.99 * (1 - 0.1 / leverage) @@ -446,7 +446,7 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) assert len(trade.orders) == 4 assert trade.open_order_id is not None assert trade.open_rate == 1.99 - assert trade.stake_amount == 60 + assert pytest.approx(trade.stake_amount) == 60 assert trade.orders[-1].price == 1.95 assert pytest.approx(trade.orders[-1].cost) == 120 * leverage diff --git a/tests/test_worker.py b/tests/test_worker.py index ddca9525b..ae511852f 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -1,7 +1,10 @@ import logging import time +from datetime import timedelta from unittest.mock import MagicMock, PropertyMock +import time_machine + from freqtrade.data.dataprovider import DataProvider from freqtrade.enums import State from freqtrade.worker import Worker @@ -59,13 +62,58 @@ def test_throttle(mocker, default_conf, caplog) -> None: end = time.time() assert result == 42 - assert end - start > 0.1 + assert 0.3 > end - start > 0.1 assert log_has_re(r"Throttling with 'throttled_func\(\)': sleep for \d\.\d{2} s.*", caplog) result = worker._throttle(throttled_func, throttle_secs=-1) assert result == 42 +def test_throttle_sleep_time(mocker, default_conf, caplog) -> None: + + caplog.set_level(logging.DEBUG) + worker = get_patched_worker(mocker, default_conf) + sleep_mock = mocker.patch("freqtrade.worker.Worker._sleep") + with time_machine.travel("2022-09-01 05:00:00 +00:00") as t: + def throttled_func(x=1): + t.shift(timedelta(seconds=x)) + return 42 + + assert worker._throttle(throttled_func, throttle_secs=5) == 42 + # This moves the clock by 1 second + assert sleep_mock.call_count == 1 + assert 3.8 < sleep_mock.call_args[0][0] < 4.1 + + sleep_mock.reset_mock() + # This moves the clock by 1 second + assert worker._throttle(throttled_func, throttle_secs=10) == 42 + assert sleep_mock.call_count == 1 + assert 8.8 < sleep_mock.call_args[0][0] < 9.1 + + sleep_mock.reset_mock() + # This moves the clock by 5 second, so we only throttle by 5s + assert worker._throttle(throttled_func, throttle_secs=10, x=5) == 42 + assert sleep_mock.call_count == 1 + assert 4.8 < sleep_mock.call_args[0][0] < 5.1 + + t.move_to("2022-09-01 05:01:00 +00:00") + sleep_mock.reset_mock() + # Throttle for more than 5m (1 timeframe) + assert worker._throttle(throttled_func, throttle_secs=400, x=5) == 42 + assert sleep_mock.call_count == 1 + assert 394.8 < sleep_mock.call_args[0][0] < 395.1 + + t.move_to("2022-09-01 05:01:00 +00:00") + + sleep_mock.reset_mock() + # Throttle for more than 5m (1 timeframe) + assert worker._throttle(throttled_func, throttle_secs=400, timeframe='5m', + timeframe_offset=0.4, x=5) == 42 + assert sleep_mock.call_count == 1 + # 300 (5m) - 60 (1m - see set time above) - 5 (duration of throttled_func) = 235 + assert 235.2 < sleep_mock.call_args[0][0] < 235.6 + + def test_throttle_with_assets(mocker, default_conf) -> None: def throttled_func(nb_assets=-1): return nb_assets