GDM Price Power & Under Current
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © manegirishd

//@version=5
indicator(title="GDM Price Power & Under Current", shorttitle="GDMPP&UC", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

Power1Input = input.int(21, minval=1, title="Power 1 Length", group="Power Settings")
Power2Input = input.int(9, minval=1, title="Power 2 Length", group="Power Settings")
Power3Input = input.int(7, minval=1, title="Power 3 Length", group="Power Settings")
PowerSourceInput = input.source(close, "Source", group="Power Settings")

UnderCurrentTypeInput = input.string("SMA", title="Under Current Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Under Current Settings")
UnderCurrent1LengthInput = input.int(21, title="Under Current 1 Length", group="Under Current Settings")
UnderCurrent2LengthInput = input.int(9, title="Under Current 2 Length", group="Under Current Settings")
UnderCurrent3LengthInput = input.int(7, title="Under Current 3 Length", group="Under Current Settings")

up1 = ta.rma(math.max(ta.change(PowerSourceInput), 0), Power1Input)
down1 = ta.rma(-math.min(ta.change(PowerSourceInput), 0), Power1Input)
Power1 = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - (100 / (1 + up1 / down1))
UnderCurrent1 = ma(Power1, UnderCurrent1LengthInput, UnderCurrentTypeInput)

up2 = ta.rma(math.max(ta.change(PowerSourceInput), 0), Power2Input)
down2 = ta.rma(-math.min(ta.change(PowerSourceInput), 0), Power2Input)
Power2 = down2 == 0 ? 100 : up2 == 0 ? 0 : 100 - (100 / (1 + up2 / down2))
UnderCurrent2 = ma(Power2, UnderCurrent2LengthInput, UnderCurrentTypeInput)

up3 = ta.rma(math.max(ta.change(PowerSourceInput), 0), Power3Input)
down3 = ta.rma(-math.min(ta.change(PowerSourceInput), 0), Power3Input)
Power3 = down3 == 0 ? 100 : up3 == 0 ? 0 : 100 - (100 / (1 + up3 / down3))
UnderCurrent3 = ma(Power3, UnderCurrent3LengthInput, UnderCurrentTypeInput)

plot(Power1, "Power1", color=#2196F3)
plot(UnderCurrent1, "UnderCurrent1", color=#311B92)

plot(Power2, "Power2", color=#FF5252)
plot(UnderCurrent2, "UnderCurrent2", color=#9C27B0)

plot(Power3, "Power3", color=#4CAF50)
plot(UnderCurrent3, "UnderCurrent3", color=#808000)