Home Creators Posts Import Register Favorites Logout

Content

Introduction

Hi there, and welcome to another post where I will test one of the strategies that the Freqtrade developers have left for us in their Github repository.

As I said in my last post and Youtube video, the coming tests to find the best algorithmic bot trading strategies will be coming from this repo. I am picking the already low hanging fruits here and at some point I was going to review and test these strategies anyways. So why not take these and test them now to attain the knowledge and experience from people that already have coded these algo strategies.

I will try to keep these posts short and only stick to the basic explanation of these strategies unless they are performing above exceptionally well or are coded in such an interesting way that this is worth mentioning.

Patreon members get additional benefits because there I go into more details about how I optimize this strategy. Also you can get all the output and strategy as a Patreon supporter!

The ‘average strategy’

This time I will test the "AverageStrategy" and this strategy is again created by Gert Wohlgemuth.

It is a very simple strategy that only makes use of two exponential moving averages. The author mentions in the comments that it is just a proof of concept and really does not perform well. But we will see about that

"""
author@: Gert Wohlgemuth
idea:
buys and sells on crossovers - doesn't really perfom that well and its just a proof of concept
"""

Timeframe, ROI & Take profit

This stragey is originally created to be used on the 4 hour timeframe.

The ROI setting is set to take profit at 50% gains, no matter how much time it takes. Which is indicated by the 0 in the minimal_roi config section.

This simple strategy also has a stop loss setting that sells the position after it has suffered a loss of 20% after the initial buy.

Another thing to mention here is that this strategy is prepared to do hyperparameter optimisation on the buy range as well. So it will take the EMA parameter in consideration here and tries to search in the configured spaces to find the most profitable settings for the strategy.

INTERFACE_VERSION: int = 3
# Minimal ROI designed for the strategy.
# This attribute will be overridden if the config file contains "minimal_roi"
minimal_roi = {
"0": 0.5
}
# Optimal stoploss designed for the strategy
# This attribute will be overridden if the config file contains "stoploss"
stoploss = -0.2
# Optimal timeframe for the strategy
timeframe = '4h'
buy_range_short = IntParameter(5, 20, default=8)
buy_range_long = IntParameter(20, 120, default=21)

Used indicators in this strategy

The EMA indicator is used in this strategy.

In the buy space set in the previous section, the short EMA is set to a default value of 8 and the long EMA is set to a default value of 21.

You can see on the screen below that on the bitcoin chart this crossover strategy looks to follow the price band quite narrowly. The 8 EMA is almost the same as the bitcoin daily price movement and the 21 EMA follows close, but not close enough to create all kinds of false crossover signals.

EMA 8 and 21 on the XBT chart

The dataframe columns are created with a simple for loop statement here that will create a dataframe column for both the short and long EMA,

def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# Combine all ranges ... to avoid duplicate calculation
for val in list(set(list(self.buy_range_short.range) + list(self.buy_range_long.range))):
dataframe[f'ema{val}'] = ta.EMA(dataframe, timeperiod=val)
print(dataframe)
return dataframe

I added the print statement here myself to see how this would look and you can see below the output to my screen when I backtested this strategy.

Buy and sell settings

The buy and sell settings of this strategy are as simple as the rest of the code. The buy signal will be given when the short EMA will cross above the long EMA and the volume of the pair is above 0. This means that the pair is only bought at the crossover and not as long as the short EMA is above the long EMA.

This difference should be taken into consideration. If this strategy had a buy signal like this "short EMA > long EMA", then it could happen that the bot sells because the ROI is reached. But in that condition the bot can also buy a position again as long as the short EMA is above the long EMA.

With the crossed above setting, it only buys positions at that specific occurrence and only sell the position if one of three conditions are met (ROI, Sell signal, Stoploss). And will not buy a position anymore as long as there is no crossover event happening.

That same principle also goes for the sell reason. The bot only sells on the crossed above event. Not earlier and not later. For this sell situation in this spot trading strategy it is not such a big deal, but when trading with futures is will be again. In that case some additional trading opportunities could be missed.

But for now this is enough explanation. Let’s see how this strategy performs with the current settings of the short EMA set to 8 and the long EMA set to 21.

def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

