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

    #16
    I am expecting it to be true because it is
    I am not sure about assigning values to bool series. I had the same code where bullFlag[0] was bullFlag and all worked. I am assigning bullFlag[0] = true; down there in the code



    if (isHighSequence && trendDirection == 1 && High[0] > Close[0])
    {
    Print("condition 1 bull");
    double slope = (High[lookbackPeriod - 1] - High[0]) / lookbackPeriod;
    trendlineValueDn = (slope * 0) + High[0];
    Print("condition 2 bull");
    Print("bullFlagCounter"+bullFlagCounter);
    Print("bullFlag[0]"+bullFlag[0]);
    string newLineTagB = "BullFlag" + bullFlagCounter;
    if (bullFlag[0] && bullFlagCounter > 0)
    {
    RemoveDrawObject("BullFlag" + (bullFlagCounter - 1));
    }
    Draw.Line(this, newLineTagB, true, lookbackPeriod - 1, High[lookbackPeriod - 1], 0, High[0], BullFlagColor, DashStyleHelper.Solid, 2);
    bullFlag[0] = true;
    bullFlagCounter++;
    // Alert("Alert", Priority.High, "Down trend line broken", NinjaTrader.Core.Globals.InstallDir + @"\sounds\Reversing.wav", 10, Brushes.Transparent, Brushes.Transparent);
    }​

    Comment


      #17
      Hello tkaboris,

      "it doesnt remove object because bearFlag[0] is evaluating to false in this line"

      You've previously stated in post # the bool is false which is causing the condition 'if (bearFlag[0] && bearFlagCounter > 0)'.

      "I am expecting it to be true because it is"

      This doesn't make sense. It can't be both true and false. The print output will tell you what it is.
      Also, this is specific. What specific values in the output are leading you to believe that on the specific date and time the bool series was set to true on that bar.

      If this is the only line where this bool series is assigned a value, then the condition to set this value was not true at that date and time.

      Add debugging prints to understand why.
      Temporarily comment out all other prints.

      Print the time of the bar and a values in the condition that sets this bool series value.

      'if (isHighSequence && trendDirection == 1 && High[0] > Close[0])'
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        I dont know, it worked previosly with regular bools. Maybe switching to series it takes some additioanl steps.

        All I want is just to make sure if we are in the bullflag condition and there is object drawn, remove it and draw a new one.



        if (isHighSequence && trendDirection == 1 && High[0] > Close[0])
        {
        Print("condition 1 bull");
        double slope = (High[lookbackPeriod - 1] - High[0]) / lookbackPeriod;
        trendlineValueDn = (slope * 0) + High[0];
        Print("condition 2 bull");
        Print("bullFlagCounter"+bullFlagCounter);
        Print("bullFlag[0]"+bullFlag[0]);
        string newLineTagB = "BullFlag" + bullFlagCounter;
        if (bullFlag[0] && bullFlagCounter > 0)
        {
        RemoveDrawObject("BullFlag" + (bullFlagCounter - 1));
        }
        Draw.Line(this, newLineTagB, true, lookbackPeriod - 1, High[lookbackPeriod - 1], 0, High[0], BullFlagColor, DashStyleHelper.Solid, 2);
        bullFlag[0] = true;
        bullFlagCounter++;
        Print("bullFlag[0] in condition"+bullFlag[0]);
        // Alert("Alert", Priority.High, "Down trend line broken", NinjaTrader.Core.Globals.InstallDir + @"\sounds\Reversing.wav", 10, Brushes.Transparent, Brushes.Transparent);
        }​

        output
        Enabling NinjaScript strategy 'BullBearFlagTrader/312672204' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=Unique entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
        condition 1 bull
        condition 2 bull
        bullFlagCounter100
        bullFlag[0]False
        bullFlag[0] in conditionTrue
        condition 1 bull
        condition 2 bull
        bullFlagCounter101
        bullFlag[0]False
        bullFlag[0] in conditionTrue​

        Comment


          #19
          Hello tkaboris,

          You have not included the time of the bar or labels for the comparison operators as directed.
          Nor have you specified the date and time where you are expecting the bool to be true.

          Each series bar value is a separate value. It will be false for each new bar, unless the value for that bar is set to true.

          The print for the condition:
          'if (isHighSequence && trendDirection == 1 && High[0] > Close[0])'

          Would appear as:
          Print(string.Format("{0} | isHighSequence: {1} && trendDirection: {2} == 1 && High[0]: {3} > Close[0]: {3}", Time[0], isHighSequence, trendDirection, High[0], Close[0]));

          All other prints should be commented out so only this print is appearing in the output.

          "All I want is just to make sure if we are in the bullflag condition and there is object drawn, remove it and draw a new one."

          Since this is not happening as you expect, you need to debug the script and understand why.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            ok i included this in the print and It should have removed first lime line but it didnt.

            Enabling NinjaScript strategy 'BullBearFlagTrader/312672206' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=Unique entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
            10/23/2024 12:22:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20518 > Close[0]: 20518
            10/23/2024 12:24:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20517.5 > Close[0]: 20517.5

            Click image for larger version

