跳到主要内容

Strategy Customization

This page explains how to customize your strategies, add new indicators and set up trading rules.

If you haven't already, please familiarize yourself with:

Develop Your Own Strategy

The bot includes a default strategy file.

Also, several other strategies are available in the strategy repository.

You will however most likely have your own idea for a strategy.

This document intends to help you convert your ideas into a working strategy.

Generating a Strategy Template

To get started, you can use the command:

freqtrade new-strategy --strategy AwesomeStrategy

This will create a new strategy called AwesomeStrategy from a template, which will be located using the filename user_data/strategies/AwesomeStrategy.py.

Strategy Name vs Filename

There is a difference between the name of the strategy and the filename. In most commands, Freqtrade uses the name of the strategy, not the filename.

Template Profitability

The new-strategy command generates starting examples which will not be profitable out of the box.

Different Template Levels

freqtrade new-strategy has an additional parameter, --template, which controls the amount of pre-build information you get in the created strategy. Use --template minimal to get an empty strategy without any indicator examples, or --template advanced to get a template with more complicated features defined.

Anatomy of a Strategy

A strategy file contains all the information needed to build the strategy logic:

  • Candle data in OHLCV format
  • Indicators
  • Entry logic
    • Signals
  • Exit logic
    • Signals
    • Minimal ROI
    • Callbacks ("custom functions")
  • Stoploss
    • Fixed/absolute
    • Trailing
    • Callbacks ("custom functions")
  • Pricing [optional]
  • Position adjustment [optional]

The bot includes a sample strategy called SampleStrategy that you can use as a basis: user_data/strategies/sample_strategy.py.

You can test it with the parameter: --strategy SampleStrategy. Remember that you use the strategy class name, not the filename.

Additionally, there is an attribute called INTERFACE_VERSION, which defines the version of the strategy interface the bot should use.

The current version is 3 - which is also the default when it's not set explicitly in the strategy.

You may see older strategies set to interface version 2, and these will need to be updated to v3 terminology as future versions will require this to be set.

Starting the bot in dry or live mode is accomplished using the trade command:

freqtrade trade --strategy AwesomeStrategy

Bot Modes

Freqtrade strategies can be processed by the Freqtrade bot in 5 main modes:

  • Backtesting - Test strategy with historical data
  • Hyperopting - Optimize strategy parameters
  • Dry ("forward testing") - Paper trading with live data
  • Live - Real trading with actual money
  • FreqAI (not covered here) - Machine learning integration

Check the configuration documentation about how to set the bot to dry or live mode.

Always use dry mode when testing as this gives you an idea of how your strategy will work in reality without risking capital.

Diving in Deeper

For the following section we will use the user_data/strategies/sample_strategy.py file as reference.

Strategies and Backtesting

To avoid problems and unexpected differences between backtesting and dry/live modes, please be aware that during backtesting the full time range is passed to the populate_*() methods at once.

It is therefore best to use vectorized operations (across the whole dataframe, not loops) and avoid index referencing (df.iloc[-1]), but instead use df.shift() to get to the previous candle.

Using Future Data

Since backtesting passes the full time range to the populate_*() methods, the strategy author needs to take care to avoid having the strategy utilize data from the future.

Some common patterns for this are listed in the Common Mistakes section of this document.

Lookahead and Recursive Analysis

Freqtrade includes two helpful commands to help assess common lookahead (using future data) and recursive bias (variance in indicator values) issues. Before running a strategy in dry or live mode, you should always use these commands first. Please check the relevant documentation for lookahead and recursive analysis.

Dataframe

Freqtrade uses pandas to store/provide the candlestick (OHLCV) data.

Pandas is a great library developed for processing large amounts of data in tabular format.

Each row in a dataframe corresponds to one candle on a chart, with the latest complete candle always being the last in the dataframe (sorted by date).

If we were to look at the first few rows of the main dataframe using the pandas head() function, we would see:

> dataframe.head()
date open high low close volume
0 2021-11-09 23:25:00+00:00 67279.67 67321.84 67255.01 67300.97 44.62253
1 2021-11-09 23:30:00+00:00 67300.97 67301.34 67183.03 67187.01 61.38076
2 2021-11-09 23:35:00+00:00 67187.02 67187.02 67031.93 67123.81 113.42728
3 2021-11-09 23:40:00+00:00 67123.80 67222.40 67080.33 67160.48 78.96008
4 2021-11-09 23:45:00+00:00 67160.48 67160.48 66901.26 66943.37 111.39292

