OPEN-SOURCE SCRIPT

Moving Average Ratio [InvestorUnknown]

4412
Overview

The "Moving Average Ratio" (MAR) indicator is a versatile tool designed for valuation, mean-reversion, and long-term trend analysis. This indicator provides multiple display modes to cater to different analytical needs, allowing traders and investors to gain deeper insights into the market dynamics.

Features

1. Moving Average Ratio (MAR):
  • Calculates the ratio of the chosen source (close, open, ohlc4, hl2 …) to a longer-term moving average of choice (SMA, EMA, HMA, WMA, DEMA)
  • Useful for identifying overbought or oversold conditions, aiding in mean-reversion strategies and valuation of assets.
  • For some high beta asset classes, like cryptocurrencies, you might want to use logarithmic scale for the raw MAR, below you can see the visual difference of using Linear and Logarithmic scale on BTC


снимок

2. MAR Z-Score:
  • Computes the Z-Score of the MAR to standardize the ratio over chosen time period, making it easier to identify extreme values relative to the historical mean.
  • Helps in detecting significant deviations from the mean, which can indicate potential reversal points and buying/selling opportunities


снимок

3. MAR Trend Analysis:
  • Uses a combination of short-term (default 1, raw MAR) and long-term moving averages of the MAR to identify trend changes.
  • Provides a visual representation of bullish and bearish trends based on moving average crossings.
  • Using Logarithmic scale can improve the visuals for some asset classes.


снимок

4. MAR Momentum:
  • Measures the momentum of the MAR by calculating the difference over a specified period.
  • Useful for detecting changes in the market momentum and potential trend reversals.


5. MAR Rate of Change (ROC):
  • Calculates the rate of change of the MAR to assess the speed and direction of price movements.
  • Helps in identifying accelerating or decelerating trends.


снимок

MAR Momentum and Rate of Change are very similar, the only difference is that the Momentum is expressed in units of the MAR change and ROC is expressed as % change of MAR over chosen time period.


Customizable Settings

General Settings:
  • Display Mode: Select the display mode from MAR, MAR Z-Score, MAR Trend, MAR Momentum, or MAR ROC.
  • Color Bars: Option to color the bars based on the current display mode.
  • Wait for Bar Close: Toggle to wait for the bar to close before updating the MAR value.


MAR Settings:
  • Length: Period for the moving average calculation.
  • Source: Data source for the moving average calculation.
  • Moving Average Type: Select the type of moving average (SMA, EMA, WMA, HMA, DEMA).


Z-Score Settings:
  • Z-Score Length: Period for the Z-Score calculation.


Trend Analysis Settings:
  • Moving Average Type: Select the type of moving average for trend analysis (SMA, EMA).
  • Longer Moving Average: Period for the longer moving average.
  • Shorter Moving Average: Period for the shorter moving average.


Momentum Settings:
  • Momentum Length: Period for the momentum calculation.


Rate of Change Settings:
  • ROC Length: Period for the rate of change calculation.



Calculation and Plotting

Moving Average Ratio (MAR):
  • Calculates the ratio of the price to the selected moving average type and length.
  • Plots the MAR with a gradient color based on its Z-Score, aiding in visual identification of extreme values.


// Moving Average Ratio (MAR)
ma_main = switch ma_main_type
    "SMA"  => ta.sma(src,  len)
    "EMA"  => ta.ema(src,  len)
    "WMA"  => ta.wma(src,  len)
    "HMA"  => ta.hma(src,  len)
    "DEMA" => ta.dema(src, len)

mar = (waitforclose ? src[1] : src) / ma_main

z_col = color.from_gradient(z, -2.5, 2.5, color.green, color.red)
plot(disp_mode.mar ? mar : na, color = z_col, histbase = 1, style = plot.style_columns)
barcolor(color_bars ? (disp_mode.mar ? (z_col) : na) : na)


