Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Compare current bar to X bars back

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

    Compare current bar to X bars back

    Hi,
    I use UniRenko to trade trends based on comparing current bar close to previous bar close and have some logic for Stop and Reverse. Right now, I am able to enter trades by comparing Default Input[0] > Default Input[1]

    I can't find an easy way to allow comparing current bar to X bar back. For example, upon reverse, I want to make sure that Close[0] > Close [ConfirmationBars]. I tried to use offset, but "bars ago" doesn't take input, just takes an integer value.

    #2
    Hello mgalal,

    Can you provide a sample of what you had tried in code? What you explained would be correct, you could use Close[VariableNameHere] to use a BarsAgo on that series. The variable needs to be an int.



    I look forward to being of further assistance.

    Comment


      #3
      Here's a couple of screen of what's in progress (see first line in Condition Group Editor). Before I proceed, I wanted to confirm that this is the correct approach. The input name is call "ConfirmationBars", type in, default to 0.
      Attached Files

      Comment


        #4
        Originally posted by mgalal View Post
        I can't find an easy way to allow comparing current bar to X bar back. For example, upon reverse, I want to make sure that Close[0] > Close [ConfirmationBars]. I tried to use offset, but "bars ago" doesn't take input, just takes an integer value.
        I don't use the Strategy Builder, so let's just address the concepts at play here.

        NinjaTrader defines "BarsAgo index" as the value used to access elements in a
        Series, such as Time[0] or High[6] or Close[ConfirmationBars] -- all the values
        in red are known as "BarsAgo indexes".

        Series != Array
        They may look the same when accessing elements, but they are not the same.

        A "BarsAgo index" looks exactly like an array index, but when '[0]' or '[1]', etc,
        is appended to the end of a Series, your mind has to switch gears and say
        "that's not an array index, it's a BarsAgo index".

        When you say 'X bars back' you're precisely talking about a BarsAgo index.
        In fact, that's where the name comes from: you want to look at the Close value
        from 5 bars ago? No problem -- just use Close[5] -- '5' is the 'BarsAgo index'.

        [NinjaTrader had to call it something, so they invented a name out of thin air.
        I'm serious -- a 'BarsAgo index' is not a C# thing -- it is a NinjaScript thing.]

        A "BarsAgo index" is just a number (technically, a non-negative int) and you can
        use either a constant or a variable. Constant values, like in Close[0] or Close[1],
        are pretty common.

        And, yes, you are exactly correct. A 'BarsAgo index' does not take an input value.
        How can it? It's just an integer value.

        [EDIT: Yes, the edit box for 'Bars ago' in Strategy Builder does take an input value,
        but that's more a function of the Strategy Builder, not the 'BarsAgo index' concept.
        My apologies if I've completely overblown any issues in the original question.]

        Make sense?

        Last edited by bltdavid; 08-04-2021, 01:49 PM.

        Comment


          #5
          Bltdavid, thank you. I just want to use strategy builder for now. Hope I find the answer. I am using UniRenko and want to avoid entering a trade until an X number of bars have formed after a reversal.

          let’s say I want to only enter after 3 bars after reversal I would want to compare

          Default Input [0] > Default Input [3]
          and
          Open [4] < close [4]

          Comment


            #6
            Hello mgalal,

            To count in the builder is a more difficult concept and requires using a series. You can find an example of that concept in the following post. What you described could be achieved by the same general concept. In the reversal condition you could set a bool variable to true to note that the counter should be started. Each bar after that could be counted and once a certain count was reached you could perform an action.

            Is there a way to increment an int in the strategy builder? When I go to set an int equal to itself there's no way to add 1 to it as well. Just wondering if this is possible in the wizard.


            Please let me know if I may be of further assistance.

            Comment


              #7
              Hi Jesse,
              I have been trying to translate the example from bar count to trades on specific condition (first bar close in uptrend, count exits, stop when max trade variable is met), do it again on downtrend.. The example use bar numbers to increment the series, when I want to count trades regardless of bar numbers. Could you help me?

              Comment


                #8
                Hello mgalal,

                The example I linked to shows the general concept of how you would count something in the builder so you could use that as a guide to setting up a series in your strategy. Once you have a series like the example shows then you could add to the series value when a condition is true. That sample has a crossover condition which increments the series each time the cross happens.

                In your description you would replace the crossover condition with your first bar close in uptrend condition. You could also add that same series code into sets for the exits to count when that condition is true as well.

                You can reference that series in other sets to get its value to form conditions like when to stop.

                You would otherwise need to be more specific here on what you need help with in relation to the sample or what you are trying. While I won't be able to generate the strategy for you I can assist with describing concepts or linking to samples which relate to what you are doing. If you can provide screenshots of what you are trying with your questions that can also help.

                Please let me know if I may be of further assistance.

                Comment


                  #9
                  Hi Jesse,
                  Thanks for the reply. I found two examples and I am not clear how to use them yet. Let me be more specific.

                  First, the series is set to 0 on first bar on the chart, which reset the counter, but incrementing the counter is done seemingly using bar counting. In the cross over example, every new bar formed increments the counter by 1, until condition is met to see if bars since entry condition is equal 10. This comparison is done for the crossover to test for 10 bars after crossover 1 bar back. It uses two series' to that comparison. Another example, uses 1 series only to as a counter. I am not clear on the difference in using 2 vs 1 series. Another difference is that the cross over example, incrementing is done using the expression myCounter[0] = (myCounter[0]+1) and in the 1 series example, it increments using 1 bar back value myCounter[0] = (myCounter[1]+1). I am not sure what's the difference here or why.

                  Besides that:
                  - I am not certain how to turn the counter into a trade counter instead of a bar counter. A trade may have 10 bars in 1 entry/exit combo, and another may be 20 bars in an entry/exit combo. I am not sure how to address the difference for that. Another is, I always want to reset the counter because my entry condition will check if myCounter[0] >= MaxTradeCount variable being true, don't enter a trade. If isReverse=true, reset counter, enter trades, and count trades..

                  Simply put, I am not sure how to count trades, not bars using the examples, and I am not sure how to reset the counter on specific conditions (Is it to simply set myCounter[0]=0 when isReverse=true like setting myCounter[0]=0 on first bar? Hope this adds color to my design question.
                  Last edited by mgalal; 09-29-2021, 05:06 PM.

                  Comment


                    #10
                    Jesse, I got the counter to work for trade, but now I need to figure out the reset approach. Can you explain to me what's the purpose of myCounter[0]=myCounter[1] is in the second tab?

                    Basically, imagine in the screenshot below, then when UniRenko changes direction (reverse), I want to reset the counter so that trade #33 is actually trade #1.

                    Click image for larger version  Name:	Screen Shot 2021-09-29 at 10.24.41 PM.png Views:	0 Size:	871.9 KB ID:	1172986
                    Last edited by mgalal; 09-30-2021, 08:16 AM.

                    Comment


                      #11
                      Hello mgalal,

                      The code: myCounter[0]=myCounter[1]
                      This persists the series value to the next bar. That would be for example if you set the value to 1 then it would stay as 1 on the next bar.

                      If you wanted to reset the counter you would need to add an action which sets the myCounter series to 0 during that condition. The above code would persist the 0 to the next bar.

                      ​​​​​​​

                      Please let me know if I may be of further assistance.


                      Comment


                        #12
                        Hey Jesse, I tried multiple ways to reset mycounter, I even put it in my logic for FirstBarOfSession and still counts all trades on a chart whether they are 5 days or 2 or 30. Any ideas on how to diagnose the issue? Print/log value after specific tabs? Usually I find that my print/log are verbose.

                        Comment


                          #13
                          Hello mgalal,

                          You can try printing the value in each of the sets to see the order your logic is executing. Aside from using prints I would likely need to see what you modified to better understand what may be happening.

                          Please let me know if I may be of further assistance.

                          Comment


                            #14
                            I'll try the print/set. Otherwise, do I just attach the strategy here?

                            Comment


                              #15
                              Hello mgalal,

                              Yes you can attach the strategy here. If you do please make sure to point out what change you made and what the expectation/observed result are so that we can better assist.

                              I look forward to being of further assistance.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, 03-13-2026, 05:17 AM
                              0 responses
                              93 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              152 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              80 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              53 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              65 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X