|
| 1 | +# Optimize trading strategy using Freqtrade |
| 2 | + |
| 3 | +Short demo on building, testing and optimizing a trading strategy using Freqtrade. |
| 4 | + |
| 5 | +The DevBootstrap YouTube screencast supporting this repo is [here](https://www.youtube.com/watch?v=wq3uLSDJxUQ). Enjoy! :) |
| 6 | + |
| 7 | +## Alias Docker-Compose Command |
| 8 | + |
| 9 | +First, I recommend to alias `docker-compose` to `dc` and `docker-compose run --rm "$@"` to `dcr` to save of typing. |
| 10 | + |
| 11 | +Put this in your `~/.bash_profile` file so that its always aliased like this! |
| 12 | + |
| 13 | +``` |
| 14 | +alias dc=docker-compose |
| 15 | +dcr() { docker-compose run --rm "$@"; } |
| 16 | +``` |
| 17 | + |
| 18 | +Now run `source ~/.bash_profile`. |
| 19 | + |
| 20 | +## Installing Freqtrade |
| 21 | + |
| 22 | +Install and run [via Docker](https://www.freqtrade.io/en/stable/docker_quickstart/). |
| 23 | + |
| 24 | +Now install the necessary dependencies to run Freqtrade: |
| 25 | + |
| 26 | +``` |
| 27 | +mkdir ft_userdata |
| 28 | +cd ft_userdata/ |
| 29 | +# Download the dc file from the repository |
| 30 | +curl https://raw.githubusercontent.com/freqtrade/freqtrade/stable/docker-compose.yml -o docker-compose.yml |
| 31 | +
|
| 32 | +# Pull the freqtrade image |
| 33 | +dc pull |
| 34 | +
|
| 35 | +# Create user directory structure |
| 36 | +dcr freqtrade create-userdir --userdir user_data |
| 37 | +
|
| 38 | +# Create configuration - Requires answering interactive questions |
| 39 | +dcr freqtrade new-config --config user_data/config.json |
| 40 | +``` |
| 41 | + |
| 42 | +**NOTE**: Any freqtrade commands are available by running `dcr freqtrade <command> <optional arguments>`. So the only difference to run the command via `docker-compose` is to prefix the command with our new alias `dcr` (which runs `docker-compose run --rm "$@"` ... see above for details.) |
| 43 | + |
| 44 | +## Config Bot |
| 45 | + |
| 46 | +If you used the `new-config` sub-command (see above) when installing the bot, the installation script should have already created the default configuration file (`config.json`) for you. |
| 47 | + |
| 48 | +The params that we will set to note are (from `config.json`). This allows all the available balance to be distrubuted accross all possible trades. So in dry run mode we have a default paper money balance of 1000 (can be changed using `dry_run_wallet` param) and if we set to have a max of 10 trades then Freqtrade would distribute the funds accrosss all 10 trades aprroximatly equally (1000 / 10 = 100 / trade). |
| 49 | + |
| 50 | +``` |
| 51 | +"stake_amount" : "unlimited", |
| 52 | +"tradable_balance_ratio": 0.99, |
| 53 | +``` |
| 54 | + |
| 55 | +The above are used for Dry Runs and is the ['Dynamic Stake Amount'](https://www.freqtrade.io/en/stable/configuration/#dynamic-stake-amount). For live trading you might want to change this. For example, only allow bot to trade 20% of excahnge account funds and cancel open orders on exit (if market goes crazy!) |
| 56 | + |
| 57 | +``` |
| 58 | +"tradable_balance_ratio": 0.2, |
| 59 | +"cancel_open_orders_on_exit": true |
| 60 | +``` |
| 61 | + |
| 62 | +For details of all available parameters, please refer to [the configuration parameters docs](https://www.freqtrade.io/en/stable/configuration/#configuration-parameters). |
| 63 | + |
| 64 | +## Create a Strategy |
| 65 | + |
| 66 | +So I've created a 'BBRSINaiveStrategy' based on [RSI](https://www.investopedia.com/terms/r/rsi.asp) and B[ollenger Bands](https://www.investopedia.com/terms/b/bollingerbands.asp). Take a look at the file [`bbrsi_naive_strategy.py`](ft_userdata/user_data/strategies/bbrsi_naive_strategy.py) file for details. |
| 67 | + |
| 68 | +To tell your instance of Freqtrade about this strategy, open your `docker-compose.yml` file and update the strategy flag (last flag of the command) to `--strategy BBRSINaiveStrategy` |
| 69 | + |
| 70 | +For more details on Strategy Customization, please refer to the [Freqtrade Docs](https://www.freqtrade.io/en/stable/strategy-customization/) |
| 71 | + |
| 72 | +## Remove past trade data |
| 73 | + |
| 74 | +If you have run the bot already, you will need to clear out any existing dry run trades from the database. The easiest way to do this is to delete the sqlite database by running the command `rm user_data/tradesv3.sqlite`. |
| 75 | + |
| 76 | +## Sandbox / Dry Run |
| 77 | + |
| 78 | +As a quick sanity check, you can now immediately start the bot in a sandbox mode and it will start trading (with paper money - not real money!). |
| 79 | + |
| 80 | +To start trading in sandbox mode, simply start the service as a daemon using Docker Compose, like so and follow the log trail as follows: |
| 81 | + |
| 82 | +``` |
| 83 | +dc up -d |
| 84 | +dc ps |
| 85 | +dc logs -f |
| 86 | +``` |
| 87 | + |
| 88 | +## Setup a pairs file |
| 89 | + |
| 90 | +We will use Binance so we create a data directory for binance and copy our `pairs.json` file into that directory: |
| 91 | + |
| 92 | +``` |
| 93 | +mkdir -p user_data/data/binance |
| 94 | +cp pairs.json user_data/data/binance/. |
| 95 | +``` |
| 96 | + |
| 97 | +Now put whatever pairs you are interested to download into the `pairs.json` file. Take a look at the [pairs.json](ft_userdata/user_data/data/binance/pairs.json) file included in this repo. |
| 98 | + |
| 99 | +## Download Data |
| 100 | + |
| 101 | +Now that we have our pairs file in place, lets download the OHLCV data for backtesting our strategy. |
| 102 | + |
| 103 | +``` |
| 104 | +dcr freqtrade download-data --exchange binance -t 15m |
| 105 | +``` |
| 106 | + |
| 107 | +List the available data using the `list-data` sub-command: |
| 108 | + |
| 109 | +``` |
| 110 | +dcr freqtrade list-data --exchange binance |
| 111 | +``` |
| 112 | + |
| 113 | +Manually inspect the json files to examine the data is as expected (i.e. that it contains the expected `OHLCV` data requested). |
| 114 | + |
| 115 | +## List the available data for backtesting |
| 116 | + |
| 117 | +Note to list the available data you need to pass the `--data-format-ohlcv jsongz` flag as below: |
| 118 | + |
| 119 | +``` |
| 120 | +dcr freqtrade list-data --exchange binance |
| 121 | +``` |
| 122 | + |
| 123 | +## Backtest |
| 124 | + |
| 125 | +Now we have the data for 1h and 4h OHLCV data for our pairs lets Backtest this strategy: |
| 126 | + |
| 127 | +``` |
| 128 | +dcr freqtrade backtesting --datadir user_data/data/binance --export trades --stake-amount 100 -s BBRSINaiveStrategy -i 15m |
| 129 | +``` |
| 130 | + |
| 131 | +For details on interpreting the result, refer to ['Understading the backtesting result'](https://www.freqtrade.io/en/stable/backtesting/#understand-the-backtesting-result) |
| 132 | + |
| 133 | +## Plotting |
| 134 | + |
| 135 | +Plot the results to see how the bot entered and exited trades over time. Remember to change the Docker image being referenced in the docker-compose file to `freqtradeorg/freqtrade:develop_plot` before running the below command. |
| 136 | + |
| 137 | +Note that the `plot_config` that is contained in the strategy will be applied to the chart. |
| 138 | + |
| 139 | +``` |
| 140 | +dcr freqtrade plot-dataframe --strategy BBRSINaiveStrategy -p ALGO/USDT -i 15m |
| 141 | +``` |
| 142 | + |
| 143 | +Once the plot is ready you will see the message `Stored plot as /freqtrade/user_data/plot/freqtrade-plot-ALGO_USDT-15m.html` which you can open in a browser window. |
| 144 | + |
| 145 | +## Optimize |
| 146 | + |
| 147 | +To optimize the strategy we will use the Hyperopt module of freqtrade. First up we need to create a new hyperopt file from a template: |
| 148 | + |
| 149 | +``` |
| 150 | +dcr freqtrade new-hyperopt --hyperopt BBRSIHyperopt |
| 151 | +``` |
| 152 | + |
| 153 | +Now add desired definitions for buy/sell guards and triggers to the Hyperopt file. Then run the optimization like so (NOTE: set the time interval and the number of epochs to test using the `-i` and `-e` flags: |
| 154 | + |
| 155 | +``` |
| 156 | +dcr freqtrade hyperopt --hyperopt BBRSIHyperopt --hyperopt-loss SharpeHyperOptLoss --strategy BBRSINaiveStrategy -i 15m |
| 157 | +``` |
| 158 | + |
| 159 | +## Update Strategy |
| 160 | + |
| 161 | +Apply the suggested optimized results from the Hyperopt to the strategy. Either replace the current strategy or create a new 'optimized' strategy. |
| 162 | + |
| 163 | +## Backtest |
| 164 | + |
| 165 | +Now we have updated our strategy based on the result from the hyperopt lets run a backtest again: |
| 166 | + |
| 167 | +``` |
| 168 | +dcr freqtrade backtesting --datadir user_data/data/binance --export trades --stake-amount 100 -s BBRSIOptimizedStrategy -i 15m |
| 169 | +``` |
| 170 | + |
| 171 | +## Sandbox / Dry Run |
| 172 | + |
| 173 | +Before you run the Dry Run, don't forget to check your local config.json file is configured. Particularly the `dry_run` is true, the `dry_run_wallet` is set to something reasonable (like 1000 USDT) and that the `timeframe` is set to the same that you have used when building and optimizing your strategy! |
| 174 | + |
| 175 | +``` |
| 176 | +"max_open_trades": 10, |
| 177 | +"stake_currency": "USDT", |
| 178 | +"stake_amount" : "unlimited", |
| 179 | +"tradable_balance_ratio": 0.99, |
| 180 | +"fiat_display_currency": "USD", |
| 181 | +"timeframe": "15min", |
| 182 | +"dry_run": true, |
| 183 | +"dry_run_wallet": 1000, |
| 184 | +``` |
| 185 | + |
| 186 | +## View Dry Run via Freq UI |
| 187 | + |
| 188 | +For use with docker you will need to enable the api server in the Freqtrade config and set `listen_ip_address` to "0.0.0.0", and also set the `username` & `password` so that you can login like so: |
| 189 | + |
| 190 | +``` |
| 191 | +... |
| 192 | +
|
| 193 | +"api_server": { |
| 194 | + "enabled": true, |
| 195 | + "listen_ip_address": "0.0.0.0", |
| 196 | + "username": "Freqtrader", |
| 197 | + "password": "secretpass!", |
| 198 | +P |
| 199 | +... |
| 200 | +``` |
| 201 | + |
| 202 | +In the docker-compose.yml file also map the ports like so: |
| 203 | + |
| 204 | +``` |
| 205 | +ports: |
| 206 | + - "127.0.0.1:8080:8080" |
| 207 | +``` |
| 208 | + |
| 209 | +Then you can access the Freq UI via a browser at [http://127.0.0.1:8080/](http://127.0.0.1:8080/). You can also access and control the bot via a REST API too! |
0 commit comments