Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

  • Forums
  • Pinescript
  • Request indicator values from different chart timeframes and use to send an alert

Request indicator values from different chart timeframes and use to send an alert

Nov 2, 2022 - 8:46 AM

Viewed 2025 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/1465538 COPY
  • Hi All,
    I am VERY new to pinescript and im trying to set up an alert to trigger each time a group of indicator parameters are met from different time frames on Tradingview.
    So far all i have done is make a mess of a script that i tried to modify.
    I have added some lines in which i know are not going to work but just wanted to show an idea of what i am trying to achieve.

    I just want it to send me an alert on my pc or phone each time the conditions are met for each ticker.

    strategy(title=Buy Sell Stategy, shorttitle=Buy Sell Stategy, overlay=true)

    // Input
    fastMAlen = input(12, minval=1, title=MACD fast moving average)
    slowMAlen=input(26,minval=1, title=MACD slow moving average)
    signalMACDlen = input(9,minval=1, title=MACD signal line moving average)
    StochLength = input(14, minval=1, title=Stochastic Length)
    switch=input(true, title=Enable MACD Bar Color?)

    // MACD Calculation
    1hrMACD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 60, (syminfo.MACD),
    1hrsignalMACD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 60, (syminfo.signalMACD),
    12hrMACD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 720, (syminfo.MACD),
    12hrsignalMACD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 720, (syminfo.signalMACD),
    MACD = ema(close, fastMAlen) - ema(close, slowMAlen)
    signalMACD = ema(MACD, signalMACDlen)
    delta = MACD - signalMACD
    fastMA = ema(close,fastMAlen)
    slowMA = ema(close,slowMAlen)

    // Stochastic Calculation
    1hrsmoothK = GetTickValueSymbol(symbol) =>
    request.security(symbol, 60, (syminfo.smoothK)[1],
    1hrsmoothD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 60, (syminfo.smoothD)[1],
    4hrsmoothK = GetTickValueSymbol(symbol) =>
    request.security(symbol, 240, (syminfo.smoothK)[1],
    4hrsmoothD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 240, (syminfo.smoothD)[1],
    12hrsmoothK = GetTickValueSymbol(symbol) =>
    request.security(symbol, 720, (syminfo.smoothK)[1],
    12hrsmoothK = GetTickValueSymbol(symbol) =>
    request.security(symbol, 720, (syminfo.smoothD)[1],
    smoothK = input(3, title=Smoothing of Stochastic %K)
    smoothD = input(3, title=Moving Average of Stochastic %K)
    k = sma(stoch(close, high, low, StochLength), smoothK)
    d = sma(k, smoothD)

    //RSI Calculation
    xRSI = rsi(close, 10)

    // Colors
    bartrendcolor = 1hrMACD > 1hrsignalMACD and 4hrsmoothK > 4hrsmoothD and 12hrMACD > 12hrsignalMACD and 12hrsmoothK > 12hrsmoothD? lime : MACD < signalMACD and k < d and xRSI < 50? red : MACD < signalMACD? yellow : gray

    barcolor(switch?bartrendcolor:na)

    // === STRATEGY ===
    // conditions

    calc_on_every_tick
    longCond = 1hrMACD > 1hrsignalMACD and 4hrsmoothK > 4hrsmoothD and 12hrMACD > 12hrsignalMACD and 12hrsmoothK > 12hrsmoothD
    shortCond = (charttimeframe = (60) k < d)

    strategy.entry(long, strategy.long, when=longCond==true)
    strategy.close(long, when=shortCond==true)

    // === /STRATEGY ===

    timeframe_gaps

    Thanks for any help
    Juz

    0
  • Hi Juz

    I'm not very clear on what's working vs not working with your script
    Though, with the request.security function, if you're requesting a previous historical value using the [1] operator, you have to use the lookahead=barmerge.lookahead_on parameter

    Documentation: https://www.tradingview.com/pine-script-reference/v5/#fun_request{dot}security

    2/ I think there is one missing parenthesis for all the lines like this where you're calling the request.security function

    Should be like this

    
    1hrsmoothK = GetTickValueSymbol(symbol) =>
    request.security(symbol, 60, syminfo.smoothK[1], barmerge.gaps_off, barmerge.lookahead_on)
    
    0
  • Hi Dave, thanks for the reply
    I have changed the requests to look like above.
    I am trying to get different variables from different time charts and then combine the outcome to trigger an alert, problem is that if I request macd and signal from 2 different timeframes im not sure how to name them in order to get the information and use it.
    If i rename them as below then i get the following error, but if i dont rename them 1hrmacd etc then im not sure how i can use 2 different timeframe macd inputs to trigger the alert

    Compilation error. Line 15: no viable alternative at input '1'

    1hrMACD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 60, syminfo.MACD[1], barmerge.gaps_off, barmerge.lookahead_on)
    1hrsignalMACD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 60, syminfo.signalMACD[1], barmerge.gaps_off, barmerge.lookahead_on)
    12hrMACD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 720, syminfo.MACD[1], barmerge.gaps_off, barmerge.lookahead_on)
    12hrsignalMACD = GetTickValueSymbol(symbol) =>
    request.security(symbol, 720, syminfo.signalMACD[1], barmerge.gaps_off, barmerge.lookahead_on)
    MACD = ema(close, fastMAlen) - ema(close, slowMAlen)
    signalMACD = ema(MACD, signalMACDlen)

    0
  • i also tried it this way

    MACD = GetTickValueSymbol(symbol)
    request.security(symbol, 60, syminfo.MACD[1], barmerge.gaps_off, barmerge.lookahead_on)

    which seems to work but then when i try to set conditions like below i get a no viable input for macd

    longCond = MACD 60 > signalMACD 60 and smoothK 240 > smoothD

    0
  • Solved

    0
CONTENTS