Stoch script

mail@pastecode.io avatar
unknown
plain_text
6 months ago
7.3 kB
17
Indexable
Never
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Capitulation2023

//@version=5
// ---------------------------------------------------------------------------------------------- //

indicator("Clean Stoch on chart", "Clean - Stoch on chart", true, max_lines_count = 300, max_boxes_count = 300, max_bars_back = 300)

group_ocs         = 'Plotting Settings - General  ================'
oscLookbackLength = input.int(150, 'Display Length', minval = 10, step = 10, maxval = 300, group=group_ocs) 
oscPlacement      = input.string('Bottom', 'Placement', options = ['Top', 'Bottom'], group=group_ocs)
oscHight          = 11 - input.int(7, 'Hight' , minval = 1, maxval = 10 , group=group_ocs )
oscVerticalOffset = input.int(3, "Vertical Offset", minval = -3, maxval = 10, group=group_ocs) / 10
highlight     = input.bool(true, "Highlight Stoch/Signal Area")
highlightBreakouts = input(title='Highlight Overbought/Oversold Breakouts ?', defval=true)
highlightCrossovers = input(title='Highlight SMI/Signal Crossovers ?', defval=true)

group_macd        = 'MACD Settings  =============================='
//macdFastLength    = input.int(12, "Fast Length", minval = 1, group=group_macd)
//macdSlowLength    = input.int(26, "Slow Length", minval = 1, group=group_macd)
macdSource        = input(close , "Source", group=group_macd)
macdSignalLength  = input.int(12 , "Signal Smoothing",  minval = 1, maxval = 50       , group=group_macd)
macdSourceMA      = input.string("EMA", "Oscillator MA Type" , options=["SMA", "EMA"], group=group_macd)
macdSignalMA      = input.string("EMA", "Signal Line MA Type", options=["SMA", "EMA"], group=group_macd)
q = input.int(title='Stochastic Lookback', defval=13, minval=1)
r = input.int(title='1st Smoothing Length', defval=25, minval=1)
s = input.int(title='2nd Smoothing Length', defval=2, minval=1)
src = input(title='Source', defval=close)


group_stoch       = 'Stochastic Settings  =========================='
//stochPeriodK      = input.int(14, "%K Length", minval=1, group=group_stoch)
//stochSmoothK      = input.int(1 , "%K Smoothing", minval=1, group=group_stoch)
//stochPeriodD      = input.int(3 , "%D Smoothing", minval=1, group=group_stoch)
stochUpperBand    = input.int(40, 'Upper Band', group=group_stoch)
stochLowerBand    = input.int(-40, 'Lower Band', group=group_stoch)
stochUpperBandMax    = input.int(75, 'Max Upper Band', group=group_stoch)
stochLowerBandMin    = input.int(-75, 'Min Lower Band', group=group_stoch)

// Inputs  -------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------- //
// Calculations  -------------------------------------------------------------------------------- //
hh = ta.highest(q)
ll = ta.lowest(q)

numerator = ta.ema(ta.ema(src - 0.5 * (hh + ll), r), s)
denominator = 0.5 * ta.ema(ta.ema(hh - ll, r), s)

smi = 100 * numerator / denominator
signal = ta.ema(smi, macdSignalLength)

hist = smi - signal
histColor = hist >= 0 ? hist[1] < hist ? #26A69A : #B2DFDB : hist[1] < hist ? #FFCDD2 : #EF5350


// Plotting  ------------------------------------------------------------------------------------ //

var a_lines     = array.new_line()
var a_hist      = array.new_box()
var a_fill      = array.new_linefill()

priceHighest    = ta.highest(high, oscLookbackLength)
priceLowest     = ta.lowest (low , oscLookbackLength)
priceChangeRate = (priceHighest - priceLowest) / priceHighest
priceLowest    := priceLowest  * (1 - priceChangeRate * oscVerticalOffset)
priceHighest   := priceHighest * (1 + priceChangeRate * oscVerticalOffset)
oscHighest      = ta.highest(smi, oscLookbackLength)

hight = priceChangeRate / oscHight



if barstate.islast
    if array.size(a_lines) > 0
        for i = 1 to array.size(a_lines)
            line.delete(array.shift(a_lines))

    if array.size(a_hist) > 0
        for i = 1 to array.size(a_hist)
            box.delete(array.shift(a_hist))

    if array.size(a_fill) > 0
        for i = 1 to array.size(a_fill)
            linefill.delete(array.shift(a_fill))
              

    for barIndex = 0 to oscLookbackLength - 1
        if array.size(a_lines) < 498
            array.push(a_hist , box.new (bar_index[barIndex],  oscPlacement == 'Top' ? priceHighest : priceLowest, 
                                             bar_index[barIndex], (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + hist[barIndex]  / oscHighest * hight), histColor[barIndex], 2))
            array.push(a_lines, line.new(bar_index[barIndex],     (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + smi[barIndex]        / oscHighest * hight), 
                                         bar_index[barIndex + 1], (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + smi[barIndex + 1]    / oscHighest * hight), xloc.bar_index, extend.none, color.green, line.style_solid, 1))
            array.push(a_lines, line.new(bar_index[barIndex],     (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + signal[barIndex]     / oscHighest * hight), 
                                         bar_index[barIndex + 1], (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + signal[barIndex + 1] / oscHighest * hight), xloc.bar_index, extend.none, color.red, line.style_solid, 1))
            if highlight
                array.push(a_fill, linefill.new(array.get(a_lines, 2 * barIndex), array.get(a_lines, 2 * barIndex + 1), smi[barIndex] > signal[barIndex] and highlightBreakouts ? color.new(color.green,50) : color.new(color.red,50)))
               

//
obLevel   = (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + stochUpperBand / oscHighest * hight)
osLevel   = (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + stochLowerBand / oscHighest * hight)
obLevelMax   = (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + stochUpperBandMax / oscHighest * hight)
osLevelMin   = (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 + stochLowerBandMin / oscHighest * hight)

array.push(a_lines, line.new(bar_index[oscLookbackLength], obLevel  , bar_index, obLevel  , xloc.bar_index, extend.none, color.green, line.style_dotted, 2))
array.push(a_lines, line.new(bar_index[oscLookbackLength], osLevel  , bar_index, osLevel  , xloc.bar_index, extend.none, color.red, line.style_dotted, 2))
array.push(a_lines, line.new(bar_index[oscLookbackLength], obLevelMax  , bar_index, obLevelMax  , xloc.bar_index, extend.none, color.green, line.style_solid, 2))
array.push(a_lines, line.new(bar_index[oscLookbackLength], osLevelMin  , bar_index, osLevelMin  , xloc.bar_index, extend.none, color.red, line.style_solid, 2))

//array.push(a_fill, linefill.new(array.get(a_lines, 0), array.get(a_lines, 1),  fillColor))
//midLevel  = (oscPlacement == 'Top' ? priceHighest : priceLowest) * (1 +     midLine / oscHighest * hight)
//array.push(a_lines, line.new(bar_index[oscLookbackLength], midLevel , bar_index, midLevel , xloc.bar_index, extend.none, color.new(color.gray, 50) ,line.style_dashed, 1))
// Plotting  ------------------------------------------------------------------------------------ //