skip to Main Content

looking to see the stochastic of a lower TF to get a better entry < with a view to creating an alert to telegram>.
Have used the pine script v5 manual for guidance <request.security_lower_tf >. When I use a chart on a daily and place the code to look a the 15min stochastic chart. It does not corelate with a chart based on 15min and using the stochastic based on this chart for the same pair.

when I change to a daily or weekly chart the stoch based on 15min changes. So I seem not be accessing the lower timeframe data. I can understand if I was using an hourly chart, because possibly I could only be getting 4 bars worth of data of 15min. But if this was the case would expect the 15min stoch. on a weekly/monthly chart not to change.

Also the scale seems to wrong across different time periods
Code:

//@version=5
indicator(title="Stochastic LWR TImeframe", shorttitle="Stoch", format=format.price, precision=2)

string i_Period = input.timeframe(title="Lower Timeframe", defval="15")
periodK = input.int(14, title="%K Length", minval=1)
smoothK = input.int(1, title="%K Smoothing", minval=1)
periodD = input.int(3, title="%D Smoothing", minval=1)



//GET STOCHASTIC VALUES 
float k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
float d = ta.sma(k,periodD)

// Get lower timeframe values
float[] LTF_k = request.security_lower_tf(syminfo.tickerid, i_Period, k)
float LTF_k1 = nz(array.sum(LTF_k) /k)
float[] LTF_d = request.security_lower_tf(syminfo.tickerid, i_Period, d)
float LTF_d1 = nz(array.sum(LTF_d) /d)

      

//PLOT
plot(LTF_k1, title="%K", color=#2962FF)
plot(LTF_d1, title="%D", color=#FF6D00)
h0 = hline(80, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")

2

Answers


  1. The problem you’re encountering with the Stochastic values not aligning with the lower timeframe data might be due to how you’re utilizing the request.security_lower_tf function. Let’s examine your code more closely:

    //FETCH STOCHASTIC VALUES 
    float k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
    float d = ta.sma(k, periodD)
    
    // Obtain lower timeframe values
    float[] LTF_k = request.security_lower_tf(syminfo.tickerid, i_Period, k)
    float LTF_k1 = nz(array.sum(LTF_k) / k)
    float[] LTF_d = request.security_lower_tf(syminfo.tickerid, i_Period, d)
    float LTF_d1 = nz(array.sum(LTF_d) / d)
    

    The request.security_lower_tf function is employed to access the data from a lower timeframe within your current timeframe. However, in your code, you’re attempting to use it on the calculated Stochastic values (k and d) rather than the close, high, and low prices. That’s why you’re not achieving the expected results.

    To resolve this issue, you should employ request.security_lower_tf on the close, high, and low prices instead. Here’s an updated version of your code:

    //@version=5
    indicator(title="Stochastic LWR TImeframe", shorttitle="Stoch", format=format.price, precision=2)
    
    string i_Period = input.timeframe(title="Lower Timeframe", defval="15")
    periodK = input.int(14, title="%K Length", minval=1)
    smoothK = input.int(1, title="%K Smoothing", minval=1)
    periodD = input.int(3, title="%D Smoothing", minval=1)
    
    // Obtain lower timeframe close, high, and low prices
    float[] LTF_close = request.security_lower_tf(syminfo.tickerid, i_Period, close)
    float[] LTF_high = request.security_lower_tf(syminfo.tickerid, i_Period, high)
    float[] LTF_low = request.security_lower_tf(syminfo.tickerid, i_Period, low)
    
    // Calculate Stochastic values on the lower timeframe
    float LTF_k = ta.sma(ta.stoch(LTF_close, LTF_high, LTF_low, periodK), smoothK)
    float LTF_d = ta.sma(LTF_k, periodD)
    
    // Plot Stochastic values
    plot(LTF_k, title="%K", color=#2962FF)
    plot(LTF_d, title="%D", color=#FF6D00)
    h0 = hline(80, "Upper Band", color=#787B86)
    hline(50, "Middle Band", color=color.new(#787B86, 50))
    h1 = hline(20, "Lower Band", color=#787B86)
    fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")
    
    Login or Signup to reply.
  2. Your stoch calculation is off. Just do it as you would do on your chart’s timeframe and then pass the k and d to security().

    //@version=5
    indicator(title="Stochastic LWR TImeframe", shorttitle="Stoch", format=format.price, precision=2)
    
    string i_Period = input.timeframe(title="Lower Timeframe", defval="15")
    periodK = input.int(14, title="%K Length", minval=1)
    smoothK = input.int(1, title="%K Smoothing", minval=1)
    periodD = input.int(3, title="%D Smoothing", minval=1)
    
    ctf_k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
    ctf_d = ta.sma(ctf_k, periodD)
    
    [_ltf_k, _ltf_d] = request.security_lower_tf(syminfo.tickerid, i_Period, [ctf_k, ctf_d])
    
    len = array.size(_ltf_k)
    
    float ltf_k = (len > 0) ? array.get(_ltf_k, len - 1) : na
    float ltf_d = (len > 0) ? array.get(_ltf_d, len - 1) : na
    
    //PLOT
    plot(ltf_k, title="%K", color=#2962FF)
    plot(ltf_d, title="%D", color=#FF6D00)
    h0 = hline(80, "Upper Band", color=#787B86)
    hline(50, "Middle Band", color=color.new(#787B86, 50))
    h1 = hline(20, "Lower Band", color=#787B86)
    fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search