Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate on Bar Close == false... need help == true

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

    Calculate on Bar Close == false... need help == true

    Hello All !

    I'm still hard at work on this new strategy. I've made a few changes and I've run into something I'm not quite sure how to handle.

    In my strategy, there is a scenario where the bar closes above the upper band of a moving average envelope, and at that point, I would like for the strategy to play a sound. At the CLOSE of that bar. Not in the middle... or when it touches the band... but at the instant the bar closes ABOVE the band.

    Okay... that's the first scenario... here's the second:

    There is also a time when price opens above the upper band of the moving average envelope. In that case, I would like for the strategy to draw a dot. When the open is ABOVE the envelope's upper band.

    Now... to make things more interesting... I want the scenario to be "calculate on bar close == false" because I want the dot to be drawn the INSTANT the bar opens above the upper band... and NOT after that bar has closed...

    There are two things happening which are making me a bit crazy...

    First... instead of the strategy playing the sound at the CLOSE of the bar... it is playing the sound over and over again as the bar bumps up against the upper band on its way to closing above it. In other words, instead of it playing the sound once at the close of the bar... it is playing it over and over again once the bar touches the upper band.

    Even though I have the strategy set to calculate intra-bar... doesn't it know the difference between a bar in progress and a bar that has closed?

    Code:
    If (Close[0] > MAEnvelopes(blah, blah,).Upper[0]
    {
    PlaySound(blah, blah);
    }
    Doesn't the strategy know when it has closed the current bar? Why is it playing the sound over and over again intra-bar when I've instructed it to play the sound at the close of the bar? I understand that it is calculating intra-bar... but the close only happens once... it's not "closing" over and over again. Right?

    Now on to the second problem I'm having...

    I have the strategy set to draw a dot when the bar opens above the upper band.

    Code:
    if (Open[0] > MAEnvelopes(blah, blah).Upper[0])
    {
    DrawDot(blah, blah);
    }
    Well... it's drawing dots even on bars which open just below the upper band. In fact, some of the bars are opening two ticks below the upper band and the strategy is still drawing a dot there. However... if I set the strategy to "calculate on bar close == true..." it doesn't draw the dot unless the opening was INDEED above the upper band. Huh? Why does having intra-bar calculations change the result of the equation? Either the bar opened above the upper band or it didn't. How does having intra-bar calculations change that outcome?

    As always, I truly enjoy and benefit greatly from your responses, and I thank you in advance for your help on this.

    -V

    #2
    Hello V,

    Thank you for your post.

    Using the following code you should not have an issue. The reason the Close[0] keeps playing the sound is the Close[0] is the last price with CalculateOnBarClose = false, so the Close[0] is always updating. So with the code below I utilize if(FirstTickOfBar), allowing me to check that the tick is the first tick of the bar. I then check Close[1] as this is the most recently closed bar and I check Open[0] as that is the current bar's open.
    Code:
    			if(FirstTickOfBar)
    			{
    				if(Close[1] > MAEnvelopes(0.1, 1, 12).Upper[0])
    				{
    					PlaySound("Alert1.wav");
    				}
    				
    				if(Open[0] > MAEnvelopes(0.1, 1, 12).Upper[0])
    				{
    					DrawDot("OpenDot"+CurrentBar, true, 0, Open[0], Color.Black);
    				}
    			}
    For information on FirstTickOfBar please visit the following link: http://www.ninjatrader.com/support/h...ttickofbar.htm

    Please let me know if you have any questions.

    Comment


      #3
      Originally posted by Aurum View Post
      Hello All !

      I'm still hard at work on this new strategy. I've made a few changes and I've run into something I'm not quite sure how to handle.

      In my strategy, there is a scenario where the bar closes above the upper band of a moving average envelope, and at that point, I would like for the strategy to play a sound. At the CLOSE of that bar. Not in the middle... or when it touches the band... but at the instant the bar closes ABOVE the band.

      Okay... that's the first scenario... here's the second:

      There is also a time when price opens above the upper band of the moving average envelope. In that case, I would like for the strategy to draw a dot. When the open is ABOVE the envelope's upper band.

      Now... to make things more interesting... I want the scenario to be "calculate on bar close == false" because I want the dot to be drawn the INSTANT the bar opens above the upper band... and NOT after that bar has closed...

      There are two things happening which are making me a bit crazy...

      First... instead of the strategy playing the sound at the CLOSE of the bar... it is playing the sound over and over again as the bar bumps up against the upper band on its way to closing above it. In other words, instead of it playing the sound once at the close of the bar... it is playing it over and over again once the bar touches the upper band.

      Even though I have the strategy set to calculate intra-bar... doesn't it know the difference between a bar in progress and a bar that has closed?

      Code:
      If (Close[0] > MAEnvelopes(blah, blah,).Upper[0]
      {
      PlaySound(blah, blah);
      }
      Doesn't the strategy know when it has closed the current bar? Why is it playing the sound over and over again intra-bar when I've instructed it to play the sound at the close of the bar? I understand that it is calculating intra-bar... but the close only happens once... it's not "closing" over and over again. Right?

      Now on to the second problem I'm having...

      I have the strategy set to draw a dot when the bar opens above the upper band.

      Code:
      if (Open[0] > MAEnvelopes(blah, blah).Upper[0])
      {
      DrawDot(blah, blah);
      }
      Well... it's drawing dots even on bars which open just below the upper band. In fact, some of the bars are opening two ticks below the upper band and the strategy is still drawing a dot there. However... if I set the strategy to "calculate on bar close == true..." it doesn't draw the dot unless the opening was INDEED above the upper band. Huh? Why does having intra-bar calculations change the result of the equation? Either the bar opened above the upper band or it didn't. How does having intra-bar calculations change that outcome?

      As always, I truly enjoy and benefit greatly from your responses, and I thank you in advance for your help on this.

      -V
      Is this from the Strategy Wizard?

      Comment


        #4
        Hi Koganam, Thank you for your response. Yes... it's generated by the wizard. I just made a code-only version and tested it, but I'm still getting the same results as before regarding the "open above the moving average envelope". I'll address it in a separate post and include a screenshot there (in reply to Patrick's post).

        Thank you again. Your posts are always appreciated.

        -V

        Comment


          #5
          Hello PatrickH, and thank you so much for your fast reply.

          The sound is now playing as anticipated. But, I'm still having a problem with the open above the MA upper band.

          I've included annotated screen captures, but in a nutshell, what is happening, is that the bar opens at a position which appears to be above the upper band. The price markers on the right of the chart confirm this. The price marker for the bar is well above the price marker for the upper band. In other words, when the bar opened, at that instant, the opening price was above the upper band.

          BUT...

          Once the bar closes (I'm using 10 range bars)... the open appears BELOW the upper band. And, when you middle click over the bar, you can see that the opening price is as expected, but the upper band calculation is now much higher than it was at the open of the bar. In other words, instead of it leaving the band where it was at the open of the bar, it moved the band higher by a couple of ticks, causing the open to now be below the band. This is a MAJOR problem, since orders will be placed based on this positioning.

          Further... if I set calculate on bar close to "true," this doesn't happen.

          Here's my concern: the MA envelope is in a certain position when the bar opens. It's a mathematical certainty and the position should be fixed at that instant in time. Something is happening in the intra-bar calculations which is different from the calculations for "bar close." But, they should both be the same number. When that bar opened, the MA upper band was in a specific numerical location. Either the bar opened above the MA upper band, or it didn't. It can't be both ways. No matter how it is calculated, if the bar opened above the band, that should be reflected after the bar closes. Instead, what is happening is that the math is being "readjusted" after the bar closes, which is changing the result of the equation. Not good.

          Again... if I set "calculate" to true... it never sees that bar open above the upper band, and therefore, doesn't draw a dot above the bar. If I set it to "false," it sees that bar open above the band and draws a dot immediately. Then, once the bar closes, it resets the upper band back above the open of that bar (matching the result achieved by setting "calculate" to true). I need to be able to track the open of a bar in real time, and expect that it will give me the correct result.

          Anyway... I need for the strategy to see when a bar REALLY opens above the upper band, in real time. What's happening now, is that the bar didn't really open above the band... but the strategy somehow thinks it did. (unless I set calculate to "true," which defeats the purpose of the strategy, which is to capture an open above the upper band at the instant it happens).

          I'm hoping I've been clear in my explanation. Thanks again for your assistance. Please see the attached screen captures (the same thing happened on the subsequent bar also).

          -V
          Attached Files
          Last edited by Aurum; 08-17-2014, 01:24 PM.

          Comment


            #6
            Originally posted by Aurum View Post
            ...
            I've included annotated screen captures, but in a nutshell, what is happening, is that the bar opens at a position which appears to be above the upper band. The price markers on the right of the chart confirm this. The price marker for the bar is well above the price marker for the upper band. In other words, when the bar opened, at that instant, the opening price was above the upper band.

            BUT...

            Once the bar closes (I'm using 10 range bars)... the open appears BELOW the upper band. And, when you middle click over the bar, you can see that the opening price is as expected, but the upper band calculation is now much higher than it was at the open of the bar. In other words, instead of it leaving the band where it was at the open of the bar, it moved the band higher by a couple of ticks, causing the open to now be below the band. This is a MAJOR problem, since orders will be placed based on this positioning.

            Further... if I set calculate on bar close to "true," this doesn't happen.

            Here's my concern: the MA envelope is in a certain position when the bar opens. It's a mathematical certainty and the position should be fixed at that instant in time. Something is happening in the intra-bar calculations which is different from the calculations for "bar close." But, they should both be the same number. When that bar opened, the MA upper band was in a specific numerical location. Either the bar opened above the MA upper band, or it didn't. It can't be both ways. No matter how it is calculated, if the bar opened above the band, that should be reflected after the bar closes. Instead, what is happening is that the math is being "readjusted" after the bar closes, which is changing the result of the equation. Not good.

            Again... if I set "calculate" to true... it never sees that bar open above the upper band, and therefore, doesn't draw a dot above the bar. If I set it to "false," it sees that bar open above the band and draws a dot immediately. Then, once the bar closes, it resets the upper band back above the open of that bar (matching the result achieved by setting "calculate" to true). I need to be able to track the open of a bar in real time, and expect that it will give me the correct result.

            Anyway... I need for the strategy to see when a bar REALLY opens above the upper band, in real time. What's happening now, is that the bar didn't really open above the band... but the strategy somehow thinks it did. (unless I set calculate to "true," which defeats the purpose of the strategy, which is to capture an open above the upper band at the instant it happens).

            I'm hoping I've been clear in my explanation. Thanks again for your assistance. Please see the attached screen captures (the same thing happened on the subsequent bar also).

            -V
            Your observations are correct; your reasoning, not quite.

            I am reasonably certain that you are calculating your envelope based on the close of the bar. At the open, the close of the bar is the same as the open, (because you have COBC = false), and your envelope is drawn, however it is, based on that value. In the particular case that you describe using 10-Range bars, by the close of the bar, the close may be anything up to 10 ticks higher than the open, and any calculation based on that close MUST necessarily be different from what was calculated based on the earlier close (when it was equal to the open).

            IOW, you must take into account the numbers that you are crunching, not just assume that they will do what you want them to do. The code is doing what you asked it to do, not arbitrarily readjusting anything. If you want your code to behave differently, you will have to code it to do so.
            Last edited by koganam; 08-17-2014, 06:45 PM.

            Comment


              #7
              Hello again Koganam, and thank you for your reply.

              Yes... when I open the condition window in the strategy wizard, the "input series" is "default input," which I imagine is the close of the bar.

              Maybe you or PatrickH can assist me in finding the correct selection to achieve the result I seek. I'm trying to get an accurate reading of the bar opening above the envelope. Apparently, when the MA envelope is set to "default input" it is waiting until the close of the bar to calculate where it was at the open of the bar (weird).

              Of the several settings available (i.e. Close, High, Low, Median, Open, Typical, etc.), which would be the correct setting to get a consistent result so that the open and the close give me the same number? In other words, what selection should I make so that wherever the bar is in relation to the band at the open of the bar, that relationship doesn't change after the bar has closed. If the bar opens 2 ticks above the upper band, how can I set it up so that it is still 2 ticks above the band after the bar closes? Is this even possible?

              Is there a way to set the calculation of the MA envelope so that it sits stationary while the bar forms around it?

              Using your statement about "asking" the code... how do I "ask" the code to do what I'm... well... asking...

              -V
              Last edited by Aurum; 08-17-2014, 03:41 PM.

              Comment


                #8
                Originally posted by Aurum View Post
                ...

                Maybe you or PatrickH can assist me in finding the correct selection to achieve the result I seek. I'm trying to get an accurate reading of the bar opening above the envelope. Apparently, when the MA envelope is set to "default input" it is waiting until the close of the bar to calculate where it was at the open of the bar (weird).
                There is nothing weird about it. In calculating the envelope, you are asking to calculate an arithmetic quantity from a dataset where one value is a changeling. That means that your result is not invariant. The only time when that result becomes an invariant is when the set of input values ceases to change: which is what occurs at the close of the bar.
                Of the several settings available (i.e. Close, High, Low, Median, Open, Typical, etc.), which would be the correct setting to get a consistent result so that the open and the close give me the same number? In other words, what selection should I make so that wherever the bar is in relation to the band at the open of the bar, that relationship doesn't change after the bar has closed. If the bar opens 2 ticks above the upper band, how can I set it up so that it is still 2 ticks above the band after the bar closes? Is this even possible?
                That is impossible in an absolute arithmetic sense, except in the unique case where the open is equal to the close of the bar.

                There is no way arithmetically to calculate the mean value of 2 datasets, and get the same value if the datasets have only one value that is uncommon.

                Is there a way to set the calculation of the MA envelope so that it sits stationary while the bar forms around it?
                Not if that value depends on the close of the bar, which, after all, is not an invariant, as COBC is false.
                Last edited by koganam; 08-18-2014, 08:47 AM.

                Comment


                  #9
                  Hello Koganam (and PatrickH if you're still with us)...

                  So, are we saying that in NinjaTrader, it is not possible to calculate accurately where the bar opens in relation to a moving average, until that bar closes? In other words, if I want to enter a trade when the bar opens above a moving average, I have to wait until that bar closes before NinjaTrader can give me an accurate picture of where the bar opened in relation to the moving average? This doesn't seem to make any sense. In my example, using 10 range bars, I have to wait until the bar has completed its entire 10 range spread and has actually closed, before I can use the "open" of the bar as an entry or exit signal? By that time, my entry opportunity is long gone (potentially 10 ticks away), and I am potentially in a very bad place for an entry.

                  I really need to understand this because I'm spending loads of time creating trade logic which, it would appear, is useless.

                  My trade logic is pretty simple, and I'm surprised NinjaTrader can't accomplish it. Here is my trade logic:

                  "When the bar opens above the MA envelope, enter a trade." I can't enter a trade accurately if NinjaTrader doesn't know where the bar opened until after the bar closes. There must be a way to accurately determine where the bar opens in relation to the moving average band.

                  How then, can one use intra-bar data for entries and exits? In other words, how can a trader use logic such as: "when the bar opens above the moving average" as an entry or exit signal? Are we confined to only using the close of a bar? If that's the case, then trading with range bars (especially 10 range bars) becomes very limited. In fact, trading with any longer time-frame becomes very limited, since by the time that bar closes it is VERY far from the entry point. If a trader can't use the data from the "open" of a bar to signal entries and exits at the open of that bar, it kind of defeats the purpose of having an automated strategy. Right?

                  I'm hoping PatrackH will jump in on this also, or someone from NinjaTrader.

                  I'm trying to create a strategy which opens a trade when the 10 range bar opens above the moving average envelope band. Obviously, this needs to be an accurate calculation. If the calculation is not accurate, it could actually have me trading on bars which didn't "really" open above the band, which, in my scenario, would be catastrophic in a live trading environment. Are we now saying this is not possible in NinjaTrader? If it IS possible, I'm just asking how.

                  Thank you again for your assistance. Please continue to remain patient as I try and get my head around this. I'm spending a LOT of time designing a strategy which seems to not be possible at the end of it all. I just don't want to waste a bunch of time designing something which is impossible on this particular trading platform.

                  Again... thank you!

                  -V

                  Comment


                    #10
                    Originally posted by Aurum View Post
                    Hello Koganam (and PatrickH if you're still with us)...

                    So, are we saying that in NinjaTrader, it is not possible to calculate accurately where the bar opens in relation to a moving average, until that bar closes? In other words, if I want to enter a trade when the bar opens above a moving average, I have to wait until that bar closes before NinjaTrader can give me an accurate picture of where the bar opened in relation to the moving average? This doesn't seem to make any sense. In my example, using 10 range bars, I have to wait until the bar has completed its entire 10 range spread and has actually closed, before I can use the "open" of the bar as an entry or exit signal? By that time, my entry opportunity is long gone (potentially 10 ticks away), and I am potentially in a very bad place for an entry.

                    I really need to understand this because I'm spending loads of time creating trade logic which, it would appear, is useless.

                    My trade logic is pretty simple, and I'm surprised NinjaTrader can't accomplish it. Here is my trade logic:

                    "When the bar opens above the MA envelope, enter a trade." I can't enter a trade accurately if NinjaTrader doesn't know where the bar opened until after the bar closes. There must be a way to accurately determine where the bar opens in relation to the moving average band.

                    How then, can one use intra-bar data for entries and exits? In other words, how can a trader use logic such as: "when the bar opens above the moving average" as an entry or exit signal? Are we confined to only using the close of a bar? If that's the case, then trading with range bars (especially 10 range bars) becomes very limited. In fact, trading with any longer time-frame becomes very limited, since by the time that bar closes it is VERY far from the entry point. If a trader can't use the data from the "open" of a bar to signal entries and exits at the open of that bar, it kind of defeats the purpose of having an automated strategy. Right?

                    I'm hoping PatrackH will jump in on this also, or someone from NinjaTrader.

                    I'm trying to create a strategy which opens a trade when the 10 range bar opens above the moving average envelope band. Obviously, this needs to be an accurate calculation. If the calculation is not accurate, it could actually have me trading on bars which didn't "really" open above the band, which, in my scenario, would be catastrophic in a live trading environment. Are we now saying this is not possible in NinjaTrader? If it IS possible, I'm just asking how.

                    Thank you again for your assistance. Please continue to remain patient as I try and get my head around this. I'm spending a LOT of time designing a strategy which seems to not be possible at the end of it all. I just don't want to waste a bunch of time designing something which is impossible on this particular trading platform.

                    Again... thank you!

                    -V
                    That is not what I am saying.

                    I am saying that arithmetic is arithmetic, and no amount of wishful thinking will change it.

                    You are analyzing a dynamic situation. At the open of the bar, you have a set of data, which is very unlikely to be the same data at the close of the bar, That means that regardless who is making the trade, a person or a mechanical device, a decision has to be made with no knowledge of the future. If we had perfect knowledge of the future, we would all be perfect traders, who would never make a loss.

                    So you can make a decision to enter early at the open of the bar, at which time, the open may, be accurately calculated, as being above the band because the band is being calculated with the data then available. You are taking the chance then that the data at the end of the bar would show that the open was not above the band, when the bar closed, a result of the fluidity of prices. The position of the band depends entirely on the data with which it is calculated at the time that it is calculated. If that data changes, which in this case, it must, then that change will be reflected in the calculation. The alternative it to wait for confirmation at the bar close, in which case, yes, the trade may well have been too far developed to enter safely. IOW, whether it is the computer or you, the decision as to when to enter the trade still has to be made, with the concomitant danger, that the trade may be entered too early or too late.

                    This is the same decision that you have to make when you are manually watching a chart and taking trades. That you have now delegated that function to a computer does not mean that the market dynamics, or the reality of arithmetic are going to change, just because we wish it so.

                    The values calculated at any one time have NOTHING to do with values calculated at any other time, except of course that the data in this case, actually has a big overlap. What values one gets from any calculations, would depend on the data set: in this particular case, mostly how largely a price delta affects the calculation of the mean. To give you a crude approximation, that is the same reason that a longer moving average is less jagged than a faster moving average: the net effect of the latest value, or its variation, is much less because that effect is being spread over a larger number of calculation points.

                    If you are wishing to remove all uncertainty, you are seeking a formula, not a decision. It also unfortunately suggests that you are probably still in the "Holy Grail seeking" stage. ref: https://www.bigmiketrading.com/psych...html#post21212

                    Decisions are made with imperfect data: a formula, because the data is perfect, can have only one outcome. Deal with the reality of the data, not how you wish it to be be. If the data changes, any juxtaposition of that data relative to another datum, is likely to change. That is a matter of logic, with no need to understand higher mathematical concepts.
                    Last edited by koganam; 08-18-2014, 01:11 AM.

                    Comment


                      #11
                      Excellent post, Koganam! As expected. Excellent.

                      Your explanation is very helpful. In fact... it sparked even more creativity and brought a large degree of clarity to this situation. Instead of calculating the open of the bar above the band, what I will do is calculate the open of the bar based on the close of the PREVIOUS bar. In other words, if the previous bar closed above the band... and the current bar >= previous bar... then logically, it is above the band (or at least there is a very high likelihood). Done.

                      Definitely not in the "holy grail" seeking stage. Just trying to understand how to accomplish one of the steps in the strategy using the calculation power (and the precision) of the strategy. I've noticed that the computer's entry performance FAR outclasses mine.

                      Thank you again for your very, very great reply! If I create something profitable... I will give you a peek at it as a way of saying thanks for all your help!

                      Off to test the new theory.

                      -V

                      Comment


                        #12
                        Use a secondary Tick data series?

                        Have you tried going back to COBC = true and using a secondary data series?

                        Inside Initialize() you'd add:

                        Code:
                          Add(PeriodType.Tick, 1);  // BIP == 1
                        In OnBarUpdate() you could do something like:

                        Code:
                                private int CurrentTick     = 0;
                                private int TickCurrentBar  = -1;
                                private int SaveCurrentBar  = -1;
                        
                                protected override void OnBarUpdate()
                                {
                                    if (BarsInProgress == 1)
                                    {
                                        ++CurrentTick;
                                        TickCurrentBar = CurrentBars[0];
                                        Strategy_OnTickUpdate();   // <-- every tick logic (regardless of COBC)
                                    }
                                    else if (BarsInProgress == 0)
                                    {
                                        if (SaveCurrentBar != CurrentBars[0])
                                        {
                                            CurrentTick = 0;
                                            Strategy_OnEndOfBarUpdate();   // <-- your COBC=true logic goes here
                                            SaveCurrentBar = CurrentBars[0];
                                        }
                                        else
                                            Strategy_OnIntraBarUpdate();  // <-- your COBC=false logic goes here
                                    }
                                }
                        The secondary data series is based upon every tick, so Strategy_OnTickUpdate() is called for every tick as the range bar is being built.

                        The idea is that you'd check for (CurrentTick == 0) inside Strategy_OnTickUpdate() and when this is true you'd do your logic using whatever price you want.

                        This is similar to FirstTickOfBar logic that Patrick was talking about.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        633 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        364 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        105 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        567 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        568 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X