Elffuss BraidHub

kikocis/freqtrade public

develop · 5.1 KiBRenderedRawPermalink
1# Analyzing bot data with Jupyter notebooks
2
3You can analyze the results of backtests and trading history easily using Jupyter notebooks. Sample notebooks are located at `user_data/notebooks/` after initializing the user directory with `freqtrade create-userdir --userdir user_data`.
4
5## Quick start with docker
6
7Freqtrade provides a docker-compose file which starts up a jupyter lab server.
8You can run this server using the following command: `docker-compose -f docker/docker-compose-jupyter.yml up`
9
10This will create a dockercontainer running jupyter lab, which will be accessible using `https://127.0.0.1:8888/lab`.
11Please use the link that's printed in the console after startup for simplified login.
12
13For more information, Please visit the [Data analysis with Docker](docker_quickstart.md#data-analayis-using-docker-compose) section.
14
15### Pro tips
16
17* See [jupyter.org](https://jupyter.org/documentation) for usage instructions.
18* Don't forget to start a Jupyter notebook server from within your conda or venv environment or use [nb_conda_kernels](https://github.com/Anaconda-Platform/nb_conda_kernels)*
19* Copy the example notebook before use so your changes don't get overwritten with the next freqtrade update.
20
21### Using virtual environment with system-wide Jupyter installation
22
23Sometimes it can be desired to use a system-wide installation of Jupyter notebook, and use a jupyter kernel from the virtual environment.
24This prevents you from installing the full jupyter suite multiple times per system, and provides an easy way to switch between tasks (freqtrade / other analytics tasks).
25
26For this to work, first activate your virtual environment and run the following commands:
27
28``` bash
29# Activate virtual environment
30source .env/bin/activate
31
32pip install ipykernel
33ipython kernel install --user --name=freqtrade
34# Restart jupyter (lab / notebook)
35# select kernel "freqtrade" in the notebook
36```
37
38!!! Note
39 This section is provided for completeness, the Freqtrade Team won't provide full support for problems with this setup and will recommend to install Jupyter in the virtual environment directly, as that is the easiest way to get jupyter notebooks up and running. For help with this setup please refer to the [Project Jupyter](https://jupyter.org/) [documentation](https://jupyter.org/documentation) or [help channels](https://jupyter.org/community).
40
41!!! Warning
42 Some tasks don't work especially well in notebooks. For example, anything using asynchronous execution is a problem for Jupyter. Also, freqtrade's primary entry point is the shell cli, so using pure python in a notebook bypasses arguments that provide required objects and parameters to helper functions. You may need to set those values or create expected objects manually.
43
44## Recommended workflow
45
46| Task | Tool |
47 --- | ---
48Bot operations | CLI
49Repetitive tasks | Shell scripts
50Data analysis & visualization | Notebook
51
521. Use the CLI to
53 * download historical data
54 * run a backtest
55 * run with real-time data
56 * export results
57
581. Collect these actions in shell scripts
59 * save complicated commands with arguments
60 * execute multi-step operations
61 * automate testing strategies and preparing data for analysis
62
631. Use a notebook to
64 * visualize data
65 * munge and plot to generate insights
66
67## Example utility snippets
68
69### Change directory to root
70
71Jupyter notebooks execute from the notebook directory. The following snippet searches for the project root, so relative paths remain consistent.
72
73```python
74import os
75from pathlib import Path
76
77# Change directory
78# Modify this cell to insure that the output shows the correct path.
79# Define all paths relative to the project root shown in the cell output
80project_root = "somedir/freqtrade"
81i=0
82try:
83 os.chdirdir(project_root)
84 assert Path('LICENSE').is_file()
85except:
86 while i<4 and (not Path('LICENSE').is_file()):
87 os.chdir(Path(Path.cwd(), '../'))
88 i+=1
89 project_root = Path.cwd()
90print(Path.cwd())
91```
92
93### Load multiple configuration files
94
95This option can be useful to inspect the results of passing in multiple configs.
96This will also run through the whole Configuration initialization, so the configuration is completely initialized to be passed to other methods.
97
98``` python
99import json
100from freqtrade.configuration import Configuration
101
102# Load config from multiple files
103config = Configuration.from_files(["config1.json", "config2.json"])
104
105# Show the config in memory
106print(json.dumps(config['original_config'], indent=2))
107```
108
109For Interactive environments, have an additional configuration specifying `user_data_dir` and pass this in last, so you don't have to change directories while running the bot.
110Best avoid relative paths, since this starts at the storage location of the jupyter notebook, unless the directory is changed.
111
112``` json
113{
114 "user_data_dir": "~/.freqtrade/"
115}
116```
117
118### Further Data analysis documentation
119
120* [Strategy debugging](strategy_analysis_example.md) - also available as Jupyter notebook (`user_data/notebooks/strategy_analysis_example.ipynb`)
121* [Plotting](plotting.md)
122
123Feel free to submit an issue or Pull Request enhancing this document if you would like to share ideas on how to best analyze the data.