Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using BarsInARow as index?

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

    Using BarsInARow as index?

    I'd like to use a previous up candle to reference an open before a series of bars in a row.
    Is there a way to use the output calculation from BarsInARow to get the bar index of the bar I'm trying to reference?

    Example: (bullish bar = reference bar), (random number of bars) down bars follow, then a bullish bar[1]

    I'd like to be able to get the open of the bullish reference bar and bar[1] for my calculations in the depicted scenario.

    #2
    Hello Conceptzx,

    Thank you for your post.

    I would like to make sure I fully understand your inquiry. If possible, please provide an example and/or a screenshot that points out the kind of bars you are referring to.
    • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
    • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
    ​Based on your description, my understanding is that when there is a bullish (green) bar followed by several bearish (red) bars, and then finally another bullish (green) bar, you want to get the index from the first bullish bar. Is this correct? Are you looking to get the barsAgo reference for that bar or the absolute bar index in the series for that bar? Since you are looking to get the open, it sounds like you will need to get the barsAgo reference, correct?

    I look forward to your clarification.

    Comment


      #3
      Id Like to use the 2 blue candles in this instance. The number or bearish bars between them is not fixed so a regular indexing method isn't viable.

      The most recent blue bar would be indexed as [1] at the time when it closed for my calculation.
      Attached Files
      Last edited by Conceptzx; 10-19-2023, 09:45 AM.

      Comment


        #4
        Hello Conceptzx,

        Thank you for your reply.

        You could consider something like using a bool and saving the CurrentBar - 1 index when a bullish bar followed by a bearish bar is detected. You can get a barsAgo index by subtracting the saved bar index from the CurrentBar index. Or, you could consider saving the Open price of a bullish bar so you don't have to worry as much about the bar indexes - you had mentioned that you'd, "like to be able to get the open of the bullish reference bar and bar[1] for my calculations"
        Are you looking for this comparison between bullish bars only when there is at least one bearish bar between them and not when there are two bullish bars in a row? What are the exact conditions that you are looking to narrow down for particular bar's open prices?

        I appreciate your patience and look forward to assisting you further.

        Comment


          #5
          Is there a sample script for such an operation available?

          Comment


            #6
            Hello Conceptzx,

            Thank you for your reply.

            There is not an existing sample script, though I was asking clarifying questions to try and create a snippet for you to reference. For example, a bullish bar could be detected when the Close is greater than the Open of the bar. You could set a condition that checks for that and saves the open price of the candle, then when another new bullish bar is made you could save that open price and compare the two. You could also save the index for each bar to compare how many bars elapsed between the two. For example:

            Code:
            private double lastBullOpen;
            private int lastBullIndex, barsSinceLastBull;
            protected override void OnBarUpdate()
            {
            // detect a bullish bar
            if (Close[0] > Open[0])
            {
            // print that shows a bullish bar was found and print the open price of this bar
            Print(Time[0] + " Bullish bar found.  Open price: " + Open[0]);
            
            if (lastBullOpen != null && lastBullIndex != null)
            {
            barsSinceLastBull = CurrentBar - lastBullIndex;
            Print(Time[0] + " Current open price: " + Open[0] + " open price of last bullish bar: " + lastBullOpen + " bars since last bull bar: " + barsSinceLastBull);
            // now save the current bar's open to the lastBullOpen and current bar index to lastBullIndex for reference on the next bullish bar
            lastBullOpen = Open[0];
            lastBullIndex = CurrentBar;
            }
            }
            }
            This snippet could be modified in other ways to meet your needs, though the print statements should help to verify that the desired open prices are being printed out. If the open prices match what you are looking for, then you could use them for whatever other calculations you have planned in your script.

            Please let us know if we may be of further assistance.

            Comment


              #7
              Thank you. I'll try to work it into my indicator.

              Comment


                #8
                One last question, how do I reference the open of the last bull bar?

                Open[barsSinceLastBull]?

                Comment


                  #9
                  Originally posted by Conceptzx View Post
                  One last question, how do I reference the open of the last bull bar?

                  Open[barsSinceLastBull]?
                  Yes, that is one way you could do it, though in the snippet it is also already saved to the variable lastBullOpen so you could just use lastBullOpen (without the need for any bar index). I suggest trying both values in a Print() statement to see if they are coming out as expected and match the values you see on the chart. Then you could decide which one you prefer to use.

                  Feel free to reach out with any additional questions or concerns.

                  Comment


                    #10
                    I'm returning a CS0103 error for lastBullOpen.

                    I'm trying to add a couple additional candle pattern definitions to the default CandleStickPatterns indicator, not sure if that changes anything or not as it uses n.Open[x] or similar as the bar definition. I understand if you must decline to assist me here as this was a locked file.

                    I've successfully added a couple new patterns but trying to use a variable as a bar index seems to be giving me a degree or difficulty.

                    Comment


                      #11
                      Originally posted by Conceptzx View Post
                      I'm returning a CS0103 error for lastBullOpen.

                      I'm trying to add a couple additional candle pattern definitions to the default CandleStickPatterns indicator, not sure if that changes anything or not as it uses n.Open[x] or similar as the bar definition. I understand if you must decline to assist me here as this was a locked file.

                      I've successfully added a couple new patterns but trying to use a variable as a bar index seems to be giving me a degree or difficulty.
                      Have you created the lastBullOpen object at the class level of the script before OnStateChange()? For example:

                      Code:
                      public class YourIndicatorName: Indicator
                      {​
                      private double lastBullOpen;
                      You mentioned, "trying to use a variable as a bar index seems to be giving me a degree or difficulty" - if you are still having difficulties with this, please provide a snippet of your code to demonstrate what you have tried along with an explanation of what you are attempting to achieve and we might be able to offer additional suggestions or resources.

                      I appreciate your time and patience.

                      Comment


                        #12
                        Code:
                            public class CandlestickPattern : Indicator
                            {
                                private Brush                        downBrush                = Brushes.DimGray;
                                private CandleStickPatternLogic        logic;
                                private int                         numPatternsFound;
                                private TextPosition                textBoxPosition            = TextPosition.BottomRight;
                                private Brush                        textBrush                = Brushes.DimGray;
                                private Brush                        upBrush                    = Brushes.DimGray;
                                private int UpBarCount                                         = 0;
                                private int DwnBarCount                                     = 0;
                                public Series<double> UpBars;
                                public Series<double> DwnBars;
                                private double lastBullOpen;
                                private double lastBullHigh;
                                private double lastBullLow;
                                private double lastBullClose;
                                private double lastBearOpen;
                                private double lastBearHigh;
                                private double lastBearLow;
                                private double lastBearClose;
                                private int lastBullIndex,             barsSinceLastBull;
                                private int lastBearIndex,             barsSinceLastBear;
                                private NinjaTrader.NinjaScript.Indicators.ConceptzX_Suite.CX_True_Range         tr;
                                private NinjaTrader.NinjaScript.Indicators.ConceptzX_Suite.CX_Closing_Range        cr;​

                        Code:
                                    if (Close[0] > Open[0])
                                    {
                                        // print that shows a bullish bar was found and print the open price of this bar
                                        Print(Time[0] + " Bullish bar found.  Open price: " + Open[0]);
                        
                                        if (lastBullOpen != null && lastBullIndex != null)
                                        {
                                            barsSinceLastBull = CurrentBar - lastBullIndex;
                                            Print(Time[0] + " Current open price: " + Open[0] + " open price of last bullish bar: " + lastBullOpen + " bars since last bull bar: " + barsSinceLastBull);
                                            // now save the current bar's open to the lastBullOpen and current bar index to lastBullIndex for reference on the next bullish bar
                                        lastBullOpen     = Open[0];
                                        lastBullHigh     = High[0];
                                        lastBullLow        = Low[0];
                                        lastBullClose    = Close[0];
                                        lastBullIndex = CurrentBar;
                                        }
                                    }
                                    if (Close[0] < Open[0])
                                    {
                                        Print(Time[0] + " Bearish bar found.  Open price: " + Open[0]);
                        
                                        if (lastBearOpen != null && lastBearIndex != null)
                                        {
                                            barsSinceLastBear = CurrentBar - lastBearIndex;
                                            Print(Time[0] + " Current open price: " + Open[0] + " open price of last bearish bar: " + lastBearOpen + " bars since last bear bar: " + barsSinceLastBear);
                                        lastBearOpen     = Open[0];
                                        lastBearHigh     = High[0];
                                        lastBearLow        = Low[0];
                                        lastBearClose    = Close[0];
                                        lastBearIndex = CurrentBar;
                                        }​

                        Code:
                        case ChartPattern.BullishOrderBlock2:     found = (lastBullOpen > n.Open[2]); break;
                        .


                        At this time, I'm working on basic functionality so I have not gotten too technical with my calculations as I debug what I have now I did make the additions at the Class level.
                        I'm just trying to get the Last Bull Candle part of the code working at this time. I've tried a few different ways to define the last bull candle, but nothing has worked so far.​

                        Comment


                          #13
                          Hello Conceptzx,

                          Thank you for your reply.

                          You said, "I've tried a few different ways to define the last bull candle, but nothing has worked so far.​" Which part of the code are you referring to and what do you mean by "define" the last bull candle? Does this have to do with the ChartPattern being ChartPattern.BullishOrderBlock2? I don't think I am following exactly what part of your logic you have a question about.

                          I look forward to your clarification.

                          Comment


                            #14
                            I've tried Open[lastBullOpen], lastBullindex, lastBullOpen, and n.Open[lastBullIndex], etc. Trying to define the first of the two blue candles for the bar calculation and it's always returned a cs0103.

                            I'm trying to get line 759 to work at this time. It's currently disabled in the cs file so it could be in a compiled state. .

                            (I have a couple different additions I'm working on so you'll see some things disabled and others active and unfinished.)
                            Attached Files
                            Last edited by Conceptzx; 10-20-2023, 05:48 AM. Reason: Updated the file

                            Comment


                              #15
                              Hello Conceptzx,

                              Thank you for your reply.

                              To clarify, in my snippet the value saved to lastBullOpen was the actual open price of the last bullish bar:
                              lastBullOpen = Open[0];

                              This means there is no need to use that as an index. I see you are also using that in your logic, which means you could even just print the value of lastBullOpen and it will already be the open price from that bar on which the value was saved.

                              As for lastBullIndex, that is an actual bar index from left to right on the chart, and not a barsAgo index for how many bars back. If you use an absolute bar index for a barsAgo reference, you could run into errors. To get the bars ago for the last bullish bar, it would be CurrentBar - lastBullIndex. We have more details regarding referencing the correct bar here:


                              Are you currently editing the existing CandleStickPattern indicator that comes with NinjaTrader, or have you created a copy of this script that you are modifying? The script you have provided has the same name and I am not able to move it into my own indicators folder without receiving several errors. If you have not already, please right-click the script and select Save As then give the script a new name such as CandleStickPatternModified in order to save the script into its own new file and prevent errors related to the modification of a system script.

                              I appreciate your time and patience.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              576 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              334 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              553 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X