OPEN-SOURCE SCRIPT

Timeframe-Based Dynamic MA [odnac]

snapshotsnapshot
snapshot
snapshot

This code is a Timeframe-Based Dynamic MA indicator, written in Pine Script, that dynamically calculates and displays the Simple Moving Average (SMA), Exponential Moving Average (EMA), and Volume Weighted Moving Average (VWMA) based on a 24-hour period, according to the selected timeframe. It automatically adjusts the length of the moving averages for each timeframe, showing the appropriate value optimized for that specific timeframe.

Code Explanation:

Settings:

inputLength: A user input that allows setting the base time (24 hours by default). This value determines the reference for calculating the length of the moving averages according to the timeframe.
transp: A setting for the transparency of the moving average lines. It can accept values from 0 to 100 (0 is opaque, 100 is fully transparent).
Timeframe-Based Moving Average Calculation:

The length variable is dynamically calculated based on the current chart's timeframe.
For shorter timeframes like 1-minute, 2-minute, 3-minute, 5-minute, 10-minute, 15-minute, 30-minute, and 45-minute, the length is calculated by multiplying 60 / selected timeframe to obtain the moving average length based on a 24-hour period.
For longer timeframes like 1 hour, 4 hours, and 1 day, fixed values are used to set the moving average length.
Moving Average Calculation:

sma, ema, vwma: These are the Simple Moving Average, Exponential Moving Average, and Volume Weighted Moving Average calculated based on the length.
else_sma, else_ema, else_vwma: These represent the moving averages fetched from the 1-hour chart. For timeframes that are not calculated directly, the values are taken from the 1-hour chart.
Displaying the Moving Averages:

The moving averages are plotted according to the length calculated for the current timeframe.
If the length for the current timeframe is valid, the corresponding SMA, EMA, and VWMA values are displayed. Otherwise, the values fetched from the 1-hour chart are used.
The moving averages are displayed with the transparency (transp) value set by the user, controlling their opacity on the chart.
How to Use:

Base Time: The user sets a base time. For example, setting inputLength to 24 will calculate the moving average length based on a 24-hour period, which will be dynamically adjusted and displayed according to the selected timeframe.
Transparency Setting: The transparency of the moving average lines can be adjusted using the transp value.
Supported Timeframes:

For shorter timeframes (1-minute, 2-minute, 3-minute, 5-minute, 10-minute, 15-minute, 30-minute, 45-minute), the moving average lengths are dynamically calculated and displayed.
For longer timeframes (1 hour, 4 hours, 1 day), fixed length values are used.
This indicator allows you to dynamically calculate daily moving averages across different timeframes and visually check which moving average is the most appropriate for the selected timeframe.

Disclaimer