MAR Z-Score:
  • Computes the Z-Score of the MAR and plots it with a color gradient indicating the magnitude of deviation from the mean.


// MAR Z-Score
mean = ta.sma(math.log(mar), z_len)
stdev = ta.stdev(math.log(mar),z_len)
z = (math.log(mar) - mean) / stdev

plot(disp_mode.mar_z ? z : na,  color = z_col, histbase = 0, style = plot.style_columns)
plot(disp_mode.mar_z ? 1 : na, color = color.new(color.red,70))
plot(disp_mode.mar_z ? 2 : na, color = color.new(color.red,50))
plot(disp_mode.mar_z ? 3 : na, color = color.new(color.red,30))
plot(disp_mode.mar_z ? -1 : na, color = color.new(color.green,70))
plot(disp_mode.mar_z ? -2 : na, color = color.new(color.green,50))
plot(disp_mode.mar_z ? -3 : na, color = color.new(color.green,30))
barcolor(color_bars ? (disp_mode.mar_z ? (z_col) : na) : na)


MAR Trend:
  • Plots the MAR along with its short-term and long-term moving averages.
  • Uses color changes to indicate bullish or bearish trends based on moving average crossings.


// MAR Trend - Moving Average Crossing
mar_ma_long = switch ma_trend_type
    "SMA"  => ta.sma(mar,  len_trend_long)
    "EMA"  => ta.ema(mar,  len_trend_long)

mar_ma_short = switch ma_trend_type
    "SMA"  => ta.sma(mar,  len_trend_short)
    "EMA"  => ta.ema(mar,  len_trend_short)

plot(disp_mode.mar_t ? mar : na, color = mar_ma_long < mar_ma_short ? color.new(color.green,50) : color.new(color.red,50), histbase = 1, style = plot.style_columns)
plot(disp_mode.mar_t ? mar_ma_long : na, color = mar_ma_long < mar_ma_short ? color.green : color.red, linewidth = 4)
plot(disp_mode.mar_t ? mar_ma_short : na, color = mar_ma_long < mar_ma_short ? color.green : color.red, linewidth = 2)
barcolor(color_bars ? (disp_mode.mar_t ? (mar_ma_long < mar_ma_short ? color.green : color.red) : na) : na)


MAR Momentum:
  • Plots the momentum of the MAR, coloring the bars to indicate increasing or decreasing momentum.


// MAR Momentum
mar_mom = mar - mar[len_mom]
// MAR Momentum
mom_col = mar_mom > 0 ? (mar_mom > mar_mom[1] ? color.new(color.green,0): color.new(color.green,30)) : (mar_mom < mar_mom[1] ? color.new(color.red,0): color.new(color.red,30))
plot(disp_mode.mar_m ? mar_mom : na, color = mom_col, histbase = 0, style = plot.style_columns)


MAR Rate of Change (ROC):
  • Plots the ROC of the MAR, using color changes to show the direction and strength of the rate of change.


// MAR Rate of Change
mar_roc = ta.roc(mar,len_roc)
// MAR ROC
roc_col = mar_roc > 0 ? (mar_roc > mar_roc[1] ? color.new(color.green,0): color.new(color.green,30)) : (mar_roc < mar_roc[1] ? color.new(color.red,0): color.new(color.red,30))
plot(disp_mode.mar_r ? mar_roc : na, color = roc_col, histbase = 0, style = plot.style_columns)



Summary:
This multi-purpose indicator provides a comprehensive toolset for various trading strategies, including valuation, mean-reversion, and trend analysis. By offering multiple display modes and customizable settings, it allows users to tailor the indicator to their specific analytical needs and market conditions.



Отказ от ответственности

Все виды контента, которые вы можете увидеть на TradingView, не являются финансовыми, инвестиционными, торговыми или любыми другими рекомендациями. Мы не предоставляем советы по покупке и продаже активов. Подробнее — в Условиях использования TradingView.