A dataframe is a table where columns are not single values, but a series of data values. As such, simple python comparisons like the following will not work:

if dataframe['rsi'] > 30:
dataframe['enter_long'] = 1

The above section will fail with The truth value of a Series is ambiguous [...].

This must instead be written in a pandas-compatible way, so the operation is performed across the whole dataframe, i.e. vectorisation.

dataframe.loc[
(dataframe['rsi'] > 30)
, 'enter_long'] = 1

With this section, you have a new column in your dataframe, which has 1 assigned whenever RSI is above 30.

Freqtrade uses this new column as an entry signal, where it is assumed that a trade will subsequently open on the next open candle.

Pandas provides fast ways to calculate metrics, i.e. "vectorisation". To benefit from this speed, it is advised to not use loops, but use vectorized methods instead.

Vectorized operations perform calculations across the whole range of data and are therefore, compared to looping through each row, a lot faster when calculating indicators.

Signals vs Trades
  • Signals are generated from indicators at candle close, and are intentions to enter a trade.
  • Trades are orders that are executed (on the exchange in live mode) where a trade will then open as close to next candle open as possible.
Trade Order Assumptions

In backtesting, signals are generated on candle close. Trades are then initiated immediately on next candle open.

In dry and live, this may be delayed due to all pair dataframes needing to be analysed first, then trade processing for each of those pairs happens. This means that in dry/live you need to be mindful of having as low a computation delay as possible, usually by running a low number of pairs and having a CPU with a good clock speed.

Why Can't I See "Real Time" Candle Data?

Freqtrade does not store incomplete/unfinished candles in the dataframe.

The use of incomplete data for making strategy decisions is called "repainting" and you might see other platforms allow this.

Freqtrade does not. Only complete/finished candle data is available in the dataframe.

Customize Indicators

Entry and exit signals need indicators. You can add more indicators by extending the list contained in the method populate_indicators() from your strategy file.

You should only add the indicators used in either populate_entry_trend(), populate_exit_trend(), or to populate another indicator, otherwise performance may suffer.

It's important to always return the dataframe from these three functions without removing/modifying the columns "open", "high", "low", "close", "volume", otherwise these fields would contain something unexpected.

Sample:

def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Adds several different TA indicators to the given DataFrame

Performance Note: For the best performance be frugal on the number of indicators
you are using. Let uncomment only the indicator you are using in your strategies
or your hyperopt configuration, otherwise you will waste your memory and CPU usage.