Name:	image.png
Views:	40
Size:	38.5 KB
ID:	1322516

            Comment


              #21
              Hello tkaboris,

              Apologies, I made a mistake in the print. The 4th index {3} should be {4}.

              Print(string.Format("{0} | isHighSequence: {1} && trendDirection: {2} == 1 && High[0]: {3} > Close[0]: {4}", Time[0], isHighSequence, trendDirection, High[0], Close[0]));

              May I have you rerun this?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                sure
                Enabling NinjaScript strategy 'BullBearFlagTrader/312672206' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=Unique entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
                10/23/2024 12:22:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20518 > Close[0]: 20515.75
                10/23/2024 12:24:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20517.5 > Close[0]: 20516.75

                Comment


                  #23
                  Hello tkaboris,

                  Thanks for providing the new output.

                  So is 10/23/2024 12:24:00 AM the date and time that you are calling RemoveDrawObject()?

                  If the bool series was set to true on this bar, and then was set back to false, I would expect there is probably another line of code setting the bool series to false, or the condition that checks this bool series is evaluated before the bool series is assigned a value.

                  We can confirm by keeping the print for the condition that sets the bool series, and then printing the condition that checks the bool series.

                  'if (bearFlag[0] && bearFlagCounter > 0)'

                  The print for this would appear:

                  Print(string.Format("{0} | bearFlag[0]: {1} && bearFlagCounter: {2} > 0", Time[0], bearFlag[0], bearFlagCounter));


                  With these two prints, which print is appearing first for the 10/23/2024 12:24:00 AM bar?

                  Please provide the output.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    for this print its second line
                    With these two prints, which print is appearing first for the 10/23/2024 12:24:00 AM bar?

                    I updated the code (changed bullname from bear to bull since thats what i am testing now)
                    if (isHighSequence && trendDirection == 1 && High[0] > Close[0])
                    {
                    Print(string.Format("{0} | isHighSequence: {1} && trendDirection: {2} == 1 && High[0]: {3} > Close[0]: {4}", Time[0], isHighSequence, trendDirection, High[0], Close[0]));
                    double slope = (High[lookbackPeriod - 1] - High[0]) / lookbackPeriod;
                    trendlineValueDn = (slope * 0) + High[0];
                    if (bullFlag[0] && bullFlagCounter > 0)
                    {
                    Print(string.Format("{0} | bullFlag[0]: {1} && bullFlagCounter: {2} > 0", Time[0], bullFlag[0], bullFlagCounter));
                    }
                    string newLineTagB = "BullFlag" + bullFlagCounter;
                    if (bullFlag[0] && bullFlagCounter > 0)
                    {
                    RemoveDrawObject("BullFlag" + (bullFlagCounter - 1));
                    }
                    Draw.Line(this, newLineTagB, true, lookbackPeriod - 1, High[lookbackPeriod - 1], 0, High[0], BullFlagColor, DashStyleHelper.Solid, 2);
                    bullFlag[0] = true;
                    bullFlagCounter++;

                    // Alert("Alert", Priority.High, "Down trend line broken", NinjaTrader.Core.Globals.InstallDir + @"\sounds\Reversing.wav", 10, Brushes.Transparent, Brushes.Transparent);
                    }

                    Print is the same
                    10/23/2024 12:22:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20518 > Close[0]: 20515.75
                    10/23/2024 12:24:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20517.5 > Close[0]: 20516.75​​
                    Click image for larger version

