Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=6
- indicator('STH MVRV Moving Average with EMA', shorttitle = 'STHMVRV', overlay = false)
- // Input for the SMA length and EMA length
- smaLengthInput = input.int(100, title = 'SMA Length')
- emaLengthInput = input.int(55, title = 'EMA Length')
- // Smoothing option for SMA
- smoothingType = input.string('None', title = 'Smoothing Type', options = ['None', 'EMA', 'SMA'])
- smoothingLength = input.int(20, title = 'Smoothing Length', minval = 1)
- // Selectable option for MVRVMA color mode
- mvrvMAColorMode = input.string('Normal', title = 'MVRVMA Color Mode', options = ['Normal', 'DI+ vs DI-'])
- // Calculate the market value only on the daily time frame
- isDaily = ta.change(time('D'))
- marketValue = request.security(syminfo.tickerid, 'D', close)
- // Get the realized value
- realValue = ta.sma(na(marketValue) ? ta.valuewhen(bool(isDaily), close, 0) : marketValue, smaLengthInput)
- // Calculate the STH MVRV
- mvrv = marketValue / realValue
- // Calculate the moving average of the STH MVRV on the daily time frame
- mvrvMA = request.security(syminfo.tickerid, 'D', ta.sma(mvrv, 2))
- // Calculate the EMA of the MVRVMA
- emaMVRVMA = ta.ema(mvrvMA, emaLengthInput)
- // Levels
- plot(1, color = color.new(color.purple, 0), title = 'LvL 1', linewidth = 2)
- hline(1, title = 'Profit line', color = color.new(color.purple, 0), linewidth = 1, linestyle = hline.style_dashed)
- // STH Channel with 100-Day SMA
- sma_100 = ta.sma(close, smaLengthInput)
- sma_smoothed = switch smoothingType
- 'None' => sma_100
- 'EMA' => ta.ema(sma_100, smoothingLength)
- 'SMA' => ta.sma(sma_100, smoothingLength)
- deviation_percent = input.float(8.0, title = 'Deviation (%)', step = 1) / 100
- deviation_up = sma_smoothed * (1 + deviation_percent)
- deviation_down = sma_smoothed * (1 - deviation_percent)
- // Directional Movement calculations for DI+ and DI-
- len = input.int(14, title = 'DI Length')
- TrueRange = ta.tr
- DirectionalMovementPlus = math.max(high - high[1], 0)
- DirectionalMovementMinus = math.max(low[1] - low, 0)
- // Declare Smoothed Variables
- var float SmoothedTrueRange = na
- var float SmoothedDirectionalMovementPlus = na
- var float SmoothedDirectionalMovementMinus = na
- // Compute Smoothed Values
- SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1]) / len) + TrueRange
- SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1]) / len) + DirectionalMovementPlus
- SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1]) / len) + DirectionalMovementMinus
- DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
- DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
- // Color logic for MVRVMA based on selection
- normalColor = mvrvMA[1] > 1 ? #ffebba : #b13e00
- diColor = DIPlus > DIMinus ? #ffebba : #b13e00
- mvrvMA_final_color = mvrvMAColorMode == 'DI+ vs DI-' ? diColor : normalColor
- // Plot the STH MVRV moving average with selectable color mode
- plot(mvrvMA, title = 'STH MVRV MA', color = mvrvMA_final_color, linewidth = 2)
- // Plot the EMA of the MVRVMA
- plot(emaMVRVMA, title = 'MVRVMA EMA', color = #da00ff, linewidth = 1)
- // Change deviation_up line color based on DI+ > DI-
- deviationColor = DIPlus > DIMinus ? #ffebba : #b13e00
- plot(sma_smoothed, color = #5d606b, linewidth = 2, title = '100-Day SMA (Smoothed)', force_overlay = true)
- plot(deviation_up, color = deviationColor, linewidth = 2, title = 'Upper Deviation', force_overlay = true)
- plot(deviation_down, color = deviationColor, linewidth = 2, title = 'Lower Deviation', force_overlay = true)
- // Bar color logic
- isAbove = mvrvMA > emaMVRVMA
- isBelow = mvrvMA < emaMVRVMA
- barcolor(isAbove ? color.green : isBelow ? color.red : na)
- // Create a table for statistics
- var table statsTable = table.new(position = position.top_right, columns = 2, rows = 6, bgcolor = color.black)
- // Define table values
- table.cell(statsTable, 0, 0, "Trend", text_color = color.white)
- table.cell(statsTable, 1, 0, mvrvMA > emaMVRVMA ? "↑ Uptrend" : "↓ Downtrend", text_color = mvrvMA > emaMVRVMA ? color.green : color.red)
- table.cell(statsTable, 0, 1, "DI+ vs DI-", text_color = color.white)
- table.cell(statsTable, 1, 1, DIPlus > DIMinus ? "Bullish" : "Bearish", text_color = DIPlus > DIMinus ? color.green : color.red)
- table.cell(statsTable, 0, 2, "Deviation Up", text_color = color.white)
- table.cell(statsTable, 1, 2, str.tostring(deviation_up, "#.##"), text_color = color.green)
- table.cell(statsTable, 0, 3, "100-Day SMA", text_color = color.white)
- table.cell(statsTable, 1, 3, str.tostring(sma_smoothed, "#.##"), text_color = color.blue)
- table.cell(statsTable, 0, 4, "Deviation Down", text_color = color.white)
- table.cell(statsTable, 1, 4, str.tostring(deviation_down, "#.##"), text_color = color.red)
- table.cell(statsTable, 0, 5, "Last DI Crossover", text_color = color.white)
- table.cell(statsTable, 1, 5, str.tostring(ta.barssince(DIPlus > DIMinus), "#") + " bars ago", text_color = color.gray)
Advertisement