pine-scriptpine-script-v5technical-indicator

Pine script - declaration variable overruling reassignment operator


I think I'm trying to write a loop. I am trying to switch the value of ims_dir (and eventually ms_dir too) depending on fulfillments of two if statements. I've tried declaring ims_dir = na, and then having the first if statement also accept ims_dir == na, but that messes with the if statements function. Really I'm just looking for a way to switch between these two if statements indefinitely depending on when they meet the criteria, but having to declare the variable that acts as the switch so I can have the if statements re-assign it's value is messing with the if statements. I added <<<< on the right so you can easily find it, hope it helps.

Pretty sure this is an easy fix, but not sure how to do it. Thanks so much for your help!!!!

indicator('WP Market Structure', 'WP MS', true, format.price, max_bars_back=5000)
//
//
//
startpoint = input.time(timestamp('21 Nov 2022 00:00'), 'start', confirm=true)
start_dir  = input.int (1, 'Direction', [1, -1])


highest = ta.highest(3)
lowest  = ta.lowest (3)
//
//
//
var line [] lines = array.new_line()
// var line [] lines = array.new_line()
// var line [] lines = array.new_line()
//
//
//
ms_management(start_point, array_lines) =>
    //
    if (array.size(array_lines) > 0)
        //
        double_close_dn = (close<open and (close<open)[1])
        double_close_up = (close>open and (close>open)[1])
        int ms_dir  = na //THIS                                                             <<<<
        int ims_dir = na //THIS                                                             <<<<
        //
        //
        //
        if ms_dir==1
            if ims_dir==1 //THIS                                                            <<<<
                if not double_close_dn
                    line.set_xy2(array.get(array_lines, 0), time, highest)
                    //
                if double_close_dn
                    line.set_xy2(array.get(array_lines, 0), time, highest)
                    array.unshift(lines, line.new(time, highest, time, low, xloc=xloc.bar_time, extend=extend.none, style=line.style_solid, width=1, color=color.red) )
                    ims_dir := -1 //Swtich variable to -1 so other if block will take over  <<<<
                    //
                //
            if ims_dir==-1 //THIS                                                           <<<<
                if not double_close_up
                    line.set_xy2(array.get(array_lines, 0), time, lowest)
                    //
                if double_close_up
                    line.set_xy2(array.get(array_lines, 0), time, lowest)
                    array.unshift(lines, line.new(time, lowest, time, high, xloc=xloc.bar_time, extend=extend.none, style=line.style_solid, width=1, color=color.green) )
                    ims_dir := 1 //Swtich variable to 1 so other if block will take over    <<<<
                    //
                //
        label.new(bar_index, close, str.tostring(ims_dir))
            //
        if false //ms_dir==-1 //Havent gotten to this part yet
            if not double_close_up
                line.set_xy2(array.get(array_lines, 0), time, low)
            else if double_close_up
                line.set_xy2(array.get(array_lines, 0), time, lowest)
                array.unshift(lines, line.new(time, low, time, high, xloc=xloc.bar_time, extend=extend.none, style=line.style_solid, width=1, color=color.blue) )
        
        //
    //
//
//
//
if time == startpoint
    array.unshift(lines, line.new(time, low, time, high + 10000, xloc=xloc.bar_time, extend=extend.none, style=line.style_solid, width=1, color=color.blue) )
//
// 
//
ms_management(startpoint, lines)
//
//
//

Solution

  • hey there is litle error in your code at line 32 and 33 becuse u have not store the value if u have assigned the 1 then u have to store it thats why your code is not working here is your correct code link [https://pastebin.com/BjqFerar] if u are trying to make a zig-zag line here is good indicator [https://www.tradingview.com/script/Hkrtv45t-Option-Pair-Zigzag/]