Name:	image.png
Views:	76
Size:	395.5 KB
ID:	1322542

                    Comment


                      #25
                      Hello tkaboris,

                      Prints for conditions should be one line above the condition so they print for every bar, and not within the logic block otherwise they will only print when the condition is true.
                      We need to know why the condition is not true, so the print needs to be printed when the condition is not true.

                      Below is a link to a support article on adding debugging prints to understand behavior. Be sure you watch the videos to understand how to use prints to understand the behavior.


                      With the code you have suggested, 'bullFlag[0] = true;' is below the condition and logic block for 'if (bullFlag[0] && bullFlagCounter > 0)'.

                      This means the condition evaluates as false, then after that you set the value to true.


                      Let's say you want to raise a green flag when there is an apple in a box. You check the box and there is no apple in it, so you don't raise flag. Then after box was checked and no flag was raised you put an apple in the box. Nothing happens because you have already checked the box before you placed an apple in it.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #26
                        I moved this block before wholel condition starts so it appears on every bar but nothing prints.
                        if (bullFlag[0] && bullFlagCounter > 0)
                        {
                        Print(string.Format("{0} | bullFlag[0]: {1} && bullFlagCounter: {2} > 0", Time[0], bullFlag[0], bullFlagCounter));
                        }
                        if(!bullFlag[0])
                        {
                        Print("No bullflag" + Time[0]);
                        }​

                        I see its turning off booloean to false but i dont have it anywhere in the code where it turns off, i mean i do and when I add prints to those locations where it turns it off , prints dont print, so those conditions dont trigger for boolean to turn it to false.


                        my output
                        No bullflag10/23/2024 12:10:00 AM
                        No bullflag10/23/2024 12:12:00 AM
                        No bullflag10/23/2024 12:14:00 AM
                        No bullflag10/23/2024 12:16:00 AM
                        No bullflag10/23/2024 12:18:00 AM
                        No bullflag10/23/2024 12:20:00 AM
                        No bullflag10/23/2024 12:22:00 AM
                        10/23/2024 12:22:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20518 > Close[0]: 20515.75
                        No bullflag10/23/2024 12:24:00 AM
                        10/23/2024 12:24:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20517.5 > Close[0]: 20516.75​

                        Comment


                          #27
                          Hello tkaboris,

                          The variable assignment of the value for this bar is what you are looking for right?

                          When the 'if (isHighSequence && trendDirection == 1 && High[0] > Close[0])' condition is true, set bullFlag[0] to true.

                          The assignment should be above the conditions that compare the value.

                          bullFlag[0] = true;

                          Should be above 'if (bullFlag[0] && bullFlagCounter > 0)' in the code.



                          "I moved this block before wholel condition starts so it appears on every bar but nothing prints."

                          The condition is not true because bullFlag[0] was never assigned a value of true. Nothing prints because the condition is not evaluating as true.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            my code
                            bullFlag[0] = true;
                            if (isHighSequence && trendDirection == 1 && High[0] > Close[0])
                            {
                            Print(string.Format("{0} | isHighSequence: {1} && trendDirection: {2} == 1 && High[0]: {3} > Close[0]: {4}", Time[0], isHighSequence, trendDirection, High[0], Close[0]));
                            double slope = (High[lookbackPeriod - 1] - High[0]) / lookbackPeriod;
                            trendlineValueDn = (slope * 0) + High[0];
                            if (bullFlag[0] && bullFlagCounter > 0)
                            {
                            Print(string.Format("{0} | bullFlag[0]: {1} && bullFlagCounter: {2} > 0", Time[0], bullFlag[0], bullFlagCounter));
                            }
                            string newLineTagB = "BullFlag" + bullFlagCounter;
                            if (bullFlag[0] && bullFlagCounter > 0)
                            {
                            RemoveDrawObject("BullFlag" + (bullFlagCounter - 1));
                            }
                            Draw.Line(this, newLineTagB, true, lookbackPeriod - 1, High[lookbackPeriod - 1], 0, High[0], BullFlagColor, DashStyleHelper.Solid, 2);
                            // bullFlag[0] = true;
                            bullFlagCounter++;

                            // Alert("Alert", Priority.High, "Down trend line broken", NinjaTrader.Core.Globals.InstallDir + @"\sounds\Reversing.wav", 10, Brushes.Transparent, Brushes.Transparent);
                            }​

                            my output
                            No bullflag10/23/2024 12:12:00 AM
                            No bullflag10/23/2024 12:14:00 AM
                            No bullflag10/23/2024 12:16:00 AM
                            No bullflag10/23/2024 12:18:00 AM
                            No bullflag10/23/2024 12:20:00 AM
                            No bullflag10/23/2024 12:22:00 AM
                            10/23/2024 12:22:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20518 > Close[0]: 20515.75
                            10/23/2024 12:22:00 AM | bullFlag[0]: True && bullFlagCounter: 100 > 0
                            No bullflag10/23/2024 12:24:00 AM
                            10/23/2024 12:24:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20517.5 > Close[0]: 20516.75
                            10/23/2024 12:24:00 AM | bullFlag[0]: True && bullFlagCounter: 101 > 0
                            No bullflag10/23/2024 12:26:00 AM
                            No bullflag10/23/2024 12:28:00 AM
                            No bullflag10/23/2024 12:30:00 AM
                            No bullflag10/23/2024 12:32:00 AM
                            No bullflag10/23/2024 12:34:00 AM
                            No bullflag10/23/2024 12:36:00 AM
                            10/23/2024 12:36:00 AM | isHighSequence: True && trendDirection: 1 == 1 && High[0]: 20518.75 > Close[0]: 20517.75
                            10/23/2024 12:36:00 AM | bullFlag[0]: True && bullFlagCounter: 102 > 0
                            No bullflag10/23/2024 12:38:00 AM
                            No bullflag10/23/2024 12:40:00 AM
                            No bullflag10/23/2024 12:42:00 AM
                            No bullflag10/23/2024 12:44:00 AM​

                            the problem now is it deletes the first drawn line (where red line is now) and draws next on lime line And I dont want that. I want both lines to stay drawn.


                            Click image for larger version