:param dataframe: Dataframe with data from the exchange
:param metadata: Additional information, like the currently traded pair
:return: a Dataframe with all mandatory indicators for the strategies
"""
dataframe['sar'] = ta.SAR(dataframe)
dataframe['adx'] = ta.ADX(dataframe)
stoch = ta.STOCHF(dataframe)
dataframe['fastd'] = stoch['fastd']
dataframe['fastk'] = stoch['fastk']
dataframe['bb_lower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband']
dataframe['sma'] = ta.SMA(dataframe, timeperiod=40)
dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9)
dataframe['mfi'] = ta.MFI(dataframe)
dataframe['rsi'] = ta.RSI(dataframe)
dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5)
dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50)
dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)
dataframe['ao'] = awesome_oscillator(dataframe)
macd = ta.MACD(dataframe)
dataframe['macd'] = macd['macd']
dataframe['macdsignal'] = macd['macdsignal']
dataframe['macdhist'] = macd['macdhist']
hilbert = ta.HT_SINE(dataframe)
dataframe['htsine'] = hilbert['sine']
dataframe['htleadsine'] = hilbert['leadsine']
dataframe['plus_dm'] = ta.PLUS_DM(dataframe)
dataframe['plus_di'] = ta.PLUS_DI(dataframe)
dataframe['minus_dm'] = ta.MINUS_DM(dataframe)
dataframe['minus_di'] = ta.MINUS_DI(dataframe)

# remember to always return the dataframe
return dataframe
Want More Indicator Examples?

Look into the user_data/strategies/sample_strategy.py. Then uncomment indicators you need.

Indicator Libraries

Out of the box, Freqtrade installs the following technical libraries:

Additional technical libraries can be installed as necessary, or custom indicators may be written/invented by the strategy author.

Strategy Startup Period

Some indicators have an unstable startup period in which there isn't enough candle data to calculate any values (NaN), or the calculation is incorrect. This can lead to inconsistencies, since Freqtrade does not know how long this unstable period is and uses whatever indicator values are in the dataframe.

To account for this, the strategy can be assigned the startup_candle_count attribute.

This should be set to the maximum number of candles that the strategy requires to calculate stable indicators. In the case where a user includes higher timeframes with informative pairs, the startup_candle_count does not necessarily change. The value is the maximum period (in candles) that any of the informatives timeframes need to compute stable indicators.

You can use recursive-analysis to check and find the correct startup_candle_count to be used. When recursive analysis shows a variance of 0%, then you can be sure that you have enough startup candle data.

In this example strategy, this should be set to 400 (startup_candle_count = 400), since the minimum needed history for ema100 calculation to make sure the value is correct is 400 candles.

dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)

By letting the bot know how much history is needed, backtest trades can start at the specified timerange during backtesting and hyperopt.

Using Multiple Calls to Get OHLCV

If you receive a warning like WARNING - Using 3 calls to get OHLCV. This can result in slower operations for the bot. Please check if you really need 1500 candles for your strategy - you should consider if you really need this much historic data for your signals.

Having this will cause Freqtrade to make multiple calls for the same pair, which will obviously be slower than one network request.

As a consequence, Freqtrade will take longer to refresh candles - and should therefore be avoided if possible.

This is capped to 5 total calls to avoid overloading the exchange, or make Freqtrade too slow.

Startup Candle Count Limit

startup_candle_count should be below ohlcv_candle_limit * 5 (which is 500 * 5 for most exchanges) - since only this amount of candles will be available during Dry-Run/Live Trade operations.

Example

Let's try to backtest 1 month (January 2019) of 5m candles using an example strategy with EMA100, as above.

freqtrade backtesting --timerange 20190101-20190201 --timeframe 5m

Assuming startup_candle_count is set to 400, backtesting knows it needs 400 candles to generate valid entry signals. It will load data from 20190101 - (400 * 5m) - which is ~2018-12-30 11:40:00.

If this data is available, indicators will be calculated with this extended timerange. The unstable startup period (up to 2019-01-01 00:00:00) will then be removed before backtesting is carried out.

Unavailable Startup Candle Data

If data for the startup period is not available, then the timerange will be adjusted to account for this startup period. In our example, backtesting would then start from 2019-01-02 09:20:00.

Entry Signal Rules

Edit the method populate_entry_trend() in your strategy file to update your entry strategy.

It's important to always return the dataframe without removing/modifying the columns "open", "high", "low", "close", "volume", otherwise these fields would contain something unexpected. The strategy may then produce invalid values, or cease to work entirely.

This method will also define a new column, "enter_long" ("enter_short" for shorts), which needs to contain 1 for entries, and 0 for "no action". enter_long is a mandatory column that must be set even if the strategy is shorting only.

You can name your entry signals by using the "enter_tag" column, which can help debug and assess your strategy later.

Sample from user_data/strategies/sample_strategy.py:

def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the entry signal for the given dataframe
:param dataframe: DataFrame populated with indicators
:param metadata: Additional information, like the currently traded pair
:return: DataFrame with entry column
"""
dataframe.loc[
(
(qtpylib.crossed_above(dataframe['rsi'], 30)) & # Signal: RSI crosses above 30
(dataframe['tema'] <= dataframe['bb_middle']) & # Guard: tema below BB middle
(dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising
(dataframe['volume'] > 0) # Make sure Volume is not 0
),
'enter_long'] = 1

# Uncomment to use shorts (Only used in futures/margin mode. Check the documentation for more info)
"""
dataframe.loc[
(
(qtpylib.crossed_below(dataframe['rsi'], 70)) & # Signal: RSI crosses below 70
(dataframe['tema'] > dataframe['bb_middle']) & # Guard: tema above BB middle
(dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling
(dataframe['volume'] > 0) # Make sure Volume is not 0
),
'enter_short'] = 1
"""

return dataframe
Entry Tags

You can set entry tags to help identify which condition triggered the entry:

dataframe.loc[
(dataframe['rsi'] < 30),
['enter_long', 'enter_tag']
] = (1, 'rsi_oversold')

Next Steps

This covers the basics of strategy customization. For more advanced topics, see:

Resources