Search code examples
pine-scripttradingpine-script-v5tradingview-api

Having issues with Multi-Time Frame input


I'm a novice coder who needs help. How am I able to add the input.timeframe() here? I'm having problems with all the aspects of the request.security() part with the ma_function(). I want to be able to lock the ATR Values to the timeframe inputed, instead of changing whenever I change timeframes

Here is all the code I have so far. Any help would be much appreciated! Thank you for your time!

atr_ma = input.string(group="ATR - Average True Range", title="Smoothing:", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
    atr_lenght = input.int(group="ATR - Average True Range", title="Lenght:", defval=14, minval=1)
    atr_multiply = input.int(group="ATR - Average True Range", title="Multiplier:", defval=2, minval=1)

color_atrText = color.silver
color_atrLow = color.teal
color_atrHigh = color.red

ma_function(source, atr_lenght) =>
    if atr_ma == "RMA"
        ta.rma(source, atr_lenght)
    else
        if atr_ma == "SMA"
            ta.sma(source, atr_lenght)
        else
            if atr_ma == "EMA"
                ta.ema(source, atr_lenght)
            else
                ta.wma(source, atr_lenght)
                
atrValue = ma_function(ta.tr(true), atr_lenght) * atr_multiply
atrValueHigh = ma_function(ta.tr(true), atr_lenght) * atr_multiply + high
atrValueLow = low - ma_function(ta.tr(true), atr_lenght) * atr_multiply

var table AtrTable = table.new(position.bottom_center, 3, 1, border_width = 3)   
f_fillCell(_table, _column, _row, _value, _timeframe) =>
    _cellText = _timeframe+ str.tostring(_value, "#.#")
    table.cell(_table, _column, _row, _cellText, text_color = color_atrText)
    table.cell_set_text_color(AtrTable, 1, 0, color.new(color_atrLow, transp = 0))
    table.cell_set_text_color(AtrTable, 2, 0, color.new(color_atrHigh, transp = 0))
    
if barstate.islast and show_atr == "On"
    f_fillCell(AtrTable, 0, 0, atrValue, "ATR: ")
    f_fillCell(AtrTable, 1, 0, atrValueLow, "L: " )
    f_fillCell(AtrTable, 2, 0, atrValueHigh, "H: ")   

Solution

  • If I understood you correctly, this is what you want

    
    
    //@version=5
    indicator("My script")
    
    i_res = input.timeframe('D', "Resolution", options=['D', 'W', 'M'])
    atr_ma = input.string(group="ATR - Average True Range", title="Smoothing:", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
    atr_lenght = input.int(group="ATR - Average True Range", title="Lenght:", defval=14, minval=1)
    atr_multiply = input.int(group="ATR - Average True Range", title="Multiplier:", defval=2, minval=1)
    
    color_atrText = color.silver
    color_atrLow = color.teal
    color_atrHigh = color.red
    
    ma_function(source, atr_lenght) =>
        if atr_ma == "RMA"
            ta.rma(source, atr_lenght)
        else
            if atr_ma == "SMA"
                ta.sma(source, atr_lenght)
            else
                if atr_ma == "EMA"
                    ta.ema(source, atr_lenght)
                else
                    ta.wma(source, atr_lenght)
    
                    
    atrValue = request.security("", i_res,  ma_function(ta.tr(true), atr_lenght) * atr_multiply)
    atrValueHigh = request.security("", i_res, ma_function(ta.tr(true), atr_lenght) * atr_multiply + high)
    atrValueLow = request.security("", i_res, low - ma_function(ta.tr(true), atr_lenght) * atr_multiply)
    
    var table AtrTable = table.new(position.bottom_center, 3, 1, border_width = 3)   
    f_fillCell(_table, _column, _row, _value, _timeframe) =>
        _cellText = _timeframe+ str.tostring(_value, "#.#")
        table.cell(_table, _column, _row, _cellText, text_color = color_atrText)
        table.cell_set_text_color(AtrTable, 1, 0, color.new(color_atrLow, transp = 0))
        table.cell_set_text_color(AtrTable, 2, 0, color.new(color_atrHigh, transp = 0))
        
    if barstate.islast 
        f_fillCell(AtrTable, 0, 0, atrValue, "ATR: ")
        f_fillCell(AtrTable, 1, 0, atrValueLow, "L: " )
        f_fillCell(AtrTable, 2, 0, atrValueHigh, "H: ")  
    
    

    if not please elaborate