Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Error at 23:5 Value with NA type cannot be assigned to a variable that was defined…

  1. User

    Error at 23:5 Value with NA type cannot be assigned to a variable that was defined without type keyword: //@version=5 indicator(title="BoS and ChoCh Indicator", shorttitle="BoS & ChoCh Indicator", overlay=true, max_lines_count=500, max_labels_count=500)

    // Input parameters swingLookback = input.int(title="Swing Lookback Period", defval=3, minval=1, maxval=20) showBoS = input.bool(title="Show Break of Structure (BoS)", defval=true) showChoCh = input.bool(title="Show Change of Character (ChoCh)", defval=true) showLabels = input.bool(title="Show Labels", defval=true) lineExtension = input.string(title="Line Extension", defval="Right", options=["None", "Right", "Both"]) higherTF = input.timeframe(title="Higher Timeframe", defval="", tooltip="Leave empty to use current timeframe")

    // Map line extension to extend parameter extend_mode = switch lineExtension "None" => extend.none "Right" => extend.right "Both" => extend.both => extend.right

    // Function to detect swings and times f_detectSwings(_swingLookback) => // Identify swing highs and lows swh = ta.pivothigh(high, _swingLookback, _swingLookback) swhTime = na if not na(swh) swhTime := time[_swingLookback] swl = ta.pivotlow(low, _swingLookback, _swingLookback) swlTime = na if not na(swl) swlTime := time[_swingLookback] [swh, swhTime, swl, swlTime]

    // Variables to store swing highs and lows var float[] swingHighs = array.new_float() var int[] swingHighTimes = array.new_int() var float[] swingLows = array.new_float() var int[] swingLowTimes = array.new_int()

    // Get swings from higher timeframe if specified [swh, swhTime, swl, swlTime] = request.security(syminfo.tickerid, higherTF == "" ? timeframe.period : higherTF, f_detectSwings(swingLookback), barmerge.gaps_off, barmerge.lookahead_off)

    // Update swing highs and lows arrays if not na(swh) array.unshift(swingHighs, swh) array.unshift(swingHighTimes, swhTime) if array.size(swingHighs) > 10 array.pop(swingHighs) array.pop(swingHighTimes)

    if not na(swl) array.unshift(swingLows, swl) array.unshift(swingLowTimes, swlTime) if array.size(swingLows) > 10 array.pop(swingLows) array.pop(swingLowTimes)

    // Determine the current trend var string trend = na if array.size(swingHighs) >= 2 and array.size(swingLows) >= 2 lastSwingHigh = array.get(swingHighs, 0) prevSwingHigh = array.get(swingHighs, 1) lastSwingLow = array.get(swingLows, 0) prevSwingLow = array.get(swingLows, 1)

    // If last swing high is higher than previous swing high and last swing low is higher than previous swing low
    if lastSwingHigh > prevSwingHigh and lastSwingLow > prevSwingLow
        trend := "uptrend"
    // If last swing high is lower than previous swing high and last swing low is lower than previous swing low
    else if lastSwingHigh < prevSwingHigh and lastSwingLow < prevSwingLow
        trend := "downtrend"
    else
        trend := "sideways"
    

    // Variables to hold BoS and ChoCh lines and labels var line bosLine = na var label bosLabel = na var line chochLine = na var label chochLabel = na

    // Variables to store event times var int bosTime = na var int chochTime = na

    // Detect BoS and ChoCh based on trend if trend == "uptrend" // Break of Structure (BoS) to the upside if showBoS and close > array.get(swingHighs, 0) bosTime := time // Delete previous BoS line and label if not na(bosLine) line.delete(bosLine) if not na(bosLabel) label.delete(bosLabel) // Draw BoS line bosLine := line.new(x1=array.get(swingHighTimes, 0), y1=array.get(swingHighs, 0), x2=bosTime, y2=array.get(swingHighs, 0), xloc=xloc.bar_time, extend=extend_mode, color=color.green, width=2) // Add label if showLabels bosLabel := label.new(x=bosTime, y=array.get(swingHighs, 0), text="BoS High", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.green, 0), textcolor=color.white, size=size.tiny) // Change of Character (ChoCh) to the downside using Low if showChoCh and low < array.get(swingLows, 0) chochTime := time // Delete previous ChoCh line and label if not na(chochLine) line.delete(chochLine) if not na(chochLabel) label.delete(chochLabel) // Draw ChoCh line chochLine := line.new(x1=array.get(swingLowTimes, 0), y1=array.get(swingLows, 0), x2=chochTime, y2=array.get(swingLows, 0), xloc=xloc.bar_time, extend=extend_mode, color=color.orange, width=2) // Add label if showLabels chochLabel := label.new(x=chochTime, y=array.get(swingLows, 0), text="ChoCh Low", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.orange, 0), textcolor=color.white, size=size.tiny) // Reset trend trend := "downtrend"

    else if trend == "downtrend" // Break of Structure (BoS) to the downside if showBoS and close < array.get(swingLows, 0) bosTime := time // Delete previous BoS line and label if not na(bosLine) line.delete(bosLine) if not na(bosLabel) label.delete(bosLabel) // Draw BoS line bosLine := line.new(x1=array.get(swingLowTimes, 0), y1=array.get(swingLows, 0), x2=bosTime, y2=array.get(swingLows, 0), xloc=xloc.bar_time, extend=extend_mode, color=color.red, width=2) // Add label if showLabels bosLabel := label.new(x=bosTime, y=array.get(swingLows, 0), text="BoS Low", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.red, 0), textcolor=color.white, size=size.tiny) // Change of Character (ChoCh) to the upside using High if showChoCh and high > array.get(swingHighs, 0) chochTime := time // Delete previous ChoCh line and label if not na(chochLine) line.delete(chochLine) if not na(chochLabel) label.delete(chochLabel) // Draw ChoCh line chochLine := line.new(x1=array.get(swingHighTimes, 0), y1=array.get(swingHighs, 0), x2=chochTime, y2=array.get(swingHighs, 0), xloc=xloc.bar_time, extend=extend_mode, color=color.purple, width=2) // Add label if showLabels chochLabel := label.new(x=chochTime, y=array.get(swingHighs, 0), text="ChoCh High", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.purple, 0), textcolor=color.white, size=size.tiny) // Reset trend trend := "uptrend"

  2. Assistant

    The error you're encountering stems from assigning na (which has a special na type in Pine Script) to a variable without explicitly specifying its type. In Pine Script version 5, when you assign na to a variable, the compiler needs to know the intended type of the variable to handle it correctly. Without specifying the type, the compiler cannot infer the type from na, leading to the error you've encountered.

    Here's how you can fix the error:

    In your function f_detectSwings, you need to explicitly declare the types of swhTime and swlTime when initializing them with na. Modify lines 23 and 27 by specifying the variable type int:

    int swhTime = na
    ...
    int swlTime = na
    

    Updated Code Snippet:

    f_detectSwings(_swingLookback) =>
        // Identify swing highs and lows
        swh = ta.pivothigh(high, _swingLookback, _swingLookback)
        int swhTime = na
        if not na(swh)
            swhTime := time[_swingLookback]
        swl = ta.pivotlow(low, _swingLookback, _swingLookback)
        int swlTime = na
        if not na(swl)
            swlTime := time[_swingLookback]
        [swh, swhTime, swl, swlTime]
    

    By explicitly declaring swhTime and swlTime as integers, you inform the compiler of the expected type, and it can handle the na assignment correctly. This change should resolve the error, and your script should compile and run as expected.