Ladder StDev
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jason5480

//@version=5
indicator(title = 'Ladder StDev',
          shorttitle = 'LStDev',
          timeframe = '',
          timeframe_gaps = true)

import HeWhoMustNotBeNamed/arraymethods/1

// INPUT ============================================================================================================
stdevMode = input.string(defval = 'PRICE', title = 'StDev Mode', options = ['PRICE', 'RETURN'], tooltip = 'Use the "PRICE" as source for the standard deviation calculations. Or use "RETURN" to use the log returns of the price as source for the standard deviation.')
priceSrc = input.source(defval = close, title = '  Price Src', tooltip = 'The source to be used for the standard deviation calculation when PRICE mode is selected or for the log returns when RETURN mode is selected.')
stdevLength = input.int(defval = 20, title = 'StDev Len', minval = 1, tooltip = 'The length to be used for the standard deviation calculation.')

// LOGIC ============================================================================================================
var positiveStDevs = array.new<float>()
var negativeStDevs = array.new<float>()

float stdevSrc = stdevMode == 'PRICE' ? priceSrc : math.log(priceSrc/priceSrc[1])

if(close > open)
    positiveStDevs.push(stdevSrc, stdevLength)
else
    negativeStDevs.push(stdevSrc, stdevLength)

float possitiveStDev = array.stdev(positiveStDevs, false)
float negativeStDev = array.stdev(negativeStDevs, false)

float ladderPositiveStDev = stdevMode == 'PRICE' ? possitiveStDev : priceSrc * (math.exp(possitiveStDev) - 1.0)
float ladderNegativeStDev = stdevMode == 'PRICE' ? negativeStDev : priceSrc * (math.exp(negativeStDev) - 1.0)

// PLOT =============================================================================================================
var positiveColor = color.new(#26A69A, 0)
plot(series = ladderPositiveStDev, title = 'Positive StDev', color = positiveColor, linewidth = 1, style = plot.style_line, trackprice = true)
var negativeColor = color.new(#EF5350, 0)
plot(series = ladderNegativeStDev, title = 'Negative StDev', color = negativeColor, linewidth = 1, style = plot.style_line, trackprice = true)