Search code examples
pine-script

Cannot call 'input' with 'defval'=series[integer]


I am getting the following error trying to declare a variable in my pinescript.

Cannot call 'input' with 'defval'=series[integer]

Script code:

// Function to determine the input value based on the timeframe
f_get_period_input() =>
    var int period = na
    if (timeframe.isintraday and timeframe.multiplier == 60) // Hourly timeframe
        period := 24
    else if (timeframe.isdaily) // Daily timeframe
        period := 30
    else
        period := na

// Get the appropriate period value
period_input = f_get_period_input()

// Use the period_input in your script
periods = input(period_input != na ? period_input : 3, minval=1, title="Moving Average Period")

Solution

  • You cannot do that. defval argument of input() expects a const.

    defval (const int/float/bool/string/color or source-type built-ins) Determines the default value of the input variable proposed in the script's "Settings/Inputs" tab, from where script users can change it. Source-type built-ins are built-in series float variables that specify the source of the calculation: close, hlc3, etc.

    The value of a const variable needs to be established before script starts its execution. However, things like timeframe.isintraday, timeframe.multiplier, timeframe.isdaily are only accessible once the script starts its execution.

    You can just get the input as you would normally do and modify it later if needed.

    Also, use the na() function when you want to test for na. Do not directly compare.

    in_periods = input.int(3, minval=1, title="Moving Average Period")
    
    periods = not na(period_input) ? period_input : in_periods