The script uses two distinct timeframes: a higher (daily) timeframe for identifying the trend and a lower (hourly) timeframe for making trades. This combination allows the script to follow the larger trend while timing entries and exits with more precision on a shorter timeframe. Moving Averages Calculation:
higher_ma: The 20-period Simple Moving Average (SMA) calculated based on the daily timeframe. This average gives a sense of the larger trend direction. lower_ma: The 20-period SMA calculated on the hourly (current) timeframe, providing a dynamic level for detecting entry and exit points within the broader trend. Trend Identification:
Bullish Trend: The script determines that a bullish trend is present if the current price is above the daily moving average (higher_ma). Bearish Trend: Similarly, a bearish trend is identified when the current price is below this daily moving average. Trade Signals:
Buy Signal: A buy signal is generated when the price on the hourly chart crosses above the hourly 20-period MA, but only if the higher (daily) timeframe trend is bullish. This ensures that buy trades align with the larger upward trend. Sell Signal: A sell signal is generated when the price on the hourly chart crosses below the hourly 20-period MA, but only if the daily trend is bearish. This ensures that sell trades are consistent with the broader downtrend. Plotting and Visual Cues:
Higher Timeframe MA: The daily 20-period moving average is plotted in red to help visualize the long-term trend. Buy and Sell Signals: Buy signals appear as green labels below the price bars with the text "BUY," while sell signals appear as red labels above the bars with the text "SELL." Background Coloring: The background changes color based on the identified trend for easier trend recognition: Green (with transparency) when the daily trend is bullish. Red (with transparency) when the daily trend is bearish.