0

I am trying to plot stochastic (14,1,1) in trading view using pine script. But I if I plot the stochastics indicator using the indicators tool of trading view, the output is different.I am using the code as below:

//@version=4
study("My Script")
Length = input (14, minval=1, title = "Stochastic Length")
k = input (1, minval=1, title = "Stochastic %K")
d = input (1, minval=1, title = "Stochastic %D")
Sto = stoch (close, highest(Length), lowest(Length), Length)
K= sma (Sto, k)
D= sma (Sto, d)
plot (D, title  ="%D", color = color.red, linewidth=1)
plot (K, title  ="%K", color = color.aqua, linewidth=1)
hline(80, title = "Upper Limit", color = color.red, linestyle =  hline.style_dotted)
hline(20, title = "Lower Limit", color = color.lime, linestyle =  hline.style_dotted)

Screenshot of stochastics in tradingview

The difference can be seen in the above screenshot.

Pls help in correcting the code if I am doing something wrong.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Pratik
  • 3
  • 1
  • 4

3 Answers3

2

Need to use:

Sto = stoch (close, high, low, Length)

instead of:

Sto = stoch (close, highest(Length), lowest(Length), Length)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
0

In 5 version of Pine script

//@version=5
indicator("lk - Stoch (v5)")

//Input values for configuration on the chart
Length = input.int (defval=14, title = "Stochastic Length", minval=1, step=1)
k = input.int (defval=3, title = "Stochastic %K", minval=1, step=1)
d = input.int (defval=3, title = "Stochastic %D", minval=1, step=1)


Sto = ta.stoch (close, high, low, Length)
K= ta.sma (Sto, k)
D= ta.sma (Sto, d)

//Drawing 
plot (D, title  ="%D", color = color.green, linewidth=1)
plot (K, title  ="%K", color = color.lime, linewidth=1)
hline(80, title = "Upper Limit", color = color.red, linestyle =  hline.style_dotted)
hline(20, title = "Lower Limit", color = color.lime, linestyle =  hline.style_dotted)
0

Need to work out highest and lowest first

length = input.int (defval=14, title = "Stochastic Length", minval=1, step=1)
smoothD = input.int (defval=5, title = "Stochastic %D", minval=1, step=1)

highest = ta.highest(high, length)
// lowest low
lowest = ta.lowest(low, length)

k = ta.stoch (close, highest, lowest, length)
d = ta.sma(k, smoothD)
Abe
  • 651
  • 1
  • 5
  • 4