Harrocop

MACD + MA HTF Strategy - Dynamic Smoothing

MACD + MA HTF Strategy - Dynamic Smoothing
The MACD alone generally gives too many false signals and is therefore often used in combination with different indicators. The basic idea to combine the MACD with a moving average on a higher time frame is a commonly used technique to only enter Longs in a uptrend and only Shorts in a downtrend while trading on lower timeframe charts. However, the main issue in many strategy scripts is that the HTF indicator is not visible on lower timecharts. With this strategy example I used the Dynamic Smoothing code to visualise the HTF MA filter to display on you lower timechart. This way it is easier to optimize the strategy settings to the instrument chart.

Orginality and Usefulness - Dynamic Smoothing
Visualizing a higher time frame on a lower timechart often gives jagged lines on your chart. As the calculation is done on less bars, compared to the bars you have open on your timechart. The dynamic smoothing factor is derived by taking the ratio of minutes of the higher time frame to the current time frame. This ensures the moving average remains fluid and consistent across different time frames, eliminating 'jagged' lines on your chart. This new MA value is then used as HTF filter on top of the MACD entry settings. Always make sure the time chart is equal or lower than the timeframe settings in the configuration settings. The intention of the script is to visualise the higher time frame confirmations while trading on a lower timechart.

Code example how to achieve Dynamic Smoothing:
// Get minutes for current and higher timeframes
// Function to convert a timeframe string to its equivalent in minutes
timeframeToMinutes(tf) =>
    multiplier = 1
    if (str.endswith(tf, "D"))
        multiplier := 1440
    else if (str.endswith(tf, "W"))
        multiplier := 10080
    else if (str.endswith(tf, "M"))
        multiplier := 43200
    else if (str.endswith(tf, "H"))
        multiplier := int(str.tonumber(str.replace(tf, "H", "")))
    else
        multiplier := int(str.tonumber(str.replace(tf, "m", "")))
    multiplier

// Get minutes for current and higher timeframes
currentTFMinutes = timeframeToMinutes(timeframe.period)
higherTFMinutes = timeframeToMinutes(TimeFrame_Trend)

// Calculate the smoothing factor
dynamicSmoothing = math.round(higherTFMinutes / currentTFMinutes)
MA_Value_Smooth = ta.sma(MA_Value_HTF, dynamicSmoothing)

Complete Strategy
The MACD and HTF moving average is used to determine when to enter a new position. However a strategy should consider more factors than only the timing of entering a trade. To complete the strategy I included:
  • an option to choose from different MA types per indicator
  • a Risk Management Tool
  • a Take Profit Logic
  • a Trailing stop loss
  • a Hard Stoploss
  • a visual representation of TP and SL
This is merely an example how to structure a strategy and many different setups are possible.
The features in this script are explained below:

Different MA types
The script supports various MA types like EMA, SMA, DEMA, TEMA, WMA and HMA. You can select the MA type and different timeframe to your liking.

Risk Management Tool
Traders can choose to allocate their position size based on a percentage of equity or a fixed number of contracts. This feature ensures prudent risk management and helps traders align their position sizes with their risk tolerance. In the strategy -0.5 is equal to 50% of equity and 1.5 is 150% of equity used. Make sure to align the % of equity with your maxdrawn results with backtesting. Personally I don't want higher max drawdowns than 15%. For the strategy results the script considers 0.05 commission rate on each trade, to stay conservative. For a more detailed explanation I refer to my earlier published tradingviewblog about risk management:
Take Profit Logic
The take profit logic in this script is designed for optimization, offering three distinct exit levels. Traders can customize these levels based on their risk appetite. The script allows adjustment of both the percentage take profit level and the position size, catering to individual trading strategies and objectives. The default settings closes 33% of the position when TP target is hit. The TP levels are simply calculated by inputting a % of of the entryprice.

Trailing Stop and Hard Stoploss
To mitigate downside risks and protect profits, the script incorporates a well-thought-out trailing stop mechanism based on the Average True Range (ATR). This dynamic trailing stop adapts to market volatility, allowing traders to secure gains while letting profitable positions run. Additionally, to prevent significant losses, a fixed stop loss is implemented, providing an added layer of protection.

Visual Representation of Take Profit and Stoploss Levels
For enhanced visualization, take profit and stoploss levels are displayed on the chart. Take profit levels are depicted with green lines, providing a clear indication of potential exit points. Conversely, the trailing stop loss is presented by the red line, serving as visual cues for risk management. Visualizing indicators makes is easier to optimize settings to your liking.

Ideal Settings and Accessibility
This script is intended to be used on lower timeframe charts like 10 to 30 minutes. You can Align the MACD entry settings equal to your opened timechart or use a slightly higher timeframe. For the MA trend filters, higher timeframe settings such as 30 min, 1 hour, 4 hours, or 1 day are recommended for trading the trend.

Disclaimer
Trading involves significant risk and may not be suitable for all investors. The information provided in this script is for educational purposes only and should not be considered as financial advice. Past performance is not indicative of future results. By using this script, you acknowledge that you understand and accept these risks.

保護スクリプト
このスクリプトは保護スクリプトとして公開されていますが、利用することができます。お気に入りに登録してチャート上でご利用頂けます。但しこのスクリプトのソースコードを閲覧したり変更することはできません。
免責事項

これらの情報および投稿は、TradingViewが提供または保証する金融、投資、取引、またはその他の種類のアドバイスや推奨を意図したものではなく、またそのようなものでもありません。詳しくは利用規約をご覧ください。

チャートでこのスクリプトを利用したいですか?