pine-scriptstochastic

Stochastic %D values above 80 and below 20


Here is a simple script I wrote to indicate a simple reversal pattern. I want to add to each line so when the stochastic %D is higher than 80 will trigger a red down arrow and when %D is below 20 trigger a green up arrow.

study("Ringed HL", overlay=true)
datadown = open[1] > open[2] and close[1] > close[2] and close[2] > open[2] and close[1] > open[1] and close < open
dataup =  open[1] < open[2] and close[1] < close[2] and close[2] < open[2] and close[1] < open[1] and close > open
plotshape(datadown, style=shape.triangledown, location=location.abovebar, color=color.red)
plotshape(dataup, style=shape.triangleup, location=location.belowbar, color=color.green)

Solution

  • I think this is what you're looking for.
    I've added a checkbox input to show all arrows below/above the threshold.
    If unchecked, it'll only show the first arrow when it crosses the treshold.

    //@version=4
    study("Ringed HL", overlay=true)
    
    Length          = input (14, minval=1, title = "Stochastic Length")
    k               = input ( 1, minval=1, title = "Stochastic %K")
    d               = input ( 5, minval=1, title = "Stochastic %D")
    showAll         = input(false)
    
    sto             = stoch (close, highest(Length), lowest(Length), Length)
    K               = sma (sto, k)
    D               = sma (sto, d)
    
    
    D_over_80       = D > 80
    D_under_20      = D < 20
    
    show_D_over_80  = showAll ? D_over_80  : D_over_80  != D_over_80[1]  ? D_over_80  : na 
    show_D_under_20 = showAll ? D_under_20 : D_under_20 != D_under_20[1] ? D_under_20 : na 
    
    plotshape(show_D_over_80,  style=shape.triangledown, location=location.abovebar, color=color.red)
    plotshape(show_D_under_20, style=shape.triangleup,   location=location.belowbar, color=color.green)
    

    Update: Code integrated with original question code.

    //@version=4
    study("Ringed HL", overlay=true)
    
    Length          = input (14, minval=1, title = "Stochastic Length")
    k               = input ( 1, minval=1, title = "Stochastic %K")
    d               = input ( 5, minval=1, title = "Stochastic %D")
    showAll         = input(false)
    
    sto             = stoch (close, highest(Length), lowest(Length), Length)
    K               = sma (sto, k)
    D               = sma (sto, d)
    
    D_over_80       = D > 80
    D_under_20      = D < 20
    
    datadown        = open[1] > open[2] and close[1] > close[2] and close[2] > open[2] and close[1] > open[1] and close < open
    dataup          = open[1] < open[2] and close[1] < close[2] and close[2] < open[2] and close[1] < open[1] and close > open
    
    show_datadown   = datadown and D_over_80
    show_dataup     = dataup   and D_under_20
    
    plotshape(show_datadown, style=shape.triangledown, location=location.abovebar, color=color.red)
    plotshape(show_dataup, style=shape.triangleup, location=location.belowbar, color=color.green)