Compare commits

..

475 Commits

Author SHA1 Message Date
Matthias
cb37166086 Merge pull request #3731 from freqtrade/release_2020.8
Release 2020.8
2020-08-31 06:52:49 +02:00
Matthias
77f2d46e29 Version bump to 2020.8 2020-08-30 10:11:57 +02:00
Matthias
b8aa07a6e8 Merge branch 'master' into release_2020.8 2020-08-30 10:11:45 +02:00
Matthias
2ae04af694 Improve some doc wording 2020-08-29 10:26:26 +02:00
Matthias
a9e7ee8113 Merge pull request #3683 from freqtrade/logging_endpoints
Logging endpoints
2020-08-27 15:11:17 +02:00
Matthias
dc6d71f651 Improve comment formatting 2020-08-27 14:41:31 +02:00
Matthias
cf719bc5d3 Fix logformat to use epoch timestamp in ms 2020-08-27 12:04:55 +02:00
Matthias
bf5a082358 bufferhandler should log right from the beginning 2020-08-27 11:37:20 +02:00
Matthias
309ea1246a Update config to use single quotes 2020-08-26 20:52:09 +02:00
Matthias
d1fe3c1a3d Merge pull request #3719 from freqtrade/fix/crossed_numpy_types
Allow numpy numbers as comparisons, too
2020-08-26 10:02:55 +02:00
Matthias
9d4ecb625a Allow numpy numbers as comparisons, too 2020-08-26 07:16:29 +02:00
Matthias
21f4aba4e3 Merge pull request #3055 from yazeed/verify_date_on_new_candle_on_get_signal
Verify date on last candle before producing signal
2020-08-25 20:22:48 +02:00
Matthias
605ed90567 Merge pull request #3592 from freqtrade/stoploss_distance
Add stoploss-distance (to current price) to /status output
2020-08-25 19:56:23 +02:00
Matthias
c6ead02da0 Merge pull request #3705 from mschultheiss83/update_bad_exchanges
update bad exchanges
2020-08-25 11:47:51 +02:00
Matthias
3bb69bc1bd Add returns statement to docstring 2020-08-24 17:31:00 +02:00
Matthias
fca11160e4 Improve docstring of is_pair_locked 2020-08-24 17:18:57 +02:00
Matthias
354a406248 Sort imports in interface.py 2020-08-24 11:45:38 +02:00
Matthias
b613fb7bc5 Merge pull request #3707 from freqtrade/update_sandbox_docs
Update sandbox documentation
2020-08-24 11:41:54 +02:00
Matthias
502e21b2cb Add unfilled explanation for sandboxes 2020-08-24 11:17:43 +02:00
Matthias
c272944834 Lock pair until a new candle arrives 2020-08-24 11:09:09 +02:00
Matthias
da097aea6a Merge pull request #3713 from freqtrade/dependabot/pip/develop/sqlalchemy-1.3.19
Bump sqlalchemy from 1.3.18 to 1.3.19
2020-08-24 09:29:30 +02:00
dependabot[bot]
7ece7294b2 Bump sqlalchemy from 1.3.18 to 1.3.19
Bumps [sqlalchemy](https://github.com/sqlalchemy/sqlalchemy) from 1.3.18 to 1.3.19.
- [Release notes](https://github.com/sqlalchemy/sqlalchemy/releases)
- [Changelog](https://github.com/sqlalchemy/sqlalchemy/blob/master/CHANGES)
- [Commits](https://github.com/sqlalchemy/sqlalchemy/commits)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-24 07:18:00 +00:00
Matthias
496a9cfb20 Merge pull request #3714 from freqtrade/dependabot/pip/develop/pandas-1.1.1
Bump pandas from 1.1.0 to 1.1.1
2020-08-24 09:13:02 +02:00
Matthias
c3523daa09 Merge pull request #3711 from freqtrade/dependabot/pip/develop/arrow-0.16.0
Bump arrow from 0.15.8 to 0.16.0
2020-08-24 09:04:42 +02:00
Matthias
c1b464f53c Merge pull request #3712 from freqtrade/dependabot/pip/develop/ccxt-1.33.52
Bump ccxt from 1.33.18 to 1.33.52
2020-08-24 09:01:05 +02:00
Matthias
7953c69922 Merge pull request #3709 from freqtrade/dependabot/pip/develop/pytest-mock-3.3.0
Bump pytest-mock from 3.2.0 to 3.3.0
2020-08-24 08:55:36 +02:00
Matthias
c83d6bd1e2 Merge pull request #3708 from freqtrade/dependabot/pip/develop/mkdocs-material-5.5.8
Bump mkdocs-material from 5.5.7 to 5.5.8
2020-08-24 08:55:09 +02:00
dependabot[bot]
f22fc8ef3e Bump pandas from 1.1.0 to 1.1.1
Bumps [pandas](https://github.com/pandas-dev/pandas) from 1.1.0 to 1.1.1.
- [Release notes](https://github.com/pandas-dev/pandas/releases)
- [Changelog](https://github.com/pandas-dev/pandas/blob/master/RELEASE.md)
- [Commits](https://github.com/pandas-dev/pandas/compare/v1.1.0...v1.1.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-24 06:45:20 +00:00
dependabot[bot]
0e20b8f530 Bump ccxt from 1.33.18 to 1.33.52
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.33.18 to 1.33.52.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.33.18...1.33.52)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-24 06:45:12 +00:00
dependabot[bot]
74c97369d9 Bump arrow from 0.15.8 to 0.16.0
Bumps [arrow](https://github.com/arrow-py/arrow) from 0.15.8 to 0.16.0.
- [Release notes](https://github.com/arrow-py/arrow/releases)
- [Changelog](https://github.com/arrow-py/arrow/blob/master/CHANGELOG.rst)
- [Commits](https://github.com/arrow-py/arrow/compare/0.15.8...0.16.0)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-24 06:44:54 +00:00
dependabot[bot]
4c48fe96ed Bump pytest-mock from 3.2.0 to 3.3.0
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 3.2.0 to 3.3.0.
- [Release notes](https://github.com/pytest-dev/pytest-mock/releases)
- [Changelog](https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst)
- [Commits](https://github.com/pytest-dev/pytest-mock/compare/v3.2.0...v3.3.0)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-24 06:44:52 +00:00
dependabot[bot]
5799cc51f2 Bump mkdocs-material from 5.5.7 to 5.5.8
Bumps [mkdocs-material](https://github.com/squidfunk/mkdocs-material) from 5.5.7 to 5.5.8.
- [Release notes](https://github.com/squidfunk/mkdocs-material/releases)
- [Changelog](https://github.com/squidfunk/mkdocs-material/blob/master/docs/changelog.md)
- [Commits](https://github.com/squidfunk/mkdocs-material/compare/5.5.7...5.5.8)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-24 06:44:50 +00:00
Matthias
8b767eedfd Merge branch 'develop' into pr/yazeed/3055 2020-08-24 07:21:48 +02:00
Matthias
8478e083dc Improve wording of sandbox documentation 2020-08-23 21:16:44 +02:00
Matthias
8940ba828f Update sandbox documentation 2020-08-23 21:14:00 +02:00
Matthias
a55dd8444d Fix loglevel of using_cached-rate 2020-08-23 19:31:35 +02:00
Matthias
ec94961437 Reduce loglevel of "using cached rate" 2020-08-23 19:14:28 +02:00
Matthias
92d8adf36c Merge pull request #3706 from freqtrade/fix_api_dates
Dates should be changed to UTC to provide the correct timestamp
2020-08-23 10:33:02 +02:00
Matthias
05ec56d906 Dates should be changed to UTC to provide the correct timestamp 2020-08-23 10:16:28 +02:00
Matthias
73417f11f1 Fix rendering issue on readthedocs 2020-08-23 09:11:52 +02:00
Martin Schultheiss
2701a7cb12 update bad exchanges 2020-08-23 09:11:34 +02:00
Matthias
c2707bdd9b Merge pull request #3688 from freqtrade/Fredrik81-stoploss.md
Update stoploss.md
2020-08-23 09:09:26 +02:00
Matthias
d8a6410fd1 Fix small bug when using max-open-trades -1 in backtesting 2020-08-23 09:00:57 +02:00
Matthias
637147f89c Update sql cheatsheet parentheses 2020-08-22 09:33:35 +02:00
Fredrik81
0e368b16ab Update stoploss.md 2020-08-21 18:25:45 +02:00
Matthias
3d93236709 Remove unused import 2020-08-21 14:55:47 +02:00
Matthias
301f74fd1b Merge pull request #3418 from freqtrade/hyperopt_colorama_init
Test colorama init again (after the fixes done to progressbar)
2020-08-21 14:54:35 +02:00
Matthias
fa0c8fa0b3 Readd note about windows hyperopt color output 2020-08-21 14:26:23 +02:00
Matthias
c4e597977c Merge pull request #3701 from freqtrade/fix/optimize_reports
Fix bug in backtesting
2020-08-20 20:21:38 +02:00
Matthias
4f1179d85c Test for empty case 2020-08-20 20:11:58 +02:00
Matthias
f5a9001dc0 Handle backtest results without any trades 2020-08-20 19:51:36 +02:00
Matthias
bca24c8b6b Clarify hyperopt dataprovider usage 2020-08-20 19:35:40 +02:00
Fredrik81
55c6e56762 Update stoploss.md 2020-08-19 23:07:03 +02:00
Matthias
42273ae042 Merge pull request #3695 from freqtrade/fix_daily_rpc
Fix daily rpc for webservice
2020-08-19 14:16:24 +02:00
Matthias
3d515ed5bf Merge pull request #3558 from freqtrade/bt_add_maxdrawdown
Revise backtesting export format, add some metrics
2020-08-19 06:39:47 +02:00
Matthias
e206cc9c21 Adjust tests 2020-08-18 20:15:41 +02:00
Matthias
375e671aaf Move formatting of /daily to telegram
so /daily can return numbers in the API
2020-08-18 20:12:14 +02:00
Matthias
d8e1f97465 Fix documentation typo 2020-08-18 19:44:44 +02:00
Matthias
9982ad2f36 Add profit to backtest summary output 2020-08-18 16:59:24 +02:00
Matthias
668d167adc Add docstring to store_backtest_stats 2020-08-18 16:15:24 +02:00
Matthias
4eb17b4daf Remove unneeded function 2020-08-18 15:20:37 +02:00
Matthias
aa866294cd Reformulate documentation 2020-08-18 14:02:22 +02:00
Matthias
ce15c55185 Add libffi-dev to rpi image 2020-08-17 20:24:30 +02:00
Matthias
e0fa549bb4 Merge pull request #3692 from freqtrade/dependabot/pip/develop/prompt-toolkit-3.0.6
Bump prompt-toolkit from 3.0.5 to 3.0.6
2020-08-17 09:08:59 +02:00
Matthias
53ea6e0a86 Merge pull request #3693 from freqtrade/dependabot/pip/develop/coveralls-2.1.2
Bump coveralls from 2.1.1 to 2.1.2
2020-08-17 09:08:41 +02:00
Matthias
0f5dd6d0fa Merge pull request #3690 from freqtrade/dependabot/pip/develop/pytest-cov-2.10.1
Bump pytest-cov from 2.10.0 to 2.10.1
2020-08-17 08:58:38 +02:00
Matthias
70e213751d Merge pull request #3691 from freqtrade/dependabot/pip/develop/ccxt-1.33.18
Bump ccxt from 1.32.88 to 1.33.18
2020-08-17 08:58:00 +02:00
Matthias
161417b6f4 Merge pull request #3689 from freqtrade/dependabot/pip/develop/mkdocs-material-5.5.7
Bump mkdocs-material from 5.5.3 to 5.5.7
2020-08-17 08:57:41 +02:00
dependabot[bot]
30a2df14cb Bump coveralls from 2.1.1 to 2.1.2
Bumps [coveralls](https://github.com/coveralls-clients/coveralls-python) from 2.1.1 to 2.1.2.
- [Release notes](https://github.com/coveralls-clients/coveralls-python/releases)
- [Changelog](https://github.com/coveralls-clients/coveralls-python/blob/master/CHANGELOG.md)
- [Commits](https://github.com/coveralls-clients/coveralls-python/compare/2.1.1...2.1.2)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-17 06:25:10 +00:00
dependabot[bot]
c8ddd5654a Bump prompt-toolkit from 3.0.5 to 3.0.6
Bumps [prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit) from 3.0.5 to 3.0.6.
- [Release notes](https://github.com/prompt-toolkit/python-prompt-toolkit/releases)
- [Changelog](https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/CHANGELOG)
- [Commits](https://github.com/prompt-toolkit/python-prompt-toolkit/compare/3.0.5...3.0.6)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-17 06:25:04 +00:00
dependabot[bot]
988bff9eae Bump ccxt from 1.32.88 to 1.33.18
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.32.88 to 1.33.18.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.32.88...1.33.18)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-17 06:24:54 +00:00
dependabot[bot]
7af7fb261b Bump pytest-cov from 2.10.0 to 2.10.1
Bumps [pytest-cov](https://github.com/pytest-dev/pytest-cov) from 2.10.0 to 2.10.1.
- [Release notes](https://github.com/pytest-dev/pytest-cov/releases)
- [Changelog](https://github.com/pytest-dev/pytest-cov/blob/master/CHANGELOG.rst)
- [Commits](https://github.com/pytest-dev/pytest-cov/compare/v2.10.0...v2.10.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-17 06:24:50 +00:00
dependabot[bot]
da6672841a Bump mkdocs-material from 5.5.3 to 5.5.7
Bumps [mkdocs-material](https://github.com/squidfunk/mkdocs-material) from 5.5.3 to 5.5.7.
- [Release notes](https://github.com/squidfunk/mkdocs-material/releases)
- [Changelog](https://github.com/squidfunk/mkdocs-material/blob/master/docs/changelog.md)
- [Commits](https://github.com/squidfunk/mkdocs-material/compare/5.5.3...5.5.7)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-17 06:24:49 +00:00
Matthias
a6dac9acf3 Merge pull request #3667 from freqtrade/hyperopt_enable_dataprovider
Hyperopt enable dataprovider
2020-08-17 07:00:48 +02:00
Matthias
1f153f51ee Merge pull request #3660 from freqtrade/hyperopt_default_tests
Move DefaultHyperopt to tests
2020-08-17 06:49:55 +02:00
Fredrik81
d6ea442588 Update stoploss.md 2020-08-17 02:10:56 +02:00
Fredrik81
2a6faaae64 Update stoploss.md 2020-08-17 02:07:32 +02:00
Fredrik81
4619a50097 Update configuration.md 2020-08-17 02:07:25 +02:00
Fredrik81
bd308889fc Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 14:58:06 +02:00
Fredrik81
f8efb87a67 Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 14:57:53 +02:00
Fredrik81
ddba999fe2 Update stoploss.md 2020-08-16 13:44:32 +02:00
Fredrik81
81a75c97cf Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:17:11 +02:00
Fredrik81
5091767276 Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:17:01 +02:00
Fredrik81
8b348fc247 Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:16:35 +02:00
Fredrik81
67e9721274 Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:14:50 +02:00
Fredrik81
4a0c988b67 Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:13:54 +02:00
Fredrik81
e30a38932f Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:13:40 +02:00
Fredrik81
902d40a32a Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:13:27 +02:00
Fredrik81
4ade3daa1e Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:09:19 +02:00
Fredrik81
1ce392f652 Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:05:38 +02:00
Fredrik81
c60192e4bd Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:05:07 +02:00
Fredrik81
bae8e5ed1a Update docs/stoploss.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-16 13:03:56 +02:00
Matthias
cffac3f7b6 Merge pull request #3619 from hroff-1902/cleanup_agefilter
Cleanup AgeFilter, PriceFilter
2020-08-16 09:13:11 +02:00
Fredrik81
b9e46a3c5a Update stoploss.md
Updated documentation to simplify examples
2020-08-16 03:02:10 +02:00
Matthias
56ca37fd8b Also provide stacktrace via log endpoints 2020-08-15 20:15:02 +02:00
Matthias
c2573c998b Remove Hyperopt note about windows 2020-08-15 16:26:47 +02:00
Matthias
cc91d51389 Fix wording in configuration.md 2020-08-15 09:18:00 +02:00
Matthias
142f87b68c Adjust tests to new wordings 2020-08-15 09:11:46 +02:00
Matthias
1cb10d8f8e Merge branch 'develop' into pr/hroff-1902/3619 2020-08-15 09:08:59 +02:00
Matthias
9dd2800b98 Apply some review changes 2020-08-15 09:08:50 +02:00
Matthias
89b9a8cb1f Merge pull request #3396 from freqtrade/fix/broken_getpairs
Use dict for symbol_is_pair
2020-08-15 08:58:53 +02:00
Matthias
f3d4b114bb Skip windows test failure 2020-08-15 08:47:09 +02:00
Matthias
1ffa3d1ae0 Improve telegram message formatting 2020-08-15 08:31:36 +02:00
Matthias
f5863a1c6f Fix mypy errors 2020-08-15 08:15:47 +02:00
Matthias
9659e516c8 Remove queue import
Improve tests
2020-08-14 20:35:15 +02:00
Matthias
c4f78203ab Initialize streamhandler early to have it apply to all logs 2020-08-14 20:08:55 +02:00
Matthias
cdfcdb86c9 Increase logfile size 2020-08-14 20:00:09 +02:00
Matthias
251eb5aa96 Test for bufferingHandler too 2020-08-14 19:51:50 +02:00
Matthias
122c0e8ddc Readd accidentally dropped StreamHandler 2020-08-14 19:50:56 +02:00
Matthias
200de312fe Merge pull request #3677 from Blackhawke/develop
improve Edge documentation
2020-08-14 19:43:10 +02:00
Matthias
9ad8e74247 Add tests for log-endpoints 2020-08-14 19:41:27 +02:00
Matthias
5d691b5ee3 Fix warning box typo 2020-08-14 19:34:22 +02:00
Blackhawke
f3cedc849a Update docs/edge.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-14 09:27:04 -07:00
Blackhawke
a14ce9d7d9 Update docs/edge.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-14 09:26:28 -07:00
Blackhawke
47b215fe0a Update docs/edge.md
Co-authored-by: Matthias <xmatthias@outlook.com>
2020-08-14 09:25:53 -07:00
Matthias
904c4ecc23 Document /logs endpoints 2020-08-14 15:44:52 +02:00
Matthias
5f79caa307 Implement /logs endpoints in telegram and restAPI 2020-08-14 15:44:36 +02:00
Matthias
b989ba0f82 Simplify setup of handlers 2020-08-14 14:53:21 +02:00
Matthias
48944fd4cb Logging with queueHandler 2020-08-14 14:41:46 +02:00
Matthias
93717cfef1 Merge pull request #3008 from yazeed/more_info_hyperopt_fixed
Wins/draws/losses/median profit in hyperopt output
2020-08-14 09:25:12 +02:00
Matthias
b98107375e Improve formatting of result string to be a bit conciser 2020-08-14 07:31:14 +02:00
Matthias
d76ee43246 Show wins / draws / losses in hyperopt table 2020-08-14 07:14:10 +02:00
Matthias
22f6e884ed Merge pull request #3676 from freqtrade/stoploss_remove_unused_argument
[minor] Cleanup and exception hierarchy documentation
2020-08-14 07:11:56 +02:00
Matthias
05bd099f51 Merge branch 'develop' into pr/yazeed/3008 2020-08-14 06:58:09 +02:00
Matthias
4109b31dac Update wording in documentation 2020-08-14 06:46:34 +02:00
Matthias
067d1fd72a Merge pull request #3553 from qkum/patch-3
Update faq.md
2020-08-13 08:20:03 +02:00
Matthias
6b85b1a34d Don't only recommend pycharm, but keep it open to other editors too. 2020-08-13 08:06:57 +02:00
Matthias
e45e41adb4 Improve docs test to catch !!! errors 2020-08-13 08:05:05 +02:00
Matthias
1dabade883 small rewording of FAQ documentation 2020-08-13 08:02:36 +02:00
Matthias
c6741ea6c3 Merge branch 'develop' into fix/broken_getpairs 2020-08-12 20:13:06 +02:00
Matthias
5d61c56650 Merge pull request #3597 from freqtrade/fix/3579
consistently use filled before amount from orders
2020-08-12 19:56:57 +02:00
Blackhawke
827c31d4bc Re-arranged the introduction to better explain the theory of operation and the limitations of Edge. Added paragraphs at the bottom of "running edge independently" to better explain Edge's order of operations processing and potential differences between historical output and live/dry-run operation. 2020-08-12 09:42:16 -07:00
Matthias
3afd5b631e Remove erroneous import 2020-08-12 15:34:29 +02:00
Matthias
815d88fd4a Fix test after merge, fix forgotten 'amount' 2020-08-12 15:32:56 +02:00
Matthias
9999d0ffb5 Merge branch 'develop' into fix/3579 2020-08-12 15:28:51 +02:00
Matthias
faa2bbb555 Document exception hierarchy 2020-08-12 14:29:14 +02:00
Matthias
6dfa159a91 Small comment adjustments in exchange class 2020-08-12 14:11:19 +02:00
Matthias
7cf3e15e54 Merge pull request #3091 from yazeed/min-max-objective
--min-objective and --max-objective for hyperopt-list
2020-08-12 14:05:05 +02:00
Matthias
1f1a819b29 Remove unused 3rd argument to create_stoploss call 2020-08-12 11:21:00 +02:00
Matthias
2fed066e76 Simplify objective code formatting 2020-08-12 10:40:44 +02:00
Matthias
2dc36bb79e Remove inversion of min/max objective selection 2020-08-11 20:52:18 +02:00
Matthias
56655b97cf Refactor hyperopt_filter method 2020-08-11 20:37:01 +02:00
Matthias
f51c03aa86 Revert changes to color using --no-color 2020-08-11 20:29:47 +02:00
Matthias
77541935a8 Fix small merge mistake 2020-08-11 20:18:49 +02:00
Matthias
688d657fe2 Merge branch 'develop' into pr/yazeed/3091 2020-08-11 20:04:43 +02:00
Matthias
dda78677a0 Merge pull request #3649 from freqtrade/improve_cancel_order_handling
Better handle cancelled buy orders
2020-08-11 19:58:05 +02:00
Matthias
d77c53960d Show API backoff in logs to better investigate eventual problems) 2020-08-11 19:27:25 +02:00
Matthias
c9c43d2f0b Move log-message of retrying before decrementing count
Otherwise the message is always one round "late".
2020-08-11 15:27:41 +02:00
Matthias
064928a0eb Merge branch 'develop' into improve_cancel_order_handling 2020-08-11 15:25:47 +02:00
Matthias
52eae04945 Merge pull request #3670 from freqtrade/dependabot/pip/develop/ccxt-1.32.88
Bump ccxt from 1.32.45 to 1.32.88
2020-08-10 20:37:40 +02:00
Matthias
0d96a311f7 Merge pull request #3669 from freqtrade/dependabot/pip/develop/mkdocs-material-5.5.3
Bump mkdocs-material from 5.5.1 to 5.5.3
2020-08-10 20:23:02 +02:00
dependabot[bot]
1afe4df7be Bump ccxt from 1.32.45 to 1.32.88
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.32.45 to 1.32.88.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.32.45...1.32.88)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-10 06:17:36 +00:00
dependabot[bot]
17613f203a Bump mkdocs-material from 5.5.1 to 5.5.3
Bumps [mkdocs-material](https://github.com/squidfunk/mkdocs-material) from 5.5.1 to 5.5.3.
- [Release notes](https://github.com/squidfunk/mkdocs-material/releases)
- [Changelog](https://github.com/squidfunk/mkdocs-material/blob/master/docs/changelog.md)
- [Commits](https://github.com/squidfunk/mkdocs-material/compare/5.5.1...5.5.3)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-10 06:17:04 +00:00
Matthias
2663aede24 Update test to reflect new column naming 2020-08-09 10:28:11 +02:00
Matthias
b576e1d463 Merge branch 'develop' into bt_add_maxdrawdown 2020-08-09 10:25:57 +02:00
Matthias
87e4a82041 Merge branch 'develop' into bt_add_maxdrawdown 2020-08-09 08:34:36 +02:00
Matthias
fca41a44bb Also logg timeframe 2020-08-08 20:20:58 +02:00
Matthias
3670be5dd2 Merge pull request #3641 from freqtrade/fix/edgeremovebumps
Fix edge with removebumps enabled
2020-08-08 17:33:08 +02:00
Matthias
2afe1d5b11 Add link to full sample 2020-08-08 17:30:31 +02:00
Matthias
09aa954b68 Update strategy-customization documentation 2020-08-08 17:24:19 +02:00
Matthias
5e1032c4af Simplify strategy documentation, move "substrategies" to advanced page 2020-08-08 17:08:38 +02:00
Matthias
dd430455e4 Enable dataprovier for hyperopt 2020-08-08 17:04:32 +02:00
Matthias
e2643103b6 Merge pull request #3611 from thopd88/telegram-delete-command
Add telegram /delete command
2020-08-08 15:19:40 +02:00
Matthias
f3ce54150e Simplify Telegram table 2020-08-08 15:06:13 +02:00
Matthias
02810adcf7 Merge pull request #3662 from freqtrade/Fredrik81-patch-1
Update strategy_methods_advanced.j2
2020-08-07 06:23:22 +02:00
Fredrik81
eba73307e4 Update strategy_methods_advanced.j2
Fix def confirm_trade_exit arguments
2020-08-07 01:13:36 +02:00
Matthias
d01070dba8 Increase coverage of edge_cli 2020-08-06 09:22:41 +02:00
Matthias
995d3e1ed5 Don't search internal path for Hyperopt files 2020-08-06 09:07:48 +02:00
Matthias
59370672b8 Fix more tests 2020-08-06 09:00:28 +02:00
Matthias
081625c5dc Have hyperopt tests use new hyperopt location 2020-08-06 08:51:01 +02:00
Matthias
8b6d10daf1 Move DefaultHyperopt to test folder (aligned to strategy) 2020-08-06 08:50:41 +02:00
Matthias
5082acc33f Fix typos in documentation 2020-08-06 07:54:54 +02:00
Matthias
767332405e Merge pull request #3642 from freqtrade/new_release
New release 2020.7
2020-08-06 06:50:08 +02:00
Matthias
8ed3b81c61 Implement /delete in rest client 2020-08-04 19:57:28 +02:00
Matthias
075c73b9e3 Improve formatting of telegram message 2020-08-04 19:56:49 +02:00
Matthias
817f5289db /delete should Cancel open orders (and stoploss orders) 2020-08-04 19:43:22 +02:00
Matthias
9163c7f3d3 Improve api response 2020-08-04 19:43:05 +02:00
Matthias
b954af33cf Fix type erorr in callable 2020-08-04 16:01:41 +02:00
Matthias
4b0164770c Add test for /delete 2020-08-04 14:49:59 +02:00
Matthias
26c7341b7d Add test for api-server DELETE trade 2020-08-04 14:41:38 +02:00
Matthias
215972c68f Implement /delete for api-server 2020-08-04 14:41:22 +02:00
Matthias
c0083c4244 Merge branch 'develop' into pr/thopd88/3611 2020-08-04 07:00:54 +02:00
Matthias
b22fabe1f3 Merge pull request #3651 from freqtrade/dependabot/pip/develop/pytest-6.0.1
Bump pytest from 5.4.3 to 6.0.1
2020-08-03 21:31:31 +02:00
Matthias
a3688b159f Improve formatting 2020-08-03 19:28:57 +02:00
Matthias
a33346c6b6 Fix testing errors - which surfaced with pytest 6.0.1 2020-08-03 19:22:07 +02:00
Matthias
55233db07e Merge pull request #3654 from freqtrade/dependabot/pip/develop/ccxt-1.32.45
Bump ccxt from 1.32.7 to 1.32.45
2020-08-03 09:42:57 +02:00
Matthias
54bee1d183 Merge pull request #3653 from freqtrade/dependabot/pip/develop/pandas-1.1.0
Bump pandas from 1.0.5 to 1.1.0
2020-08-03 09:40:24 +02:00
Matthias
45256763ca Merge pull request #3652 from freqtrade/dependabot/pip/develop/mkdocs-material-5.5.1
Bump mkdocs-material from 5.5.0 to 5.5.1
2020-08-03 09:36:01 +02:00
dependabot[bot]
b3f04d89d2 Bump ccxt from 1.32.7 to 1.32.45
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.32.7 to 1.32.45.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.32.7...1.32.45)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-03 07:17:50 +00:00
dependabot[bot]
1855a444fa Bump pandas from 1.0.5 to 1.1.0
Bumps [pandas](https://github.com/pandas-dev/pandas) from 1.0.5 to 1.1.0.
- [Release notes](https://github.com/pandas-dev/pandas/releases)
- [Changelog](https://github.com/pandas-dev/pandas/blob/master/RELEASE.md)
- [Commits](https://github.com/pandas-dev/pandas/compare/v1.0.5...v1.1.0)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-03 07:17:32 +00:00
dependabot[bot]
809b3ddafc Bump mkdocs-material from 5.5.0 to 5.5.1
Bumps [mkdocs-material](https://github.com/squidfunk/mkdocs-material) from 5.5.0 to 5.5.1.
- [Release notes](https://github.com/squidfunk/mkdocs-material/releases)
- [Changelog](https://github.com/squidfunk/mkdocs-material/blob/master/docs/changelog.md)
- [Commits](https://github.com/squidfunk/mkdocs-material/compare/5.5.0...5.5.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-03 07:17:31 +00:00
dependabot[bot]
5ff09a06c7 Bump pytest from 5.4.3 to 6.0.1
Bumps [pytest](https://github.com/pytest-dev/pytest) from 5.4.3 to 6.0.1.
- [Release notes](https://github.com/pytest-dev/pytest/releases)
- [Changelog](https://github.com/pytest-dev/pytest/blob/master/CHANGELOG.rst)
- [Commits](https://github.com/pytest-dev/pytest/compare/5.4.3...6.0.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-03 07:17:30 +00:00
Matthias
3915101d2d Add more backoff to fetch_order endpoint 2020-08-02 10:32:17 +02:00
Matthias
6c77feee85 Improve some exchange logs 2020-08-02 10:18:19 +02:00
Matthias
99bfa839eb Improve logging for sell exception 2020-08-02 10:12:15 +02:00
Matthias
071e82043a Better handle cancelled buy orders 2020-08-01 15:59:50 +02:00
Matthias
7263f83f78 Version bump 2020.7 2020-07-28 19:53:05 +02:00
Matthias
653bbc292b Merge branch 'master' into new_release 2020-07-28 19:52:44 +02:00
Matthias
d1cbc567e4 Fix filtering for bumped pairs 2020-07-28 13:41:09 +02:00
Matthias
14cb29aae1 Add test for remove_pumps in edge 2020-07-28 08:16:55 +02:00
Matthias
f3af02c06f Merge pull request #3635 from freqtrade/dependabot/pip/develop/ccxt-1.32.7
Bump ccxt from 1.32.3 to 1.32.7
2020-07-27 09:23:23 +02:00
dependabot[bot]
7318d02ebc Bump ccxt from 1.32.3 to 1.32.7
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.32.3 to 1.32.7.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.32.3...1.32.7)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-27 07:05:17 +00:00
Matthias
aab5596fa6 Convert trade open / close to timestamp
(to allow uniform analysis of backtest and real trade data - while
giving control of date-formatting to the endsystem.
2020-07-27 07:20:40 +02:00
Matthias
977a6d4e9c Add profit_total to results line 2020-07-26 16:10:48 +02:00
Matthias
454046f745 Add stake_currency and max_opeN_trades to backtest result 2020-07-26 15:55:54 +02:00
Matthias
8d0f338bf2 Timestamps should be in ms 2020-07-26 15:23:21 +02:00
Matthias
9ed5fed887 Fix output format to be of an identical type 2020-07-26 15:17:54 +02:00
Matthias
902e8fa62f Fix wrong spelling in one subcomponent 2020-07-26 14:39:00 +02:00
Matthias
65755989b4 Merge pull request #3631 from freqtrade/dependabot/pip/develop/mkdocs-material-5.5.0
Bump mkdocs-material from 5.4.0 to 5.5.0
2020-07-26 14:00:00 +02:00
Matthias
7a51bfbaba Merge pull request #3628 from freqtrade/dependabot/pip/develop/scipy-1.5.2
Bump scipy from 1.5.1 to 1.5.2
2020-07-26 13:28:42 +02:00
Matthias
fe27d2c10d Merge pull request #3629 from freqtrade/dependabot/pip/develop/urllib3-1.25.10
Bump urllib3 from 1.25.9 to 1.25.10
2020-07-26 13:27:42 +02:00
Matthias
90034a8e5e Merge pull request #3632 from freqtrade/dependabot/pip/develop/ccxt-1.32.3
Bump ccxt from 1.31.37 to 1.32.3
2020-07-26 11:23:26 +02:00
Matthias
1cfbbfb433 Merge pull request #3633 from freqtrade/dependabot/pip/develop/plotly-4.9.0
Bump plotly from 4.8.2 to 4.9.0
2020-07-26 11:11:54 +02:00
Matthias
73042781f4 Merge pull request #3630 from freqtrade/dependabot/pip/develop/numpy-1.19.1
Bump numpy from 1.19.0 to 1.19.1
2020-07-26 11:11:23 +02:00
dependabot[bot]
dbcccac6cd Bump ccxt from 1.31.37 to 1.32.3
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.31.37 to 1.32.3.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.31.37...1.32.3)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-26 08:53:51 +00:00
dependabot[bot]
b4d22f1000 Bump urllib3 from 1.25.9 to 1.25.10
Bumps [urllib3](https://github.com/urllib3/urllib3) from 1.25.9 to 1.25.10.
- [Release notes](https://github.com/urllib3/urllib3/releases)
- [Changelog](https://github.com/urllib3/urllib3/blob/master/CHANGES.rst)
- [Commits](https://github.com/urllib3/urllib3/compare/1.25.9...1.25.10)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-26 08:53:36 +00:00
Matthias
364295d2b3 Merge pull request #3627 from freqtrade/dependabot/pip/develop/arrow-0.15.8
Bump arrow from 0.15.7 to 0.15.8
2020-07-26 10:52:50 +02:00
dependabot[bot]
63e7490a55 Bump plotly from 4.8.2 to 4.9.0
Bumps [plotly](https://github.com/plotly/plotly.py) from 4.8.2 to 4.9.0.
- [Release notes](https://github.com/plotly/plotly.py/releases)
- [Changelog](https://github.com/plotly/plotly.py/blob/master/CHANGELOG.md)
- [Commits](https://github.com/plotly/plotly.py/compare/v4.8.2...v4.9.0)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-26 08:37:45 +00:00
dependabot[bot]
838743bf01 Bump mkdocs-material from 5.4.0 to 5.5.0
Bumps [mkdocs-material](https://github.com/squidfunk/mkdocs-material) from 5.4.0 to 5.5.0.
- [Release notes](https://github.com/squidfunk/mkdocs-material/releases)
- [Changelog](https://github.com/squidfunk/mkdocs-material/blob/master/docs/changelog.md)
- [Commits](https://github.com/squidfunk/mkdocs-material/compare/5.4.0...5.5.0)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-26 08:37:25 +00:00
dependabot[bot]
2ff03e173d Bump numpy from 1.19.0 to 1.19.1
Bumps [numpy](https://github.com/numpy/numpy) from 1.19.0 to 1.19.1.
- [Release notes](https://github.com/numpy/numpy/releases)
- [Changelog](https://github.com/numpy/numpy/blob/master/doc/HOWTO_RELEASE.rst.txt)
- [Commits](https://github.com/numpy/numpy/compare/v1.19.0...v1.19.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-26 08:37:17 +00:00
dependabot[bot]
d1d6f69e43 Bump scipy from 1.5.1 to 1.5.2
Bumps [scipy](https://github.com/scipy/scipy) from 1.5.1 to 1.5.2.
- [Release notes](https://github.com/scipy/scipy/releases)
- [Commits](https://github.com/scipy/scipy/compare/v1.5.1...v1.5.2)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-26 08:37:13 +00:00
dependabot[bot]
6ce4fd7aff Bump arrow from 0.15.7 to 0.15.8
Bumps [arrow](https://github.com/arrow-py/arrow) from 0.15.7 to 0.15.8.
- [Release notes](https://github.com/arrow-py/arrow/releases)
- [Changelog](https://github.com/arrow-py/arrow/blob/master/CHANGELOG.rst)
- [Commits](https://github.com/arrow-py/arrow/compare/0.15.7...0.15.8)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-26 08:37:10 +00:00
Matthias
db8f3a9e9b Merge pull request #3609 from thopd88/develop
Add telegram /trades command
2020-07-25 16:45:09 +02:00
Matthias
e0c14e6214 Add /trades to help (so users know about it) 2020-07-23 07:56:05 +02:00
Matthias
fdc84eef59 /trades shall only return closed trades 2020-07-23 07:50:45 +02:00
Matthias
8300eb59d4 Extend create_mock_trades to create 4 trades
2 closed, and 2 open trades
2020-07-23 07:50:28 +02:00
Matthias
0f18b2a0d4 Add test and fix case where no trades were closed yet 2020-07-23 07:12:14 +02:00
thopd88
0bad55637e fix flake8 indent error 2020-07-23 10:12:52 +07:00
thopd88
a3daf8e41c Fix line too long 2020-07-23 09:47:53 +07:00
thopd88
0502fe0496 New /trades 3 columns and exclude open trades 2020-07-23 09:36:05 +07:00
hroff-1902
f48250b414 Make flake happy 2020-07-22 22:56:24 +03:00
hroff-1902
50767cd569 Adjust tests for AgeFilter 2020-07-22 22:48:29 +03:00
hroff-1902
5c2481082e Add tests for PriceFilter 2020-07-22 22:46:30 +03:00
hroff-1902
c78199d3d9 Add checks for parameters of PriceFilter 2020-07-22 22:45:46 +03:00
hroff-1902
a1e292f56a Improve docs 2020-07-22 22:09:30 +03:00
hroff-1902
daee414d7a Fix docs formatting 2020-07-22 21:51:25 +03:00
hroff-1902
5213abf510 AgeFilter is always enabled 2020-07-22 21:44:39 +03:00
hroff-1902
f6bde8bd9c Improve exception message wordings 2020-07-22 21:43:15 +03:00
Matthias
7e980037a4 Merge pull request #3554 from jblestang/Fix_#3544
Adding a dataprovider to the strategy before plotting
2020-07-22 15:56:16 +02:00
Matthias
f5f529cace Use correct initialization of DataProvider 2020-07-22 15:17:45 +02:00
Matthias
b060164b1f Merge pull request #3618 from freqtrade/dependabot/docker/python-3.8.5-slim-buster
Bump python from 3.8.4-slim-buster to 3.8.5-slim-buster
2020-07-22 08:41:26 +02:00
dependabot[bot]
2a5f8d8895 Bump python from 3.8.4-slim-buster to 3.8.5-slim-buster
Bumps python from 3.8.4-slim-buster to 3.8.5-slim-buster.

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-22 06:22:45 +00:00
hroff-1902
dbf4d1a694 Merge pull request #3616 from freqtrade/fix/pairfilter
Fix pairfilter crash
2020-07-21 23:09:06 +03:00
Matthias
6a10c715fa Fix 0 division (if last = 0, something went wrong!) 2020-07-21 20:34:29 +02:00
Matthias
939f91734f Test confirming 0 division ... 2020-07-21 20:34:19 +02:00
hroff-1902
d8fa17cee8 Merge pull request #3614 from freqtrade/info_message_hyperopt
[minor] Reduce severity of hyperopt "does not provide" messages
2020-07-21 00:14:18 +03:00
hroff-1902
844ff1e068 Merge pull request #3613 from freqtrade/webhook/trade_id
Add trade_id to webhooks
2020-07-21 00:10:53 +03:00
Matthias
7d6708fc6a Reduce severity of hyperopt "does not provide" messages
closes #3371
2020-07-20 20:04:23 +02:00
Matthias
21dcef1134 Add trade_id to webhooks
allowing for easier corelation of different messages
2020-07-20 19:57:05 +02:00
Matthias
4774896169 Evaluate average before price in order returns 2020-07-20 19:39:12 +02:00
Matthias
4c97527b04 FIx failing test 2020-07-20 19:11:15 +02:00
hroff-1902
22c8f845ec Merge pull request #3606 from freqtrade/docs/informative
Improve informative pair sample
2020-07-20 19:22:48 +03:00
hroff-1902
b7c6f868b2 Merge pull request #3478 from hroff-1902/exchange-cosmetics-5
Minor: Exchange cosmetics
2020-07-20 18:58:46 +03:00
Matthias
3955fc6190 Merge pull request #3612 from freqtrade/fix-doc-sqlrequest
missing coma in sql request
2020-07-20 08:58:13 +02:00
gautier pialat
811028ae92 missing coma in sql request 2020-07-20 07:17:34 +02:00
thopd88
eaa7370174 add /delete command 2020-07-20 11:08:18 +07:00
thopd88
28f4a1101e Revert "Add telegram /delete command to delete tradeid"
This reverts commit 08fdd7d863.
2020-07-20 10:54:17 +07:00
Matthias
263dcd221d Merge pull request #3608 from thopd88/patch-1
Fix SQL syntax error when compare pair strings in rpc_forcebuy
2020-07-19 19:28:08 +02:00
Matthias
772473e93e Merge pull request #3610 from pan-long/develop
Correct a typo in stop loss doc.
2020-07-19 19:20:24 +02:00
Pan Long
37a9edfa35 Correct a typo in stop loss doc. 2020-07-20 00:37:06 +08:00
thopd88
08fdd7d863 Add telegram /delete command to delete tradeid
code inspired from _rpc_forcesell
2020-07-19 22:10:59 +07:00
thopd88
dd3a2675b5 Add telegram trades command to list recent trades 2020-07-19 22:02:53 +07:00
Alex Pham
3271c773a7 Fix SQL syntax error when compare pair strings
First happens in Postgres
2020-07-19 21:30:55 +07:00
Matthias
49395601e9 Improve informative pair sample 2020-07-19 10:02:06 +02:00
Matthias
ea1ddeb87d Merge pull request #3570 from gambcl/develop
Added range checks to min_days_listed in AgeFilter
2020-07-19 09:37:17 +02:00
Matthias
d849b32a02 Merge pull request #3601 from freqtrade/dependabot/pip/develop/ccxt-1.31.37
Bump ccxt from 1.30.93 to 1.31.37
2020-07-16 09:46:31 +02:00
dependabot[bot]
cd7ba99528 Bump ccxt from 1.30.93 to 1.31.37
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.30.93 to 1.31.37.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.30.93...1.31.37)

Signed-off-by: dependabot[bot] <support@github.com>
2020-07-16 07:23:16 +00:00
Matthias
288a2bdbb0 Merge pull request #3599 from freqtrade/dependabot/add-v2-config-file
Update Dependabot config file
2020-07-16 09:22:29 +02:00
dependabot-preview[bot]
eaf2b53d59 Update Dependabot config file 2020-07-16 05:10:46 +00:00
Matthias
de46744aa9 Use filled before amount for order data
closes #3579
2020-07-15 21:08:16 +02:00
Matthias
98f2e79f27 Adjust tests to use correctly trimmed amount 2020-07-15 20:55:33 +02:00
Matthias
3721736aaf Convert to real amount before placing order
to keep the correct amount in the database
2020-07-15 20:28:07 +02:00
Matthias
c1c018d8fe Fix tests that require amount_requested 2020-07-15 20:27:00 +02:00
Matthias
eafab38db3 Complete implementation of amount_requested 2020-07-15 20:20:14 +02:00
Matthias
c826f7a707 Add amount_requested to database 2020-07-15 20:15:29 +02:00
hroff-1902
18a5822a33 Merge pull request #3596 from freqtrade/fix/0fee
Allow 0 fee value by correctly checking for None
2020-07-15 20:52:32 +03:00
Matthias
d13cb4c055 Introduce safe_value_fallback_2 2020-07-15 19:50:09 +02:00
Matthias
5cebc9f39d Move stoploss_on_exchange_limit_ratio to configuration schema 2020-07-15 19:28:40 +02:00
Matthias
c1191400a4 Allow 0 fee value by correctly checking for None 2020-07-15 19:20:20 +02:00
gambcl
1051ab917a Replaced logging with OperationalException when AgeFilter given invalid parameters 2020-07-15 12:40:54 +01:00
Matthias
82c68f07cd Add stoploss-distance (to current price) to /status output 2020-07-14 20:16:18 +02:00
Matthias
bdf611352e Update summary-metrics output 2020-07-14 19:34:01 +02:00
Matthias
2417898d00 Apply documentation suggestions from code review
Co-authored-by: hroff-1902 <47309513+hroff-1902@users.noreply.github.com>
2020-07-14 19:27:52 +02:00
hroff-1902
0f4fc67b83 Merge pull request #3582 from freqtrade/data/list
List available backtesting data
2020-07-14 19:38:32 +03:00
Matthias
0228b63418 Don't print empty table 2020-07-14 16:42:47 +02:00
Matthias
0ca81480d4 Merge pull request #3590 from freqtrade/dependabot/docker/python-3.8.4-slim-buster
Bump python from 3.8.3-slim-buster to 3.8.4-slim-buster
2020-07-14 09:48:51 +02:00
dependabot-preview[bot]
ae55d54967 Bump python from 3.8.3-slim-buster to 3.8.4-slim-buster
Bumps python from 3.8.3-slim-buster to 3.8.4-slim-buster.

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-14 06:33:57 +00:00
Matthias
62c55b1863 Enhance formatting, Add pair filter 2020-07-14 06:55:34 +02:00
hroff-1902
43a1fe6d08 Merge pull request #3589 from freqtrade/api/timeframe_ms
[minor] Send timeframe min and ms in show_config response
2020-07-13 23:24:30 +03:00
Matthias
01f325a9e4 Send timeframe min and ms in show_config response 2020-07-13 21:15:33 +02:00
Matthias
0b36693acc Add filter for stoploss_on_exchange_limit_ratio to constants 2020-07-13 19:48:21 +02:00
Matthias
c2acf4bb82 Merge pull request #3584 from freqtrade/dependabot/pip/develop/pytest-mock-3.2.0
Bump pytest-mock from 3.1.1 to 3.2.0
2020-07-13 12:22:51 +02:00
Matthias
89c634c70d Merge pull request #3586 from freqtrade/dependabot/pip/develop/ccxt-1.30.93
Bump ccxt from 1.30.64 to 1.30.93
2020-07-13 12:22:21 +02:00
Matthias
e4b5bbe117 Merge pull request #3587 from freqtrade/dependabot/pip/develop/coveralls-2.1.1
Bump coveralls from 2.0.0 to 2.1.1
2020-07-13 12:21:53 +02:00
Matthias
b0b76091c8 Merge pull request #3585 from freqtrade/dependabot/pip/develop/pycoingecko-1.3.0
Bump pycoingecko from 1.2.0 to 1.3.0
2020-07-13 12:21:26 +02:00
dependabot-preview[bot]
50573bd397 Bump coveralls from 2.0.0 to 2.1.1
Bumps [coveralls](https://github.com/coveralls-clients/coveralls-python) from 2.0.0 to 2.1.1.
- [Release notes](https://github.com/coveralls-clients/coveralls-python/releases)
- [Changelog](https://github.com/coveralls-clients/coveralls-python/blob/master/CHANGELOG.md)
- [Commits](https://github.com/coveralls-clients/coveralls-python/compare/2.0.0...2.1.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-13 09:02:07 +00:00
dependabot-preview[bot]
d1e4e463ae Bump ccxt from 1.30.64 to 1.30.93
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.30.64 to 1.30.93.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.30.64...1.30.93)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-13 09:01:58 +00:00
dependabot-preview[bot]
58eb26d73a Bump pycoingecko from 1.2.0 to 1.3.0
Bumps [pycoingecko](https://github.com/man-c/pycoingecko) from 1.2.0 to 1.3.0.
- [Release notes](https://github.com/man-c/pycoingecko/releases)
- [Changelog](https://github.com/man-c/pycoingecko/blob/master/CHANGELOG.md)
- [Commits](https://github.com/man-c/pycoingecko/compare/1.2.0...1.3.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-13 09:01:14 +00:00
dependabot-preview[bot]
79af6180bd Bump pytest-mock from 3.1.1 to 3.2.0
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 3.1.1 to 3.2.0.
- [Release notes](https://github.com/pytest-dev/pytest-mock/releases)
- [Changelog](https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst)
- [Commits](https://github.com/pytest-dev/pytest-mock/compare/v3.1.1...v3.2.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-13 09:00:50 +00:00
Matthias
6ee6e51ab4 Merge branch 'develop' into pr/hroff-1902/3478 2020-07-13 07:22:43 +02:00
Matthias
3811f4692b Merge pull request #3577 from freqtrade/fix/doctypo
[minor] Fix typo in docs, install sqlite3 in docker image
2020-07-12 12:50:04 +02:00
Matthias
ed2e35ba5d Update docs/sql_cheatsheet.md
Co-authored-by: hroff-1902 <47309513+hroff-1902@users.noreply.github.com>
2020-07-12 12:36:16 +02:00
Matthias
b035d9e267 Update return type comment 2020-07-12 10:23:09 +02:00
Matthias
33c3990972 Add documentation for list-data command 2020-07-12 10:05:47 +02:00
Matthias
5bb81abce2 Add test for start_list_data 2020-07-12 10:01:51 +02:00
Matthias
02afde857d Add list-data command 2020-07-12 09:57:00 +02:00
Matthias
d4fc52d2d5 Add tests for ohlcv_get_available_data 2020-07-12 09:56:46 +02:00
Matthias
422825ea1b Add ohlcv_get_available_data to find available data 2020-07-12 09:50:53 +02:00
hroff-1902
a4b0e8117a Merge pull request #3580 from BlueSkyTrading/patch-2
removed duplicate
2020-07-11 22:37:52 +03:00
HumanBot
f0a1a1720f removed duplicate
removed duplicate word using using
2020-07-11 15:21:54 -04:00
Matthias
ecbca3fab0 Add sqlite3 to dockerfile 2020-07-11 09:13:39 +02:00
Matthias
588043af86 Fix documentation brackets, add delete trade hints 2020-07-11 07:29:11 +02:00
Matthias
40bdc93653 Add test for short_desc of priceFilter 2020-07-10 20:28:29 +02:00
gambcl
14eab9be04 Added min_price, max_price to PriceFilter 2020-07-08 22:02:04 +01:00
gambcl
091285ba43 Fix flake8 error in test_pairlist.py 2020-07-08 18:32:14 +01:00
gambcl
2e45859aef Added range checks to min_days_listed in AgeFilter 2020-07-08 18:06:30 +01:00
Matthias
86cf6201c8 Merge pull request #3560 from freqtrade/dependabot/pip/develop/scipy-1.5.1
Bump scipy from 1.5.0 to 1.5.1
2020-07-07 21:54:01 +02:00
Matthias
e0c767614f Merge pull request #3561 from freqtrade/dependabot/pip/develop/ccxt-1.30.64
Bump ccxt from 1.30.48 to 1.30.64
2020-07-07 21:53:31 +02:00
dependabot-preview[bot]
deb34d2879 Bump scipy from 1.5.0 to 1.5.1
Bumps [scipy](https://github.com/scipy/scipy) from 1.5.0 to 1.5.1.
- [Release notes](https://github.com/scipy/scipy/releases)
- [Commits](https://github.com/scipy/scipy/compare/v1.5.0...v1.5.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-06 19:58:28 +00:00
Matthias
779a8401a8 Merge pull request #3563 from freqtrade/dependabot/pip/develop/joblib-0.16.0
Bump joblib from 0.15.1 to 0.16.0
2020-07-06 21:57:14 +02:00
Matthias
087a38ab78 Merge pull request #3562 from freqtrade/dependabot/pip/develop/mkdocs-material-5.4.0
Bump mkdocs-material from 5.3.3 to 5.4.0
2020-07-06 21:53:47 +02:00
dependabot-preview[bot]
93dd70c77d Bump joblib from 0.15.1 to 0.16.0
Bumps [joblib](https://github.com/joblib/joblib) from 0.15.1 to 0.16.0.
- [Release notes](https://github.com/joblib/joblib/releases)
- [Changelog](https://github.com/joblib/joblib/blob/master/CHANGES.rst)
- [Commits](https://github.com/joblib/joblib/compare/0.15.1...0.16.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-06 09:13:05 +00:00
dependabot-preview[bot]
4c8bee1e5d Bump mkdocs-material from 5.3.3 to 5.4.0
Bumps [mkdocs-material](https://github.com/squidfunk/mkdocs-material) from 5.3.3 to 5.4.0.
- [Release notes](https://github.com/squidfunk/mkdocs-material/releases)
- [Changelog](https://github.com/squidfunk/mkdocs-material/blob/master/CHANGELOG)
- [Commits](https://github.com/squidfunk/mkdocs-material/compare/5.3.3...5.4.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-06 09:12:15 +00:00
dependabot-preview[bot]
f63045b0e9 Bump ccxt from 1.30.48 to 1.30.64
Bumps [ccxt](https://github.com/ccxt/ccxt) from 1.30.48 to 1.30.64.
- [Release notes](https://github.com/ccxt/ccxt/releases)
- [Changelog](https://github.com/ccxt/ccxt/blob/master/doc/exchanges-by-country.rst)
- [Commits](https://github.com/ccxt/ccxt/compare/1.30.48...1.30.64)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-07-06 09:11:49 +00:00
hroff-1902
839b3340e6 Merge pull request #3497 from freqtrade/keep_dataframe_noapi
Analyze dataframe and keep it until the next analysis
2020-07-05 13:46:02 +03:00
Matthias
75318525a9 Update docs/strategy-advanced.md
Co-authored-by: hroff-1902 <47309513+hroff-1902@users.noreply.github.com>
2020-07-04 16:41:19 +02:00
Matthias
c4a9a79be0 Apply suggested documentation changes from code review
Co-authored-by: hroff-1902 <47309513+hroff-1902@users.noreply.github.com>
2020-07-04 09:43:49 +02:00
Matthias
1fc4451d2f Avoid \ linebreak 2020-07-03 20:32:04 +02:00
Matthias
ea5e47657a Remove ticker_interval from jupyter notebook 2020-07-03 20:27:32 +02:00
Matthias
0d15a87af8 Remove old store_backtest method 2020-07-03 20:21:32 +02:00
Matthias
523437d970 Add tst for daily stats 2020-07-03 20:03:33 +02:00
Matthias
987188e41f Add avgduration for winners and losers 2020-07-03 19:58:02 +02:00
Matthias
8e0ff4bd86 Add Win / draw / losing days 2020-07-03 19:45:45 +02:00
Matthias
42868ad24a Add best / worst day to statistics 2020-07-03 19:30:29 +02:00
Matthias
804c42933d Document summary-statistics 2020-07-03 08:02:27 +02:00
Matthias
d56f9655e2 Update notebook with new statistics example 2020-07-03 07:20:43 +02:00
Matthias
619eb183fe Allow strategy for plot-profit
to allow loading of multi-backtest files
2020-07-03 07:03:43 +02:00
Matthias
16a842f9f6 Have plotting support folder-based exportfilename 2020-07-03 06:58:27 +02:00
Matthias
d999fa2a7e Test autogetting result filename 2020-07-03 06:58:27 +02:00
Matthias
7c5587aeaa exportfilename can be a file or directory 2020-07-03 06:58:27 +02:00
Matthias
2ed808da1f Extract .last_result.json to constant 2020-07-03 06:58:27 +02:00
Matthias
59e0ca0aaa Add pairlist to backtest-result 2020-07-03 06:58:27 +02:00
Matthias
59ac4b9c9a Test writing statistics 2020-07-03 06:58:27 +02:00
Matthias
5b1a7ba00f Test multistrat loading 2020-07-03 06:58:27 +02:00
Matthias
f952f74bf1 Add test for new format 2020-07-03 06:58:27 +02:00
Matthias
573502d972 Update test for load_trades_from_db 2020-07-03 06:58:27 +02:00
Matthias
afefe92523 Add multi-strategy loading logic 2020-07-03 06:58:27 +02:00
Matthias
c13ec4a1d4 implement fallback loading for load_backtest_data 2020-07-03 06:58:27 +02:00
Matthias
1339479882 Have sell_type stringify correctly 2020-07-03 06:58:27 +02:00
Matthias
04eaf2c39c Add test for get_last_backtest_Result 2020-07-03 06:58:27 +02:00
Matthias
7727292861 Rename duration to trade_duration 2020-07-03 06:58:27 +02:00
Matthias
f368aabcc7 Add amount to backtest-result 2020-07-03 06:58:27 +02:00
Matthias
6e94734678 Add fee to backtestresult 2020-07-03 06:58:27 +02:00
Matthias
03ab61959b Add test for generate_backtest_stats 2020-07-03 06:58:27 +02:00
Matthias
af9a9592b7 Remove unnecessary statement 2020-07-03 06:58:27 +02:00
Matthias
075eb0a161 Fix sequence of saving 2020-07-03 06:58:27 +02:00
Matthias
dacb40a976 Add get_latest_backtest_filename 2020-07-03 06:58:27 +02:00
Matthias
0fa56be9d2 remove openIndex and closeIndex from backtest-report 2020-07-03 06:58:27 +02:00
Matthias
04cbc2cde5 Shorten variable 2020-07-03 06:58:27 +02:00
Matthias
2881718733 Adapt tests for new column names 2020-07-03 06:58:27 +02:00
Matthias
b068e7c564 Rename open_time and close_time to *date 2020-07-03 06:58:27 +02:00
Matthias
415853583b Save backtest-stats 2020-07-03 06:58:27 +02:00
Matthias
81c8e8677d use 0 as profit mean, not nan 2020-07-03 06:58:27 +02:00
Matthias
480c5117f1 Handle empty return strings 2020-07-03 06:58:27 +02:00
Matthias
5fce7f3b22 Add market Change
closes #2524 and #3518
2020-07-03 06:58:27 +02:00
Matthias
cf044d166e Tests should use new Datetime format too 2020-07-03 06:58:27 +02:00
Matthias
fbddfaeacf Introduce DatetimePrintFormat 2020-07-03 06:58:27 +02:00
Matthias
cbcf3dbb43 Add more metrics to summarytable 2020-07-03 06:58:27 +02:00
Matthias
6922fbc3aa Add max_drawdown error handler 2020-07-03 06:58:27 +02:00
Matthias
455b26ea48 Add max drawdown to backtesting 2020-07-03 06:58:27 +02:00
Matthias
5a189ae202 Merge pull request #3552 from Theagainmen/Minor_issues
API server FIAT init fix
2020-07-03 06:30:42 +02:00
Jean-Baptiste LE STANG
da1b37b917 Merge branch 'Fix_#3544' of https://github.com/jblestang/freqtrade into Fix_#3544 2020-07-02 21:16:30 +02:00
Jean-Baptiste LE STANG
20e8a29262 Adding a dataprovider to the strategy before plotting
Fix flake8
2020-07-02 21:14:31 +02:00
Jean-Baptiste LE STANG
23c0db925e Adding a dataprovider to the strategy before plotting 2020-07-02 20:55:16 +02:00
Theagainmen
f32e522bd7 Update API test, removed 'ANY' 2020-07-02 20:03:15 +02:00
Theagainmen
39fa589735 Update API test, currently just with 'ANY' 2020-07-02 13:39:02 +02:00
Theagainmen
db965332b9 Update tests for AgeFilter message 2020-07-02 11:38:38 +02:00
Theagainmen
99ac2659f3 Init FIAT converter in api_server.py 2020-07-02 11:27:33 +02:00
Theagainmen
81850b5fdf AgeFilter add actual amount of days in log message (debug info) 2020-07-02 11:26:52 +02:00
Matthias
d9d999eaea Merge pull request #3545 from BlueSkyTrading/patch-1
fixed --export trades command
2020-06-30 19:45:48 +02:00
HumanBot
61ae471eef fixed --export trades command
refers to issue 3413 @ https://github.com/freqtrade/freqtrade/issues/3413
2020-06-30 10:13:27 -04:00
Confucius-The-Great
2f759825e4 Update faq.md
Major changes :)
2020-06-30 11:01:00 +02:00
Matthias
cf1bbb1afb Merge pull request #3517 from freqtrade/rpc/winlossratio
Show winning vs. losing trades
2020-06-30 07:48:18 +02:00
Matthias
cf26ab1dd8 Merge pull request #3527 from Theagainmen/Warning_message2
Warning message bot is stopped and left open trades
2020-06-30 07:48:02 +02:00
Matthias
c2a6f70b4c Merge branch 'develop' into keep_dataframe_noapi 2020-06-30 07:46:52 +02:00
Matthias
efd6e4a875 Add test for check_for_open_trades 2020-06-30 07:16:27 +02:00
hroff-1902
8a2f631ddd Merge pull request #3531 from freqtrade/exchange_errorhandling
Improve exchange errorhandling and API backoff
2020-06-30 07:53:09 +03:00
Matthias
b95065d701 Log backoff 2020-06-29 20:00:42 +02:00
Matthias
4d9ecf137b Fix failing test in python 3.7
can't use Magicmock in 3.7 (works in 3.8 though).
2020-06-28 20:38:28 +02:00
Matthias
c6124180fe Fix bug when fetching orders fails 2020-06-28 19:45:42 +02:00
Matthias
6362bfc36e Fix calculate_backoff implementation 2020-06-28 19:41:21 +02:00
Matthias
cbcbb4bdb5 Rename get_stoploss_order to fetch_stoploss_order (align with fetch_order) 2020-06-28 16:30:24 +02:00
Matthias
92c70fb903 Rename get_order to fetch_order (to align to ccxt naming) 2020-06-28 16:27:35 +02:00
Matthias
e040c518ca Dynamic backoff on DDos errors 2020-06-28 16:19:12 +02:00
Matthias
29d3ff1bc9 Adjust tests to work with ExchangeError 2020-06-28 16:04:04 +02:00
Matthias
bf61bc9d83 Introduce ExchangeError 2020-06-28 16:01:40 +02:00
Matthias
e74d2af857 Have TemporaryError a subCategory of DependencyException
so it's safe to raise out of the exchange
2020-06-28 15:44:58 +02:00
Matthias
5bd4798ed0 Add retrier to stoploss calls (but without retrying) 2020-06-28 11:56:29 +02:00
Matthias
2c45114a64 Implement DDos backoff (1s) 2020-06-28 11:17:06 +02:00
Theagainmen
118f051171 Added message in cleanup and fixes 2020-06-28 11:02:50 +02:00
Theagainmen
e5676867a8 Trying to fix flake8 errors 2020-06-27 21:53:12 +02:00
Theagainmen
b938c536fa Trying to fix flake8 errors 2020-06-27 21:46:53 +02:00
Theagainmen
48289e8ca7 Added exchange name, removed capital letters 2020-06-27 20:24:50 +02:00
Theagainmen
0642ab76bf Added information to the new function 2020-06-27 18:40:44 +02:00
Theagainmen
e813573f27 Warning message for open trades when stopping bot 2020-06-27 18:35:46 +02:00
Matthias
6734269bfc Use >= to compare for winning trades 2020-06-25 19:22:50 +02:00
Matthias
0509b9a8fc Show winning vs. losing trades 2020-06-24 06:43:19 +02:00
Matthias
f976905728 Fix more exchange message typos 2020-06-18 20:00:18 +02:00
Matthias
45ffb26910 Merge branch 'develop' into pr/hroff-1902/3478 2020-06-18 19:54:46 +02:00
Matthias
eef3c01da7 Fix function header formatting 2020-06-18 19:49:05 +02:00
Matthias
f1993fb2f4 Pass analyzed dataframe to get_signal 2020-06-18 08:09:52 +02:00
Matthias
48225e0d80 Improve interface docstrings for analyze functions 2020-06-18 07:54:00 +02:00
Matthias
f2a778d294 Combine tests for empty dataframe 2020-06-18 07:03:30 +02:00
Matthias
8472fcfff9 Add empty to documentation 2020-06-18 06:50:06 +02:00
Matthias
ab9382434f Add test for get_analyzed_dataframe 2020-06-18 06:50:06 +02:00
Matthias
e5f7610b5d Add bot basics documentation 2020-06-18 06:50:06 +02:00
Matthias
8b186dbe0e Add additional test scenarios 2020-06-18 06:50:06 +02:00
Matthias
1c1a7150ae ensure confirm_trade_entry is called and has the desired effect 2020-06-18 06:50:06 +02:00
Matthias
7c3fb111f2 Confirm execute_sell calls confirm_trade_exit 2020-06-18 06:50:06 +02:00
Matthias
6d6e7196f4 Test trade entry / exit is called correctly 2020-06-18 06:50:06 +02:00
Matthias
84329ad2ca Add confirm_trade* methods to abort buying or selling 2020-06-18 06:50:06 +02:00
Matthias
de676bcaba Document get_analyzed_dataframe for dataprovider 2020-06-18 06:50:06 +02:00
Matthias
910100f1c8 Improve docstring comment 2020-06-18 06:50:06 +02:00
Matthias
dea7e3db01 Use supress_errors in strategy wrapper - ensure it's called once 2020-06-18 06:50:06 +02:00
Matthias
c047e48a47 Add errorsupression to safe wrapper 2020-06-18 06:50:06 +02:00
Matthias
bc821c7c20 Add documentation for bot_loop_start 2020-06-18 06:50:06 +02:00
Matthias
77056a3119 Add bot_loop_start callback 2020-06-18 06:50:06 +02:00
Matthias
7da955556d Add test for empty pair case 2020-06-18 06:50:06 +02:00
Matthias
8166b37253 Explicitly check if dp is available 2020-06-18 06:50:06 +02:00
Matthias
55fa514ec9 Adapt most tests 2020-06-18 06:50:05 +02:00
Matthias
273aaaff12 Introduce .analyze() function for Strategy
Fixing a few tests along the way
2020-06-18 06:50:05 +02:00
Matthias
95f3ac08d4 Update some comments 2020-06-18 06:50:05 +02:00
Matthias
9794914838 store dataframe updated as tuple 2020-06-18 06:50:05 +02:00
Matthias
fd97ad9b76 Cache analyzed dataframe 2020-06-18 06:50:05 +02:00
Matthias
0b2982caed Merge branch 'develop' into hyperopt_colorama_init 2020-06-16 10:16:41 +02:00
Matthias
9dba2a34f9 Add note for hyperopt color support on windows 2020-06-16 10:16:23 +02:00
hroff-1902
de36f3d850 Cosmetics in freqtradebot 2020-06-14 01:42:45 +03:00
hroff-1902
4660909e95 Validate stoploss_on_exchange_limit_ratio at startup time 2020-06-14 01:07:00 +03:00
hroff-1902
1bf333d320 Minor: fix test 2020-06-14 00:57:13 +03:00
hroff-1902
be03c22dba Minor: Fix exception message 2020-06-14 00:35:58 +03:00
Matthias
08049d23b4 Use "market_is_tradable" for whitelist validation 2020-06-02 20:41:29 +02:00
Matthias
b74a3addc6 Update tests 2020-06-02 20:30:31 +02:00
Matthias
b22e3a67d8 rename symbol_is_pair to market_is_tradable
Make it part of the exchange class, so subclasses can override this
2020-06-02 20:29:50 +02:00
Matthias
f6edb32a33 Run hyperopt with --print-all 2020-06-01 09:55:52 +02:00
Matthias
d9afef8fe1 Move colorama_init to where it was 2020-06-01 09:37:10 +02:00
Matthias
ffa93377b4 Test colorama init again (after the fixes done to progressbar) 2020-06-01 09:34:03 +02:00
Matthias
f3824d970b Use dict for symbol_is_pair 2020-05-29 20:20:06 +02:00
hroff-1902
bfa55f31c0 Remove wrong comment 2020-05-20 17:45:27 +03:00
hroff-1902
8bf38443c2 Merge branch 'develop' into verify_date_on_new_candle_on_get_signal 2020-05-20 14:05:21 +03:00
hroff-1902
7b2bb73a12 Merge branch 'develop' into verify_date_on_new_candle_on_get_signal 2020-05-19 21:34:58 +03:00
Yazeed Al Oyoun
c9711678fd fixed indent 2020-04-25 11:31:51 +02:00
Yazeed Al Oyoun
181b12b3a8 added wins/draws/losses 2020-04-25 11:31:51 +02:00
Yazeed Al Oyoun
72b088d85f added test 2020-04-25 11:31:51 +02:00
Yazeed Al Oyoun
6147498fd4 fixed indent 2020-04-25 11:31:51 +02:00
Yazeed Al Oyoun
2fb3d94938 added wins/draws/losses 2020-04-25 11:31:51 +02:00
Yazeed Al Oyoun
ef4426a65c added comma 2020-03-27 03:01:51 +01:00
Yazeed Al Oyoun
0a87fe76a3 unified language 2020-03-23 11:17:56 +01:00
Yazeed Al Oyoun
7143cac64f fixed wording of all in cli_options 2020-03-23 09:41:01 +01:00
Yazeed Al Oyoun
bf96ef08e0 added # flake8: noqa C901 2020-03-22 09:39:38 +01:00
Yazeed Al Oyoun
1976aaf13e initial push 2020-03-22 02:22:06 +01:00
Yazeed Al Oyoun
d752586b32 added test 2020-03-11 17:44:03 +01:00
Yazeed Al Oyoun
1395f65872 meh 2020-03-11 17:29:22 +01:00
Yazeed Al Oyoun
c442913feb final 2020-03-11 17:28:03 +01:00
Yazeed Al Oyoun
ba596af636 final? 2020-03-11 17:26:57 +01:00
Yazeed Al Oyoun
65a305c9ef fixed log message 2020-03-11 17:24:15 +01:00
Yazeed Al Oyoun
d25cf1395b fixed log message 2020-03-11 17:23:22 +01:00
Yazeed Al Oyoun
2e679ee2eb fixed log message 2020-03-11 17:22:21 +01:00
Yazeed Al Oyoun
dbe3c8654e fixed all, i hope 2020-03-11 17:16:21 +01:00
Yazeed Al Oyoun
7754742459 fix tests 2020-03-11 17:13:39 +01:00
Yazeed Al Oyoun
d667acb308 fixed typo 2020-03-11 17:10:57 +01:00
Yazeed Al Oyoun
a82cdf0add fixed test 2020-03-11 17:04:51 +01:00
Yazeed Al Oyoun
a85d17327b fix 2020-03-11 16:54:27 +01:00
Yazeed Al Oyoun
d239e99904 removed old code from create_trade 2020-03-11 16:49:37 +01:00
Yazeed Al Oyoun
4e45abbf13 added return false, false 2020-03-11 16:44:45 +01:00
Yazeed Al Oyoun
54bde6ac11 verify date on new candle before producing signal 2020-03-11 16:34:23 +01:00
117 changed files with 4241 additions and 1611 deletions

View File

@@ -1,17 +0,0 @@
version: 1
update_configs:
- package_manager: "python"
directory: "/"
update_schedule: "weekly"
allowed_updates:
- match:
update_type: "all"
target_branch: "develop"
- package_manager: "docker"
directory: "/"
update_schedule: "daily"
allowed_updates:
- match:
update_type: "all"

13
.github/dependabot.yml vendored Normal file
View File

@@ -0,0 +1,13 @@
version: 2
updates:
- package-ecosystem: docker
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: pip
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
target-branch: develop

View File

@@ -88,7 +88,7 @@ jobs:
run: | run: |
cp config.json.example config.json cp config.json.example config.json
freqtrade create-userdir --userdir user_data freqtrade create-userdir --userdir user_data
freqtrade hyperopt --datadir tests/testdata -e 5 --strategy SampleStrategy --hyperopt SampleHyperOpt freqtrade hyperopt --datadir tests/testdata -e 5 --strategy SampleStrategy --hyperopt SampleHyperOpt --print-all
- name: Flake8 - name: Flake8
run: | run: |
@@ -150,7 +150,7 @@ jobs:
run: | run: |
cp config.json.example config.json cp config.json.example config.json
freqtrade create-userdir --userdir user_data freqtrade create-userdir --userdir user_data
freqtrade hyperopt --datadir tests/testdata -e 5 --strategy SampleStrategy --hyperopt SampleHyperOpt freqtrade hyperopt --datadir tests/testdata -e 5 --strategy SampleStrategy --hyperopt SampleHyperOpt --print-all
- name: Flake8 - name: Flake8
run: | run: |

View File

@@ -1,7 +1,7 @@
FROM python:3.8.3-slim-buster FROM python:3.8.5-slim-buster
RUN apt-get update \ RUN apt-get update \
&& apt-get -y install curl build-essential libssl-dev \ && apt-get -y install curl build-essential libssl-dev sqlite3 \
&& apt-get clean \ && apt-get clean \
&& pip install --upgrade pip && pip install --upgrade pip

View File

@@ -1,7 +1,7 @@
FROM --platform=linux/arm/v7 python:3.7.7-slim-buster FROM --platform=linux/arm/v7 python:3.7.7-slim-buster
RUN apt-get update \ RUN apt-get update \
&& apt-get -y install curl build-essential libssl-dev libatlas3-base libgfortran5 \ && apt-get -y install curl build-essential libssl-dev libffi-dev libatlas3-base libgfortran5 sqlite3 \
&& apt-get clean \ && apt-get clean \
&& pip install --upgrade pip \ && pip install --upgrade pip \
&& echo "[global]\nextra-index-url=https://www.piwheels.org/simple" > /etc/pip.conf && echo "[global]\nextra-index-url=https://www.piwheels.org/simple" > /etc/pip.conf

View File

@@ -123,7 +123,6 @@ Telegram is not mandatory. However, this is a great way to control your bot. Mor
- `/help`: Show help message - `/help`: Show help message
- `/version`: Show version - `/version`: Show version
## Development branches ## Development branches
The project is currently setup in two main branches: The project is currently setup in two main branches:

View File

@@ -66,7 +66,7 @@
}, },
{"method": "AgeFilter", "min_days_listed": 10}, {"method": "AgeFilter", "min_days_listed": 10},
{"method": "PrecisionFilter"}, {"method": "PrecisionFilter"},
{"method": "PriceFilter", "low_price_ratio": 0.01}, {"method": "PriceFilter", "low_price_ratio": 0.01, "min_price": 0.00000010},
{"method": "SpreadFilter", "max_spread_ratio": 0.005} {"method": "SpreadFilter", "max_spread_ratio": 0.005}
], ],
"exchange": { "exchange": {

View File

@@ -66,7 +66,7 @@ Where `SampleStrategy1` and `AwesomeStrategy` refer to class names of strategies
#### Exporting trades to file #### Exporting trades to file
```bash ```bash
freqtrade backtesting --export trades freqtrade backtesting --export trades --config config.json --strategy SampleStrategy
``` ```
The exported trades can be used for [further analysis](#further-backtest-result-analysis), or can be used by the plotting script `plot_dataframe.py` in the scripts directory. The exported trades can be used for [further analysis](#further-backtest-result-analysis), or can be used by the plotting script `plot_dataframe.py` in the scripts directory.
@@ -157,17 +157,32 @@ A backtesting result will look like that:
| ADA/BTC | 1 | 0.89 | 0.89 | 0.00004434 | 0.44 | 6:00:00 | 1 | 0 | 0 | | ADA/BTC | 1 | 0.89 | 0.89 | 0.00004434 | 0.44 | 6:00:00 | 1 | 0 | 0 |
| LTC/BTC | 1 | 0.68 | 0.68 | 0.00003421 | 0.34 | 2:00:00 | 1 | 0 | 0 | | LTC/BTC | 1 | 0.68 | 0.68 | 0.00003421 | 0.34 | 2:00:00 | 1 | 0 | 0 |
| TOTAL | 2 | 0.78 | 1.57 | 0.00007855 | 0.78 | 4:00:00 | 2 | 0 | 0 | | TOTAL | 2 | 0.78 | 1.57 | 0.00007855 | 0.78 | 4:00:00 | 2 | 0 | 0 |
=============== SUMMARY METRICS ===============
| Metric | Value |
|-----------------------+---------------------|
| Backtesting from | 2019-01-01 00:00:00 |
| Backtesting to | 2019-05-01 00:00:00 |
| Total trades | 429 |
| First trade | 2019-01-01 18:30:00 |
| First trade Pair | EOS/USDT |
| Total Profit % | 152.41% |
| Trades per day | 3.575 |
| Best day | 25.27% |
| Worst day | -30.67% |
| Avg. Duration Winners | 4:23:00 |
| Avg. Duration Loser | 6:55:00 |
| | |
| Max Drawdown | 50.63% |
| Drawdown Start | 2019-02-15 14:10:00 |
| Drawdown End | 2019-04-11 18:15:00 |
| Market change | -5.88% |
===============================================
``` ```
### Backtesting report table
The 1st table contains all trades the bot made, including "left open trades". The 1st table contains all trades the bot made, including "left open trades".
The 2nd table contains a recap of sell reasons.
This table can tell you which area needs some additional work (i.e. all `sell_signal` trades are losses, so we should disable the sell-signal or work on improving that).
The 3rd table contains all trades the bot had to `forcesell` at the end of the backtest period to present a full picture.
This is necessary to simulate realistic behaviour, since the backtest period has to end at some point, while realistically, you could leave the bot running forever.
These trades are also included in the first table, but are extracted separately for clarity.
The last line will give you the overall performance of your strategy, The last line will give you the overall performance of your strategy,
here: here:
@@ -196,6 +211,58 @@ On the other hand, if you set a too high `minimal_roi` like `"0": 0.55`
(55%), there is almost no chance that the bot will ever reach this profit. (55%), there is almost no chance that the bot will ever reach this profit.
Hence, keep in mind that your performance is an integral mix of all different elements of the strategy, your configuration, and the crypto-currency pairs you have set up. Hence, keep in mind that your performance is an integral mix of all different elements of the strategy, your configuration, and the crypto-currency pairs you have set up.
### Sell reasons table
The 2nd table contains a recap of sell reasons.
This table can tell you which area needs some additional work (e.g. all or many of the `sell_signal` trades are losses, so you should work on improving the sell signal, or consider disabling it).
### Left open trades table
The 3rd table contains all trades the bot had to `forcesell` at the end of the backtesting period to present you the full picture.
This is necessary to simulate realistic behavior, since the backtest period has to end at some point, while realistically, you could leave the bot running forever.
These trades are also included in the first table, but are also shown separately in this table for clarity.
### Summary metrics
The last element of the backtest report is the summary metrics table.
It contains some useful key metrics about performance of your strategy on backtesting data.
```
=============== SUMMARY METRICS ===============
| Metric | Value |
|-----------------------+---------------------|
| Backtesting from | 2019-01-01 00:00:00 |
| Backtesting to | 2019-05-01 00:00:00 |
| Total trades | 429 |
| First trade | 2019-01-01 18:30:00 |
| First trade Pair | EOS/USDT |
| Total Profit % | 152.41% |
| Trades per day | 3.575 |
| Best day | 25.27% |
| Worst day | -30.67% |
| Avg. Duration Winners | 4:23:00 |
| Avg. Duration Loser | 6:55:00 |
| | |
| Max Drawdown | 50.63% |
| Drawdown Start | 2019-02-15 14:10:00 |
| Drawdown End | 2019-04-11 18:15:00 |
| Market change | -5.88% |
===============================================
```
- `Total trades`: Identical to the total trades of the backtest output table.
- `First trade`: First trade entered.
- `First trade pair`: Which pair was part of the first trade.
- `Backtesting from` / `Backtesting to`: Backtesting range (usually defined with the `--timerange` option).
- `Total Profit %`: Total profit per stake amount. Aligned to the TOTAL column of the first table.
- `Trades per day`: Total trades divided by the backtesting duration in days (this will give you information about how many trades to expect from the strategy).
- `Best day` / `Worst day`: Best and worst day based on daily profit.
- `Avg. Duration Winners` / `Avg. Duration Loser`: Average durations for winning and losing trades.
- `Max Drawdown`: Maximum drawdown experienced. For example, the value of 50% means that from highest to subsequent lowest point, a 50% drop was experienced).
- `Drawdown Start` / `Drawdown End`: Start and end datetimes for this largest drawdown (can also be visualized via the `plot-dataframe` sub-command).
- `Market change`: Change of the market during the backtest period. Calculated as average of all pairs changes from the first to the last candle using the "close" column.
### Assumptions made by backtesting ### Assumptions made by backtesting
Since backtesting lacks some detailed information about what happens within a candle, it needs to take a few assumptions: Since backtesting lacks some detailed information about what happens within a candle, it needs to take a few assumptions:

58
docs/bot-basics.md Normal file
View File

@@ -0,0 +1,58 @@
# Freqtrade basics
This page provides you some basic concepts on how Freqtrade works and operates.
## Freqtrade terminology
* Trade: Open position.
* Open Order: Order which is currently placed on the exchange, and is not yet complete.
* Pair: Tradable pair, usually in the format of Quote/Base (e.g. XRP/USDT).
* Timeframe: Candle length to use (e.g. `"5m"`, `"1h"`, ...).
* Indicators: Technical indicators (SMA, EMA, RSI, ...).
* Limit order: Limit orders which execute at the defined limit price or better.
* Market order: Guaranteed to fill, may move price depending on the order size.
## Fee handling
All profit calculations of Freqtrade include fees. For Backtesting / Hyperopt / Dry-run modes, the exchange default fee is used (lowest tier on the exchange). For live operations, fees are used as applied by the exchange (this includes BNB rebates etc.).
## Bot execution logic
Starting freqtrade in dry-run or live mode (using `freqtrade trade`) will start the bot and start the bot iteration loop.
By default, loop runs every few seconds (`internals.process_throttle_secs`) and does roughly the following in the following sequence:
* Fetch open trades from persistence.
* Calculate current list of tradable pairs.
* Download ohlcv data for the pairlist including all [informative pairs](strategy-customization.md#get-data-for-non-tradeable-pairs)
This step is only executed once per Candle to avoid unnecessary network traffic.
* Call `bot_loop_start()` strategy callback.
* Analyze strategy per pair.
* Call `populate_indicators()`
* Call `populate_buy_trend()`
* Call `populate_sell_trend()`
* Check timeouts for open orders.
* Calls `check_buy_timeout()` strategy callback for open buy orders.
* Calls `check_sell_timeout()` strategy callback for open sell orders.
* Verifies existing positions and eventually places sell orders.
* Considers stoploss, ROI and sell-signal.
* Determine sell-price based on `ask_strategy` configuration setting.
* Before a sell order is placed, `confirm_trade_exit()` strategy callback is called.
* Check if trade-slots are still available (if `max_open_trades` is reached).
* Verifies buy signal trying to enter new positions.
* Determine buy-price based on `bid_strategy` configuration setting.
* Before a buy order is placed, `confirm_trade_entry()` strategy callback is called.
This loop will be repeated again and again until the bot is stopped.
## Backtesting / Hyperopt execution logic
[backtesting](backtesting.md) or [hyperopt](hyperopt.md) do only part of the above logic, since most of the trading operations are fully simulated.
* Load historic data for configured pairlist.
* Calculate indicators (calls `populate_indicators()`).
* Calls `populate_buy_trend()` and `populate_sell_trend()`
* Loops per candle simulating entry and exit points.
* Generate backtest report output
!!! Note
Both Backtesting and Hyperopt include exchange default Fees in the calculation. Custom fees can be passed to backtesting / hyperopt by specifying the `--fee` argument.

View File

@@ -55,9 +55,9 @@ Mandatory parameters are marked as **Required**, which means that they are requi
| `process_only_new_candles` | Enable processing of indicators only when new candles arrive. If false each loop populates the indicators, this will mean the same candle is processed many times creating system load but can be useful of your strategy depends on tick data not only candle. [Strategy Override](#parameters-in-the-strategy). <br>*Defaults to `false`.* <br> **Datatype:** Boolean | `process_only_new_candles` | Enable processing of indicators only when new candles arrive. If false each loop populates the indicators, this will mean the same candle is processed many times creating system load but can be useful of your strategy depends on tick data not only candle. [Strategy Override](#parameters-in-the-strategy). <br>*Defaults to `false`.* <br> **Datatype:** Boolean
| `minimal_roi` | **Required.** Set the threshold as ratio the bot will use to sell a trade. [More information below](#understand-minimal_roi). [Strategy Override](#parameters-in-the-strategy). <br> **Datatype:** Dict | `minimal_roi` | **Required.** Set the threshold as ratio the bot will use to sell a trade. [More information below](#understand-minimal_roi). [Strategy Override](#parameters-in-the-strategy). <br> **Datatype:** Dict
| `stoploss` | **Required.** Value as ratio of the stoploss used by the bot. More details in the [stoploss documentation](stoploss.md). [Strategy Override](#parameters-in-the-strategy). <br> **Datatype:** Float (as ratio) | `stoploss` | **Required.** Value as ratio of the stoploss used by the bot. More details in the [stoploss documentation](stoploss.md). [Strategy Override](#parameters-in-the-strategy). <br> **Datatype:** Float (as ratio)
| `trailing_stop` | Enables trailing stoploss (based on `stoploss` in either configuration or strategy file). More details in the [stoploss documentation](stoploss.md). [Strategy Override](#parameters-in-the-strategy). <br> **Datatype:** Boolean | `trailing_stop` | Enables trailing stoploss (based on `stoploss` in either configuration or strategy file). More details in the [stoploss documentation](stoploss.md#trailing-stop-loss). [Strategy Override](#parameters-in-the-strategy). <br> **Datatype:** Boolean
| `trailing_stop_positive` | Changes stoploss once profit has been reached. More details in the [stoploss documentation](stoploss.md). [Strategy Override](#parameters-in-the-strategy). <br> **Datatype:** Float | `trailing_stop_positive` | Changes stoploss once profit has been reached. More details in the [stoploss documentation](stoploss.md#trailing-stop-loss-custom-positive-loss). [Strategy Override](#parameters-in-the-strategy). <br> **Datatype:** Float
| `trailing_stop_positive_offset` | Offset on when to apply `trailing_stop_positive`. Percentage value which should be positive. More details in the [stoploss documentation](stoploss.md). [Strategy Override](#parameters-in-the-strategy). <br>*Defaults to `0.0` (no offset).* <br> **Datatype:** Float | `trailing_stop_positive_offset` | Offset on when to apply `trailing_stop_positive`. Percentage value which should be positive. More details in the [stoploss documentation](stoploss.md#trailing-stop-loss-only-once-the-trade-has-reached-a-certain-offset). [Strategy Override](#parameters-in-the-strategy). <br>*Defaults to `0.0` (no offset).* <br> **Datatype:** Float
| `trailing_only_offset_is_reached` | Only apply trailing stoploss when the offset is reached. [stoploss documentation](stoploss.md). [Strategy Override](#parameters-in-the-strategy). <br>*Defaults to `false`.* <br> **Datatype:** Boolean | `trailing_only_offset_is_reached` | Only apply trailing stoploss when the offset is reached. [stoploss documentation](stoploss.md). [Strategy Override](#parameters-in-the-strategy). <br>*Defaults to `false`.* <br> **Datatype:** Boolean
| `unfilledtimeout.buy` | **Required.** How long (in minutes) the bot will wait for an unfilled buy order to complete, after which the order will be cancelled. [Strategy Override](#parameters-in-the-strategy).<br> **Datatype:** Integer | `unfilledtimeout.buy` | **Required.** How long (in minutes) the bot will wait for an unfilled buy order to complete, after which the order will be cancelled. [Strategy Override](#parameters-in-the-strategy).<br> **Datatype:** Integer
| `unfilledtimeout.sell` | **Required.** How long (in minutes) the bot will wait for an unfilled sell order to complete, after which the order will be cancelled. [Strategy Override](#parameters-in-the-strategy).<br> **Datatype:** Integer | `unfilledtimeout.sell` | **Required.** How long (in minutes) the bot will wait for an unfilled sell order to complete, after which the order will be cancelled. [Strategy Override](#parameters-in-the-strategy).<br> **Datatype:** Integer
@@ -275,27 +275,16 @@ the static list of pairs) if we should buy.
The `order_types` configuration parameter maps actions (`buy`, `sell`, `stoploss`, `emergencysell`) to order-types (`market`, `limit`, ...) as well as configures stoploss to be on the exchange and defines stoploss on exchange update interval in seconds. The `order_types` configuration parameter maps actions (`buy`, `sell`, `stoploss`, `emergencysell`) to order-types (`market`, `limit`, ...) as well as configures stoploss to be on the exchange and defines stoploss on exchange update interval in seconds.
This allows to buy using limit orders, sell using This allows to buy using limit orders, sell using
limit-orders, and create stoplosses using using market orders. It also allows to set the limit-orders, and create stoplosses using market orders. It also allows to set the
stoploss "on exchange" which means stoploss order would be placed immediately once stoploss "on exchange" which means stoploss order would be placed immediately once
the buy order is fulfilled. the buy order is fulfilled.
If `stoploss_on_exchange` and `trailing_stop` are both set, then the bot will use `stoploss_on_exchange_interval` to check and update the stoploss on exchange periodically.
`order_types` can be set in the configuration file or in the strategy.
`order_types` set in the configuration file overwrites values set in the strategy as a whole, so you need to configure the whole `order_types` dictionary in one place. `order_types` set in the configuration file overwrites values set in the strategy as a whole, so you need to configure the whole `order_types` dictionary in one place.
If this is configured, the following 4 values (`buy`, `sell`, `stoploss` and If this is configured, the following 4 values (`buy`, `sell`, `stoploss` and
`stoploss_on_exchange`) need to be present, otherwise the bot will fail to start. `stoploss_on_exchange`) need to be present, otherwise the bot will fail to start.
`emergencysell` is an optional value, which defaults to `market` and is used when creating stoploss on exchange orders fails. For information on (`emergencysell`,`stoploss_on_exchange`,`stoploss_on_exchange_interval`,`stoploss_on_exchange_limit_ratio`) please see stop loss documentation [stop loss on exchange](stoploss.md)
The below is the default which is used if this is not configured in either strategy or configuration file.
Not all Exchanges support `stoploss_on_exchange`. If an exchange supports both limit and market stoploss orders, then the value of `stoploss` will be used to determine the stoploss type.
If `stoploss_on_exchange` uses limit orders, the exchange needs 2 prices, the stoploss_price and the Limit price.
`stoploss` defines the stop-price - and limit should be slightly below this.
This defaults to 0.99 / 1% (configurable via `stoploss_on_exchange_limit_ratio`).
Calculation example: we bought the asset at 100$.
Stop-price is 95$, then limit would be `95 * 0.99 = 94.05$` - so the stoploss will happen between 95$ and 94.05$.
Syntax for Strategy: Syntax for Strategy:
@@ -662,16 +651,29 @@ Filters low-value coins which would not allow setting stoplosses.
#### PriceFilter #### PriceFilter
The `PriceFilter` allows filtering of pairs by price. The `PriceFilter` allows filtering of pairs by price. Currently the following price filters are supported:
Currently, only `low_price_ratio` setting is implemented, where a raise of 1 price unit (pip) is below the `low_price_ratio` ratio. * `min_price`
This option is disabled by default, and will only apply if set to <> 0. * `max_price`
* `low_price_ratio`
The `min_price` setting removes pairs where the price is below the specified price. This is useful if you wish to avoid trading very low-priced pairs.
This option is disabled by default, and will only apply if set to > 0.
The `max_price` setting removes pairs where the price is above the specified price. This is useful if you wish to trade only low-priced pairs.
This option is disabled by default, and will only apply if set to > 0.
The `low_price_ratio` setting removes pairs where a raise of 1 price unit (pip) is above the `low_price_ratio` ratio.
This option is disabled by default, and will only apply if set to > 0.
For `PriceFiler` at least one of its `min_price`, `max_price` or `low_price_ratio` settings must be applied.
Calculation example: Calculation example:
Min price precision is 8 decimals. If price is 0.00000011 - one step would be 0.00000012 - which is almost 10% higher than the previous value. Min price precision for SHITCOIN/BTC is 8 decimals. If its price is 0.00000011 - one price step above would be 0.00000012, which is ~9% higher than the previous price value. You may filter out this pair by using PriceFilter with `low_price_ratio` set to 0.09 (9%) or with `min_price` set to 0.00000011, correspondingly.
These pairs are dangerous since it may be impossible to place the desired stoploss - and often result in high losses. Here is what the PriceFilters takes over. !!! Warning "Low priced pairs"
Low priced pairs with high "1 pip movements" are dangerous since they are often illiquid and it may also be impossible to place the desired stoploss, which can often result in high losses since price needs to be rounded to the next tradable price - so instead of having a stoploss of -5%, you could end up with a stoploss of -9% simply due to price rounding.
#### ShuffleFilter #### ShuffleFilter

View File

@@ -158,6 +158,58 @@ It'll also remove original jsongz data files (`--erase` parameter).
freqtrade convert-trade-data --format-from jsongz --format-to json --datadir ~/.freqtrade/data/kraken --erase freqtrade convert-trade-data --format-from jsongz --format-to json --datadir ~/.freqtrade/data/kraken --erase
``` ```
### Subcommand list-data
You can get a list of downloaded data using the `list-data` subcommand.
```
usage: freqtrade list-data [-h] [-v] [--logfile FILE] [-V] [-c PATH] [-d PATH]
[--userdir PATH] [--exchange EXCHANGE]
[--data-format-ohlcv {json,jsongz}]
[-p PAIRS [PAIRS ...]]
optional arguments:
-h, --help show this help message and exit
--exchange EXCHANGE Exchange name (default: `bittrex`). Only valid if no
config is provided.
--data-format-ohlcv {json,jsongz}
Storage format for downloaded candle (OHLCV) data.
(default: `json`).
-p PAIRS [PAIRS ...], --pairs PAIRS [PAIRS ...]
Show profits for only these pairs. Pairs are space-
separated.
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
Path to directory with historical backtesting data.
--userdir PATH, --user-data-dir PATH
Path to userdata directory.
```
#### Example list-data
```bash
> freqtrade list-data --userdir ~/.freqtrade/user_data/
Found 33 pair / timeframe combinations.
pairs timeframe
---------- -----------------------------------------
ADA/BTC 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d
ADA/ETH 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d
ETH/BTC 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d
ETH/USDT 5m, 15m, 30m, 1h, 2h, 4h
```
### Pairs file ### Pairs file
In alternative to the whitelist from `config.json`, a `pairs.json` file can be used. In alternative to the whitelist from `config.json`, a `pairs.json` file can be used.

View File

@@ -9,21 +9,20 @@ and are no longer supported. Please avoid their usage in your configuration.
### the `--refresh-pairs-cached` command line option ### the `--refresh-pairs-cached` command line option
`--refresh-pairs-cached` in the context of backtesting, hyperopt and edge allows to refresh candle data for backtesting. `--refresh-pairs-cached` in the context of backtesting, hyperopt and edge allows to refresh candle data for backtesting.
Since this leads to much confusion, and slows down backtesting (while not being part of backtesting) this has been singled out Since this leads to much confusion, and slows down backtesting (while not being part of backtesting) this has been singled out as a separate freqtrade sub-command `freqtrade download-data`.
as a seperate freqtrade subcommand `freqtrade download-data`.
This command line option was deprecated in 2019.7-dev (develop branch) and removed in 2019.9 (master branch). This command line option was deprecated in 2019.7-dev (develop branch) and removed in 2019.9.
### The **--dynamic-whitelist** command line option ### The **--dynamic-whitelist** command line option
This command line option was deprecated in 2018 and removed freqtrade 2019.6-dev (develop branch) This command line option was deprecated in 2018 and removed freqtrade 2019.6-dev (develop branch)
and in freqtrade 2019.7 (master branch). and in freqtrade 2019.7.
### the `--live` command line option ### the `--live` command line option
`--live` in the context of backtesting allowed to download the latest tick data for backtesting. `--live` in the context of backtesting allowed to download the latest tick data for backtesting.
Did only download the latest 500 candles, so was ineffective in getting good backtest data. Did only download the latest 500 candles, so was ineffective in getting good backtest data.
Removed in 2019-7-dev (develop branch) and in freqtrade 2019-8 (master branch) Removed in 2019-7-dev (develop branch) and in freqtrade 2019.8.
### Allow running multiple pairlists in sequence ### Allow running multiple pairlists in sequence
@@ -31,6 +30,6 @@ The former `"pairlist"` section in the configuration has been removed, and is re
The old section of configuration parameters (`"pairlist"`) has been deprecated in 2019.11 and has been removed in 2020.4. The old section of configuration parameters (`"pairlist"`) has been deprecated in 2019.11 and has been removed in 2020.4.
### deprecation of bidVolume and askVolume from volumepairlist ### deprecation of bidVolume and askVolume from volume-pairlist
Since only quoteVolume can be compared between assets, the other options (bidVolume, askVolume) have been deprecated in 2020.4. Since only quoteVolume can be compared between assets, the other options (bidVolume, askVolume) have been deprecated in 2020.4.

View File

@@ -85,6 +85,35 @@ docker-compose exec freqtrade_develop /bin/bash
![image](https://user-images.githubusercontent.com/419355/65456522-ba671a80-de06-11e9-9598-df9ca0d8dcac.png) ![image](https://user-images.githubusercontent.com/419355/65456522-ba671a80-de06-11e9-9598-df9ca0d8dcac.png)
## ErrorHandling
Freqtrade Exceptions all inherit from `FreqtradeException`.
This general class of error should however not be used directly. Instead, multiple specialized sub-Exceptions exist.
Below is an outline of exception inheritance hierarchy:
```
+ FreqtradeException
|
+---+ OperationalException
|
+---+ DependencyException
| |
| +---+ PricingError
| |
| +---+ ExchangeError
| |
| +---+ TemporaryError
| |
| +---+ DDosProtection
| |
| +---+ InvalidOrderException
| |
| +---+ RetryableOrderError
|
+---+ StrategyError
```
## Modules ## Modules
### Dynamic Pairlist ### Dynamic Pairlist

View File

@@ -6,7 +6,8 @@ This page explains how to use Edge Positioning module in your bot in order to en
Edge positioning is not compatible with dynamic (volume-based) whitelist. Edge positioning is not compatible with dynamic (volume-based) whitelist.
!!! Note !!! Note
Edge does not consider anything else than buy/sell/stoploss signals. So trailing stoploss, ROI, and everything else are ignored in its calculation. Edge does not consider anything other than *its own* buy/sell/stoploss signals. It ignores the stoploss, trailing stoploss, and ROI settings in the strategy configuration file.
Therefore, it is important to understand that Edge can improve the performance of some trading strategies but *decrease* the performance of others.
## Introduction ## Introduction
@@ -89,7 +90,7 @@ You can also use this value to evaluate the effectiveness of modifications to th
## How does it work? ## How does it work?
If enabled in config, Edge will go through historical data with a range of stoplosses in order to find buy and sell/stoploss signals. It then calculates win rate and expectancy over *N* trades for each stoploss. Here is an example: Edge combines dynamic stoploss, dynamic positions, and whitelist generation into one isolated module which is then applied to the trading strategy. If enabled in config, Edge will go through historical data with a range of stoplosses in order to find buy and sell/stoploss signals. It then calculates win rate and expectancy over *N* trades for each stoploss. Here is an example:
| Pair | Stoploss | Win Rate | Risk Reward Ratio | Expectancy | | Pair | Stoploss | Win Rate | Risk Reward Ratio | Expectancy |
|----------|:-------------:|-------------:|------------------:|-----------:| |----------|:-------------:|-------------:|------------------:|-----------:|
@@ -186,6 +187,12 @@ An example of its output:
| APPC/BTC | -0.02 | 0.44 | 2.28 | 1.27 | 0.44 | 25 | 43 | | APPC/BTC | -0.02 | 0.44 | 2.28 | 1.27 | 0.44 | 25 | 43 |
| NEBL/BTC | -0.03 | 0.63 | 1.29 | 0.58 | 0.44 | 19 | 59 | | NEBL/BTC | -0.03 | 0.63 | 1.29 | 0.58 | 0.44 | 19 | 59 |
Edge produced the above table by comparing `calculate_since_number_of_days` to `minimum_expectancy` to find `min_trade_number` historical information based on the config file. The timerange Edge uses for its comparisons can be further limited by using the `--timerange` switch.
In live and dry-run modes, after the `process_throttle_secs` has passed, Edge will again process `calculate_since_number_of_days` against `minimum_expectancy` to find `min_trade_number`. If no `min_trade_number` is found, the bot will return "whitelist empty". Depending on the trade strategy being deployed, "whitelist empty" may be return much of the time - or *all* of the time. The use of Edge may also cause trading to occur in bursts, though this is rare.
If you encounter "whitelist empty" a lot, condsider tuning `calculate_since_number_of_days`, `minimum_expectancy` and `min_trade_number` to align to the trading frequency of your strategy.
### Update cached pairs with the latest data ### Update cached pairs with the latest data
Edge requires historic data the same way as backtesting does. Edge requires historic data the same way as backtesting does.

View File

@@ -1,5 +1,9 @@
# Freqtrade FAQ # Freqtrade FAQ
## Beginner Tips & Tricks
* When you work with your strategy & hyperopt file you should use a proper code editor like vscode or Pycharm. A good code editor will provide syntax highlighting as well as line numbers, making it easy to find syntax errors (most likely, pointed out by Freqtrade during startup).
## Freqtrade common issues ## Freqtrade common issues
### The bot does not start ### The bot does not start
@@ -15,10 +19,12 @@ This could have the following reasons:
### I have waited 5 minutes, why hasn't the bot made any trades yet?! ### I have waited 5 minutes, why hasn't the bot made any trades yet?!
Depending on the buy strategy, the amount of whitelisted coins, the * Depending on the buy strategy, the amount of whitelisted coins, the
situation of the market etc, it can take up to hours to find good entry situation of the market etc, it can take up to hours to find good entry
position for a trade. Be patient! position for a trade. Be patient!
* Or it may because of a configuration error? Best check the logs, it's usually telling you if the bot is simply not getting buy signals (only heartbeat messages), or if there is something wrong (errors / exceptions in the log).
### I have made 12 trades already, why is my total profit negative?! ### I have made 12 trades already, why is my total profit negative?!
I understand your disappointment but unfortunately 12 trades is just I understand your disappointment but unfortunately 12 trades is just
@@ -129,25 +135,27 @@ to find a great result (unless if you are very lucky), so you probably
have to run it for 10.000 or more. But it will take an eternity to have to run it for 10.000 or more. But it will take an eternity to
compute. compute.
We recommend you to run it at least 10.000 epochs: Since hyperopt uses Bayesian search, running for too many epochs may not produce greater results.
It's therefore recommended to run between 500-1000 epochs over and over until you hit at least 10.000 epocs in total (or are satisfied with the result). You can best judge by looking at the results - if the bot keeps discovering better strategies, it's best to keep on going.
```bash ```bash
freqtrade hyperopt -e 10000 freqtrade hyperopt -e 1000
``` ```
or if you want intermediate result to see or if you want intermediate result to see
```bash ```bash
for i in {1..100}; do freqtrade hyperopt -e 100; done for i in {1..100}; do freqtrade hyperopt -e 1000; done
``` ```
### Why it is so long to run hyperopt? ### Why does it take a long time to run hyperopt?
Finding a great Hyperopt results takes time. * Discovering a great strategy with Hyperopt takes time. Study www.freqtrade.io, the Freqtrade Documentation page, join the Freqtrade [Slack community](https://join.slack.com/t/highfrequencybot/shared_invite/enQtNjU5ODcwNjI1MDU3LTU1MTgxMjkzNmYxNWE1MDEzYzQ3YmU4N2MwZjUyNjJjODRkMDVkNjg4YTAyZGYzYzlhOTZiMTE4ZjQ4YzM0OGE) - or the Freqtrade [discord community](https://discord.gg/X89cVG). While you patiently wait for the most advanced, free crypto bot in the world, to hand you a possible golden strategy specially designed just for you.
If you wonder why it takes a while to find great hyperopt results * If you wonder why it can take from 20 minutes to days to do 1000 epocs here are some answers:
This answer was written during the under the release 0.15.1, when we had: This answer was written during the release 0.15.1, when we had:
- 8 triggers - 8 triggers
- 9 guards: let's say we evaluate even 10 values from each - 9 guards: let's say we evaluate even 10 values from each
@@ -157,7 +165,14 @@ The following calculation is still very rough and not very precise
but it will give the idea. With only these triggers and guards there is but it will give the idea. With only these triggers and guards there is
already 8\*10^9\*10 evaluations. A roughly total of 80 billion evals. already 8\*10^9\*10 evaluations. A roughly total of 80 billion evals.
Did you run 100 000 evals? Congrats, you've done roughly 1 / 100 000 th Did you run 100 000 evals? Congrats, you've done roughly 1 / 100 000 th
of the search space. of the search space, assuming that the bot never tests the same parameters more than once.
* The time it takes to run 1000 hyperopt epocs depends on things like: The available cpu, harddisk, ram, timeframe, timerange, indicator settings, indicator count, amount of coins that hyperopt test strategies on and the resulting trade count - which can be 650 trades in a year or 10.0000 trades depending if the strategy aims for big profits by trading rarely or for many low profit trades.
Example: 4% profit 650 times vs 0,3% profit a trade 10.000 times in a year. If we assume you set the --timerange to 365 days.
Example:
`freqtrade --config config.json --strategy SampleStrategy --hyperopt SampleHyperopt -e 1000 --timerange 20190601-20200601`
## Edge module ## Edge module

View File

@@ -370,6 +370,9 @@ By default, hyperopt prints colorized results -- epochs with positive profit are
You can use the `--print-all` command line option if you would like to see all results in the hyperopt output, not only the best ones. When `--print-all` is used, current best results are also colorized by default -- they are printed in bold (bright) style. This can also be switched off with the `--no-color` command line option. You can use the `--print-all` command line option if you would like to see all results in the hyperopt output, not only the best ones. When `--print-all` is used, current best results are also colorized by default -- they are printed in bold (bright) style. This can also be switched off with the `--no-color` command line option.
!!! Note "Windows and color output"
Windows does not support color-output nativly, therefore it is automatically disabled. To have color-output for hyperopt running under windows, please consider using WSL.
### Understand Hyperopt ROI results ### Understand Hyperopt ROI results
If you are optimizing ROI (i.e. if optimization search-space contains 'all', 'default' or 'roi'), your result will look as follows and include a ROI table: If you are optimizing ROI (i.e. if optimization search-space contains 'all', 'default' or 'roi'), your result will look as follows and include a ROI table:
@@ -498,8 +501,3 @@ After you run Hyperopt for the desired amount of epochs, you can later list all
Once the optimized strategy has been implemented into your strategy, you should backtest this strategy to make sure everything is working as expected. Once the optimized strategy has been implemented into your strategy, you should backtest this strategy to make sure everything is working as expected.
To achieve same results (number of trades, their durations, profit, etc.) than during Hyperopt, please use same set of arguments `--dmmp`/`--disable-max-market-positions` and `--eps`/`--enable-position-stacking` for Backtesting. To achieve same results (number of trades, their durations, profit, etc.) than during Hyperopt, please use same set of arguments `--dmmp`/`--disable-max-market-positions` and `--eps`/`--enable-position-stacking` for Backtesting.
## Next Step
Now you have a perfect bot and want to control it from Telegram. Your
next step is to learn the [Telegram usage](telegram-usage.md).

View File

@@ -224,7 +224,8 @@ Possible options for the `freqtrade plot-profit` subcommand:
``` ```
usage: freqtrade plot-profit [-h] [-v] [--logfile FILE] [-V] [-c PATH] usage: freqtrade plot-profit [-h] [-v] [--logfile FILE] [-V] [-c PATH]
[-d PATH] [--userdir PATH] [-p PAIRS [PAIRS ...]] [-d PATH] [--userdir PATH] [-s NAME]
[--strategy-path PATH] [-p PAIRS [PAIRS ...]]
[--timerange TIMERANGE] [--export EXPORT] [--timerange TIMERANGE] [--export EXPORT]
[--export-filename PATH] [--db-url PATH] [--export-filename PATH] [--db-url PATH]
[--trade-source {DB,file}] [-i TIMEFRAME] [--trade-source {DB,file}] [-i TIMEFRAME]
@@ -270,6 +271,11 @@ Common arguments:
--userdir PATH, --user-data-dir PATH --userdir PATH, --user-data-dir PATH
Path to userdata directory. Path to userdata directory.
Strategy arguments:
-s NAME, --strategy NAME
Specify strategy class name which will be used by the
bot.
--strategy-path PATH Specify additional strategy lookup path.
``` ```
The `-p/--pairs` argument, can be used to limit the pairs that are considered for this calculation. The `-p/--pairs` argument, can be used to limit the pairs that are considered for this calculation.
@@ -279,7 +285,7 @@ Examples:
Use custom backtest-export file Use custom backtest-export file
``` bash ``` bash
freqtrade plot-profit -p LTC/BTC --export-filename user_data/backtest_results/backtest-result-Strategy005.json freqtrade plot-profit -p LTC/BTC --export-filename user_data/backtest_results/backtest-result.json
``` ```
Use custom database Use custom database

View File

@@ -1,2 +1,2 @@
mkdocs-material==5.3.3 mkdocs-material==5.5.8
mdx_truly_sane_lists==1.2 mdx_truly_sane_lists==1.2

View File

@@ -46,7 +46,7 @@ secrets.token_hex()
### Configuration with docker ### Configuration with docker
If you run your bot using docker, you'll need to have the bot listen to incomming connections. The security is then handled by docker. If you run your bot using docker, you'll need to have the bot listen to incoming connections. The security is then handled by docker.
``` json ``` json
"api_server": { "api_server": {
@@ -106,26 +106,30 @@ python3 scripts/rest_client.py --config rest_config.json <command> [optional par
## Available commands ## Available commands
| Command | Default | Description | | Command | Description |
|----------|---------|-------------| |----------|-------------|
| `start` | | Starts the trader | `ping` | Simple command testing the API Readiness - requires no authentication.
| `stop` | | Stops the trader | `start` | Starts the trader
| `stopbuy` | | Stops the trader from opening new trades. Gracefully closes open trades according to their rules. | `stop` | Stops the trader
| `reload_config` | | Reloads the configuration file | `stopbuy` | Stops the trader from opening new trades. Gracefully closes open trades according to their rules.
| `show_config` | | Shows part of the current configuration with relevant settings to operation | `reload_config` | Reloads the configuration file
| `status` | | Lists all open trades | `trades` | List last trades.
| `count` | | Displays number of trades used and available | `delete_trade <trade_id>` | Remove trade from the database. Tries to close open orders. Requires manual handling of this trade on the exchange.
| `profit` | | Display a summary of your profit/loss from close trades and some stats about your performance | `show_config` | Shows part of the current configuration with relevant settings to operation
| `forcesell <trade_id>` | | Instantly sells the given trade (Ignoring `minimum_roi`). | `logs` | Shows last log messages
| `forcesell all` | | Instantly sells all open trades (Ignoring `minimum_roi`). | `status` | Lists all open trades
| `forcebuy <pair> [rate]` | | Instantly buys the given pair. Rate is optional. (`forcebuy_enable` must be set to True) | `count` | Displays number of trades used and available
| `performance` | | Show performance of each finished trade grouped by pair | `profit` | Display a summary of your profit/loss from close trades and some stats about your performance
| `balance` | | Show account balance per currency | `forcesell <trade_id>` | Instantly sells the given trade (Ignoring `minimum_roi`).
| `daily <n>` | 7 | Shows profit or loss per day, over the last n days | `forcesell all` | Instantly sells all open trades (Ignoring `minimum_roi`).
| `whitelist` | | Show the current whitelist | `forcebuy <pair> [rate]` | Instantly buys the given pair. Rate is optional. (`forcebuy_enable` must be set to True)
| `blacklist [pair]` | | Show the current blacklist, or adds a pair to the blacklist. | `performance` | Show performance of each finished trade grouped by pair
| `edge` | | Show validated pairs by Edge if it is enabled. | `balance` | Show account balance per currency
| `version` | | Show version | `daily <n>` | Shows profit or loss per day, over the last n days (n defaults to 7)
| `whitelist` | Show the current whitelist
| `blacklist [pair]` | Show the current blacklist, or adds a pair to the blacklist.
| `edge` | Show validated pairs by Edge if it is enabled.
| `version` | Show version
Possible commands can be listed from the rest-client script using the `help` command. Possible commands can be listed from the rest-client script using the `help` command.
@@ -135,78 +139,83 @@ python3 scripts/rest_client.py help
``` output ``` output
Possible commands: Possible commands:
balance balance
Get the account balance Get the account balance.
:returns: json object
blacklist blacklist
Show the current blacklist Show the current blacklist.
:param add: List of coins to add (example: "BNB/BTC") :param add: List of coins to add (example: "BNB/BTC")
:returns: json object
count count
Returns the amount of open trades Return the amount of open trades.
:returns: json object
daily daily
Returns the amount of open trades Return the amount of open trades.
:returns: json object
delete_trade
Delete trade from the database.
Tries to close open orders. Requires manual handling of this asset on the exchange.
:param trade_id: Deletes the trade with this ID from the database.
edge edge
Returns information about edge Return information about edge.
:returns: json object
forcebuy forcebuy
Buy an asset Buy an asset.
:param pair: Pair to buy (ETH/BTC) :param pair: Pair to buy (ETH/BTC)
:param price: Optional - price to buy :param price: Optional - price to buy
:returns: json object of the trade
forcesell forcesell
Force-sell a trade Force-sell a trade.
:param tradeid: Id of the trade (can be received via status command) :param tradeid: Id of the trade (can be received via status command)
:returns: json object
logs
Show latest logs.
:param limit: Limits log messages to the last <limit> logs. No limit to get all the trades.
performance performance
Returns the performance of the different coins Return the performance of the different coins.
:returns: json object
profit profit
Returns the profit summary Return the profit summary.
:returns: json object
reload_config reload_config
Reload configuration Reload configuration.
:returns: json object
show_config show_config
Returns part of the configuration, relevant for trading operations. Returns part of the configuration, relevant for trading operations.
:return: json object containing the version
start start
Start the bot if it's in stopped state. Start the bot if it's in the stopped state.
:returns: json object
status status
Get the status of open trades Get the status of open trades.
:returns: json object
stop stop
Stop the bot. Use start to restart Stop the bot. Use `start` to restart.
:returns: json object
stopbuy stopbuy
Stop buying (but handle sells gracefully). Stop buying (but handle sells gracefully). Use `reload_config` to reset.
use reload_config to reset
:returns: json object trades
Return trades history.
:param limit: Limits trades to the X last trades. No limit to get all the trades.
version version
Returns the version of the bot Return the version of the bot.
:returns: json object containing the version
whitelist whitelist
Show the current whitelist Show the current whitelist.
:returns: json object
``` ```
## Advanced API usage using JWT tokens ## Advanced API usage using JWT tokens

View File

@@ -1,104 +1,59 @@
# Sandbox API testing # Sandbox API testing
Where an exchange provides a sandbox for risk-free integration, or end-to-end, testing CCXT provides access to these. Some exchanges provide sandboxes or testbeds for risk-free testing, while running the bot against a real exchange.
With some configuration, freqtrade (in combination with ccxt) provides access to these.
This document is a *light overview of configuring Freqtrade and GDAX sandbox. This document is an overview to configure Freqtrade to be used with sandboxes.
This can be useful to developers and trader alike as Freqtrade is quite customisable. This can be useful to developers and trader alike.
When testing your API connectivity, make sure to use the following URLs. ## Exchanges known to have a sandbox / testnet
***Website**
https://public.sandbox.gdax.com * [binance](https://testnet.binance.vision/)
***REST API** * [coinbasepro](https://public.sandbox.pro.coinbase.com)
https://api-public.sandbox.gdax.com * [gemini](https://exchange.sandbox.gemini.com/)
* [huobipro](https://www.testnet.huobi.pro/)
* [kucoin](https://sandbox.kucoin.com/)
* [phemex](https://testnet.phemex.com/)
!!! Note
We did not test correct functioning of all of the above testnets. Please report your experiences with each sandbox.
--- ---
# Configure a Sandbox account on Gdax ## Configure a Sandbox account
Aim of this document section When testing your API connectivity, make sure to use the appropriate sandbox / testnet URL.
- An sanbox account In general, you should follow these steps to enable an exchange's sandbox:
- create 2FA (needed to create an API)
- Add test 50BTC to account
- Create :
- - API-KEY
- - API-Secret
- - API Password
## Acccount * Figure out if an exchange has a sandbox (most likely by using google or the exchange's support documents)
* Create a sandbox account (often the sandbox-account requires separate registration)
* [Add some test assets to account](#add-test-funds)
* Create API keys
This link will redirect to the sandbox main page to login / create account dialogues: ### Add test funds
https://public.sandbox.pro.coinbase.com/orders/
After registration and Email confimation you wil be redirected into your sanbox account. It is easy to verify you're in sandbox by checking the URL bar. Usually, sandbox exchanges allow depositing funds directly via web-interface.
> https://public.sandbox.pro.coinbase.com/ You should make sure to have a realistic amount of funds available to your test-account, so results are representable of your real account funds.
## Enable 2Fa (a prerequisite to creating sandbox API Keys) !!! Warning
Test exchanges will **NEVER** require your real credit card or banking details!
From within sand box site select your profile, top right. ## Configure freqtrade to use a exchange's sandbox
>Or as a direct link: https://public.sandbox.pro.coinbase.com/profile
From the menu panel to the left of the screen select ### Sandbox URLs
> Security: "*View or Update*"
In the new site select "enable authenticator" as typical google Authenticator.
- open Google Authenticator on your phone
- scan barcode
- enter your generated 2fa
## Enable API Access
From within sandbox select profile>api>create api-keys
>or as a direct link: https://public.sandbox.pro.coinbase.com/profile/api
Click on "create one" and ensure **view** and **trade** are "checked" and sumbit your 2FA
- **Copy and paste the Passphase** into a notepade this will be needed later
- **Copy and paste the API Secret** popup into a notepad this will needed later
- **Copy and paste the API Key** into a notepad this will needed later
## Add 50 BTC test funds
To add funds, use the web interface deposit and withdraw buttons.
To begin select 'Wallets' from the top menu.
> Or as a direct link: https://public.sandbox.pro.coinbase.com/wallets
- Deposits (bottom left of screen)
- - Deposit Funds Bitcoin
- - - Coinbase BTC Wallet
- - - - Max (50 BTC)
- - - - - Deposit
*This process may be repeated for other currencies, ETH as example*
---
# Configure Freqtrade to use Gax Sandbox
The aim of this document section
- Enable sandbox URLs in Freqtrade
- Configure API
- - secret
- - key
- - passphrase
## Sandbox URLs
Freqtrade makes use of CCXT which in turn provides a list of URLs to Freqtrade. Freqtrade makes use of CCXT which in turn provides a list of URLs to Freqtrade.
These include `['test']` and `['api']`. These include `['test']` and `['api']`.
- `[Test]` if available will point to an Exchanges sandbox. * `[Test]` if available will point to an Exchanges sandbox.
- `[Api]` normally used, and resolves to live API target on the exchange * `[Api]` normally used, and resolves to live API target on the exchange.
To make use of sandbox / test add "sandbox": true, to your config.json To make use of sandbox / test add "sandbox": true, to your config.json
```json ```json
"exchange": { "exchange": {
"name": "gdax", "name": "coinbasepro",
"sandbox": true, "sandbox": true,
"key": "5wowfxemogxeowo;heiohgmd", "key": "5wowfxemogxeowo;heiohgmd",
"secret": "/ZMH1P62rCVmwefewrgcewX8nh4gob+lywxfwfxwwfxwfNsH1ySgvWCUR/w==", "secret": "/ZMH1P62rCVmwefewrgcewX8nh4gob+lywxfwfxwwfxwfNsH1ySgvWCUR/w==",
@@ -106,36 +61,57 @@ To make use of sandbox / test add "sandbox": true, to your config.json
"outdated_offset": 5 "outdated_offset": 5
"pair_whitelist": [ "pair_whitelist": [
"BTC/USD" "BTC/USD"
]
},
"datadir": "user_data/data/coinbasepro_sandbox"
``` ```
Also insert your Also the following information:
- api-key (noted earlier) * api-key (created for the sandbox webpage)
- api-secret (noted earlier) * api-secret (noted earlier)
- password (the passphrase - noted earlier) * password (the passphrase - noted earlier)
!!! Tip "Different data directory"
We also recommend to set `datadir` to something identifying downloaded data as sandbox data, to avoid having sandbox data mixed with data from the real exchange.
This can be done by adding the `"datadir"` key to the configuration.
Now, whenever you use this configuration, your data directory will be set to this directory.
--- ---
## You should now be ready to test your sandbox ## You should now be ready to test your sandbox
Ensure Freqtrade logs show the sandbox URL, and trades made are shown in sandbox. Ensure Freqtrade logs show the sandbox URL, and trades made are shown in sandbox. Also make sure to select a pair which shows at least some decent value (which very often is BTC/<somestablecoin>).
** Typically the BTC/USD has the most activity in sandbox to test against.
## GDAX - Old Candles problem ## Common problems with sandbox exchanges
It is my experience that GDAX sandbox candles may be 20+- minutes out of date. This can cause trades to fail as one of Freqtrades safety checks. Sandbox exchange instances often have very low volume, which can cause some problems which usually are not seen on a real exchange instance.
To disable this check, add / change the `"outdated_offset"` parameter in the exchange section of your configuration to adjust for this delay. ### Old Candles problem
Example based on the above configuration:
```json Since Sandboxes often have low volume, candles can be quite old and show no volume.
"exchange": { To disable the error "Outdated history for pair ...", best increase the parameter `"outdated_offset"` to a number that seems realistic for the sandbox you're using.
"name": "gdax",
"sandbox": true, ### Unfilled orders
"key": "5wowfxemogxeowo;heiohgmd",
"secret": "/ZMH1P62rCVmwefewrgcewX8nh4gob+lywxfwfxwwfxwfNsH1ySgvWCUR/w==", Sandboxes often have very low volumes - which means that many trades can go unfilled, or can go unfilled for a very long time.
"password": "1bkjfkhfhfu6sr",
"outdated_offset": 30 To mitigate this, you can try to match the first order on the opposite orderbook side using the following configuration:
"pair_whitelist": [
"BTC/USD" ``` jsonc
``` "order_types": {
"buy": "limit",
"sell": "limit"
// ...
},
"bid_strategy": {
"price_side": "ask",
// ...
},
"ask_strategy":{
"price_side": "bid",
// ...
},
```
The configuration is similar to the suggested configuration for market orders - however by using limit-orders you can avoid moving the price too much, and you can set the worst price you might get.

View File

@@ -13,6 +13,15 @@ Feel free to use a visual Database editor like SqliteBrowser if you feel more co
sudo apt-get install sqlite3 sudo apt-get install sqlite3
``` ```
### Using sqlite3 via docker-compose
The freqtrade docker image does contain sqlite3, so you can edit the database without having to install anything on the host system.
``` bash
docker-compose exec freqtrade /bin/bash
sqlite3 <databasefile>.sqlite
```
## Open the DB ## Open the DB
```bash ```bash
@@ -100,8 +109,8 @@ UPDATE trades
SET is_open=0, SET is_open=0,
close_date=<close_date>, close_date=<close_date>,
close_rate=<close_rate>, close_rate=<close_rate>,
close_profit=close_rate/open_rate-1, close_profit = close_rate / open_rate - 1,
close_profit_abs = (amount * <close_rate> * (1 - fee_close) - (amount * open_rate * 1 - fee_open)), close_profit_abs = (amount * <close_rate> * (1 - fee_close) - (amount * (open_rate * (1 - fee_open)))),
sell_reason=<sell_reason> sell_reason=<sell_reason>
WHERE id=<trade_ID_to_update>; WHERE id=<trade_ID_to_update>;
``` ```
@@ -111,24 +120,39 @@ WHERE id=<trade_ID_to_update>;
```sql ```sql
UPDATE trades UPDATE trades
SET is_open=0, SET is_open=0,
close_date='2017-12-20 03:08:45.103418', close_date='2020-06-20 03:08:45.103418',
close_rate=0.19638016, close_rate=0.19638016,
close_profit=0.0496, close_profit=0.0496,
close_profit_abs = (amount * 0.19638016 * (1 - fee_close) - (amount * open_rate * 1 - fee_open)) close_profit_abs = (amount * 0.19638016 * (1 - fee_close) - (amount * (open_rate * (1 - fee_open)))),
sell_reason='force_sell' sell_reason='force_sell'
WHERE id=31; WHERE id=31;
``` ```
## Insert manually a new trade ## Manually insert a new trade
```sql ```sql
INSERT INTO trades (exchange, pair, is_open, fee_open, fee_close, open_rate, stake_amount, amount, open_date) INSERT INTO trades (exchange, pair, is_open, fee_open, fee_close, open_rate, stake_amount, amount, open_date)
VALUES ('bittrex', 'ETH/BTC', 1, 0.0025, 0.0025, <open_rate>, <stake_amount>, <amount>, '<datetime>') VALUES ('binance', 'ETH/BTC', 1, 0.0025, 0.0025, <open_rate>, <stake_amount>, <amount>, '<datetime>')
``` ```
##### Example: ### Insert trade example
```sql ```sql
INSERT INTO trades (exchange, pair, is_open, fee_open, fee_close, open_rate, stake_amount, amount, open_date) INSERT INTO trades (exchange, pair, is_open, fee_open, fee_close, open_rate, stake_amount, amount, open_date)
VALUES ('bittrex', 'ETH/BTC', 1, 0.0025, 0.0025, 0.00258580, 0.002, 0.7715262081, '2017-11-28 12:44:24.000000') VALUES ('binance', 'ETH/BTC', 1, 0.0025, 0.0025, 0.00258580, 0.002, 0.7715262081, '2020-06-28 12:44:24.000000')
``` ```
## Remove trade from the database
Maybe you'd like to remove a trade from the database, because something went wrong.
```sql
DELETE FROM trades WHERE id = <tradeid>;
```
```sql
DELETE FROM trades WHERE id = 31;
```
!!! Warning
This will remove this trade from the database. Please make sure you got the correct id and **NEVER** run this query without the `where` clause.

View File

@@ -6,7 +6,63 @@ For example, value `-0.10` will cause immediate sell if the profit dips below -1
Most of the strategy files already include the optimal `stoploss` value. Most of the strategy files already include the optimal `stoploss` value.
!!! Info !!! Info
All stoploss properties mentioned in this file can be set in the Strategy, or in the configuration. Configuration values will override the strategy values. All stoploss properties mentioned in this file can be set in the Strategy, or in the configuration.
<ins>Configuration values will override the strategy values.</ins>
## Stop Loss On-Exchange/Freqtrade
Those stoploss modes can be *on exchange* or *off exchange*.
These modes can be configured with these values:
``` python
'emergencysell': 'market',
'stoploss_on_exchange': False
'stoploss_on_exchange_interval': 60,
'stoploss_on_exchange_limit_ratio': 0.99
```
!!! Note
Stoploss on exchange is only supported for Binance (stop-loss-limit), Kraken (stop-loss-market) and FTX (stop limit and stop-market) as of now.
<ins>Do not set too low stoploss value if using stop loss on exchange!</ins>
If set to low/tight then you have greater risk of missing fill on the order and stoploss will not work
### stoploss_on_exchange and stoploss_on_exchange_limit_ratio
Enable or Disable stop loss on exchange.
If the stoploss is *on exchange* it means a stoploss limit order is placed on the exchange immediately after buy order happens successfully. This will protect you against sudden crashes in market as the order will be in the queue immediately and if market goes down then the order has more chance of being fulfilled.
If `stoploss_on_exchange` uses limit orders, the exchange needs 2 prices, the stoploss_price and the Limit price.
`stoploss` defines the stop-price where the limit order is placed - and limit should be slightly below this.
If an exchange supports both limit and market stoploss orders, then the value of `stoploss` will be used to determine the stoploss type.
Calculation example: we bought the asset at 100$.
Stop-price is 95$, then limit would be `95 * 0.99 = 94.05$` - so the limit order fill can happen between 95$ and 94.05$.
For example, assuming the stoploss is on exchange, and trailing stoploss is enabled, and the market is going up, then the bot automatically cancels the previous stoploss order and puts a new one with a stop value higher than the previous stoploss order.
### stoploss_on_exchange_interval
In case of stoploss on exchange there is another parameter called `stoploss_on_exchange_interval`. This configures the interval in seconds at which the bot will check the stoploss and update it if necessary.
The bot cannot do these every 5 seconds (at each iteration), otherwise it would get banned by the exchange.
So this parameter will tell the bot how often it should update the stoploss order. The default value is 60 (1 minute).
This same logic will reapply a stoploss order on the exchange should you cancel it accidentally.
### emergencysell
`emergencysell` is an optional value, which defaults to `market` and is used when creating stop loss on exchange orders fails.
The below is the default which is used if not changed in strategy or configuration file.
Example from strategy file:
``` python
order_types = {
'buy': 'limit',
'sell': 'limit',
'emergencysell': 'market',
'stoploss': 'market',
'stoploss_on_exchange': True,
'stoploss_on_exchange_interval': 60,
'stoploss_on_exchange_limit_ratio': 0.99
}
```
## Stop Loss Types ## Stop Loss Types
@@ -17,29 +73,29 @@ At this stage the bot contains the following stoploss support modes:
3. Trailing stop loss, custom positive loss. 3. Trailing stop loss, custom positive loss.
4. Trailing stop loss only once the trade has reached a certain offset. 4. Trailing stop loss only once the trade has reached a certain offset.
Those stoploss modes can be *on exchange* or *off exchange*. If the stoploss is *on exchange* it means a stoploss limit order is placed on the exchange immediately after buy order happens successfully. This will protect you against sudden crashes in market as the order will be in the queue immediately and if market goes down then the order has more chance of being fulfilled. ### Static Stop Loss
In case of stoploss on exchange there is another parameter called `stoploss_on_exchange_interval`. This configures the interval in seconds at which the bot will check the stoploss and update it if necessary.
For example, assuming the stoploss is on exchange, and trailing stoploss is enabled, and the market is going up, then the bot automatically cancels the previous stoploss order and puts a new one with a stop value higher than the previous stoploss order.
The bot cannot do this every 5 seconds (at each iteration), otherwise it would get banned by the exchange.
So this parameter will tell the bot how often it should update the stoploss order. The default value is 60 (1 minute).
This same logic will reapply a stoploss order on the exchange should you cancel it accidentally.
!!! Note
Stoploss on exchange is only supported for Binance (stop-loss-limit), Kraken (stop-loss-market) and FTX (stop limit and stop-market) as of now.
## Static Stop Loss
This is very simple, you define a stop loss of x (as a ratio of price, i.e. x * 100% of price). This will try to sell the asset once the loss exceeds the defined loss. This is very simple, you define a stop loss of x (as a ratio of price, i.e. x * 100% of price). This will try to sell the asset once the loss exceeds the defined loss.
## Trailing Stop Loss Example of stop loss:
``` python
stoploss = -0.10
```
For example, simplified math:
* the bot buys an asset at a price of 100$
* the stop loss is defined at -10%
* the stop loss would get triggered once the asset drops below 90$
### Trailing Stop Loss
The initial value for this is `stoploss`, just as you would define your static Stop loss. The initial value for this is `stoploss`, just as you would define your static Stop loss.
To enable trailing stoploss: To enable trailing stoploss:
``` python ``` python
trailing_stop = True stoploss = -0.10
trailing_stop = True
``` ```
This will now activate an algorithm, which automatically moves the stop loss up every time the price of your asset increases. This will now activate an algorithm, which automatically moves the stop loss up every time the price of your asset increases.
@@ -47,35 +103,43 @@ This will now activate an algorithm, which automatically moves the stop loss up
For example, simplified math: For example, simplified math:
* the bot buys an asset at a price of 100$ * the bot buys an asset at a price of 100$
* the stop loss is defined at 2% * the stop loss is defined at -10%
* the stop loss would get triggered once the asset dropps below 98$ * the stop loss would get triggered once the asset drops below 90$
* assuming the asset now increases to 102$ * assuming the asset now increases to 102$
* the stop loss will now be 2% of 102$ or 99.96$ * the stop loss will now be -10% of 102$ = 91.8$
* now the asset drops in value to 101$, the stop loss will still be 99.96$ and would trigger at 99.96$. * now the asset drops in value to 101$, the stop loss will still be 91.8$ and would trigger at 91.8$.
In summary: The stoploss will be adjusted to be always be 2% of the highest observed price. In summary: The stoploss will be adjusted to be always be -10% of the highest observed price.
### Custom positive stoploss ### Trailing stop loss, custom positive loss
It is also possible to have a default stop loss, when you are in the red with your buy, but once your profit surpasses a certain percentage, the system will utilize a new stop loss, which can have a different value. It is also possible to have a default stop loss, when you are in the red with your buy (buy - fee), but once you hit positive result the system will utilize a new stop loss, which can have a different value.
For example your default stop loss is 5%, but once you have 1.1% profit, it will be changed to be only a 1% stop loss, which trails the green candles until it goes below them. For example, your default stop loss is -10%, but once you have more than 0% profit (example 0.1%) a different trailing stoploss will be used.
Both values require `trailing_stop` to be set to true. !!! Note
If you want the stoploss to only be changed when you break even of making a profit (what most users want) please refer to next section with [offset enabled](#Trailing-stop-loss-only-once-the-trade-has-reached-a-certain-offset).
Both values require `trailing_stop` to be set to true and `trailing_stop_positive` with a value.
``` python ``` python
trailing_stop_positive = 0.01 stoploss = -0.10
trailing_stop_positive_offset = 0.011 trailing_stop = True
trailing_stop_positive = 0.02
``` ```
The 0.01 would translate to a 1% stop loss, once you hit 1.1% profit. For example, simplified math:
* the bot buys an asset at a price of 100$
* the stop loss is defined at -10%
* the stop loss would get triggered once the asset drops below 90$
* assuming the asset now increases to 102$
* the stop loss will now be -2% of 102$ = 99.96$ (99.96$ stop loss will be locked in and will follow asset price increasements with -2%)
* now the asset drops in value to 101$, the stop loss will still be 99.96$ and would trigger at 99.96$
The 0.02 would translate to a -2% stop loss.
Before this, `stoploss` is used for the trailing stoploss. Before this, `stoploss` is used for the trailing stoploss.
Read the [next section](#trailing-only-once-offset-is-reached) to keep stoploss at 5% of the entry point. ### Trailing stop loss only once the trade has reached a certain offset
!!! Tip
Make sure to have this value (`trailing_stop_positive_offset`) lower than minimal ROI, otherwise minimal ROI will apply first and sell the trade.
### Trailing only once offset is reached
It is also possible to use a static stoploss until the offset is reached, and then trail the trade to take profits once the market turns. It is also possible to use a static stoploss until the offset is reached, and then trail the trade to take profits once the market turns.
@@ -84,20 +148,31 @@ This option can be used with or without `trailing_stop_positive`, but uses `trai
``` python ``` python
trailing_stop_positive_offset = 0.011 trailing_stop_positive_offset = 0.011
trailing_only_offset_is_reached = true trailing_only_offset_is_reached = True
``` ```
Simplified example: Configuration (offset is buyprice + 3%):
``` python ``` python
stoploss = 0.05 stoploss = -0.10
trailing_stop = True
trailing_stop_positive = 0.02
trailing_stop_positive_offset = 0.03 trailing_stop_positive_offset = 0.03
trailing_only_offset_is_reached = True trailing_only_offset_is_reached = True
``` ```
For example, simplified math:
* the bot buys an asset at a price of 100$ * the bot buys an asset at a price of 100$
* the stop loss is defined at 5% * the stop loss is defined at -10%
* the stop loss will remain at 95% until profit reaches +3% * the stop loss would get triggered once the asset drops below 90$
* stoploss will remain at 90$ unless asset increases to or above our configured offset
* assuming the asset now increases to 103$ (where we have the offset configured)
* the stop loss will now be -2% of 103$ = 100.94$
* now the asset drops in value to 101$, the stop loss will still be 100.94$ and would trigger at 100.94$
!!! Tip
Make sure to have this value (`trailing_stop_positive_offset`) lower than minimal ROI, otherwise minimal ROI will apply first and sell the trade.
## Changing stoploss on open trades ## Changing stoploss on open trades

View File

@@ -1,7 +1,12 @@
# Advanced Strategies # Advanced Strategies
This page explains some advanced concepts available for strategies. This page explains some advanced concepts available for strategies.
If you're just getting started, please be familiar with the methods described in the [Strategy Customization](strategy-customization.md) documentation first. If you're just getting started, please be familiar with the methods described in the [Strategy Customization](strategy-customization.md) documentation and with the [Freqtrade basics](bot-basics.md) first.
[Freqtrade basics](bot-basics.md) describes in which sequence each method described below is called, which can be helpful to understand which method to use for your custom needs.
!!! Note
All callback methods described below should only be implemented in a strategy if they are actually used.
## Custom order timeout rules ## Custom order timeout rules
@@ -89,3 +94,129 @@ class Awesomestrategy(IStrategy):
return True return True
return False return False
``` ```
## Bot loop start callback
A simple callback which is called once at the start of every bot throttling iteration.
This can be used to perform calculations which are pair independent (apply to all pairs), loading of external data, etc.
``` python
import requests
class Awesomestrategy(IStrategy):
# ... populate_* methods
def bot_loop_start(self, **kwargs) -> None:
"""
Called at the start of the bot iteration (one loop).
Might be used to perform pair-independent tasks
(e.g. gather some remote resource for comparison)
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
"""
if self.config['runmode'].value in ('live', 'dry_run'):
# Assign this to the class by using self.*
# can then be used by populate_* methods
self.remote_data = requests.get('https://some_remote_source.example.com')
```
## Bot order confirmation
### Trade entry (buy order) confirmation
`confirm_trade_entry()` can be used to abort a trade entry at the latest second (maybe because the price is not what we expect).
``` python
class Awesomestrategy(IStrategy):
# ... populate_* methods
def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float,
time_in_force: str, **kwargs) -> bool:
"""
Called right before placing a buy order.
Timing for this function is critical, so avoid doing heavy computations or
network requests in this method.
For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/
When not implemented by a strategy, returns True (always confirming).
:param pair: Pair that's about to be bought.
:param order_type: Order type (as configured in order_types). usually limit or market.
:param amount: Amount in target (quote) currency that's going to be traded.
:param rate: Rate that's going to be used when using limit orders
:param time_in_force: Time in force. Defaults to GTC (Good-til-cancelled).
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
:return bool: When True is returned, then the buy-order is placed on the exchange.
False aborts the process
"""
return True
```
### Trade exit (sell order) confirmation
`confirm_trade_exit()` can be used to abort a trade exit (sell) at the latest second (maybe because the price is not what we expect).
``` python
from freqtrade.persistence import Trade
class Awesomestrategy(IStrategy):
# ... populate_* methods
def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float,
rate: float, time_in_force: str, sell_reason: str, **kwargs) -> bool:
"""
Called right before placing a regular sell order.
Timing for this function is critical, so avoid doing heavy computations or
network requests in this method.
For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/
When not implemented by a strategy, returns True (always confirming).
:param pair: Pair that's about to be sold.
:param order_type: Order type (as configured in order_types). usually limit or market.
:param amount: Amount in quote currency.
:param rate: Rate that's going to be used when using limit orders
:param time_in_force: Time in force. Defaults to GTC (Good-til-cancelled).
:param sell_reason: Sell reason.
Can be any of ['roi', 'stop_loss', 'stoploss_on_exchange', 'trailing_stop_loss',
'sell_signal', 'force_sell', 'emergency_sell']
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
:return bool: When True is returned, then the sell-order is placed on the exchange.
False aborts the process
"""
if sell_reason == 'force_sell' and trade.calc_profit_ratio(rate) < 0:
# Reject force-sells with negative profit
# This is just a sample, please adjust to your needs
# (this does not necessarily make sense, assuming you know when you're force-selling)
return False
return True
```
## Derived strategies
The strategies can be derived from other strategies. This avoids duplication of your custom strategy code. You can use this technique to override small parts of your main strategy, leaving the rest untouched:
``` python
class MyAwesomeStrategy(IStrategy):
...
stoploss = 0.13
trailing_stop = False
# All other attributes and methods are here as they
# should be in any custom strategy...
...
class MyAwesomeStrategy2(MyAwesomeStrategy):
# Override something
stoploss = 0.08
trailing_stop = True
```
Both attributes and methods may be overriden, altering behavior of the original strategy in a way you need.

View File

@@ -1,6 +1,8 @@
# Strategy Customization # Strategy Customization
This page explains where to customize your strategies, and add new indicators. This page explains how to customize your strategies, add new indicators and set up trading rules.
Please familiarize yourself with [Freqtrade basics](bot-basics.md) first, which provides overall info on how the bot operates.
## Install a custom strategy file ## Install a custom strategy file
@@ -56,12 +58,12 @@ file as reference.**
!!! Note "Strategies and Backtesting" !!! Note "Strategies and Backtesting"
To avoid problems and unexpected differences between Backtesting and dry/live modes, please be aware To avoid problems and unexpected differences between Backtesting and dry/live modes, please be aware
that during backtesting the full time-interval is passed to the `populate_*()` methods at once. that during backtesting the full time range is passed to the `populate_*()` methods at once.
It is therefore best to use vectorized operations (across the whole dataframe, not loops) and It is therefore best to use vectorized operations (across the whole dataframe, not loops) and
avoid index referencing (`df.iloc[-1]`), but instead use `df.shift()` to get to the previous candle. avoid index referencing (`df.iloc[-1]`), but instead use `df.shift()` to get to the previous candle.
!!! Warning "Warning: Using future data" !!! Warning "Warning: Using future data"
Since backtesting passes the full time interval to the `populate_*()` methods, the strategy author Since backtesting passes the full time range to the `populate_*()` methods, the strategy author
needs to take care to avoid having the strategy utilize data from the future. needs to take care to avoid having the strategy utilize data from the future.
Some common patterns for this are listed in the [Common Mistakes](#common-mistakes-when-developing-strategies) section of this document. Some common patterns for this are listed in the [Common Mistakes](#common-mistakes-when-developing-strategies) section of this document.
@@ -249,7 +251,7 @@ minimal_roi = {
While technically not completely disabled, this would sell once the trade reaches 10000% Profit. While technically not completely disabled, this would sell once the trade reaches 10000% Profit.
To use times based on candle duration (timeframe), the following snippet can be handy. To use times based on candle duration (timeframe), the following snippet can be handy.
This will allow you to change the ticket_interval for the strategy, and ROI times will still be set as candles (e.g. after 3 candles ...) This will allow you to change the timeframe for the strategy, and ROI times will still be set as candles (e.g. after 3 candles ...)
``` python ``` python
from freqtrade.exchange import timeframe_to_minutes from freqtrade.exchange import timeframe_to_minutes
@@ -283,7 +285,7 @@ If your exchange supports it, it's recommended to also set `"stoploss_on_exchang
For more information on order_types please look [here](configuration.md#understand-order_types). For more information on order_types please look [here](configuration.md#understand-order_types).
### Timeframe (ticker interval) ### Timeframe (formerly ticker interval)
This is the set of candles the bot should download and use for the analysis. This is the set of candles the bot should download and use for the analysis.
Common values are `"1m"`, `"5m"`, `"15m"`, `"1h"`, however all values supported by your exchange should work. Common values are `"1m"`, `"5m"`, `"15m"`, `"1h"`, however all values supported by your exchange should work.
@@ -326,15 +328,15 @@ class Awesomestrategy(IStrategy):
*** ***
### Additional data (informative_pairs) ## Additional data (informative_pairs)
#### Get data for non-tradeable pairs ### Get data for non-tradeable pairs
Data for additional, informative pairs (reference pairs) can be beneficial for some strategies. Data for additional, informative pairs (reference pairs) can be beneficial for some strategies.
Ohlcv data for these pairs will be downloaded as part of the regular whitelist refresh process and is available via `DataProvider` just as other pairs (see below). OHLCV data for these pairs will be downloaded as part of the regular whitelist refresh process and is available via `DataProvider` just as other pairs (see below).
These parts will **not** be traded unless they are also specified in the pair whitelist, or have been selected by Dynamic Whitelisting. These parts will **not** be traded unless they are also specified in the pair whitelist, or have been selected by Dynamic Whitelisting.
The pairs need to be specified as tuples in the format `("pair", "interval")`, with pair as the first and time interval as the second argument. The pairs need to be specified as tuples in the format `("pair", "timeframe")`, with pair as the first and timeframe as the second argument.
Sample: Sample:
@@ -345,15 +347,17 @@ def informative_pairs(self):
] ]
``` ```
A full sample can be found [in the DataProvider section](#complete-data-provider-sample).
!!! Warning !!! Warning
As these pairs will be refreshed as part of the regular whitelist refresh, it's best to keep this list short. As these pairs will be refreshed as part of the regular whitelist refresh, it's best to keep this list short.
All intervals and all pairs can be specified as long as they are available (and active) on the used exchange. All timeframes and all pairs can be specified as long as they are available (and active) on the used exchange.
It is however better to use resampling to longer time-intervals when possible It is however better to use resampling to longer timeframes whenever possible
to avoid hammering the exchange with too many requests and risk being blocked. to avoid hammering the exchange with too many requests and risk being blocked.
*** ***
### Additional data (DataProvider) ## Additional data (DataProvider)
The strategy provides access to the `DataProvider`. This allows you to get additional data to use in your strategy. The strategy provides access to the `DataProvider`. This allows you to get additional data to use in your strategy.
@@ -361,11 +365,16 @@ All methods return `None` in case of failure (do not raise an exception).
Please always check the mode of operation to select the correct method to get data (samples see below). Please always check the mode of operation to select the correct method to get data (samples see below).
#### Possible options for DataProvider !!! Warning "Hyperopt"
Dataprovider is available during hyperopt, however it can only be used in `populate_indicators()` within a strategy.
It is not available in `populate_buy()` and `populate_sell()` methods, nor in `populate_indicators()`, if this method located in the hyperopt file.
- [`available_pairs`](#available_pairs) - Property with tuples listing cached pairs with their intervals (pair, interval). ### Possible options for DataProvider
- [`current_whitelist()`](#current_whitelist) - Returns a current list of whitelisted pairs. Useful for accessing dynamic whitelists (ie. VolumePairlist)
- [`available_pairs`](#available_pairs) - Property with tuples listing cached pairs with their timeframe (pair, timeframe).
- [`current_whitelist()`](#current_whitelist) - Returns a current list of whitelisted pairs. Useful for accessing dynamic whitelists (i.e. VolumePairlist)
- [`get_pair_dataframe(pair, timeframe)`](#get_pair_dataframepair-timeframe) - This is a universal method, which returns either historical data (for backtesting) or cached live data (for the Dry-Run and Live-Run modes). - [`get_pair_dataframe(pair, timeframe)`](#get_pair_dataframepair-timeframe) - This is a universal method, which returns either historical data (for backtesting) or cached live data (for the Dry-Run and Live-Run modes).
- [`get_analyzed_dataframe(pair, timeframe)`](#get_analyzed_dataframepair-timeframe) - Returns the analyzed dataframe (after calling `populate_indicators()`, `populate_buy()`, `populate_sell()`) and the time of the latest analysis.
- `historic_ohlcv(pair, timeframe)` - Returns historical data stored on disk. - `historic_ohlcv(pair, timeframe)` - Returns historical data stored on disk.
- `market(pair)` - Returns market data for the pair: fees, limits, precisions, activity flag, etc. See [ccxt documentation](https://github.com/ccxt/ccxt/wiki/Manual#markets) for more details on the Market data structure. - `market(pair)` - Returns market data for the pair: fees, limits, precisions, activity flag, etc. See [ccxt documentation](https://github.com/ccxt/ccxt/wiki/Manual#markets) for more details on the Market data structure.
- `ohlcv(pair, timeframe)` - Currently cached candle (OHLCV) data for the pair, returns DataFrame or empty DataFrame. - `ohlcv(pair, timeframe)` - Currently cached candle (OHLCV) data for the pair, returns DataFrame or empty DataFrame.
@@ -373,9 +382,9 @@ Please always check the mode of operation to select the correct method to get da
- [`ticker(pair)`](#tickerpair) - Returns current ticker data for the pair. See [ccxt documentation](https://github.com/ccxt/ccxt/wiki/Manual#price-tickers) for more details on the Ticker data structure. - [`ticker(pair)`](#tickerpair) - Returns current ticker data for the pair. See [ccxt documentation](https://github.com/ccxt/ccxt/wiki/Manual#price-tickers) for more details on the Ticker data structure.
- `runmode` - Property containing the current runmode. - `runmode` - Property containing the current runmode.
#### Example Usages: ### Example Usages
#### *available_pairs* ### *available_pairs*
``` python ``` python
if self.dp: if self.dp:
@@ -383,44 +392,31 @@ if self.dp:
print(f"available {pair}, {timeframe}") print(f"available {pair}, {timeframe}")
``` ```
#### *current_whitelist()* ### *current_whitelist()*
Imagine you've developed a strategy that trades the `5m` timeframe using signals generated from a `1d` timeframe on the top 10 volume pairs by volume. Imagine you've developed a strategy that trades the `5m` timeframe using signals generated from a `1d` timeframe on the top 10 volume pairs by volume.
The strategy might look something like this: The strategy might look something like this:
*Scan through the top 10 pairs by volume using the `VolumePairList` every 5 minutes and use a 14 day ATR to buy and sell.* *Scan through the top 10 pairs by volume using the `VolumePairList` every 5 minutes and use a 14 day RSI to buy and sell.*
Due to the limited available data, it's very difficult to resample our `5m` candles into daily candles for use in a 14 day ATR. Most exchanges limit us to just 500 candles which effectively gives us around 1.74 daily candles. We need 14 days at least! Due to the limited available data, it's very difficult to resample our `5m` candles into daily candles for use in a 14 day RSI. Most exchanges limit us to just 500 candles which effectively gives us around 1.74 daily candles. We need 14 days at least!
Since we can't resample our data we will have to use an informative pair; and since our whitelist will be dynamic we don't know which pair(s) to use. Since we can't resample our data we will have to use an informative pair; and since our whitelist will be dynamic we don't know which pair(s) to use.
This is where calling `self.dp.current_whitelist()` comes in handy. This is where calling `self.dp.current_whitelist()` comes in handy.
```python ```python
class SampleStrategy(IStrategy):
# strategy init stuff...
timeframe = '5m'
# more strategy init stuff..
def informative_pairs(self): def informative_pairs(self):
# get access to all pairs available in whitelist. # get access to all pairs available in whitelist.
pairs = self.dp.current_whitelist() pairs = self.dp.current_whitelist()
# Assign tf to each pair so they can be downloaded and cached for strategy. # Assign tf to each pair so they can be downloaded and cached for strategy.
informative_pairs = [(pair, '1d') for pair in pairs] informative_pairs = [(pair, '1d') for pair in pairs]
return informative_pairs return informative_pairs
def populate_indicators(self, dataframe, metadata):
# Get the informative pair
informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe='1d')
# Get the 14 day ATR.
atr = ta.ATR(informative, timeperiod=14)
# Do other stuff
``` ```
#### *get_pair_dataframe(pair, timeframe)* ### *get_pair_dataframe(pair, timeframe)*
``` python ``` python
# fetch live / historical candle (OHLCV) data for the first informative pair # fetch live / historical candle (OHLCV) data for the first informative pair
@@ -431,14 +427,27 @@ if self.dp:
``` ```
!!! Warning "Warning about backtesting" !!! Warning "Warning about backtesting"
Be carefull when using dataprovider in backtesting. `historic_ohlcv()` (and `get_pair_dataframe()` 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, 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). so please be aware of it and make sure to not "look into the future" to avoid surprises when running in dry/live mode.
!!! Warning "Warning in hyperopt" ### *get_analyzed_dataframe(pair, timeframe)*
This option cannot currently be used during hyperopt.
#### *orderbook(pair, maximum)* This method is used by freqtrade internally to determine the last signal.
It can also be used in specific callbacks to get the signal that caused the action (see [Advanced Strategy Documentation](strategy-advanced.md) for more details on available callbacks).
``` python
# fetch current dataframe
if self.dp:
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.
This should not happen when using whitelisted pairs.
### *orderbook(pair, maximum)*
``` python ``` python
if self.dp: if self.dp:
@@ -449,10 +458,9 @@ if self.dp:
``` ```
!!! Warning !!! Warning
The order book is not part of the historic data which means backtesting and hyperopt will not work if this The order book is not part of the historic data which means backtesting and hyperopt will not work correctly if this method is used.
method is used.
#### *ticker(pair)* ### *ticker(pair)*
``` python ``` python
if self.dp: if self.dp:
@@ -469,8 +477,77 @@ if self.dp:
does not always fills in the `last` field (so it can be None), etc. So you need to carefully verify the ticker does not always fills in the `last` field (so it can be None), etc. So you need to carefully verify the ticker
data returned from the exchange and add appropriate error handling / defaults. data returned from the exchange and add appropriate error handling / defaults.
!!! Warning "Warning about backtesting"
This method will always return up-to-date values - so usage during backtesting / hyperopt will lead to wrong results.
### Complete Data-provider sample
```python
class SampleStrategy(IStrategy):
# strategy init stuff...
timeframe = '5m'
# more strategy init stuff..
def informative_pairs(self):
# get access to all pairs available in whitelist.
pairs = self.dp.current_whitelist()
# Assign tf to each pair so they can be downloaded and cached for strategy.
informative_pairs = [(pair, '1d') for pair in pairs]
# Optionally Add additional "static" pairs
informative_pairs += [("ETH/USDT", "5m"),
("BTC/TUSD", "15m"),
]
return informative_pairs
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
if not self.dp:
# Don't do anything if DataProvider is not available.
return dataframe
inf_tf = '1d'
# Get the informative pair
informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=inf_tf)
# Get the 14 day rsi
informative['rsi'] = ta.RSI(informative, timeperiod=14)
# Rename columns to be unique
informative.columns = [f"{col}_{inf_tf}" for col in informative.columns]
# Assuming inf_tf = '1d' - then the columns will now be:
# date_1d, open_1d, high_1d, low_1d, close_1d, rsi_1d
# Combine the 2 dataframes
# all indicators on the informative sample MUST be calculated before this point
dataframe = pd.merge(dataframe, informative, left_on='date', right_on=f'date_{inf_tf}', how='left')
# FFill to have the 1d value available in every row throughout the day.
# Without this, comparisons would only work once per day.
dataframe = dataframe.ffill()
# Calculate rsi of the original dataframe (5m timeframe)
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
# Do other stuff
# ...
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[
(
(qtpylib.crossed_above(dataframe['rsi'], 30)) & # Signal: RSI crosses above 30
(dataframe['rsi_1d'] < 30) & # Ensure daily RSI is < 30
(dataframe['volume'] > 0) # Ensure this candle had volume (important for backtesting)
),
'buy'] = 1
```
*** ***
### Additional data (Wallets)
## Additional data (Wallets)
The strategy provides access to the `Wallets` object. This contains the current balances on the exchange. The strategy provides access to the `Wallets` object. This contains the current balances on the exchange.
@@ -486,14 +563,15 @@ if self.wallets:
total_eth = self.wallets.get_total('ETH') total_eth = self.wallets.get_total('ETH')
``` ```
#### Possible options for Wallets ### Possible options for Wallets
- `get_free(asset)` - currently available balance to trade - `get_free(asset)` - currently available balance to trade
- `get_used(asset)` - currently tied up balance (open orders) - `get_used(asset)` - currently tied up balance (open orders)
- `get_total(asset)` - total available balance - sum of the 2 above - `get_total(asset)` - total available balance - sum of the 2 above
*** ***
### Additional data (Trades)
## Additional data (Trades)
A history of Trades can be retrieved in the strategy by querying the database. A history of Trades can be retrieved in the strategy by querying the database.
@@ -539,13 +617,13 @@ Sample return value: ETH/BTC had 5 trades, with a total profit of 1.5% (ratio of
!!! Warning !!! Warning
Trade history is not available during backtesting or hyperopt. Trade history is not available during backtesting or hyperopt.
### Prevent trades from happening for a specific pair ## Prevent trades from happening for a specific pair
Freqtrade locks pairs automatically for the current candle (until that candle is over) when a pair is sold, preventing an immediate re-buy of that pair. Freqtrade locks pairs automatically for the current candle (until that candle is over) when a pair is sold, preventing an immediate re-buy of that pair.
Locked pairs will show the message `Pair <pair> is currently locked.`. Locked pairs will show the message `Pair <pair> is currently locked.`.
#### Locking pairs from within the strategy ### Locking pairs from within the strategy
Sometimes it may be desired to lock a pair after certain events happen (e.g. multiple losing trades in a row). Sometimes it may be desired to lock a pair after certain events happen (e.g. multiple losing trades in a row).
@@ -562,7 +640,7 @@ To verify if a pair is currently locked, use `self.is_pair_locked(pair)`.
!!! Warning !!! Warning
Locking pairs is not functioning during backtesting. Locking pairs is not functioning during backtesting.
##### Pair locking example #### Pair locking example
``` python ``` python
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
@@ -584,7 +662,7 @@ if self.config['runmode'].value in ('live', 'dry_run'):
self.lock_pair(metadata['pair'], until=datetime.now(timezone.utc) + timedelta(hours=12)) self.lock_pair(metadata['pair'], until=datetime.now(timezone.utc) + timedelta(hours=12))
``` ```
### Print created dataframe ## Print created dataframe
To inspect the created dataframe, you can issue a print-statement in either `populate_buy_trend()` or `populate_sell_trend()`. To inspect the created dataframe, you can issue a print-statement in either `populate_buy_trend()` or `populate_sell_trend()`.
You may also want to print the pair so it's clear what data is currently shown. You may also want to print the pair so it's clear what data is currently shown.
@@ -608,36 +686,7 @@ def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
Printing more than a few rows is also possible (simply use `print(dataframe)` instead of `print(dataframe.tail())`), however not recommended, as that will be very verbose (~500 lines per pair every 5 seconds). Printing more than a few rows is also possible (simply use `print(dataframe)` instead of `print(dataframe.tail())`), however not recommended, as that will be very verbose (~500 lines per pair every 5 seconds).
### Specify custom strategy location ## Common mistakes when developing strategies
If you want to use a strategy from a different directory you can pass `--strategy-path`
```bash
freqtrade trade --strategy AwesomeStrategy --strategy-path /some/directory
```
### Derived strategies
The strategies can be derived from other strategies. This avoids duplication of your custom strategy code. You can use this technique to override small parts of your main strategy, leaving the rest untouched:
``` python
class MyAwesomeStrategy(IStrategy):
...
stoploss = 0.13
trailing_stop = False
# All other attributes and methods are here as they
# should be in any custom strategy...
...
class MyAwesomeStrategy2(MyAwesomeStrategy):
# Override something
stoploss = 0.08
trailing_stop = True
```
Both attributes and methods may be overriden, altering behavior of the original strategy in a way you need.
### Common mistakes when developing strategies
Backtesting analyzes the whole time-range at once for performance reasons. Because of this, strategy authors need to make sure that strategies do not look-ahead into the future. Backtesting analyzes the whole time-range at once for performance reasons. Because of this, strategy authors need to make sure that strategies do not look-ahead into the future.
This is a common pain-point, which can cause huge differences between backtesting and dry/live run methods, since they all use data which is not available during dry/live runs, so these strategies will perform well during backtesting, but will fail / perform badly in real conditions. This is a common pain-point, which can cause huge differences between backtesting and dry/live run methods, since they all use data which is not available during dry/live runs, so these strategies will perform well during backtesting, but will fail / perform badly in real conditions.
@@ -649,7 +698,7 @@ The following lists some common patterns which should be avoided to prevent frus
- don't use `dataframe['volume'].mean()`. This uses the full DataFrame for backtesting, including data from the future. Use `dataframe['volume'].rolling(<window>).mean()` instead - don't use `dataframe['volume'].mean()`. This uses the full DataFrame for backtesting, including data from the future. Use `dataframe['volume'].rolling(<window>).mean()` instead
- don't use `.resample('1h')`. This uses the left border of the interval, so moves data from an hour to the start of the hour. Use `.resample('1h', label='right')` instead. - don't use `.resample('1h')`. This uses the left border of the interval, so moves data from an hour to the start of the hour. Use `.resample('1h', label='right')` instead.
### Further strategy ideas ## Further strategy ideas
To get additional Ideas for strategies, head over to our [strategy repository](https://github.com/freqtrade/freqtrade-strategies). Feel free to use them as they are - but results will depend on the current market situation, pairs used etc. - therefore please backtest the strategy for your exchange/desired pairs first, evaluate carefully, use at your own risk. To get additional Ideas for strategies, head over to our [strategy repository](https://github.com/freqtrade/freqtrade-strategies). Feel free to use them as they are - but results will depend on the current market situation, pairs used etc. - therefore please backtest the strategy for your exchange/desired pairs first, evaluate carefully, use at your own risk.
Feel free to use any of them as inspiration for your own strategies. Feel free to use any of them as inspiration for your own strategies.

View File

@@ -85,10 +85,44 @@ Analyze a trades dataframe (also used below for plotting)
```python ```python
from freqtrade.data.btanalysis import load_backtest_data from freqtrade.data.btanalysis import load_backtest_data, load_backtest_stats
# Load backtest results # if backtest_dir points to a directory, it'll automatically load the last backtest file.
trades = load_backtest_data(config["user_data_dir"] / "backtest_results/backtest-result.json") backtest_dir = config["user_data_dir"] / "backtest_results"
# backtest_dir can also point to a specific file
# backtest_dir = config["user_data_dir"] / "backtest_results/backtest-result-2020-07-01_20-04-22.json"
```
```python
# You can get the full backtest statistics by using the following command.
# This contains all information used to generate the backtest result.
stats = load_backtest_stats(backtest_dir)
strategy = 'SampleStrategy'
# All statistics are available per strategy, so if `--strategy-list` was used during backtest, this will be reflected here as well.
# Example usages:
print(stats['strategy'][strategy]['results_per_pair'])
# Get pairlist used for this backtest
print(stats['strategy'][strategy]['pairlist'])
# Get market change (average change of all pairs from start to end of the backtest period)
print(stats['strategy'][strategy]['market_change'])
# Maximum drawdown ()
print(stats['strategy'][strategy]['max_drawdown'])
# Maximum drawdown start and end
print(stats['strategy'][strategy]['drawdown_start'])
print(stats['strategy'][strategy]['drawdown_end'])
# Get strategy comparison (only relevant if multiple strategies were compared)
print(stats['strategy_comparison'])
```
```python
# Load backtested trades as dataframe
trades = load_backtest_data(backtest_dir)
# Show value-counts per pair # Show value-counts per pair
trades.groupby("pair")["sell_reason"].value_counts() trades.groupby("pair")["sell_reason"].value_counts()

View File

@@ -9,7 +9,7 @@ Telegram user id.
Start a chat with the [Telegram BotFather](https://telegram.me/BotFather) Start a chat with the [Telegram BotFather](https://telegram.me/BotFather)
Send the message `/newbot`. Send the message `/newbot`.
*BotFather response:* *BotFather response:*
@@ -47,28 +47,31 @@ Per default, the Telegram bot shows predefined commands. Some commands
are only available by sending them to the bot. The table below list the are only available by sending them to the bot. The table below list the
official commands. You can ask at any moment for help with `/help`. official commands. You can ask at any moment for help with `/help`.
| Command | Default | Description | | Command | Description |
|----------|---------|-------------| |----------|-------------|
| `/start` | | Starts the trader | `/start` | Starts the trader
| `/stop` | | Stops the trader | `/stop` | Stops the trader
| `/stopbuy` | | Stops the trader from opening new trades. Gracefully closes open trades according to their rules. | `/stopbuy` | Stops the trader from opening new trades. Gracefully closes open trades according to their rules.
| `/reload_config` | | Reloads the configuration file | `/reload_config` | Reloads the configuration file
| `/show_config` | | Shows part of the current configuration with relevant settings to operation | `/show_config` | Shows part of the current configuration with relevant settings to operation
| `/status` | | Lists all open trades | `/logs [limit]` | Show last log messages.
| `/status table` | | List all open trades in a table format. Pending buy orders are marked with an asterisk (*) Pending sell orders are marked with a double asterisk (**) | `/status` | Lists all open trades
| `/count` | | Displays number of trades used and available | `/status table` | List all open trades in a table format. Pending buy orders are marked with an asterisk (*) Pending sell orders are marked with a double asterisk (**)
| `/profit` | | Display a summary of your profit/loss from close trades and some stats about your performance | `/trades [limit]` | List all recently closed trades in a table format.
| `/forcesell <trade_id>` | | Instantly sells the given trade (Ignoring `minimum_roi`). | `/delete <trade_id>` | Delete a specific trade from the Database. Tries to close open orders. Requires manual handling of this trade on the exchange.
| `/forcesell all` | | Instantly sells all open trades (Ignoring `minimum_roi`). | `/count` | Displays number of trades used and available
| `/forcebuy <pair> [rate]` | | Instantly buys the given pair. Rate is optional. (`forcebuy_enable` must be set to True) | `/profit` | Display a summary of your profit/loss from close trades and some stats about your performance
| `/performance` | | Show performance of each finished trade grouped by pair | `/forcesell <trade_id>` | Instantly sells the given trade (Ignoring `minimum_roi`).
| `/balance` | | Show account balance per currency | `/forcesell all` | Instantly sells all open trades (Ignoring `minimum_roi`).
| `/daily <n>` | 7 | Shows profit or loss per day, over the last n days | `/forcebuy <pair> [rate]` | Instantly buys the given pair. Rate is optional. (`forcebuy_enable` must be set to True)
| `/whitelist` | | Show the current whitelist | `/performance` | Show performance of each finished trade grouped by pair
| `/blacklist [pair]` | | Show the current blacklist, or adds a pair to the blacklist. | `/balance` | Show account balance per currency
| `/edge` | | Show validated pairs by Edge if it is enabled. | `/daily <n>` | Shows profit or loss per day, over the last n days (n defaults to 7)
| `/help` | | Show help message | `/whitelist` | Show the current whitelist
| `/version` | | Show version | `/blacklist [pair]` | Show the current blacklist, or adds a pair to the blacklist.
| `/edge` | Show validated pairs by Edge if it is enabled.
| `/help` | Show help message
| `/version` | Show version
## Telegram commands in action ## Telegram commands in action
@@ -113,6 +116,7 @@ For each open trade, the bot will send you the following message.
### /status table ### /status table
Return the status of all open trades in a table format. Return the status of all open trades in a table format.
``` ```
ID Pair Since Profit ID Pair Since Profit
---- -------- ------- -------- ---- -------- ------- --------
@@ -123,6 +127,7 @@ Return the status of all open trades in a table format.
### /count ### /count
Return the number of trades used and available. Return the number of trades used and available.
``` ```
current max current max
--------- ----- --------- -----
@@ -208,7 +213,7 @@ Shows the current whitelist
Shows the current blacklist. Shows the current blacklist.
If Pair is set, then this pair will be added to the pairlist. If Pair is set, then this pair will be added to the pairlist.
Also supports multiple pairs, seperated by a space. Also supports multiple pairs, separated by a space.
Use `/reload_config` to reset the blacklist. Use `/reload_config` to reset the blacklist.
> Using blacklist `StaticPairList` with 2 pairs > Using blacklist `StaticPairList` with 2 pairs
@@ -216,7 +221,7 @@ Use `/reload_config` to reset the blacklist.
### /edge ### /edge
Shows pairs validated by Edge along with their corresponding winrate, expectancy and stoploss values. Shows pairs validated by Edge along with their corresponding win-rate, expectancy and stoploss values.
> **Edge only validated following pairs:** > **Edge only validated following pairs:**
``` ```

View File

@@ -432,9 +432,9 @@ usage: freqtrade hyperopt-list [-h] [-v] [--logfile FILE] [-V] [-c PATH]
[--max-trades INT] [--min-avg-time FLOAT] [--max-trades INT] [--min-avg-time FLOAT]
[--max-avg-time FLOAT] [--min-avg-profit FLOAT] [--max-avg-time FLOAT] [--min-avg-profit FLOAT]
[--max-avg-profit FLOAT] [--max-avg-profit FLOAT]
[--min-total-profit FLOAT] [--min-total-profit FLOAT] [--max-total-profit FLOAT]
[--max-total-profit FLOAT] [--no-color] [--min-objective FLOAT] [--max-objective FLOAT]
[--print-json] [--no-details] [--no-color] [--print-json] [--no-details]
[--export-csv FILE] [--export-csv FILE]
optional arguments: optional arguments:
@@ -453,6 +453,10 @@ optional arguments:
Select epochs on above total profit. Select epochs on above total profit.
--max-total-profit FLOAT --max-total-profit FLOAT
Select epochs on below total profit. Select epochs on below total profit.
--min-objective FLOAT
Select epochs on above objective (- is added by default).
--max-objective FLOAT
Select epochs on below objective (- is added by default).
--no-color Disable colorization of hyperopt results. May be --no-color Disable colorization of hyperopt results. May be
useful if you are redirecting output to a file. useful if you are redirecting output to a file.
--print-json Print best result detailization in JSON format. --print-json Print best result detailization in JSON format.

View File

@@ -47,6 +47,7 @@ Different payloads can be configured for different events. Not all fields are ne
The fields in `webhook.webhookbuy` are filled when the bot executes a buy. Parameters are filled using string.format. The fields in `webhook.webhookbuy` are filled when the bot executes a buy. Parameters are filled using string.format.
Possible parameters are: Possible parameters are:
* `trade_id`
* `exchange` * `exchange`
* `pair` * `pair`
* `limit` * `limit`
@@ -63,6 +64,7 @@ Possible parameters are:
The fields in `webhook.webhookbuycancel` are filled when the bot cancels a buy order. Parameters are filled using string.format. The fields in `webhook.webhookbuycancel` are filled when the bot cancels a buy order. Parameters are filled using string.format.
Possible parameters are: Possible parameters are:
* `trade_id`
* `exchange` * `exchange`
* `pair` * `pair`
* `limit` * `limit`
@@ -79,6 +81,7 @@ Possible parameters are:
The fields in `webhook.webhooksell` are filled when the bot sells a trade. Parameters are filled using string.format. The fields in `webhook.webhooksell` are filled when the bot sells a trade. Parameters are filled using string.format.
Possible parameters are: Possible parameters are:
* `trade_id`
* `exchange` * `exchange`
* `pair` * `pair`
* `gain` * `gain`
@@ -100,6 +103,7 @@ Possible parameters are:
The fields in `webhook.webhooksellcancel` are filled when the bot cancels a sell order. Parameters are filled using string.format. The fields in `webhook.webhooksellcancel` are filled when the bot cancels a sell order. Parameters are filled using string.format.
Possible parameters are: Possible parameters are:
* `trade_id`
* `exchange` * `exchange`
* `pair` * `pair`
* `gain` * `gain`

View File

@@ -1,5 +1,5 @@
""" Freqtrade bot """ """ Freqtrade bot """
__version__ = '2020.6' __version__ = '2020.8'
if __version__ == 'develop': if __version__ == 'develop':

View File

@@ -9,7 +9,8 @@ Note: Be careful with file-scoped imports in these subfiles.
from freqtrade.commands.arguments import Arguments from freqtrade.commands.arguments import Arguments
from freqtrade.commands.build_config_commands import start_new_config from freqtrade.commands.build_config_commands import start_new_config
from freqtrade.commands.data_commands import (start_convert_data, from freqtrade.commands.data_commands import (start_convert_data,
start_download_data) start_download_data,
start_list_data)
from freqtrade.commands.deploy_commands import (start_create_userdir, from freqtrade.commands.deploy_commands import (start_create_userdir,
start_new_hyperopt, start_new_hyperopt,
start_new_strategy) start_new_strategy)

View File

@@ -54,6 +54,8 @@ ARGS_BUILD_HYPEROPT = ["user_data_dir", "hyperopt", "template"]
ARGS_CONVERT_DATA = ["pairs", "format_from", "format_to", "erase"] ARGS_CONVERT_DATA = ["pairs", "format_from", "format_to", "erase"]
ARGS_CONVERT_DATA_OHLCV = ARGS_CONVERT_DATA + ["timeframes"] ARGS_CONVERT_DATA_OHLCV = ARGS_CONVERT_DATA + ["timeframes"]
ARGS_LIST_DATA = ["exchange", "dataformat_ohlcv", "pairs"]
ARGS_DOWNLOAD_DATA = ["pairs", "pairs_file", "days", "download_trades", "exchange", ARGS_DOWNLOAD_DATA = ["pairs", "pairs_file", "days", "download_trades", "exchange",
"timeframes", "erase", "dataformat_ohlcv", "dataformat_trades"] "timeframes", "erase", "dataformat_ohlcv", "dataformat_trades"]
@@ -71,6 +73,7 @@ ARGS_HYPEROPT_LIST = ["hyperopt_list_best", "hyperopt_list_profitable",
"hyperopt_list_min_avg_time", "hyperopt_list_max_avg_time", "hyperopt_list_min_avg_time", "hyperopt_list_max_avg_time",
"hyperopt_list_min_avg_profit", "hyperopt_list_max_avg_profit", "hyperopt_list_min_avg_profit", "hyperopt_list_max_avg_profit",
"hyperopt_list_min_total_profit", "hyperopt_list_max_total_profit", "hyperopt_list_min_total_profit", "hyperopt_list_max_total_profit",
"hyperopt_list_min_objective", "hyperopt_list_max_objective",
"print_colorized", "print_json", "hyperopt_list_no_details", "print_colorized", "print_json", "hyperopt_list_no_details",
"export_csv"] "export_csv"]
@@ -78,7 +81,7 @@ ARGS_HYPEROPT_SHOW = ["hyperopt_list_best", "hyperopt_list_profitable", "hyperop
"print_json", "hyperopt_show_no_header"] "print_json", "hyperopt_show_no_header"]
NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes", NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes",
"list-markets", "list-pairs", "list-strategies", "list-markets", "list-pairs", "list-strategies", "list-data",
"list-hyperopts", "hyperopt-list", "hyperopt-show", "list-hyperopts", "hyperopt-list", "hyperopt-show",
"plot-dataframe", "plot-profit", "show-trades"] "plot-dataframe", "plot-profit", "show-trades"]
@@ -159,7 +162,7 @@ class Arguments:
self._build_args(optionlist=['version'], parser=self.parser) self._build_args(optionlist=['version'], parser=self.parser)
from freqtrade.commands import (start_create_userdir, start_convert_data, from freqtrade.commands import (start_create_userdir, start_convert_data,
start_download_data, start_download_data, start_list_data,
start_hyperopt_list, start_hyperopt_show, start_hyperopt_list, start_hyperopt_show,
start_list_exchanges, start_list_hyperopts, start_list_exchanges, start_list_hyperopts,
start_list_markets, start_list_strategies, start_list_markets, start_list_strategies,
@@ -233,6 +236,15 @@ class Arguments:
convert_trade_data_cmd.set_defaults(func=partial(start_convert_data, ohlcv=False)) convert_trade_data_cmd.set_defaults(func=partial(start_convert_data, ohlcv=False))
self._build_args(optionlist=ARGS_CONVERT_DATA, parser=convert_trade_data_cmd) self._build_args(optionlist=ARGS_CONVERT_DATA, parser=convert_trade_data_cmd)
# Add list-data subcommand
list_data_cmd = subparsers.add_parser(
'list-data',
help='List downloaded data.',
parents=[_common_parser],
)
list_data_cmd.set_defaults(func=start_list_data)
self._build_args(optionlist=ARGS_LIST_DATA, parser=list_data_cmd)
# Add backtesting subcommand # Add backtesting subcommand
backtesting_cmd = subparsers.add_parser('backtesting', help='Backtesting module.', backtesting_cmd = subparsers.add_parser('backtesting', help='Backtesting module.',
parents=[_common_parser, _strategy_parser]) parents=[_common_parser, _strategy_parser])
@@ -354,7 +366,7 @@ class Arguments:
plot_profit_cmd = subparsers.add_parser( plot_profit_cmd = subparsers.add_parser(
'plot-profit', 'plot-profit',
help='Generate plot showing profits.', help='Generate plot showing profits.',
parents=[_common_parser], parents=[_common_parser, _strategy_parser],
) )
plot_profit_cmd.set_defaults(func=start_plot_profit) plot_profit_cmd.set_defaults(func=start_plot_profit)
self._build_args(optionlist=ARGS_PLOT_PROFIT, parser=plot_profit_cmd) self._build_args(optionlist=ARGS_PLOT_PROFIT, parser=plot_profit_cmd)

View File

@@ -455,37 +455,49 @@ AVAILABLE_CLI_OPTIONS = {
), ),
"hyperopt_list_min_avg_time": Arg( "hyperopt_list_min_avg_time": Arg(
'--min-avg-time', '--min-avg-time',
help='Select epochs on above average time.', help='Select epochs above average time.',
type=float, type=float,
metavar='FLOAT', metavar='FLOAT',
), ),
"hyperopt_list_max_avg_time": Arg( "hyperopt_list_max_avg_time": Arg(
'--max-avg-time', '--max-avg-time',
help='Select epochs on under average time.', help='Select epochs below average time.',
type=float, type=float,
metavar='FLOAT', metavar='FLOAT',
), ),
"hyperopt_list_min_avg_profit": Arg( "hyperopt_list_min_avg_profit": Arg(
'--min-avg-profit', '--min-avg-profit',
help='Select epochs on above average profit.', help='Select epochs above average profit.',
type=float, type=float,
metavar='FLOAT', metavar='FLOAT',
), ),
"hyperopt_list_max_avg_profit": Arg( "hyperopt_list_max_avg_profit": Arg(
'--max-avg-profit', '--max-avg-profit',
help='Select epochs on below average profit.', help='Select epochs below average profit.',
type=float, type=float,
metavar='FLOAT', metavar='FLOAT',
), ),
"hyperopt_list_min_total_profit": Arg( "hyperopt_list_min_total_profit": Arg(
'--min-total-profit', '--min-total-profit',
help='Select epochs on above total profit.', help='Select epochs above total profit.',
type=float, type=float,
metavar='FLOAT', metavar='FLOAT',
), ),
"hyperopt_list_max_total_profit": Arg( "hyperopt_list_max_total_profit": Arg(
'--max-total-profit', '--max-total-profit',
help='Select epochs on below total profit.', help='Select epochs below total profit.',
type=float,
metavar='FLOAT',
),
"hyperopt_list_min_objective": Arg(
'--min-objective',
help='Select epochs above objective.',
type=float,
metavar='FLOAT',
),
"hyperopt_list_max_objective": Arg(
'--max-objective',
help='Select epochs below objective.',
type=float, type=float,
metavar='FLOAT', metavar='FLOAT',
), ),

View File

@@ -1,5 +1,6 @@
import logging import logging
import sys import sys
from collections import defaultdict
from typing import Any, Dict, List from typing import Any, Dict, List
import arrow import arrow
@@ -11,6 +12,7 @@ from freqtrade.data.history import (convert_trades_to_ohlcv,
refresh_backtest_ohlcv_data, refresh_backtest_ohlcv_data,
refresh_backtest_trades_data) refresh_backtest_trades_data)
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes
from freqtrade.resolvers import ExchangeResolver from freqtrade.resolvers import ExchangeResolver
from freqtrade.state import RunMode from freqtrade.state import RunMode
@@ -33,8 +35,8 @@ def start_download_data(args: Dict[str, Any]) -> None:
"Downloading data requires a list of pairs. " "Downloading data requires a list of pairs. "
"Please check the documentation on how to configure this.") "Please check the documentation on how to configure this.")
logger.info(f'About to download pairs: {config["pairs"]}, ' logger.info(f"About to download pairs: {config['pairs']}, "
f'intervals: {config["timeframes"]} to {config["datadir"]}') f"intervals: {config['timeframes']} to {config['datadir']}")
pairs_not_available: List[str] = [] pairs_not_available: List[str] = []
@@ -49,21 +51,21 @@ def start_download_data(args: Dict[str, Any]) -> None:
if config.get('download_trades'): if config.get('download_trades'):
pairs_not_available = refresh_backtest_trades_data( pairs_not_available = refresh_backtest_trades_data(
exchange, pairs=config["pairs"], datadir=config['datadir'], exchange, pairs=config['pairs'], datadir=config['datadir'],
timerange=timerange, erase=bool(config.get("erase")), timerange=timerange, erase=bool(config.get('erase')),
data_format=config['dataformat_trades']) data_format=config['dataformat_trades'])
# Convert downloaded trade data to different timeframes # Convert downloaded trade data to different timeframes
convert_trades_to_ohlcv( convert_trades_to_ohlcv(
pairs=config["pairs"], timeframes=config["timeframes"], pairs=config['pairs'], timeframes=config['timeframes'],
datadir=config['datadir'], timerange=timerange, erase=bool(config.get("erase")), datadir=config['datadir'], timerange=timerange, erase=bool(config.get('erase')),
data_format_ohlcv=config['dataformat_ohlcv'], data_format_ohlcv=config['dataformat_ohlcv'],
data_format_trades=config['dataformat_trades'], data_format_trades=config['dataformat_trades'],
) )
else: else:
pairs_not_available = refresh_backtest_ohlcv_data( pairs_not_available = refresh_backtest_ohlcv_data(
exchange, pairs=config["pairs"], timeframes=config["timeframes"], exchange, pairs=config['pairs'], timeframes=config['timeframes'],
datadir=config['datadir'], timerange=timerange, erase=bool(config.get("erase")), datadir=config['datadir'], timerange=timerange, erase=bool(config.get('erase')),
data_format=config['dataformat_ohlcv']) data_format=config['dataformat_ohlcv'])
except KeyboardInterrupt: except KeyboardInterrupt:
@@ -88,3 +90,30 @@ def start_convert_data(args: Dict[str, Any], ohlcv: bool = True) -> None:
convert_trades_format(config, convert_trades_format(config,
convert_from=args['format_from'], convert_to=args['format_to'], convert_from=args['format_from'], convert_to=args['format_to'],
erase=args['erase']) erase=args['erase'])
def start_list_data(args: Dict[str, Any]) -> None:
"""
List available backtest data
"""
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
from freqtrade.data.history.idatahandler import get_datahandler
from tabulate import tabulate
dhc = get_datahandler(config['datadir'], config['dataformat_ohlcv'])
paircombs = dhc.ohlcv_get_available_data(config['datadir'])
if args['pairs']:
paircombs = [comb for comb in paircombs if comb[0] in args['pairs']]
print(f"Found {len(paircombs)} pair / timeframe combinations.")
groupedpair = defaultdict(list)
for pair, timeframe in sorted(paircombs, key=lambda x: (x[0], timeframe_to_minutes(x[1]))):
groupedpair[pair].append(timeframe)
if groupedpair:
print(tabulate([(pair, ', '.join(timeframes)) for pair, timeframes in groupedpair.items()],
headers=("Pair", "Timeframe"),
tablefmt='psql', stralign='right'))

View File

@@ -75,7 +75,7 @@ def start_new_strategy(args: Dict[str, Any]) -> None:
if args["strategy"] == "DefaultStrategy": if args["strategy"] == "DefaultStrategy":
raise OperationalException("DefaultStrategy is not allowed as name.") raise OperationalException("DefaultStrategy is not allowed as name.")
new_path = config['user_data_dir'] / USERPATH_STRATEGIES / (args["strategy"] + ".py") new_path = config['user_data_dir'] / USERPATH_STRATEGIES / (args['strategy'] + '.py')
if new_path.exists(): if new_path.exists():
raise OperationalException(f"`{new_path}` already exists. " raise OperationalException(f"`{new_path}` already exists. "
@@ -125,11 +125,11 @@ def start_new_hyperopt(args: Dict[str, Any]) -> None:
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
if "hyperopt" in args and args["hyperopt"]: if 'hyperopt' in args and args['hyperopt']:
if args["hyperopt"] == "DefaultHyperopt": if args['hyperopt'] == 'DefaultHyperopt':
raise OperationalException("DefaultHyperopt is not allowed as name.") raise OperationalException("DefaultHyperopt is not allowed as name.")
new_path = config['user_data_dir'] / USERPATH_HYPEROPTS / (args["hyperopt"] + ".py") new_path = config['user_data_dir'] / USERPATH_HYPEROPTS / (args['hyperopt'] + '.py')
if new_path.exists(): if new_path.exists():
raise OperationalException(f"`{new_path}` already exists. " raise OperationalException(f"`{new_path}` already exists. "

View File

@@ -35,7 +35,9 @@ def start_hyperopt_list(args: Dict[str, Any]) -> None:
'filter_min_avg_profit': config.get('hyperopt_list_min_avg_profit', None), 'filter_min_avg_profit': config.get('hyperopt_list_min_avg_profit', None),
'filter_max_avg_profit': config.get('hyperopt_list_max_avg_profit', None), 'filter_max_avg_profit': config.get('hyperopt_list_max_avg_profit', None),
'filter_min_total_profit': config.get('hyperopt_list_min_total_profit', None), 'filter_min_total_profit': config.get('hyperopt_list_min_total_profit', None),
'filter_max_total_profit': config.get('hyperopt_list_max_total_profit', None) 'filter_max_total_profit': config.get('hyperopt_list_max_total_profit', None),
'filter_min_objective': config.get('hyperopt_list_min_objective', None),
'filter_max_objective': config.get('hyperopt_list_max_objective', None),
} }
results_file = (config['user_data_dir'] / results_file = (config['user_data_dir'] /
@@ -45,7 +47,7 @@ def start_hyperopt_list(args: Dict[str, Any]) -> None:
epochs = Hyperopt.load_previous_results(results_file) epochs = Hyperopt.load_previous_results(results_file)
total_epochs = len(epochs) total_epochs = len(epochs)
epochs = _hyperopt_filter_epochs(epochs, filteroptions) epochs = hyperopt_filter_epochs(epochs, filteroptions)
if print_colorized: if print_colorized:
colorama_init(autoreset=True) colorama_init(autoreset=True)
@@ -92,14 +94,16 @@ def start_hyperopt_show(args: Dict[str, Any]) -> None:
'filter_min_avg_profit': config.get('hyperopt_list_min_avg_profit', None), 'filter_min_avg_profit': config.get('hyperopt_list_min_avg_profit', None),
'filter_max_avg_profit': config.get('hyperopt_list_max_avg_profit', None), 'filter_max_avg_profit': config.get('hyperopt_list_max_avg_profit', None),
'filter_min_total_profit': config.get('hyperopt_list_min_total_profit', None), 'filter_min_total_profit': config.get('hyperopt_list_min_total_profit', None),
'filter_max_total_profit': config.get('hyperopt_list_max_total_profit', None) 'filter_max_total_profit': config.get('hyperopt_list_max_total_profit', None),
'filter_min_objective': config.get('hyperopt_list_min_objective', None),
'filter_max_objective': config.get('hyperopt_list_max_objective', None)
} }
# Previous evaluations # Previous evaluations
epochs = Hyperopt.load_previous_results(results_file) epochs = Hyperopt.load_previous_results(results_file)
total_epochs = len(epochs) total_epochs = len(epochs)
epochs = _hyperopt_filter_epochs(epochs, filteroptions) epochs = hyperopt_filter_epochs(epochs, filteroptions)
filtered_epochs = len(epochs) filtered_epochs = len(epochs)
if n > filtered_epochs: if n > filtered_epochs:
@@ -119,7 +123,7 @@ def start_hyperopt_show(args: Dict[str, Any]) -> None:
header_str="Epoch details") header_str="Epoch details")
def _hyperopt_filter_epochs(epochs: List, filteroptions: dict) -> List: def hyperopt_filter_epochs(epochs: List, filteroptions: dict) -> List:
""" """
Filter our items from the list of hyperopt results Filter our items from the list of hyperopt results
""" """
@@ -127,6 +131,24 @@ def _hyperopt_filter_epochs(epochs: List, filteroptions: dict) -> List:
epochs = [x for x in epochs if x['is_best']] epochs = [x for x in epochs if x['is_best']]
if filteroptions['only_profitable']: if filteroptions['only_profitable']:
epochs = [x for x in epochs if x['results_metrics']['profit'] > 0] epochs = [x for x in epochs if x['results_metrics']['profit'] > 0]
epochs = _hyperopt_filter_epochs_trade_count(epochs, filteroptions)
epochs = _hyperopt_filter_epochs_duration(epochs, filteroptions)
epochs = _hyperopt_filter_epochs_profit(epochs, filteroptions)
epochs = _hyperopt_filter_epochs_objective(epochs, filteroptions)
logger.info(f"{len(epochs)} " +
("best " if filteroptions['only_best'] else "") +
("profitable " if filteroptions['only_profitable'] else "") +
"epochs found.")
return epochs
def _hyperopt_filter_epochs_trade_count(epochs: List, filteroptions: dict) -> List:
if filteroptions['filter_min_trades'] > 0: if filteroptions['filter_min_trades'] > 0:
epochs = [ epochs = [
x for x in epochs x for x in epochs
@@ -137,6 +159,11 @@ def _hyperopt_filter_epochs(epochs: List, filteroptions: dict) -> List:
x for x in epochs x for x in epochs
if x['results_metrics']['trade_count'] < filteroptions['filter_max_trades'] if x['results_metrics']['trade_count'] < filteroptions['filter_max_trades']
] ]
return epochs
def _hyperopt_filter_epochs_duration(epochs: List, filteroptions: dict) -> List:
if filteroptions['filter_min_avg_time'] is not None: if filteroptions['filter_min_avg_time'] is not None:
epochs = [x for x in epochs if x['results_metrics']['trade_count'] > 0] epochs = [x for x in epochs if x['results_metrics']['trade_count'] > 0]
epochs = [ epochs = [
@@ -149,6 +176,12 @@ def _hyperopt_filter_epochs(epochs: List, filteroptions: dict) -> List:
x for x in epochs x for x in epochs
if x['results_metrics']['duration'] < filteroptions['filter_max_avg_time'] if x['results_metrics']['duration'] < filteroptions['filter_max_avg_time']
] ]
return epochs
def _hyperopt_filter_epochs_profit(epochs: List, filteroptions: dict) -> List:
if filteroptions['filter_min_avg_profit'] is not None: if filteroptions['filter_min_avg_profit'] is not None:
epochs = [x for x in epochs if x['results_metrics']['trade_count'] > 0] epochs = [x for x in epochs if x['results_metrics']['trade_count'] > 0]
epochs = [ epochs = [
@@ -173,10 +206,18 @@ def _hyperopt_filter_epochs(epochs: List, filteroptions: dict) -> List:
x for x in epochs x for x in epochs
if x['results_metrics']['profit'] < filteroptions['filter_max_total_profit'] if x['results_metrics']['profit'] < filteroptions['filter_max_total_profit']
] ]
return epochs
logger.info(f"{len(epochs)} " +
("best " if filteroptions['only_best'] else "") + def _hyperopt_filter_epochs_objective(epochs: List, filteroptions: dict) -> List:
("profitable " if filteroptions['only_profitable'] else "") +
"epochs found.") if filteroptions['filter_min_objective'] is not None:
epochs = [x for x in epochs if x['results_metrics']['trade_count'] > 0]
epochs = [x for x in epochs if x['loss'] < filteroptions['filter_min_objective']]
if filteroptions['filter_max_objective'] is not None:
epochs = [x for x in epochs if x['results_metrics']['trade_count'] > 0]
epochs = [x for x in epochs if x['loss'] > filteroptions['filter_max_objective']]
return epochs return epochs

View File

@@ -14,7 +14,7 @@ from freqtrade.configuration import setup_utils_configuration
from freqtrade.constants import USERPATH_HYPEROPTS, USERPATH_STRATEGIES from freqtrade.constants import USERPATH_HYPEROPTS, USERPATH_STRATEGIES
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange import (available_exchanges, ccxt_exchanges, from freqtrade.exchange import (available_exchanges, ccxt_exchanges,
market_is_active, symbol_is_pair) market_is_active)
from freqtrade.misc import plural from freqtrade.misc import plural
from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.resolvers import ExchangeResolver, StrategyResolver
from freqtrade.state import RunMode from freqtrade.state import RunMode
@@ -163,7 +163,7 @@ def start_list_markets(args: Dict[str, Any], pairs_only: bool = False) -> None:
tabular_data.append({'Id': v['id'], 'Symbol': v['symbol'], tabular_data.append({'Id': v['id'], 'Symbol': v['symbol'],
'Base': v['base'], 'Quote': v['quote'], 'Base': v['base'], 'Quote': v['quote'],
'Active': market_is_active(v), 'Active': market_is_active(v),
**({'Is pair': symbol_is_pair(v['symbol'])} **({'Is pair': exchange.market_is_tradable(v)}
if not pairs_only else {})}) if not pairs_only else {})})
if (args.get('print_one_column', False) or if (args.get('print_one_column', False) or

View File

@@ -54,7 +54,7 @@ class Configuration:
:param files: List of file paths :param files: List of file paths
:return: configuration dictionary :return: configuration dictionary
""" """
c = Configuration({"config": files}, RunMode.OTHER) c = Configuration({'config': files}, RunMode.OTHER)
return c.get_config() return c.get_config()
def load_from_files(self, files: List[str]) -> Dict[str, Any]: def load_from_files(self, files: List[str]) -> Dict[str, Any]:
@@ -123,10 +123,10 @@ class Configuration:
the -v/--verbose, --logfile options the -v/--verbose, --logfile options
""" """
# Log level # Log level
config.update({'verbosity': self.args.get("verbosity", 0)}) config.update({'verbosity': self.args.get('verbosity', 0)})
if 'logfile' in self.args and self.args["logfile"]: if 'logfile' in self.args and self.args['logfile']:
config.update({'logfile': self.args["logfile"]}) config.update({'logfile': self.args['logfile']})
setup_logging(config) setup_logging(config)
@@ -149,22 +149,22 @@ class Configuration:
def _process_common_options(self, config: Dict[str, Any]) -> None: def _process_common_options(self, config: Dict[str, Any]) -> None:
# Set strategy if not specified in config and or if it's non default # Set strategy if not specified in config and or if it's non default
if self.args.get("strategy") or not config.get('strategy'): if self.args.get('strategy') or not config.get('strategy'):
config.update({'strategy': self.args.get("strategy")}) config.update({'strategy': self.args.get('strategy')})
self._args_to_config(config, argname='strategy_path', self._args_to_config(config, argname='strategy_path',
logstring='Using additional Strategy lookup path: {}') logstring='Using additional Strategy lookup path: {}')
if ('db_url' in self.args and self.args["db_url"] and if ('db_url' in self.args and self.args['db_url'] and
self.args["db_url"] != constants.DEFAULT_DB_PROD_URL): self.args['db_url'] != constants.DEFAULT_DB_PROD_URL):
config.update({'db_url': self.args["db_url"]}) config.update({'db_url': self.args['db_url']})
logger.info('Parameter --db-url detected ...') logger.info('Parameter --db-url detected ...')
if config.get('forcebuy_enable', False): if config.get('forcebuy_enable', False):
logger.warning('`forcebuy` RPC message enabled.') logger.warning('`forcebuy` RPC message enabled.')
# Support for sd_notify # Support for sd_notify
if 'sd_notify' in self.args and self.args["sd_notify"]: if 'sd_notify' in self.args and self.args['sd_notify']:
config['internals'].update({'sd_notify': True}) config['internals'].update({'sd_notify': True})
def _process_datadir_options(self, config: Dict[str, Any]) -> None: def _process_datadir_options(self, config: Dict[str, Any]) -> None:
@@ -173,24 +173,24 @@ class Configuration:
--user-data, --datadir --user-data, --datadir
""" """
# Check exchange parameter here - otherwise `datadir` might be wrong. # Check exchange parameter here - otherwise `datadir` might be wrong.
if "exchange" in self.args and self.args["exchange"]: if 'exchange' in self.args and self.args['exchange']:
config['exchange']['name'] = self.args["exchange"] config['exchange']['name'] = self.args['exchange']
logger.info(f"Using exchange {config['exchange']['name']}") logger.info(f"Using exchange {config['exchange']['name']}")
if 'pair_whitelist' not in config['exchange']: if 'pair_whitelist' not in config['exchange']:
config['exchange']['pair_whitelist'] = [] config['exchange']['pair_whitelist'] = []
if 'user_data_dir' in self.args and self.args["user_data_dir"]: if 'user_data_dir' in self.args and self.args['user_data_dir']:
config.update({'user_data_dir': self.args["user_data_dir"]}) config.update({'user_data_dir': self.args['user_data_dir']})
elif 'user_data_dir' not in config: elif 'user_data_dir' not in config:
# Default to cwd/user_data (legacy option ...) # Default to cwd/user_data (legacy option ...)
config.update({'user_data_dir': str(Path.cwd() / "user_data")}) config.update({'user_data_dir': str(Path.cwd() / 'user_data')})
# reset to user_data_dir so this contains the absolute path. # reset to user_data_dir so this contains the absolute path.
config['user_data_dir'] = create_userdata_dir(config['user_data_dir'], create_dir=False) config['user_data_dir'] = create_userdata_dir(config['user_data_dir'], create_dir=False)
logger.info('Using user-data directory: %s ...', config['user_data_dir']) logger.info('Using user-data directory: %s ...', config['user_data_dir'])
config.update({'datadir': create_datadir(config, self.args.get("datadir", None))}) config.update({'datadir': create_datadir(config, self.args.get('datadir', None))})
logger.info('Using data directory: %s ...', config.get('datadir')) logger.info('Using data directory: %s ...', config.get('datadir'))
if self.args.get('exportfilename'): if self.args.get('exportfilename'):
@@ -199,7 +199,7 @@ class Configuration:
config['exportfilename'] = Path(config['exportfilename']) config['exportfilename'] = Path(config['exportfilename'])
else: else:
config['exportfilename'] = (config['user_data_dir'] config['exportfilename'] = (config['user_data_dir']
/ 'backtest_results/backtest-result.json') / 'backtest_results')
def _process_optimize_options(self, config: Dict[str, Any]) -> None: def _process_optimize_options(self, config: Dict[str, Any]) -> None:
@@ -219,8 +219,8 @@ class Configuration:
config.update({'use_max_market_positions': False}) config.update({'use_max_market_positions': False})
logger.info('Parameter --disable-max-market-positions detected ...') logger.info('Parameter --disable-max-market-positions detected ...')
logger.info('max_open_trades set to unlimited ...') logger.info('max_open_trades set to unlimited ...')
elif 'max_open_trades' in self.args and self.args["max_open_trades"]: elif 'max_open_trades' in self.args and self.args['max_open_trades']:
config.update({'max_open_trades': self.args["max_open_trades"]}) config.update({'max_open_trades': self.args['max_open_trades']})
logger.info('Parameter --max-open-trades detected, ' logger.info('Parameter --max-open-trades detected, '
'overriding max_open_trades to: %s ...', config.get('max_open_trades')) 'overriding max_open_trades to: %s ...', config.get('max_open_trades'))
elif config['runmode'] in NON_UTIL_MODES: elif config['runmode'] in NON_UTIL_MODES:
@@ -334,6 +334,12 @@ class Configuration:
self._args_to_config(config, argname='hyperopt_list_max_total_profit', self._args_to_config(config, argname='hyperopt_list_max_total_profit',
logstring='Parameter --max-total-profit detected: {}') logstring='Parameter --max-total-profit detected: {}')
self._args_to_config(config, argname='hyperopt_list_min_objective',
logstring='Parameter --min-objective detected: {}')
self._args_to_config(config, argname='hyperopt_list_max_objective',
logstring='Parameter --max-objective detected: {}')
self._args_to_config(config, argname='hyperopt_list_no_details', self._args_to_config(config, argname='hyperopt_list_no_details',
logstring='Parameter --no-details detected: {}') logstring='Parameter --no-details detected: {}')
@@ -441,12 +447,12 @@ class Configuration:
config['pairs'].sort() config['pairs'].sort()
return return
if "config" in self.args and self.args["config"]: if 'config' in self.args and self.args['config']:
logger.info("Using pairlist from configuration.") logger.info("Using pairlist from configuration.")
config['pairs'] = config.get('exchange', {}).get('pair_whitelist') config['pairs'] = config.get('exchange', {}).get('pair_whitelist')
else: else:
# Fall back to /dl_path/pairs.json # Fall back to /dl_path/pairs.json
pairs_file = config['datadir'] / "pairs.json" pairs_file = config['datadir'] / 'pairs.json'
if pairs_file.exists(): if pairs_file.exists():
with pairs_file.open('r') as f: with pairs_file.open('r') as f:
config['pairs'] = json_load(f) config['pairs'] = json_load(f)

View File

@@ -26,12 +26,15 @@ AVAILABLE_PAIRLISTS = ['StaticPairList', 'VolumePairList',
'ShuffleFilter', 'SpreadFilter'] 'ShuffleFilter', 'SpreadFilter']
AVAILABLE_DATAHANDLERS = ['json', 'jsongz'] AVAILABLE_DATAHANDLERS = ['json', 'jsongz']
DRY_RUN_WALLET = 1000 DRY_RUN_WALLET = 1000
DATETIME_PRINT_FORMAT = '%Y-%m-%d %H:%M:%S'
MATH_CLOSE_PREC = 1e-14 # Precision used for float comparisons MATH_CLOSE_PREC = 1e-14 # Precision used for float comparisons
DEFAULT_DATAFRAME_COLUMNS = ['date', 'open', 'high', 'low', 'close', 'volume'] DEFAULT_DATAFRAME_COLUMNS = ['date', 'open', 'high', 'low', 'close', 'volume']
# Don't modify sequence of DEFAULT_TRADES_COLUMNS # Don't modify sequence of DEFAULT_TRADES_COLUMNS
# it has wide consequences for stored trades files # it has wide consequences for stored trades files
DEFAULT_TRADES_COLUMNS = ['timestamp', 'id', 'type', 'side', 'price', 'amount', 'cost'] DEFAULT_TRADES_COLUMNS = ['timestamp', 'id', 'type', 'side', 'price', 'amount', 'cost']
LAST_BT_RESULT_FN = '.last_result.json'
USERPATH_HYPEROPTS = 'hyperopts' USERPATH_HYPEROPTS = 'hyperopts'
USERPATH_STRATEGIES = 'strategies' USERPATH_STRATEGIES = 'strategies'
USERPATH_NOTEBOOKS = 'notebooks' USERPATH_NOTEBOOKS = 'notebooks'
@@ -156,7 +159,9 @@ CONF_SCHEMA = {
'emergencysell': {'type': 'string', 'enum': ORDERTYPE_POSSIBILITIES}, 'emergencysell': {'type': 'string', 'enum': ORDERTYPE_POSSIBILITIES},
'stoploss': {'type': 'string', 'enum': ORDERTYPE_POSSIBILITIES}, 'stoploss': {'type': 'string', 'enum': ORDERTYPE_POSSIBILITIES},
'stoploss_on_exchange': {'type': 'boolean'}, 'stoploss_on_exchange': {'type': 'boolean'},
'stoploss_on_exchange_interval': {'type': 'number'} 'stoploss_on_exchange_interval': {'type': 'number'},
'stoploss_on_exchange_limit_ratio': {'type': 'number', 'minimum': 0.0,
'maximum': 1.0}
}, },
'required': ['buy', 'sell', 'stoploss', 'stoploss_on_exchange'] 'required': ['buy', 'sell', 'stoploss', 'stoploss_on_exchange']
}, },
@@ -339,4 +344,5 @@ CANCEL_REASON = {
} }
# List of pairs with their timeframes # List of pairs with their timeframes
ListPairsWithTimeframes = List[Tuple[str, str]] PairWithTimeframe = Tuple[str, str]
ListPairsWithTimeframes = List[PairWithTimeframe]

View File

@@ -3,52 +3,123 @@ Helpers when analyzing backtest data
""" """
import logging import logging
from pathlib import Path from pathlib import Path
from typing import Dict, Union, Tuple from typing import Dict, Union, Tuple, Any, Optional
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from datetime import timezone from datetime import timezone
from freqtrade import persistence from freqtrade import persistence
from freqtrade.constants import LAST_BT_RESULT_FN
from freqtrade.misc import json_load from freqtrade.misc import json_load
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# must align with columns in backtest.py # must align with columns in backtest.py
BT_DATA_COLUMNS = ["pair", "profit_percent", "open_time", "close_time", "index", "duration", BT_DATA_COLUMNS = ["pair", "profit_percent", "open_date", "close_date", "index", "trade_duration",
"open_rate", "close_rate", "open_at_end", "sell_reason"] "open_rate", "close_rate", "open_at_end", "sell_reason"]
def load_backtest_data(filename: Union[Path, str]) -> pd.DataFrame: def get_latest_backtest_filename(directory: Union[Path, str]) -> str:
""" """
Load backtest data file. Get latest backtest export based on '.last_result.json'.
:param filename: pathlib.Path object, or string pointing to the file. :param directory: Directory to search for last result
:return: a dataframe with the analysis results :return: string containing the filename of the latest backtest result
:raises: ValueError in the following cases:
* Directory does not exist
* `directory/.last_result.json` does not exist
* `directory/.last_result.json` has the wrong content
""" """
if isinstance(filename, str): if isinstance(directory, str):
filename = Path(filename) directory = Path(directory)
if not directory.is_dir():
raise ValueError(f"Directory '{directory}' does not exist.")
filename = directory / LAST_BT_RESULT_FN
if not filename.is_file(): if not filename.is_file():
raise ValueError(f"File {filename} does not exist.") raise ValueError(
f"Directory '{directory}' does not seem to contain backtest statistics yet.")
with filename.open() as file: with filename.open() as file:
data = json_load(file) data = json_load(file)
df = pd.DataFrame(data, columns=BT_DATA_COLUMNS) if 'latest_backtest' not in data:
raise ValueError(f"Invalid '{LAST_BT_RESULT_FN}' format.")
df['open_time'] = pd.to_datetime(df['open_time'], return data['latest_backtest']
unit='s',
utc=True,
infer_datetime_format=True def load_backtest_stats(filename: Union[Path, str]) -> Dict[str, Any]:
) """
df['close_time'] = pd.to_datetime(df['close_time'], Load backtest statistics file.
unit='s', :param filename: pathlib.Path object, or string pointing to the file.
utc=True, :return: a dictionary containing the resulting file.
infer_datetime_format=True """
) if isinstance(filename, str):
df['profit'] = df['close_rate'] - df['open_rate'] filename = Path(filename)
df = df.sort_values("open_time").reset_index(drop=True) if filename.is_dir():
filename = filename / get_latest_backtest_filename(filename)
if not filename.is_file():
raise ValueError(f"File {filename} does not exist.")
logger.info(f"Loading backtest result from {filename}")
with filename.open() as file:
data = json_load(file)
return data
def load_backtest_data(filename: Union[Path, str], strategy: Optional[str] = None) -> pd.DataFrame:
"""
Load backtest data file.
:param filename: pathlib.Path object, or string pointing to a file or directory
:param strategy: Strategy to load - mainly relevant for multi-strategy backtests
Can also serve as protection to load the correct result.
:return: a dataframe with the analysis results
:raise: ValueError if loading goes wrong.
"""
data = load_backtest_stats(filename)
if not isinstance(data, list):
# new, nested format
if 'strategy' not in data:
raise ValueError("Unknown dataformat.")
if not strategy:
if len(data['strategy']) == 1:
strategy = list(data['strategy'].keys())[0]
else:
raise ValueError("Detected backtest result with more than one strategy. "
"Please specify a strategy.")
if strategy not in data['strategy']:
raise ValueError(f"Strategy {strategy} not available in the backtest result.")
data = data['strategy'][strategy]['trades']
df = pd.DataFrame(data)
df['open_date'] = pd.to_datetime(df['open_date'],
utc=True,
infer_datetime_format=True
)
df['close_date'] = pd.to_datetime(df['close_date'],
utc=True,
infer_datetime_format=True
)
else:
# old format - only with lists.
df = pd.DataFrame(data, columns=BT_DATA_COLUMNS)
df['open_date'] = pd.to_datetime(df['open_date'],
unit='s',
utc=True,
infer_datetime_format=True
)
df['close_date'] = pd.to_datetime(df['close_date'],
unit='s',
utc=True,
infer_datetime_format=True
)
df['profit_abs'] = df['close_rate'] - df['open_rate']
df = df.sort_values("open_date").reset_index(drop=True)
return df return df
@@ -62,9 +133,9 @@ def analyze_trade_parallelism(results: pd.DataFrame, timeframe: str) -> pd.DataF
""" """
from freqtrade.exchange import timeframe_to_minutes from freqtrade.exchange import timeframe_to_minutes
timeframe_min = timeframe_to_minutes(timeframe) timeframe_min = timeframe_to_minutes(timeframe)
dates = [pd.Series(pd.date_range(row[1].open_time, row[1].close_time, dates = [pd.Series(pd.date_range(row[1]['open_date'], row[1]['close_date'],
freq=f"{timeframe_min}min")) freq=f"{timeframe_min}min"))
for row in results[['open_time', 'close_time']].iterrows()] for row in results[['open_date', 'close_date']].iterrows()]
deltas = [len(x) for x in dates] deltas = [len(x) for x in dates]
dates = pd.Series(pd.concat(dates).values, name='date') dates = pd.Series(pd.concat(dates).values, name='date')
df2 = pd.DataFrame(np.repeat(results.values, deltas, axis=0), columns=results.columns) df2 = pd.DataFrame(np.repeat(results.values, deltas, axis=0), columns=results.columns)
@@ -90,21 +161,26 @@ def evaluate_result_multi(results: pd.DataFrame, timeframe: str,
return df_final[df_final['open_trades'] > max_open_trades] return df_final[df_final['open_trades'] > max_open_trades]
def load_trades_from_db(db_url: str) -> pd.DataFrame: def load_trades_from_db(db_url: str, strategy: Optional[str] = None) -> pd.DataFrame:
""" """
Load trades from a DB (using dburl) Load trades from a DB (using dburl)
:param db_url: Sqlite url (default format sqlite:///tradesv3.dry-run.sqlite) :param db_url: Sqlite url (default format sqlite:///tradesv3.dry-run.sqlite)
:param strategy: Strategy to load - mainly relevant for multi-strategy backtests
Can also serve as protection to load the correct result.
:return: Dataframe containing Trades :return: Dataframe containing Trades
""" """
trades: pd.DataFrame = pd.DataFrame([], columns=BT_DATA_COLUMNS)
persistence.init(db_url, clean_open_orders=False) persistence.init(db_url, clean_open_orders=False)
columns = ["pair", "open_time", "close_time", "profit", "profit_percent", columns = ["pair", "open_date", "close_date", "profit", "profit_percent",
"open_rate", "close_rate", "amount", "duration", "sell_reason", "open_rate", "close_rate", "amount", "trade_duration", "sell_reason",
"fee_open", "fee_close", "open_rate_requested", "close_rate_requested", "fee_open", "fee_close", "open_rate_requested", "close_rate_requested",
"stake_amount", "max_rate", "min_rate", "id", "exchange", "stake_amount", "max_rate", "min_rate", "id", "exchange",
"stop_loss", "initial_stop_loss", "strategy", "timeframe"] "stop_loss", "initial_stop_loss", "strategy", "timeframe"]
filters = []
if strategy:
filters.append(Trade.strategy == strategy)
trades = pd.DataFrame([(t.pair, trades = pd.DataFrame([(t.pair,
t.open_date.replace(tzinfo=timezone.utc), t.open_date.replace(tzinfo=timezone.utc),
t.close_date.replace(tzinfo=timezone.utc) if t.close_date else None, t.close_date.replace(tzinfo=timezone.utc) if t.close_date else None,
@@ -123,16 +199,16 @@ def load_trades_from_db(db_url: str) -> pd.DataFrame:
t.stop_loss, t.initial_stop_loss, t.stop_loss, t.initial_stop_loss,
t.strategy, t.timeframe t.strategy, t.timeframe
) )
for t in Trade.get_trades().all()], for t in Trade.get_trades(filters).all()],
columns=columns) columns=columns)
return trades return trades
def load_trades(source: str, db_url: str, exportfilename: Path, def load_trades(source: str, db_url: str, exportfilename: Path,
no_trades: bool = False) -> pd.DataFrame: no_trades: bool = False, strategy: Optional[str] = None) -> pd.DataFrame:
""" """
Based on configuration option "trade_source": Based on configuration option 'trade_source':
* loads data from DB (using `db_url`) * loads data from DB (using `db_url`)
* loads data from backtestfile (using `exportfilename`) * loads data from backtestfile (using `exportfilename`)
:param source: "DB" or "file" - specify source to load from :param source: "DB" or "file" - specify source to load from
@@ -148,7 +224,7 @@ def load_trades(source: str, db_url: str, exportfilename: Path,
if source == "DB": if source == "DB":
return load_trades_from_db(db_url) return load_trades_from_db(db_url)
elif source == "file": elif source == "file":
return load_backtest_data(exportfilename) return load_backtest_data(exportfilename, strategy)
def extract_trades_of_period(dataframe: pd.DataFrame, trades: pd.DataFrame, def extract_trades_of_period(dataframe: pd.DataFrame, trades: pd.DataFrame,
@@ -163,11 +239,31 @@ def extract_trades_of_period(dataframe: pd.DataFrame, trades: pd.DataFrame,
else: else:
trades_start = dataframe.iloc[0]['date'] trades_start = dataframe.iloc[0]['date']
trades_stop = dataframe.iloc[-1]['date'] trades_stop = dataframe.iloc[-1]['date']
trades = trades.loc[(trades['open_time'] >= trades_start) & trades = trades.loc[(trades['open_date'] >= trades_start) &
(trades['close_time'] <= trades_stop)] (trades['close_date'] <= trades_stop)]
return trades return trades
def calculate_market_change(data: Dict[str, pd.DataFrame], column: str = "close") -> float:
"""
Calculate market change based on "column".
Calculation is done by taking the first non-null and the last non-null element of each column
and calculating the pctchange as "(last - first) / first".
Then the results per pair are combined as mean.
:param data: Dict of Dataframes, dict key should be pair.
:param column: Column in the original dataframes to use
:return:
"""
tmp_means = []
for pair, df in data.items():
start = df[column].dropna().iloc[0]
end = df[column].dropna().iloc[-1]
tmp_means.append((end - start) / start)
return np.mean(tmp_means)
def combine_dataframes_with_mean(data: Dict[str, pd.DataFrame], def combine_dataframes_with_mean(data: Dict[str, pd.DataFrame],
column: str = "close") -> pd.DataFrame: column: str = "close") -> pd.DataFrame:
""" """
@@ -190,7 +286,7 @@ def create_cum_profit(df: pd.DataFrame, trades: pd.DataFrame, col_name: str,
""" """
Adds a column `col_name` with the cumulative profit for the given trades array. Adds a column `col_name` with the cumulative profit for the given trades array.
:param df: DataFrame with date index :param df: DataFrame with date index
:param trades: DataFrame containing trades (requires columns close_time and profit_percent) :param trades: DataFrame containing trades (requires columns close_date and profit_percent)
:param col_name: Column name that will be assigned the results :param col_name: Column name that will be assigned the results
:param timeframe: Timeframe used during the operations :param timeframe: Timeframe used during the operations
:return: Returns df with one additional column, col_name, containing the cumulative profit. :return: Returns df with one additional column, col_name, containing the cumulative profit.
@@ -201,7 +297,7 @@ def create_cum_profit(df: pd.DataFrame, trades: pd.DataFrame, col_name: str,
from freqtrade.exchange import timeframe_to_minutes from freqtrade.exchange import timeframe_to_minutes
timeframe_minutes = timeframe_to_minutes(timeframe) timeframe_minutes = timeframe_to_minutes(timeframe)
# Resample to timeframe to make sure trades match candles # Resample to timeframe to make sure trades match candles
_trades_sum = trades.resample(f'{timeframe_minutes}min', on='close_time' _trades_sum = trades.resample(f'{timeframe_minutes}min', on='close_date'
)[['profit_percent']].sum() )[['profit_percent']].sum()
df.loc[:, col_name] = _trades_sum.cumsum() df.loc[:, col_name] = _trades_sum.cumsum()
# Set first value to 0 # Set first value to 0
@@ -211,13 +307,13 @@ def create_cum_profit(df: pd.DataFrame, trades: pd.DataFrame, col_name: str,
return df return df
def calculate_max_drawdown(trades: pd.DataFrame, *, date_col: str = 'close_time', def calculate_max_drawdown(trades: pd.DataFrame, *, date_col: str = 'close_date',
value_col: str = 'profit_percent' value_col: str = 'profit_percent'
) -> Tuple[float, pd.Timestamp, pd.Timestamp]: ) -> Tuple[float, pd.Timestamp, pd.Timestamp]:
""" """
Calculate max drawdown and the corresponding close dates Calculate max drawdown and the corresponding close dates
:param trades: DataFrame containing trades (requires columns close_time and profit_percent) :param trades: DataFrame containing trades (requires columns close_date and profit_percent)
:param date_col: Column in DataFrame to use for dates (defaults to 'close_time') :param date_col: Column in DataFrame to use for dates (defaults to 'close_date')
:param value_col: Column in DataFrame to use for values (defaults to 'profit_percent') :param value_col: Column in DataFrame to use for values (defaults to 'profit_percent')
:return: Tuple (float, highdate, lowdate) with absolute max drawdown, high and low time :return: Tuple (float, highdate, lowdate) with absolute max drawdown, high and low time
:raise: ValueError if trade-dataframe was found empty. :raise: ValueError if trade-dataframe was found empty.

View File

@@ -5,16 +5,17 @@ including ticker and orderbook data, live and historical candle (OHLCV) data
Common Interface for bot and strategy to access data. Common Interface for bot and strategy to access data.
""" """
import logging import logging
from typing import Any, Dict, List, Optional from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, Tuple
from arrow import Arrow
from pandas import DataFrame from pandas import DataFrame
from freqtrade.constants import ListPairsWithTimeframes, PairWithTimeframe
from freqtrade.data.history import load_pair_history from freqtrade.data.history import load_pair_history
from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.exceptions import ExchangeError, OperationalException
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.state import RunMode from freqtrade.state import RunMode
from freqtrade.constants import ListPairsWithTimeframes
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -25,6 +26,18 @@ class DataProvider:
self._config = config self._config = config
self._exchange = exchange self._exchange = exchange
self._pairlists = pairlists self._pairlists = pairlists
self.__cached_pairs: Dict[PairWithTimeframe, Tuple[DataFrame, datetime]] = {}
def _set_cached_df(self, pair: str, timeframe: str, dataframe: DataFrame) -> None:
"""
Store cached Dataframe.
Using private method as this should never be used by a user
(but the class is exposed via `self.dp` to the strategy)
:param pair: pair to get the data for
:param timeframe: Timeframe to get data for
:param dataframe: analyzed dataframe
"""
self.__cached_pairs[(pair, timeframe)] = (dataframe, Arrow.utcnow().datetime)
def refresh(self, def refresh(self,
pairlist: ListPairsWithTimeframes, pairlist: ListPairsWithTimeframes,
@@ -89,6 +102,20 @@ class DataProvider:
logger.warning(f"No data found for ({pair}, {timeframe}).") logger.warning(f"No data found for ({pair}, {timeframe}).")
return data return data
def get_analyzed_dataframe(self, pair: str, timeframe: str) -> Tuple[DataFrame, datetime]:
"""
:param pair: pair to get the data for
:param timeframe: timeframe to get data for
:return: Tuple of (Analyzed Dataframe, lastrefreshed) for the requested pair / timeframe
combination.
Returns empty dataframe and Epoch 0 (1970-01-01) if no dataframe was cached.
"""
if (pair, timeframe) in self.__cached_pairs:
return self.__cached_pairs[(pair, timeframe)]
else:
return (DataFrame(), datetime.fromtimestamp(0, tz=timezone.utc))
def market(self, pair: str) -> Optional[Dict[str, Any]]: def market(self, pair: str) -> Optional[Dict[str, Any]]:
""" """
Return market data for the pair Return market data for the pair
@@ -105,7 +132,7 @@ class DataProvider:
""" """
try: try:
return self._exchange.fetch_ticker(pair) return self._exchange.fetch_ticker(pair)
except DependencyException: except ExchangeError:
return {} return {}
def orderbook(self, pair: str, maximum: int) -> Dict[str, List]: def orderbook(self, pair: str, maximum: int) -> Dict[str, List]:

View File

@@ -13,6 +13,7 @@ from typing import List, Optional, Type
from pandas import DataFrame from pandas import DataFrame
from freqtrade.configuration import TimeRange from freqtrade.configuration import TimeRange
from freqtrade.constants import ListPairsWithTimeframes
from freqtrade.data.converter import (clean_ohlcv_dataframe, from freqtrade.data.converter import (clean_ohlcv_dataframe,
trades_remove_duplicates, trim_dataframe) trades_remove_duplicates, trim_dataframe)
from freqtrade.exchange import timeframe_to_seconds from freqtrade.exchange import timeframe_to_seconds
@@ -28,6 +29,14 @@ class IDataHandler(ABC):
def __init__(self, datadir: Path) -> None: def __init__(self, datadir: Path) -> None:
self._datadir = datadir self._datadir = datadir
@abstractclassmethod
def ohlcv_get_available_data(cls, datadir: Path) -> ListPairsWithTimeframes:
"""
Returns a list of all pairs with ohlcv data available in this datadir
:param datadir: Directory to search for ohlcv files
:return: List of Tuples of (pair, timeframe)
"""
@abstractclassmethod @abstractclassmethod
def ohlcv_get_pairs(cls, datadir: Path, timeframe: str) -> List[str]: def ohlcv_get_pairs(cls, datadir: Path, timeframe: str) -> List[str]:
""" """

View File

@@ -8,7 +8,8 @@ from pandas import DataFrame, read_json, to_datetime
from freqtrade import misc from freqtrade import misc
from freqtrade.configuration import TimeRange from freqtrade.configuration import TimeRange
from freqtrade.constants import DEFAULT_DATAFRAME_COLUMNS from freqtrade.constants import (DEFAULT_DATAFRAME_COLUMNS,
ListPairsWithTimeframes)
from freqtrade.data.converter import trades_dict_to_list from freqtrade.data.converter import trades_dict_to_list
from .idatahandler import IDataHandler, TradeList from .idatahandler import IDataHandler, TradeList
@@ -21,6 +22,18 @@ class JsonDataHandler(IDataHandler):
_use_zip = False _use_zip = False
_columns = DEFAULT_DATAFRAME_COLUMNS _columns = DEFAULT_DATAFRAME_COLUMNS
@classmethod
def ohlcv_get_available_data(cls, datadir: Path) -> ListPairsWithTimeframes:
"""
Returns a list of all pairs with ohlcv data available in this datadir
:param datadir: Directory to search for ohlcv files
:return: List of Tuples of (pair, timeframe)
"""
_tmp = [re.search(r'^([a-zA-Z_]+)\-(\d+\S+)(?=.json)', p.name)
for p in datadir.glob(f"*.{cls._get_file_extension()}")]
return [(match[1].replace('_', '/'), match[2]) for match in _tmp
if match and len(match.groups()) > 1]
@classmethod @classmethod
def ohlcv_get_pairs(cls, datadir: Path, timeframe: str) -> List[str]: def ohlcv_get_pairs(cls, datadir: Path, timeframe: str) -> List[str]:
""" """

View File

@@ -9,7 +9,7 @@ import utils_find_1st as utf1st
from pandas import DataFrame from pandas import DataFrame
from freqtrade.configuration import TimeRange from freqtrade.configuration import TimeRange
from freqtrade.constants import UNLIMITED_STAKE_AMOUNT from freqtrade.constants import UNLIMITED_STAKE_AMOUNT, DATETIME_PRINT_FORMAT
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.data.history import get_timerange, load_data, refresh_data from freqtrade.data.history import get_timerange, load_data, refresh_data
from freqtrade.strategy.interface import SellType from freqtrade.strategy.interface import SellType
@@ -121,12 +121,9 @@ class Edge:
# Print timeframe # Print timeframe
min_date, max_date = get_timerange(preprocessed) min_date, max_date = get_timerange(preprocessed)
logger.info( logger.info(f'Measuring data from {min_date.strftime(DATETIME_PRINT_FORMAT)} '
'Measuring data from %s up to %s (%s days) ...', f'up to {max_date.strftime(DATETIME_PRINT_FORMAT)} '
min_date.isoformat(), f'({(max_date - min_date).days} days)..')
max_date.isoformat(),
(max_date - min_date).days
)
headers = ['date', 'buy', 'open', 'close', 'sell', 'high', 'low'] headers = ['date', 'buy', 'open', 'close', 'sell', 'high', 'low']
trades: list = [] trades: list = []
@@ -240,7 +237,7 @@ class Edge:
# All returned values are relative, they are defined as ratios. # All returned values are relative, they are defined as ratios.
stake = 0.015 stake = 0.015
result['trade_duration'] = result['close_time'] - result['open_time'] result['trade_duration'] = result['close_date'] - result['open_date']
result['trade_duration'] = result['trade_duration'].map( result['trade_duration'] = result['trade_duration'].map(
lambda x: int(x.total_seconds() / 60)) lambda x: int(x.total_seconds() / 60))
@@ -281,8 +278,8 @@ class Edge:
# #
# Removing Pumps # Removing Pumps
if self.edge_config.get('remove_pumps', False): if self.edge_config.get('remove_pumps', False):
results = results.groupby(['pair', 'stoploss']).apply( results = results[results['profit_abs'] < 2 * results['profit_abs'].std()
lambda x: x[x['profit_abs'] < 2 * x['profit_abs'].std() + x['profit_abs'].mean()]) + results['profit_abs'].mean()]
########################################################################## ##########################################################################
# Removing trades having a duration more than X minutes (set in config) # Removing trades having a duration more than X minutes (set in config)
@@ -430,10 +427,8 @@ class Edge:
'stoploss': stoploss, 'stoploss': stoploss,
'profit_ratio': '', 'profit_ratio': '',
'profit_abs': '', 'profit_abs': '',
'open_time': date_column[open_trade_index], 'open_date': date_column[open_trade_index],
'close_time': date_column[exit_index], 'close_date': date_column[exit_index],
'open_index': start_point + open_trade_index,
'close_index': start_point + exit_index,
'trade_duration': '', 'trade_duration': '',
'open_rate': round(open_price, 15), 'open_rate': round(open_price, 15),
'close_rate': round(exit_price, 15), 'close_rate': round(exit_price, 15),

View File

@@ -29,7 +29,14 @@ class PricingError(DependencyException):
""" """
class InvalidOrderException(FreqtradeException): class ExchangeError(DependencyException):
"""
Error raised out of the exchange.
Has multiple Errors to determine the appropriate error.
"""
class InvalidOrderException(ExchangeError):
""" """
This is returned when the order is not valid. Example: This is returned when the order is not valid. Example:
If stoploss on exchange order is hit, then trying to cancel the order If stoploss on exchange order is hit, then trying to cancel the order
@@ -37,7 +44,14 @@ class InvalidOrderException(FreqtradeException):
""" """
class TemporaryError(FreqtradeException): class RetryableOrderError(InvalidOrderException):
"""
This is returned when the order is not found.
This Error will be repeated with increasing backof (in line with DDosError).
"""
class TemporaryError(ExchangeError):
""" """
Temporary network or exchange related error. Temporary network or exchange related error.
This could happen when an exchange is congested, unavailable, or the user This could happen when an exchange is congested, unavailable, or the user
@@ -45,6 +59,13 @@ class TemporaryError(FreqtradeException):
""" """
class DDosProtection(TemporaryError):
"""
Temporary error caused by DDOS protection.
Bot will wait for a second and then retry.
"""
class StrategyError(FreqtradeException): class StrategyError(FreqtradeException):
""" """
Errors with custom user-code deteced. Errors with custom user-code deteced.

View File

@@ -12,8 +12,7 @@ from freqtrade.exchange.exchange import (timeframe_to_seconds,
timeframe_to_msecs, timeframe_to_msecs,
timeframe_to_next_date, timeframe_to_next_date,
timeframe_to_prev_date) timeframe_to_prev_date)
from freqtrade.exchange.exchange import (market_is_active, from freqtrade.exchange.exchange import (market_is_active)
symbol_is_pair)
from freqtrade.exchange.kraken import Kraken from freqtrade.exchange.kraken import Kraken
from freqtrade.exchange.binance import Binance from freqtrade.exchange.binance import Binance
from freqtrade.exchange.bibox import Bibox from freqtrade.exchange.bibox import Bibox

View File

@@ -4,9 +4,11 @@ from typing import Dict
import ccxt import ccxt
from freqtrade.exceptions import (DependencyException, InvalidOrderException, from freqtrade.exceptions import (DDosProtection, ExchangeError,
OperationalException, TemporaryError) InvalidOrderException, OperationalException,
TemporaryError)
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.exchange.common import retrier
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -39,6 +41,7 @@ class Binance(Exchange):
""" """
return order['type'] == 'stop_loss_limit' and stop_loss > float(order['info']['stopPrice']) return order['type'] == 'stop_loss_limit' and stop_loss > float(order['info']['stopPrice'])
@retrier(retries=0)
def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict: def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict:
""" """
creates a stoploss limit order. creates a stoploss limit order.
@@ -77,8 +80,8 @@ class Binance(Exchange):
'stop price: %s. limit: %s', pair, stop_price, rate) 'stop price: %s. limit: %s', pair, stop_price, rate)
return order return order
except ccxt.InsufficientFunds as e: except ccxt.InsufficientFunds as e:
raise DependencyException( raise ExchangeError(
f'Insufficient funds to create {ordertype} sell order on market {pair}.' f'Insufficient funds to create {ordertype} sell order on market {pair}. '
f'Tried to sell amount {amount} at rate {rate}. ' f'Tried to sell amount {amount} at rate {rate}. '
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.InvalidOrder as e: except ccxt.InvalidOrder as e:
@@ -88,6 +91,8 @@ class Binance(Exchange):
f'Could not create {ordertype} sell order on market {pair}. ' f'Could not create {ordertype} sell order on market {pair}. '
f'Tried to sell amount {amount} at rate {rate}. ' f'Tried to sell amount {amount} at rate {rate}. '
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not place sell order due to {e.__class__.__name__}. Message: {e}') from e f'Could not place sell order due to {e.__class__.__name__}. Message: {e}') from e

View File

@@ -1,6 +1,10 @@
import asyncio
import logging import logging
import time
from functools import wraps
from freqtrade.exceptions import TemporaryError from freqtrade.exceptions import (DDosProtection, RetryableOrderError,
TemporaryError)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -12,6 +16,7 @@ BAD_EXCHANGES = {
"Details in https://github.com/freqtrade/freqtrade/issues/1983", "Details in https://github.com/freqtrade/freqtrade/issues/1983",
"hitbtc": "This API cannot be used with Freqtrade. " "hitbtc": "This API cannot be used with Freqtrade. "
"Use `hitbtc2` exchange id to access this exchange.", "Use `hitbtc2` exchange id to access this exchange.",
"phemex": "Does not provide history. ",
**dict.fromkeys([ **dict.fromkeys([
'adara', 'adara',
'anxpro', 'anxpro',
@@ -88,6 +93,13 @@ MAP_EXCHANGE_CHILDCLASS = {
} }
def calculate_backoff(retrycount, max_retries):
"""
Calculate backoff
"""
return (max_retries - retrycount) ** 2 + 1
def retrier_async(f): def retrier_async(f):
async def wrapper(*args, **kwargs): async def wrapper(*args, **kwargs):
count = kwargs.pop('count', API_RETRY_COUNT) count = kwargs.pop('count', API_RETRY_COUNT)
@@ -96,9 +108,13 @@ def retrier_async(f):
except TemporaryError as ex: except TemporaryError as ex:
logger.warning('%s() returned exception: "%s"', f.__name__, ex) logger.warning('%s() returned exception: "%s"', f.__name__, ex)
if count > 0: if count > 0:
logger.warning('retrying %s() still for %s times', f.__name__, count)
count -= 1 count -= 1
kwargs.update({'count': count}) kwargs.update({'count': count})
logger.warning('retrying %s() still for %s times', f.__name__, count) if isinstance(ex, DDosProtection):
backoff_delay = calculate_backoff(count + 1, API_RETRY_COUNT)
logger.info(f"Applying DDosProtection backoff delay: {backoff_delay}")
await asyncio.sleep(backoff_delay)
return await wrapper(*args, **kwargs) return await wrapper(*args, **kwargs)
else: else:
logger.warning('Giving up retrying: %s()', f.__name__) logger.warning('Giving up retrying: %s()', f.__name__)
@@ -106,19 +122,31 @@ def retrier_async(f):
return wrapper return wrapper
def retrier(f): def retrier(_func=None, retries=API_RETRY_COUNT):
def wrapper(*args, **kwargs): def decorator(f):
count = kwargs.pop('count', API_RETRY_COUNT) @wraps(f)
try: def wrapper(*args, **kwargs):
return f(*args, **kwargs) count = kwargs.pop('count', retries)
except TemporaryError as ex: try:
logger.warning('%s() returned exception: "%s"', f.__name__, ex) return f(*args, **kwargs)
if count > 0: except (TemporaryError, RetryableOrderError) as ex:
count -= 1 logger.warning('%s() returned exception: "%s"', f.__name__, ex)
kwargs.update({'count': count}) if count > 0:
logger.warning('retrying %s() still for %s times', f.__name__, count) logger.warning('retrying %s() still for %s times', f.__name__, count)
return wrapper(*args, **kwargs) count -= 1
else: kwargs.update({'count': count})
logger.warning('Giving up retrying: %s()', f.__name__) if isinstance(ex, DDosProtection) or isinstance(ex, RetryableOrderError):
raise ex # increasing backoff
return wrapper backoff_delay = calculate_backoff(count + 1, retries)
logger.info(f"Applying DDosProtection backoff delay: {backoff_delay}")
time.sleep(backoff_delay)
return wrapper(*args, **kwargs)
else:
logger.warning('Giving up retrying: %s()', f.__name__)
raise ex
return wrapper
# Support both @retrier and @retrier(retries=2) syntax
if _func is None:
return decorator
else:
return decorator(_func)

View File

@@ -18,12 +18,13 @@ from ccxt.base.decimal_to_precision import (ROUND_DOWN, ROUND_UP, TICK_SIZE,
TRUNCATE, decimal_to_precision) TRUNCATE, decimal_to_precision)
from pandas import DataFrame from pandas import DataFrame
from freqtrade.data.converter import ohlcv_to_dataframe, trades_dict_to_list
from freqtrade.exceptions import (DependencyException, InvalidOrderException,
OperationalException, TemporaryError)
from freqtrade.exchange.common import BAD_EXCHANGES, retrier, retrier_async
from freqtrade.misc import deep_merge_dicts, safe_value_fallback
from freqtrade.constants import ListPairsWithTimeframes from freqtrade.constants import ListPairsWithTimeframes
from freqtrade.data.converter import ohlcv_to_dataframe, trades_dict_to_list
from freqtrade.exceptions import (DDosProtection, ExchangeError,
InvalidOrderException, OperationalException,
RetryableOrderError, TemporaryError)
from freqtrade.exchange.common import BAD_EXCHANGES, retrier, retrier_async
from freqtrade.misc import deep_merge_dicts, safe_value_fallback2
CcxtModuleType = Any CcxtModuleType = Any
@@ -84,8 +85,8 @@ class Exchange:
# Deep merge ft_has with default ft_has options # Deep merge ft_has with default ft_has options
self._ft_has = deep_merge_dicts(self._ft_has, deepcopy(self._ft_has_default)) self._ft_has = deep_merge_dicts(self._ft_has, deepcopy(self._ft_has_default))
if exchange_config.get("_ft_has_params"): if exchange_config.get('_ft_has_params'):
self._ft_has = deep_merge_dicts(exchange_config.get("_ft_has_params"), self._ft_has = deep_merge_dicts(exchange_config.get('_ft_has_params'),
self._ft_has) self._ft_has)
logger.info("Overriding exchange._ft_has with config params, result: %s", self._ft_has) logger.info("Overriding exchange._ft_has with config params, result: %s", self._ft_has)
@@ -186,6 +187,11 @@ class Exchange:
def timeframes(self) -> List[str]: def timeframes(self) -> List[str]:
return list((self._api.timeframes or {}).keys()) return list((self._api.timeframes or {}).keys())
@property
def ohlcv_candle_limit(self) -> int:
"""exchange ohlcv candle limit"""
return int(self._ohlcv_candle_limit)
@property @property
def markets(self) -> Dict: def markets(self) -> Dict:
"""exchange ccxt markets""" """exchange ccxt markets"""
@@ -216,7 +222,7 @@ class Exchange:
if quote_currencies: if quote_currencies:
markets = {k: v for k, v in markets.items() if v['quote'] in quote_currencies} markets = {k: v for k, v in markets.items() if v['quote'] in quote_currencies}
if pairs_only: if pairs_only:
markets = {k: v for k, v in markets.items() if symbol_is_pair(v['symbol'])} markets = {k: v for k, v in markets.items() if self.market_is_tradable(v)}
if active_only: if active_only:
markets = {k: v for k, v in markets.items() if market_is_active(v)} markets = {k: v for k, v in markets.items() if market_is_active(v)}
return markets return markets
@@ -240,6 +246,19 @@ class Exchange:
""" """
return self.markets.get(pair, {}).get('base', '') return self.markets.get(pair, {}).get('base', '')
def market_is_tradable(self, market: Dict[str, Any]) -> bool:
"""
Check if the market symbol is tradable by Freqtrade.
By default, checks if it's splittable by `/` and both sides correspond to base / quote
"""
symbol_parts = market['symbol'].split('/')
return (len(symbol_parts) == 2 and
len(symbol_parts[0]) > 0 and
len(symbol_parts[1]) > 0 and
symbol_parts[0] == market.get('base') and
symbol_parts[1] == market.get('quote')
)
def klines(self, pair_interval: Tuple[str, str], copy: bool = True) -> DataFrame: def klines(self, pair_interval: Tuple[str, str], copy: bool = True) -> DataFrame:
if pair_interval in self._klines: if pair_interval in self._klines:
return self._klines[pair_interval].copy() if copy else self._klines[pair_interval] return self._klines[pair_interval].copy() if copy else self._klines[pair_interval]
@@ -252,8 +271,8 @@ class Exchange:
api.urls['api'] = api.urls['test'] api.urls['api'] = api.urls['test']
logger.info("Enabled Sandbox API on %s", name) logger.info("Enabled Sandbox API on %s", name)
else: else:
logger.warning(name, "No Sandbox URL in CCXT, exiting. " logger.warning(
"Please check your config.json") f"No Sandbox URL in CCXT for {name}, exiting. Please check your config.json")
raise OperationalException(f'Exchange {name} does not provide a sandbox api') raise OperationalException(f'Exchange {name} does not provide a sandbox api')
def _load_async_markets(self, reload: bool = False) -> None: def _load_async_markets(self, reload: bool = False) -> None:
@@ -351,7 +370,7 @@ class Exchange:
for pair in [f"{curr_1}/{curr_2}", f"{curr_2}/{curr_1}"]: for pair in [f"{curr_1}/{curr_2}", f"{curr_2}/{curr_1}"]:
if pair in self.markets and self.markets[pair].get('active'): if pair in self.markets and self.markets[pair].get('active'):
return pair return pair
raise DependencyException(f"Could not combine {curr_1} and {curr_2} to get a valid pair.") raise ExchangeError(f"Could not combine {curr_1} and {curr_2} to get a valid pair.")
def validate_timeframes(self, timeframe: Optional[str]) -> None: def validate_timeframes(self, timeframe: Optional[str]) -> None:
""" """
@@ -474,6 +493,7 @@ class Exchange:
"id": order_id, "id": order_id,
'pair': pair, 'pair': pair,
'price': rate, 'price': rate,
'average': rate,
'amount': _amount, 'amount': _amount,
'cost': _amount * rate, 'cost': _amount * rate,
'type': ordertype, 'type': ordertype,
@@ -518,15 +538,17 @@ class Exchange:
amount, rate_for_order, params) amount, rate_for_order, params)
except ccxt.InsufficientFunds as e: except ccxt.InsufficientFunds as e:
raise DependencyException( raise ExchangeError(
f'Insufficient funds to create {ordertype} {side} order on market {pair}.' f'Insufficient funds to create {ordertype} {side} order on market {pair}. '
f'Tried to {side} amount {amount} at rate {rate}.' f'Tried to {side} amount {amount} at rate {rate}.'
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.InvalidOrder as e: except ccxt.InvalidOrder as e:
raise DependencyException( raise ExchangeError(
f'Could not create {ordertype} {side} order on market {pair}.' f'Could not create {ordertype} {side} order on market {pair}. '
f'Tried to {side} amount {amount} at rate {rate}.' f'Tried to {side} amount {amount} at rate {rate}. '
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not place {side} order due to {e.__class__.__name__}. Message: {e}') from e f'Could not place {side} order due to {e.__class__.__name__}. Message: {e}') from e
@@ -606,6 +628,8 @@ class Exchange:
balances.pop("used", None) balances.pop("used", None)
return balances return balances
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not get balance due to {e.__class__.__name__}. Message: {e}') from e f'Could not get balance due to {e.__class__.__name__}. Message: {e}') from e
@@ -620,6 +644,8 @@ class Exchange:
raise OperationalException( raise OperationalException(
f'Exchange {self._api.name} does not support fetching tickers in batch. ' f'Exchange {self._api.name} does not support fetching tickers in batch. '
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not load tickers due to {e.__class__.__name__}. Message: {e}') from e f'Could not load tickers due to {e.__class__.__name__}. Message: {e}') from e
@@ -630,9 +656,11 @@ class Exchange:
def fetch_ticker(self, pair: str) -> dict: def fetch_ticker(self, pair: str) -> dict:
try: try:
if pair not in self._api.markets or not self._api.markets[pair].get('active'): if pair not in self._api.markets or not self._api.markets[pair].get('active'):
raise DependencyException(f"Pair {pair} not available") raise ExchangeError(f"Pair {pair} not available")
data = self._api.fetch_ticker(pair) data = self._api.fetch_ticker(pair)
return data return data
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not load ticker due to {e.__class__.__name__}. Message: {e}') from e f'Could not load ticker due to {e.__class__.__name__}. Message: {e}') from e
@@ -766,6 +794,8 @@ class Exchange:
raise OperationalException( raise OperationalException(
f'Exchange {self._api.name} does not support fetching historical ' f'Exchange {self._api.name} does not support fetching historical '
f'candle (OHLCV) data. Message: {e}') from e f'candle (OHLCV) data. Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError(f'Could not fetch historical candle (OHLCV) data ' raise TemporaryError(f'Could not fetch historical candle (OHLCV) data '
f'for pair {pair} due to {e.__class__.__name__}. ' f'for pair {pair} due to {e.__class__.__name__}. '
@@ -802,6 +832,8 @@ class Exchange:
raise OperationalException( raise OperationalException(
f'Exchange {self._api.name} does not support fetching historical trade data.' f'Exchange {self._api.name} does not support fetching historical trade data.'
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError(f'Could not load trade history due to {e.__class__.__name__}. ' raise TemporaryError(f'Could not load trade history due to {e.__class__.__name__}. '
f'Message: {e}') from e f'Message: {e}') from e
@@ -933,7 +965,7 @@ class Exchange:
def check_order_canceled_empty(self, order: Dict) -> bool: def check_order_canceled_empty(self, order: Dict) -> bool:
""" """
Verify if an order has been cancelled without being partially filled Verify if an order has been cancelled without being partially filled
:param order: Order dict as returned from get_order() :param order: Order dict as returned from fetch_order()
:return: True if order has been cancelled without being filled, False otherwise. :return: True if order has been cancelled without being filled, False otherwise.
""" """
return order.get('status') in ('closed', 'canceled') and order.get('filled') == 0.0 return order.get('status') in ('closed', 'canceled') and order.get('filled') == 0.0
@@ -948,13 +980,15 @@ class Exchange:
except ccxt.InvalidOrder as e: except ccxt.InvalidOrder as e:
raise InvalidOrderException( raise InvalidOrderException(
f'Could not cancel order. Message: {e}') from e f'Could not cancel order. Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not cancel order due to {e.__class__.__name__}. Message: {e}') from e f'Could not cancel order due to {e.__class__.__name__}. Message: {e}') from e
except ccxt.BaseError as e: except ccxt.BaseError as e:
raise OperationalException(e) from e raise OperationalException(e) from e
# Assign method to get_stoploss_order to allow easy overriding in other classes # Assign method to cancel_stoploss_order to allow easy overriding in other classes
cancel_stoploss_order = cancel_order cancel_stoploss_order = cancel_order
def is_cancel_order_result_suitable(self, corder) -> bool: def is_cancel_order_result_suitable(self, corder) -> bool:
@@ -968,7 +1002,7 @@ class Exchange:
""" """
Cancel order returning a result. Cancel order returning a result.
Creates a fake result if cancel order returns a non-usable result Creates a fake result if cancel order returns a non-usable result
and get_order does not work (certain exchanges don't return cancelled orders) and fetch_order does not work (certain exchanges don't return cancelled orders)
:param order_id: Orderid to cancel :param order_id: Orderid to cancel
:param pair: Pair corresponding to order_id :param pair: Pair corresponding to order_id
:param amount: Amount to use for fake response :param amount: Amount to use for fake response
@@ -979,17 +1013,17 @@ class Exchange:
if self.is_cancel_order_result_suitable(corder): if self.is_cancel_order_result_suitable(corder):
return corder return corder
except InvalidOrderException: except InvalidOrderException:
logger.warning(f"Could not cancel order {order_id}.") logger.warning(f"Could not cancel order {order_id} for {pair}.")
try: try:
order = self.get_order(order_id, pair) order = self.fetch_order(order_id, pair)
except InvalidOrderException: except InvalidOrderException:
logger.warning(f"Could not fetch cancelled order {order_id}.") logger.warning(f"Could not fetch cancelled order {order_id}.")
order = {'fee': {}, 'status': 'canceled', 'amount': amount, 'info': {}} order = {'fee': {}, 'status': 'canceled', 'amount': amount, 'info': {}}
return order return order
@retrier @retrier(retries=5)
def get_order(self, order_id: str, pair: str) -> Dict: def fetch_order(self, order_id: str, pair: str) -> Dict:
if self._config['dry_run']: if self._config['dry_run']:
try: try:
order = self._dry_run_open_orders[order_id] order = self._dry_run_open_orders[order_id]
@@ -1000,25 +1034,30 @@ class Exchange:
f'Tried to get an invalid dry-run-order (id: {order_id}). Message: {e}') from e f'Tried to get an invalid dry-run-order (id: {order_id}). Message: {e}') from e
try: try:
return self._api.fetch_order(order_id, pair) return self._api.fetch_order(order_id, pair)
except ccxt.OrderNotFound as e:
raise RetryableOrderError(
f'Order not found (pair: {pair} id: {order_id}). Message: {e}') from e
except ccxt.InvalidOrder as e: except ccxt.InvalidOrder as e:
raise InvalidOrderException( raise InvalidOrderException(
f'Tried to get an invalid order (id: {order_id}). Message: {e}') from e f'Tried to get an invalid order (pair: {pair} id: {order_id}). Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not get order due to {e.__class__.__name__}. Message: {e}') from e f'Could not get order due to {e.__class__.__name__}. Message: {e}') from e
except ccxt.BaseError as e: except ccxt.BaseError as e:
raise OperationalException(e) from e raise OperationalException(e) from e
# Assign method to get_stoploss_order to allow easy overriding in other classes # Assign method to fetch_stoploss_order to allow easy overriding in other classes
get_stoploss_order = get_order fetch_stoploss_order = fetch_order
@retrier @retrier
def fetch_l2_order_book(self, pair: str, limit: int = 100) -> dict: def fetch_l2_order_book(self, pair: str, limit: int = 100) -> dict:
""" """
get order book level 2 from exchange Get L2 order book from exchange.
Can be limited to a certain amount (if supported).
Notes: Returns a dict in the format
20180619: bittrex doesnt support limits -.- {'asks': [price, volume], 'bids': [price, volume]}
""" """
try: try:
@@ -1027,6 +1066,8 @@ class Exchange:
raise OperationalException( raise OperationalException(
f'Exchange {self._api.name} does not support fetching order book.' f'Exchange {self._api.name} does not support fetching order book.'
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not get order book due to {e.__class__.__name__}. Message: {e}') from e f'Could not get order book due to {e.__class__.__name__}. Message: {e}') from e
@@ -1063,7 +1104,8 @@ class Exchange:
matched_trades = [trade for trade in my_trades if trade['order'] == order_id] matched_trades = [trade for trade in my_trades if trade['order'] == order_id]
return matched_trades return matched_trades
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not get trades due to {e.__class__.__name__}. Message: {e}') from e f'Could not get trades due to {e.__class__.__name__}. Message: {e}') from e
@@ -1080,6 +1122,8 @@ class Exchange:
return self._api.calculate_fee(symbol=symbol, type=type, side=side, amount=amount, return self._api.calculate_fee(symbol=symbol, type=type, side=side, amount=amount,
price=price, takerOrMaker=taker_or_maker)['rate'] price=price, takerOrMaker=taker_or_maker)['rate']
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not get fee info due to {e.__class__.__name__}. Message: {e}') from e f'Could not get fee info due to {e.__class__.__name__}. Message: {e}') from e
@@ -1114,7 +1158,7 @@ class Exchange:
if fee_curr in self.get_pair_base_currency(order['symbol']): if fee_curr in self.get_pair_base_currency(order['symbol']):
# Base currency - divide by amount # Base currency - divide by amount
return round( return round(
order['fee']['cost'] / safe_value_fallback(order, order, 'filled', 'amount'), 8) order['fee']['cost'] / safe_value_fallback2(order, order, 'filled', 'amount'), 8)
elif fee_curr in self.get_pair_quote_currency(order['symbol']): elif fee_curr in self.get_pair_quote_currency(order['symbol']):
# Quote currency - divide by cost # Quote currency - divide by cost
return round(order['fee']['cost'] / order['cost'], 8) if order['cost'] else None return round(order['fee']['cost'] / order['cost'], 8) if order['cost'] else None
@@ -1127,9 +1171,9 @@ class Exchange:
comb = self.get_valid_pair_combination(fee_curr, self._config['stake_currency']) comb = self.get_valid_pair_combination(fee_curr, self._config['stake_currency'])
tick = self.fetch_ticker(comb) tick = self.fetch_ticker(comb)
fee_to_quote_rate = safe_value_fallback(tick, tick, 'last', 'ask') fee_to_quote_rate = safe_value_fallback2(tick, tick, 'last', 'ask')
return round((order['fee']['cost'] * fee_to_quote_rate) / order['cost'], 8) return round((order['fee']['cost'] * fee_to_quote_rate) / order['cost'], 8)
except DependencyException: except ExchangeError:
return None return None
def extract_cost_curr_rate(self, order: Dict) -> Tuple[float, str, Optional[float]]: def extract_cost_curr_rate(self, order: Dict) -> Tuple[float, str, Optional[float]]:
@@ -1142,7 +1186,6 @@ class Exchange:
return (order['fee']['cost'], return (order['fee']['cost'],
order['fee']['currency'], order['fee']['currency'],
self.calculate_fee_rate(order)) self.calculate_fee_rate(order))
# calculate rate ? (order['fee']['cost'] / (order['amount'] * order['price']))
def is_exchange_bad(exchange_name: str) -> bool: def is_exchange_bad(exchange_name: str) -> bool:
@@ -1228,20 +1271,6 @@ def timeframe_to_next_date(timeframe: str, date: datetime = None) -> datetime:
return datetime.fromtimestamp(new_timestamp, tz=timezone.utc) return datetime.fromtimestamp(new_timestamp, tz=timezone.utc)
def symbol_is_pair(market_symbol: str, base_currency: str = None,
quote_currency: str = None) -> bool:
"""
Check if the market symbol is a pair, i.e. that its symbol consists of the base currency and the
quote currency separated by '/' character. If base_currency and/or quote_currency is passed,
it also checks that the symbol contains appropriate base and/or quote currency part before
and after the separating character correspondingly.
"""
symbol_parts = market_symbol.split('/')
return (len(symbol_parts) == 2 and
(symbol_parts[0] == base_currency if base_currency else len(symbol_parts[0]) > 0) and
(symbol_parts[1] == quote_currency if quote_currency else len(symbol_parts[1]) > 0))
def market_is_active(market: Dict) -> bool: def market_is_active(market: Dict) -> bool:
""" """
Return True if the market is active. Return True if the market is active.

View File

@@ -1,11 +1,12 @@
""" FTX exchange subclass """ """ FTX exchange subclass """
import logging import logging
from typing import Dict from typing import Any, Dict
import ccxt import ccxt
from freqtrade.exceptions import (DependencyException, InvalidOrderException, from freqtrade.exceptions import (DDosProtection, ExchangeError,
OperationalException, TemporaryError) InvalidOrderException, OperationalException,
TemporaryError)
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.exchange.common import retrier from freqtrade.exchange.common import retrier
@@ -19,6 +20,16 @@ class Ftx(Exchange):
"ohlcv_candle_limit": 1500, "ohlcv_candle_limit": 1500,
} }
def market_is_tradable(self, market: Dict[str, Any]) -> bool:
"""
Check if the market symbol is tradable by Freqtrade.
Default checks + check if pair is spot pair (no futures trading yet).
"""
parent_check = super().market_is_tradable(market)
return (parent_check and
market.get('spot', False) is True)
def stoploss_adjust(self, stop_loss: float, order: Dict) -> bool: def stoploss_adjust(self, stop_loss: float, order: Dict) -> bool:
""" """
Verify stop_loss against stoploss-order value (limit or price) Verify stop_loss against stoploss-order value (limit or price)
@@ -26,6 +37,7 @@ class Ftx(Exchange):
""" """
return order['type'] == 'stop' and stop_loss > float(order['price']) return order['type'] == 'stop' and stop_loss > float(order['price'])
@retrier(retries=0)
def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict: def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict:
""" """
Creates a stoploss order. Creates a stoploss order.
@@ -59,7 +71,7 @@ class Ftx(Exchange):
'stop price: %s.', pair, stop_price) 'stop price: %s.', pair, stop_price)
return order return order
except ccxt.InsufficientFunds as e: except ccxt.InsufficientFunds as e:
raise DependencyException( raise ExchangeError(
f'Insufficient funds to create {ordertype} sell order on market {pair}. ' f'Insufficient funds to create {ordertype} sell order on market {pair}. '
f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. ' f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. '
f'Message: {e}') from e f'Message: {e}') from e
@@ -68,14 +80,16 @@ class Ftx(Exchange):
f'Could not create {ordertype} sell order on market {pair}. ' f'Could not create {ordertype} sell order on market {pair}. '
f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. ' f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. '
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not place sell order due to {e.__class__.__name__}. Message: {e}') from e f'Could not place sell order due to {e.__class__.__name__}. Message: {e}') from e
except ccxt.BaseError as e: except ccxt.BaseError as e:
raise OperationalException(e) from e raise OperationalException(e) from e
@retrier @retrier(retries=5)
def get_stoploss_order(self, order_id: str, pair: str) -> Dict: def fetch_stoploss_order(self, order_id: str, pair: str) -> Dict:
if self._config['dry_run']: if self._config['dry_run']:
try: try:
order = self._dry_run_open_orders[order_id] order = self._dry_run_open_orders[order_id]
@@ -96,6 +110,8 @@ class Ftx(Exchange):
except ccxt.InvalidOrder as e: except ccxt.InvalidOrder as e:
raise InvalidOrderException( raise InvalidOrderException(
f'Tried to get an invalid order (id: {order_id}). Message: {e}') from e f'Tried to get an invalid order (id: {order_id}). Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not get order due to {e.__class__.__name__}. Message: {e}') from e f'Could not get order due to {e.__class__.__name__}. Message: {e}') from e
@@ -111,6 +127,8 @@ class Ftx(Exchange):
except ccxt.InvalidOrder as e: except ccxt.InvalidOrder as e:
raise InvalidOrderException( raise InvalidOrderException(
f'Could not cancel order. Message: {e}') from e f'Could not cancel order. Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not cancel order due to {e.__class__.__name__}. Message: {e}') from e f'Could not cancel order due to {e.__class__.__name__}. Message: {e}') from e

View File

@@ -1,11 +1,12 @@
""" Kraken exchange subclass """ """ Kraken exchange subclass """
import logging import logging
from typing import Dict from typing import Any, Dict
import ccxt import ccxt
from freqtrade.exceptions import (DependencyException, InvalidOrderException, from freqtrade.exceptions import (DDosProtection, ExchangeError,
OperationalException, TemporaryError) InvalidOrderException, OperationalException,
TemporaryError)
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.exchange.common import retrier from freqtrade.exchange.common import retrier
@@ -21,6 +22,16 @@ class Kraken(Exchange):
"trades_pagination_arg": "since", "trades_pagination_arg": "since",
} }
def market_is_tradable(self, market: Dict[str, Any]) -> bool:
"""
Check if the market symbol is tradable by Freqtrade.
Default checks + check if pair is darkpool pair.
"""
parent_check = super().market_is_tradable(market)
return (parent_check and
market.get('darkpool', False) is False)
@retrier @retrier
def get_balances(self) -> dict: def get_balances(self) -> dict:
if self._config['dry_run']: if self._config['dry_run']:
@@ -45,6 +56,8 @@ class Kraken(Exchange):
balances[bal]['free'] = balances[bal]['total'] - balances[bal]['used'] balances[bal]['free'] = balances[bal]['total'] - balances[bal]['used']
return balances return balances
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not get balance due to {e.__class__.__name__}. Message: {e}') from e f'Could not get balance due to {e.__class__.__name__}. Message: {e}') from e
@@ -58,6 +71,7 @@ class Kraken(Exchange):
""" """
return order['type'] == 'stop-loss' and stop_loss > float(order['price']) return order['type'] == 'stop-loss' and stop_loss > float(order['price'])
@retrier(retries=0)
def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict: def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict:
""" """
Creates a stoploss market order. Creates a stoploss market order.
@@ -84,8 +98,8 @@ class Kraken(Exchange):
'stop price: %s.', pair, stop_price) 'stop price: %s.', pair, stop_price)
return order return order
except ccxt.InsufficientFunds as e: except ccxt.InsufficientFunds as e:
raise DependencyException( raise ExchangeError(
f'Insufficient funds to create {ordertype} sell order on market {pair}.' f'Insufficient funds to create {ordertype} sell order on market {pair}. '
f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. ' f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. '
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.InvalidOrder as e: except ccxt.InvalidOrder as e:
@@ -93,6 +107,8 @@ class Kraken(Exchange):
f'Could not create {ordertype} sell order on market {pair}. ' f'Could not create {ordertype} sell order on market {pair}. '
f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. ' f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. '
f'Message: {e}') from e f'Message: {e}') from e
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e: except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError( raise TemporaryError(
f'Could not place sell order due to {e.__class__.__name__}. Message: {e}') from e f'Could not place sell order due to {e.__class__.__name__}. Message: {e}') from e

View File

@@ -11,16 +11,16 @@ from typing import Any, Dict, List, Optional
import arrow import arrow
from cachetools import TTLCache from cachetools import TTLCache
from requests.exceptions import RequestException
from freqtrade import __version__, constants, persistence from freqtrade import __version__, constants, persistence
from freqtrade.configuration import validate_config_consistency from freqtrade.configuration import validate_config_consistency
from freqtrade.data.converter import order_book_to_dataframe from freqtrade.data.converter import order_book_to_dataframe
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
from freqtrade.edge import Edge from freqtrade.edge import Edge
from freqtrade.exceptions import DependencyException, InvalidOrderException, PricingError from freqtrade.exceptions import (DependencyException, ExchangeError,
InvalidOrderException, PricingError)
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_next_date from freqtrade.exchange import timeframe_to_minutes, timeframe_to_next_date
from freqtrade.misc import safe_value_fallback from freqtrade.misc import safe_value_fallback, safe_value_fallback2
from freqtrade.pairlist.pairlistmanager import PairListManager from freqtrade.pairlist.pairlistmanager import PairListManager
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.resolvers import ExchangeResolver, StrategyResolver
@@ -119,6 +119,8 @@ class FreqtradeBot:
if self.config['cancel_open_orders_on_exit']: if self.config['cancel_open_orders_on_exit']:
self.cancel_all_open_orders() self.cancel_all_open_orders()
self.check_for_open_trades()
self.rpc.cleanup() self.rpc.cleanup()
persistence.cleanup() persistence.cleanup()
@@ -151,6 +153,10 @@ class FreqtradeBot:
self.dataprovider.refresh(self.pairlists.create_pair_list(self.active_pair_whitelist), self.dataprovider.refresh(self.pairlists.create_pair_list(self.active_pair_whitelist),
self.strategy.informative_pairs()) self.strategy.informative_pairs())
strategy_safe_wrapper(self.strategy.bot_loop_start, supress_error=True)()
self.strategy.analyze(self.active_pair_whitelist)
with self._sell_lock: with self._sell_lock:
# Check and handle any timed out open orders # Check and handle any timed out open orders
self.check_handle_timedout() self.check_handle_timedout()
@@ -175,6 +181,24 @@ class FreqtradeBot:
if self.config['cancel_open_orders_on_exit']: if self.config['cancel_open_orders_on_exit']:
self.cancel_all_open_orders() self.cancel_all_open_orders()
def check_for_open_trades(self):
"""
Notify the user when the bot is stopped
and there are still open trades active.
"""
open_trades = Trade.get_trades([Trade.is_open == 1]).all()
if len(open_trades) != 0:
msg = {
'type': RPCMessageType.WARNING_NOTIFICATION,
'status': f"{len(open_trades)} open trades active.\n\n"
f"Handle these trades manually on {self.exchange.name}, "
f"or '/start' the bot again and use '/stopbuy' "
f"to handle open trades gracefully. \n"
f"{'Trades are simulated.' if self.config['dry_run'] else ''}",
}
self.rpc.send_msg(msg)
def _refresh_active_whitelist(self, trades: List[Trade] = []) -> List[str]: def _refresh_active_whitelist(self, trades: List[Trade] = []) -> List[str]:
""" """
Refresh active whitelist from pairlist or edge and extend it with Refresh active whitelist from pairlist or edge and extend it with
@@ -251,7 +275,7 @@ class FreqtradeBot:
rate = self._buy_rate_cache.get(pair) rate = self._buy_rate_cache.get(pair)
# Check if cache has been invalidated # Check if cache has been invalidated
if rate: if rate:
logger.info(f"Using cached buy rate for {pair}.") logger.debug(f"Using cached buy rate for {pair}.")
return rate return rate
bid_strategy = self.config.get('bid_strategy', {}) bid_strategy = self.config.get('bid_strategy', {})
@@ -409,7 +433,9 @@ class FreqtradeBot:
""" """
logger.debug(f"create_trade for pair {pair}") logger.debug(f"create_trade for pair {pair}")
if self.strategy.is_pair_locked(pair): analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(pair, self.strategy.timeframe)
if self.strategy.is_pair_locked(
pair, analyzed_df.iloc[-1]['date'] if len(analyzed_df) > 0 else None):
logger.info(f"Pair {pair} is currently locked.") logger.info(f"Pair {pair} is currently locked.")
return False return False
@@ -420,9 +446,7 @@ class FreqtradeBot:
return False return False
# running get_signal on historical data fetched # running get_signal on historical data fetched
(buy, sell) = self.strategy.get_signal( (buy, sell) = self.strategy.get_signal(pair, self.strategy.timeframe, analyzed_df)
pair, self.strategy.timeframe,
self.dataprovider.ohlcv(pair, self.strategy.timeframe))
if buy and not sell: if buy and not sell:
stake_amount = self.get_trade_stake_amount(pair) stake_amount = self.get_trade_stake_amount(pair)
@@ -495,6 +519,12 @@ class FreqtradeBot:
amount = stake_amount / buy_limit_requested amount = stake_amount / buy_limit_requested
order_type = self.strategy.order_types['buy'] order_type = self.strategy.order_types['buy']
if not strategy_safe_wrapper(self.strategy.confirm_trade_entry, default_retval=True)(
pair=pair, order_type=order_type, amount=amount, rate=buy_limit_requested,
time_in_force=time_in_force):
logger.info(f"User requested abortion of buying {pair}")
return False
amount = self.exchange.amount_to_precision(pair, amount)
order = self.exchange.buy(pair=pair, ordertype=order_type, order = self.exchange.buy(pair=pair, ordertype=order_type,
amount=amount, rate=buy_limit_requested, amount=amount, rate=buy_limit_requested,
time_in_force=time_in_force) time_in_force=time_in_force)
@@ -503,6 +533,7 @@ class FreqtradeBot:
# we assume the order is executed at the price requested # we assume the order is executed at the price requested
buy_limit_filled_price = buy_limit_requested buy_limit_filled_price = buy_limit_requested
amount_requested = amount
if order_status == 'expired' or order_status == 'rejected': if order_status == 'expired' or order_status == 'rejected':
order_tif = self.strategy.order_time_in_force['buy'] order_tif = self.strategy.order_time_in_force['buy']
@@ -523,15 +554,15 @@ class FreqtradeBot:
order['filled'], order['amount'], order['remaining'] order['filled'], order['amount'], order['remaining']
) )
stake_amount = order['cost'] stake_amount = order['cost']
amount = order['amount'] amount = safe_value_fallback(order, 'filled', 'amount')
buy_limit_filled_price = order['price'] buy_limit_filled_price = safe_value_fallback(order, 'average', 'price')
order_id = None order_id = None
# in case of FOK the order may be filled immediately and fully # in case of FOK the order may be filled immediately and fully
elif order_status == 'closed': elif order_status == 'closed':
stake_amount = order['cost'] stake_amount = order['cost']
amount = order['amount'] amount = safe_value_fallback(order, 'filled', 'amount')
buy_limit_filled_price = order['price'] buy_limit_filled_price = safe_value_fallback(order, 'average', 'price')
# Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL # Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL
fee = self.exchange.get_fee(symbol=pair, taker_or_maker='maker') fee = self.exchange.get_fee(symbol=pair, taker_or_maker='maker')
@@ -539,6 +570,7 @@ class FreqtradeBot:
pair=pair, pair=pair,
stake_amount=stake_amount, stake_amount=stake_amount,
amount=amount, amount=amount,
amount_requested=amount_requested,
fee_open=fee, fee_open=fee,
fee_close=fee, fee_close=fee,
open_rate=buy_limit_filled_price, open_rate=buy_limit_filled_price,
@@ -569,6 +601,7 @@ class FreqtradeBot:
Sends rpc notification when a buy occured. Sends rpc notification when a buy occured.
""" """
msg = { msg = {
'trade_id': trade.id,
'type': RPCMessageType.BUY_NOTIFICATION, 'type': RPCMessageType.BUY_NOTIFICATION,
'exchange': self.exchange.name.capitalize(), 'exchange': self.exchange.name.capitalize(),
'pair': trade.pair, 'pair': trade.pair,
@@ -592,6 +625,7 @@ class FreqtradeBot:
current_rate = self.get_buy_rate(trade.pair, False) current_rate = self.get_buy_rate(trade.pair, False)
msg = { msg = {
'trade_id': trade.id,
'type': RPCMessageType.BUY_CANCEL_NOTIFICATION, 'type': RPCMessageType.BUY_CANCEL_NOTIFICATION,
'exchange': self.exchange.name.capitalize(), 'exchange': self.exchange.name.capitalize(),
'pair': trade.pair, 'pair': trade.pair,
@@ -629,7 +663,7 @@ class FreqtradeBot:
trades_closed += 1 trades_closed += 1
except DependencyException as exception: except DependencyException as exception:
logger.warning('Unable to sell trade: %s', exception) logger.warning('Unable to sell trade %s: %s', trade.pair, exception)
# Updating wallets if any trade occured # Updating wallets if any trade occured
if trades_closed: if trades_closed:
@@ -660,7 +694,7 @@ class FreqtradeBot:
rate = self._sell_rate_cache.get(pair) rate = self._sell_rate_cache.get(pair)
# Check if cache has been invalidated # Check if cache has been invalidated
if rate: if rate:
logger.info(f"Using cached sell rate for {pair}.") logger.debug(f"Using cached sell rate for {pair}.")
return rate return rate
ask_strategy = self.config.get('ask_strategy', {}) ask_strategy = self.config.get('ask_strategy', {})
@@ -697,9 +731,10 @@ class FreqtradeBot:
if (config_ask_strategy.get('use_sell_signal', True) or if (config_ask_strategy.get('use_sell_signal', True) or
config_ask_strategy.get('ignore_roi_if_buy_signal', False)): config_ask_strategy.get('ignore_roi_if_buy_signal', False)):
(buy, sell) = self.strategy.get_signal( analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(trade.pair,
trade.pair, self.strategy.timeframe, self.strategy.timeframe)
self.dataprovider.ohlcv(trade.pair, self.strategy.timeframe))
(buy, sell) = self.strategy.get_signal(trade.pair, self.strategy.timeframe, analyzed_df)
if config_ask_strategy.get('use_order_book', False): if config_ask_strategy.get('use_order_book', False):
order_book_min = config_ask_strategy.get('order_book_min', 1) order_book_min = config_ask_strategy.get('order_book_min', 1)
@@ -736,7 +771,7 @@ class FreqtradeBot:
logger.debug('Found no sell signal for %s.', trade) logger.debug('Found no sell signal for %s.', trade)
return False return False
def create_stoploss_order(self, trade: Trade, stop_price: float, rate: float) -> bool: def create_stoploss_order(self, trade: Trade, stop_price: float) -> bool:
""" """
Abstracts creating stoploss orders from the logic. Abstracts creating stoploss orders from the logic.
Handles errors and updates the trade database object. Handles errors and updates the trade database object.
@@ -755,7 +790,7 @@ class FreqtradeBot:
logger.warning('Selling the trade forcefully') logger.warning('Selling the trade forcefully')
self.execute_sell(trade, trade.stop_loss, sell_reason=SellType.EMERGENCY_SELL) self.execute_sell(trade, trade.stop_loss, sell_reason=SellType.EMERGENCY_SELL)
except DependencyException: except ExchangeError:
trade.stoploss_order_id = None trade.stoploss_order_id = None
logger.exception('Unable to place a stoploss order on exchange.') logger.exception('Unable to place a stoploss order on exchange.')
return False return False
@@ -773,8 +808,8 @@ class FreqtradeBot:
try: try:
# First we check if there is already a stoploss on exchange # First we check if there is already a stoploss on exchange
stoploss_order = self.exchange.get_stoploss_order(trade.stoploss_order_id, trade.pair) \ stoploss_order = self.exchange.fetch_stoploss_order(
if trade.stoploss_order_id else None trade.stoploss_order_id, trade.pair) if trade.stoploss_order_id else None
except InvalidOrderException as exception: except InvalidOrderException as exception:
logger.warning('Unable to fetch stoploss order: %s', exception) logger.warning('Unable to fetch stoploss order: %s', exception)
@@ -795,20 +830,17 @@ class FreqtradeBot:
return False return False
# If buy order is fulfilled but there is no stoploss, we add a stoploss on exchange # If buy order is fulfilled but there is no stoploss, we add a stoploss on exchange
if (not stoploss_order): if not stoploss_order:
stoploss = self.edge.stoploss(pair=trade.pair) if self.edge else self.strategy.stoploss stoploss = self.edge.stoploss(pair=trade.pair) if self.edge else self.strategy.stoploss
stop_price = trade.open_rate * (1 + stoploss) stop_price = trade.open_rate * (1 + stoploss)
if self.create_stoploss_order(trade=trade, stop_price=stop_price, rate=stop_price): if self.create_stoploss_order(trade=trade, stop_price=stop_price):
trade.stoploss_last_update = datetime.now() trade.stoploss_last_update = datetime.now()
return False return False
# If stoploss order is canceled for some reason we add it # If stoploss order is canceled for some reason we add it
if stoploss_order and stoploss_order['status'] in ('canceled', 'cancelled'): if stoploss_order and stoploss_order['status'] in ('canceled', 'cancelled'):
if self.create_stoploss_order(trade=trade, stop_price=trade.stop_loss, if self.create_stoploss_order(trade=trade, stop_price=trade.stop_loss):
rate=trade.stop_loss):
return False return False
else: else:
trade.stoploss_order_id = None trade.stoploss_order_id = None
@@ -845,8 +877,7 @@ class FreqtradeBot:
f"for pair {trade.pair}") f"for pair {trade.pair}")
# Create new stoploss order # Create new stoploss order
if not self.create_stoploss_order(trade=trade, stop_price=trade.stop_loss, if not self.create_stoploss_order(trade=trade, stop_price=trade.stop_loss):
rate=trade.stop_loss):
logger.warning(f"Could not create trailing stoploss order " logger.warning(f"Could not create trailing stoploss order "
f"for pair {trade.pair}.") f"for pair {trade.pair}.")
@@ -890,8 +921,8 @@ class FreqtradeBot:
try: try:
if not trade.open_order_id: if not trade.open_order_id:
continue continue
order = self.exchange.get_order(trade.open_order_id, trade.pair) order = self.exchange.fetch_order(trade.open_order_id, trade.pair)
except (RequestException, DependencyException, InvalidOrderException): except (ExchangeError):
logger.info('Cannot query order for %s due to %s', trade, traceback.format_exc()) logger.info('Cannot query order for %s due to %s', trade, traceback.format_exc())
continue continue
@@ -923,8 +954,8 @@ class FreqtradeBot:
for trade in Trade.get_open_order_trades(): for trade in Trade.get_open_order_trades():
try: try:
order = self.exchange.get_order(trade.open_order_id, trade.pair) order = self.exchange.fetch_order(trade.open_order_id, trade.pair)
except (DependencyException, InvalidOrderException): except (ExchangeError):
logger.info('Cannot query order for %s due to %s', trade, traceback.format_exc()) logger.info('Cannot query order for %s due to %s', trade, traceback.format_exc())
continue continue
@@ -946,6 +977,12 @@ class FreqtradeBot:
reason = constants.CANCEL_REASON['TIMEOUT'] reason = constants.CANCEL_REASON['TIMEOUT']
corder = self.exchange.cancel_order_with_result(trade.open_order_id, trade.pair, corder = self.exchange.cancel_order_with_result(trade.open_order_id, trade.pair,
trade.amount) trade.amount)
# Avoid race condition where the order could not be cancelled coz its already filled.
# Simply bailing here is the only safe way - as this order will then be
# handled in the next iteration.
if corder.get('status') not in ('canceled', 'closed'):
logger.warning(f"Order {trade.open_order_id} for {trade.pair} not cancelled.")
return False
else: else:
# Order was cancelled already, so we can reuse the existing dict # Order was cancelled already, so we can reuse the existing dict
corder = order corder = order
@@ -954,7 +991,7 @@ class FreqtradeBot:
logger.info('Buy order %s for %s.', reason, trade) logger.info('Buy order %s for %s.', reason, trade)
# Using filled to determine the filled amount # Using filled to determine the filled amount
filled_amount = safe_value_fallback(corder, order, 'filled', 'filled') filled_amount = safe_value_fallback2(corder, order, 'filled', 'filled')
if isclose(filled_amount, 0.0, abs_tol=constants.MATH_CLOSE_PREC): if isclose(filled_amount, 0.0, abs_tol=constants.MATH_CLOSE_PREC):
logger.info('Buy order fully cancelled. Removing %s from database.', trade) logger.info('Buy order fully cancelled. Removing %s from database.', trade)
@@ -1077,12 +1114,20 @@ class FreqtradeBot:
order_type = self.strategy.order_types.get("emergencysell", "market") order_type = self.strategy.order_types.get("emergencysell", "market")
amount = self._safe_sell_amount(trade.pair, trade.amount) amount = self._safe_sell_amount(trade.pair, trade.amount)
time_in_force = self.strategy.order_time_in_force['sell']
if not strategy_safe_wrapper(self.strategy.confirm_trade_exit, default_retval=True)(
pair=trade.pair, trade=trade, order_type=order_type, amount=amount, rate=limit,
time_in_force=time_in_force,
sell_reason=sell_reason.value):
logger.info(f"User requested abortion of selling {trade.pair}")
return False
# Execute sell and update trade record # Execute sell and update trade record
order = self.exchange.sell(pair=str(trade.pair), order = self.exchange.sell(pair=str(trade.pair),
ordertype=order_type, ordertype=order_type,
amount=amount, rate=limit, amount=amount, rate=limit,
time_in_force=self.strategy.order_time_in_force['sell'] time_in_force=time_in_force
) )
trade.open_order_id = order['id'] trade.open_order_id = order['id']
@@ -1113,6 +1158,7 @@ class FreqtradeBot:
msg = { msg = {
'type': RPCMessageType.SELL_NOTIFICATION, 'type': RPCMessageType.SELL_NOTIFICATION,
'trade_id': trade.id,
'exchange': trade.exchange.capitalize(), 'exchange': trade.exchange.capitalize(),
'pair': trade.pair, 'pair': trade.pair,
'gain': gain, 'gain': gain,
@@ -1155,6 +1201,7 @@ class FreqtradeBot:
msg = { msg = {
'type': RPCMessageType.SELL_CANCEL_NOTIFICATION, 'type': RPCMessageType.SELL_CANCEL_NOTIFICATION,
'trade_id': trade.id,
'exchange': trade.exchange.capitalize(), 'exchange': trade.exchange.capitalize(),
'pair': trade.pair, 'pair': trade.pair,
'gain': gain, 'gain': gain,
@@ -1202,14 +1249,15 @@ class FreqtradeBot:
# Update trade with order values # Update trade with order values
logger.info('Found open order for %s', trade) logger.info('Found open order for %s', trade)
try: try:
order = action_order or self.exchange.get_order(order_id, trade.pair) order = action_order or self.exchange.fetch_order(order_id, trade.pair)
except InvalidOrderException as exception: except InvalidOrderException as exception:
logger.warning('Unable to fetch order %s: %s', order_id, exception) logger.warning('Unable to fetch order %s: %s', order_id, exception)
return False return False
# Try update amount (binance-fix) # Try update amount (binance-fix)
try: try:
new_amount = self.get_real_amount(trade, order, order_amount) new_amount = self.get_real_amount(trade, order, order_amount)
if not isclose(order['amount'], new_amount, abs_tol=constants.MATH_CLOSE_PREC): if not isclose(safe_value_fallback(order, 'filled', 'amount'), new_amount,
abs_tol=constants.MATH_CLOSE_PREC):
order['amount'] = new_amount order['amount'] = new_amount
order.pop('filled', None) order.pop('filled', None)
trade.recalc_open_trade_price() trade.recalc_open_trade_price()
@@ -1255,7 +1303,7 @@ class FreqtradeBot:
""" """
# Init variables # Init variables
if order_amount is None: if order_amount is None:
order_amount = order['amount'] order_amount = safe_value_fallback(order, 'filled', 'amount')
# Only run for closed orders # Only run for closed orders
if trade.fee_updated(order.get('side', '')) or order['status'] == 'open': if trade.fee_updated(order.get('side', '')) or order['status'] == 'open':
return order_amount return order_amount

View File

@@ -1,14 +1,18 @@
import logging import logging
import sys import sys
from logging import Formatter from logging import Formatter
from logging.handlers import RotatingFileHandler, SysLogHandler from logging.handlers import (BufferingHandler, RotatingFileHandler,
from typing import Any, Dict, List SysLogHandler)
from typing import Any, Dict
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
LOGFORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
# Initialize bufferhandler - will be used for /log endpoints
bufferHandler = BufferingHandler(1000)
bufferHandler.setFormatter(Formatter(LOGFORMAT))
def _set_loggers(verbosity: int = 0, api_verbosity: str = 'info') -> None: def _set_loggers(verbosity: int = 0, api_verbosity: str = 'info') -> None:
@@ -33,17 +37,31 @@ def _set_loggers(verbosity: int = 0, api_verbosity: str = 'info') -> None:
) )
def setup_logging_pre() -> None:
"""
Early setup for logging.
Uses INFO loglevel and only the Streamhandler.
Early messages (before proper logging setup) will therefore only be sent to additional
logging handlers after the real initialization, because we don't know which
ones the user desires beforehand.
"""
logging.basicConfig(
level=logging.INFO,
format=LOGFORMAT,
handlers=[logging.StreamHandler(sys.stderr), bufferHandler]
)
def setup_logging(config: Dict[str, Any]) -> None: def setup_logging(config: Dict[str, Any]) -> None:
""" """
Process -v/--verbose, --logfile options Process -v/--verbose, --logfile options
""" """
# Log level # Log level
verbosity = config['verbosity'] verbosity = config['verbosity']
logging.root.addHandler(bufferHandler)
# Log to stderr
log_handlers: List[logging.Handler] = [logging.StreamHandler(sys.stderr)]
logfile = config.get('logfile') logfile = config.get('logfile')
if logfile: if logfile:
s = logfile.split(':') s = logfile.split(':')
if s[0] == 'syslog': if s[0] == 'syslog':
@@ -58,28 +76,27 @@ def setup_logging(config: Dict[str, Any]) -> None:
# to perform reduction of repeating messages if this is set in the # to perform reduction of repeating messages if this is set in the
# syslog config. The messages should be equal for this. # syslog config. The messages should be equal for this.
handler.setFormatter(Formatter('%(name)s - %(levelname)s - %(message)s')) handler.setFormatter(Formatter('%(name)s - %(levelname)s - %(message)s'))
log_handlers.append(handler) logging.root.addHandler(handler)
elif s[0] == 'journald': elif s[0] == 'journald':
try: try:
from systemd.journal import JournaldLogHandler from systemd.journal import JournaldLogHandler
except ImportError: except ImportError:
raise OperationalException("You need the systemd python package be installed in " raise OperationalException("You need the systemd python package be installed in "
"order to use logging to journald.") "order to use logging to journald.")
handler = JournaldLogHandler() handler_jd = JournaldLogHandler()
# No datetime field for logging into journald, to allow syslog # No datetime field for logging into journald, to allow syslog
# to perform reduction of repeating messages if this is set in the # to perform reduction of repeating messages if this is set in the
# syslog config. The messages should be equal for this. # syslog config. The messages should be equal for this.
handler.setFormatter(Formatter('%(name)s - %(levelname)s - %(message)s')) handler_jd.setFormatter(Formatter('%(name)s - %(levelname)s - %(message)s'))
log_handlers.append(handler) logging.root.addHandler(handler_jd)
else: else:
log_handlers.append(RotatingFileHandler(logfile, handler_rf = RotatingFileHandler(logfile,
maxBytes=1024 * 1024, # 1Mb maxBytes=1024 * 1024 * 10, # 10Mb
backupCount=10)) backupCount=10)
handler_rf.setFormatter(Formatter(LOGFORMAT))
logging.root.addHandler(handler_rf)
logging.basicConfig( logging.root.setLevel(logging.INFO if verbosity < 1 else logging.DEBUG)
level=logging.INFO if verbosity < 1 else logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=log_handlers
)
_set_loggers(verbosity, config.get('api_server', {}).get('verbosity', 'info')) _set_loggers(verbosity, config.get('api_server', {}).get('verbosity', 'info'))
logger.info('Verbosity set to %s', verbosity) logger.info('Verbosity set to %s', verbosity)

View File

@@ -3,18 +3,17 @@
Main Freqtrade bot script. Main Freqtrade bot script.
Read the documentation to know what cli arguments you need. Read the documentation to know what cli arguments you need.
""" """
import logging
from freqtrade.exceptions import FreqtradeException, OperationalException
import sys import sys
from typing import Any, List
# check min. python version # check min. python version
if sys.version_info < (3, 6): if sys.version_info < (3, 6):
sys.exit("Freqtrade requires Python version >= 3.6") sys.exit("Freqtrade requires Python version >= 3.6")
# flake8: noqa E402
import logging
from typing import Any, List
from freqtrade.commands import Arguments from freqtrade.commands import Arguments
from freqtrade.exceptions import FreqtradeException, OperationalException
from freqtrade.loggers import setup_logging_pre
logger = logging.getLogger('freqtrade') logger = logging.getLogger('freqtrade')
@@ -28,6 +27,7 @@ def main(sysargv: List[str] = None) -> None:
return_code: Any = 1 return_code: Any = 1
try: try:
setup_logging_pre()
arguments = Arguments(sysargv) arguments = Arguments(sysargv)
args = arguments.get_parsed_arg() args = arguments.get_parsed_arg()

View File

@@ -134,7 +134,21 @@ def round_dict(d, n):
return {k: (round(v, n) if isinstance(v, float) else v) for k, v in d.items()} return {k: (round(v, n) if isinstance(v, float) else v) for k, v in d.items()}
def safe_value_fallback(dict1: dict, dict2: dict, key1: str, key2: str, default_value=None): def safe_value_fallback(obj: dict, key1: str, key2: str, default_value=None):
"""
Search a value in obj, return this if it's not None.
Then search key2 in obj - return that if it's not none - then use default_value.
Else falls back to None.
"""
if key1 in obj and obj[key1] is not None:
return obj[key1]
else:
if key2 in obj and obj[key2] is not None:
return obj[key2]
return default_value
def safe_value_fallback2(dict1: dict, dict2: dict, key1: str, key2: str, default_value=None):
""" """
Search a value in dict1, return this if it's not 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. Fall back to dict2 - return key2 from dict2 if it's not None.

View File

@@ -13,6 +13,7 @@ from pandas import DataFrame
from freqtrade.configuration import (TimeRange, remove_credentials, from freqtrade.configuration import (TimeRange, remove_credentials,
validate_config_consistency) validate_config_consistency)
from freqtrade.constants import DATETIME_PRINT_FORMAT
from freqtrade.data import history from freqtrade.data import history
from freqtrade.data.converter import trim_dataframe from freqtrade.data.converter import trim_dataframe
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
@@ -20,11 +21,10 @@ from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
from freqtrade.optimize.optimize_reports import (generate_backtest_stats, from freqtrade.optimize.optimize_reports import (generate_backtest_stats,
show_backtest_results, show_backtest_results,
store_backtest_result) store_backtest_stats)
from freqtrade.pairlist.pairlistmanager import PairListManager from freqtrade.pairlist.pairlistmanager import PairListManager
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.resolvers import ExchangeResolver, StrategyResolver
from freqtrade.state import RunMode
from freqtrade.strategy.interface import IStrategy, SellCheckTuple, SellType from freqtrade.strategy.interface import IStrategy, SellCheckTuple, SellType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -37,14 +37,15 @@ class BacktestResult(NamedTuple):
pair: str pair: str
profit_percent: float profit_percent: float
profit_abs: float profit_abs: float
open_time: datetime open_date: datetime
close_time: datetime open_rate: float
open_index: int open_fee: float
close_index: int close_date: datetime
close_rate: float
close_fee: float
amount: float
trade_duration: float trade_duration: float
open_at_end: bool open_at_end: bool
open_rate: float
close_rate: float
sell_reason: SellType sell_reason: SellType
@@ -65,9 +66,8 @@ class Backtesting:
self.strategylist: List[IStrategy] = [] self.strategylist: List[IStrategy] = []
self.exchange = ExchangeResolver.load_exchange(self.config['exchange']['name'], self.config) self.exchange = ExchangeResolver.load_exchange(self.config['exchange']['name'], self.config)
if self.config.get('runmode') != RunMode.HYPEROPT: dataprovider = DataProvider(self.config, self.exchange)
self.dataprovider = DataProvider(self.config, self.exchange) IStrategy.dp = dataprovider
IStrategy.dp = self.dataprovider
if self.config.get('strategy_list', None): if self.config.get('strategy_list', None):
for strat in list(self.config['strategy_list']): for strat in list(self.config['strategy_list']):
@@ -101,7 +101,7 @@ class Backtesting:
if len(self.pairlists.whitelist) == 0: if len(self.pairlists.whitelist) == 0:
raise OperationalException("No pair in whitelist.") raise OperationalException("No pair in whitelist.")
if config.get('fee'): if config.get('fee', None) is not None:
self.fee = config['fee'] self.fee = config['fee']
else: else:
self.fee = self.exchange.get_fee(symbol=self.pairlists.whitelist[0]) self.fee = self.exchange.get_fee(symbol=self.pairlists.whitelist[0])
@@ -137,10 +137,10 @@ class Backtesting:
min_date, max_date = history.get_timerange(data) min_date, max_date = history.get_timerange(data)
logger.info( logger.info(f'Loading data from {min_date.strftime(DATETIME_PRINT_FORMAT)} '
'Loading data from %s up to %s (%s days)..', f'up to {max_date.strftime(DATETIME_PRINT_FORMAT)} '
min_date.isoformat(), max_date.isoformat(), (max_date - min_date).days f'({(max_date - min_date).days} days)..')
)
# Adjust startts forward if not enough data is available # Adjust startts forward if not enough data is available
timerange.adjust_start_if_necessary(timeframe_to_seconds(self.timeframe), timerange.adjust_start_if_necessary(timeframe_to_seconds(self.timeframe),
self.required_startup, min_date) self.required_startup, min_date)
@@ -225,7 +225,7 @@ class Backtesting:
open_rate=buy_row.open, open_rate=buy_row.open,
open_date=buy_row.date, open_date=buy_row.date,
stake_amount=stake_amount, stake_amount=stake_amount,
amount=stake_amount / buy_row.open, amount=round(stake_amount / buy_row.open, 8),
fee_open=self.fee, fee_open=self.fee,
fee_close=self.fee, fee_close=self.fee,
is_open=True, is_open=True,
@@ -246,14 +246,15 @@ class Backtesting:
return BacktestResult(pair=pair, return BacktestResult(pair=pair,
profit_percent=trade.calc_profit_ratio(rate=closerate), profit_percent=trade.calc_profit_ratio(rate=closerate),
profit_abs=trade.calc_profit(rate=closerate), profit_abs=trade.calc_profit(rate=closerate),
open_time=buy_row.date, open_date=buy_row.date,
close_time=sell_row.date,
trade_duration=trade_dur,
open_index=buy_row.Index,
close_index=sell_row.Index,
open_at_end=False,
open_rate=buy_row.open, open_rate=buy_row.open,
open_fee=self.fee,
close_date=sell_row.date,
close_rate=closerate, close_rate=closerate,
close_fee=self.fee,
amount=trade.amount,
trade_duration=trade_dur,
open_at_end=False,
sell_reason=sell.sell_type sell_reason=sell.sell_type
) )
if partial_ohlcv: if partial_ohlcv:
@@ -262,15 +263,16 @@ class Backtesting:
bt_res = BacktestResult(pair=pair, bt_res = BacktestResult(pair=pair,
profit_percent=trade.calc_profit_ratio(rate=sell_row.open), profit_percent=trade.calc_profit_ratio(rate=sell_row.open),
profit_abs=trade.calc_profit(rate=sell_row.open), profit_abs=trade.calc_profit(rate=sell_row.open),
open_time=buy_row.date, open_date=buy_row.date,
close_time=sell_row.date, open_rate=buy_row.open,
open_fee=self.fee,
close_date=sell_row.date,
close_rate=sell_row.open,
close_fee=self.fee,
amount=trade.amount,
trade_duration=int(( trade_duration=int((
sell_row.date - buy_row.date).total_seconds() // 60), sell_row.date - buy_row.date).total_seconds() // 60),
open_index=buy_row.Index,
close_index=sell_row.Index,
open_at_end=True, open_at_end=True,
open_rate=buy_row.open,
close_rate=sell_row.open,
sell_reason=SellType.FORCE_SELL sell_reason=SellType.FORCE_SELL
) )
logger.debug(f"{pair} - Force selling still open trade, " logger.debug(f"{pair} - Force selling still open trade, "
@@ -356,8 +358,8 @@ class Backtesting:
if trade_entry: if trade_entry:
logger.debug(f"{pair} - Locking pair till " logger.debug(f"{pair} - Locking pair till "
f"close_time={trade_entry.close_time}") f"close_date={trade_entry.close_date}")
lock_pair_until[pair] = trade_entry.close_time lock_pair_until[pair] = trade_entry.close_date
trades.append(trade_entry) trades.append(trade_entry)
else: else:
# Set lock_pair_until to end of testing period if trade could not be closed # Set lock_pair_until to end of testing period if trade could not be closed
@@ -400,10 +402,9 @@ class Backtesting:
preprocessed[pair] = trim_dataframe(df, timerange) preprocessed[pair] = trim_dataframe(df, timerange)
min_date, max_date = history.get_timerange(preprocessed) min_date, max_date = history.get_timerange(preprocessed)
logger.info( logger.info(f'Backtesting with data from {min_date.strftime(DATETIME_PRINT_FORMAT)} '
'Backtesting with data from %s up to %s (%s days)..', f'up to {max_date.strftime(DATETIME_PRINT_FORMAT)} '
min_date.isoformat(), max_date.isoformat(), (max_date - min_date).days f'({(max_date - min_date).days} days)..')
)
# Execute backtest and print results # Execute backtest and print results
all_results[self.strategy.get_strategy_name()] = self.backtest( all_results[self.strategy.get_strategy_name()] = self.backtest(
processed=preprocessed, processed=preprocessed,
@@ -414,8 +415,10 @@ class Backtesting:
position_stacking=position_stacking, position_stacking=position_stacking,
) )
stats = generate_backtest_stats(self.config, data, all_results,
min_date=min_date, max_date=max_date)
if self.config.get('export', False): if self.config.get('export', False):
store_backtest_result(self.config['exportfilename'], all_results) store_backtest_stats(self.config['exportfilename'], stats)
# Show backtest results # Show backtest results
stats = generate_backtest_stats(self.config, data, all_results)
show_backtest_results(self.config, stats) show_backtest_results(self.config, stats)

View File

@@ -4,27 +4,28 @@
This module contains the hyperopt logic This module contains the hyperopt logic
""" """
import io
import locale import locale
import logging import logging
import random import random
import warnings import warnings
from math import ceil
from collections import OrderedDict from collections import OrderedDict
from math import ceil
from operator import itemgetter from operator import itemgetter
from pathlib import Path from pathlib import Path
from pprint import pformat from pprint import pformat
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
import progressbar
import rapidjson import rapidjson
import tabulate
from colorama import Fore, Style from colorama import Fore, Style
from colorama import init as colorama_init
from joblib import (Parallel, cpu_count, delayed, dump, load, from joblib import (Parallel, cpu_count, delayed, dump, load,
wrap_non_picklable_objects) wrap_non_picklable_objects)
from pandas import DataFrame, json_normalize, isna from pandas import DataFrame, isna, json_normalize
import progressbar
import tabulate
from os import path
import io
from freqtrade.constants import DATETIME_PRINT_FORMAT
from freqtrade.data.converter import trim_dataframe from freqtrade.data.converter import trim_dataframe
from freqtrade.data.history import get_timerange from freqtrade.data.history import get_timerange
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
@@ -32,9 +33,11 @@ from freqtrade.misc import plural, round_dict
from freqtrade.optimize.backtesting import Backtesting from freqtrade.optimize.backtesting import Backtesting
# Import IHyperOpt and IHyperOptLoss to allow unpickling classes from these modules # Import IHyperOpt and IHyperOptLoss to allow unpickling classes from these modules
from freqtrade.optimize.hyperopt_interface import IHyperOpt # noqa: F401 from freqtrade.optimize.hyperopt_interface import IHyperOpt # noqa: F401
from freqtrade.optimize.hyperopt_loss_interface import IHyperOptLoss # noqa: F401 from freqtrade.optimize.hyperopt_loss_interface import \
IHyperOptLoss # noqa: F401
from freqtrade.resolvers.hyperopt_resolver import (HyperOptLossResolver, from freqtrade.resolvers.hyperopt_resolver import (HyperOptLossResolver,
HyperOptResolver) HyperOptResolver)
from freqtrade.strategy import IStrategy
# Suppress scikit-learn FutureWarnings from skopt # Suppress scikit-learn FutureWarnings from skopt
with warnings.catch_warnings(): with warnings.catch_warnings():
@@ -312,11 +315,16 @@ class Hyperopt:
trials = json_normalize(results, max_level=1) trials = json_normalize(results, max_level=1)
trials['Best'] = '' trials['Best'] = ''
if 'results_metrics.winsdrawslosses' not in trials.columns:
# Ensure compatibility with older versions of hyperopt results
trials['results_metrics.winsdrawslosses'] = 'N/A'
trials = trials[['Best', 'current_epoch', 'results_metrics.trade_count', trials = trials[['Best', 'current_epoch', 'results_metrics.trade_count',
'results_metrics.winsdrawslosses',
'results_metrics.avg_profit', 'results_metrics.total_profit', 'results_metrics.avg_profit', 'results_metrics.total_profit',
'results_metrics.profit', 'results_metrics.duration', 'results_metrics.profit', 'results_metrics.duration',
'loss', 'is_initial_point', 'is_best']] 'loss', 'is_initial_point', 'is_best']]
trials.columns = ['Best', 'Epoch', 'Trades', 'Avg profit', 'Total profit', trials.columns = ['Best', 'Epoch', 'Trades', 'W/D/L', 'Avg profit', 'Total profit',
'Profit', 'Avg duration', 'Objective', 'is_initial_point', 'is_best'] 'Profit', 'Avg duration', 'Objective', 'is_initial_point', 'is_best']
trials['is_profit'] = False trials['is_profit'] = False
trials.loc[trials['is_initial_point'], 'Best'] = '* ' trials.loc[trials['is_initial_point'], 'Best'] = '* '
@@ -390,7 +398,7 @@ class Hyperopt:
return return
# Verification for overwrite # Verification for overwrite
if path.isfile(csv_file): if Path(csv_file).is_file():
logger.error(f"CSV file already exists: {csv_file}") logger.error(f"CSV file already exists: {csv_file}")
return return
@@ -558,9 +566,17 @@ class Hyperopt:
} }
def _calculate_results_metrics(self, backtesting_results: DataFrame) -> Dict: def _calculate_results_metrics(self, backtesting_results: DataFrame) -> Dict:
wins = len(backtesting_results[backtesting_results.profit_percent > 0])
draws = len(backtesting_results[backtesting_results.profit_percent == 0])
losses = len(backtesting_results[backtesting_results.profit_percent < 0])
return { return {
'trade_count': len(backtesting_results.index), 'trade_count': len(backtesting_results.index),
'wins': wins,
'draws': draws,
'losses': losses,
'winsdrawslosses': f"{wins}/{draws}/{losses}",
'avg_profit': backtesting_results.profit_percent.mean() * 100.0, 'avg_profit': backtesting_results.profit_percent.mean() * 100.0,
'median_profit': backtesting_results.profit_percent.median() * 100.0,
'total_profit': backtesting_results.profit_abs.sum(), 'total_profit': backtesting_results.profit_abs.sum(),
'profit': backtesting_results.profit_percent.sum() * 100.0, 'profit': backtesting_results.profit_percent.sum() * 100.0,
'duration': backtesting_results.trade_duration.mean(), 'duration': backtesting_results.trade_duration.mean(),
@@ -572,7 +588,10 @@ class Hyperopt:
""" """
stake_cur = self.config['stake_currency'] stake_cur = self.config['stake_currency']
return (f"{results_metrics['trade_count']:6d} trades. " return (f"{results_metrics['trade_count']:6d} trades. "
f"{results_metrics['wins']}/{results_metrics['draws']}"
f"/{results_metrics['losses']} Wins/Draws/Losses. "
f"Avg profit {results_metrics['avg_profit']: 6.2f}%. " f"Avg profit {results_metrics['avg_profit']: 6.2f}%. "
f"Median profit {results_metrics['median_profit']: 6.2f}%. "
f"Total profit {results_metrics['total_profit']: 11.8f} {stake_cur} " f"Total profit {results_metrics['total_profit']: 11.8f} {stake_cur} "
f"({results_metrics['profit']: 7.2f}\N{GREEK CAPITAL LETTER SIGMA}%). " f"({results_metrics['profit']: 7.2f}\N{GREEK CAPITAL LETTER SIGMA}%). "
f"Avg duration {results_metrics['duration']:5.1f} min." f"Avg duration {results_metrics['duration']:5.1f} min."
@@ -625,15 +644,17 @@ class Hyperopt:
preprocessed[pair] = trim_dataframe(df, timerange) preprocessed[pair] = trim_dataframe(df, timerange)
min_date, max_date = get_timerange(data) min_date, max_date = get_timerange(data)
logger.info( logger.info(f'Hyperopting with data from {min_date.strftime(DATETIME_PRINT_FORMAT)} '
'Hyperopting with data from %s up to %s (%s days)..', f'up to {max_date.strftime(DATETIME_PRINT_FORMAT)} '
min_date.isoformat(), max_date.isoformat(), (max_date - min_date).days f'({(max_date - min_date).days} days)..')
)
dump(preprocessed, self.data_pickle_file) dump(preprocessed, self.data_pickle_file)
# We don't need exchange instance anymore while running hyperopt # We don't need exchange instance anymore while running hyperopt
self.backtesting.exchange = None # type: ignore self.backtesting.exchange = None # type: ignore
self.backtesting.pairlists = None # type: ignore self.backtesting.pairlists = None # type: ignore
self.backtesting.strategy.dp = None # type: ignore
IStrategy.dp = None # type: ignore
self.epochs = self.load_previous_results(self.results_file) self.epochs = self.load_previous_results(self.results_file)
@@ -644,6 +665,10 @@ class Hyperopt:
self.dimensions: List[Dimension] = self.hyperopt_space() self.dimensions: List[Dimension] = self.hyperopt_space()
self.opt = self.get_optimizer(self.dimensions, config_jobs) self.opt = self.get_optimizer(self.dimensions, config_jobs)
if self.print_colorized:
colorama_init(autoreset=True)
try: try:
with Parallel(n_jobs=config_jobs) as parallel: with Parallel(n_jobs=config_jobs) as parallel:
jobs = parallel._effective_n_jobs() jobs = parallel._effective_n_jobs()

View File

@@ -43,7 +43,7 @@ class SharpeHyperOptLossDaily(IHyperOptLoss):
normalize=True) normalize=True)
sum_daily = ( sum_daily = (
results.resample(resample_freq, on='close_time').agg( results.resample(resample_freq, on='close_date').agg(
{"profit_percent_after_slippage": sum}).reindex(t_index).fillna(0) {"profit_percent_after_slippage": sum}).reindex(t_index).fillna(0)
) )

View File

@@ -45,7 +45,7 @@ class SortinoHyperOptLossDaily(IHyperOptLoss):
normalize=True) normalize=True)
sum_daily = ( sum_daily = (
results.resample(resample_freq, on='close_time').agg( results.resample(resample_freq, on='close_date').agg(
{"profit_percent_after_slippage": sum}).reindex(t_index).fillna(0) {"profit_percent_after_slippage": sum}).reindex(t_index).fillna(0)
) )

View File

@@ -1,46 +1,40 @@
import logging import logging
from datetime import timedelta from datetime import datetime, timedelta, timezone
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List from typing import Any, Dict, List
from arrow import Arrow
from pandas import DataFrame from pandas import DataFrame
from numpy import int64
from tabulate import tabulate from tabulate import tabulate
from freqtrade.constants import DATETIME_PRINT_FORMAT, LAST_BT_RESULT_FN
from freqtrade.data.btanalysis import calculate_max_drawdown, calculate_market_change
from freqtrade.misc import file_dump_json from freqtrade.misc import file_dump_json
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def store_backtest_result(recordfilename: Path, all_results: Dict[str, DataFrame]) -> None: def store_backtest_stats(recordfilename: Path, stats: Dict[str, DataFrame]) -> None:
""" """
Stores backtest results to file (one file per strategy) Stores backtest results
:param recordfilename: Destination filename :param recordfilename: Path object, which can either be a filename or a directory.
:param all_results: Dict of Dataframes, one results dataframe per strategy Filenames will be appended with a timestamp right before the suffix
while for diectories, <directory>/backtest-result-<datetime>.json will be used as filename
:param stats: Dataframe containing the backtesting statistics
""" """
for strategy, results in all_results.items(): if recordfilename.is_dir():
records = backtest_result_to_list(results) filename = (recordfilename /
f'backtest-result-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.json')
else:
filename = Path.joinpath(
recordfilename.parent,
f'{recordfilename.stem}-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}'
).with_suffix(recordfilename.suffix)
file_dump_json(filename, stats)
if records: latest_filename = Path.joinpath(filename.parent, LAST_BT_RESULT_FN)
filename = recordfilename file_dump_json(latest_filename, {'latest_backtest': str(filename.name)})
if len(all_results) > 1:
# Inject strategy to filename
filename = Path.joinpath(
recordfilename.parent,
f'{recordfilename.stem}-{strategy}').with_suffix(recordfilename.suffix)
logger.info(f'Dumping backtest results to {filename}')
file_dump_json(filename, records)
def backtest_result_to_list(results: DataFrame) -> List[List]:
"""
Converts a list of Backtest-results to list
:param results: Dataframe containing results for one strategy
:return: List of Lists containing the trades
"""
return [[t.pair, t.profit_percent, t.open_time.timestamp(),
t.close_time.timestamp(), t.open_index - 1, t.trade_duration,
t.open_rate, t.close_rate, t.open_at_end, t.sell_reason.value]
for index, t in results.iterrows()]
def _get_line_floatfmt() -> List[str]: def _get_line_floatfmt() -> List[str]:
@@ -66,11 +60,12 @@ def _generate_result_line(result: DataFrame, max_open_trades: int, first_column:
return { return {
'key': first_column, 'key': first_column,
'trades': len(result), 'trades': len(result),
'profit_mean': result['profit_percent'].mean(), 'profit_mean': result['profit_percent'].mean() if len(result) > 0 else 0.0,
'profit_mean_pct': result['profit_percent'].mean() * 100.0, 'profit_mean_pct': result['profit_percent'].mean() * 100.0 if len(result) > 0 else 0.0,
'profit_sum': result['profit_percent'].sum(), 'profit_sum': result['profit_percent'].sum(),
'profit_sum_pct': result['profit_percent'].sum() * 100.0, 'profit_sum_pct': result['profit_percent'].sum() * 100.0,
'profit_total_abs': result['profit_abs'].sum(), 'profit_total_abs': result['profit_abs'].sum(),
'profit_total': result['profit_percent'].sum() / max_open_trades,
'profit_total_pct': result['profit_percent'].sum() * 100.0 / max_open_trades, 'profit_total_pct': result['profit_percent'].sum() * 100.0 / max_open_trades,
'duration_avg': str(timedelta( 'duration_avg': str(timedelta(
minutes=round(result['trade_duration'].mean())) minutes=round(result['trade_duration'].mean()))
@@ -141,7 +136,7 @@ def generate_sell_reason_stats(max_open_trades: int, results: DataFrame) -> List
'profit_sum': profit_sum, 'profit_sum': profit_sum,
'profit_sum_pct': round(profit_sum * 100, 2), 'profit_sum_pct': round(profit_sum * 100, 2),
'profit_total_abs': result['profit_abs'].sum(), 'profit_total_abs': result['profit_abs'].sum(),
'profit_pct_total': profit_percent_tot, 'profit_total_pct': profit_percent_tot,
} }
) )
return tabular_data return tabular_data
@@ -189,18 +184,58 @@ def generate_edge_table(results: dict) -> str:
floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore
def generate_daily_stats(results: DataFrame) -> Dict[str, Any]:
if len(results) == 0:
return {
'backtest_best_day': 0,
'backtest_worst_day': 0,
'winning_days': 0,
'draw_days': 0,
'losing_days': 0,
'winner_holding_avg': timedelta(),
'loser_holding_avg': timedelta(),
}
daily_profit = results.resample('1d', on='close_date')['profit_percent'].sum()
worst = min(daily_profit)
best = max(daily_profit)
winning_days = sum(daily_profit > 0)
draw_days = sum(daily_profit == 0)
losing_days = sum(daily_profit < 0)
winning_trades = results.loc[results['profit_percent'] > 0]
losing_trades = results.loc[results['profit_percent'] < 0]
return {
'backtest_best_day': best,
'backtest_worst_day': worst,
'winning_days': winning_days,
'draw_days': draw_days,
'losing_days': losing_days,
'winner_holding_avg': (timedelta(minutes=round(winning_trades['trade_duration'].mean()))
if not winning_trades.empty else timedelta()),
'loser_holding_avg': (timedelta(minutes=round(losing_trades['trade_duration'].mean()))
if not losing_trades.empty else timedelta()),
}
def generate_backtest_stats(config: Dict, btdata: Dict[str, DataFrame], def generate_backtest_stats(config: Dict, btdata: Dict[str, DataFrame],
all_results: Dict[str, DataFrame]) -> Dict[str, Any]: all_results: Dict[str, DataFrame],
min_date: Arrow, max_date: Arrow
) -> Dict[str, Any]:
""" """
:param config: Configuration object used for backtest :param config: Configuration object used for backtest
:param btdata: Backtest data :param btdata: Backtest data
:param all_results: backtest result - dictionary with { Strategy: results}. :param all_results: backtest result - dictionary with { Strategy: results}.
:param min_date: Backtest start date
:param max_date: Backtest end date
:return: :return:
Dictionary containing results per strategy and a stratgy summary. Dictionary containing results per strategy and a stratgy summary.
""" """
stake_currency = config['stake_currency'] stake_currency = config['stake_currency']
max_open_trades = config['max_open_trades'] max_open_trades = config['max_open_trades']
result: Dict[str, Any] = {'strategy': {}} result: Dict[str, Any] = {'strategy': {}}
market_change = calculate_market_change(btdata, 'close')
for strategy, results in all_results.items(): for strategy, results in all_results.items():
pair_results = generate_pair_metrics(btdata, stake_currency=stake_currency, pair_results = generate_pair_metrics(btdata, stake_currency=stake_currency,
@@ -212,14 +247,58 @@ def generate_backtest_stats(config: Dict, btdata: Dict[str, DataFrame],
max_open_trades=max_open_trades, max_open_trades=max_open_trades,
results=results.loc[results['open_at_end']], results=results.loc[results['open_at_end']],
skip_nan=True) skip_nan=True)
daily_stats = generate_daily_stats(results)
results['open_timestamp'] = results['open_date'].astype(int64) // 1e6
results['close_timestamp'] = results['close_date'].astype(int64) // 1e6
backtest_days = (max_date - min_date).days
strat_stats = { strat_stats = {
'trades': backtest_result_to_list(results), 'trades': results.to_dict(orient='records'),
'results_per_pair': pair_results, 'results_per_pair': pair_results,
'sell_reason_summary': sell_reason_stats, 'sell_reason_summary': sell_reason_stats,
'left_open_trades': left_open_results, 'left_open_trades': left_open_results,
} 'total_trades': len(results),
'profit_mean': results['profit_percent'].mean() if len(results) > 0 else 0,
'profit_total': results['profit_percent'].sum(),
'profit_total_abs': results['profit_abs'].sum(),
'backtest_start': min_date.datetime,
'backtest_start_ts': min_date.timestamp * 1000,
'backtest_end': max_date.datetime,
'backtest_end_ts': max_date.timestamp * 1000,
'backtest_days': backtest_days,
'trades_per_day': round(len(results) / backtest_days, 2) if backtest_days > 0 else 0,
'market_change': market_change,
'pairlist': list(btdata.keys()),
'stake_amount': config['stake_amount'],
'stake_currency': config['stake_currency'],
'max_open_trades': (config['max_open_trades']
if config['max_open_trades'] != float('inf') else -1),
'timeframe': config['timeframe'],
**daily_stats,
}
result['strategy'][strategy] = strat_stats result['strategy'][strategy] = strat_stats
try:
max_drawdown, drawdown_start, drawdown_end = calculate_max_drawdown(
results, value_col='profit_percent')
strat_stats.update({
'max_drawdown': max_drawdown,
'drawdown_start': drawdown_start,
'drawdown_start_ts': drawdown_start.timestamp() * 1000,
'drawdown_end': drawdown_end,
'drawdown_end_ts': drawdown_end.timestamp() * 1000,
})
except ValueError:
strat_stats.update({
'max_drawdown': 0.0,
'drawdown_start': datetime(1970, 1, 1, tzinfo=timezone.utc),
'drawdown_start_ts': 0,
'drawdown_end': datetime(1970, 1, 1, tzinfo=timezone.utc),
'drawdown_end_ts': 0,
})
strategy_results = generate_strategy_metrics(stake_currency=stake_currency, strategy_results = generate_strategy_metrics(stake_currency=stake_currency,
max_open_trades=max_open_trades, max_open_trades=max_open_trades,
all_results=all_results) all_results=all_results)
@@ -273,7 +352,7 @@ def text_table_sell_reason(sell_reason_stats: List[Dict[str, Any]], stake_curren
output = [[ output = [[
t['sell_reason'], t['trades'], t['wins'], t['draws'], t['losses'], t['sell_reason'], t['trades'], t['wins'], t['draws'], t['losses'],
t['profit_mean_pct'], t['profit_sum_pct'], t['profit_total_abs'], t['profit_pct_total'], t['profit_mean_pct'], t['profit_sum_pct'], t['profit_total_abs'], t['profit_total_pct'],
] for t in sell_reason_stats] ] for t in sell_reason_stats]
return tabulate(output, headers=headers, tablefmt="orgtbl", stralign="right") return tabulate(output, headers=headers, tablefmt="orgtbl", stralign="right")
@@ -298,6 +377,35 @@ def text_table_strategy(strategy_results, stake_currency: str) -> str:
floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") floatfmt=floatfmt, tablefmt="orgtbl", stralign="right")
def text_table_add_metrics(strat_results: Dict) -> str:
if len(strat_results['trades']) > 0:
min_trade = min(strat_results['trades'], key=lambda x: x['open_date'])
metrics = [
('Backtesting from', strat_results['backtest_start'].strftime(DATETIME_PRINT_FORMAT)),
('Backtesting to', strat_results['backtest_end'].strftime(DATETIME_PRINT_FORMAT)),
('Total trades', strat_results['total_trades']),
('First trade', min_trade['open_date'].strftime(DATETIME_PRINT_FORMAT)),
('First trade Pair', min_trade['pair']),
('Total Profit %', f"{round(strat_results['profit_total'] * 100, 2)}%"),
('Trades per day', strat_results['trades_per_day']),
('Best day', f"{round(strat_results['backtest_best_day'] * 100, 2)}%"),
('Worst day', f"{round(strat_results['backtest_worst_day'] * 100, 2)}%"),
('Days win/draw/lose', f"{strat_results['winning_days']} / "
f"{strat_results['draw_days']} / {strat_results['losing_days']}"),
('Avg. Duration Winners', f"{strat_results['winner_holding_avg']}"),
('Avg. Duration Loser', f"{strat_results['loser_holding_avg']}"),
('', ''), # Empty line to improve readability
('Max Drawdown', f"{round(strat_results['max_drawdown'] * 100, 2)}%"),
('Drawdown Start', strat_results['drawdown_start'].strftime(DATETIME_PRINT_FORMAT)),
('Drawdown End', strat_results['drawdown_end'].strftime(DATETIME_PRINT_FORMAT)),
('Market change', f"{round(strat_results['market_change'] * 100, 2)}%"),
]
return tabulate(metrics, headers=["Metric", "Value"], tablefmt="orgtbl")
else:
return ''
def show_backtest_results(config: Dict, backtest_stats: Dict): def show_backtest_results(config: Dict, backtest_stats: Dict):
stake_currency = config['stake_currency'] stake_currency = config['stake_currency']
@@ -312,15 +420,21 @@ def show_backtest_results(config: Dict, backtest_stats: Dict):
table = text_table_sell_reason(sell_reason_stats=results['sell_reason_summary'], table = text_table_sell_reason(sell_reason_stats=results['sell_reason_summary'],
stake_currency=stake_currency) stake_currency=stake_currency)
if isinstance(table, str): if isinstance(table, str) and len(table) > 0:
print(' SELL REASON STATS '.center(len(table.splitlines()[0]), '=')) print(' SELL REASON STATS '.center(len(table.splitlines()[0]), '='))
print(table) print(table)
table = text_table_bt_results(results['left_open_trades'], stake_currency=stake_currency) table = text_table_bt_results(results['left_open_trades'], stake_currency=stake_currency)
if isinstance(table, str): if isinstance(table, str) and len(table) > 0:
print(' LEFT OPEN TRADES REPORT '.center(len(table.splitlines()[0]), '=')) print(' LEFT OPEN TRADES REPORT '.center(len(table.splitlines()[0]), '='))
print(table) print(table)
if isinstance(table, str):
table = text_table_add_metrics(results)
if isinstance(table, str) and len(table) > 0:
print(' SUMMARY METRICS '.center(len(table.splitlines()[0]), '='))
print(table)
if isinstance(table, str) and len(table) > 0:
print('=' * len(table.splitlines()[0])) print('=' * len(table.splitlines()[0]))
print() print()

View File

@@ -5,6 +5,7 @@ import logging
import arrow import arrow
from typing import Any, Dict from typing import Any, Dict
from freqtrade.exceptions import OperationalException
from freqtrade.misc import plural from freqtrade.misc import plural
from freqtrade.pairlist.IPairList import IPairList from freqtrade.pairlist.IPairList import IPairList
@@ -23,7 +24,13 @@ class AgeFilter(IPairList):
super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos) super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos)
self._min_days_listed = pairlistconfig.get('min_days_listed', 10) self._min_days_listed = pairlistconfig.get('min_days_listed', 10)
self._enabled = self._min_days_listed >= 1
if self._min_days_listed < 1:
raise OperationalException("AgeFilter requires min_days_listed to be >= 1")
if self._min_days_listed > exchange.ohlcv_candle_limit:
raise OperationalException("AgeFilter requires min_days_listed to not exceed "
"exchange max request size "
f"({exchange.ohlcv_candle_limit})")
@property @property
def needstickers(self) -> bool: def needstickers(self) -> bool:
@@ -69,7 +76,7 @@ class AgeFilter(IPairList):
return True return True
else: else:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, " self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, "
f"because age is less than " f"because age {len(daily_candles)} is less than "
f"{self._min_days_listed} " f"{self._min_days_listed} "
f"{plural(self._min_days_listed, 'day')}") f"{plural(self._min_days_listed, 'day')}")
return False return False

View File

@@ -162,6 +162,11 @@ class IPairList(ABC):
f"{self._exchange.name}. Removing it from whitelist..") f"{self._exchange.name}. Removing it from whitelist..")
continue continue
if not self._exchange.market_is_tradable(markets[pair]):
logger.warning(f"Pair {pair} is not tradable with Freqtrade."
"Removing it from whitelist..")
continue
if self._exchange.get_pair_quote_currency(pair) != self._config['stake_currency']: if self._exchange.get_pair_quote_currency(pair) != self._config['stake_currency']:
logger.warning(f"Pair {pair} is not compatible with your stake currency " logger.warning(f"Pair {pair} is not compatible with your stake currency "
f"{self._config['stake_currency']}. Removing it from whitelist..") f"{self._config['stake_currency']}. Removing it from whitelist..")

View File

@@ -4,6 +4,7 @@ Price pair list filter
import logging import logging
from typing import Any, Dict from typing import Any, Dict
from freqtrade.exceptions import OperationalException
from freqtrade.pairlist.IPairList import IPairList from freqtrade.pairlist.IPairList import IPairList
@@ -18,7 +19,17 @@ class PriceFilter(IPairList):
super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos) super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos)
self._low_price_ratio = pairlistconfig.get('low_price_ratio', 0) self._low_price_ratio = pairlistconfig.get('low_price_ratio', 0)
self._enabled = self._low_price_ratio != 0 if self._low_price_ratio < 0:
raise OperationalException("PriceFilter requires low_price_ratio to be >= 0")
self._min_price = pairlistconfig.get('min_price', 0)
if self._min_price < 0:
raise OperationalException("PriceFilter requires min_price to be >= 0")
self._max_price = pairlistconfig.get('max_price', 0)
if self._max_price < 0:
raise OperationalException("PriceFilter requires max_price to be >= 0")
self._enabled = ((self._low_price_ratio > 0) or
(self._min_price > 0) or
(self._max_price > 0))
@property @property
def needstickers(self) -> bool: def needstickers(self) -> bool:
@@ -33,7 +44,18 @@ class PriceFilter(IPairList):
""" """
Short whitelist method description - used for startup-messages Short whitelist method description - used for startup-messages
""" """
return f"{self.name} - Filtering pairs priced below {self._low_price_ratio * 100}%." active_price_filters = []
if self._low_price_ratio != 0:
active_price_filters.append(f"below {self._low_price_ratio * 100}%")
if self._min_price != 0:
active_price_filters.append(f"below {self._min_price:.8f}")
if self._max_price != 0:
active_price_filters.append(f"above {self._max_price:.8f}")
if len(active_price_filters):
return f"{self.name} - Filtering pairs priced {' or '.join(active_price_filters)}."
return f"{self.name} - No price filters configured."
def _validate_pair(self, ticker) -> bool: def _validate_pair(self, ticker) -> bool:
""" """
@@ -41,15 +63,33 @@ class PriceFilter(IPairList):
:param ticker: ticker dict as returned from ccxt.load_markets() :param ticker: ticker dict as returned from ccxt.load_markets()
:return: True if the pair can stay, false if it should be removed :return: True if the pair can stay, false if it should be removed
""" """
if ticker['last'] is None: if ticker['last'] is None or ticker['last'] == 0:
self.log_on_refresh(logger.info, self.log_on_refresh(logger.info,
f"Removed {ticker['symbol']} from whitelist, because " f"Removed {ticker['symbol']} from whitelist, because "
"ticker['last'] is empty (Usually no trade in the last 24h).") "ticker['last'] is empty (Usually no trade in the last 24h).")
return False return False
compare = self._exchange.price_get_one_pip(ticker['symbol'], ticker['last'])
changeperc = compare / ticker['last'] # Perform low_price_ratio check.
if changeperc > self._low_price_ratio: if self._low_price_ratio != 0:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, " compare = self._exchange.price_get_one_pip(ticker['symbol'], ticker['last'])
f"because 1 unit is {changeperc * 100:.3f}%") changeperc = compare / ticker['last']
return False if changeperc > self._low_price_ratio:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, "
f"because 1 unit is {changeperc * 100:.3f}%")
return False
# Perform min_price check.
if self._min_price != 0:
if ticker['last'] < self._min_price:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, "
f"because last price < {self._min_price:.8f}")
return False
# Perform max_price check.
if self._max_price != 0:
if ticker['last'] > self._max_price:
self.log_on_refresh(logger.info, f"Removed {ticker['symbol']} from whitelist, "
f"because last price > {self._max_price:.8f}")
return False
return True return True

View File

@@ -2,7 +2,7 @@
This module contains the class to persist trades into SQLite This module contains the class to persist trades into SQLite
""" """
import logging import logging
from datetime import datetime from datetime import datetime, timezone
from decimal import Decimal from decimal import Decimal
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
@@ -17,6 +17,7 @@ from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.pool import StaticPool from sqlalchemy.pool import StaticPool
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.misc import safe_value_fallback
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -86,7 +87,7 @@ def check_migrate(engine) -> None:
logger.debug(f'trying {table_back_name}') logger.debug(f'trying {table_back_name}')
# Check for latest column # Check for latest column
if not has_column(cols, 'timeframe'): if not has_column(cols, 'amount_requested'):
logger.info(f'Running database migration - backup available as {table_back_name}') logger.info(f'Running database migration - backup available as {table_back_name}')
fee_open = get_column_def(cols, 'fee_open', 'fee') fee_open = get_column_def(cols, 'fee_open', 'fee')
@@ -119,6 +120,7 @@ def check_migrate(engine) -> None:
cols, 'close_profit_abs', cols, 'close_profit_abs',
f"(amount * close_rate * (1 - {fee_close})) - {open_trade_price}") f"(amount * close_rate * (1 - {fee_close})) - {open_trade_price}")
sell_order_status = get_column_def(cols, 'sell_order_status', 'null') sell_order_status = get_column_def(cols, 'sell_order_status', 'null')
amount_requested = get_column_def(cols, 'amount_requested', 'amount')
# Schema migration necessary # Schema migration necessary
engine.execute(f"alter table trades rename to {table_back_name}") engine.execute(f"alter table trades rename to {table_back_name}")
@@ -134,7 +136,7 @@ def check_migrate(engine) -> None:
fee_open, fee_open_cost, fee_open_currency, fee_open, fee_open_cost, fee_open_currency,
fee_close, fee_close_cost, fee_open_currency, open_rate, fee_close, fee_close_cost, fee_open_currency, open_rate,
open_rate_requested, close_rate, close_rate_requested, close_profit, open_rate_requested, close_rate, close_rate_requested, close_profit,
stake_amount, amount, open_date, close_date, open_order_id, stake_amount, amount, amount_requested, open_date, close_date, open_order_id,
stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct, stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct,
stoploss_order_id, stoploss_last_update, stoploss_order_id, stoploss_last_update,
max_rate, min_rate, sell_reason, sell_order_status, strategy, max_rate, min_rate, sell_reason, sell_order_status, strategy,
@@ -153,7 +155,7 @@ def check_migrate(engine) -> None:
{fee_close_cost} fee_close_cost, {fee_close_currency} fee_close_currency, {fee_close_cost} fee_close_cost, {fee_close_currency} fee_close_currency,
open_rate, {open_rate_requested} open_rate_requested, close_rate, open_rate, {open_rate_requested} open_rate_requested, close_rate,
{close_rate_requested} close_rate_requested, close_profit, {close_rate_requested} close_rate_requested, close_profit,
stake_amount, amount, open_date, close_date, open_order_id, stake_amount, amount, {amount_requested}, open_date, close_date, open_order_id,
{stop_loss} stop_loss, {stop_loss_pct} stop_loss_pct, {stop_loss} stop_loss, {stop_loss_pct} stop_loss_pct,
{initial_stop_loss} initial_stop_loss, {initial_stop_loss} initial_stop_loss,
{initial_stop_loss_pct} initial_stop_loss_pct, {initial_stop_loss_pct} initial_stop_loss_pct,
@@ -215,6 +217,7 @@ class Trade(_DECL_BASE):
close_profit_abs = Column(Float) close_profit_abs = Column(Float)
stake_amount = Column(Float, nullable=False) stake_amount = Column(Float, nullable=False)
amount = Column(Float) amount = Column(Float)
amount_requested = Column(Float)
open_date = Column(DateTime, nullable=False, default=datetime.utcnow) open_date = Column(DateTime, nullable=False, default=datetime.utcnow)
close_date = Column(DateTime) close_date = Column(DateTime)
open_order_id = Column(String) open_order_id = Column(String)
@@ -256,6 +259,7 @@ class Trade(_DECL_BASE):
'is_open': self.is_open, 'is_open': self.is_open,
'exchange': self.exchange, 'exchange': self.exchange,
'amount': round(self.amount, 8), 'amount': round(self.amount, 8),
'amount_requested': round(self.amount_requested, 8) if self.amount_requested else None,
'stake_amount': round(self.stake_amount, 8), 'stake_amount': round(self.stake_amount, 8),
'strategy': self.strategy, 'strategy': self.strategy,
'ticker_interval': self.timeframe, # DEPRECATED 'ticker_interval': self.timeframe, # DEPRECATED
@@ -270,16 +274,17 @@ class Trade(_DECL_BASE):
'open_date_hum': arrow.get(self.open_date).humanize(), 'open_date_hum': arrow.get(self.open_date).humanize(),
'open_date': self.open_date.strftime("%Y-%m-%d %H:%M:%S"), 'open_date': self.open_date.strftime("%Y-%m-%d %H:%M:%S"),
'open_timestamp': int(self.open_date.timestamp() * 1000), 'open_timestamp': int(self.open_date.replace(tzinfo=timezone.utc).timestamp() * 1000),
'open_rate': self.open_rate, 'open_rate': self.open_rate,
'open_rate_requested': self.open_rate_requested, 'open_rate_requested': self.open_rate_requested,
'open_trade_price': self.open_trade_price, 'open_trade_price': round(self.open_trade_price, 8),
'close_date_hum': (arrow.get(self.close_date).humanize() 'close_date_hum': (arrow.get(self.close_date).humanize()
if self.close_date else None), if self.close_date else None),
'close_date': (self.close_date.strftime("%Y-%m-%d %H:%M:%S") 'close_date': (self.close_date.strftime("%Y-%m-%d %H:%M:%S")
if self.close_date else None), if self.close_date else None),
'close_timestamp': int(self.close_date.timestamp() * 1000) if self.close_date else None, 'close_timestamp': int(self.close_date.replace(
tzinfo=timezone.utc).timestamp() * 1000) if self.close_date else None,
'close_rate': self.close_rate, 'close_rate': self.close_rate,
'close_rate_requested': self.close_rate_requested, 'close_rate_requested': self.close_rate_requested,
'close_profit': self.close_profit, 'close_profit': self.close_profit,
@@ -294,8 +299,8 @@ class Trade(_DECL_BASE):
'stoploss_order_id': self.stoploss_order_id, 'stoploss_order_id': self.stoploss_order_id,
'stoploss_last_update': (self.stoploss_last_update.strftime("%Y-%m-%d %H:%M:%S") 'stoploss_last_update': (self.stoploss_last_update.strftime("%Y-%m-%d %H:%M:%S")
if self.stoploss_last_update else None), if self.stoploss_last_update else None),
'stoploss_last_update_timestamp': (int(self.stoploss_last_update.timestamp() * 1000) 'stoploss_last_update_timestamp': int(self.stoploss_last_update.replace(
if self.stoploss_last_update else None), tzinfo=timezone.utc).timestamp() * 1000) if self.stoploss_last_update else None,
'initial_stop_loss': self.initial_stop_loss, # Deprecated - should not be used 'initial_stop_loss': self.initial_stop_loss, # Deprecated - should not be used
'initial_stop_loss_abs': self.initial_stop_loss, 'initial_stop_loss_abs': self.initial_stop_loss,
'initial_stop_loss_ratio': (self.initial_stop_loss_pct 'initial_stop_loss_ratio': (self.initial_stop_loss_pct
@@ -360,25 +365,25 @@ class Trade(_DECL_BASE):
def update(self, order: Dict) -> None: def update(self, order: Dict) -> None:
""" """
Updates this entity with amount and actual open/close rates. Updates this entity with amount and actual open/close rates.
:param order: order retrieved by exchange.get_order() :param order: order retrieved by exchange.fetch_order()
:return: None :return: None
""" """
order_type = order['type'] order_type = order['type']
# Ignore open and cancelled orders # Ignore open and cancelled orders
if order['status'] == 'open' or order['price'] is None: if order['status'] == 'open' or safe_value_fallback(order, 'average', 'price') is None:
return return
logger.info('Updating trade (id=%s) ...', self.id) logger.info('Updating trade (id=%s) ...', self.id)
if order_type in ('market', 'limit') and order['side'] == 'buy': if order_type in ('market', 'limit') and order['side'] == 'buy':
# Update open rate and actual amount # Update open rate and actual amount
self.open_rate = Decimal(order['price']) self.open_rate = Decimal(safe_value_fallback(order, 'average', 'price'))
self.amount = Decimal(order.get('filled', order['amount'])) self.amount = Decimal(safe_value_fallback(order, 'filled', 'amount'))
self.recalc_open_trade_price() self.recalc_open_trade_price()
logger.info('%s_BUY has been fulfilled for %s.', order_type.upper(), self) logger.info('%s_BUY has been fulfilled for %s.', order_type.upper(), self)
self.open_order_id = None self.open_order_id = None
elif order_type in ('market', 'limit') and order['side'] == 'sell': elif order_type in ('market', 'limit') and order['side'] == 'sell':
self.close(order['price']) self.close(safe_value_fallback(order, 'average', 'price'))
logger.info('%s_SELL has been fulfilled for %s.', order_type.upper(), self) logger.info('%s_SELL has been fulfilled for %s.', order_type.upper(), self)
elif order_type in ('stop_loss_limit', 'stop-loss', 'stop'): elif order_type in ('stop_loss_limit', 'stop-loss', 'stop'):
self.stoploss_order_id = None self.stoploss_order_id = None

View File

@@ -8,13 +8,16 @@ from freqtrade.configuration import TimeRange
from freqtrade.data.btanalysis import (calculate_max_drawdown, from freqtrade.data.btanalysis import (calculate_max_drawdown,
combine_dataframes_with_mean, combine_dataframes_with_mean,
create_cum_profit, create_cum_profit,
extract_trades_of_period, load_trades) extract_trades_of_period,
load_trades)
from freqtrade.data.converter import trim_dataframe from freqtrade.data.converter import trim_dataframe
from freqtrade.data.dataprovider import DataProvider
from freqtrade.data.history import load_data from freqtrade.data.history import load_data
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_prev_date from freqtrade.exchange import timeframe_to_prev_date
from freqtrade.misc import pair_to_filename from freqtrade.misc import pair_to_filename
from freqtrade.resolvers import StrategyResolver from freqtrade.resolvers import ExchangeResolver, StrategyResolver
from freqtrade.strategy import IStrategy
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -35,15 +38,15 @@ def init_plotscript(config):
""" """
if "pairs" in config: if "pairs" in config:
pairs = config["pairs"] pairs = config['pairs']
else: else:
pairs = config["exchange"]["pair_whitelist"] pairs = config['exchange']['pair_whitelist']
# Set timerange to use # Set timerange to use
timerange = TimeRange.parse_timerange(config.get("timerange")) timerange = TimeRange.parse_timerange(config.get('timerange'))
data = load_data( data = load_data(
datadir=config.get("datadir"), datadir=config.get('datadir'),
pairs=pairs, pairs=pairs,
timeframe=config.get('timeframe', '5m'), timeframe=config.get('timeframe', '5m'),
timerange=timerange, timerange=timerange,
@@ -51,19 +54,22 @@ def init_plotscript(config):
) )
no_trades = False no_trades = False
filename = config.get('exportfilename')
if config.get('no_trades', False): if config.get('no_trades', False):
no_trades = True no_trades = True
elif not config['exportfilename'].is_file() and config['trade_source'] == 'file': elif config['trade_source'] == 'file':
logger.warning("Backtest file is missing skipping trades.") if not filename.is_dir() and not filename.is_file():
no_trades = True logger.warning("Backtest file is missing skipping trades.")
no_trades = True
trades = load_trades( trades = load_trades(
config['trade_source'], config['trade_source'],
db_url=config.get('db_url'), db_url=config.get('db_url'),
exportfilename=config.get('exportfilename'), exportfilename=filename,
no_trades=no_trades no_trades=no_trades,
strategy=config.get('strategy'),
) )
trades = trim_dataframe(trades, timerange, 'open_time') trades = trim_dataframe(trades, timerange, 'open_date')
return {"ohlcv": data, return {"ohlcv": data,
"trades": trades, "trades": trades,
@@ -163,10 +169,11 @@ def plot_trades(fig, trades: pd.DataFrame) -> make_subplots:
if trades is not None and len(trades) > 0: if trades is not None and len(trades) > 0:
# Create description for sell summarizing the trade # Create description for sell summarizing the trade
trades['desc'] = trades.apply(lambda row: f"{round(row['profit_percent'] * 100, 1)}%, " trades['desc'] = trades.apply(lambda row: f"{round(row['profit_percent'] * 100, 1)}%, "
f"{row['sell_reason']}, {row['duration']} min", f"{row['sell_reason']}, "
f"{row['trade_duration']} min",
axis=1) axis=1)
trade_buys = go.Scatter( trade_buys = go.Scatter(
x=trades["open_time"], x=trades["open_date"],
y=trades["open_rate"], y=trades["open_rate"],
mode='markers', mode='markers',
name='Trade buy', name='Trade buy',
@@ -181,7 +188,7 @@ def plot_trades(fig, trades: pd.DataFrame) -> make_subplots:
) )
trade_sells = go.Scatter( trade_sells = go.Scatter(
x=trades.loc[trades['profit_percent'] > 0, "close_time"], x=trades.loc[trades['profit_percent'] > 0, "close_date"],
y=trades.loc[trades['profit_percent'] > 0, "close_rate"], y=trades.loc[trades['profit_percent'] > 0, "close_rate"],
text=trades.loc[trades['profit_percent'] > 0, "desc"], text=trades.loc[trades['profit_percent'] > 0, "desc"],
mode='markers', mode='markers',
@@ -194,7 +201,7 @@ def plot_trades(fig, trades: pd.DataFrame) -> make_subplots:
) )
) )
trade_sells_loss = go.Scatter( trade_sells_loss = go.Scatter(
x=trades.loc[trades['profit_percent'] <= 0, "close_time"], x=trades.loc[trades['profit_percent'] <= 0, "close_date"],
y=trades.loc[trades['profit_percent'] <= 0, "close_rate"], y=trades.loc[trades['profit_percent'] <= 0, "close_rate"],
text=trades.loc[trades['profit_percent'] <= 0, "desc"], text=trades.loc[trades['profit_percent'] <= 0, "desc"],
mode='markers', mode='markers',
@@ -467,6 +474,8 @@ def load_and_plot_trades(config: Dict[str, Any]):
""" """
strategy = StrategyResolver.load_strategy(config) strategy = StrategyResolver.load_strategy(config)
exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config)
IStrategy.dp = DataProvider(config, exchange)
plot_elements = init_plotscript(config) plot_elements = init_plotscript(config)
trades = plot_elements['trades'] trades = plot_elements['trades']
pair_counter = 0 pair_counter = 0
@@ -482,13 +491,13 @@ def load_and_plot_trades(config: Dict[str, Any]):
pair=pair, pair=pair,
data=df_analyzed, data=df_analyzed,
trades=trades_pair, trades=trades_pair,
indicators1=config.get("indicators1", []), indicators1=config.get('indicators1', []),
indicators2=config.get("indicators2", []), indicators2=config.get('indicators2', []),
plot_config=strategy.plot_config if hasattr(strategy, 'plot_config') else {} plot_config=strategy.plot_config if hasattr(strategy, 'plot_config') else {}
) )
store_plot_file(fig, filename=generate_plot_filename(pair, config['timeframe']), store_plot_file(fig, filename=generate_plot_filename(pair, config['timeframe']),
directory=config['user_data_dir'] / "plot") directory=config['user_data_dir'] / 'plot')
logger.info('End of plotting process. %s plots generated', pair_counter) logger.info('End of plotting process. %s plots generated', pair_counter)
@@ -505,8 +514,8 @@ def plot_profit(config: Dict[str, Any]) -> None:
# Filter trades to relevant pairs # Filter trades to relevant pairs
# Remove open pairs - we don't know the profit yet so can't calculate profit for these. # Remove open pairs - we don't know the profit yet so can't calculate profit for these.
# Also, If only one open pair is left, then the profit-generation would fail. # Also, If only one open pair is left, then the profit-generation would fail.
trades = trades[(trades['pair'].isin(plot_elements["pairs"])) trades = trades[(trades['pair'].isin(plot_elements['pairs']))
& (~trades['close_time'].isnull()) & (~trades['close_date'].isnull())
] ]
if len(trades) == 0: if len(trades) == 0:
raise OperationalException("No trades found, cannot generate Profit-plot without " raise OperationalException("No trades found, cannot generate Profit-plot without "
@@ -514,7 +523,7 @@ def plot_profit(config: Dict[str, Any]) -> None:
# Create an average close price of all the pairs that were involved. # Create an average close price of all the pairs that were involved.
# this could be useful to gauge the overall market trend # this could be useful to gauge the overall market trend
fig = generate_profit_graph(plot_elements["pairs"], plot_elements["ohlcv"], fig = generate_profit_graph(plot_elements['pairs'], plot_elements['ohlcv'],
trades, config.get('timeframe', '5m')) trades, config.get('timeframe', '5m'))
store_plot_file(fig, filename='freqtrade-profit-plot.html', store_plot_file(fig, filename='freqtrade-profit-plot.html',
directory=config['user_data_dir'] / "plot", auto_open=True) directory=config['user_data_dir'] / 'plot', auto_open=True)

View File

@@ -23,7 +23,7 @@ class HyperOptResolver(IResolver):
object_type = IHyperOpt object_type = IHyperOpt
object_type_str = "Hyperopt" object_type_str = "Hyperopt"
user_subdir = USERPATH_HYPEROPTS user_subdir = USERPATH_HYPEROPTS
initial_search_path = Path(__file__).parent.parent.joinpath('optimize').resolve() initial_search_path = None
@staticmethod @staticmethod
def load_hyperopt(config: Dict) -> IHyperOpt: def load_hyperopt(config: Dict) -> IHyperOpt:
@@ -42,14 +42,14 @@ class HyperOptResolver(IResolver):
extra_dir=config.get('hyperopt_path')) extra_dir=config.get('hyperopt_path'))
if not hasattr(hyperopt, 'populate_indicators'): if not hasattr(hyperopt, 'populate_indicators'):
logger.warning("Hyperopt class does not provide populate_indicators() method. " logger.info("Hyperopt class does not provide populate_indicators() method. "
"Using populate_indicators from the strategy.") "Using populate_indicators from the strategy.")
if not hasattr(hyperopt, 'populate_buy_trend'): if not hasattr(hyperopt, 'populate_buy_trend'):
logger.warning("Hyperopt class does not provide populate_buy_trend() method. " logger.info("Hyperopt class does not provide populate_buy_trend() method. "
"Using populate_buy_trend from the strategy.") "Using populate_buy_trend from the strategy.")
if not hasattr(hyperopt, 'populate_sell_trend'): if not hasattr(hyperopt, 'populate_sell_trend'):
logger.warning("Hyperopt class does not provide populate_sell_trend() method. " logger.info("Hyperopt class does not provide populate_sell_trend() method. "
"Using populate_sell_trend from the strategy.") "Using populate_sell_trend from the strategy.")
return hyperopt return hyperopt

View File

@@ -16,7 +16,9 @@ from werkzeug.security import safe_str_cmp
from werkzeug.serving import make_server from werkzeug.serving import make_server
from freqtrade.__init__ import __version__ from freqtrade.__init__ import __version__
from freqtrade.constants import DATETIME_PRINT_FORMAT
from freqtrade.rpc.rpc import RPC, RPCException from freqtrade.rpc.rpc import RPC, RPCException
from freqtrade.rpc.fiat_convert import CryptoToFiatConverter
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -31,7 +33,7 @@ class ArrowJSONEncoder(JSONEncoder):
elif isinstance(obj, date): elif isinstance(obj, date):
return obj.strftime("%Y-%m-%d") return obj.strftime("%Y-%m-%d")
elif isinstance(obj, datetime): elif isinstance(obj, datetime):
return obj.strftime("%Y-%m-%d %H:%M:%S") return obj.strftime(DATETIME_PRINT_FORMAT)
iterable = iter(obj) iterable = iter(obj)
except TypeError: except TypeError:
pass pass
@@ -55,7 +57,7 @@ def require_login(func: Callable[[Any, Any], Any]):
# Type should really be Callable[[ApiServer], Any], but that will create a circular dependency # Type should really be Callable[[ApiServer], Any], but that will create a circular dependency
def rpc_catch_errors(func: Callable[[Any], Any]): def rpc_catch_errors(func: Callable[..., Any]):
def func_wrapper(obj, *args, **kwargs): def func_wrapper(obj, *args, **kwargs):
@@ -105,6 +107,9 @@ class ApiServer(RPC):
# Register application handling # Register application handling
self.register_rest_rpc_urls() self.register_rest_rpc_urls()
if self._config.get('fiat_display_currency', None):
self._fiat_converter = CryptoToFiatConverter()
thread = threading.Thread(target=self.run, daemon=True) thread = threading.Thread(target=self.run, daemon=True)
thread.start() thread.start()
@@ -182,6 +187,7 @@ class ApiServer(RPC):
self.app.add_url_rule(f'{BASE_URI}/count', 'count', view_func=self._count, methods=['GET']) self.app.add_url_rule(f'{BASE_URI}/count', 'count', view_func=self._count, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/daily', 'daily', view_func=self._daily, methods=['GET']) self.app.add_url_rule(f'{BASE_URI}/daily', 'daily', view_func=self._daily, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/edge', 'edge', view_func=self._edge, methods=['GET']) self.app.add_url_rule(f'{BASE_URI}/edge', 'edge', view_func=self._edge, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/logs', 'log', view_func=self._get_logs, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/profit', 'profit', self.app.add_url_rule(f'{BASE_URI}/profit', 'profit',
view_func=self._profit, methods=['GET']) view_func=self._profit, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/performance', 'performance', self.app.add_url_rule(f'{BASE_URI}/performance', 'performance',
@@ -196,6 +202,8 @@ class ApiServer(RPC):
view_func=self._ping, methods=['GET']) view_func=self._ping, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/trades', 'trades', self.app.add_url_rule(f'{BASE_URI}/trades', 'trades',
view_func=self._trades, methods=['GET']) view_func=self._trades, methods=['GET'])
self.app.add_url_rule(f'{BASE_URI}/trades/<int:tradeid>', 'trades_delete',
view_func=self._trades_delete, methods=['DELETE'])
# Combined actions and infos # Combined actions and infos
self.app.add_url_rule(f'{BASE_URI}/blacklist', 'blacklist', view_func=self._blacklist, self.app.add_url_rule(f'{BASE_URI}/blacklist', 'blacklist', view_func=self._blacklist,
methods=['GET', 'POST']) methods=['GET', 'POST'])
@@ -342,6 +350,18 @@ class ApiServer(RPC):
return self.rest_dump(stats) return self.rest_dump(stats)
@require_login
@rpc_catch_errors
def _get_logs(self):
"""
Returns latest logs
get:
param:
limit: Only get a certain number of records
"""
limit = int(request.args.get('limit', 0)) or None
return self.rest_dump(self._rpc_get_logs(limit))
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors
def _edge(self): def _edge(self):
@@ -420,6 +440,19 @@ class ApiServer(RPC):
results = self._rpc_trade_history(limit) results = self._rpc_trade_history(limit)
return self.rest_dump(results) return self.rest_dump(results)
@require_login
@rpc_catch_errors
def _trades_delete(self, tradeid):
"""
Handler for DELETE /trades/<tradeid> endpoint.
Removes the trade from the database (tries to cancel open orders first!)
get:
param:
tradeid: Numeric trade-id assigned to the trade.
"""
result = self._rpc_delete(tradeid)
return self.rest_dump(result)
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors
def _whitelist(self): def _whitelist(self):

View File

@@ -6,12 +6,14 @@ from abc import abstractmethod
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
from enum import Enum from enum import Enum
from math import isnan from math import isnan
from typing import Any, Dict, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple, Union
import arrow import arrow
from numpy import NAN, mean from numpy import NAN, mean
from freqtrade.exceptions import DependencyException, TemporaryError from freqtrade.exceptions import ExchangeError, PricingError
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_msecs
from freqtrade.loggers import bufferHandler
from freqtrade.misc import shorten_date from freqtrade.misc import shorten_date
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.rpc.fiat_convert import CryptoToFiatConverter from freqtrade.rpc.fiat_convert import CryptoToFiatConverter
@@ -103,6 +105,8 @@ class RPC:
'trailing_only_offset_is_reached': config.get('trailing_only_offset_is_reached'), 'trailing_only_offset_is_reached': config.get('trailing_only_offset_is_reached'),
'ticker_interval': config['timeframe'], # DEPRECATED 'ticker_interval': config['timeframe'], # DEPRECATED
'timeframe': config['timeframe'], 'timeframe': config['timeframe'],
'timeframe_ms': timeframe_to_msecs(config['timeframe']),
'timeframe_min': timeframe_to_minutes(config['timeframe']),
'exchange': config['exchange']['name'], 'exchange': config['exchange']['name'],
'strategy': config['strategy'], 'strategy': config['strategy'],
'forcebuy_enabled': config.get('forcebuy_enable', False), 'forcebuy_enabled': config.get('forcebuy_enable', False),
@@ -126,11 +130,11 @@ class RPC:
for trade in trades: for trade in trades:
order = None order = None
if trade.open_order_id: if trade.open_order_id:
order = self._freqtrade.exchange.get_order(trade.open_order_id, trade.pair) order = self._freqtrade.exchange.fetch_order(trade.open_order_id, trade.pair)
# calculate profit and send message to user # calculate profit and send message to user
try: try:
current_rate = self._freqtrade.get_sell_rate(trade.pair, False) current_rate = self._freqtrade.get_sell_rate(trade.pair, False)
except DependencyException: except (ExchangeError, PricingError):
current_rate = NAN current_rate = NAN
current_profit = trade.calc_profit_ratio(current_rate) current_profit = trade.calc_profit_ratio(current_rate)
current_profit_abs = trade.calc_profit(current_rate) current_profit_abs = trade.calc_profit(current_rate)
@@ -154,6 +158,7 @@ class RPC:
current_profit_abs=current_profit_abs, current_profit_abs=current_profit_abs,
stoploss_current_dist=stoploss_current_dist, stoploss_current_dist=stoploss_current_dist,
stoploss_current_dist_ratio=round(stoploss_current_dist_ratio, 8), stoploss_current_dist_ratio=round(stoploss_current_dist_ratio, 8),
stoploss_current_dist_pct=round(stoploss_current_dist_ratio * 100, 2),
stoploss_entry_dist=stoploss_entry_dist, stoploss_entry_dist=stoploss_entry_dist,
stoploss_entry_dist_ratio=round(stoploss_entry_dist_ratio, 8), stoploss_entry_dist_ratio=round(stoploss_entry_dist_ratio, 8),
open_order='({} {} rem={:.8f})'.format( open_order='({} {} rem={:.8f})'.format(
@@ -174,7 +179,7 @@ class RPC:
# calculate profit and send message to user # calculate profit and send message to user
try: try:
current_rate = self._freqtrade.get_sell_rate(trade.pair, False) current_rate = self._freqtrade.get_sell_rate(trade.pair, False)
except DependencyException: except (PricingError, ExchangeError):
current_rate = NAN current_rate = NAN
trade_percent = (100 * trade.calc_profit_ratio(current_rate)) trade_percent = (100 * trade.calc_profit_ratio(current_rate))
trade_profit = trade.calc_profit(current_rate) trade_profit = trade.calc_profit(current_rate)
@@ -220,22 +225,20 @@ class RPC:
]).order_by(Trade.close_date).all() ]).order_by(Trade.close_date).all()
curdayprofit = sum(trade.close_profit_abs for trade in trades) curdayprofit = sum(trade.close_profit_abs for trade in trades)
profit_days[profitday] = { profit_days[profitday] = {
'amount': f'{curdayprofit:.8f}', 'amount': curdayprofit,
'trades': len(trades) 'trades': len(trades)
} }
data = [ data = [
{ {
'date': key, 'date': key,
'abs_profit': f'{float(value["amount"]):.8f}', 'abs_profit': value["amount"],
'fiat_value': '{value:.3f}'.format( 'fiat_value': self._fiat_converter.convert_amount(
value=self._fiat_converter.convert_amount(
value['amount'], value['amount'],
stake_currency, stake_currency,
fiat_display_currency fiat_display_currency
) if self._fiat_converter else 0, ) if self._fiat_converter else 0,
), 'trade_count': value["trades"],
'trade_count': f'{value["trades"]}',
} }
for key, value in profit_days.items() for key, value in profit_days.items()
] ]
@@ -248,9 +251,10 @@ class RPC:
def _rpc_trade_history(self, limit: int) -> Dict: def _rpc_trade_history(self, limit: int) -> Dict:
""" Returns the X last trades """ """ Returns the X last trades """
if limit > 0: if limit > 0:
trades = Trade.get_trades().order_by(Trade.id.desc()).limit(limit) trades = Trade.get_trades([Trade.is_open.is_(False)]).order_by(
Trade.id.desc()).limit(limit)
else: else:
trades = Trade.get_trades().order_by(Trade.id.desc()).all() trades = Trade.get_trades([Trade.is_open.is_(False)]).order_by(Trade.id.desc()).all()
output = [trade.to_json() for trade in trades] output = [trade.to_json() for trade in trades]
@@ -269,6 +273,8 @@ class RPC:
profit_closed_coin = [] profit_closed_coin = []
profit_closed_ratio = [] profit_closed_ratio = []
durations = [] durations = []
winning_trades = 0
losing_trades = 0
for trade in trades: for trade in trades:
current_rate: float = 0.0 current_rate: float = 0.0
@@ -282,11 +288,15 @@ class RPC:
profit_ratio = trade.close_profit profit_ratio = trade.close_profit
profit_closed_coin.append(trade.close_profit_abs) profit_closed_coin.append(trade.close_profit_abs)
profit_closed_ratio.append(profit_ratio) profit_closed_ratio.append(profit_ratio)
if trade.close_profit >= 0:
winning_trades += 1
else:
losing_trades += 1
else: else:
# Get current rate # Get current rate
try: try:
current_rate = self._freqtrade.get_sell_rate(trade.pair, False) current_rate = self._freqtrade.get_sell_rate(trade.pair, False)
except DependencyException: except (PricingError, ExchangeError):
current_rate = NAN current_rate = NAN
profit_ratio = trade.calc_profit_ratio(rate=current_rate) profit_ratio = trade.calc_profit_ratio(rate=current_rate)
@@ -344,6 +354,8 @@ class RPC:
'avg_duration': str(timedelta(seconds=sum(durations) / num)).split('.')[0], 'avg_duration': str(timedelta(seconds=sum(durations) / num)).split('.')[0],
'best_pair': best_pair[0] if best_pair else '', 'best_pair': best_pair[0] if best_pair else '',
'best_rate': round(best_pair[1] * 100, 2) if best_pair else 0, 'best_rate': round(best_pair[1] * 100, 2) if best_pair else 0,
'winning_trades': winning_trades,
'losing_trades': losing_trades,
} }
def _rpc_balance(self, stake_currency: str, fiat_display_currency: str) -> Dict: def _rpc_balance(self, stake_currency: str, fiat_display_currency: str) -> Dict:
@@ -352,7 +364,7 @@ class RPC:
total = 0.0 total = 0.0
try: try:
tickers = self._freqtrade.exchange.get_tickers() tickers = self._freqtrade.exchange.get_tickers()
except (TemporaryError, DependencyException): except (ExchangeError):
raise RPCException('Error getting current tickers.') raise RPCException('Error getting current tickers.')
self._freqtrade.wallets.update(require_update=False) self._freqtrade.wallets.update(require_update=False)
@@ -373,7 +385,7 @@ class RPC:
if pair.startswith(stake_currency): if pair.startswith(stake_currency):
rate = 1.0 / rate rate = 1.0 / rate
est_stake = rate * balance.total est_stake = rate * balance.total
except (TemporaryError, DependencyException): except (ExchangeError):
logger.warning(f" Could not get rate for pair {coin}.") logger.warning(f" Could not get rate for pair {coin}.")
continue continue
total = total + (est_stake or 0) total = total + (est_stake or 0)
@@ -442,7 +454,7 @@ class RPC:
def _exec_forcesell(trade: Trade) -> None: def _exec_forcesell(trade: Trade) -> None:
# Check if there is there is an open order # Check if there is there is an open order
if trade.open_order_id: if trade.open_order_id:
order = self._freqtrade.exchange.get_order(trade.open_order_id, trade.pair) order = self._freqtrade.exchange.fetch_order(trade.open_order_id, trade.pair)
# Cancel open LIMIT_BUY orders and close trade # Cancel open LIMIT_BUY orders and close trade
if order and order['status'] == 'open' \ if order and order['status'] == 'open' \
@@ -511,7 +523,7 @@ class RPC:
# check if valid pair # check if valid pair
# check if pair already has an open pair # check if pair already has an open pair
trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair.is_(pair)]).first() trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair == pair]).first()
if trade: if trade:
raise RPCException(f'position for {pair} already open - id: {trade.id}') raise RPCException(f'position for {pair} already open - id: {trade.id}')
@@ -520,11 +532,51 @@ class RPC:
# execute buy # execute buy
if self._freqtrade.execute_buy(pair, stakeamount, price): if self._freqtrade.execute_buy(pair, stakeamount, price):
trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair.is_(pair)]).first() trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair == pair]).first()
return trade return trade
else: else:
return None return None
def _rpc_delete(self, trade_id: str) -> Dict[str, Union[str, int]]:
"""
Handler for delete <id>.
Delete the given trade and close eventually existing open orders.
"""
with self._freqtrade._sell_lock:
c_count = 0
trade = Trade.get_trades(trade_filter=[Trade.id == trade_id]).first()
if not trade:
logger.warning('delete trade: Invalid argument received')
raise RPCException('invalid argument')
# Try cancelling regular order if that exists
if trade.open_order_id:
try:
self._freqtrade.exchange.cancel_order(trade.open_order_id, trade.pair)
c_count += 1
except (ExchangeError):
pass
# cancel stoploss on exchange ...
if (self._freqtrade.strategy.order_types.get('stoploss_on_exchange')
and trade.stoploss_order_id):
try:
self._freqtrade.exchange.cancel_stoploss_order(trade.stoploss_order_id,
trade.pair)
c_count += 1
except (ExchangeError):
pass
Trade.session.delete(trade)
Trade.session.flush()
self._freqtrade.wallets.update()
return {
'result': 'success',
'trade_id': trade_id,
'result_msg': f'Deleted trade {trade_id}. Closed {c_count} open orders.',
'cancel_order_count': c_count,
}
def _rpc_performance(self) -> List[Dict[str, Any]]: def _rpc_performance(self) -> List[Dict[str, Any]]:
""" """
Handler for performance. Handler for performance.
@@ -580,6 +632,24 @@ class RPC:
} }
return res return res
def _rpc_get_logs(self, limit: Optional[int]) -> Dict[str, Any]:
"""Returns the last X logs"""
if limit:
buffer = bufferHandler.buffer[-limit:]
else:
buffer = bufferHandler.buffer
records = [[datetime.fromtimestamp(r.created).strftime("%Y-%m-%d %H:%M:%S"),
r.created * 1000, r.name, r.levelname,
r.message + ('\n' + r.exc_text if r.exc_text else '')]
for r in buffer]
# Log format:
# [logtime-formatted, logepoch, logger-name, loglevel, message \n + exception]
# e.g. ["2020-08-27 11:35:01", 1598520901097.9397,
# "freqtrade.worker", "INFO", "Starting worker develop"]
return {'log_count': len(records), 'logs': records}
def _rpc_edge(self) -> List[Dict[str, Any]]: def _rpc_edge(self) -> List[Dict[str, Any]]:
""" Returns information related to Edge """ """ Returns information related to Edge """
if not self._freqtrade.edge: if not self._freqtrade.edge:

View File

@@ -5,12 +5,14 @@ This module manage Telegram communication
""" """
import json import json
import logging import logging
import arrow
from typing import Any, Callable, Dict from typing import Any, Callable, Dict
from tabulate import tabulate from tabulate import tabulate
from telegram import ParseMode, ReplyKeyboardMarkup, Update from telegram import ParseMode, ReplyKeyboardMarkup, Update
from telegram.error import NetworkError, TelegramError from telegram.error import NetworkError, TelegramError
from telegram.ext import CallbackContext, CommandHandler, Updater from telegram.ext import CallbackContext, CommandHandler, Updater
from telegram.utils.helpers import escape_markdown
from freqtrade.__init__ import __version__ from freqtrade.__init__ import __version__
from freqtrade.rpc import RPC, RPCException, RPCMessageType from freqtrade.rpc import RPC, RPCException, RPCMessageType
@@ -92,6 +94,8 @@ class Telegram(RPC):
CommandHandler('stop', self._stop), CommandHandler('stop', self._stop),
CommandHandler('forcesell', self._forcesell), CommandHandler('forcesell', self._forcesell),
CommandHandler('forcebuy', self._forcebuy), CommandHandler('forcebuy', self._forcebuy),
CommandHandler('trades', self._trades),
CommandHandler('delete', self._delete_trade),
CommandHandler('performance', self._performance), CommandHandler('performance', self._performance),
CommandHandler('daily', self._daily), CommandHandler('daily', self._daily),
CommandHandler('count', self._count), CommandHandler('count', self._count),
@@ -100,6 +104,7 @@ class Telegram(RPC):
CommandHandler('stopbuy', self._stopbuy), CommandHandler('stopbuy', self._stopbuy),
CommandHandler('whitelist', self._whitelist), CommandHandler('whitelist', self._whitelist),
CommandHandler('blacklist', self._blacklist), CommandHandler('blacklist', self._blacklist),
CommandHandler('logs', self._logs),
CommandHandler('edge', self._edge), CommandHandler('edge', self._edge),
CommandHandler('help', self._help), CommandHandler('help', self._help),
CommandHandler('version', self._version), CommandHandler('version', self._version),
@@ -236,17 +241,18 @@ class Telegram(RPC):
("*Close Profit:* `{close_profit_pct}`" ("*Close Profit:* `{close_profit_pct}`"
if r['close_profit_pct'] is not None else ""), if r['close_profit_pct'] is not None else ""),
"*Current Profit:* `{current_profit_pct:.2f}%`", "*Current Profit:* `{current_profit_pct:.2f}%`",
# Adding initial stoploss only if it is different from stoploss
"*Initial Stoploss:* `{initial_stop_loss:.8f}` " +
("`({initial_stop_loss_pct:.2f}%)`") if (
r['stop_loss'] != r['initial_stop_loss']
and r['initial_stop_loss_pct'] is not None) else "",
# Adding stoploss and stoploss percentage only if it is not None
"*Stoploss:* `{stop_loss:.8f}` " +
("`({stop_loss_pct:.2f}%)`" if r['stop_loss_pct'] else ""),
] ]
if (r['stop_loss'] != r['initial_stop_loss']
and r['initial_stop_loss_pct'] is not None):
# Adding initial stoploss only if it is different from stoploss
lines.append("*Initial Stoploss:* `{initial_stop_loss:.8f}` "
"`({initial_stop_loss_pct:.2f}%)`")
# Adding stoploss and stoploss percentage only if it is not None
lines.append("*Stoploss:* `{stop_loss:.8f}` " +
("`({stop_loss_pct:.2f}%)`" if r['stop_loss_pct'] else ""))
lines.append("*Stoploss distance:* `{stoploss_current_dist:.8f}` "
"`({stoploss_current_dist_pct:.2f}%)`")
if r['open_order']: if r['open_order']:
if r['sell_order_status']: if r['sell_order_status']:
lines.append("*Open Order:* `{open_order}` - `{sell_order_status}`") lines.append("*Open Order:* `{open_order}` - `{sell_order_status}`")
@@ -302,8 +308,8 @@ class Telegram(RPC):
) )
stats_tab = tabulate( stats_tab = tabulate(
[[day['date'], [[day['date'],
f"{day['abs_profit']} {stats['stake_currency']}", f"{day['abs_profit']:.8f} {stats['stake_currency']}",
f"{day['fiat_value']} {stats['fiat_display_currency']}", f"{day['fiat_value']:.3f} {stats['fiat_display_currency']}",
f"{day['trade_count']} trades"] for day in stats['data']], f"{day['trade_count']} trades"] for day in stats['data']],
headers=[ headers=[
'Day', 'Day',
@@ -366,7 +372,9 @@ class Telegram(RPC):
f"∙ `{profit_all_fiat:.3f} {fiat_disp_cur}`\n" f"∙ `{profit_all_fiat:.3f} {fiat_disp_cur}`\n"
f"*Total Trade Count:* `{trade_count}`\n" f"*Total Trade Count:* `{trade_count}`\n"
f"*First Trade opened:* `{first_trade_date}`\n" f"*First Trade opened:* `{first_trade_date}`\n"
f"*Latest Trade opened:* `{latest_trade_date}`") f"*Latest Trade opened:* `{latest_trade_date}\n`"
f"*Win / Loss:* `{stats['winning_trades']} / {stats['losing_trades']}`"
)
if stats['closed_trade_count'] > 0: if stats['closed_trade_count'] > 0:
markdown_msg += (f"\n*Avg. Duration:* `{avg_duration}`\n" markdown_msg += (f"\n*Avg. Duration:* `{avg_duration}`\n"
f"*Best Performing:* `{best_pair}: {best_rate:.2f}%`") f"*Best Performing:* `{best_pair}: {best_rate:.2f}%`")
@@ -494,6 +502,62 @@ class Telegram(RPC):
except RPCException as e: except RPCException as e:
self._send_msg(str(e)) self._send_msg(str(e))
@authorized_only
def _trades(self, update: Update, context: CallbackContext) -> None:
"""
Handler for /trades <n>
Returns last n recent trades.
:param bot: telegram bot
:param update: message update
:return: None
"""
stake_cur = self._config['stake_currency']
try:
nrecent = int(context.args[0])
except (TypeError, ValueError, IndexError):
nrecent = 10
try:
trades = self._rpc_trade_history(
nrecent
)
trades_tab = tabulate(
[[arrow.get(trade['open_date']).humanize(),
trade['pair'],
f"{(100 * trade['close_profit']):.2f}% ({trade['close_profit_abs']})"]
for trade in trades['trades']],
headers=[
'Open Date',
'Pair',
f'Profit ({stake_cur})',
],
tablefmt='simple')
message = (f"<b>{min(trades['trades_count'], nrecent)} recent trades</b>:\n"
+ (f"<pre>{trades_tab}</pre>" if trades['trades_count'] > 0 else ''))
self._send_msg(message, parse_mode=ParseMode.HTML)
except RPCException as e:
self._send_msg(str(e))
@authorized_only
def _delete_trade(self, update: Update, context: CallbackContext) -> None:
"""
Handler for /delete <id>.
Delete the given trade
:param bot: telegram bot
:param update: message update
:return: None
"""
trade_id = context.args[0] if len(context.args) > 0 else None
try:
msg = self._rpc_delete(trade_id)
self._send_msg((
'`{result_msg}`\n'
'Please make sure to take care of this asset on the exchange manually.'
).format(**msg))
except RPCException as e:
self._send_msg(str(e))
@authorized_only @authorized_only
def _performance(self, update: Update, context: CallbackContext) -> None: def _performance(self, update: Update, context: CallbackContext) -> None:
""" """
@@ -576,6 +640,38 @@ class Telegram(RPC):
except RPCException as e: except RPCException as e:
self._send_msg(str(e)) self._send_msg(str(e))
@authorized_only
def _logs(self, update: Update, context: CallbackContext) -> None:
"""
Handler for /logs
Shows the latest logs
"""
try:
try:
limit = int(context.args[0])
except (TypeError, ValueError, IndexError):
limit = 10
logs = self._rpc_get_logs(limit)['logs']
msgs = ''
msg_template = "*{}* {}: {} \\- `{}`"
for logrec in logs:
msg = msg_template.format(escape_markdown(logrec[0], version=2),
escape_markdown(logrec[2], version=2),
escape_markdown(logrec[3], version=2),
escape_markdown(logrec[4], version=2))
if len(msgs + msg) + 10 >= MAX_TELEGRAM_MESSAGE_LENGTH:
# Send message immediately if it would become too long
self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN_V2)
msgs = msg + '\n'
else:
# Append message to messages to send
msgs += msg + '\n'
if msgs:
self._send_msg(msgs, parse_mode=ParseMode.MARKDOWN_V2)
except RPCException as e:
self._send_msg(str(e))
@authorized_only @authorized_only
def _edge(self, update: Update, context: CallbackContext) -> None: def _edge(self, update: Update, context: CallbackContext) -> None:
""" """
@@ -607,10 +703,12 @@ class Telegram(RPC):
" *table :* `will display trades in a table`\n" " *table :* `will display trades in a table`\n"
" `pending buy orders are marked with an asterisk (*)`\n" " `pending buy orders are marked with an asterisk (*)`\n"
" `pending sell orders are marked with a double asterisk (**)`\n" " `pending sell orders are marked with a double asterisk (**)`\n"
"*/trades [limit]:* `Lists last closed trades (limited to 10 by default)`\n"
"*/profit:* `Lists cumulative profit from all finished trades`\n" "*/profit:* `Lists cumulative profit from all finished trades`\n"
"*/forcesell <trade_id>|all:* `Instantly sells the given trade or all trades, " "*/forcesell <trade_id>|all:* `Instantly sells the given trade or all trades, "
"regardless of profit`\n" "regardless of profit`\n"
f"{forcebuy_text if self._config.get('forcebuy_enable', False) else ''}" f"{forcebuy_text if self._config.get('forcebuy_enable', False) else ''}"
"*/delete <trade_id>:* `Instantly delete the given trade in the database`\n"
"*/performance:* `Show performance of each finished trade grouped by pair`\n" "*/performance:* `Show performance of each finished trade grouped by pair`\n"
"*/daily <n>:* `Shows profit or loss per day, over the last n days`\n" "*/daily <n>:* `Shows profit or loss per day, over the last n days`\n"
"*/count:* `Show number of trades running compared to allowed number of trades`" "*/count:* `Show number of trades running compared to allowed number of trades`"
@@ -619,6 +717,7 @@ class Telegram(RPC):
"*/stopbuy:* `Stops buying, but handles open trades gracefully` \n" "*/stopbuy:* `Stops buying, but handles open trades gracefully` \n"
"*/reload_config:* `Reload configuration file` \n" "*/reload_config:* `Reload configuration file` \n"
"*/show_config:* `Show running configuration` \n" "*/show_config:* `Show running configuration` \n"
"*/logs [limit]:* `Show latest logs - defaults to 10` \n"
"*/whitelist:* `Show current whitelist` \n" "*/whitelist:* `Show current whitelist` \n"
"*/blacklist [pair]:* `Show current blacklist, or adds one or more pairs " "*/blacklist [pair]:* `Show current blacklist, or adds one or more pairs "
"to the blacklist.` \n" "to the blacklist.` \n"

View File

@@ -7,20 +7,20 @@ import warnings
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from datetime import datetime, timezone from datetime import datetime, timezone
from enum import Enum from enum import Enum
from typing import Dict, NamedTuple, Optional, Tuple from typing import Dict, List, NamedTuple, Optional, Tuple
import arrow import arrow
from pandas import DataFrame from pandas import DataFrame
from freqtrade.constants import ListPairsWithTimeframes
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
from freqtrade.exceptions import StrategyError from freqtrade.exceptions import OperationalException, StrategyError
from freqtrade.exchange import timeframe_to_minutes from freqtrade.exchange import timeframe_to_minutes
from freqtrade.exchange.exchange import timeframe_to_next_date
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
from freqtrade.constants import ListPairsWithTimeframes
from freqtrade.wallets import Wallets from freqtrade.wallets import Wallets
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -45,6 +45,10 @@ class SellType(Enum):
EMERGENCY_SELL = "emergency_sell" EMERGENCY_SELL = "emergency_sell"
NONE = "" NONE = ""
def __str__(self):
# explicitly convert to String to help with exporting data.
return self.value
class SellCheckTuple(NamedTuple): class SellCheckTuple(NamedTuple):
""" """
@@ -191,6 +195,63 @@ class IStrategy(ABC):
""" """
return False return False
def bot_loop_start(self, **kwargs) -> None:
"""
Called at the start of the bot iteration (one loop).
Might be used to perform pair-independent tasks
(e.g. gather some remote resource for comparison)
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
"""
pass
def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float,
time_in_force: str, **kwargs) -> bool:
"""
Called right before placing a buy order.
Timing for this function is critical, so avoid doing heavy computations or
network requests in this method.
For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/
When not implemented by a strategy, returns True (always confirming).
:param pair: Pair that's about to be bought.
:param order_type: Order type (as configured in order_types). usually limit or market.
:param amount: Amount in target (quote) currency that's going to be traded.
:param rate: Rate that's going to be used when using limit orders
:param time_in_force: Time in force. Defaults to GTC (Good-til-cancelled).
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
:return bool: When True is returned, then the buy-order is placed on the exchange.
False aborts the process
"""
return True
def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float,
rate: float, time_in_force: str, sell_reason: str, **kwargs) -> bool:
"""
Called right before placing a regular sell order.
Timing for this function is critical, so avoid doing heavy computations or
network requests in this method.
For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/
When not implemented by a strategy, returns True (always confirming).
:param pair: Pair that's about to be sold.
:param trade: trade object.
:param order_type: Order type (as configured in order_types). usually limit or market.
:param amount: Amount in quote currency.
:param rate: Rate that's going to be used when using limit orders
:param time_in_force: Time in force. Defaults to GTC (Good-til-cancelled).
:param sell_reason: Sell reason.
Can be any of ['roi', 'stop_loss', 'stoploss_on_exchange', 'trailing_stop_loss',
'sell_signal', 'force_sell', 'emergency_sell']
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
:return bool: When True is returned, then the sell-order is placed on the exchange.
False aborts the process
"""
return True
def informative_pairs(self) -> ListPairsWithTimeframes: def informative_pairs(self) -> ListPairsWithTimeframes:
""" """
Define additional, informative pair/interval combinations to be cached from the exchange. Define additional, informative pair/interval combinations to be cached from the exchange.
@@ -204,6 +265,10 @@ class IStrategy(ABC):
""" """
return [] return []
###
# END - Intended to be overridden by strategy
###
def get_strategy_name(self) -> str: def get_strategy_name(self) -> str:
""" """
Returns strategy class name Returns strategy class name
@@ -233,13 +298,25 @@ class IStrategy(ABC):
if pair in self._pair_locked_until: if pair in self._pair_locked_until:
del self._pair_locked_until[pair] del self._pair_locked_until[pair]
def is_pair_locked(self, pair: str) -> bool: def is_pair_locked(self, pair: str, candle_date: datetime = None) -> bool:
""" """
Checks if a pair is currently locked Checks if a pair is currently locked
The 2nd, optional parameter ensures that locks are applied until the new candle arrives,
and not stop at 14:00:00 - while the next candle arrives at 14:00:02 leaving a gap
of 2 seconds for a buy to happen on an old signal.
:param: pair: "Pair to check"
:param candle_date: Date of the last candle. Optional, defaults to current date
:returns: locking state of the pair in question.
""" """
if pair not in self._pair_locked_until: if pair not in self._pair_locked_until:
return False return False
return self._pair_locked_until[pair] >= datetime.now(timezone.utc) if not candle_date:
return self._pair_locked_until[pair] >= datetime.now(timezone.utc)
else:
# Locking should happen until a new candle arrives
lock_time = timeframe_to_next_date(self.timeframe, candle_date)
# lock_time = candle_date + timedelta(minutes=timeframe_to_minutes(self.timeframe))
return self._pair_locked_until[pair] > lock_time
def analyze_ticker(self, dataframe: DataFrame, metadata: dict) -> DataFrame: def analyze_ticker(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
""" """
@@ -273,6 +350,8 @@ class IStrategy(ABC):
# Defs that only make change on new candle data. # Defs that only make change on new candle data.
dataframe = self.analyze_ticker(dataframe, metadata) dataframe = self.analyze_ticker(dataframe, metadata)
self._last_candle_seen_per_pair[pair] = dataframe.iloc[-1]['date'] self._last_candle_seen_per_pair[pair] = dataframe.iloc[-1]['date']
if self.dp:
self.dp._set_cached_df(pair, self.timeframe, dataframe)
else: else:
logger.debug("Skipping TA Analysis for already analyzed candle") logger.debug("Skipping TA Analysis for already analyzed candle")
dataframe['buy'] = 0 dataframe['buy'] = 0
@@ -284,13 +363,53 @@ class IStrategy(ABC):
return dataframe return dataframe
def analyze_pair(self, pair: str) -> None:
"""
Fetch data for this pair from dataprovider and analyze.
Stores the dataframe into the dataprovider.
The analyzed dataframe is then accessible via `dp.get_analyzed_dataframe()`.
:param pair: Pair to analyze.
"""
if not self.dp:
raise OperationalException("DataProvider not found.")
dataframe = self.dp.ohlcv(pair, self.timeframe)
if not isinstance(dataframe, DataFrame) or dataframe.empty:
logger.warning('Empty candle (OHLCV) data for pair %s', pair)
return
try:
df_len, df_close, df_date = self.preserve_df(dataframe)
dataframe = strategy_safe_wrapper(
self._analyze_ticker_internal, message=""
)(dataframe, {'pair': pair})
self.assert_df(dataframe, df_len, df_close, df_date)
except StrategyError as error:
logger.warning(f"Unable to analyze candle (OHLCV) data for pair {pair}: {error}")
return
if dataframe.empty:
logger.warning('Empty dataframe for pair %s', pair)
return
def analyze(self, pairs: List[str]) -> None:
"""
Analyze all pairs using analyze_pair().
:param pairs: List of pairs to analyze
"""
for pair in pairs:
self.analyze_pair(pair)
@staticmethod @staticmethod
def preserve_df(dataframe: DataFrame) -> Tuple[int, float, datetime]: def preserve_df(dataframe: DataFrame) -> Tuple[int, float, datetime]:
""" keep some data for dataframes """ """ keep some data for dataframes """
return len(dataframe), dataframe["close"].iloc[-1], dataframe["date"].iloc[-1] return len(dataframe), dataframe["close"].iloc[-1], dataframe["date"].iloc[-1]
def assert_df(self, dataframe: DataFrame, df_len: int, df_close: float, df_date: datetime): def assert_df(self, dataframe: DataFrame, df_len: int, df_close: float, df_date: datetime):
""" make sure data is unmodified """ """
Ensure dataframe (length, last candle) was not modified, and has all elements we need.
"""
message = "" message = ""
if df_len != len(dataframe): if df_len != len(dataframe):
message = "length" message = "length"
@@ -304,31 +423,17 @@ class IStrategy(ABC):
else: else:
raise StrategyError(f"Dataframe returned from strategy has mismatching {message}.") raise StrategyError(f"Dataframe returned from strategy has mismatching {message}.")
def get_signal(self, pair: str, interval: str, dataframe: DataFrame) -> Tuple[bool, bool]: def get_signal(self, pair: str, timeframe: str, dataframe: DataFrame) -> Tuple[bool, bool]:
""" """
Calculates current signal based several technical analysis indicators Calculates current signal based based on the buy / sell columns of the dataframe.
Used by Bot to get the signal to buy or sell
:param pair: pair in format ANT/BTC :param pair: pair in format ANT/BTC
:param interval: Interval to use (in min) :param timeframe: timeframe to use
:param dataframe: Dataframe to analyze :param dataframe: Analyzed dataframe to get signal from.
:return: (Buy, Sell) A bool-tuple indicating buy/sell signal :return: (Buy, Sell) A bool-tuple indicating buy/sell signal
""" """
if not isinstance(dataframe, DataFrame) or dataframe.empty: if not isinstance(dataframe, DataFrame) or dataframe.empty:
logger.warning('Empty candle (OHLCV) data for pair %s', pair) logger.warning(f'Empty candle (OHLCV) data for pair {pair}')
return False, False
try:
df_len, df_close, df_date = self.preserve_df(dataframe)
dataframe = strategy_safe_wrapper(
self._analyze_ticker_internal, message=""
)(dataframe, {'pair': pair})
self.assert_df(dataframe, df_len, df_close, df_date)
except StrategyError as error:
logger.warning(f"Unable to analyze candle (OHLCV) data for pair {pair}: {error}")
return False, False
if dataframe.empty:
logger.warning('Empty dataframe for pair %s', pair)
return False, False return False, False
latest_date = dataframe['date'].max() latest_date = dataframe['date'].max()
@@ -337,24 +442,18 @@ class IStrategy(ABC):
latest_date = arrow.get(latest_date) latest_date = arrow.get(latest_date)
# Check if dataframe is out of date # Check if dataframe is out of date
interval_minutes = timeframe_to_minutes(interval) timeframe_minutes = timeframe_to_minutes(timeframe)
offset = self.config.get('exchange', {}).get('outdated_offset', 5) offset = self.config.get('exchange', {}).get('outdated_offset', 5)
if latest_date < (arrow.utcnow().shift(minutes=-(interval_minutes * 2 + offset))): if latest_date < (arrow.utcnow().shift(minutes=-(timeframe_minutes * 2 + offset))):
logger.warning( logger.warning(
'Outdated history for pair %s. Last tick is %s minutes old', 'Outdated history for pair %s. Last tick is %s minutes old',
pair, pair, int((arrow.utcnow() - latest_date).total_seconds() // 60)
(arrow.utcnow() - latest_date).seconds // 60
) )
return False, False return False, False
(buy, sell) = latest[SignalType.BUY.value] == 1, latest[SignalType.SELL.value] == 1 (buy, sell) = latest[SignalType.BUY.value] == 1, latest[SignalType.SELL.value] == 1
logger.debug( logger.debug('trigger: %s (pair=%s) buy=%s sell=%s',
'trigger: %s (pair=%s) buy=%s sell=%s', latest['date'], pair, str(buy), str(sell))
latest['date'],
pair,
str(buy),
str(sell)
)
return buy, sell return buy, sell
def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool, def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool,
@@ -500,7 +599,8 @@ class IStrategy(ABC):
def ohlcvdata_to_dataframe(self, data: Dict[str, DataFrame]) -> Dict[str, DataFrame]: def ohlcvdata_to_dataframe(self, data: Dict[str, DataFrame]) -> Dict[str, DataFrame]:
""" """
Creates a dataframe and populates indicators for given candle (OHLCV) data Populates indicators for given candle (OHLCV) data (for multiple pairs)
Does not run advice_buy or advise_sell!
Used by optimize operations only, not during dry / live runs. Used by optimize operations only, not during dry / live runs.
Using .copy() to get a fresh copy of the dataframe for every strategy run. Using .copy() to get a fresh copy of the dataframe for every strategy run.
Has positive effects on memory usage for whatever reason - also when Has positive effects on memory usage for whatever reason - also when

View File

@@ -5,7 +5,7 @@ from freqtrade.exceptions import StrategyError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def strategy_safe_wrapper(f, message: str = "", default_retval=None): def strategy_safe_wrapper(f, message: str = "", default_retval=None, supress_error=False):
""" """
Wrapper around user-provided methods and functions. Wrapper around user-provided methods and functions.
Caches all exceptions and returns either the default_retval (if it's not None) or raises Caches all exceptions and returns either the default_retval (if it's not None) or raises
@@ -20,7 +20,7 @@ def strategy_safe_wrapper(f, message: str = "", default_retval=None):
f"Strategy caused the following exception: {error}" f"Strategy caused the following exception: {error}"
f"{f}" f"{f}"
) )
if default_retval is None: if default_retval is None and not supress_error:
raise StrategyError(str(error)) from error raise StrategyError(str(error)) from error
return default_retval return default_retval
except Exception as error: except Exception as error:
@@ -28,7 +28,7 @@ def strategy_safe_wrapper(f, message: str = "", default_retval=None):
f"{message}" f"{message}"
f"Unexpected error {error} calling {f}" f"Unexpected error {error} calling {f}"
) )
if default_retval is None: if default_retval is None and not supress_error:
raise StrategyError(str(error)) from error raise StrategyError(str(error)) from error
return default_retval return default_retval

View File

@@ -34,7 +34,7 @@
"# config = Configuration.from_files([\"config.json\"])\n", "# config = Configuration.from_files([\"config.json\"])\n",
"\n", "\n",
"# Define some constants\n", "# Define some constants\n",
"config[\"ticker_interval\"] = \"5m\"\n", "config[\"timeframe\"] = \"5m\"\n",
"# Name of the strategy class\n", "# Name of the strategy class\n",
"config[\"strategy\"] = \"SampleStrategy\"\n", "config[\"strategy\"] = \"SampleStrategy\"\n",
"# Location of the data\n", "# Location of the data\n",
@@ -53,7 +53,7 @@
"from freqtrade.data.history import load_pair_history\n", "from freqtrade.data.history import load_pair_history\n",
"\n", "\n",
"candles = load_pair_history(datadir=data_location,\n", "candles = load_pair_history(datadir=data_location,\n",
" timeframe=config[\"ticker_interval\"],\n", " timeframe=config[\"timeframe\"],\n",
" pair=pair)\n", " pair=pair)\n",
"\n", "\n",
"# Confirm success\n", "# Confirm success\n",
@@ -136,10 +136,51 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from freqtrade.data.btanalysis import load_backtest_data\n", "from freqtrade.data.btanalysis import load_backtest_data, load_backtest_stats\n",
"\n", "\n",
"# Load backtest results\n", "# if backtest_dir points to a directory, it'll automatically load the last backtest file.\n",
"trades = load_backtest_data(config[\"user_data_dir\"] / \"backtest_results/backtest-result.json\")\n", "backtest_dir = config[\"user_data_dir\"] / \"backtest_results\"\n",
"# backtest_dir can also point to a specific file \n",
"# backtest_dir = config[\"user_data_dir\"] / \"backtest_results/backtest-result-2020-07-01_20-04-22.json\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# You can get the full backtest statistics by using the following command.\n",
"# This contains all information used to generate the backtest result.\n",
"stats = load_backtest_stats(backtest_dir)\n",
"\n",
"strategy = 'SampleStrategy'\n",
"# All statistics are available per strategy, so if `--strategy-list` was used during backtest, this will be reflected here as well.\n",
"# Example usages:\n",
"print(stats['strategy'][strategy]['results_per_pair'])\n",
"# Get pairlist used for this backtest\n",
"print(stats['strategy'][strategy]['pairlist'])\n",
"# Get market change (average change of all pairs from start to end of the backtest period)\n",
"print(stats['strategy'][strategy]['market_change'])\n",
"# Maximum drawdown ()\n",
"print(stats['strategy'][strategy]['max_drawdown'])\n",
"# Maximum drawdown start and end\n",
"print(stats['strategy'][strategy]['drawdown_start'])\n",
"print(stats['strategy'][strategy]['drawdown_end'])\n",
"\n",
"\n",
"# Get strategy comparison (only relevant if multiple strategies were compared)\n",
"print(stats['strategy_comparison'])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load backtested trades as dataframe\n",
"trades = load_backtest_data(backtest_dir)\n",
"\n", "\n",
"# Show value-counts per pair\n", "# Show value-counts per pair\n",
"trades.groupby(\"pair\")[\"sell_reason\"].value_counts()" "trades.groupby(\"pair\")[\"sell_reason\"].value_counts()"

View File

@@ -1,4 +1,65 @@
def bot_loop_start(self, **kwargs) -> None:
"""
Called at the start of the bot iteration (one loop).
Might be used to perform pair-independent tasks
(e.g. gather some remote ressource for comparison)
For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/
When not implemented by a strategy, this simply does nothing.
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
"""
pass
def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float,
time_in_force: str, **kwargs) -> bool:
"""
Called right before placing a buy order.
Timing for this function is critical, so avoid doing heavy computations or
network requests in this method.
For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/
When not implemented by a strategy, returns True (always confirming).
:param pair: Pair that's about to be bought.
:param order_type: Order type (as configured in order_types). usually limit or market.
:param amount: Amount in target (quote) currency that's going to be traded.
:param rate: Rate that's going to be used when using limit orders
:param time_in_force: Time in force. Defaults to GTC (Good-til-cancelled).
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
:return bool: When True is returned, then the buy-order is placed on the exchange.
False aborts the process
"""
return True
def confirm_trade_exit(self, pair: str, trade: 'Trade', order_type: str, amount: float,
rate: float, time_in_force: str, sell_reason: str, **kwargs) -> bool:
"""
Called right before placing a regular sell order.
Timing for this function is critical, so avoid doing heavy computations or
network requests in this method.
For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/
When not implemented by a strategy, returns True (always confirming).
:param pair: Pair that's about to be sold.
:param trade: trade object.
:param order_type: Order type (as configured in order_types). usually limit or market.
:param amount: Amount in quote currency.
:param rate: Rate that's going to be used when using limit orders
:param time_in_force: Time in force. Defaults to GTC (Good-til-cancelled).
:param sell_reason: Sell reason.
Can be any of ['roi', 'stop_loss', 'stoploss_on_exchange', 'trailing_stop_loss',
'sell_signal', 'force_sell', 'emergency_sell']
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
:return bool: When True is returned, then the sell-order is placed on the exchange.
False aborts the process
"""
return True
def check_buy_timeout(self, pair: str, trade: 'Trade', order: dict, **kwargs) -> bool: def check_buy_timeout(self, pair: str, trade: 'Trade', order: dict, **kwargs) -> bool:
""" """
Check buy timeout function callback. Check buy timeout function callback.

View File

@@ -222,7 +222,7 @@ def crossed(series1, series2, direction=None):
if isinstance(series1, np.ndarray): if isinstance(series1, np.ndarray):
series1 = pd.Series(series1) series1 = pd.Series(series1)
if isinstance(series2, (float, int, np.ndarray)): if isinstance(series2, (float, int, np.ndarray, np.integer, np.floating)):
series2 = pd.Series(index=series1.index, data=series2) series2 = pd.Series(index=series1.index, data=series2)
if direction is None or direction == "above": if direction is None or direction == "above":

View File

@@ -90,6 +90,9 @@ class Worker:
if state == State.RUNNING: if state == State.RUNNING:
self.freqtrade.startup() self.freqtrade.startup()
if state == State.STOPPED:
self.freqtrade.check_for_open_trades()
# Reset heartbeat timestamp to log the heartbeat message at # Reset heartbeat timestamp to log the heartbeat message at
# first throttling iteration when the state changes # first throttling iteration when the state changes
self._heartbeat_msg = 0 self._heartbeat_msg = 0

View File

@@ -3,6 +3,7 @@ nav:
- Home: index.md - Home: index.md
- Installation Docker: docker.md - Installation Docker: docker.md
- Installation: installation.md - Installation: installation.md
- Freqtrade Basics: bot-basics.md
- Configuration: configuration.md - Configuration: configuration.md
- Strategy Customization: strategy-customization.md - Strategy Customization: strategy-customization.md
- Stoploss: stoploss.md - Stoploss: stoploss.md

View File

@@ -1,17 +1,17 @@
# requirements without requirements installable via conda # requirements without requirements installable via conda
# mainly used for Raspberry pi installs # mainly used for Raspberry pi installs
ccxt==1.30.48 ccxt==1.33.52
SQLAlchemy==1.3.18 SQLAlchemy==1.3.19
python-telegram-bot==12.8 python-telegram-bot==12.8
arrow==0.15.7 arrow==0.16.0
cachetools==4.1.1 cachetools==4.1.1
requests==2.24.0 requests==2.24.0
urllib3==1.25.9 urllib3==1.25.10
wrapt==1.12.1 wrapt==1.12.1
jsonschema==3.2.0 jsonschema==3.2.0
TA-Lib==0.4.18 TA-Lib==0.4.18
tabulate==0.8.7 tabulate==0.8.7
pycoingecko==1.2.0 pycoingecko==1.3.0
jinja2==2.11.2 jinja2==2.11.2
# find first, C search in arrays # find first, C search in arrays
@@ -32,4 +32,4 @@ flask-cors==3.0.8
colorama==0.4.3 colorama==0.4.3
# Building config files interactively # Building config files interactively
questionary==1.5.2 questionary==1.5.2
prompt-toolkit==3.0.5 prompt-toolkit==3.0.6

View File

@@ -3,15 +3,15 @@
-r requirements-plot.txt -r requirements-plot.txt
-r requirements-hyperopt.txt -r requirements-hyperopt.txt
coveralls==2.0.0 coveralls==2.1.2
flake8==3.8.3 flake8==3.8.3
flake8-type-annotations==0.1.0 flake8-type-annotations==0.1.0
flake8-tidy-imports==4.1.0 flake8-tidy-imports==4.1.0
mypy==0.782 mypy==0.782
pytest==5.4.3 pytest==6.0.1
pytest-asyncio==0.14.0 pytest-asyncio==0.14.0
pytest-cov==2.10.0 pytest-cov==2.10.1
pytest-mock==3.1.1 pytest-mock==3.3.0
pytest-random-order==1.0.4 pytest-random-order==1.0.4
# Convert jupyter notebooks to markdown documents # Convert jupyter notebooks to markdown documents

View File

@@ -2,9 +2,9 @@
-r requirements.txt -r requirements.txt
# Required for hyperopt # Required for hyperopt
scipy==1.5.0 scipy==1.5.2
scikit-learn==0.23.1 scikit-learn==0.23.1
scikit-optimize==0.7.4 scikit-optimize==0.7.4
filelock==3.0.12 filelock==3.0.12
joblib==0.15.1 joblib==0.16.0
progressbar2==3.51.4 progressbar2==3.51.4

View File

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

View File

@@ -1,5 +1,5 @@
# Load common requirements # Load common requirements
-r requirements-common.txt -r requirements-common.txt
numpy==1.19.0 numpy==1.19.1
pandas==1.0.5 pandas==1.1.1

View File

@@ -62,6 +62,9 @@ class FtRestClient():
def _get(self, apipath, params: dict = None): def _get(self, apipath, params: dict = None):
return self._call("GET", apipath, params=params) return self._call("GET", apipath, params=params)
def _delete(self, apipath, params: dict = None):
return self._call("DELETE", apipath, params=params)
def _post(self, apipath, params: dict = None, data: dict = None): def _post(self, apipath, params: dict = None, data: dict = None):
return self._call("POST", apipath, params=params, data=data) return self._call("POST", apipath, params=params, data=data)
@@ -156,6 +159,14 @@ class FtRestClient():
""" """
return self._get("show_config") return self._get("show_config")
def logs(self, limit=None):
"""Show latest logs.
:param limit: Limits log messages to the last <limit> logs. No limit to get all the trades.
:return: json object
"""
return self._get("logs", params={"limit": limit} if limit else 0)
def trades(self, limit=None): def trades(self, limit=None):
"""Return trades history. """Return trades history.
@@ -164,6 +175,15 @@ class FtRestClient():
""" """
return self._get("trades", params={"limit": limit} if limit else 0) return self._get("trades", params={"limit": limit} if limit else 0)
def delete_trade(self, trade_id):
"""Delete trade from the database.
Tries to close open orders. Requires manual handling of this asset on the exchange.
:param trade_id: Deletes the trade with this ID from the database.
:return: json object
"""
return self._delete("trades/{}".format(trade_id))
def whitelist(self): def whitelist(self):
"""Show the current whitelist. """Show the current whitelist.
@@ -264,11 +284,11 @@ def main(args):
print_commands() print_commands()
sys.exit() sys.exit()
config = load_config(args["config"]) config = load_config(args['config'])
url = config.get("api_server", {}).get("server_url", "127.0.0.1") url = config.get('api_server', {}).get('server_url', '127.0.0.1')
port = config.get("api_server", {}).get("listen_port", "8080") port = config.get('api_server', {}).get('listen_port', '8080')
username = config.get("api_server", {}).get("username") username = config.get('api_server', {}).get('username')
password = config.get("api_server", {}).get("password") password = config.get('api_server', {}).get('password')
server_url = f"http://{url}:{port}" server_url = f"http://{url}:{port}"
client = FtRestClient(server_url, username, password) client = FtRestClient(server_url, username, password)

View File

@@ -6,12 +6,12 @@ import pytest
from freqtrade.commands import (start_convert_data, start_create_userdir, from freqtrade.commands import (start_convert_data, start_create_userdir,
start_download_data, start_hyperopt_list, start_download_data, start_hyperopt_list,
start_hyperopt_show, start_list_exchanges, start_hyperopt_show, start_list_data,
start_list_hyperopts, start_list_markets, start_list_exchanges, start_list_hyperopts,
start_list_strategies, start_list_timeframes, start_list_markets, start_list_strategies,
start_new_hyperopt, start_new_strategy, start_list_timeframes, start_new_hyperopt,
start_show_trades, start_test_pairlist, start_new_strategy, start_show_trades,
start_trading) start_test_pairlist, start_trading)
from freqtrade.configuration import setup_utils_configuration from freqtrade.configuration import setup_utils_configuration
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.state import RunMode from freqtrade.state import RunMode
@@ -667,7 +667,7 @@ def test_start_list_hyperopts(mocker, caplog, capsys):
args = [ args = [
"list-hyperopts", "list-hyperopts",
"--hyperopt-path", "--hyperopt-path",
str(Path(__file__).parent.parent / "optimize"), str(Path(__file__).parent.parent / "optimize" / "hyperopts"),
"-1" "-1"
] ]
pargs = get_args(args) pargs = get_args(args)
@@ -683,7 +683,7 @@ def test_start_list_hyperopts(mocker, caplog, capsys):
args = [ args = [
"list-hyperopts", "list-hyperopts",
"--hyperopt-path", "--hyperopt-path",
str(Path(__file__).parent.parent / "optimize"), str(Path(__file__).parent.parent / "optimize" / "hyperopts"),
] ]
pargs = get_args(args) pargs = get_args(args)
# pargs['config'] = None # pargs['config'] = None
@@ -692,7 +692,6 @@ def test_start_list_hyperopts(mocker, caplog, capsys):
assert "TestHyperoptLegacy" not in captured.out assert "TestHyperoptLegacy" not in captured.out
assert "legacy_hyperopt.py" not in captured.out assert "legacy_hyperopt.py" not in captured.out
assert "DefaultHyperOpt" in captured.out assert "DefaultHyperOpt" in captured.out
assert "test_hyperopt.py" in captured.out
def test_start_test_pairlist(mocker, caplog, tickers, default_conf, capsys): def test_start_test_pairlist(mocker, caplog, tickers, default_conf, capsys):
@@ -736,7 +735,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--no-details" "--no-details",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -749,7 +748,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--best", "--best",
"--no-details" "--no-details",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -763,7 +762,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--profitable", "--profitable",
"--no-details" "--no-details",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -776,7 +775,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
" 11/12", " 12/12"]) " 11/12", " 12/12"])
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--profitable" "--profitable",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -792,7 +791,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
"hyperopt-list", "hyperopt-list",
"--no-details", "--no-details",
"--no-color", "--no-color",
"--min-trades", "20" "--min-trades", "20",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -806,7 +805,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
"hyperopt-list", "hyperopt-list",
"--profitable", "--profitable",
"--no-details", "--no-details",
"--max-trades", "20" "--max-trades", "20",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -821,7 +820,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
"hyperopt-list", "hyperopt-list",
"--profitable", "--profitable",
"--no-details", "--no-details",
"--min-avg-profit", "0.11" "--min-avg-profit", "0.11",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -835,7 +834,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--no-details", "--no-details",
"--max-avg-profit", "0.10" "--max-avg-profit", "0.10",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -849,7 +848,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--no-details", "--no-details",
"--min-total-profit", "0.4" "--min-total-profit", "0.4",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -863,7 +862,35 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--no-details", "--no-details",
"--max-total-profit", "0.4" "--max-total-profit", "0.4",
]
pargs = get_args(args)
pargs['config'] = None
start_hyperopt_list(pargs)
captured = capsys.readouterr()
assert all(x in captured.out
for x in [" 1/12", " 2/12", " 3/12", " 5/12", " 6/12", " 7/12", " 8/12",
" 9/12", " 11/12"])
assert all(x not in captured.out
for x in [" 4/12", " 10/12", " 12/12"])
args = [
"hyperopt-list",
"--no-details",
"--min-objective", "0.1",
]
pargs = get_args(args)
pargs['config'] = None
start_hyperopt_list(pargs)
captured = capsys.readouterr()
assert all(x in captured.out
for x in [" 10/12"])
assert all(x not in captured.out
for x in [" 1/12", " 2/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12",
" 9/12", " 11/12", " 12/12"])
args = [
"hyperopt-list",
"--no-details",
"--max-objective", "0.1",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -878,7 +905,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
"hyperopt-list", "hyperopt-list",
"--profitable", "--profitable",
"--no-details", "--no-details",
"--min-avg-time", "2000" "--min-avg-time", "2000",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -892,7 +919,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--no-details", "--no-details",
"--max-avg-time", "1500" "--max-avg-time", "1500",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -906,7 +933,7 @@ def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results):
args = [ args = [
"hyperopt-list", "hyperopt-list",
"--no-details", "--no-details",
"--export-csv", "test_file.csv" "--export-csv", "test_file.csv",
] ]
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
@@ -1043,6 +1070,40 @@ def test_convert_data_trades(mocker, testdatadir):
assert trades_mock.call_args[1]['erase'] is False assert trades_mock.call_args[1]['erase'] is False
def test_start_list_data(testdatadir, capsys):
args = [
"list-data",
"--data-format-ohlcv",
"json",
"--datadir",
str(testdatadir),
]
pargs = get_args(args)
pargs['config'] = None
start_list_data(pargs)
captured = capsys.readouterr()
assert "Found 16 pair / timeframe combinations." in captured.out
assert "\n| Pair | Timeframe |\n" in captured.out
assert "\n| UNITTEST/BTC | 1m, 5m, 8m, 30m |\n" in captured.out
args = [
"list-data",
"--data-format-ohlcv",
"json",
"--pairs", "XRP/ETH",
"--datadir",
str(testdatadir),
]
pargs = get_args(args)
pargs['config'] = None
start_list_data(pargs)
captured = capsys.readouterr()
assert "Found 2 pair / timeframe combinations." in captured.out
assert "\n| Pair | Timeframe |\n" in captured.out
assert "UNITTEST/BTC" not in captured.out
assert "\n| XRP/ETH | 1m, 5m |\n" in captured.out
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
def test_show_trades(mocker, fee, capsys, caplog): def test_show_trades(mocker, fee, capsys, caplog):
mocker.patch("freqtrade.persistence.init") mocker.patch("freqtrade.persistence.init")
@@ -1055,7 +1116,7 @@ def test_show_trades(mocker, fee, capsys, caplog):
pargs = get_args(args) pargs = get_args(args)
pargs['config'] = None pargs['config'] = None
start_show_trades(pargs) start_show_trades(pargs)
assert log_has("Printing 3 Trades: ", caplog) assert log_has("Printing 4 Trades: ", caplog)
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Trade(id=1" in captured.out assert "Trade(id=1" in captured.out
assert "Trade(id=2" in captured.out assert "Trade(id=2" in captured.out

View File

@@ -78,7 +78,7 @@ def patch_exchange(mocker, api_mock=None, id='bittrex', mock_markets=True) -> No
def get_patched_exchange(mocker, config, api_mock=None, id='bittrex', def get_patched_exchange(mocker, config, api_mock=None, id='bittrex',
mock_markets=True) -> Exchange: mock_markets=True) -> Exchange:
patch_exchange(mocker, api_mock, id, mock_markets) patch_exchange(mocker, api_mock, id, mock_markets)
config["exchange"]["name"] = id config['exchange']['name'] = id
try: try:
exchange = ExchangeResolver.load_exchange(id, config) exchange = ExchangeResolver.load_exchange(id, config)
except ImportError: except ImportError:
@@ -163,7 +163,7 @@ def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False)) -> None:
:param value: which value IStrategy.get_signal() must return :param value: which value IStrategy.get_signal() must return
:return: None :return: None
""" """
freqtrade.strategy.get_signal = lambda e, s, t: value freqtrade.strategy.get_signal = lambda e, s, x: value
freqtrade.exchange.refresh_latest_ohlcv = lambda p: None freqtrade.exchange.refresh_latest_ohlcv = lambda p: None
@@ -176,11 +176,13 @@ def create_mock_trades(fee):
pair='ETH/BTC', pair='ETH/BTC',
stake_amount=0.001, stake_amount=0.001,
amount=123.0, amount=123.0,
amount_requested=123.0,
fee_open=fee.return_value, fee_open=fee.return_value,
fee_close=fee.return_value, fee_close=fee.return_value,
open_rate=0.123, open_rate=0.123,
exchange='bittrex', exchange='bittrex',
open_order_id='dry_run_buy_12345' open_order_id='dry_run_buy_12345',
strategy='DefaultStrategy',
) )
Trade.session.add(trade) Trade.session.add(trade)
@@ -188,6 +190,7 @@ def create_mock_trades(fee):
pair='ETC/BTC', pair='ETC/BTC',
stake_amount=0.001, stake_amount=0.001,
amount=123.0, amount=123.0,
amount_requested=123.0,
fee_open=fee.return_value, fee_open=fee.return_value,
fee_close=fee.return_value, fee_close=fee.return_value,
open_rate=0.123, open_rate=0.123,
@@ -195,7 +198,22 @@ def create_mock_trades(fee):
close_profit=0.005, close_profit=0.005,
exchange='bittrex', exchange='bittrex',
is_open=False, is_open=False,
open_order_id='dry_run_sell_12345' open_order_id='dry_run_sell_12345',
strategy='DefaultStrategy',
)
Trade.session.add(trade)
trade = Trade(
pair='XRP/BTC',
stake_amount=0.001,
amount=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_rate=0.05,
close_rate=0.06,
close_profit=0.01,
exchange='bittrex',
is_open=False,
) )
Trade.session.add(trade) Trade.session.add(trade)
@@ -204,11 +222,13 @@ def create_mock_trades(fee):
pair='ETC/BTC', pair='ETC/BTC',
stake_amount=0.001, stake_amount=0.001,
amount=123.0, amount=123.0,
amount_requested=124.0,
fee_open=fee.return_value, fee_open=fee.return_value,
fee_close=fee.return_value, fee_close=fee.return_value,
open_rate=0.123, open_rate=0.123,
exchange='bittrex', exchange='bittrex',
open_order_id='prod_buy_12345' open_order_id='prod_buy_12345',
strategy='DefaultStrategy',
) )
Trade.session.add(trade) Trade.session.add(trade)
@@ -661,7 +681,8 @@ def shitcoinmarkets(markets):
Fixture with shitcoin markets - used to test filters in pairlists Fixture with shitcoin markets - used to test filters in pairlists
""" """
shitmarkets = deepcopy(markets) shitmarkets = deepcopy(markets)
shitmarkets.update({'HOT/BTC': { shitmarkets.update({
'HOT/BTC': {
'id': 'HOTBTC', 'id': 'HOTBTC',
'symbol': 'HOT/BTC', 'symbol': 'HOT/BTC',
'base': 'HOT', 'base': 'HOT',
@@ -766,7 +787,32 @@ def shitcoinmarkets(markets):
"spot": True, "spot": True,
"future": False, "future": False,
"active": True "active": True
}, },
'ADADOUBLE/USDT': {
"percentage": True,
"tierBased": False,
"taker": 0.001,
"maker": 0.001,
"precision": {
"base": 8,
"quote": 8,
"amount": 2,
"price": 4
},
"limits": {
},
"id": "ADADOUBLEUSDT",
"symbol": "ADADOUBLE/USDT",
"base": "ADADOUBLE",
"quote": "USDT",
"baseId": "ADADOUBLE",
"quoteId": "USDT",
"info": {},
"type": "spot",
"spot": True,
"future": False,
"active": True
},
}) })
return shitmarkets return shitmarkets
@@ -787,6 +833,7 @@ def limit_buy_order():
'price': 0.00001099, 'price': 0.00001099,
'amount': 90.99181073, 'amount': 90.99181073,
'filled': 90.99181073, 'filled': 90.99181073,
'cost': 0.0009999,
'remaining': 0.0, 'remaining': 0.0,
'status': 'closed' 'status': 'closed'
} }
@@ -1387,6 +1434,28 @@ def tickers():
"quoteVolume": 0.0, "quoteVolume": 0.0,
"info": {} "info": {}
}, },
"ADADOUBLE/USDT": {
"symbol": "ADADOUBLE/USDT",
"timestamp": 1580469388244,
"datetime": "2020-01-31T11:16:28.244Z",
"high": None,
"low": None,
"bid": 0.7305,
"bidVolume": None,
"ask": 0.7342,
"askVolume": None,
"vwap": None,
"open": None,
"close": None,
"last": 0,
"previousClose": None,
"change": None,
"percentage": 2.628,
"average": None,
"baseVolume": 0.0,
"quoteVolume": 0.0,
"info": {}
},
}) })

View File

@@ -6,24 +6,48 @@ from arrow import Arrow
from pandas import DataFrame, DateOffset, Timestamp, to_datetime from pandas import DataFrame, DateOffset, Timestamp, to_datetime
from freqtrade.configuration import TimeRange from freqtrade.configuration import TimeRange
from freqtrade.constants import LAST_BT_RESULT_FN
from freqtrade.data.btanalysis import (BT_DATA_COLUMNS, from freqtrade.data.btanalysis import (BT_DATA_COLUMNS,
analyze_trade_parallelism, analyze_trade_parallelism,
calculate_market_change,
calculate_max_drawdown, calculate_max_drawdown,
combine_dataframes_with_mean, combine_dataframes_with_mean,
create_cum_profit, create_cum_profit,
extract_trades_of_period, extract_trades_of_period,
get_latest_backtest_filename,
load_backtest_data, load_trades, load_backtest_data, load_trades,
load_trades_from_db) load_trades_from_db)
from freqtrade.data.history import load_data, load_pair_history from freqtrade.data.history import load_data, load_pair_history
from freqtrade.optimize.backtesting import BacktestResult
from tests.conftest import create_mock_trades from tests.conftest import create_mock_trades
def test_load_backtest_data(testdatadir): def test_get_latest_backtest_filename(testdatadir, mocker):
with pytest.raises(ValueError, match=r"Directory .* does not exist\."):
get_latest_backtest_filename(testdatadir / 'does_not_exist')
with pytest.raises(ValueError,
match=r"Directory .* does not seem to contain .*"):
get_latest_backtest_filename(testdatadir.parent)
res = get_latest_backtest_filename(testdatadir)
assert res == 'backtest-result_new.json'
res = get_latest_backtest_filename(str(testdatadir))
assert res == 'backtest-result_new.json'
mocker.patch("freqtrade.data.btanalysis.json_load", return_value={})
with pytest.raises(ValueError, match=r"Invalid '.last_result.json' format."):
get_latest_backtest_filename(testdatadir)
def test_load_backtest_data_old_format(testdatadir):
filename = testdatadir / "backtest-result_test.json" filename = testdatadir / "backtest-result_test.json"
bt_data = load_backtest_data(filename) bt_data = load_backtest_data(filename)
assert isinstance(bt_data, DataFrame) assert isinstance(bt_data, DataFrame)
assert list(bt_data.columns) == BT_DATA_COLUMNS + ["profit"] assert list(bt_data.columns) == BT_DATA_COLUMNS + ["profit_abs"]
assert len(bt_data) == 179 assert len(bt_data) == 179
# Test loading from string (must yield same result) # Test loading from string (must yield same result)
@@ -34,6 +58,49 @@ def test_load_backtest_data(testdatadir):
load_backtest_data(str("filename") + "nofile") load_backtest_data(str("filename") + "nofile")
def test_load_backtest_data_new_format(testdatadir):
filename = testdatadir / "backtest-result_new.json"
bt_data = load_backtest_data(filename)
assert isinstance(bt_data, DataFrame)
assert set(bt_data.columns) == set(list(BacktestResult._fields) + ["profit_abs"])
assert len(bt_data) == 179
# Test loading from string (must yield same result)
bt_data2 = load_backtest_data(str(filename))
assert bt_data.equals(bt_data2)
# Test loading from folder (must yield same result)
bt_data3 = load_backtest_data(testdatadir)
assert bt_data.equals(bt_data3)
with pytest.raises(ValueError, match=r"File .* does not exist\."):
load_backtest_data(str("filename") + "nofile")
with pytest.raises(ValueError, match=r"Unknown dataformat."):
load_backtest_data(testdatadir / LAST_BT_RESULT_FN)
def test_load_backtest_data_multi(testdatadir):
filename = testdatadir / "backtest-result_multistrat.json"
for strategy in ('DefaultStrategy', 'TestStrategy'):
bt_data = load_backtest_data(filename, strategy=strategy)
assert isinstance(bt_data, DataFrame)
assert set(bt_data.columns) == set(list(BacktestResult._fields) + ["profit_abs"])
assert len(bt_data) == 179
# Test loading from string (must yield same result)
bt_data2 = load_backtest_data(str(filename), strategy=strategy)
assert bt_data.equals(bt_data2)
with pytest.raises(ValueError, match=r"Strategy XYZ not available in the backtest result\."):
load_backtest_data(filename, strategy='XYZ')
with pytest.raises(ValueError, match=r"Detected backtest result with more than one strategy.*"):
load_backtest_data(filename)
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
def test_load_trades_from_db(default_conf, fee, mocker): def test_load_trades_from_db(default_conf, fee, mocker):
@@ -43,15 +110,19 @@ def test_load_trades_from_db(default_conf, fee, mocker):
trades = load_trades_from_db(db_url=default_conf['db_url']) trades = load_trades_from_db(db_url=default_conf['db_url'])
assert init_mock.call_count == 1 assert init_mock.call_count == 1
assert len(trades) == 3 assert len(trades) == 4
assert isinstance(trades, DataFrame) assert isinstance(trades, DataFrame)
assert "pair" in trades.columns assert "pair" in trades.columns
assert "open_time" in trades.columns assert "open_date" in trades.columns
assert "profit_percent" in trades.columns assert "profit_percent" in trades.columns
for col in BT_DATA_COLUMNS: for col in BT_DATA_COLUMNS:
if col not in ['index', 'open_at_end']: if col not in ['index', 'open_at_end']:
assert col in trades.columns assert col in trades.columns
trades = load_trades_from_db(db_url=default_conf['db_url'], strategy='DefaultStrategy')
assert len(trades) == 3
trades = load_trades_from_db(db_url=default_conf['db_url'], strategy='NoneStrategy')
assert len(trades) == 0
def test_extract_trades_of_period(testdatadir): def test_extract_trades_of_period(testdatadir):
@@ -66,13 +137,13 @@ def test_extract_trades_of_period(testdatadir):
{'pair': [pair, pair, pair, pair], {'pair': [pair, pair, pair, pair],
'profit_percent': [0.0, 0.1, -0.2, -0.5], 'profit_percent': [0.0, 0.1, -0.2, -0.5],
'profit_abs': [0.0, 1, -2, -5], 'profit_abs': [0.0, 1, -2, -5],
'open_time': to_datetime([Arrow(2017, 11, 13, 15, 40, 0).datetime, 'open_date': to_datetime([Arrow(2017, 11, 13, 15, 40, 0).datetime,
Arrow(2017, 11, 14, 9, 41, 0).datetime, Arrow(2017, 11, 14, 9, 41, 0).datetime,
Arrow(2017, 11, 14, 14, 20, 0).datetime, Arrow(2017, 11, 14, 14, 20, 0).datetime,
Arrow(2017, 11, 15, 3, 40, 0).datetime, Arrow(2017, 11, 15, 3, 40, 0).datetime,
], utc=True ], utc=True
), ),
'close_time': to_datetime([Arrow(2017, 11, 13, 16, 40, 0).datetime, 'close_date': to_datetime([Arrow(2017, 11, 13, 16, 40, 0).datetime,
Arrow(2017, 11, 14, 10, 41, 0).datetime, Arrow(2017, 11, 14, 10, 41, 0).datetime,
Arrow(2017, 11, 14, 15, 25, 0).datetime, Arrow(2017, 11, 14, 15, 25, 0).datetime,
Arrow(2017, 11, 15, 3, 55, 0).datetime, Arrow(2017, 11, 15, 3, 55, 0).datetime,
@@ -81,10 +152,10 @@ def test_extract_trades_of_period(testdatadir):
trades1 = extract_trades_of_period(data, trades) trades1 = extract_trades_of_period(data, trades)
# First and last trade are dropped as they are out of range # First and last trade are dropped as they are out of range
assert len(trades1) == 2 assert len(trades1) == 2
assert trades1.iloc[0].open_time == Arrow(2017, 11, 14, 9, 41, 0).datetime assert trades1.iloc[0].open_date == Arrow(2017, 11, 14, 9, 41, 0).datetime
assert trades1.iloc[0].close_time == Arrow(2017, 11, 14, 10, 41, 0).datetime assert trades1.iloc[0].close_date == Arrow(2017, 11, 14, 10, 41, 0).datetime
assert trades1.iloc[-1].open_time == Arrow(2017, 11, 14, 14, 20, 0).datetime assert trades1.iloc[-1].open_date == Arrow(2017, 11, 14, 14, 20, 0).datetime
assert trades1.iloc[-1].close_time == Arrow(2017, 11, 14, 15, 25, 0).datetime assert trades1.iloc[-1].close_date == Arrow(2017, 11, 14, 15, 25, 0).datetime
def test_analyze_trade_parallelism(default_conf, mocker, testdatadir): def test_analyze_trade_parallelism(default_conf, mocker, testdatadir):
@@ -105,7 +176,8 @@ def test_load_trades(default_conf, mocker):
load_trades("DB", load_trades("DB",
db_url=default_conf.get('db_url'), db_url=default_conf.get('db_url'),
exportfilename=default_conf.get('exportfilename'), exportfilename=default_conf.get('exportfilename'),
no_trades=False no_trades=False,
strategy="DefaultStrategy",
) )
assert db_mock.call_count == 1 assert db_mock.call_count == 1
@@ -135,6 +207,14 @@ def test_load_trades(default_conf, mocker):
assert bt_mock.call_count == 0 assert bt_mock.call_count == 0
def test_calculate_market_change(testdatadir):
pairs = ["ETH/BTC", "ADA/BTC"]
data = load_data(datadir=testdatadir, pairs=pairs, timeframe='5m')
result = calculate_market_change(data)
assert isinstance(result, float)
assert pytest.approx(result) == 0.00955514
def test_combine_dataframes_with_mean(testdatadir): def test_combine_dataframes_with_mean(testdatadir):
pairs = ["ETH/BTC", "ADA/BTC"] pairs = ["ETH/BTC", "ADA/BTC"]
data = load_data(datadir=testdatadir, pairs=pairs, timeframe='5m') data = load_data(datadir=testdatadir, pairs=pairs, timeframe='5m')
@@ -165,7 +245,7 @@ def test_create_cum_profit1(testdatadir):
filename = testdatadir / "backtest-result_test.json" filename = testdatadir / "backtest-result_test.json"
bt_data = load_backtest_data(filename) bt_data = load_backtest_data(filename)
# Move close-time to "off" the candle, to make sure the logic still works # Move close-time to "off" the candle, to make sure the logic still works
bt_data.loc[:, 'close_time'] = bt_data.loc[:, 'close_time'] + DateOffset(seconds=20) bt_data.loc[:, 'close_date'] = bt_data.loc[:, 'close_date'] + DateOffset(seconds=20)
timerange = TimeRange.parse_timerange("20180110-20180112") timerange = TimeRange.parse_timerange("20180110-20180112")
df = load_pair_history(pair="TRX/BTC", timeframe='5m', df = load_pair_history(pair="TRX/BTC", timeframe='5m',
@@ -204,11 +284,11 @@ def test_calculate_max_drawdown2():
-0.033961, 0.010680, 0.010886, -0.029274, 0.011178, 0.010693, 0.010711] -0.033961, 0.010680, 0.010886, -0.029274, 0.011178, 0.010693, 0.010711]
dates = [Arrow(2020, 1, 1).shift(days=i) for i in range(len(values))] dates = [Arrow(2020, 1, 1).shift(days=i) for i in range(len(values))]
df = DataFrame(zip(values, dates), columns=['profit', 'open_time']) df = DataFrame(zip(values, dates), columns=['profit', 'open_date'])
# sort by profit and reset index # sort by profit and reset index
df = df.sort_values('profit').reset_index(drop=True) df = df.sort_values('profit').reset_index(drop=True)
df1 = df.copy() df1 = df.copy()
drawdown, h, low = calculate_max_drawdown(df, date_col='open_time', value_col='profit') drawdown, h, low = calculate_max_drawdown(df, date_col='open_date', value_col='profit')
# Ensure df has not been altered. # Ensure df has not been altered.
assert df.equals(df1) assert df.equals(df1)
@@ -217,6 +297,6 @@ def test_calculate_max_drawdown2():
assert h < low assert h < low
assert drawdown == 0.091755 assert drawdown == 0.091755
df = DataFrame(zip(values[:5], dates[:5]), columns=['profit', 'open_time']) df = DataFrame(zip(values[:5], dates[:5]), columns=['profit', 'open_date'])
with pytest.raises(ValueError, match='No losing trade, therefore no drawdown.'): with pytest.raises(ValueError, match='No losing trade, therefore no drawdown.'):
calculate_max_drawdown(df, date_col='open_time', value_col='profit') calculate_max_drawdown(df, date_col='open_date', value_col='profit')

View File

@@ -1,11 +1,12 @@
from datetime import datetime, timezone
from unittest.mock import MagicMock from unittest.mock import MagicMock
from pandas import DataFrame
import pytest import pytest
from pandas import DataFrame
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
from freqtrade.exceptions import ExchangeError, OperationalException
from freqtrade.pairlist.pairlistmanager import PairListManager from freqtrade.pairlist.pairlistmanager import PairListManager
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.state import RunMode from freqtrade.state import RunMode
from tests.conftest import get_patched_exchange from tests.conftest import get_patched_exchange
@@ -164,7 +165,7 @@ def test_ticker(mocker, default_conf, tickers):
assert 'symbol' in res assert 'symbol' in res
assert res['symbol'] == 'ETH/BTC' assert res['symbol'] == 'ETH/BTC'
ticker_mock = MagicMock(side_effect=DependencyException('Pair not found')) ticker_mock = MagicMock(side_effect=ExchangeError('Pair not found'))
mocker.patch("freqtrade.exchange.Exchange.fetch_ticker", ticker_mock) mocker.patch("freqtrade.exchange.Exchange.fetch_ticker", ticker_mock)
exchange = get_patched_exchange(mocker, default_conf) exchange = get_patched_exchange(mocker, default_conf)
dp = DataProvider(default_conf, exchange) dp = DataProvider(default_conf, exchange)
@@ -194,3 +195,29 @@ def test_current_whitelist(mocker, default_conf, tickers):
with pytest.raises(OperationalException): with pytest.raises(OperationalException):
dp = DataProvider(default_conf, exchange) dp = DataProvider(default_conf, exchange)
dp.current_whitelist() dp.current_whitelist()
def test_get_analyzed_dataframe(mocker, default_conf, ohlcv_history):
default_conf["runmode"] = RunMode.DRY_RUN
timeframe = default_conf["timeframe"]
exchange = get_patched_exchange(mocker, default_conf)
dp = DataProvider(default_conf, exchange)
dp._set_cached_df("XRP/BTC", timeframe, ohlcv_history)
dp._set_cached_df("UNITTEST/BTC", timeframe, ohlcv_history)
assert dp.runmode == RunMode.DRY_RUN
dataframe, time = dp.get_analyzed_dataframe("UNITTEST/BTC", timeframe)
assert ohlcv_history.equals(dataframe)
assert isinstance(time, datetime)
dataframe, time = dp.get_analyzed_dataframe("XRP/BTC", timeframe)
assert ohlcv_history.equals(dataframe)
assert isinstance(time, datetime)
dataframe, time = dp.get_analyzed_dataframe("NOTHING/BTC", timeframe)
assert dataframe.empty
assert isinstance(time, datetime)
assert time == datetime(1970, 1, 1, tzinfo=timezone.utc)

View File

@@ -36,7 +36,7 @@ def _backup_file(file: Path, copy_file: bool = False) -> None:
""" """
Backup existing file to avoid deleting the user file Backup existing file to avoid deleting the user file
:param file: complete path to the file :param file: complete path to the file
:param touch_file: create an empty file in replacement :param copy_file: keep file in place too.
:return: None :return: None
""" """
file_swp = str(file) + '.swp' file_swp = str(file) + '.swp'
@@ -631,6 +631,20 @@ def test_jsondatahandler_ohlcv_get_pairs(testdatadir):
assert set(pairs) == {'UNITTEST/BTC'} assert set(pairs) == {'UNITTEST/BTC'}
def test_jsondatahandler_ohlcv_get_available_data(testdatadir):
paircombs = JsonDataHandler.ohlcv_get_available_data(testdatadir)
# Convert to set to avoid failures due to sorting
assert set(paircombs) == {('UNITTEST/BTC', '5m'), ('ETH/BTC', '5m'), ('XLM/BTC', '5m'),
('TRX/BTC', '5m'), ('LTC/BTC', '5m'), ('XMR/BTC', '5m'),
('ZEC/BTC', '5m'), ('UNITTEST/BTC', '1m'), ('ADA/BTC', '5m'),
('ETC/BTC', '5m'), ('NXT/BTC', '5m'), ('DASH/BTC', '5m'),
('XRP/ETH', '1m'), ('XRP/ETH', '5m'), ('UNITTEST/BTC', '30m'),
('UNITTEST/BTC', '8m')}
paircombs = JsonGzDataHandler.ohlcv_get_available_data(testdatadir)
assert set(paircombs) == {('UNITTEST/BTC', '8m')}
def test_jsondatahandler_trades_get_pairs(testdatadir): def test_jsondatahandler_trades_get_pairs(testdatadir):
pairs = JsonGzDataHandler.trades_get_pairs(testdatadir) pairs = JsonGzDataHandler.trades_get_pairs(testdatadir)
# Convert to set to avoid failures due to sorting # Convert to set to avoid failures due to sorting

View File

@@ -163,8 +163,8 @@ def test_edge_results(edge_conf, mocker, caplog, data) -> None:
for c, trade in enumerate(data.trades): for c, trade in enumerate(data.trades):
res = results.iloc[c] res = results.iloc[c]
assert res.exit_type == trade.sell_reason assert res.exit_type == trade.sell_reason
assert res.open_time == _get_frame_time_from_offset(trade.open_tick).replace(tzinfo=None) assert res.open_date == _get_frame_time_from_offset(trade.open_tick).replace(tzinfo=None)
assert res.close_time == _get_frame_time_from_offset(trade.close_tick).replace(tzinfo=None) assert res.close_date == _get_frame_time_from_offset(trade.close_tick).replace(tzinfo=None)
def test_adjust(mocker, edge_conf): def test_adjust(mocker, edge_conf):
@@ -354,10 +354,8 @@ def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectanc
'stoploss': -0.9, 'stoploss': -0.9,
'profit_percent': '', 'profit_percent': '',
'profit_abs': '', 'profit_abs': '',
'open_time': np.datetime64('2018-10-03T00:05:00.000000000'), 'open_date': np.datetime64('2018-10-03T00:05:00.000000000'),
'close_time': np.datetime64('2018-10-03T00:10:00.000000000'), 'close_date': np.datetime64('2018-10-03T00:10:00.000000000'),
'open_index': 1,
'close_index': 1,
'trade_duration': '', 'trade_duration': '',
'open_rate': 17, 'open_rate': 17,
'close_rate': 17, 'close_rate': 17,
@@ -367,10 +365,8 @@ def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectanc
'stoploss': -0.9, 'stoploss': -0.9,
'profit_percent': '', 'profit_percent': '',
'profit_abs': '', 'profit_abs': '',
'open_time': np.datetime64('2018-10-03T00:20:00.000000000'), 'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_time': np.datetime64('2018-10-03T00:25:00.000000000'), 'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '', 'trade_duration': '',
'open_rate': 20, 'open_rate': 20,
'close_rate': 20, 'close_rate': 20,
@@ -380,10 +376,8 @@ def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectanc
'stoploss': -0.9, 'stoploss': -0.9,
'profit_percent': '', 'profit_percent': '',
'profit_abs': '', 'profit_abs': '',
'open_time': np.datetime64('2018-10-03T00:30:00.000000000'), 'open_date': np.datetime64('2018-10-03T00:30:00.000000000'),
'close_time': np.datetime64('2018-10-03T00:40:00.000000000'), 'close_date': np.datetime64('2018-10-03T00:40:00.000000000'),
'open_index': 6,
'close_index': 7,
'trade_duration': '', 'trade_duration': '',
'open_rate': 26, 'open_rate': 26,
'close_rate': 34, 'close_rate': 34,
@@ -409,3 +403,98 @@ def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectanc
final = edge._process_expectancy(trades_df) final = edge._process_expectancy(trades_df)
assert len(final) == 0 assert len(final) == 0
assert isinstance(final, dict) assert isinstance(final, dict)
def test_process_expectancy_remove_pumps(mocker, edge_conf, fee,):
edge_conf['edge']['min_trade_number'] = 2
edge_conf['edge']['remove_pumps'] = True
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
freqtrade.exchange.get_fee = fee
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
trades = [
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:05:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:10:00.000000000'),
'open_index': 1,
'close_index': 1,
'trade_duration': '',
'open_rate': 17,
'close_rate': 15,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 20,
'close_rate': 10,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 20,
'close_rate': 10,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 20,
'close_rate': 10,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 20,
'close_rate': 10,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:30:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:40:00.000000000'),
'open_index': 6,
'close_index': 7,
'trade_duration': '',
'open_rate': 26,
'close_rate': 134,
'exit_type': 'sell_signal'}
]
trades_df = DataFrame(trades)
trades_df = edge._fill_calculable_fields(trades_df)
final = edge._process_expectancy(trades_df)
assert 'TEST/BTC' in final
assert final['TEST/BTC'].stoploss == -0.9
assert final['TEST/BTC'].nb_trades == len(trades_df) - 1
assert round(final['TEST/BTC'].winrate, 10) == 0.0

View File

@@ -5,8 +5,9 @@ import ccxt
import pytest import pytest
from freqtrade.exceptions import (DependencyException, InvalidOrderException, from freqtrade.exceptions import (DependencyException, InvalidOrderException,
OperationalException, TemporaryError) OperationalException)
from tests.conftest import get_patched_exchange from tests.conftest import get_patched_exchange
from tests.exchange.test_exchange import ccxt_exceptionhandlers
@pytest.mark.parametrize('limitratio,expected', [ @pytest.mark.parametrize('limitratio,expected', [
@@ -62,15 +63,9 @@ def test_stoploss_order_binance(default_conf, mocker, limitratio, expected):
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
with pytest.raises(TemporaryError): ccxt_exceptionhandlers(mocker, default_conf, api_mock, "binance",
api_mock.create_order = MagicMock(side_effect=ccxt.NetworkError("No connection")) "stoploss", "create_order", retries=1,
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') pair='ETH/BTC', amount=1, stop_price=220, order_types={})
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
with pytest.raises(OperationalException, match=r".*DeadBeef.*"):
api_mock.create_order = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
def test_stoploss_order_dry_run_binance(default_conf, mocker): def test_stoploss_order_dry_run_binance(default_conf, mocker):

View File

@@ -4,18 +4,19 @@ import copy
import logging import logging
from datetime import datetime, timezone from datetime import datetime, timezone
from random import randint from random import randint
from unittest.mock import MagicMock, Mock, PropertyMock from unittest.mock import MagicMock, Mock, PropertyMock, patch
import arrow import arrow
import ccxt import ccxt
import pytest import pytest
from pandas import DataFrame from pandas import DataFrame
from freqtrade.exceptions import (DependencyException, InvalidOrderException, from freqtrade.exceptions import (DDosProtection, DependencyException,
OperationalException, TemporaryError) InvalidOrderException, OperationalException,
TemporaryError)
from freqtrade.exchange import Binance, Exchange, Kraken from freqtrade.exchange import Binance, Exchange, Kraken
from freqtrade.exchange.common import API_RETRY_COUNT from freqtrade.exchange.common import API_RETRY_COUNT, calculate_backoff
from freqtrade.exchange.exchange import (market_is_active, symbol_is_pair, from freqtrade.exchange.exchange import (market_is_active,
timeframe_to_minutes, timeframe_to_minutes,
timeframe_to_msecs, timeframe_to_msecs,
timeframe_to_next_date, timeframe_to_next_date,
@@ -37,12 +38,20 @@ def get_mock_coro(return_value):
def ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name, def ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
fun, mock_ccxt_fun, **kwargs): fun, mock_ccxt_fun, retries=API_RETRY_COUNT + 1, **kwargs):
with patch('freqtrade.exchange.common.time.sleep'):
with pytest.raises(DDosProtection):
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.DDoSProtection("DDos"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
getattr(exchange, fun)(**kwargs)
assert api_mock.__dict__[mock_ccxt_fun].call_count == retries
with pytest.raises(TemporaryError): with pytest.raises(TemporaryError):
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.NetworkError("DeaDBeef")) api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.NetworkError("DeaDBeef"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
getattr(exchange, fun)(**kwargs) getattr(exchange, fun)(**kwargs)
assert api_mock.__dict__[mock_ccxt_fun].call_count == API_RETRY_COUNT + 1 assert api_mock.__dict__[mock_ccxt_fun].call_count == retries
with pytest.raises(OperationalException): with pytest.raises(OperationalException):
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.BaseError("DeadBeef")) api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
@@ -51,12 +60,21 @@ def ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
assert api_mock.__dict__[mock_ccxt_fun].call_count == 1 assert api_mock.__dict__[mock_ccxt_fun].call_count == 1
async def async_ccxt_exception(mocker, default_conf, api_mock, fun, mock_ccxt_fun, **kwargs): async def async_ccxt_exception(mocker, default_conf, api_mock, fun, mock_ccxt_fun,
retries=API_RETRY_COUNT + 1, **kwargs):
with patch('freqtrade.exchange.common.asyncio.sleep', get_mock_coro(None)):
with pytest.raises(DDosProtection):
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.DDoSProtection("Dooh"))
exchange = get_patched_exchange(mocker, default_conf, api_mock)
await getattr(exchange, fun)(**kwargs)
assert api_mock.__dict__[mock_ccxt_fun].call_count == retries
with pytest.raises(TemporaryError): with pytest.raises(TemporaryError):
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.NetworkError("DeadBeef")) api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.NetworkError("DeadBeef"))
exchange = get_patched_exchange(mocker, default_conf, api_mock) exchange = get_patched_exchange(mocker, default_conf, api_mock)
await getattr(exchange, fun)(**kwargs) await getattr(exchange, fun)(**kwargs)
assert api_mock.__dict__[mock_ccxt_fun].call_count == API_RETRY_COUNT + 1 assert api_mock.__dict__[mock_ccxt_fun].call_count == retries
with pytest.raises(OperationalException): with pytest.raises(OperationalException):
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.BaseError("DeadBeef")) api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
@@ -697,13 +715,13 @@ def test_validate_order_types(default_conf, mocker):
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') mocker.patch('freqtrade.exchange.Exchange.validate_timeframes')
mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency')
mocker.patch('freqtrade.exchange.Exchange.name', 'Bittrex') mocker.patch('freqtrade.exchange.Exchange.name', 'Bittrex')
default_conf['order_types'] = { default_conf['order_types'] = {
'buy': 'limit', 'buy': 'limit',
'sell': 'limit', 'sell': 'limit',
'stoploss': 'market', 'stoploss': 'market',
'stoploss_on_exchange': False 'stoploss_on_exchange': False
} }
Exchange(default_conf) Exchange(default_conf)
type(api_mock).has = PropertyMock(return_value={'createMarketOrder': False}) type(api_mock).has = PropertyMock(return_value={'createMarketOrder': False})
@@ -713,9 +731,8 @@ def test_validate_order_types(default_conf, mocker):
'buy': 'limit', 'buy': 'limit',
'sell': 'limit', 'sell': 'limit',
'stoploss': 'market', 'stoploss': 'market',
'stoploss_on_exchange': 'false' 'stoploss_on_exchange': False
} }
with pytest.raises(OperationalException, with pytest.raises(OperationalException,
match=r'Exchange .* does not support market orders.'): match=r'Exchange .* does not support market orders.'):
Exchange(default_conf) Exchange(default_conf)
@@ -726,7 +743,6 @@ def test_validate_order_types(default_conf, mocker):
'stoploss': 'limit', 'stoploss': 'limit',
'stoploss_on_exchange': True 'stoploss_on_exchange': True
} }
with pytest.raises(OperationalException, with pytest.raises(OperationalException,
match=r'On exchange stoploss is not supported for .*'): match=r'On exchange stoploss is not supported for .*'):
Exchange(default_conf) Exchange(default_conf)
@@ -1127,9 +1143,10 @@ def test_get_balance_prod(default_conf, mocker, exchange_name):
exchange.get_balance(currency='BTC') exchange.get_balance(currency='BTC')
def test_get_balances_dry_run(default_conf, mocker): @pytest.mark.parametrize("exchange_name", EXCHANGES)
def test_get_balances_dry_run(default_conf, mocker, exchange_name):
default_conf['dry_run'] = True default_conf['dry_run'] = True
exchange = get_patched_exchange(mocker, default_conf) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
assert exchange.get_balances() == {} assert exchange.get_balances() == {}
@@ -1802,7 +1819,7 @@ def test_cancel_order_with_result_error(default_conf, mocker, exchange_name, cap
res = exchange.cancel_order_with_result('1234', 'ETH/BTC', 1541) res = exchange.cancel_order_with_result('1234', 'ETH/BTC', 1541)
assert isinstance(res, dict) assert isinstance(res, dict)
assert log_has("Could not cancel order 1234.", caplog) assert log_has("Could not cancel order 1234 for ETH/BTC.", caplog)
assert log_has("Could not fetch cancelled order 1234.", caplog) assert log_has("Could not fetch cancelled order 1234.", caplog)
assert res['amount'] == 1541 assert res['amount'] == 1541
@@ -1847,36 +1864,48 @@ def test_cancel_stoploss_order(default_conf, mocker, exchange_name):
@pytest.mark.parametrize("exchange_name", EXCHANGES) @pytest.mark.parametrize("exchange_name", EXCHANGES)
def test_get_order(default_conf, mocker, exchange_name): def test_fetch_order(default_conf, mocker, exchange_name):
default_conf['dry_run'] = True default_conf['dry_run'] = True
order = MagicMock() order = MagicMock()
order.myid = 123 order.myid = 123
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
exchange._dry_run_open_orders['X'] = order exchange._dry_run_open_orders['X'] = order
assert exchange.get_order('X', 'TKN/BTC').myid == 123 assert exchange.fetch_order('X', 'TKN/BTC').myid == 123
with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'): with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'):
exchange.get_order('Y', 'TKN/BTC') exchange.fetch_order('Y', 'TKN/BTC')
default_conf['dry_run'] = False default_conf['dry_run'] = False
api_mock = MagicMock() api_mock = MagicMock()
api_mock.fetch_order = MagicMock(return_value=456) api_mock.fetch_order = MagicMock(return_value=456)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
assert exchange.get_order('X', 'TKN/BTC') == 456 assert exchange.fetch_order('X', 'TKN/BTC') == 456
with pytest.raises(InvalidOrderException): with pytest.raises(InvalidOrderException):
api_mock.fetch_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found")) api_mock.fetch_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
exchange.get_order(order_id='_', pair='TKN/BTC') exchange.fetch_order(order_id='_', pair='TKN/BTC')
assert api_mock.fetch_order.call_count == 1 assert api_mock.fetch_order.call_count == 1
api_mock.fetch_order = MagicMock(side_effect=ccxt.OrderNotFound("Order not found"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
with patch('freqtrade.exchange.common.time.sleep') as tm:
with pytest.raises(InvalidOrderException):
exchange.fetch_order(order_id='_', pair='TKN/BTC')
# Ensure backoff is called
assert tm.call_args_list[0][0][0] == 1
assert tm.call_args_list[1][0][0] == 2
assert tm.call_args_list[2][0][0] == 5
assert tm.call_args_list[3][0][0] == 10
assert api_mock.fetch_order.call_count == 6
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name, ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
'get_order', 'fetch_order', 'fetch_order', 'fetch_order', retries=6,
order_id='_', pair='TKN/BTC') order_id='_', pair='TKN/BTC')
@pytest.mark.parametrize("exchange_name", EXCHANGES) @pytest.mark.parametrize("exchange_name", EXCHANGES)
def test_get_stoploss_order(default_conf, mocker, exchange_name): def test_fetch_stoploss_order(default_conf, mocker, exchange_name):
# Don't test FTX here - that needs a seperate test # Don't test FTX here - that needs a seperate test
if exchange_name == 'ftx': if exchange_name == 'ftx':
return return
@@ -1885,25 +1914,26 @@ def test_get_stoploss_order(default_conf, mocker, exchange_name):
order.myid = 123 order.myid = 123
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
exchange._dry_run_open_orders['X'] = order exchange._dry_run_open_orders['X'] = order
assert exchange.get_stoploss_order('X', 'TKN/BTC').myid == 123 assert exchange.fetch_stoploss_order('X', 'TKN/BTC').myid == 123
with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'): with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'):
exchange.get_stoploss_order('Y', 'TKN/BTC') exchange.fetch_stoploss_order('Y', 'TKN/BTC')
default_conf['dry_run'] = False default_conf['dry_run'] = False
api_mock = MagicMock() api_mock = MagicMock()
api_mock.fetch_order = MagicMock(return_value=456) api_mock.fetch_order = MagicMock(return_value=456)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
assert exchange.get_stoploss_order('X', 'TKN/BTC') == 456 assert exchange.fetch_stoploss_order('X', 'TKN/BTC') == 456
with pytest.raises(InvalidOrderException): with pytest.raises(InvalidOrderException):
api_mock.fetch_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found")) api_mock.fetch_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
exchange.get_stoploss_order(order_id='_', pair='TKN/BTC') exchange.fetch_stoploss_order(order_id='_', pair='TKN/BTC')
assert api_mock.fetch_order.call_count == 1 assert api_mock.fetch_order.call_count == 1
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name, ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
'get_stoploss_order', 'fetch_order', 'fetch_stoploss_order', 'fetch_order',
retries=6,
order_id='_', pair='TKN/BTC') order_id='_', pair='TKN/BTC')
@@ -2111,6 +2141,13 @@ def test_get_markets(default_conf, mocker, markets,
assert sorted(pairs.keys()) == sorted(expected_keys) assert sorted(pairs.keys()) == sorted(expected_keys)
def test_get_markets_error(default_conf, mocker):
ex = get_patched_exchange(mocker, default_conf)
mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=None))
with pytest.raises(OperationalException, match="Markets were not loaded."):
ex.get_markets('LTC', 'USDT', True, False)
def test_timeframe_to_minutes(): def test_timeframe_to_minutes():
assert timeframe_to_minutes("5m") == 5 assert timeframe_to_minutes("5m") == 5
assert timeframe_to_minutes("10m") == 10 assert timeframe_to_minutes("10m") == 10
@@ -2182,25 +2219,42 @@ def test_timeframe_to_next_date():
assert timeframe_to_next_date("5m") > date assert timeframe_to_next_date("5m") > date
@pytest.mark.parametrize("market_symbol,base_currency,quote_currency,expected_result", [ @pytest.mark.parametrize("market_symbol,base,quote,exchange,add_dict,expected_result", [
("BTC/USDT", None, None, True), ("BTC/USDT", 'BTC', 'USDT', "binance", {}, True),
("USDT/BTC", None, None, True), ("USDT/BTC", 'USDT', 'BTC', "binance", {}, True),
("BTCUSDT", None, None, False), ("USDT/BTC", 'BTC', 'USDT', "binance", {}, False), # Reversed currencies
("BTC/USDT", None, "USDT", True), ("BTCUSDT", 'BTC', 'USDT', "binance", {}, False), # No seperating /
("USDT/BTC", None, "USDT", False), ("BTCUSDT", None, "USDT", "binance", {}, False), #
("BTCUSDT", None, "USDT", False), ("USDT/BTC", "BTC", None, "binance", {}, False),
("BTC/USDT", "BTC", None, True), ("BTCUSDT", "BTC", None, "binance", {}, False),
("USDT/BTC", "BTC", None, False), ("BTC/USDT", "BTC", "USDT", "binance", {}, True),
("BTCUSDT", "BTC", None, False), ("BTC/USDT", "USDT", "BTC", "binance", {}, False), # reversed currencies
("BTC/USDT", "BTC", "USDT", True), ("BTC/USDT", "BTC", "USD", "binance", {}, False), # Wrong quote currency
("BTC/USDT", "USDT", "BTC", False), ("BTC/", "BTC", 'UNK', "binance", {}, False),
("BTC/USDT", "BTC", "USD", False), ("/USDT", 'UNK', 'USDT', "binance", {}, False),
("BTCUSDT", "BTC", "USDT", False), ("BTC/EUR", 'BTC', 'EUR', "kraken", {"darkpool": False}, True),
("BTC/", None, None, False), ("EUR/BTC", 'EUR', 'BTC', "kraken", {"darkpool": False}, True),
("/USDT", None, None, False), ("EUR/BTC", 'BTC', 'EUR', "kraken", {"darkpool": False}, False), # Reversed currencies
("BTC/EUR", 'BTC', 'USD', "kraken", {"darkpool": False}, False), # wrong quote currency
("BTC/EUR", 'BTC', 'EUR', "kraken", {"darkpool": True}, False), # no darkpools
("BTC/EUR.d", 'BTC', 'EUR', "kraken", {"darkpool": True}, False), # no darkpools
("BTC/USD", 'BTC', 'USD', "ftx", {'spot': True}, True),
("USD/BTC", 'USD', 'BTC', "ftx", {'spot': True}, True),
("BTC/USD", 'BTC', 'USDT', "ftx", {'spot': True}, False), # Wrong quote currency
("BTC/USD", 'USD', 'BTC', "ftx", {'spot': True}, False), # Reversed currencies
("BTC/USD", 'BTC', 'USD', "ftx", {'spot': False}, False), # Can only trade spot markets
("BTC-PERP", 'BTC', 'USD', "ftx", {'spot': False}, False), # Can only trade spot markets
]) ])
def test_symbol_is_pair(market_symbol, base_currency, quote_currency, expected_result) -> None: def test_market_is_tradable(mocker, default_conf, market_symbol, base,
assert symbol_is_pair(market_symbol, base_currency, quote_currency) == expected_result quote, add_dict, exchange, expected_result) -> None:
ex = get_patched_exchange(mocker, default_conf, id=exchange)
market = {
'symbol': market_symbol,
'base': base,
'quote': quote,
**(add_dict),
}
assert ex.market_is_tradable(market) == expected_result
@pytest.mark.parametrize("market,expected_result", [ @pytest.mark.parametrize("market,expected_result", [
@@ -2271,3 +2325,27 @@ def test_calculate_fee_rate(mocker, default_conf, order, expected) -> None:
ex = get_patched_exchange(mocker, default_conf) ex = get_patched_exchange(mocker, default_conf)
assert ex.calculate_fee_rate(order) == expected assert ex.calculate_fee_rate(order) == expected
@pytest.mark.parametrize('retrycount,max_retries,expected', [
(0, 3, 10),
(1, 3, 5),
(2, 3, 2),
(3, 3, 1),
(0, 1, 2),
(1, 1, 1),
(0, 4, 17),
(1, 4, 10),
(2, 4, 5),
(3, 4, 2),
(4, 4, 1),
(0, 5, 26),
(1, 5, 17),
(2, 5, 10),
(3, 5, 5),
(4, 5, 2),
(5, 5, 1),
])
def test_calculate_backoff(retrycount, max_retries, expected):
assert calculate_backoff(retrycount, max_retries) == expected

View File

@@ -6,9 +6,9 @@ from unittest.mock import MagicMock
import ccxt import ccxt
import pytest import pytest
from freqtrade.exceptions import (DependencyException, InvalidOrderException, from freqtrade.exceptions import DependencyException, InvalidOrderException
OperationalException, TemporaryError)
from tests.conftest import get_patched_exchange from tests.conftest import get_patched_exchange
from .test_exchange import ccxt_exceptionhandlers from .test_exchange import ccxt_exceptionhandlers
STOPLOSS_ORDERTYPE = 'stop' STOPLOSS_ORDERTYPE = 'stop'
@@ -85,15 +85,9 @@ def test_stoploss_order_ftx(default_conf, mocker):
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx') exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx')
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
with pytest.raises(TemporaryError): ccxt_exceptionhandlers(mocker, default_conf, api_mock, "ftx",
api_mock.create_order = MagicMock(side_effect=ccxt.NetworkError("No connection")) "stoploss", "create_order", retries=1,
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx') pair='ETH/BTC', amount=1, stop_price=220, order_types={})
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
with pytest.raises(OperationalException, match=r".*DeadBeef.*"):
api_mock.create_order = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx')
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
def test_stoploss_order_dry_run_ftx(default_conf, mocker): def test_stoploss_order_dry_run_ftx(default_conf, mocker):
@@ -130,34 +124,35 @@ def test_stoploss_adjust_ftx(mocker, default_conf):
assert not exchange.stoploss_adjust(1501, order) assert not exchange.stoploss_adjust(1501, order)
def test_get_stoploss_order(default_conf, mocker): def test_fetch_stoploss_order(default_conf, mocker):
default_conf['dry_run'] = True default_conf['dry_run'] = True
order = MagicMock() order = MagicMock()
order.myid = 123 order.myid = 123
exchange = get_patched_exchange(mocker, default_conf, id='ftx') exchange = get_patched_exchange(mocker, default_conf, id='ftx')
exchange._dry_run_open_orders['X'] = order exchange._dry_run_open_orders['X'] = order
assert exchange.get_stoploss_order('X', 'TKN/BTC').myid == 123 assert exchange.fetch_stoploss_order('X', 'TKN/BTC').myid == 123
with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'): with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'):
exchange.get_stoploss_order('Y', 'TKN/BTC') exchange.fetch_stoploss_order('Y', 'TKN/BTC')
default_conf['dry_run'] = False default_conf['dry_run'] = False
api_mock = MagicMock() api_mock = MagicMock()
api_mock.fetch_orders = MagicMock(return_value=[{'id': 'X', 'status': '456'}]) api_mock.fetch_orders = MagicMock(return_value=[{'id': 'X', 'status': '456'}])
exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx') exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx')
assert exchange.get_stoploss_order('X', 'TKN/BTC')['status'] == '456' assert exchange.fetch_stoploss_order('X', 'TKN/BTC')['status'] == '456'
api_mock.fetch_orders = MagicMock(return_value=[{'id': 'Y', 'status': '456'}]) api_mock.fetch_orders = MagicMock(return_value=[{'id': 'Y', 'status': '456'}])
exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx') exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx')
with pytest.raises(InvalidOrderException, match=r"Could not get stoploss order for id X"): with pytest.raises(InvalidOrderException, match=r"Could not get stoploss order for id X"):
exchange.get_stoploss_order('X', 'TKN/BTC')['status'] exchange.fetch_stoploss_order('X', 'TKN/BTC')['status']
with pytest.raises(InvalidOrderException): with pytest.raises(InvalidOrderException):
api_mock.fetch_orders = MagicMock(side_effect=ccxt.InvalidOrder("Order not found")) api_mock.fetch_orders = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx') exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx')
exchange.get_stoploss_order(order_id='_', pair='TKN/BTC') exchange.fetch_stoploss_order(order_id='_', pair='TKN/BTC')
assert api_mock.fetch_orders.call_count == 1 assert api_mock.fetch_orders.call_count == 1
ccxt_exceptionhandlers(mocker, default_conf, api_mock, 'ftx', ccxt_exceptionhandlers(mocker, default_conf, api_mock, 'ftx',
'get_stoploss_order', 'fetch_orders', 'fetch_stoploss_order', 'fetch_orders',
retries=6,
order_id='_', pair='TKN/BTC') order_id='_', pair='TKN/BTC')

View File

@@ -6,8 +6,7 @@ from unittest.mock import MagicMock
import ccxt import ccxt
import pytest import pytest
from freqtrade.exceptions import (DependencyException, InvalidOrderException, from freqtrade.exceptions import DependencyException, InvalidOrderException
OperationalException, TemporaryError)
from tests.conftest import get_patched_exchange from tests.conftest import get_patched_exchange
from tests.exchange.test_exchange import ccxt_exceptionhandlers from tests.exchange.test_exchange import ccxt_exceptionhandlers
@@ -206,15 +205,9 @@ def test_stoploss_order_kraken(default_conf, mocker):
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken')
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
with pytest.raises(TemporaryError): ccxt_exceptionhandlers(mocker, default_conf, api_mock, "kraken",
api_mock.create_order = MagicMock(side_effect=ccxt.NetworkError("No connection")) "stoploss", "create_order", retries=1,
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') pair='ETH/BTC', amount=1, stop_price=220, order_types={})
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
with pytest.raises(OperationalException, match=r".*DeadBeef.*"):
api_mock.create_order = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken')
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
def test_stoploss_order_dry_run_kraken(default_conf, mocker): def test_stoploss_order_dry_run_kraken(default_conf, mocker):

View File

@@ -395,5 +395,5 @@ def test_backtest_results(default_conf, fee, mocker, caplog, data) -> None:
for c, trade in enumerate(data.trades): for c, trade in enumerate(data.trades):
res = results.iloc[c] res = results.iloc[c]
assert res.sell_reason == trade.sell_reason assert res.sell_reason == trade.sell_reason
assert res.open_time == _get_frame_time_from_offset(trade.open_tick) assert res.open_date == _get_frame_time_from_offset(trade.open_tick)
assert res.close_time == _get_frame_time_from_offset(trade.close_tick) assert res.close_date == _get_frame_time_from_offset(trade.close_tick)

View File

@@ -308,6 +308,11 @@ def test_data_with_fee(default_conf, mocker, testdatadir) -> None:
assert backtesting.fee == 0.1234 assert backtesting.fee == 0.1234
assert fee_mock.call_count == 0 assert fee_mock.call_count == 0
default_conf['fee'] = 0.0
backtesting = Backtesting(default_conf)
assert backtesting.fee == 0.0
assert fee_mock.call_count == 0
def test_data_to_dataframe_bt(default_conf, mocker, testdatadir) -> None: def test_data_to_dataframe_bt(default_conf, mocker, testdatadir) -> None:
patch_exchange(mocker) patch_exchange(mocker)
@@ -349,8 +354,8 @@ def test_backtesting_start(default_conf, mocker, testdatadir, caplog) -> None:
exists = [ exists = [
'Using stake_currency: BTC ...', 'Using stake_currency: BTC ...',
'Using stake_amount: 0.001 ...', 'Using stake_amount: 0.001 ...',
'Backtesting with data from 2017-11-14T21:17:00+00:00 ' 'Backtesting with data from 2017-11-14 21:17:00 '
'up to 2017-11-14T22:59:00+00:00 (0 days)..' 'up to 2017-11-14 22:59:00 (0 days)..'
] ]
for line in exists: for line in exists:
assert log_has(line, caplog) assert log_has(line, caplog)
@@ -459,28 +464,29 @@ def test_backtest(default_conf, fee, mocker, testdatadir) -> None:
{'pair': [pair, pair], {'pair': [pair, pair],
'profit_percent': [0.0, 0.0], 'profit_percent': [0.0, 0.0],
'profit_abs': [0.0, 0.0], 'profit_abs': [0.0, 0.0],
'open_time': pd.to_datetime([Arrow(2018, 1, 29, 18, 40, 0).datetime, 'open_date': pd.to_datetime([Arrow(2018, 1, 29, 18, 40, 0).datetime,
Arrow(2018, 1, 30, 3, 30, 0).datetime], utc=True Arrow(2018, 1, 30, 3, 30, 0).datetime], utc=True
), ),
'close_time': pd.to_datetime([Arrow(2018, 1, 29, 22, 35, 0).datetime, 'open_rate': [0.104445, 0.10302485],
'open_fee': [0.0025, 0.0025],
'close_date': pd.to_datetime([Arrow(2018, 1, 29, 22, 35, 0).datetime,
Arrow(2018, 1, 30, 4, 10, 0).datetime], utc=True), Arrow(2018, 1, 30, 4, 10, 0).datetime], utc=True),
'open_index': [78, 184], 'close_rate': [0.104969, 0.103541],
'close_index': [125, 192], 'close_fee': [0.0025, 0.0025],
'amount': [0.00957442, 0.0097064],
'trade_duration': [235, 40], 'trade_duration': [235, 40],
'open_at_end': [False, False], 'open_at_end': [False, False],
'open_rate': [0.104445, 0.10302485],
'close_rate': [0.104969, 0.103541],
'sell_reason': [SellType.ROI, SellType.ROI] 'sell_reason': [SellType.ROI, SellType.ROI]
}) })
pd.testing.assert_frame_equal(results, expected) pd.testing.assert_frame_equal(results, expected)
data_pair = processed[pair] data_pair = processed[pair]
for _, t in results.iterrows(): for _, t in results.iterrows():
ln = data_pair.loc[data_pair["date"] == t["open_time"]] ln = data_pair.loc[data_pair["date"] == t["open_date"]]
# Check open trade rate alignes to open rate # Check open trade rate alignes to open rate
assert ln is not None assert ln is not None
assert round(ln.iloc[0]["open"], 6) == round(t["open_rate"], 6) assert round(ln.iloc[0]["open"], 6) == round(t["open_rate"], 6)
# check close trade rate alignes to close rate or is between high and low # check close trade rate alignes to close rate or is between high and low
ln = data_pair.loc[data_pair["date"] == t["close_time"]] ln = data_pair.loc[data_pair["date"] == t["close_date"]]
assert (round(ln.iloc[0]["open"], 6) == round(t["close_rate"], 6) or assert (round(ln.iloc[0]["open"], 6) == round(t["close_rate"], 6) or
round(ln.iloc[0]["low"], 6) < round( round(ln.iloc[0]["low"], 6) < round(
t["close_rate"], 6) < round(ln.iloc[0]["high"], 6)) t["close_rate"], 6) < round(ln.iloc[0]["high"], 6))
@@ -672,10 +678,10 @@ def test_backtest_start_timerange(default_conf, mocker, caplog, testdatadir):
f'Using data directory: {testdatadir} ...', f'Using data directory: {testdatadir} ...',
'Using stake_currency: BTC ...', 'Using stake_currency: BTC ...',
'Using stake_amount: 0.001 ...', 'Using stake_amount: 0.001 ...',
'Loading data from 2017-11-14T20:57:00+00:00 ' 'Loading data from 2017-11-14 20:57:00 '
'up to 2017-11-14T22:58:00+00:00 (0 days)..', 'up to 2017-11-14 22:58:00 (0 days)..',
'Backtesting with data from 2017-11-14T21:17:00+00:00 ' 'Backtesting with data from 2017-11-14 21:17:00 '
'up to 2017-11-14T22:58:00+00:00 (0 days)..', 'up to 2017-11-14 22:58:00 (0 days)..',
'Parameter --enable-position-stacking detected ...' 'Parameter --enable-position-stacking detected ...'
] ]
@@ -702,6 +708,7 @@ def test_backtest_start_multi_strat(default_conf, mocker, caplog, testdatadir):
generate_pair_metrics=MagicMock(), generate_pair_metrics=MagicMock(),
generate_sell_reason_stats=sell_reason_mock, generate_sell_reason_stats=sell_reason_mock,
generate_strategy_metrics=strat_summary, generate_strategy_metrics=strat_summary,
generate_daily_stats=MagicMock(),
) )
patched_configuration_load_config_file(mocker, default_conf) patched_configuration_load_config_file(mocker, default_conf)
@@ -735,10 +742,10 @@ def test_backtest_start_multi_strat(default_conf, mocker, caplog, testdatadir):
f'Using data directory: {testdatadir} ...', f'Using data directory: {testdatadir} ...',
'Using stake_currency: BTC ...', 'Using stake_currency: BTC ...',
'Using stake_amount: 0.001 ...', 'Using stake_amount: 0.001 ...',
'Loading data from 2017-11-14T20:57:00+00:00 ' 'Loading data from 2017-11-14 20:57:00 '
'up to 2017-11-14T22:58:00+00:00 (0 days)..', 'up to 2017-11-14 22:58:00 (0 days)..',
'Backtesting with data from 2017-11-14T21:17:00+00:00 ' 'Backtesting with data from 2017-11-14 21:17:00 '
'up to 2017-11-14T22:58:00+00:00 (0 days)..', 'up to 2017-11-14 22:58:00 (0 days)..',
'Parameter --enable-position-stacking detected ...', 'Parameter --enable-position-stacking detected ...',
'Running backtesting for Strategy DefaultStrategy', 'Running backtesting for Strategy DefaultStrategy',
'Running backtesting for Strategy TestStrategyLegacy', 'Running backtesting for Strategy TestStrategyLegacy',
@@ -756,13 +763,11 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat
pd.DataFrame({'pair': ['XRP/BTC', 'LTC/BTC'], pd.DataFrame({'pair': ['XRP/BTC', 'LTC/BTC'],
'profit_percent': [0.0, 0.0], 'profit_percent': [0.0, 0.0],
'profit_abs': [0.0, 0.0], 'profit_abs': [0.0, 0.0],
'open_time': pd.to_datetime(['2018-01-29 18:40:00', 'open_date': pd.to_datetime(['2018-01-29 18:40:00',
'2018-01-30 03:30:00', ], utc=True '2018-01-30 03:30:00', ], utc=True
), ),
'close_time': pd.to_datetime(['2018-01-29 20:45:00', 'close_date': pd.to_datetime(['2018-01-29 20:45:00',
'2018-01-30 05:35:00', ], utc=True), '2018-01-30 05:35:00', ], utc=True),
'open_index': [78, 184],
'close_index': [125, 192],
'trade_duration': [235, 40], 'trade_duration': [235, 40],
'open_at_end': [False, False], 'open_at_end': [False, False],
'open_rate': [0.104445, 0.10302485], 'open_rate': [0.104445, 0.10302485],
@@ -772,15 +777,13 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat
pd.DataFrame({'pair': ['XRP/BTC', 'LTC/BTC', 'ETH/BTC'], pd.DataFrame({'pair': ['XRP/BTC', 'LTC/BTC', 'ETH/BTC'],
'profit_percent': [0.03, 0.01, 0.1], 'profit_percent': [0.03, 0.01, 0.1],
'profit_abs': [0.01, 0.02, 0.2], 'profit_abs': [0.01, 0.02, 0.2],
'open_time': pd.to_datetime(['2018-01-29 18:40:00', 'open_date': pd.to_datetime(['2018-01-29 18:40:00',
'2018-01-30 03:30:00', '2018-01-30 03:30:00',
'2018-01-30 05:30:00'], utc=True '2018-01-30 05:30:00'], utc=True
), ),
'close_time': pd.to_datetime(['2018-01-29 20:45:00', 'close_date': pd.to_datetime(['2018-01-29 20:45:00',
'2018-01-30 05:35:00', '2018-01-30 05:35:00',
'2018-01-30 08:30:00'], utc=True), '2018-01-30 08:30:00'], utc=True),
'open_index': [78, 184, 185],
'close_index': [125, 224, 205],
'trade_duration': [47, 40, 20], 'trade_duration': [47, 40, 20],
'open_at_end': [False, False, False], 'open_at_end': [False, False, False],
'open_rate': [0.104445, 0.10302485, 0.122541], 'open_rate': [0.104445, 0.10302485, 0.122541],
@@ -818,10 +821,10 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat
f'Using data directory: {testdatadir} ...', f'Using data directory: {testdatadir} ...',
'Using stake_currency: BTC ...', 'Using stake_currency: BTC ...',
'Using stake_amount: 0.001 ...', 'Using stake_amount: 0.001 ...',
'Loading data from 2017-11-14T20:57:00+00:00 ' 'Loading data from 2017-11-14 20:57:00 '
'up to 2017-11-14T22:58:00+00:00 (0 days)..', 'up to 2017-11-14 22:58:00 (0 days)..',
'Backtesting with data from 2017-11-14T21:17:00+00:00 ' 'Backtesting with data from 2017-11-14 21:17:00 '
'up to 2017-11-14T22:58:00+00:00 (0 days)..', 'up to 2017-11-14 22:58:00 (0 days)..',
'Parameter --enable-position-stacking detected ...', 'Parameter --enable-position-stacking detected ...',
'Running backtesting for Strategy DefaultStrategy', 'Running backtesting for Strategy DefaultStrategy',
'Running backtesting for Strategy TestStrategyLegacy', 'Running backtesting for Strategy TestStrategyLegacy',

View File

@@ -105,3 +105,17 @@ def test_edge_init_fee(mocker, edge_conf) -> None:
edge_cli = EdgeCli(edge_conf) edge_cli = EdgeCli(edge_conf)
assert edge_cli.edge.fee == 0.1234 assert edge_cli.edge.fee == 0.1234
assert fee_mock.call_count == 0 assert fee_mock.call_count == 0
def test_edge_start(mocker, edge_conf) -> None:
mock_calculate = mocker.patch('freqtrade.edge.edge_positioning.Edge.calculate',
return_value=True)
table_mock = mocker.patch('freqtrade.optimize.edge_cli.generate_edge_table')
patch_exchange(mocker)
edge_conf['stake_amount'] = 20
edge_cli = EdgeCli(edge_conf)
edge_cli.start()
assert mock_calculate.call_count == 1
assert table_mock.call_count == 1

View File

@@ -3,6 +3,7 @@ import locale
import logging import logging
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from copy import deepcopy
from typing import Dict, List from typing import Dict, List
from unittest.mock import MagicMock, PropertyMock from unittest.mock import MagicMock, PropertyMock
@@ -16,7 +17,6 @@ from freqtrade.commands.optimize_commands import (setup_optimize_configuration,
start_hyperopt) start_hyperopt)
from freqtrade.data.history import load_data from freqtrade.data.history import load_data
from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.optimize.default_hyperopt import DefaultHyperOpt
from freqtrade.optimize.default_hyperopt_loss import DefaultHyperOptLoss from freqtrade.optimize.default_hyperopt_loss import DefaultHyperOptLoss
from freqtrade.optimize.hyperopt import Hyperopt from freqtrade.optimize.hyperopt import Hyperopt
from freqtrade.resolvers.hyperopt_resolver import (HyperOptLossResolver, from freqtrade.resolvers.hyperopt_resolver import (HyperOptLossResolver,
@@ -26,15 +26,28 @@ from freqtrade.strategy.interface import SellType
from tests.conftest import (get_args, log_has, log_has_re, patch_exchange, from tests.conftest import (get_args, log_has, log_has_re, patch_exchange,
patched_configuration_load_config_file) patched_configuration_load_config_file)
from .hyperopts.default_hyperopt import DefaultHyperOpt
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def hyperopt(default_conf, mocker): def hyperopt_conf(default_conf):
default_conf.update({ hyperconf = deepcopy(default_conf)
'spaces': ['default'], hyperconf.update({
'hyperopt': 'DefaultHyperOpt', 'hyperopt': 'DefaultHyperOpt',
}) 'hyperopt_path': str(Path(__file__).parent / 'hyperopts'),
'epochs': 1,
'timerange': None,
'spaces': ['default'],
'hyperopt_jobs': 1,
})
return hyperconf
@pytest.fixture(scope='function')
def hyperopt(hyperopt_conf, mocker):
patch_exchange(mocker) patch_exchange(mocker)
return Hyperopt(default_conf) return Hyperopt(hyperopt_conf)
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@@ -46,7 +59,7 @@ def hyperopt_results():
'profit_abs': [-0.2, 0.4, 0.6], 'profit_abs': [-0.2, 0.4, 0.6],
'trade_duration': [10, 30, 10], 'trade_duration': [10, 30, 10],
'sell_reason': [SellType.STOP_LOSS, SellType.ROI, SellType.ROI], 'sell_reason': [SellType.STOP_LOSS, SellType.ROI, SellType.ROI],
'close_time': 'close_date':
[ [
datetime(2019, 1, 1, 9, 26, 3, 478039), datetime(2019, 1, 1, 9, 26, 3, 478039),
datetime(2019, 2, 1, 9, 26, 3, 478039), datetime(2019, 2, 1, 9, 26, 3, 478039),
@@ -160,7 +173,7 @@ def test_setup_hyperopt_configuration_with_arguments(mocker, default_conf, caplo
assert log_has('Parameter --print-all detected ...', caplog) assert log_has('Parameter --print-all detected ...', caplog)
def test_setup_hyperopt_configuration_unlimited_stake_amount(mocker, default_conf, caplog) -> None: def test_setup_hyperopt_configuration_unlimited_stake_amount(mocker, default_conf) -> None:
default_conf['stake_amount'] = constants.UNLIMITED_STAKE_AMOUNT default_conf['stake_amount'] = constants.UNLIMITED_STAKE_AMOUNT
patched_configuration_load_config_file(mocker, default_conf) patched_configuration_load_config_file(mocker, default_conf)
@@ -201,7 +214,7 @@ def test_hyperoptresolver(mocker, default_conf, caplog) -> None:
assert hasattr(x, "timeframe") assert hasattr(x, "timeframe")
def test_hyperoptresolver_wrongname(mocker, default_conf, caplog) -> None: def test_hyperoptresolver_wrongname(default_conf) -> None:
default_conf.update({'hyperopt': "NonExistingHyperoptClass"}) default_conf.update({'hyperopt': "NonExistingHyperoptClass"})
with pytest.raises(OperationalException, match=r'Impossible to load Hyperopt.*'): with pytest.raises(OperationalException, match=r'Impossible to load Hyperopt.*'):
@@ -216,7 +229,7 @@ def test_hyperoptresolver_noname(default_conf):
HyperOptResolver.load_hyperopt(default_conf) HyperOptResolver.load_hyperopt(default_conf)
def test_hyperoptlossresolver(mocker, default_conf, caplog) -> None: def test_hyperoptlossresolver(mocker, default_conf) -> None:
hl = DefaultHyperOptLoss hl = DefaultHyperOptLoss
mocker.patch( mocker.patch(
@@ -227,14 +240,14 @@ def test_hyperoptlossresolver(mocker, default_conf, caplog) -> None:
assert hasattr(x, "hyperopt_loss_function") assert hasattr(x, "hyperopt_loss_function")
def test_hyperoptlossresolver_wrongname(mocker, default_conf, caplog) -> None: def test_hyperoptlossresolver_wrongname(default_conf) -> None:
default_conf.update({'hyperopt_loss': "NonExistingLossClass"}) default_conf.update({'hyperopt_loss': "NonExistingLossClass"})
with pytest.raises(OperationalException, match=r'Impossible to load HyperoptLoss.*'): with pytest.raises(OperationalException, match=r'Impossible to load HyperoptLoss.*'):
HyperOptLossResolver.load_hyperoptloss(default_conf) HyperOptLossResolver.load_hyperoptloss(default_conf)
def test_start_not_installed(mocker, default_conf, caplog, import_fails) -> None: def test_start_not_installed(mocker, default_conf, import_fails) -> None:
start_mock = MagicMock() start_mock = MagicMock()
patched_configuration_load_config_file(mocker, default_conf) patched_configuration_load_config_file(mocker, default_conf)
@@ -245,6 +258,8 @@ def test_start_not_installed(mocker, default_conf, caplog, import_fails) -> None
'hyperopt', 'hyperopt',
'--config', 'config.json', '--config', 'config.json',
'--hyperopt', 'DefaultHyperOpt', '--hyperopt', 'DefaultHyperOpt',
'--hyperopt-path',
str(Path(__file__).parent / "hyperopts"),
'--epochs', '5' '--epochs', '5'
] ]
pargs = get_args(args) pargs = get_args(args)
@@ -253,9 +268,9 @@ def test_start_not_installed(mocker, default_conf, caplog, import_fails) -> None
start_hyperopt(pargs) start_hyperopt(pargs)
def test_start(mocker, default_conf, caplog) -> None: def test_start(mocker, hyperopt_conf, caplog) -> None:
start_mock = MagicMock() start_mock = MagicMock()
patched_configuration_load_config_file(mocker, default_conf) patched_configuration_load_config_file(mocker, hyperopt_conf)
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock) mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
patch_exchange(mocker) patch_exchange(mocker)
@@ -272,8 +287,8 @@ def test_start(mocker, default_conf, caplog) -> None:
assert start_mock.call_count == 1 assert start_mock.call_count == 1
def test_start_no_data(mocker, default_conf, caplog) -> None: def test_start_no_data(mocker, hyperopt_conf) -> None:
patched_configuration_load_config_file(mocker, default_conf) patched_configuration_load_config_file(mocker, hyperopt_conf)
mocker.patch('freqtrade.data.history.load_pair_history', MagicMock(return_value=pd.DataFrame)) mocker.patch('freqtrade.data.history.load_pair_history', MagicMock(return_value=pd.DataFrame))
mocker.patch( mocker.patch(
'freqtrade.optimize.hyperopt.get_timerange', 'freqtrade.optimize.hyperopt.get_timerange',
@@ -293,9 +308,9 @@ def test_start_no_data(mocker, default_conf, caplog) -> None:
start_hyperopt(pargs) start_hyperopt(pargs)
def test_start_filelock(mocker, default_conf, caplog) -> None: def test_start_filelock(mocker, hyperopt_conf, caplog) -> None:
start_mock = MagicMock(side_effect=Timeout(Hyperopt.get_lock_filename(default_conf))) start_mock = MagicMock(side_effect=Timeout(Hyperopt.get_lock_filename(hyperopt_conf)))
patched_configuration_load_config_file(mocker, default_conf) patched_configuration_load_config_file(mocker, hyperopt_conf)
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock) mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
patch_exchange(mocker) patch_exchange(mocker)
@@ -519,7 +534,7 @@ def test_roi_table_generation(hyperopt) -> None:
assert hyperopt.custom_hyperopt.generate_roi_table(params) == {0: 6, 15: 3, 25: 1, 30: 0} assert hyperopt.custom_hyperopt.generate_roi_table(params) == {0: 6, 15: 3, 25: 1, 30: 0}
def test_start_calls_optimizer(mocker, default_conf, caplog, capsys) -> None: def test_start_calls_optimizer(mocker, hyperopt_conf, capsys) -> None:
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -545,15 +560,9 @@ def test_start_calls_optimizer(mocker, default_conf, caplog, capsys) -> None:
) )
patch_exchange(mocker) patch_exchange(mocker)
# Co-test loading timeframe from strategy # Co-test loading timeframe from strategy
del default_conf['timeframe'] del hyperopt_conf['timeframe']
default_conf.update({'config': 'config.json.example',
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': 'default',
'hyperopt_jobs': 1, })
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
@@ -569,7 +578,7 @@ def test_start_calls_optimizer(mocker, default_conf, caplog, capsys) -> None:
assert hasattr(hyperopt.backtesting.strategy, "advise_sell") assert hasattr(hyperopt.backtesting.strategy, "advise_sell")
assert hasattr(hyperopt.backtesting.strategy, "advise_buy") assert hasattr(hyperopt.backtesting.strategy, "advise_buy")
assert hasattr(hyperopt, "max_open_trades") assert hasattr(hyperopt, "max_open_trades")
assert hyperopt.max_open_trades == default_conf['max_open_trades'] assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades']
assert hasattr(hyperopt, "position_stacking") assert hasattr(hyperopt, "position_stacking")
@@ -686,13 +695,36 @@ def test_buy_strategy_generator(hyperopt, testdatadir) -> None:
assert 1 in result['buy'] assert 1 in result['buy']
def test_generate_optimizer(mocker, default_conf) -> None: def test_sell_strategy_generator(hyperopt, testdatadir) -> None:
default_conf.update({'config': 'config.json.example', data = load_data(testdatadir, '1m', ['UNITTEST/BTC'], fill_up_missing=True)
'hyperopt': 'DefaultHyperOpt', dataframes = hyperopt.backtesting.strategy.ohlcvdata_to_dataframe(data)
'timerange': None, dataframe = hyperopt.custom_hyperopt.populate_indicators(dataframes['UNITTEST/BTC'],
'spaces': 'all', {'pair': 'UNITTEST/BTC'})
'hyperopt_min_trades': 1,
}) populate_sell_trend = hyperopt.custom_hyperopt.sell_strategy_generator(
{
'sell-adx-value': 20,
'sell-fastd-value': 75,
'sell-mfi-value': 80,
'sell-rsi-value': 20,
'sell-adx-enabled': True,
'sell-fastd-enabled': True,
'sell-mfi-enabled': True,
'sell-rsi-enabled': True,
'sell-trigger': 'sell-bb_upper'
}
)
result = populate_sell_trend(dataframe, {'pair': 'UNITTEST/BTC'})
# Check if some indicators are generated. We will not test all of them
print(result)
assert 'sell' in result
assert 1 in result['sell']
def test_generate_optimizer(mocker, hyperopt_conf) -> None:
hyperopt_conf.update({'spaces': 'all',
'hyperopt_min_trades': 1,
})
trades = [ trades = [
('TRX/BTC', 0.023117, 0.000233, 100) ('TRX/BTC', 0.023117, 0.000233, 100)
@@ -744,8 +776,10 @@ def test_generate_optimizer(mocker, default_conf) -> None:
} }
response_expected = { response_expected = {
'loss': 1.9840569076926293, 'loss': 1.9840569076926293,
'results_explanation': (' 1 trades. Avg profit 2.31%. Total profit 0.00023300 BTC ' 'results_explanation': (' 1 trades. 1/0/0 Wins/Draws/Losses. '
'( 2.31\N{GREEK CAPITAL LETTER SIGMA}%). Avg duration 100.0 min.' 'Avg profit 2.31%. Median profit 2.31%. Total profit '
'0.00023300 BTC ( 2.31\N{GREEK CAPITAL LETTER SIGMA}%). '
'Avg duration 100.0 min.'
).encode(locale.getpreferredencoding(), 'replace').decode('utf-8'), ).encode(locale.getpreferredencoding(), 'replace').decode('utf-8'),
'params_details': {'buy': {'adx-enabled': False, 'params_details': {'buy': {'adx-enabled': False,
'adx-value': 0, 'adx-value': 0,
@@ -776,55 +810,47 @@ def test_generate_optimizer(mocker, default_conf) -> None:
'trailing_stop_positive_offset': 0.07}}, 'trailing_stop_positive_offset': 0.07}},
'params_dict': optimizer_param, 'params_dict': optimizer_param,
'results_metrics': {'avg_profit': 2.3117, 'results_metrics': {'avg_profit': 2.3117,
'draws': 0,
'duration': 100.0, 'duration': 100.0,
'losses': 0,
'winsdrawslosses': '1/0/0',
'median_profit': 2.3117,
'profit': 2.3117, 'profit': 2.3117,
'total_profit': 0.000233, 'total_profit': 0.000233,
'trade_count': 1}, 'trade_count': 1,
'wins': 1},
'total_profit': 0.00023300 'total_profit': 0.00023300
} }
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.dimensions = hyperopt.hyperopt_space() hyperopt.dimensions = hyperopt.hyperopt_space()
generate_optimizer_value = hyperopt.generate_optimizer(list(optimizer_param.values())) generate_optimizer_value = hyperopt.generate_optimizer(list(optimizer_param.values()))
assert generate_optimizer_value == response_expected assert generate_optimizer_value == response_expected
def test_clean_hyperopt(mocker, default_conf, caplog): def test_clean_hyperopt(mocker, hyperopt_conf, caplog):
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example',
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': 'default',
'hyperopt_jobs': 1,
})
mocker.patch("freqtrade.optimize.hyperopt.Path.is_file", MagicMock(return_value=True)) mocker.patch("freqtrade.optimize.hyperopt.Path.is_file", MagicMock(return_value=True))
unlinkmock = mocker.patch("freqtrade.optimize.hyperopt.Path.unlink", MagicMock()) unlinkmock = mocker.patch("freqtrade.optimize.hyperopt.Path.unlink", MagicMock())
h = Hyperopt(default_conf) h = Hyperopt(hyperopt_conf)
assert unlinkmock.call_count == 2 assert unlinkmock.call_count == 2
assert log_has(f"Removing `{h.data_pickle_file}`.", caplog) assert log_has(f"Removing `{h.data_pickle_file}`.", caplog)
def test_continue_hyperopt(mocker, default_conf, caplog): def test_continue_hyperopt(mocker, hyperopt_conf, caplog):
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'hyperopt_continue': True})
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': 'default',
'hyperopt_jobs': 1,
'hyperopt_continue': True
})
mocker.patch("freqtrade.optimize.hyperopt.Path.is_file", MagicMock(return_value=True)) mocker.patch("freqtrade.optimize.hyperopt.Path.is_file", MagicMock(return_value=True))
unlinkmock = mocker.patch("freqtrade.optimize.hyperopt.Path.unlink", MagicMock()) unlinkmock = mocker.patch("freqtrade.optimize.hyperopt.Path.unlink", MagicMock())
Hyperopt(default_conf) Hyperopt(hyperopt_conf)
assert unlinkmock.call_count == 0 assert unlinkmock.call_count == 0
assert log_has("Continuing on previous hyperopt results.", caplog) assert log_has("Continuing on previous hyperopt results.", caplog)
def test_print_json_spaces_all(mocker, default_conf, caplog, capsys) -> None: def test_print_json_spaces_all(mocker, hyperopt_conf, capsys) -> None:
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -855,16 +881,12 @@ def test_print_json_spaces_all(mocker, default_conf, caplog, capsys) -> None:
) )
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'spaces': 'all',
'hyperopt': 'DefaultHyperOpt', 'hyperopt_jobs': 1,
'epochs': 1, 'print_json': True,
'timerange': None, })
'spaces': 'all',
'hyperopt_jobs': 1,
'print_json': True,
})
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
@@ -883,7 +905,7 @@ def test_print_json_spaces_all(mocker, default_conf, caplog, capsys) -> None:
assert dumper.call_count == 2 assert dumper.call_count == 2
def test_print_json_spaces_default(mocker, default_conf, caplog, capsys) -> None: def test_print_json_spaces_default(mocker, hyperopt_conf, capsys) -> None:
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -913,16 +935,9 @@ def test_print_json_spaces_default(mocker, default_conf, caplog, capsys) -> None
) )
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'print_json': True})
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': 'default',
'hyperopt_jobs': 1,
'print_json': True,
})
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
@@ -937,7 +952,7 @@ def test_print_json_spaces_default(mocker, default_conf, caplog, capsys) -> None
assert dumper.call_count == 2 assert dumper.call_count == 2
def test_print_json_spaces_roi_stoploss(mocker, default_conf, caplog, capsys) -> None: def test_print_json_spaces_roi_stoploss(mocker, hyperopt_conf, capsys) -> None:
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -963,16 +978,12 @@ def test_print_json_spaces_roi_stoploss(mocker, default_conf, caplog, capsys) ->
) )
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'spaces': 'roi stoploss',
'hyperopt': 'DefaultHyperOpt', 'hyperopt_jobs': 1,
'epochs': 1, 'print_json': True,
'timerange': None, })
'spaces': 'roi stoploss',
'hyperopt_jobs': 1,
'print_json': True,
})
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
@@ -987,7 +998,7 @@ def test_print_json_spaces_roi_stoploss(mocker, default_conf, caplog, capsys) ->
assert dumper.call_count == 2 assert dumper.call_count == 2
def test_simplified_interface_roi_stoploss(mocker, default_conf, caplog, capsys) -> None: def test_simplified_interface_roi_stoploss(mocker, hyperopt_conf, capsys) -> None:
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -1012,14 +1023,9 @@ def test_simplified_interface_roi_stoploss(mocker, default_conf, caplog, capsys)
) )
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'spaces': 'roi stoploss'})
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': 'roi stoploss',
'hyperopt_jobs': 1, })
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
@@ -1040,11 +1046,11 @@ def test_simplified_interface_roi_stoploss(mocker, default_conf, caplog, capsys)
assert hasattr(hyperopt.backtesting.strategy, "advise_sell") assert hasattr(hyperopt.backtesting.strategy, "advise_sell")
assert hasattr(hyperopt.backtesting.strategy, "advise_buy") assert hasattr(hyperopt.backtesting.strategy, "advise_buy")
assert hasattr(hyperopt, "max_open_trades") assert hasattr(hyperopt, "max_open_trades")
assert hyperopt.max_open_trades == default_conf['max_open_trades'] assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades']
assert hasattr(hyperopt, "position_stacking") assert hasattr(hyperopt, "position_stacking")
def test_simplified_interface_all_failed(mocker, default_conf, caplog, capsys) -> None: def test_simplified_interface_all_failed(mocker, hyperopt_conf) -> None:
mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -1055,14 +1061,9 @@ def test_simplified_interface_all_failed(mocker, default_conf, caplog, capsys) -
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'spaces': 'all', })
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': 'all',
'hyperopt_jobs': 1, })
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
@@ -1075,7 +1076,7 @@ def test_simplified_interface_all_failed(mocker, default_conf, caplog, capsys) -
hyperopt.start() hyperopt.start()
def test_simplified_interface_buy(mocker, default_conf, caplog, capsys) -> None: def test_simplified_interface_buy(mocker, hyperopt_conf, capsys) -> None:
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -1100,14 +1101,9 @@ def test_simplified_interface_buy(mocker, default_conf, caplog, capsys) -> None:
) )
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'spaces': 'buy'})
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': 'buy',
'hyperopt_jobs': 1, })
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
@@ -1128,11 +1124,11 @@ def test_simplified_interface_buy(mocker, default_conf, caplog, capsys) -> None:
assert hasattr(hyperopt.backtesting.strategy, "advise_sell") assert hasattr(hyperopt.backtesting.strategy, "advise_sell")
assert hasattr(hyperopt.backtesting.strategy, "advise_buy") assert hasattr(hyperopt.backtesting.strategy, "advise_buy")
assert hasattr(hyperopt, "max_open_trades") assert hasattr(hyperopt, "max_open_trades")
assert hyperopt.max_open_trades == default_conf['max_open_trades'] assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades']
assert hasattr(hyperopt, "position_stacking") assert hasattr(hyperopt, "position_stacking")
def test_simplified_interface_sell(mocker, default_conf, caplog, capsys) -> None: def test_simplified_interface_sell(mocker, hyperopt_conf, capsys) -> None:
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -1157,14 +1153,9 @@ def test_simplified_interface_sell(mocker, default_conf, caplog, capsys) -> None
) )
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'spaces': 'sell', })
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': 'sell',
'hyperopt_jobs': 1, })
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
@@ -1185,7 +1176,7 @@ def test_simplified_interface_sell(mocker, default_conf, caplog, capsys) -> None
assert hasattr(hyperopt.backtesting.strategy, "advise_sell") assert hasattr(hyperopt.backtesting.strategy, "advise_sell")
assert hasattr(hyperopt.backtesting.strategy, "advise_buy") assert hasattr(hyperopt.backtesting.strategy, "advise_buy")
assert hasattr(hyperopt, "max_open_trades") assert hasattr(hyperopt, "max_open_trades")
assert hyperopt.max_open_trades == default_conf['max_open_trades'] assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades']
assert hasattr(hyperopt, "position_stacking") assert hasattr(hyperopt, "position_stacking")
@@ -1195,7 +1186,7 @@ def test_simplified_interface_sell(mocker, default_conf, caplog, capsys) -> None
('sell_strategy_generator', 'sell'), ('sell_strategy_generator', 'sell'),
('sell_indicator_space', 'sell'), ('sell_indicator_space', 'sell'),
]) ])
def test_simplified_interface_failed(mocker, default_conf, caplog, capsys, method, space) -> None: def test_simplified_interface_failed(mocker, hyperopt_conf, method, space) -> None:
mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock()) mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data', mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
MagicMock(return_value=(MagicMock(), None))) MagicMock(return_value=(MagicMock(), None)))
@@ -1206,14 +1197,9 @@ def test_simplified_interface_failed(mocker, default_conf, caplog, capsys, metho
patch_exchange(mocker) patch_exchange(mocker)
default_conf.update({'config': 'config.json.example', hyperopt_conf.update({'spaces': space})
'hyperopt': 'DefaultHyperOpt',
'epochs': 1,
'timerange': None,
'spaces': space,
'hyperopt_jobs': 1, })
hyperopt = Hyperopt(default_conf) hyperopt = Hyperopt(hyperopt_conf)
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock() hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})

View File

@@ -1,16 +1,29 @@
import re
from datetime import timedelta
from pathlib import Path from pathlib import Path
import pandas as pd import pandas as pd
import pytest import pytest
from arrow import Arrow from arrow import Arrow
from freqtrade.configuration import TimeRange
from freqtrade.constants import LAST_BT_RESULT_FN
from freqtrade.data import history
from freqtrade.data.btanalysis import (get_latest_backtest_filename,
load_backtest_data)
from freqtrade.edge import PairInfo from freqtrade.edge import PairInfo
from freqtrade.optimize.optimize_reports import ( from freqtrade.optimize.optimize_reports import (generate_backtest_stats,
generate_pair_metrics, generate_edge_table, generate_sell_reason_stats, generate_daily_stats,
text_table_bt_results, text_table_sell_reason, generate_strategy_metrics, generate_edge_table,
text_table_strategy, store_backtest_result) generate_pair_metrics,
generate_sell_reason_stats,
generate_strategy_metrics,
store_backtest_stats,
text_table_bt_results,
text_table_sell_reason,
text_table_strategy)
from freqtrade.strategy.interface import SellType from freqtrade.strategy.interface import SellType
from tests.conftest import patch_exchange from tests.data.test_history import _backup_file, _clean_test_file
def test_text_table_bt_results(default_conf, mocker): def test_text_table_bt_results(default_conf, mocker):
@@ -43,6 +56,115 @@ def test_text_table_bt_results(default_conf, mocker):
assert text_table_bt_results(pair_results, stake_currency='BTC') == result_str assert text_table_bt_results(pair_results, stake_currency='BTC') == result_str
def test_generate_backtest_stats(default_conf, testdatadir):
results = {'DefStrat': pd.DataFrame({"pair": ["UNITTEST/BTC", "UNITTEST/BTC",
"UNITTEST/BTC", "UNITTEST/BTC"],
"profit_percent": [0.003312, 0.010801, 0.013803, 0.002780],
"profit_abs": [0.000003, 0.000011, 0.000014, 0.000003],
"open_date": [Arrow(2017, 11, 14, 19, 32, 00).datetime,
Arrow(2017, 11, 14, 21, 36, 00).datetime,
Arrow(2017, 11, 14, 22, 12, 00).datetime,
Arrow(2017, 11, 14, 22, 44, 00).datetime],
"close_date": [Arrow(2017, 11, 14, 21, 35, 00).datetime,
Arrow(2017, 11, 14, 22, 10, 00).datetime,
Arrow(2017, 11, 14, 22, 43, 00).datetime,
Arrow(2017, 11, 14, 22, 58, 00).datetime],
"open_rate": [0.002543, 0.003003, 0.003089, 0.003214],
"close_rate": [0.002546, 0.003014, 0.003103, 0.003217],
"trade_duration": [123, 34, 31, 14],
"open_at_end": [False, False, False, True],
"sell_reason": [SellType.ROI, SellType.STOP_LOSS,
SellType.ROI, SellType.FORCE_SELL]
})}
timerange = TimeRange.parse_timerange('1510688220-1510700340')
min_date = Arrow.fromtimestamp(1510688220)
max_date = Arrow.fromtimestamp(1510700340)
btdata = history.load_data(testdatadir, '1m', ['UNITTEST/BTC'], timerange=timerange,
fill_up_missing=True)
stats = generate_backtest_stats(default_conf, btdata, results, min_date, max_date)
assert isinstance(stats, dict)
assert 'strategy' in stats
assert 'DefStrat' in stats['strategy']
assert 'strategy_comparison' in stats
strat_stats = stats['strategy']['DefStrat']
assert strat_stats['backtest_start'] == min_date.datetime
assert strat_stats['backtest_end'] == max_date.datetime
assert strat_stats['total_trades'] == len(results['DefStrat'])
# Above sample had no loosing trade
assert strat_stats['max_drawdown'] == 0.0
results = {'DefStrat': pd.DataFrame(
{"pair": ["UNITTEST/BTC", "UNITTEST/BTC", "UNITTEST/BTC", "UNITTEST/BTC"],
"profit_percent": [0.003312, 0.010801, -0.013803, 0.002780],
"profit_abs": [0.000003, 0.000011, -0.000014, 0.000003],
"open_date": [Arrow(2017, 11, 14, 19, 32, 00).datetime,
Arrow(2017, 11, 14, 21, 36, 00).datetime,
Arrow(2017, 11, 14, 22, 12, 00).datetime,
Arrow(2017, 11, 14, 22, 44, 00).datetime],
"close_date": [Arrow(2017, 11, 14, 21, 35, 00).datetime,
Arrow(2017, 11, 14, 22, 10, 00).datetime,
Arrow(2017, 11, 14, 22, 43, 00).datetime,
Arrow(2017, 11, 14, 22, 58, 00).datetime],
"open_rate": [0.002543, 0.003003, 0.003089, 0.003214],
"close_rate": [0.002546, 0.003014, 0.0032903, 0.003217],
"trade_duration": [123, 34, 31, 14],
"open_at_end": [False, False, False, True],
"sell_reason": [SellType.ROI, SellType.STOP_LOSS,
SellType.ROI, SellType.FORCE_SELL]
})}
assert strat_stats['max_drawdown'] == 0.0
assert strat_stats['drawdown_start'] == Arrow.fromtimestamp(0).datetime
assert strat_stats['drawdown_end'] == Arrow.fromtimestamp(0).datetime
assert strat_stats['drawdown_end_ts'] == 0
assert strat_stats['drawdown_start_ts'] == 0
assert strat_stats['pairlist'] == ['UNITTEST/BTC']
# Test storing stats
filename = Path(testdatadir / 'btresult.json')
filename_last = Path(testdatadir / LAST_BT_RESULT_FN)
_backup_file(filename_last, copy_file=True)
assert not filename.is_file()
store_backtest_stats(filename, stats)
# get real Filename (it's btresult-<date>.json)
last_fn = get_latest_backtest_filename(filename_last.parent)
assert re.match(r"btresult-.*\.json", last_fn)
filename1 = (testdatadir / last_fn)
assert filename1.is_file()
content = filename1.read_text()
assert 'max_drawdown' in content
assert 'strategy' in content
assert 'pairlist' in content
assert filename_last.is_file()
_clean_test_file(filename_last)
filename1.unlink()
def test_store_backtest_stats(testdatadir, mocker):
dump_mock = mocker.patch('freqtrade.optimize.optimize_reports.file_dump_json')
store_backtest_stats(testdatadir, {})
assert dump_mock.call_count == 2
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
assert str(dump_mock.call_args_list[0][0][0]).startswith(str(testdatadir/'backtest-result'))
dump_mock.reset_mock()
filename = testdatadir / 'testresult.json'
store_backtest_stats(filename, {})
assert dump_mock.call_count == 2
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
# result will be testdatadir / testresult-<timestamp>.json
assert str(dump_mock.call_args_list[0][0][0]).startswith(str(testdatadir / 'testresult'))
def test_generate_pair_metrics(default_conf, mocker): def test_generate_pair_metrics(default_conf, mocker):
results = pd.DataFrame( results = pd.DataFrame(
@@ -68,6 +190,29 @@ def test_generate_pair_metrics(default_conf, mocker):
pytest.approx(pair_results[-1]['profit_sum_pct']) == pair_results[-1]['profit_sum'] * 100) pytest.approx(pair_results[-1]['profit_sum_pct']) == pair_results[-1]['profit_sum'] * 100)
def test_generate_daily_stats(testdatadir):
filename = testdatadir / "backtest-result_new.json"
bt_data = load_backtest_data(filename)
res = generate_daily_stats(bt_data)
assert isinstance(res, dict)
assert round(res['backtest_best_day'], 4) == 0.1796
assert round(res['backtest_worst_day'], 4) == -0.1468
assert res['winning_days'] == 14
assert res['draw_days'] == 4
assert res['losing_days'] == 3
assert res['winner_holding_avg'] == timedelta(seconds=1440)
assert res['loser_holding_avg'] == timedelta(days=1, seconds=21420)
# Select empty dataframe!
res = generate_daily_stats(bt_data.loc[bt_data['open_date'] == '2000-01-01', :])
assert isinstance(res, dict)
assert round(res['backtest_best_day'], 4) == 0.0
assert res['winning_days'] == 0
assert res['draw_days'] == 0
assert res['losing_days'] == 0
def test_text_table_sell_reason(default_conf): def test_text_table_sell_reason(default_conf):
results = pd.DataFrame( results = pd.DataFrame(
@@ -188,77 +333,3 @@ def test_generate_edge_table(edge_conf, mocker):
assert generate_edge_table(results).count('| ETH/BTC |') == 1 assert generate_edge_table(results).count('| ETH/BTC |') == 1
assert generate_edge_table(results).count( assert generate_edge_table(results).count(
'| Risk Reward Ratio | Required Risk Reward | Expectancy |') == 1 '| Risk Reward Ratio | Required Risk Reward | Expectancy |') == 1
def test_backtest_record(default_conf, fee, mocker):
names = []
records = []
patch_exchange(mocker)
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
mocker.patch(
'freqtrade.optimize.optimize_reports.file_dump_json',
new=lambda n, r: (names.append(n), records.append(r))
)
results = {'DefStrat': pd.DataFrame({"pair": ["UNITTEST/BTC", "UNITTEST/BTC",
"UNITTEST/BTC", "UNITTEST/BTC"],
"profit_percent": [0.003312, 0.010801, 0.013803, 0.002780],
"profit_abs": [0.000003, 0.000011, 0.000014, 0.000003],
"open_time": [Arrow(2017, 11, 14, 19, 32, 00).datetime,
Arrow(2017, 11, 14, 21, 36, 00).datetime,
Arrow(2017, 11, 14, 22, 12, 00).datetime,
Arrow(2017, 11, 14, 22, 44, 00).datetime],
"close_time": [Arrow(2017, 11, 14, 21, 35, 00).datetime,
Arrow(2017, 11, 14, 22, 10, 00).datetime,
Arrow(2017, 11, 14, 22, 43, 00).datetime,
Arrow(2017, 11, 14, 22, 58, 00).datetime],
"open_rate": [0.002543, 0.003003, 0.003089, 0.003214],
"close_rate": [0.002546, 0.003014, 0.003103, 0.003217],
"open_index": [1, 119, 153, 185],
"close_index": [118, 151, 184, 199],
"trade_duration": [123, 34, 31, 14],
"open_at_end": [False, False, False, True],
"sell_reason": [SellType.ROI, SellType.STOP_LOSS,
SellType.ROI, SellType.FORCE_SELL]
})}
store_backtest_result(Path("backtest-result.json"), results)
# Assert file_dump_json was only called once
assert names == [Path('backtest-result.json')]
records = records[0]
# Ensure records are of correct type
assert len(records) == 4
# reset test to test with strategy name
names = []
records = []
results['Strat'] = results['DefStrat']
results['Strat2'] = results['DefStrat']
store_backtest_result(Path("backtest-result.json"), results)
assert names == [
Path('backtest-result-DefStrat.json'),
Path('backtest-result-Strat.json'),
Path('backtest-result-Strat2.json'),
]
records = records[0]
# Ensure records are of correct type
assert len(records) == 4
# ('UNITTEST/BTC', 0.00331158, '1510684320', '1510691700', 0, 117)
# Below follows just a typecheck of the schema/type of trade-records
oix = None
for (pair, profit, date_buy, date_sell, buy_index, dur,
openr, closer, open_at_end, sell_reason) in records:
assert pair == 'UNITTEST/BTC'
assert isinstance(profit, float)
# FIX: buy/sell should be converted to ints
assert isinstance(date_buy, float)
assert isinstance(date_sell, float)
assert isinstance(openr, float)
assert isinstance(closer, float)
assert isinstance(open_at_end, bool)
assert isinstance(sell_reason, str)
isinstance(buy_index, pd._libs.tslib.Timestamp)
if oix:
assert buy_index > oix
oix = buy_index
assert dur > 0

View File

@@ -235,7 +235,7 @@ def test_VolumePairList_refresh_empty(mocker, markets_empty, whitelist_conf):
([{"method": "VolumePairList", "number_assets": 5, "sort_key": "bidVolume"}], ([{"method": "VolumePairList", "number_assets": 5, "sort_key": "bidVolume"}],
"BTC", ['HOT/BTC', 'FUEL/BTC', 'XRP/BTC', 'LTC/BTC', 'TKN/BTC']), "BTC", ['HOT/BTC', 'FUEL/BTC', 'XRP/BTC', 'LTC/BTC', 'TKN/BTC']),
([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"}], ([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"}],
"USDT", ['ETH/USDT', 'NANO/USDT', 'ADAHALF/USDT']), "USDT", ['ETH/USDT', 'NANO/USDT', 'ADAHALF/USDT', 'ADADOUBLE/USDT']),
# No pair for ETH, VolumePairList # No pair for ETH, VolumePairList
([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"}], ([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"}],
"ETH", []), "ETH", []),
@@ -275,11 +275,16 @@ def test_VolumePairList_refresh_empty(mocker, markets_empty, whitelist_conf):
([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"}, ([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"},
{"method": "PriceFilter", "low_price_ratio": 0.03}], {"method": "PriceFilter", "low_price_ratio": 0.03}],
"USDT", ['ETH/USDT', 'NANO/USDT']), "USDT", ['ETH/USDT', 'NANO/USDT']),
# Hot is removed by precision_filter, Fuel by low_price_filter. # Hot is removed by precision_filter, Fuel by low_price_ratio, Ripple by min_price.
([{"method": "VolumePairList", "number_assets": 6, "sort_key": "quoteVolume"}, ([{"method": "VolumePairList", "number_assets": 6, "sort_key": "quoteVolume"},
{"method": "PrecisionFilter"}, {"method": "PrecisionFilter"},
{"method": "PriceFilter", "low_price_ratio": 0.02}], {"method": "PriceFilter", "low_price_ratio": 0.02, "min_price": 0.01}],
"BTC", ['ETH/BTC', 'TKN/BTC', 'LTC/BTC', 'XRP/BTC']), "BTC", ['ETH/BTC', 'TKN/BTC', 'LTC/BTC']),
# Hot is removed by precision_filter, Fuel by low_price_ratio, Ethereum by max_price.
([{"method": "VolumePairList", "number_assets": 6, "sort_key": "quoteVolume"},
{"method": "PrecisionFilter"},
{"method": "PriceFilter", "low_price_ratio": 0.02, "max_price": 0.05}],
"BTC", ['TKN/BTC', 'LTC/BTC', 'XRP/BTC']),
# HOT and XRP are removed because below 1250 quoteVolume # HOT and XRP are removed because below 1250 quoteVolume
([{"method": "VolumePairList", "number_assets": 5, ([{"method": "VolumePairList", "number_assets": 5,
"sort_key": "quoteVolume", "min_value": 1250}], "sort_key": "quoteVolume", "min_value": 1250}],
@@ -298,11 +303,11 @@ def test_VolumePairList_refresh_empty(mocker, markets_empty, whitelist_conf):
# ShuffleFilter # ShuffleFilter
([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"}, ([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"},
{"method": "ShuffleFilter", "seed": 77}], {"method": "ShuffleFilter", "seed": 77}],
"USDT", ['ETH/USDT', 'ADAHALF/USDT', 'NANO/USDT']), "USDT", ['ADADOUBLE/USDT', 'ETH/USDT', 'NANO/USDT', 'ADAHALF/USDT']),
# ShuffleFilter, other seed # ShuffleFilter, other seed
([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"}, ([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"},
{"method": "ShuffleFilter", "seed": 42}], {"method": "ShuffleFilter", "seed": 42}],
"USDT", ['NANO/USDT', 'ETH/USDT', 'ADAHALF/USDT']), "USDT", ['ADAHALF/USDT', 'NANO/USDT', 'ADADOUBLE/USDT', 'ETH/USDT']),
# ShuffleFilter, no seed # ShuffleFilter, no seed
([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"}, ([{"method": "VolumePairList", "number_assets": 5, "sort_key": "quoteVolume"},
{"method": "ShuffleFilter"}], {"method": "ShuffleFilter"}],
@@ -319,7 +324,7 @@ def test_VolumePairList_refresh_empty(mocker, markets_empty, whitelist_conf):
"BTC", 'filter_at_the_beginning'), # OperationalException expected "BTC", 'filter_at_the_beginning'), # OperationalException expected
# PriceFilter after StaticPairList # PriceFilter after StaticPairList
([{"method": "StaticPairList"}, ([{"method": "StaticPairList"},
{"method": "PriceFilter", "low_price_ratio": 0.02}], {"method": "PriceFilter", "low_price_ratio": 0.02, "min_price": 0.000001, "max_price": 0.1}],
"BTC", ['ETH/BTC', 'TKN/BTC']), "BTC", ['ETH/BTC', 'TKN/BTC']),
# PriceFilter only # PriceFilter only
([{"method": "PriceFilter", "low_price_ratio": 0.02}], ([{"method": "PriceFilter", "low_price_ratio": 0.02}],
@@ -342,6 +347,9 @@ def test_VolumePairList_refresh_empty(mocker, markets_empty, whitelist_conf):
([{"method": "VolumePairList", "number_assets": 5, "sort_key": "bidVolume"}, ([{"method": "VolumePairList", "number_assets": 5, "sort_key": "bidVolume"},
{"method": "StaticPairList"}], {"method": "StaticPairList"}],
"BTC", 'static_in_the_middle'), "BTC", 'static_in_the_middle'),
([{"method": "VolumePairList", "number_assets": 20, "sort_key": "quoteVolume"},
{"method": "PriceFilter", "low_price_ratio": 0.02}],
"USDT", ['ETH/USDT', 'NANO/USDT']),
]) ])
def test_VolumePairList_whitelist_gen(mocker, whitelist_conf, shitcoinmarkets, tickers, def test_VolumePairList_whitelist_gen(mocker, whitelist_conf, shitcoinmarkets, tickers,
ohlcv_history_list, pairlists, base_currency, ohlcv_history_list, pairlists, base_currency,
@@ -389,13 +397,17 @@ def test_VolumePairList_whitelist_gen(mocker, whitelist_conf, shitcoinmarkets, t
for pairlist in pairlists: for pairlist in pairlists:
if pairlist['method'] == 'AgeFilter' and pairlist['min_days_listed'] and \ if pairlist['method'] == 'AgeFilter' and pairlist['min_days_listed'] and \
len(ohlcv_history_list) <= pairlist['min_days_listed']: len(ohlcv_history_list) <= pairlist['min_days_listed']:
assert log_has_re(r'^Removed .* from whitelist, because age is less than ' assert log_has_re(r'^Removed .* from whitelist, because age .* is less than '
r'.* day.*', caplog) r'.* day.*', caplog)
if pairlist['method'] == 'PrecisionFilter' and whitelist_result: if pairlist['method'] == 'PrecisionFilter' and whitelist_result:
assert log_has_re(r'^Removed .* from whitelist, because stop price .* ' assert log_has_re(r'^Removed .* from whitelist, because stop price .* '
r'would be <= stop limit.*', caplog) r'would be <= stop limit.*', caplog)
if pairlist['method'] == 'PriceFilter' and whitelist_result: if pairlist['method'] == 'PriceFilter' and whitelist_result:
assert (log_has_re(r'^Removed .* from whitelist, because 1 unit is .*%$', caplog) or assert (log_has_re(r'^Removed .* from whitelist, because 1 unit is .*%$', caplog) or
log_has_re(r'^Removed .* from whitelist, '
r'because last price < .*%$', caplog) or
log_has_re(r'^Removed .* from whitelist, '
r'because last price > .*%$', caplog) or
log_has_re(r"^Removed .* from whitelist, because ticker\['last'\] " log_has_re(r"^Removed .* from whitelist, because ticker\['last'\] "
r"is empty.*", caplog)) r"is empty.*", caplog))
if pairlist['method'] == 'VolumePairList': if pairlist['method'] == 'VolumePairList':
@@ -456,7 +468,9 @@ def test_pairlist_class(mocker, whitelist_conf, markets, pairlist):
# BCH/BTC not available # BCH/BTC not available
(['ETH/BTC', 'TKN/BTC', 'BCH/BTC'], "is not compatible with exchange"), (['ETH/BTC', 'TKN/BTC', 'BCH/BTC'], "is not compatible with exchange"),
# BTT/BTC is inactive # BTT/BTC is inactive
(['ETH/BTC', 'TKN/BTC', 'BTT/BTC'], "Market is not active") (['ETH/BTC', 'TKN/BTC', 'BTT/BTC'], "Market is not active"),
# XLTCUSDT is not a valid pair
(['ETH/BTC', 'TKN/BTC', 'XLTCUSDT'], "is not tradable with Freqtrade"),
]) ])
def test__whitelist_for_active_markets(mocker, whitelist_conf, markets, pairlist, whitelist, caplog, def test__whitelist_for_active_markets(mocker, whitelist_conf, markets, pairlist, whitelist, caplog,
log_message, tickers): log_message, tickers):
@@ -524,6 +538,37 @@ def test_volumepairlist_caching(mocker, markets, whitelist_conf, tickers):
assert freqtrade.pairlists._pairlist_handlers[0]._last_refresh == lrf assert freqtrade.pairlists._pairlist_handlers[0]._last_refresh == lrf
def test_agefilter_min_days_listed_too_small(mocker, default_conf, markets, tickers, caplog):
default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 10},
{'method': 'AgeFilter', 'min_days_listed': -1}]
mocker.patch.multiple('freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets),
exchange_has=MagicMock(return_value=True),
get_tickers=tickers
)
with pytest.raises(OperationalException,
match=r'AgeFilter requires min_days_listed to be >= 1'):
get_patched_freqtradebot(mocker, default_conf)
def test_agefilter_min_days_listed_too_large(mocker, default_conf, markets, tickers, caplog):
default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 10},
{'method': 'AgeFilter', 'min_days_listed': 99999}]
mocker.patch.multiple('freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets),
exchange_has=MagicMock(return_value=True),
get_tickers=tickers
)
with pytest.raises(OperationalException,
match=r'AgeFilter requires min_days_listed to not exceed '
r'exchange max request size \([0-9]+\)'):
get_patched_freqtradebot(mocker, default_conf)
def test_agefilter_caching(mocker, markets, whitelist_conf_3, tickers, ohlcv_history_list): def test_agefilter_caching(mocker, markets, whitelist_conf_3, tickers, ohlcv_history_list):
mocker.patch.multiple('freqtrade.exchange.Exchange', mocker.patch.multiple('freqtrade.exchange.Exchange',
@@ -547,6 +592,60 @@ def test_agefilter_caching(mocker, markets, whitelist_conf_3, tickers, ohlcv_his
assert freqtrade.exchange.get_historic_ohlcv.call_count == previous_call_count assert freqtrade.exchange.get_historic_ohlcv.call_count == previous_call_count
@pytest.mark.parametrize("pairlistconfig,desc_expected,exception_expected", [
({"method": "PriceFilter", "low_price_ratio": 0.001, "min_price": 0.00000010,
"max_price": 1.0},
"[{'PriceFilter': 'PriceFilter - Filtering pairs priced below "
"0.1% or below 0.00000010 or above 1.00000000.'}]",
None
),
({"method": "PriceFilter", "low_price_ratio": 0.001, "min_price": 0.00000010},
"[{'PriceFilter': 'PriceFilter - Filtering pairs priced below 0.1% or below 0.00000010.'}]",
None
),
({"method": "PriceFilter", "low_price_ratio": 0.001, "max_price": 1.00010000},
"[{'PriceFilter': 'PriceFilter - Filtering pairs priced below 0.1% or above 1.00010000.'}]",
None
),
({"method": "PriceFilter", "min_price": 0.00002000},
"[{'PriceFilter': 'PriceFilter - Filtering pairs priced below 0.00002000.'}]",
None
),
({"method": "PriceFilter"},
"[{'PriceFilter': 'PriceFilter - No price filters configured.'}]",
None
),
({"method": "PriceFilter", "low_price_ratio": -0.001},
None,
"PriceFilter requires low_price_ratio to be >= 0"
), # OperationalException expected
({"method": "PriceFilter", "min_price": -0.00000010},
None,
"PriceFilter requires min_price to be >= 0"
), # OperationalException expected
({"method": "PriceFilter", "max_price": -1.00010000},
None,
"PriceFilter requires max_price to be >= 0"
), # OperationalException expected
])
def test_pricefilter_desc(mocker, whitelist_conf, markets, pairlistconfig,
desc_expected, exception_expected):
mocker.patch.multiple('freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets),
exchange_has=MagicMock(return_value=True)
)
whitelist_conf['pairlists'] = [pairlistconfig]
if desc_expected is not None:
freqtrade = get_patched_freqtradebot(mocker, whitelist_conf)
short_desc = str(freqtrade.pairlists.short_desc())
assert short_desc == desc_expected
else: # OperationalException expected
with pytest.raises(OperationalException,
match=exception_expected):
freqtrade = get_patched_freqtradebot(mocker, whitelist_conf)
def test_pairlistmanager_no_pairlist(mocker, markets, whitelist_conf, caplog): def test_pairlistmanager_no_pairlist(mocker, markets, whitelist_conf, caplog):
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))

View File

@@ -8,12 +8,13 @@ import pytest
from numpy import isnan from numpy import isnan
from freqtrade.edge import PairInfo from freqtrade.edge import PairInfo
from freqtrade.exceptions import DependencyException, TemporaryError from freqtrade.exceptions import ExchangeError, InvalidOrderException, TemporaryError
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.rpc import RPC, RPCException from freqtrade.rpc import RPC, RPCException
from freqtrade.rpc.fiat_convert import CryptoToFiatConverter from freqtrade.rpc.fiat_convert import CryptoToFiatConverter
from freqtrade.state import State from freqtrade.state import State
from tests.conftest import get_patched_freqtradebot, patch_get_signal, create_mock_trades from tests.conftest import (create_mock_trades, get_patched_freqtradebot,
patch_get_signal)
# Functions for recurrent object patching # Functions for recurrent object patching
@@ -78,7 +79,8 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'open_rate': 1.098e-05, 'open_rate': 1.098e-05,
'close_rate': None, 'close_rate': None,
'current_rate': 1.099e-05, 'current_rate': 1.099e-05,
'amount': 91.07468124, 'amount': 91.07468123,
'amount_requested': 91.07468123,
'stake_amount': 0.001, 'stake_amount': 0.001,
'close_profit': None, 'close_profit': None,
'close_profit_pct': None, 'close_profit_pct': None,
@@ -99,6 +101,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'initial_stop_loss_ratio': -0.1, 'initial_stop_loss_ratio': -0.1,
'stoploss_current_dist': -1.1080000000000002e-06, 'stoploss_current_dist': -1.1080000000000002e-06,
'stoploss_current_dist_ratio': -0.10081893, 'stoploss_current_dist_ratio': -0.10081893,
'stoploss_current_dist_pct': -10.08,
'stoploss_entry_dist': -0.00010475, 'stoploss_entry_dist': -0.00010475,
'stoploss_entry_dist_ratio': -0.10448878, 'stoploss_entry_dist_ratio': -0.10448878,
'open_order': None, 'open_order': None,
@@ -106,7 +109,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
} }
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate', mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate',
MagicMock(side_effect=DependencyException("Pair 'ETH/BTC' not available"))) MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available")))
results = rpc._rpc_trade_status() results = rpc._rpc_trade_status()
assert isnan(results[0]['current_profit']) assert isnan(results[0]['current_profit'])
assert isnan(results[0]['current_rate']) assert isnan(results[0]['current_rate'])
@@ -141,7 +144,8 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'open_rate': 1.098e-05, 'open_rate': 1.098e-05,
'close_rate': None, 'close_rate': None,
'current_rate': ANY, 'current_rate': ANY,
'amount': 91.07468124, 'amount': 91.07468123,
'amount_requested': 91.07468123,
'stake_amount': 0.001, 'stake_amount': 0.001,
'close_profit': None, 'close_profit': None,
'close_profit_pct': None, 'close_profit_pct': None,
@@ -162,6 +166,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'initial_stop_loss_ratio': -0.1, 'initial_stop_loss_ratio': -0.1,
'stoploss_current_dist': ANY, 'stoploss_current_dist': ANY,
'stoploss_current_dist_ratio': ANY, 'stoploss_current_dist_ratio': ANY,
'stoploss_current_dist_pct': ANY,
'stoploss_entry_dist': -0.00010475, 'stoploss_entry_dist': -0.00010475,
'stoploss_entry_dist_ratio': -0.10448878, 'stoploss_entry_dist_ratio': -0.10448878,
'open_order': None, 'open_order': None,
@@ -209,7 +214,7 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
assert '-0.41% (-0.06)' == result[0][3] assert '-0.41% (-0.06)' == result[0][3]
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate', mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate',
MagicMock(side_effect=DependencyException("Pair 'ETH/BTC' not available"))) MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available")))
result, headers = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') result, headers = rpc._rpc_status_table(default_conf['stake_currency'], 'USD')
assert 'instantly' == result[0][2] assert 'instantly' == result[0][2]
assert 'ETH/BTC' in result[0][1] assert 'ETH/BTC' in result[0][1]
@@ -252,11 +257,11 @@ def test_rpc_daily_profit(default_conf, update, ticker, fee,
assert days['fiat_display_currency'] == default_conf['fiat_display_currency'] assert days['fiat_display_currency'] == default_conf['fiat_display_currency']
for day in days['data']: for day in days['data']:
# [datetime.date(2018, 1, 11), '0.00000000 BTC', '0.000 USD'] # [datetime.date(2018, 1, 11), '0.00000000 BTC', '0.000 USD']
assert (day['abs_profit'] == '0.00000000' or assert (day['abs_profit'] == 0.0 or
day['abs_profit'] == '0.00006217') day['abs_profit'] == 0.00006217)
assert (day['fiat_value'] == '0.000' or assert (day['fiat_value'] == 0.0 or
day['fiat_value'] == '0.767') day['fiat_value'] == 0.76748865)
# ensure first day is current date # ensure first day is current date
assert str(days['data'][0]['date']) == str(datetime.utcnow().date()) assert str(days['data'][0]['date']) == str(datetime.utcnow().date())
@@ -283,12 +288,66 @@ def test_rpc_trade_history(mocker, default_conf, markets, fee):
assert isinstance(trades['trades'][1], dict) assert isinstance(trades['trades'][1], dict)
trades = rpc._rpc_trade_history(0) trades = rpc._rpc_trade_history(0)
assert len(trades['trades']) == 3 assert len(trades['trades']) == 2
assert trades['trades_count'] == 3 assert trades['trades_count'] == 2
# The first trade is for ETH ... sorting is descending # The first closed trade is for ETC ... sorting is descending
assert trades['trades'][-1]['pair'] == 'ETH/BTC' assert trades['trades'][-1]['pair'] == 'ETC/BTC'
assert trades['trades'][0]['pair'] == 'ETC/BTC' assert trades['trades'][0]['pair'] == 'XRP/BTC'
assert trades['trades'][1]['pair'] == 'ETC/BTC'
def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog):
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
stoploss_mock = MagicMock()
cancel_mock = MagicMock()
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets),
cancel_order=cancel_mock,
cancel_stoploss_order=stoploss_mock,
)
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
freqtradebot.strategy.order_types['stoploss_on_exchange'] = True
create_mock_trades(fee)
rpc = RPC(freqtradebot)
with pytest.raises(RPCException, match='invalid argument'):
rpc._rpc_delete('200')
create_mock_trades(fee)
trades = Trade.query.all()
trades[1].stoploss_order_id = '1234'
trades[2].stoploss_order_id = '1234'
assert len(trades) > 2
res = rpc._rpc_delete('1')
assert isinstance(res, dict)
assert res['result'] == 'success'
assert res['trade_id'] == '1'
assert res['cancel_order_count'] == 1
assert cancel_mock.call_count == 1
assert stoploss_mock.call_count == 0
cancel_mock.reset_mock()
stoploss_mock.reset_mock()
res = rpc._rpc_delete('2')
assert isinstance(res, dict)
assert cancel_mock.call_count == 1
assert stoploss_mock.call_count == 1
assert res['cancel_order_count'] == 2
stoploss_mock = mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order',
side_effect=InvalidOrderException)
res = rpc._rpc_delete('3')
assert stoploss_mock.call_count == 1
stoploss_mock.reset_mock()
cancel_mock = mocker.patch('freqtrade.exchange.Exchange.cancel_order',
side_effect=InvalidOrderException)
res = rpc._rpc_delete('4')
assert cancel_mock.call_count == 1
assert stoploss_mock.call_count == 0
def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee, def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
@@ -365,7 +424,7 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
# Test non-available pair # Test non-available pair
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate', mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate',
MagicMock(side_effect=DependencyException("Pair 'ETH/BTC' not available"))) MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available")))
stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency) stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
assert stats['trade_count'] == 2 assert stats['trade_count'] == 2
assert stats['first_trade_date'] == 'just now' assert stats['first_trade_date'] == 'just now'
@@ -606,7 +665,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
cancel_order=cancel_order_mock, cancel_order=cancel_order_mock,
get_order=MagicMock( fetch_order=MagicMock(
return_value={ return_value={
'status': 'closed', 'status': 'closed',
'type': 'limit', 'type': 'limit',
@@ -652,7 +711,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
trade = Trade.query.filter(Trade.id == '1').first() trade = Trade.query.filter(Trade.id == '1').first()
filled_amount = trade.amount / 2 filled_amount = trade.amount / 2
mocker.patch( mocker.patch(
'freqtrade.exchange.Exchange.get_order', 'freqtrade.exchange.Exchange.fetch_order',
return_value={ return_value={
'status': 'open', 'status': 'open',
'type': 'limit', 'type': 'limit',
@@ -671,7 +730,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
amount = trade.amount amount = trade.amount
# make an limit-buy open trade, if there is no 'filled', don't sell it # make an limit-buy open trade, if there is no 'filled', don't sell it
mocker.patch( mocker.patch(
'freqtrade.exchange.Exchange.get_order', 'freqtrade.exchange.Exchange.fetch_order',
return_value={ return_value={
'status': 'open', 'status': 'open',
'type': 'limit', 'type': 'limit',
@@ -688,7 +747,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
freqtradebot.enter_positions() freqtradebot.enter_positions()
# make an limit-sell open trade # make an limit-sell open trade
mocker.patch( mocker.patch(
'freqtrade.exchange.Exchange.get_order', 'freqtrade.exchange.Exchange.fetch_order',
return_value={ return_value={
'status': 'open', 'status': 'open',
'type': 'limit', 'type': 'limit',

View File

@@ -10,10 +10,12 @@ from flask import Flask
from requests.auth import _basic_auth_str from requests.auth import _basic_auth_str
from freqtrade.__init__ import __version__ from freqtrade.__init__ import __version__
from freqtrade.loggers import setup_logging, setup_logging_pre
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.rpc.api_server import BASE_URI, ApiServer from freqtrade.rpc.api_server import BASE_URI, ApiServer
from freqtrade.state import State from freqtrade.state import State
from tests.conftest import get_patched_freqtradebot, log_has, patch_get_signal, create_mock_trades from tests.conftest import (create_mock_trades, get_patched_freqtradebot,
log_has, patch_get_signal)
_TEST_USER = "FreqTrader" _TEST_USER = "FreqTrader"
_TEST_PASS = "SuperSecurePassword1!" _TEST_PASS = "SuperSecurePassword1!"
@@ -21,6 +23,9 @@ _TEST_PASS = "SuperSecurePassword1!"
@pytest.fixture @pytest.fixture
def botclient(default_conf, mocker): def botclient(default_conf, mocker):
setup_logging_pre()
setup_logging(default_conf)
default_conf.update({"api_server": {"enabled": True, default_conf.update({"api_server": {"enabled": True,
"listen_ip_address": "127.0.0.1", "listen_ip_address": "127.0.0.1",
"listen_port": 8080, "listen_port": 8080,
@@ -50,6 +55,12 @@ def client_get(client, url):
'Origin': 'http://example.com'}) 'Origin': 'http://example.com'})
def client_delete(client, url):
# Add fake Origin to ensure CORS kicks in
return client.delete(url, headers={'Authorization': _basic_auth_str(_TEST_USER, _TEST_PASS),
'Origin': 'http://example.com'})
def assert_response(response, expected_code=200, needs_cors=True): def assert_response(response, expected_code=200, needs_cors=True):
assert response.status_code == expected_code assert response.status_code == expected_code
assert response.content_type == "application/json" assert response.content_type == "application/json"
@@ -81,20 +92,20 @@ def test_api_unauthorized(botclient):
assert rc.json == {'error': 'Unauthorized'} assert rc.json == {'error': 'Unauthorized'}
# Change only username # Change only username
ftbot.config['api_server']['username'] = "Ftrader" ftbot.config['api_server']['username'] = 'Ftrader'
rc = client_get(client, f"{BASE_URI}/version") rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401) assert_response(rc, 401)
assert rc.json == {'error': 'Unauthorized'} assert rc.json == {'error': 'Unauthorized'}
# Change only password # Change only password
ftbot.config['api_server']['username'] = _TEST_USER ftbot.config['api_server']['username'] = _TEST_USER
ftbot.config['api_server']['password'] = "WrongPassword" ftbot.config['api_server']['password'] = 'WrongPassword'
rc = client_get(client, f"{BASE_URI}/version") rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401) assert_response(rc, 401)
assert rc.json == {'error': 'Unauthorized'} assert rc.json == {'error': 'Unauthorized'}
ftbot.config['api_server']['username'] = "Ftrader" ftbot.config['api_server']['username'] = 'Ftrader'
ftbot.config['api_server']['password'] = "WrongPassword" ftbot.config['api_server']['password'] = 'WrongPassword'
rc = client_get(client, f"{BASE_URI}/version") rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401) assert_response(rc, 401)
@@ -326,6 +337,8 @@ def test_api_show_config(botclient, mocker):
assert rc.json['exchange'] == 'bittrex' assert rc.json['exchange'] == 'bittrex'
assert rc.json['ticker_interval'] == '5m' assert rc.json['ticker_interval'] == '5m'
assert rc.json['timeframe'] == '5m' assert rc.json['timeframe'] == '5m'
assert rc.json['timeframe_ms'] == 300000
assert rc.json['timeframe_min'] == 5
assert rc.json['state'] == 'running' assert rc.json['state'] == 'running'
assert not rc.json['trailing_stop'] assert not rc.json['trailing_stop']
assert 'bid_strategy' in rc.json assert 'bid_strategy' in rc.json
@@ -350,7 +363,7 @@ def test_api_daily(botclient, mocker, ticker, fee, markets):
assert rc.json['data'][0]['date'] == str(datetime.utcnow().date()) assert rc.json['data'][0]['date'] == str(datetime.utcnow().date())
def test_api_trades(botclient, mocker, ticker, fee, markets): def test_api_trades(botclient, mocker, fee, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot, (True, False)) patch_get_signal(ftbot, (True, False))
mocker.patch.multiple( mocker.patch.multiple(
@@ -366,12 +379,81 @@ def test_api_trades(botclient, mocker, ticker, fee, markets):
rc = client_get(client, f"{BASE_URI}/trades") rc = client_get(client, f"{BASE_URI}/trades")
assert_response(rc) assert_response(rc)
assert len(rc.json['trades']) == 3
assert rc.json['trades_count'] == 3
rc = client_get(client, f"{BASE_URI}/trades?limit=2")
assert_response(rc)
assert len(rc.json['trades']) == 2 assert len(rc.json['trades']) == 2
assert rc.json['trades_count'] == 2 assert rc.json['trades_count'] == 2
rc = client_get(client, f"{BASE_URI}/trades?limit=1")
assert_response(rc)
assert len(rc.json['trades']) == 1
assert rc.json['trades_count'] == 1
def test_api_delete_trade(botclient, mocker, fee, markets):
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
stoploss_mock = MagicMock()
cancel_mock = MagicMock()
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets),
cancel_order=cancel_mock,
cancel_stoploss_order=stoploss_mock,
)
rc = client_delete(client, f"{BASE_URI}/trades/1")
# Error - trade won't exist yet.
assert_response(rc, 502)
create_mock_trades(fee)
ftbot.strategy.order_types['stoploss_on_exchange'] = True
trades = Trade.query.all()
trades[1].stoploss_order_id = '1234'
assert len(trades) > 2
rc = client_delete(client, f"{BASE_URI}/trades/1")
assert_response(rc)
assert rc.json['result_msg'] == 'Deleted trade 1. Closed 1 open orders.'
assert len(trades) - 1 == len(Trade.query.all())
assert cancel_mock.call_count == 1
cancel_mock.reset_mock()
rc = client_delete(client, f"{BASE_URI}/trades/1")
# Trade is gone now.
assert_response(rc, 502)
assert cancel_mock.call_count == 0
assert len(trades) - 1 == len(Trade.query.all())
rc = client_delete(client, f"{BASE_URI}/trades/2")
assert_response(rc)
assert rc.json['result_msg'] == 'Deleted trade 2. Closed 2 open orders.'
assert len(trades) - 2 == len(Trade.query.all())
assert stoploss_mock.call_count == 1
def test_api_logs(botclient):
ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/logs")
assert_response(rc)
assert len(rc.json) == 2
assert 'logs' in rc.json
# Using a fixed comparison here would make this test fail!
assert rc.json['log_count'] > 10
assert len(rc.json['logs']) == rc.json['log_count']
assert isinstance(rc.json['logs'][0], list)
# date
assert isinstance(rc.json['logs'][0][0], str)
# created_timestamp
assert isinstance(rc.json['logs'][0][1], float)
assert isinstance(rc.json['logs'][0][2], str)
assert isinstance(rc.json['logs'][0][3], str)
assert isinstance(rc.json['logs'][0][4], str)
rc = client_get(client, f"{BASE_URI}/logs?limit=5")
assert_response(rc)
assert len(rc.json) == 2
assert 'logs' in rc.json
# Using a fixed comparison here would make this test fail!
assert rc.json['log_count'] == 5
assert len(rc.json['logs']) == rc.json['log_count']
def test_api_edge_disabled(botclient, mocker, ticker, fee, markets): def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
@@ -431,14 +513,14 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, li
'latest_trade_date': 'just now', 'latest_trade_date': 'just now',
'latest_trade_timestamp': ANY, 'latest_trade_timestamp': ANY,
'profit_all_coin': 6.217e-05, 'profit_all_coin': 6.217e-05,
'profit_all_fiat': 0, 'profit_all_fiat': 0.76748865,
'profit_all_percent': 6.2, 'profit_all_percent': 6.2,
'profit_all_percent_mean': 6.2, 'profit_all_percent_mean': 6.2,
'profit_all_ratio_mean': 0.06201058, 'profit_all_ratio_mean': 0.06201058,
'profit_all_percent_sum': 6.2, 'profit_all_percent_sum': 6.2,
'profit_all_ratio_sum': 0.06201058, 'profit_all_ratio_sum': 0.06201058,
'profit_closed_coin': 6.217e-05, 'profit_closed_coin': 6.217e-05,
'profit_closed_fiat': 0, 'profit_closed_fiat': 0.76748865,
'profit_closed_percent': 6.2, 'profit_closed_percent': 6.2,
'profit_closed_ratio_mean': 0.06201058, 'profit_closed_ratio_mean': 0.06201058,
'profit_closed_percent_mean': 6.2, 'profit_closed_percent_mean': 6.2,
@@ -446,6 +528,8 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, li
'profit_closed_percent_sum': 6.2, 'profit_closed_percent_sum': 6.2,
'trade_count': 1, 'trade_count': 1,
'closed_trade_count': 1, 'closed_trade_count': 1,
'winning_trades': 1,
'losing_trades': 0,
} }
@@ -515,7 +599,8 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
rc = client_get(client, f"{BASE_URI}/status") rc = client_get(client, f"{BASE_URI}/status")
assert_response(rc) assert_response(rc)
assert len(rc.json) == 1 assert len(rc.json) == 1
assert rc.json == [{'amount': 91.07468124, assert rc.json == [{'amount': 91.07468123,
'amount_requested': 91.07468123,
'base_currency': 'BTC', 'base_currency': 'BTC',
'close_date': None, 'close_date': None,
'close_date_hum': None, 'close_date_hum': None,
@@ -548,6 +633,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
'initial_stop_loss_ratio': -0.1, 'initial_stop_loss_ratio': -0.1,
'stoploss_current_dist': -1.1080000000000002e-06, 'stoploss_current_dist': -1.1080000000000002e-06,
'stoploss_current_dist_ratio': -0.10081893, 'stoploss_current_dist_ratio': -0.10081893,
'stoploss_current_dist_pct': -10.08,
'stoploss_entry_dist': -0.00010475, 'stoploss_entry_dist': -0.00010475,
'stoploss_entry_dist_ratio': -0.10448878, 'stoploss_entry_dist_ratio': -0.10448878,
'trade_id': 1, 'trade_id': 1,
@@ -624,7 +710,7 @@ def test_api_forcebuy(botclient, mocker, fee):
assert rc.json == {"error": "Error querying _forcebuy: Forcebuy not enabled."} assert rc.json == {"error": "Error querying _forcebuy: Forcebuy not enabled."}
# enable forcebuy # enable forcebuy
ftbot.config["forcebuy_enable"] = True ftbot.config['forcebuy_enable'] = True
fbuy_mock = MagicMock(return_value=None) fbuy_mock = MagicMock(return_value=None)
mocker.patch("freqtrade.rpc.RPC._rpc_forcebuy", fbuy_mock) mocker.patch("freqtrade.rpc.RPC._rpc_forcebuy", fbuy_mock)
@@ -637,6 +723,7 @@ def test_api_forcebuy(botclient, mocker, fee):
fbuy_mock = MagicMock(return_value=Trade( fbuy_mock = MagicMock(return_value=Trade(
pair='ETH/ETH', pair='ETH/ETH',
amount=1, amount=1,
amount_requested=1,
exchange='bittrex', exchange='bittrex',
stake_amount=1, stake_amount=1,
open_rate=0.245441, open_rate=0.245441,
@@ -653,6 +740,7 @@ def test_api_forcebuy(botclient, mocker, fee):
data='{"pair": "ETH/BTC"}') data='{"pair": "ETH/BTC"}')
assert_response(rc) assert_response(rc)
assert rc.json == {'amount': 1, assert rc.json == {'amount': 1,
'amount_requested': 1,
'trade_id': None, 'trade_id': None,
'close_date': None, 'close_date': None,
'close_date_hum': None, 'close_date_hum': None,
@@ -689,7 +777,7 @@ def test_api_forcebuy(botclient, mocker, fee):
'min_rate': None, 'min_rate': None,
'open_order_id': '123456', 'open_order_id': '123456',
'open_rate_requested': None, 'open_rate_requested': None,
'open_trade_price': 0.2460546025, 'open_trade_price': 0.24605460,
'sell_reason': None, 'sell_reason': None,
'sell_order_status': None, 'sell_order_status': None,
'strategy': None, 'strategy': None,

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