Home Creators Posts Import Register Favorites Logout

Content

Hello there and welcome to another post where I will discuss and test the MACD crossed strategy. In this strategy the MACD and CCI indicators are used to create signals for buying and selling crypto. Let’s find out how it performs.

I am still going strong on the strategies that are provided by the Freqtrade developers and this time I am testing the MACD crossover strategy which also is available in their github repository. That Berlinguyinca did an amazing job again by providing us with the code. 

As always I will go over the code, test the initial strategy performance and then see if it would perform better if I optimize the parameters that are available to optimize. So let’s quickly start by looking at the code:

The strategies code

Of course, as with any Freqtrade strategy file, first of all the additional modules are loaded.

# --- Do not remove these libs ---
from freqtrade.strategy import IStrategy
from typing import Dict, List
from functools import reduce
from pandas import DataFrame
# --------------------------------
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
Then, after the class has been created, there is a small explanation of what the strategy does:
class MACDStrategy_crossed(IStrategy):
"""
buy:
MACD crosses MACD signal above
and CCI < -50
sell:
MACD crosses MACD signal below
and CCI > 100
"""

So when the macd line crosses above the macd signal above when the CCI is in oversold territory of -50, then a buy signal is generated.

When the MACD crosses below its signal line when the CCI is in the overbought area of +100, then a sell will be executed.

A pretty simple strategy if I say so myself.

This explanation is then followed by the usual mandatory strategy parameters of ROI, stop loss and timeframe.

INTERFACE_VERSION: int = 3
# Minimal ROI designed for the strategy.
# This attribute will be overridden if the config file contains "minimal_roi"
minimal_roi = {
"60":  0.01,
"30":  0.03,
"20":  0.04,
"0":  0.05
}
# Optimal stoploss designed for the strategy
# This attribute will be overridden if the config file contains "stoploss"
stoploss = -0.3
# Optimal timeframe for the strategy
timeframe = '5m'

Already I can see that the strategy will possibly run into problems because the stoploss is already 6 times bigger then the highest ROI profit taking here…

The proposed timeframe for this strategy is the 5 minute timeframe, based on these initial settings.

After this, the strategies indicators are defined in the following method:
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
macd = ta.MACD(dataframe)
dataframe['macd'] = macd['macd']
dataframe['macdsignal'] = macd['macdsignal']
dataframe['macdhist'] = macd['macdhist']
dataframe['cci'] = ta.CCI(dataframe)
return

The MACD is set up with the default indicator values. Also the CCI here is set up with it’s default indicator values.

So as you can see. A pretty simple strategy.

Buy signals

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['macd'], dataframe['macdsignal']) &
(dataframe['cci'] <= -50.0)
),
'enter_long'] = 1
return

Buying an asset happens as stated in the strategy explanation at the start of this code.

A signal is generated when the MACD crosses above the signal line when the CCI is also below the -50 level.

Sell signals

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_below(dataframe['macd'], dataframe['macdsignal']) &
(dataframe['cci'] >= 100.0)
),
'exit_long'] = 1
return dataframe

And the same goes for the sell signals. A sell signal gets generated when the MACD crosses below the signal line while the CCI is higher than 100.

Let’s test this setup on multiple timeframes to find out which timeframes has the best response and therefore the best probability for future gains.

Initial backtest results

The initial backtest results show that the 1 day timeframe reacts best to the strategy and it’s setup.

While most of the configured pairs respond to this strategy, the gains are not that great. Also a winrate of almost 95 percent looks very good. But what is keeping this great winrate to not have such high profits?

With only 227 trades over 5 years of data, this strategy certainly tests your patience. Apparently the circumstances the buy triggers are configured on, are so rare that these occur not many times.

And as predicted the 10 stoploss triggers almost eat up 75% of the total amount of profits.

There’s not much to say then try to see if optimizing the ROI and stop loss levels improve these results. If we are lucky, the profit taking will improve in comparison with the losses from the stop losses.