"""
Based on TA indicators, populates the buy signal for the given dataframe
:param dataframe: DataFrame
:return: DataFrame with buy column
"""
dataframe.loc[
(
qtpylib.crossed_above(
dataframe[f'ema{self.buy_range_short.value}'],
dataframe[f'ema{self.buy_range_long.value}']
) &
(dataframe['volume'] > 0)
),
'enter_long'] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the sell signal for the given dataframe
:param dataframe: DataFrame
:return: DataFrame with buy column
"""
dataframe.loc[
(
qtpylib.crossed_above(
dataframe[f'ema{self.buy_range_long.value}'],
dataframe[f'ema{self.buy_range_short.value}']
) &
(dataframe['volume'] > 0)
),
'exit_long'] = 1
return dataframe

Initial backtest results

So after I have done the initial backtest procedure over the different timeframes if usually test, I get the results you see below.

If you wonder why you do not see timeframes lower than 1 hour, that is because I did nog get any results back from these timeframes on my computer. Maybe it has something to do with the amount of memory I had or some other issue with my PC. But I did not get any results back unfortunately.

My Patreon members will get all the output I generate in a separate addendum post so if you want to check up on that and you are not a supporter yet, then here is your chance to get all the additional benefits Patreon supporters have.

Of the three timeframes I did get results from, the 4 hour timeframe proved to be the most profitable one. And that is exactly the timeframe this strategy was build for.

However, there are other factors at play to determine if a algostrategy is good. On the 4 hour timeframe there are issues which prevent me from scoring this timeframe as the highest. Especially the drawdown experienced, the profit factor and the applicability on multiple asset pairs scored lower here and that is why the 1 day timeframe still seems to be the best to trade this strategy on (initially).

However if you yourself want to take more risk and do not mind the nail-biting drawdown this 4 hour timeframe has, than you can optimize this timeframe further on your own system.

I am not that tough, and do not mind taking a little bit less risk in favor of a higher winrate and Calmar ratio. So therefore I will optimize the 1 day timeframe further to see if optimizing the ROI, Stoploss and BUY parameters (better EMA settings) can improve this simple algo strategy further.

Hyperparameter optimization

After optimizing this simple Average strategy with the HyperParameter optimizaton funtion I have got some adjusted settings for ROI, Stoploss and EMA.

The ROI settings are changed from one simple 50% takeprofit point at any time to having multiple take profit points at multiple times. The stoploss setting has been widened so to let this strategy breathe more (apparently this 20% stoploss point was still too narrow). Also both the short and long EMA parameters have been adjusted to make this strategy less prone to ‘unnecessary’ exit points.

You can see on the picture below that the profit percentage was again drastically reduced in favor of a lower drawdown and higher win rate. The Sortino and Sharpe ratio also lowered, but the profit factor, expectancy and Calmar ratio improved once more. Also the win rate went from only 34% to 50%.

To see which changes this optimization caused, we have to look at the exit reasons. The initial 1 day exit reasons show that the ROI setting is the main cause for getting profits. This means that with the initial setting, every time a trade reached 50% profit, this profit was taken (155 of the 614 trades) and 51 exit signals also proved to be winning trades, but still this exit signal has more losses than winners.

The 20% stop loss point was reached in 92 trades. It this 20% too narrow for this strategy..?

The new settings changed the behavior drastically. Still the main reason for exits with losses are the exit signals (EMA short crosses below EMA long). But this is not as many as before. Also the stop loss was only reached in 7 trades.

Due to the change in EMA settings, there were less signals to trade on (first 614, now only 207). But due to the changed ROI take profit points 50% of these trades ended up winners.

So less trades but higher chances of winning trades.

Looking at the strategy score, you can see that both the old and new strategy settings end up in an end score of 215. This is just a coincidence because the scoring of the different ratios and factors are different but scored the same at the end.

Which strategy settings score will end up in the strategy league is pure a matter of personal preference and because this scoring is the same this strategy will end up on the same spot in the league anyways.

But because my current view on things is to take the least risk as possible (at the moment we still are in a bear market), I will take the optimized strategy and put these results in the strategy league.

Strategy League

Looking at the Strategy League, with an end score of 215 this simple but effective strategy ends up on the second spot. For me again a surprise.

Apparently simple is best and determining the best take profit points based on the ROI settings is key in this strategy (and perhaps in every strategy…).

Although it is not the most profitable one, and some ratios are also a little bit worse than lower ‘scoring’ strategies, with these settings it still ends up nicely in this league. Two of the main reasons for this position is the profit factor and the applicability to use this strategy on 66% of the 50 pairs configured for these tests.

For example, the Godstra strategy has way more profit but only 24% of the configured pairs react to this algo strategy. So it is a gamble if your preferred pairs react well to Godstra, whereas the Average strategy has higher chances to be profitable for a random crypto pair.

But still this has to be found out by rigorous testing of course!

So, again a strategy tested. Let’s quickly move on to the next strategy to see how it works and if it has the probability to be profitable, or not…

Goodbye!

Files

All files will be available on my Patreon site. So if you want to support me by buying me a cup of coffee or sandwich once in a while please do so, and gain additional benefits from it as well!