The SMAIP3v2 High Ranking Trading Algo. TSL or No TSL?? (Patreon)
Downloads
Content
Introduction
Hello, everyone, and welcome back to my channel where I dive deep into cryptocurrency trading strategies and their results!
This time, I’m exploring a fascinating Freqtrade strategy developed by the user known as @tirail.
This strategy, named SMAIP3v2_NoTSL, is a prime example of how to leverage technical indicators to make informed trading decisions in the crypto market.
I’ll walk through the code block by block, explaining each part so you can understand how it works and possibly implement or adapt it yourself.
So grab your coffee, and let's get into the nuts and bolts of this trading strategy!
Strategy Origins
If you search on Girhub, there are some code repositories that contain this code.
https://github.com/search?q=SMAIP3v2&type=code
https://github.com/PeetCrypto/freqtrade-stuff
https://github.com/PeetCrypto/freqtrade-stuff/blob/main/SMAIP3v2.py
https://raw.githubusercontent.com/PeetCrypto/freqtrade-stuff/main/SMAIP3v2.py
I will open the first one you see here and walk you through the code line by line.
Strategy Explanation
The strategy begins by importing essential Python libraries and modules:
These imports include `datetime` for handling date and time objects, `talib` for technical analysis functions, `pandas` for data manipulation, and various components from `freqtrade` to define and manage the strategy’s parameters and trading logic.
This following class block defines the strategy class `SMAIP3v2_NoTSL`, inheriting from Freqtrade's `IStrategy`. It sets up initial parameters like buy and sell conditions, stop-loss, and return on investment (ROI). The strategy uses a mix of simple moving averages (SMA) and exponential moving averages (EMA) as the core technical indicators to generate buy and sell signals.
These lines define parameters that can be optimized using tools like Hyperopt, a component within Freqtrade for optimizing strategy parameters. Parameters such as `base_nb_candles_buy`, which defines the number of candles to consider for buying, and `buy_trigger`, which specifies whether to use SMA or EMA for the buying indicator, are set up to be adjustable.
This strategy has been configured to use the Trailing stop loss since it gives a positive bias to the backtests. Which can make the strategy results more favorable than that they actually are. So you can understand I am not a fan of this feature when backtesting. That’s why I also created a file where the TSL has been disabled and tested that version also. I will show you the difference between these results a little bit later in the video.
As for the timeframe, it has been designed to work best on the 5 minute time frame. We’ll discover later if that is actually the best timeframe to let this strategy work on.
The confirm_trade_exit method in the SMAIP3v2_NoTSL strategy determines if a trade should be closed based on recent price action and momentum. It checks if the most recent candle's opening price and RSI (Relative Strength Index) are both higher than the previous candle's, indicating continued upward momentum. If these conditions are met during a sell due to ROI achievement, a sell signal, or a trailing stop loss, the method will prevent the trade from closing, suggesting the potential for further gains.
In the populate_indicators method of the SMAIP3v2_NoTSL strategy, several key technical indicators are calculated:
The method calculates two EMAs with periods of 50 and 200, which are used to analyze longer-term price trends. The EMA-50 tracks mid-term trends, while the EMA-200 gives insights into more extended trends.
An RSI with a very short period of 2 is used to gauge the momentum and possible overbought or oversold conditions.
Depending on the strategy's run mode and parameters, it calculates buy and sell offsets from moving averages defined by buy_trigger and sell_trigger values. These offsets are adjusted by low_offset and high_offset, respectively, helping to fine-tune the entry and exit points based on price movements relative to these averages.
And the Bad Pair Indicator here flags a pair as potentially bad for trading if the price has dropped significantly over the last 12 or 6 candles compared to its closing price. Based on thresholds set by pair_is_bad_1_threshold and pair_is_bad_2_threshold. This helps avoid trading in pairs that show unfavorable price drops, which might indicate a lack of buyer interest or a strong downtrend.
Trading Logic
Buy Logic
During the hyperoptimization process (hyperopt), it dynamically calculates moving average offsets for buying (ma_offset_buy) based on the configured moving average type (buy_trigger), number of candles (base_nb_candles_buy), and offset multiplier (low_offset). Additionally, it evaluates if a trading pair should be considered unfavorable (pair_is_bad) based on significant recent price drops over 12 and 6 candles.
As for the Buy Conditions, If The short-term moving average (EMA-50) is above the long-term moving average (EMA-200) it is suggesting an uptrend.
When the closing price is above the long-term average the uptrend is confirmed.
Also this only works if The pair is not flagged as bad, indicating it's favorable for trading.
If The closing price is below the buy offset moving average, it suggests a good entry point in an uptrend (buying the dip).
And finally there should be Positive trading volume. This confirms there is enough market activity to support the entry.
To round this buy section of:
This setup aims to buy during uptrends but only after a slight retracement, potentially allowing for buying at a lower price in a rising market.
Sell Logic
Similar to the buy logic, during hyperoptimization, it calculates moving average offsets for selling (ma_offset_sell) based on the sell trigger parameters (sell_trigger, base_nb_candles_sell, and high_offset).
To get a sell condition
When the closing price is above the sell offset moving average, it suggests that the price has reached a sufficient height above the expected trend to lock in profits.
And there should also be positive trading volume. Ensuring that the market activity can support the exit.
This strategy aims to secure profits by selling when the price is perceived to be peaking above the normal range, thus taking advantage of the heightened market conditions.
Together, these buy and sell strategies seem to form a coherent approach to trading that aims to enter the market during promising conditions and exit when a sufficient profit margin is achieved or when the market shows signs of a downturn.
Lookahead Bias
Next to the bias that is added when the trailing stop loss feature is used, there can also be lookahead bias included in the trading algorithm as a result of bad coding.
Luckily there is also a function in the backtesting engine that can spot most of the lookahead biases.
I also have run this to detect any lookahead bias, but as far as the function can spot, there is no bias detected. Which makes me more comfortable showing you the results I got from testing this strategy.
Backtest results
In this section I will present you the results of the backtests I did with the trailing stop loss enabled and with the TSL disabled. To show you the differences in results.
But I will discuss the no TSL results more, since I add more value to these results. As I told you earlier.
With TSL
With TSL enabled, the results of the strategy come at a very admirable end balance of more than18000 dollar. That’s a profit percentage of over 1700. On the 15 minute time frame the win percentage is 76 percent and 85 percent of the pairs favor this algorithm.
Without TSL
Without the TSL enablewe get a whole different picture here. Suddenly the 5 minute timeframe has the best score. Although the 15 minute follows close by to be honest.
Note however that the end balance and profit percentage on both timeframes are much lower here.
There is another interesting and important thing going on here as well. The drawdown seems to be much lower. And the Calmar, Sortino and Sharpe ratio favor the no TSL strategy more than with it enabled.
So here there are lower gains but the risk profile seems to be much better. An very interesting observation here…
Equity curve
With TSL
The second thing that is noticeable is the equity curve of both these versions.
The TSL versions equity curve rises very fast and after the bull market top flattens out. But still has a jagged profile. The gains are also more spread out over the complete chart.
Without TSL
Whereas the no tsl version has a steeper inclination in the beginning as a result of more profitable weeks during the start of the last bull market. But then the gains flatten out even more.
The most noticeable thing here is that there are no high drawdowns as far as I can see.
Drawdown
Without TSL
On the plot you see here, the highest drawdowns occur at the start and the end of the plotted time period. On average there is only a 0.6 percent drawdown over this period. And that is very good in my opinion.
Maybe a little bit too good to be honest because in my experience a drawdown of 10 percent or so is also very good, and more realistic in my opinion. But maybe I am too overcritical here.
Other plots
Without TSL
Winrate Distribution:
The left plot shows a boxplot of winrate distribution, which is quite encouraging. The median winrate is around 0.75 (75%), indicating a relatively high probability of individual trades being successful. Most winrates cluster around the median, with few outliers, suggesting the strategy consistently achieves a winrate between 60% to 80%.
Profit Distribution:
The right plot presents the weekly profit distribution. This boxplot reveals a median profit near zero, with most data points showing modest gains. However, the spread of profits, especially the outliers marked in red, indicates significant positive spikes, with some profits reaching as high as 600 usdt in certain weeks.
Volatility in Profits:
What can we say about these two boxplots. Now, despite a generally stable winrate, the profit distribution suggests high volatility in earnings. The wide range of profit outcomes, particularly the outliers, could imply that while the strategy often makes small gains, it occasionally captures large profitable moves, possibly during periods of high market volatility.
Risk Consideration:
The presence of these outliers with high profits is positive, but it's essential to consider the potential risks as well. Strategies with high profit variability might also carry a higher risk, which should be evaluated against the trader's risk tolerance and portfolio goals.
Performance Analysis:
The disparity between the stable winrate and the variable profit suggests that the strategy might be entering trades that are generally successful but vary widely in profitability. This could be due to varying market conditions or the strategy’s parameters like stop-loss and take-profit settings influencing the exit points of trades.
So, Overall, the plots suggest that while the SMAIP3v2_NoTSL strategy has a decent winrate, its profitability can be highly inconsistent. Traders might want to consider further optimization or risk management adjustments to enhance the predictability and stability of returns.
When compared to other strategies, SMAIP3v2_NoTSL appears to be a top contender, often ranking in the top tier across most indicators. This indicates that despite some risks, this strategy manages to deliver competitive returns, maintaining a good balance between risk management and profitability.
Overall, the SMAIP3v2_NoTSL strategy shows a strong performance profile, making it a potentially attractive option for traders looking for robust returns with managed risk levels.
Strategy League
At the moment this strategy has a very admirable place in the overall strategy league with its good results and decent risk management capabilities.
The trailing stop loss version managed to get a place in the top 10 of best performing algorithms. And the no TSL version also scores pretty well here too.
The version without trailing stop loss enabled was tested way way later. after I also tested out many other strategy performances. These I have to work out into video and blog posts so consider these other strategies as a teaser of things yet to come.
With TSL
Without TSL
I think that this trading algorithm certainly has good potential for the markets.
But remember that you only should use this in actual live trading after you do your own research, it fits your risk appetite and do extensive back and forward testing this on your own trading set up.
Ending
Many thanks for watching this video. If you like these kind of tests and analysis, please click the like button and subscribe to the channel.
If you want to support me then please be a patron on Patreon. You also get the additional benefits of getting all backtest output, code, plots and more to run this on your own setup.
This is it for now.
And I will see you in the next video…
Goodbye!