2019-08-07 02:35:14 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-08-11 16:52:37 +00:00
"## Strategy debugging example\n",
2019-08-09 15:53:29 +00:00
"\n",
2019-08-11 16:52:37 +00:00
"Debugging a strategy can be time-consuming. FreqTrade offers helper functions to visualize raw data."
2019-08-09 02:09:15 +00:00
]
},
{
2019-08-11 16:52:37 +00:00
"cell_type": "markdown",
2019-08-09 02:09:15 +00:00
"metadata": {},
"source": [
2019-08-11 16:52:37 +00:00
"## Setup"
2019-08-07 02:35:14 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Change directory\n",
2019-08-11 16:52:37 +00:00
"# Modify this cell to insure that the output shows the correct path.\n",
"import os\n",
"from pathlib import Path\n",
"\n",
2019-08-20 04:47:10 +00:00
"# Define all paths relative to the project root shown in the cell output\n",
2019-08-11 16:52:37 +00:00
"project_root = \"somedir/freqtrade\"\n",
"i=0\n",
2019-08-07 02:35:14 +00:00
"try:\n",
2019-08-11 16:52:37 +00:00
" os.chdirdir(project_root)\n",
" assert Path('LICENSE').is_file()\n",
2019-08-07 02:35:14 +00:00
"except:\n",
2019-08-11 16:52:37 +00:00
" while i<4 and (not Path('LICENSE').is_file()):\n",
" os.chdir(Path(Path.cwd(), '../'))\n",
" i+=1\n",
" project_root = Path.cwd()\n",
2019-08-20 04:47:10 +00:00
"print(Path.cwd())"
2019-08-09 02:09:15 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-08-11 16:52:37 +00:00
"# Customize these according to your needs.\n",
2019-08-07 02:35:14 +00:00
"\n",
"# Define some constants\n",
2019-08-09 15:41:05 +00:00
"ticker_interval = \"5m\"\n",
2019-08-07 02:35:14 +00:00
"# Name of the strategy class\n",
2019-08-27 04:41:07 +00:00
"strategy_name = 'SampleStrategy'\n",
2019-08-07 02:35:14 +00:00
"# Path to user data\n",
"user_data_dir = 'user_data'\n",
"# Location of the strategy\n",
2019-08-09 15:36:53 +00:00
"strategy_location = Path(user_data_dir, 'strategies')\n",
2019-08-07 02:35:14 +00:00
"# Location of the data\n",
2019-08-09 15:36:53 +00:00
"data_location = Path(user_data_dir, 'data', 'binance')\n",
2019-08-11 16:52:37 +00:00
"# Pair to analyze - Only use one pair here\n",
2019-08-07 02:35:14 +00:00
"pair = \"BTC_USDT\""
]
},
{
"cell_type": "code",
2019-08-22 13:43:39 +00:00
"execution_count": null,
2019-08-07 02:35:14 +00:00
"metadata": {},
2019-08-22 13:43:39 +00:00
"outputs": [],
2019-08-07 02:35:14 +00:00
"source": [
"# Load data using values set above\n",
2019-08-11 16:52:37 +00:00
"from pathlib import Path\n",
"from freqtrade.data.history import load_pair_history\n",
"\n",
"candles = load_pair_history(datadir=data_location,\n",
2019-08-07 02:35:14 +00:00
" ticker_interval=ticker_interval,\n",
" pair=pair)\n",
"\n",
"# Confirm success\n",
2019-08-11 16:52:37 +00:00
"print(\"Loaded \" + str(len(candles)) + f\" rows of data for {pair} from {data_location}\")\n",
2019-08-20 04:47:10 +00:00
"candles.head()"
2019-08-07 02:35:14 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-08-11 16:52:37 +00:00
"## Load and run strategy\n",
2019-08-09 02:09:15 +00:00
"* Rerun each time the strategy file is changed"
2019-08-07 02:35:14 +00:00
]
},
{
"cell_type": "code",
2019-08-22 13:43:39 +00:00
"execution_count": null,
2019-08-11 16:52:37 +00:00
"metadata": {
"scrolled": true
},
2019-08-22 13:43:39 +00:00
"outputs": [],
2019-08-07 02:35:14 +00:00
"source": [
"# Load strategy using values set above\n",
2019-08-11 16:52:37 +00:00
"from freqtrade.resolvers import StrategyResolver\n",
2019-08-07 02:35:14 +00:00
"strategy = StrategyResolver({'strategy': strategy_name,\n",
" 'user_data_dir': user_data_dir,\n",
" 'strategy_path': strategy_location}).strategy\n",
"\n",
2019-08-09 02:09:15 +00:00
"# Generate buy/sell signals using strategy\n",
2019-08-11 16:52:37 +00:00
"df = strategy.analyze_ticker(candles, {'pair': pair})\n",
"df.tail()"
2019-08-07 02:35:14 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-08-09 02:09:15 +00:00
"### Display the trade details\n",
"\n",
2019-08-11 16:52:37 +00:00
"* Note that using `data.head()` would also work, however most indicators have some \"startup\" data at the top of the dataframe.\n",
"* Some possible problems\n",
" * Columns with NaN values at the end of the dataframe\n",
" * Columns used in `crossed*()` functions with completely different units\n",
"* Comparison with full backtest\n",
" * having 200 buy signals as output for one pair from `analyze_ticker()` does not necessarily mean that 200 trades will be made during backtesting.\n",
" * Assuming you use only one condition such as, `df['rsi'] < 30` as buy condition, this will generate multiple \"buy\" signals for each pair in sequence (until rsi returns > 29). The bot will only buy on the first of these signals (and also only if a trade-slot (\"max_open_trades\") is still available), or on one of the middle signals, as soon as a \"slot\" becomes available. \n"
2019-08-07 02:35:14 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-08-09 02:09:15 +00:00
"# Report results\n",
"print(f\"Generated {df['buy'].sum()} buy signals\")\n",
"data = df.set_index('date', drop=True)\n",
"data.tail()"
2019-08-07 02:35:14 +00:00
]
},
{
2019-08-09 15:41:05 +00:00
"cell_type": "markdown",
2019-08-07 02:35:14 +00:00
"metadata": {},
"source": [
2019-08-09 02:09:15 +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."
2019-08-07 02:35:14 +00:00
]
}
],
"metadata": {
"file_extension": ".py",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.3"
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
2019-08-11 16:52:37 +00:00
"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
},
2019-08-07 02:35:14 +00:00
"version": 3
},
"nbformat": 4,
"nbformat_minor": 2
}