0

Another newby question. Im trying to get data from other timeframes on the macd and stoch. I have the macd working (i think) thanks to others on this site. Now that im trying to get the info for the stoch i have hit issues with the security request . I want to know the value of k and d for the given timeframe but am not sure how to set up the ta.stoch.

stoch_length        = input.int(14, title = 'K Length', minval=1 , group = stochGroup)
smoothK             = input.int(3, 'Smooth K', group = stochGroup)
smoothD             = input.int(3, "Smooth D", group = stochGroup)
OverBought          = input.int(80, group = stochGroup)
OverSold            = input.int(20, group = stochGroup)
lengthRSI           = input.int(14, "RSI Length", minval=1)
stoch_src           = input(close, title="RSI Source")
rsi1                = ta.rsi(src, lengthRSI)
k                   = ta.sma(ta.stoch(rsi1, rsi1, rsi1, stoch_length), smoothK)
d                   = ta.sma(k, smoothD)

[k5,d5,stochlength5] = request.security(syminfo.tickerid, "5", ta.stoch(stoch_src, smoothK, smoothD,stoch_length), barmerge.gaps_off,  barmerge.lookahead_on)

I want it to point to my variables so that if i change them in the inputs list it automatically calculates based on the new figures without having to go in a change them manually in the script ( if possible)

This is how i have the macd setup , i thought i was working but if i set up an alert on the 5m chart it sends the alert based on the macd of that chart , not the 60m chart

f_get_60_macd() => [src, fastMAlen, slowMAlen, hist]

[close60,macdLine60,signalLine60,histLine60] = request.security(syminfo.tickerid, "60", f_get_60_macd(), barmerge.gaps_off,  barmerge.lookahead_on)

macd_already_up_60          = ((macdLine60 > signalLine60) and  (histLine60 > histLine60[1])) or ((macdLine60 > macdLine60[1]) and  (histLine60 > histLine60[1]))
macd_already_dn_60          = ((macdLine60 < signalLine60) and ( histLine60 < histLine60[1])) or ((macdLine60 > signalLine60) and  (histLine60 < histLine60[1]))

macd_trend_state_60         = macd_already_up_60 ? 1 : macd_already_dn_60 ? -1 : 0

if  macd_trend_state_60  == 1 
    message = "60m macd trend UP " +syminfo.ticker
    alert(message, alert.freq_once_per_bar_close)

So then i can use it in the following scenario

stoch_cross_Up      = ta.crossover(k,d)
stoch_cross_Dn      = ta.crossunder(k,d)

stoch_already_up            = (k > d) and  (k > k[1])
stoch_already_dn            = ((k < d) and (k < k[1])) or ((k > d) and  (k < k[1]))

stoch_trend_state           = stoch_already_up  ? 1 : stoch_already_dn ? -1 : 0
stoch_cross_state           = stoch_cross_Up ? 1 : stoch_cross_Dn ? -1 : 0

Thanks in advance for any help

Thanks again for the help John. I have it set up like below but now i keep getting an error on line 2 of the function saying

fastMAlen           = input.int(12, minval=1, title="macd fast moving average")
slowMAlen           = input.int(26, minval=1, title="macd slow moving average")
signalmacdlen       = input.int(9, minval=1, title="macd signal line moving average")       f_get_macd() => [src, fastMAlen, slowMAlen, signalmacdlen]

f_get_macd() => [src, fastMAlen, slowMAlen, signalmacdlen]
    [macdLine,signalLine,histLine]= ta.macd(src,fastMAlen, slowMAlen, signalmacdlen)
    macd_already_up_ = ((macdLine > signalLine) and  (histLine > histLine[1])) or ((macdLine > macdLine[1]) and  (histLine > histLine[1]))
    macd_already_dn_ = ((macdLine < signalLine) and ( histLine < histLine[1])) or ((macdLine > signalLine) and  (histLine < histLine[1]))
    macd_trend_state_ = macd_already_up_ ? 1 : macd_already_dn_ ? -1 : 0
    macd_trend_state_

macd_trend_state_5 = request.security(syminfo.tickerid, "5", f_get_macd(src, fastMAlen, slowMAlen, signalmacdlen), barmerge.gaps_off)
                             
Mismatched input '[' expecting 'end of line without line continuation'      

i cant work out why. Sorry for the repeated stupid questions but as you have probably guessed i am very new to this

Juz
  • 17
  • 3

1 Answers1

0

To properly get the signal you desire in the timeframe, the entire function should be run in the desired timeframe. I am writing just the macd function here with the signals you indicated. You would need to set up a similar function for stoch.

f_get_macd() => [src, fastMAlen, slowMAlen, hist]
    [macdLine,signalLine,histLine]= ta.macd(src, fastMAlen, slowMAlen,hist)
    macd_already_up_ = ((macdLine > signalLine) and  (histLine > histLine[1])) or ((macdLine > macdLine[1]) and  (histLine > histLine[1]))
    macd_already_dn_ = ((macdLine < signalLine) and ( histLine < histLine[1])) or ((macdLine > signalLine) and  (histLine < histLine[1]))
    macd_trend_state_ = macd_already_up_ ? 1 : macd_already_dn_ ? -1 : 0
    macd_trend_state_

macd_trend_state_60 = request.security(syminfo.tickerid, "60", f_get_macd(close,fastlen,slowlen,histlen), barmerge.gaps_off)

if macd_trend_state_60  == 1 
    message = "60m macd trend UP " +syminfo.ticker
    alert(message, alert.freq_once_per_bar_close
John Baron
  • 291
  • 2
  • 8