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

//@version=5

library("ulib" )
// stochastic calculator
export Stochastic(simple int length,simple int smoothing_period,simple int k_period,simple int d_period ) =>
    hh = ta.highest(close, length)
    ll = ta.lowest(close, length)
    k = 100 * (close - ll) / (hh - ll)
    float d = ta.sma(k_period, smoothing_period)
    float d2 = ta.sma(d_period,smoothing_period )
    [k,d,d2]
export bull_stoch_condition(float k, float d) =>
    if ta.crossover(k,d) 
        bool bull_stoch_condition = true

export ema_condition(float ema_1,float ema_2,float ema_3 ) =>
    if ta.crossover(ema_1, ema_2) 
        bool ema_condition = true
    else if ta.crossover(ema_1 , ema_3)
        bool ema_condition = true
    else if ta.crossover(ema_2, ema_3)
        bool ema_condition = true

export bull_fractal_condition(int n)=>
    
// downFractal
    bool downflag = true
    bool downflag_0 = true
    bool downflag_1 = true
    bool downflag_2 = true
    bool downflag_3 = true
    bool downflag_4 = true

    for i = 1 to n
        downflag := downflag and (low[n-i] > low[n])
        downflag_0 := downflag_0 and (low[n+i] > low[n])
        downflag_1 := downflag_1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n])
        downflag_2 := downflag_2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n])
        downflag_3 := downflag_3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n])
        downflag_4 := downflag_4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])
    flagdown = downflag_0 or downflag_1 or downflag_2 or downflag_3 or downflag_4

    bool df = (downflag and flagdown)

export bear_fractal_condition(int n)=>
    bool upflag = true
    bool upflag_0 = true
    bool upflag_1 = true
    bool upflag_2 = true
    bool upflag_3 = true
    bool upflag_4 = true

    for i = 1 to n
        upflag := upflag and (high[n-i] < high[n])
        upflag_0 := upflag_0 and (high[n+i] < high[n])
        upflag_1 := upflag_1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n])
        upflag_2 := upflag_2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n])
        upflag_3 := upflag_3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n])
        upflag_4 := upflag_4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])
    flagUp = upflag_0 or upflag_1 or upflag_2 or upflag_3 or upflag_4

    bool uf = (upflag and flagUp)
export Bull( bool Fractal,bool ema,bool stochastic_osc ) =>
    if Fractal == true and ema == true and stochastic_osc == true 
        bool Bull_condition = true

