stable/docs/data-analysis.md

93 lines
2.9 KiB
Markdown
Raw Normal View History

2019-06-22 14:18:22 +00:00
# Analyzing bot data
2019-08-07 02:35:14 +00:00
You can analyze the results of backtests and trading history easily using Jupyter notebooks. A sample notebook is located at `user_data/notebooks/analysis_example.ipynb`. For usage instructions, see [jupyter.org](https://jupyter.org/documentation).
2019-06-22 14:18:22 +00:00
2019-08-07 02:35:14 +00:00
## Strategy debugging example
2019-06-22 14:18:22 +00:00
2019-08-07 02:35:14 +00:00
Debugging a strategy can be time-consuming. FreqTrade offers helper functions to visualize raw data.
2019-06-22 14:18:22 +00:00
2019-08-07 02:35:14 +00:00
### Import requirements and define variables used in the script
```python
2019-08-07 02:35:14 +00:00
# Imports
from pathlib import Path
2019-08-07 02:35:14 +00:00
import os
from freqtrade.data.history import load_pair_history
from freqtrade.resolvers import StrategyResolver
2019-08-07 02:35:14 +00:00
from freqtrade.data.btanalysis import load_backtest_data
from freqtrade.data.btanalysis import load_trades_from_db
# Define some constants
2019-08-07 02:35:14 +00:00
ticker_interval = "1m"
# Name of the strategy class
2019-08-07 02:35:14 +00:00
strategy_name = 'NewStrategy'
# Path to user data
user_data_dir = 'user_data'
# Location of the strategy
2019-08-07 02:35:14 +00:00
strategy_location = os.path.join(user_data_dir, 'strategies')
# Location of the data
2019-08-07 02:35:14 +00:00
data_location = os.path.join(user_data_dir, 'data', 'binance')
# Pair to analyze
# Only use one pair here
2019-08-07 02:35:14 +00:00
pair = "BTC_USDT"
```
2019-08-07 02:35:14 +00:00
### Load exchange data
2019-08-07 02:35:14 +00:00
```python
# Load data using values set above
bt_data = load_pair_history(datadir=Path(data_location),
2019-08-07 02:35:14 +00:00
ticker_interval=ticker_interval,
pair=pair)
2019-08-07 02:35:14 +00:00
# Confirm success
print("Loaded " + str(len(bt_data)) + f" rows of data for {pair} from {data_location}")
```
2019-08-07 02:35:14 +00:00
### Load and run strategy
2019-08-07 02:35:14 +00:00
* Rerun each time the strategy file is changed
* Display the trade details. Note that using `data.head()` would also work, however most indicators have some "startup" data at the top of the dataframe.
2019-08-07 02:35:14 +00:00
Some possible problems:
2019-08-07 02:35:14 +00:00
* Columns with NaN values at the end of the dataframe
* Columns used in `crossed*()` functions with completely different units
```python
# Load strategy using values set above
strategy = StrategyResolver({'strategy': strategy_name,
'user_data_dir': user_data_dir,
'strategy_path': strategy_location}).strategy
# Run strategy (just like in backtesting)
df = strategy.analyze_ticker(bt_data, {'pair': pair})
2019-08-07 02:35:14 +00:00
# Report results
print(f"Generated {df['buy'].sum()} buy signals")
data = df.set_index('date', drop=True)
data.tail()
```
2019-08-07 02:35:14 +00:00
### Load backtest results into a pandas dataframe
2019-08-07 02:35:14 +00:00
```python
# Load backtest results
df = load_backtest_data("user_data/backtest_data/backtest-result.json")
2019-06-22 14:18:22 +00:00
# Show value-counts per pair
df.groupby("pair")["sell_reason"].value_counts()
```
2019-08-07 02:35:14 +00:00
### Load live trading results into a pandas dataframe
2019-06-22 14:18:22 +00:00
``` python
2019-08-07 02:35:14 +00:00
# Fetch trades from database
2019-06-22 14:18:22 +00:00
df = load_trades_from_db("sqlite:///tradesv3.sqlite")
2019-08-07 02:35:14 +00:00
# Display results
2019-06-22 14:18:22 +00:00
df.groupby("pair")["sell_reason"].value_counts()
```
2019-06-24 15:20:41 +00:00
Feel 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.