stable/user_data/notebooks/strategy_analysis_example.ipynb
Jonathan Raviotta 228b91da20 cleared output
2019-09-10 23:26:27 -04:00

259 lines
6.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Strategy debugging example\n",
"\n",
"Debugging a strategy can be time-consuming. FreqTrade offers helper functions to visualize raw data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"# Fix asyncio for Jupyter\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from os import chdir\n",
"from pathlib import Path\n",
"\n",
"# Change directory to project root\n",
"chdir(Path(Path.home(), 'Documents', 'Repos', 'freqtrade'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from itertools import chain\n",
"\n",
"from freqtrade.configuration import Configuration\n",
"\n",
"# Load configuration\n",
"# Specify values for use in this script\n",
"# Edit to match your needs\n",
"config_files = [\n",
" Path('user_data', 'user_repo', 'config.json'),\n",
" Path(Path.home(), '.freqtrade', 'exchange-config.json')\n",
"]\n",
"# Create config object\n",
"config = Configuration.from_files(config_files)\n",
"# Create config string for use in cli commands\n",
"conf = \" \".join(\n",
" list(chain.from_iterable([['-c', str(file)] for file in config_files])))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"\n",
"from freqtrade.loggers import setup_logging\n",
"\n",
"# Configure logging\n",
"logger = logging.getLogger()\n",
"setup_logging(config)\n",
"logger.setLevel(logging.INFO)\n",
"logger.info(f'conf: {conf}')\n",
"# print(json.dumps(config, indent=1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Download data\n",
"!freqtrade {conf} download-data --timeframes 1m 5m 1d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Backtest"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Run backtest\n",
"!freqtrade {conf} backtesting --timerange -200 --ticker-interval 15m --refresh-pairs-cached --export=trades"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"\n",
"from freqtrade.data.btanalysis import load_trades\n",
"from freqtrade.data.history import load_pair_history\n",
"from freqtrade.plot.plotting import (extract_trades_of_period,\n",
" generate_candlestick_graph)\n",
"from freqtrade.resolvers import StrategyResolver\n",
"\n",
"# Specify pair to plot\n",
"pair = \"ETH/BTC\"\n",
"# Load ticker history\n",
"tickers = load_pair_history(datadir=Path(config['datadir']),\n",
" ticker_interval=config['ticker_interval'],\n",
" pair=pair)\n",
"\n",
"# Load strategy\n",
"strategy = StrategyResolver({\n",
" 'user_data_dir': Path(config['user_data_dir']),\n",
" 'strategy_path': Path(config['strategy_path'])\n",
"}).strategy\n",
"\n",
"# Generate buy/sell signals using strategy\n",
"data = strategy.analyze_ticker(tickers, {'pair': pair})\n",
"\n",
"# Collect trades\n",
"trades = load_trades(\n",
" config['trade_source'],\n",
" db_url=config.get('db_url'),\n",
" exportfilename=config.get('exportfilename'),\n",
")\n",
"\n",
"trades = trades.loc[trades['pair'] == pair]\n",
"trades = extract_trades_of_period(data, trades)\n",
"\n",
"# Build and display plot\n",
"fig = generate_candlestick_graph(pair=pair,\n",
" data=data,\n",
" trades=trades,\n",
" indicators1=config[\"indicators1\"],\n",
" indicators2=config[\"indicators2\"])\n",
"\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
}
],
"metadata": {
"file_extension": ".py",
"kernelspec": {
"display_name": "Python [conda env:freqtrade]",
"language": "python",
"name": "conda-env-freqtrade-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
},
"version": 3
},
"nbformat": 4,
"nbformat_minor": 2
}