Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Problem With if Logic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Problem With if Logic

    I'm having a strange issue with two if statements, I think the code and output is pretty self explanatory. The first output makes sense, however, the second output does not. The second output (after the new line) is calculated on bar update. Only one of the boolean values changed, but somehow the allSup and allRes variables changed. How is this possible? I don't want the allSup or allRes changing until x, y, and z are in agreement.

    Code:
    if (xSup && ySup && zSup) {
    allSup[0] = true;
    allRes[0] = false;
    }
    else if (!xSup && !ySup && !zSup) {
    allSup[0] = false;
    allRes[0] = true;
    }
    Print("xSup: " + xSup);
    Print("ySup: " + ySup);
    Print("zSup: " + zSup);

    Print("allSup[0]: " + allSup[0]);
    Print("allRes[0]: " + allRes[0]);


    Output:
    xSup: False
    ySup: False
    zSup: False
    allSup[0]: False
    allRes[0]: True

    xSup: False
    ySup: False
    zSup: True
    allSup[0]: True
    allRes[0]: False

    #2
    Hello Nick L,

    Thank you for your inquiry and welcome to the NinjaTrader Support Forum!

    To investigate further, can you please attach your indicator script to your response?

    To export your script do the following:
    1. Click File -> Utilities -> Export NinjaScript
    2. Enter a unique name for the file in the value for 'File name:'
    3. Select the indicator from the objects list on the left -> click the right facing arrow ">" to add the indicator to the export
    4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


    Below is a link to the help guide on Exporting NinjaScripts.
    http://www.ninjatrader.com/support/h...nt7/export.htm
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hi Zachary, thank you for the timely response.
      Unfortunately, no, I would not like to post my entire indicator code. However, I can give a brief summary of what my code is doing before and after the snippet I gave previously. I have three different indicators, that can either act as support or resistance. I set the bool values I previously wrote (xSup, ySup, zSup) depending on whether they are sup or res. Then there is the code I wrote. After that, if allSup[0] is true I plot one of the sup indicators, else I plot one of the res indicators. All of the indicators work and plot perfectly when individually plotted. There are zero bugs until the code I previously posted.

      I just don't understand how the allRes[0] and allSup[0] changed in my example. From the output you can clearly see what it did, and it makes no sense to me. Two of the three bool values were false in the first if statement, but it still went inside the if? Could it be something to do with the fact that I'm using bool's in my if statement but changing Series<bool>'s inside the if?

      In case it makes any difference, which I don't think it should, I am using NT8.

      Comment


        #4
        Hello Nick L,

        You would not need to post your entire indicator code.

        Would you be able to produce a sample script that can duplicate the behavior that you are getting with your indicator so I can investigate this further?

        Are allSup[0] and allRes[0] set to true/false elsewhere in your code?
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          In response to your last question, no, I was only modifying the values of allSup[0] and allRes[0] in the two if statements I posted.

          Update: I have fixed the issue. When I changed allRes[0] to a regular bool (just allRes), same with allSup[0] to allSup, everything works perfectly. So the error was somehow being caused by the bool series. Any ideas of what could have been causing the bool series to act the way it was? I believe NT8 has changed the way it deals with data series. Obviously there is a high chance that I was doing something wrong, but it might also be a bug in the new NT8 Series<bool> method.

          The reason I started using the bool series instead of just bool was because I was writing a strategy based on the indicator. In the strategy, when I tried to access myIndicator().allRes it always returned false. Once I switched it to Series<bool> allRes it passed correct values, most of the time. The only errors where in the strange moments like I previously posted, which only happened maybe 5-10% of the time.

          Comment


            #6
            Hello Nick L,

            When utilizing a bool Series, there will be a new element at the 0 index of your bool Series object on an OnBarUpdate() call. The previous value is now stored in the 1 index, the one previous to that at 2, etc.

            Series<T> objects are always in sync by the CurrentBar index.

            Please take a look at the NinjaTrader help guide at this link for further information about Series<T> objects: http://ninjatrader.com/support/helpG...s/?seriest.htm

            This also applied with NinjaTrader 7.

            The default value of a bool variable is false.

            I have attached two indicators, one for NT8 and one for NT7, that demonstrates this.

            You'll want to test the indicators on real-time data. The bool at the 0 index is set to true only once in the entire code. Upon a bar closing, you'll see that the 0 index will now be false and the 1 index will be true (because the previous value was true).
            Attached Files
            Zachary G.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by aa731, Today, 02:54 AM
            0 responses
            4 views
            0 likes
            Last Post aa731
            by aa731
             
            Started by thanajo, 05-04-2021, 02:11 AM
            3 responses
            470 views
            0 likes
            Last Post tradingnasdaqprueba  
            Started by Christopher_R, Today, 12:29 AM
            0 responses
            10 views
            0 likes
            Last Post Christopher_R  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            166 responses
            2,237 views
            0 likes
            Last Post sidlercom80  
            Started by thread, Yesterday, 11:58 PM
            0 responses
            5 views
            0 likes
            Last Post thread
            by thread
             
            Working...
            X