Optimizing and testing results

So after optimizing the ROI and stop loss the results of the strategy looks like this. There is a slight improvement in profits but, as we know already by now, other ratios can get worse.

So in this case, you will experience more losing trades and the amount of pairs that react good on these settings will also get worse. But the profits get better and the drawdown also improves.

The most noticable thing is that the amount of trades do not get a lot higher than the earlier 227. Now there are 239 trades in total, which is still not that many over 5 years. For each successful trade there is almost a losing trade, but the average profit per winning trade is now around 5% higher.

Profit plots

Let’s take a look at the plots to visually experience what this strategy does over time.

The plot of the initial backtest shows a steep drawdown around august 2019.

A little bit before that XRP also showed a big loss and in the drawdown period another few pairs caused this strategy to perform bad.

If I look alt the altcoin chart, then the months of august and september in 2019 performed not really well. But to say that there was a major crash going on is a little bot overdone.

The big crash happened already a year before so why these sudden drops appear in the chart should be examined further.

In any case these stop losses did exactly what I said. They caused a big hole in your trading account and from that moment on the strategy slowly crawled into a profit again. If this had not happened, it could perform much better I think.

The chart of the optimized settings looks a lot better. Already from the start it adds money to your trading account and the drawdown and underwater plot shows that only from profit peaks there are some losses.

To me this chart gives me much comfort because if I started with 1000 dollar, it would never go below that amount. As far as I can tell from these graphs.

However there is something particular in both these charts and that is the parallelism plot. You can see that the amount of trades is always relatively low and sometimes it peaks to 10 parallels trades. But not many times over the complete backtesting period. This means to me that the strategy settings might be too tight and prevents trades because it never reaches the levels configured

Maybe its because the -50 CCI lower level is just too low and should be even higher, or that the exact MACD crossover never happens when the CCI is below -50. More investigation is needed to make this strategy work better.

The strategy league

Looking at the results of both these tests I have a dilemma. The initial backtest has the better score, mainly because of the winrate and of the way the pairs react to these settings, which are pretty default.

The only thing that bothers me is that, under specific circumstances some pairs can cause major drawdowns and I am not sure why this happens at the moment. After that drawdown event it did made continuous profits and in the end it made some profits. But you had to be patient and faith in this strategy the whole time. Something that is not easy to do.

The optimization and improvements of the roi and stoploss made this strategy perform way better. But now some important factors score pretty low and therefore caused the overall score also to be lower. While the Calmar, Sortino and Sharpe ratio say that this is the better setup.

Now what to do?

This time I think I go against my own scoring and follow my gut feeling and well established ratios. Also the fact that there are only ten points separating the scoring of the initial and optimized settings; And the fact that I do not like these sudden unexplained drops in the initial settings makes me wary of this strategy. No matter what the settings are.

So therefore I will use the optimized strategy settings as the scoring in the Strategy League. I will give it lots of the credits it deserves but still do not want to score it that high because I did not experience these strange drops in other strategies. Also the optimized settings do not have these, which makes me say that this actually is the better. and more realistic one.

But nothing stops you from improving this strategy to your own situation and likings and maybe this is the best one for your situation. Who knows…

Thank you for visiting my blog and I hope you will visit some time later when I have tested another algo strategy.

Files

As always, all output files are available on my Patreon page. This way you can check my results and simulate these with your own setup. Maybe this strategy does not work good in my setup but works absolutely fantastic on yours. Also read my suggestions to improve this strategy i give over there. Maybe this improves the performance of the strategy even better… Hope to read your responses!

Comments

Sergey Dzhenganin

Could you please leave short instructions on where and how to place hyperopt files? Please and thank you!

Dutch Algotrading

just now Hyperopt files should reside in the same directory as the strategy file (both in the same dir). FT will catch the optimized file and will then use these settings. You can check this in the log you see on the screen when backtesting. Hope this helps.

Sergey Dzhenganin

will you be kind enough to provide the web page link for the first two winning strategies?