hkwo

kNN ML EMA Ribbon Trend Forecast Strategy

Algorithm Evaluation Case Study

Purpose:
This trading algorithm attempts to simulate the decision-making of a periodic long-term stock trader. Its main feature is the inclusion of a Machine Learning forecast line indicator that utilizes Euclidean distance-based calculations to construct a simple moving average of up to 200 future bars (adjusted to a set time frame).

Strategy:
The strategy complements the Machine Learning indicator by joining it with an EMA Ribbon, the EMA 200 line, as well as a version of LuxAlgo’s TrendLines with Breaks indicator. The ML sma acts as a forecast trend indicator while the EMA Ribbon and trendline breaks add extra confirmation by acting as the momentum/swing indications. It strives to automate an efficient and swift, yet human-like, decision-making process when creating entry/exit trades.
The strategy is executed when the following conditions are met:
  • The Forecast line shows an up/down trend that amounts to a higher price value than the user-set stop loss/take profit lines.
  • The EMA Ribbon provides buy/sell signals when the price breaks through the extreme (can be edited to only allow trades to occur when breaks a specific EMA). EMA lines typically are more commonly-used
  • The EMA 200 line provides a larger picture and longer-term indication of the trend. Only long trades can be entered when above the line and vice versa. Furthermore, the calculation of the forecast line data point at a specific time is enhanced by the EMA 200 line to allow for more accurate real-time trade entries/exits.
  • When all of the above conditions are met AND a confirmed breakout signal is printed, confirming a trend reversal, the trade will be entered/exited

Backtest:
I ran this algorithm through the Tesla 1-hour timeframe to undergo supervised learning of a pre-existing dataset. Although the strategy may not be the most effective at 42.2%, it demonstrates an upward trending profit margin over time which demonstrates its ability to perform within this specific dataset. However, I recommend testing the forecast line with other indicators to improve the percent profitability.

The k-Nearest Neighbors model is a supervised Machine Learning training method that is fed vast amounts of data, in this case, historical charts, and quickly covers all data points by plotting them on one-multi dimensional graphs and compares the nearest neighbors when spotlighting the current data point. For this specific model, it predicts which price point price will proceed to the current point by computing the distance between the k amount of nearest neighbors and averaging them into one. Unlike man multi-dimensional analysis models, this model does not utilize indicator features as a part of the prediction process (however it may be implemented in the future). In terms of how well it compares over historical data, it fails to consider a larger variety of predictable indications which prevents it from nearing a guaranteed high win rate. It is recommended that this strategy is tested across the hour timeframe due to its usage of a variety of EMA lengths, as well as more stable volatility within the section. Be sure to change the timeframe of the Forecast line to the desired candle length to maintain consistency.

Optimization:
Beyond the TradingView strategy is a separate repository that includes Python scripts that utilize optimization Machine Learning models to better the main model's performance. Derived from TreborNamor’s Machine Learning TradingView script, added is the optimization for the k amount within the forecast line calculations which utilizes a reinforcement learning kind of machine learning model, as well as the ability to swiftly select a specific candle timeframe. This is done through repeated analysis of preexisting information through the chart’s backtesting metrics, which then outputs the optimal combination for the highest profit margin. Typically speaking, if one were to desire a lower risk margin then they would settle for a higher stop loss and lower take profit percentages, and vice versa. However, the optimization method determines the highest profit margin for existing back testable chart data and is not an accurate representation of the stop/loss and take/profit that should be used indefinitely.

Disclaimer: The information presented in this publication is for educational purposes only and should not be considered financial advice. Always conduct thorough research and seek advice from financial professionals before making any investment decisions.

Github:
https://github .com/hakwok/TradingView-Machine-Learning-Optimization-GUI
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?