Name:	image.png
Views:	67
Size:	51.6 KB
ID:	1322565

                            Comment


                              #29
                              Hello tkaboris,

                              Where is the output 'No bullflag10/23/2024 12:12:00 AM' coming from?

                              "the problem now is it deletes the first drawn line (where red line is now) and draws next on lime line And I dont want that. I want both lines to stay drawn."

                              If you want all lines to remain, why call RemoveDrawObject?

                              You've moved 'bullFlag[0] = true;' above 'if (isHighSequence && trendDirection == 1 && High[0] > Close[0])' instead of above 'if (bullFlag[0] && bullFlagCounter > 0)' as I suggested.

                              So you want this bool to always be true?
                              Why use a bool if it's always going to be true?


                              The goal from post # 18.

                              "All I want is just to make sure if we are in the bullflag condition and there is object drawn, remove it and draw a new one."

                              I'm not certain why you would remove an object just to draw it back, instead of modifying the existing object to change it's location or properties (such as color).
                              But if this really what you want to do, it sounds like 'if (isHighSequence && trendDirection == 1 && High[0] > Close[0])' is what you are referring to a 'bullflag'.
                              If this condition is true, then remove the object and draw it back. Is this correct?

                              If so, I expect that you would want to set the value of the bool series in the logic block of that condition.

                              If you are checking to see if this bool series has been set to true, I would expect that the condition checks the value of the bool series after you have set it to true.
                              Chelsea B.NinjaTrader Customer Service

                              Comment


                                #30
                                1. i added this condition to print no bull flag before main condition, so thats where its coming from.
                                if (bullFlag[0] && bullFlagCounter > 0)
                                {
                                Print(string.Format("{0} | bullFlag[0]: {1} && bullFlagCounter: {2} > 0", Time[0], bullFlag[0], bullFlagCounter));
                                }
                                if(!bullFlag[0])
                                {
                                Print("No bullflag" + Time[0]);
                                }

                                ​2. I am deleting only first instance of the line. as you can see in post 24, there are 2 lines printed on timem 0.22-0.24. If there are two lines printed i want to delte the first line and only keep the last one in that condition.

                                Below is my old script without series bools for bullFlag boolean. It deletes the first instance and keeps the last one only

                                Click image for larger version

Name:	image.png
Views:	42
Size:	76.7 KB
ID:	1322576

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by NullPointStrategies, Today, 05:17 AM
                                0 responses
                                51 views
                                0 likes
                                Last Post NullPointStrategies  
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                128 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                69 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                42 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                46 views
                                0 likes
                                Last Post TheRealMorford  
                                Working...
                                X