Common Functions
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gliderfund

//@version=5

// https://www.tradingview.com/script/KJYxDHNj-Common-Functions/
// import gliderfund/CommonFunctions/2 as com

// @description This Library provides some handy functions commonly used in Pine Script.
library(title = "CommonFunctions", overlay = true)



// #############################################
// #############################################
//            CROSSES
// #############################################
// #############################################

// @function          Checks the existance of crosses between the series
// @param source1     First series
// @param offset1     (Optional) Offset of First series. By default 0
// @param source2     (Optional) Second series. By default 'close' price
// @param offset2     (Optional) Offset of Second series. By default 0
// @returns           [cross, crossOver, crossUnder]
export crosses(series float source1, simple int offset1 = 0, series float source2 = close, simple int offset2 = 0) =>
    cross = ta.cross(source1[offset1], source2[offset2])
    // crossOver: true when source1 crossed over source2 otherwise false
    crossOver = ta.crossover(source1[offset1], source2[offset2])
    // crossUnder: true when source1 crossed under source2 otherwise false
    crossUnder = ta.crossunder(source1[offset1], source2[offset2])

    // Result
    [cross, crossOver, crossUnder]

// End of Function




// #############################################
// #############################################
//            MARKET STATE
// #############################################
// #############################################

// @function          Determines Bullish/Bearish state according to the relative position between the series.
// @param source1     Active series used to determine bullish/bearish states.
// @param offset1     (Optional) Offset of First series. By default 0.
// @param source2     (Optional) Second series. By default 'close' price.
// @param offset2     (Optional) Offset of Second series. By default 0.
// @returns           [bullish, bearish]
export marketState(series float source1, simple int offset1 = 0, series float source2 = close, simple int offset2 = 0) =>
    // Conditions
    bullish = source1[offset1] <  source2[offset2]
    bearish = source1[offset1] >= source2[offset2]

    // Result
    [bullish, bearish]

// End of Function




// #############################################
// #############################################
//            HISTOGRAM PROFILE
// #############################################
// #############################################

// @function          Histogram profiling
// @param hist        Histogram series
// @param ref         Reference value. By default '0'
// @returns           [histPosUp, histPosDown, histNegUp, histNegDown]
export histProfile(series float hist, series float ref = 0.0) =>
    histPosUp   = hist > hist[1] and hist  > ref
    histPosDown = hist < hist[1] and hist  > ref
    histNegUp   = hist < hist[1] and hist <= ref
    histNegDown = hist > hist[1] and hist <= ref

    // Result
    [histPosUp, histPosDown, histNegUp, histNegDown]

// End of Function




// #############################################
// #############################################
//            FLOAT SELECT
// #############################################
// #############################################

// @function            Selects the appropiate float variable based on a boolean condition. If multiple sources are activated simultaneously, the order within the switch clause prevails. The first one activated is the one being output.
// @param showSrc1      Boolean controlling the activation of Source #1.
// @param src1          Source #1.
// @param showSrc2      Boolean controlling the activation of Sources #2-10. By default 'false'.
// @param src2          Sources #2-10. By default 'number'.
// @returns             Selected source.
export floatSelect(simple bool showSrc1 = true, series float src1 = 1, simple bool showSrc2 = false, series float src2 = 2, simple bool showSrc3 = false, series float src3 = 3, simple bool showSrc4 = false, series float src4 = 4, simple bool showSrc5 = false, series float src5 = 5, simple bool showSrc6 = false, series float src6 = 6, simple bool showSrc7 = false, series float src7 = 7, simple bool showSrc8 = false, series float src8 = 8, simple bool showSrc9 = false, series float src9 = 9, simple bool showSrc10 = false, series float src10 = 10) =>

    float result = switch
        showSrc1  => src1
        showSrc2  => src2
        showSrc3  => src3
        showSrc4  => src4
        showSrc5  => src5
        showSrc6  => src6
        showSrc7  => src7
        showSrc8  => src8
        showSrc9  => src9
        showSrc10 => src10
        => float(na)

    // Result
    result

// End of Function




// #############################################
// #############################################
//            INTEGER SELECT
// #############################################
// #############################################

// @function            Selects the appropiate integer variable based on a boolean condition. If multiple sources are activated simultaneously, the order within the switch clause prevails. The first one activated is the one being output.
// @param showSrc1      Boolean controlling the activation of Source #1.
// @param src1          Source #1.
// @param showSrc2      Boolean controlling the activation of Sources #2-10. By default 'false'.
// @param src2          Sources #2-10. By default 'number'.
// @returns             Selected source.
export intSelect(simple bool showSrc1 = true, series int src1 = 1, simple bool showSrc2 = false, series int src2 = 2, simple bool showSrc3 = false, series int src3 = 3, simple bool showSrc4 = false, series int src4 = 4, simple bool showSrc5 = false, series int src5 = 5, simple bool showSrc6 = false, series int src6 = 6, simple bool showSrc7 = false, series int src7 = 7, simple bool showSrc8 = false, series int src8 = 8, simple bool showSrc9 = false, series int src9 = 9, simple bool showSrc10 = false, series int src10 = 10) =>

    bool result = switch
        showSrc1  => src1
        showSrc2  => src2
        showSrc3  => src3
        showSrc4  => src4
        showSrc5  => src5
        showSrc6  => src6
        showSrc7  => src7
        showSrc8  => src8
        showSrc9  => src9
        showSrc10 => src10
        => int(na)

    // Result
    result

// End of Function





// #############################################
// #############################################
//            BOOL SELECT
// #############################################
// #############################################

// @function            Selects the appropiate bool variable based on a boolean condition. If multiple sources are activated simultaneously, the order within the switch clause prevails. The first one activated is the one being output.
// @param showSrc1      Boolean controlling the activation of Source #1.
// @param src1          Source #1.
// @param showSrc2      Boolean controlling the activation of Sources #2-10. By default 'false'.
// @param src2          Sources #2-10. By default 'false'.
// @returns             Selected source.
export boolSelect(simple bool showSrc1 = true, series bool src1 = na, simple bool showSrc2 = false, series bool src2 = na, simple bool showSrc3 = false, series bool src3 = na, simple bool showSrc4 = false, series bool src4 = na, simple bool showSrc5 = false, series bool src5 = na, simple bool showSrc6 = false, series bool src6 = na, simple bool showSrc7 = false, series bool src7 = na, simple bool showSrc8 = false, series bool src8 = na, simple bool showSrc9 = false, series bool src9 = na, simple bool showSrc10 = false, series bool src10 = na) =>

    bool result = switch
        showSrc1  => src1
        showSrc2  => src2
        showSrc3  => src3
        showSrc4  => src4
        showSrc5  => src5
        showSrc6  => src6
        showSrc7  => src7
        showSrc8  => src8
        showSrc9  => src9
        showSrc10 => src10
        => bool(na)

    // Result
    result

// End of Function
