Energy_Arrows[Salty]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © markmiotke
//@version=5
// Energy_Arrows[Salty]
// By Mark Miotke
// Initial Release: 2/20/2023
//This script quantifies the energy in a price move by comparing the relationship of 3 configurable exponential moving averages present on a slightly higher timeframe (chosen automatically based on the charts current period).  It uses the closing price by default, but this is also configurable using the Source input.  There are a few ways to use the information in this indicator.  One is to use the values above zero (colored green) to provide a bullish bias for future price, and values below zero (colored red) indicating a bearish bias for future prices.  This bias can be shown to be increasing or decreasing base on the upward or downward slope of the indicator.  The green and red arrows can be enabled to show if the bias is strengthening or weakening based on the direction they are pointing.  Finally, the height changes in the peaks of the indicator can be used to show divergence in the strength of extreme price moves to show when a pull back or reversal may occur.

indicator(title='Energy_Arrows[Salty]', shorttitle='Energy',overlay=false)
ShowZeroCrossArrows = input(true, title='Show Zero Cross Arrows', group = "Arrows")
ShowCrossArrows = input(true, title='Show Cross Arrows', group = "Arrows")
//New Inputs
//priceType default close
i_src = input.source(close, "Source", group = "Energy Oscillator")
// slowEmaLength default=26
i_lenSlow = input.int(26, "Slow EMA Length", step=1, group = "Energy Oscillator")
// fastEmaLength default=12
i_lenFast = input.int(12, "Fast EMA Length", step=1, group = "Energy Oscillator")
// smoothLength default=9
i_lenSmooth = input.int(9, "Smoothing Length", step=1, group = "Energy Oscillator")
// zoomFactor default=3
i_ZoomFactor = input.int(3, "Zoom Factor", step=1, group = "Energy Oscillator")
//funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)
Red = color.new(#FF0000, 50)
DarkRed = color.new(#BA0000, 50)
Green = color.new(#00FF00, 50)
DarkGreen = color.new(#00BA00, 50)
cRed = #FF0000
cGreen = #00FF00
cDarkRed = #BA0000
cDarkGreen = #00BA00

funcEnergy(eSrc, eLenSlow, eLenFast, eLenSmooth, eZoomFactor) =>
    energy = ta.ema(eSrc, eLenFast) - ta.ema(eSrc, eLenSlow)
    energySmoothed = ta.ema(energy, eLenSmooth)
    (energy - energySmoothed) * eZoomFactor

//Resolution, e.g. '60' - 60 minutes, 'D' - daily, 'W' - weekly, 'M' - monthly, '5D' - 5 days, '12M' - one year, '3M' - one quarter
currentPeriod = timeframe.period

s2 = currentPeriod == '1' or currentPeriod == '3' ? request.security(syminfo.tickerid, '5', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == '5' ? request.security(syminfo.tickerid, '15', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == '10' ? request.security(syminfo.tickerid, '15', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == '15' ? request.security(syminfo.tickerid, '30', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == '30' ? request.security(syminfo.tickerid, '60', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == '45' ? request.security(syminfo.tickerid, '120', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == '60' or currentPeriod == '120' or currentPeriod == '180' or currentPeriod == '240' ? request.security(syminfo.tickerid, 'D', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == 'D' ? request.security(syminfo.tickerid, 'W', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == 'W' ? request.security(syminfo.tickerid, 'M', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : currentPeriod == 'M' ? request.security(syminfo.tickerid, '3M', funcEnergy(i_src, i_lenSlow, i_lenFast, i_lenSmooth, i_ZoomFactor)) : na

c_color = s2 < 0 ? s2 <= s2[1] ? DarkRed : Red : s2 >= 0 ? s2 >= s2[1] ? Green : DarkGreen : na
//fill_Color = s2 < 0 ? s2 < s2[1] ? DarkRed : Red : s2 >= 0 ? s2 > s2[1] ? Green : DarkGreen : na

p1 = plot(s2, style=plot.style_area, color=c_color, linewidth=3)
zeroline = hline(0, title='Zero Line', color=color.black, linestyle=hline.style_dotted, linewidth=2)
plotshape(ShowZeroCrossArrows and ta.crossover(s2, 0) ? s2 : na, color=color.new(cDarkGreen, 0), style=shape.arrowup, location=location.absolute, size=size.huge)
plotshape(ShowCrossArrows and ta.crossover(s2, s2[1]) and s2 > 0 ? s2 : na, color=color.new(cDarkGreen, 0), style=shape.arrowup, location=location.absolute, size=size.huge)
plotshape(ShowCrossArrows and ta.crossunder(s2, s2[1]) and s2 > 0 ? s2 : na, color=color.new(cRed, 0), style=shape.arrowdown, location=location.absolute, size=size.normal)
plotshape(ShowZeroCrossArrows and ta.crossunder(s2, 0) ? s2 : na, color=color.new(cDarkRed, 0), style=shape.arrowdown, location=location.absolute, size=size.huge)
plotshape(ShowCrossArrows and ta.crossunder(s2, s2[1]) and s2 < 0 ? s2 : na, color=color.new(cDarkRed, 0), style=shape.arrowdown, location=location.absolute, size=size.huge)
plotshape(ShowCrossArrows and ta.crossover(s2, s2[1]) and s2 < 0 ? s2 : na, color=color.new(cGreen, 0), style=shape.arrowup, location=location.absolute, size=size.normal)
//fill(p1, plot(0, color=color.new(color.black, 0), linewidth=1), color=fill_Color)

