Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

find two bools in the last x number of bars?

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

    find two bools in the last x number of bars?

    Hi i have two bools isHighSequence and isLowSequence in my code that define a setup. I want to iterate though last 8 bars to see if those two bools both appeared(not at the same time of course). We cant sue bools for loop. What would be the approach to accomplish it?


    Code:
    bool isHighSeqAfterLowSeq = false;
    
    
    // Loop through the last 8 bars to check for isHighSequence and isLowSequence
    for (int i = 0; i < 8; i++)
    {
        // Check if either condition is true in the last 8 bars
        if (isHighSequence[i])
        {
            foundHigh = true;
        }
        if (isLowSequence[i])
        {
            foundLow = true;
        }
    
        // If both conditions have been found in the last 8 bars, set the flag to true
        if (foundHigh && foundLow)
        {
            isHighSeqAfterLowSeq = true;
            break;  // Exit the loop if both sequences have been found
        }
    }​

    #2
    Make sure you reset both foundHigh and foundLow to false before each run of the loop.
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Hello tkaboris,

      It looks like you have an index with isHighSequence[i] which would imply the variable object type is Series<bool> and not bool.


      "We cant sue bools for loop"

      A bool is a single true or false value. A Series<bool> would be a true or false value stored for every bar on the chart.
      A bool cannot be looped through as it's just a single value. A series can be looped through because its a collection with multiple values.

      To confirm you are using a Series<bool> object type correct?

      Are you assigning a value to the Series<bool> variable on each bar?

      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        I dont use series bool. Is using series the way to go for me?

        Comment


          #5
          Hello tkaboris,

          Yes, if you need to store a value for each bar on the chart use a Series<T> where the T is the object type you want to store.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            if i use bool series. how would you write a for loop?

            Comment


              #7
              Hello tkaboris,

              for (int index = 0; index < CurrentBar; index++)
              {
              Print(myBoolSeries[index]);
              }
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                HI i have this code after converting isHighSequence and isLowSequence too serires. Its not currently print when condition met. I just need foundHIgh when both bools were true anywhere in the last 8 bars

                Code:
                bool foundHigh = false;
                            
                            // Loop through the last 8 bars to check for isHighSequence and isLowSequence
                            for (int i = 0; i < 8; i++)
                            {
                                // Check if either condition is true in the last 8 bars
                                if (isHighSequence[i] && isLowSequence[i])
                                {
                                    foundHigh = true;
                                    Print("found high");
                                break;
                                }​

                Comment


                  #9
                  Hello tkaboris,

                  Add debugging prints to understand why.

                  Below is a link to a support article on adding debugging prints to understand behavior.


                  Print the time of the bar and the values in the condition isHighSequence[i] and isLowSequence[i] as well as Time[i] so we know which bar the value is for.

                  Open the NinjaScript Output window, reload the script, save the output to a text file (right-click the output window, select Save as) and attach the output text file to your reply.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    HI as seen in picture i want to capture both red and lime line are appeaerd in the last 8 bars. I shared output file.

                    bool foundHigh = false;
                    for (int i = 0; i < 8; i++)
                    {
                    Print("Time"+Time[0]);
                    Print("isHighSequence[i]"+isHighSequence[i]);
                    Print("isLowSequence[i]"+isLowSequence[i]);
                    if (isHighSequence[i] && isLowSequence[i])
                    {
                    foundHigh = true;
                    Print("found high");
                    break;
                    }

                    }​



                    Click image for larger version

Name:	image.png
Views:	52
Size:	82.2 KB
ID:	1322371

                    Comment


                      #11
                      Hello tkaboris,

                      When debugging I recommend focusing on one issue at a time. It's helpful to temporarily comment out all other prints except for the prints with the issue you are focusing on.

                      That said, just looking at the last bar, we can see these bool values are not true at the same time, so the condition evaluates as false.

                      Time10/23/2024 8:14:00 AM
                      isHighSequence[i]True
                      isLowSequence[i]False
                      Time10/23/2024 8:14:00 AM
                      isHighSequence[i]True
                      isLowSequence[i]False
                      Time10/23/2024 8:14:00 AM
                      isHighSequence[i]False
                      isLowSequence[i]True
                      Time10/23/2024 8:14:00 AM
                      isHighSequence[i]False
                      isLowSequence[i]False
                      Time10/23/2024 8:14:00 AM
                      isHighSequence[i]False
                      isLowSequence[i]False
                      Time10/23/2024 8:14:00 AM
                      isHighSequence[i]False
                      isLowSequence[i]False
                      Time10/23/2024 8:14:00 AM
                      isHighSequence[i]True
                      isLowSequence[i]False
                      Time10/23/2024 8:14:00 AM
                      isHighSequence[i]True
                      isLowSequence[i]False​

                      At what date and time are you expecting both bool values from these Series<bool> objects to both be true at the same time?

                      What is the code that assigns these series values?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        HI I accidently chose a wrong bool so i had to redo the bool (bearFlag[0] bullFlag[0] and was able to make it work with code below. However, with this implementation it messes up drawing, that it doesnt remove draw object and i am getting multiple lines for draw object. I was wondering if there is something additional needs to be done with the code after bool has been changed to series?

                        if (isLowSequence && trendDirection == -1 && Low[0] < Close[0])
                        {
                        double slope = (Low[0] - Low[lookbackPeriod - 1]) / lookbackPeriod;
                        trendlineValueUp = Low[0] - (slope * 0);

                        string newLineTag = "BearFlag" + bearFlagCounter;
                        if (bearFlag[0] && bearFlagCounter > 0)
                        {
                        RemoveDrawObject("BearFlag" + (bearFlagCounter - 1));
                        }
                        Draw.Line(this, newLineTag, true, lookbackPeriod-1, Low[lookbackPeriod - 1], 0, Low[0], BearFlagColor, DashStyleHelper.Solid, 2);
                        // Alert("Alert", Priority.High, "Up trend line broken", NinjaTrader.Core.Globals.InstallDir + @"\sounds\Reversing.wav", 10, Brushes.Transparent, Brushes.Transparent);
                        bearFlag[0] = true;
                        bearFlagCounter++;
                        } zz0.nwjk0pi080izz


                        Code:
                        turboBearFlag = false;
                                    turboBullFlag = false;
                                    bool foundHigh = false;
                                    bool foundLow = false;
                                    // Loop through the last 8 bars to check for isHighSequence and isLowSequence
                                    for (int i = 0; i < 8; i++)
                                    {
                                        // Check if either condition is true in the last 8 bars
                                        if (bullFlag[i])
                                        {
                                            foundHigh = true;
                                            
                                        }
                                        if (bearFlag[i])
                                        {
                                            foundLow = true;
                                            
                                        }
                                    
                                        // If both conditions have been found in the last 8 bars, set the flag to true
                                        if (bullFlag[0] && foundHigh && foundLow)
                                        {
                                            turboBullFlag = true;
                                            Print("turbo bull flag");
                                        }
                        
                                    }
                                    for (int i = 0; i < 8; i++)
                                    {
                                        // Check if either condition is true in the last 8 bars
                                        if (bullFlag[i])
                                        {
                                            foundHigh = true;
                                            
                                        }
                                        if (bearFlag[i])
                                        {
                                            foundLow = true;
                                            
                                        }
                                    
                                        if (bearFlag[0] && foundHigh && foundLow)
                                        {
                                            turboBearFlag = true;    
                                            Print("turbo bear flag");
                                        }
                        
                                    }​

                        Comment


                          #13
                          Hello tkaboris,

                          If the Draw.Line() method is being called more times than you expect, the condition that calls the method is evaluating as true.

                          Add debugging prints to understand why.

                          'if (isLowSequence && trendDirection == -1 && Low[0] < Close[0])'

                          If this condition is true, the draw method will be called.

                          Print the time of the bar and all values in this conditions with labels for each value and comparison operator.

                          Which value are you expecting to be false?
                          At what date and time are you expecting the condition to evaluate as false?

                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            it doesnt remove object because bearFlag[0] is evaluating to false in this line
                            if (bearFlag[0] && bearFlagCounter > 0)


                            private Series<bool> bearFlag;
                            private Series<bool> bullFlag;​
                            bearFlag = new Series<bool>(this, MaximumBarsLookBack.Infinite);
                            bullFlag = new Series<bool>(this, MaximumBarsLookBack.Infinite);​

                            Comment


                              #15
                              Hello tkaboris,

                              Why are you expecting bearFlag[0] to be true at a specific date and time?

                              What code is assigning this series a value?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              52 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              130 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              70 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              44 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X