Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator not calculating correctly

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

    Indicator not calculating correctly

    Hi,

    I'm using the indicator Swing(5).SwingHigh[1] in my strategy. Here is the code that I'm using to output the indicator:

    Print("Current Swing High(5) is " + Swing(5).SwingHigh[1].ToString() + " at " + Time[0].ToString());

    I'm running this indicator under this routine:

    if (FirstTickOfBar)

    However, as soon as the Swing High is breached, I'm getting a bad value. See attached. How is this indicator supposed to be calculated in this instance. I would have thought it would return null.

    Thanks,

    Rich
    Attached Files

    #2
    Rich, I suggest you plot the Swing High / Low methods to get to know what the indicator would return as value in realtime, the visual type Swing indicator you apply on your chart is optimized to show the swing structure visually and thus some of it's info can't be accessed the same way programmatically as it would not exist at that point back in time then.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Yes, that was helpful. Thanks!

      Comment


        #4
        Still not working...

        Well, I was finally able to fully test it and here is my conclusion:

        Running Swing(5).SwingHigh[0] calculates correctly when run:

        - as an indicator on real-time or historical data
        - within a strategy on historical data

        If you run it within a strategy on real-time data it does not calculate correctly. See the attached screenshots showing the same data differently. I'm running it as an indicator with Calculate on Close = True and I'm running it in a strategy using the (FirstTickOfBar) with Calculate on Close = False.
        Attached Files

        Comment


          #5
          richp6, can you please post the code you're using so we can see if anything else is going on? The Swing indicator recalculates after new bars arrive, so perhaps that is throwing off the values.
          AustinNinjaTrader Customer Service

          Comment


            #6
            Here's the code for the indicator:

            protected override void Initialize()
            {
            Add(new Plot(Color.FromKnownColor(KnownColor.Magenta), PlotStyle.Dot, "SH"));
            Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Dot, "SL"));
            Overlay = true;
            }

            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
            // Use this method for calculating your indicator values. Assign a value to each
            // plot below by replacing 'Close[0]' with your own formula.
            SH.Set(Swing(5).SwingHigh[0]);
            SL.Set(Swing(5).SwingLow[0]);
            }

            In my strategy, I have this code:

            protected override void Initialize()
            {
            CalculateOnBarClose = false;
            TraceOrders = true;
            ExitOnClose = false;
            Add(MySwing(1));
            }

            When I created the indicator, I inadvertently forgot to take out the user input variable, but it doesn't get used, but that is why there is a 1 in the ().

            Let me know what you come up with.

            Thanks,

            Rich

            Comment


              #7
              Rich, that code looks fine but it obviously isn't working correctly for you. Can you please also post the code to the swing indicator you created (MySwing()) along with the steps necessary to demonstrate this issue?
              AustinNinjaTrader Customer Service

              Comment


                #8
                I posted the code to the indicator MySwing in my previous post (I start by saying here is the code to the indicator).

                I did some additional testing and here is what I found out. If you run my indicator with calculate on bar close = false, then you get the same erroneous results. You should be able to easily test this.

                Now, in my strategy I'm doing two things:

                (1) I'm calling the indicator to see how it charts out.
                (2) I'm making buy / sell decisions based on this indicator within my strategy but only as part of this statement: if (FirstTickOfBar). My understanding is that it should give me the same results as calculate on bar close = true. However, it wasn't. When I mentioned that in the forum, I was told I should chart the indicator to see what is happening.
                (3) If I run my strategy with calculate on bar close = true, I get the correct results. However, my entries come to late because they wait until the bar closes to enter.

                Any suggestions?

                Comment


                  #9
                  richp6, thanks for the update. This could definitely be due to the way this indicator recalculates previous values. I will have Bertrand confirm or deny this tomorrow, but in the meantime, please put it on a chart and see what happens.
                  AustinNinjaTrader Customer Service

                  Comment


                    #10
                    I'll wait for Bertrand. As for putting it on a chart, I have already done that, which is why I was able to attach the files in the first place.

                    Comment


                      #11
                      Rich, the indicator plotted programmatically on a bar per bar basis will definitely look differently then the charted 'visual only' version - however for your entries using this on programmatic version on COBC = false, were you calling the indicator one bar back further via the index to make up for calling it basically on the open of the next bar?
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        I'm not sure what it would matter if I called the index at [0] or [1]. I understand I might get a different value, but only because a new swing could have been made on that bar. That doesn't account for the completely inaccurate calculation of the swing in the first place. The high of the bar is the high of the bar regardless. It appears to be calculating either on the close of the bar or the open of the following bar. It is only when these values equal the high of the bar is it calculating correctly with COBC = False.

                        For my own code, I have found a work around. Essentially I am now finding the swing bar using Swing(5).SwingHighBar(0,1,10). Once I find it, I take the High value of this bar. This gives me correct calculations with COBC = False. But for the benefit of NT, the swing.swinghigh / low calculation should be fixed under COBC = False.

                        Comment


                          #13
                          Rich, great you got a workaround implemented for you - I'll check into the points you mention running the Swing to update intrabar.
                          BertrandNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by WHICKED, Today, 12:45 PM
                          2 responses
                          16 views
                          0 likes
                          Last Post WHICKED
                          by WHICKED
                           
                          Started by GussJ, 03-04-2020, 03:11 PM
                          15 responses
                          3,272 views
                          0 likes
                          Last Post xiinteractive  
                          Started by Tim-c, Today, 02:10 PM
                          1 response
                          8 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by Taddypole, Today, 02:47 PM
                          0 responses
                          2 views
                          0 likes
                          Last Post Taddypole  
                          Started by chbruno, 04-24-2024, 04:10 PM
                          4 responses
                          51 views
                          0 likes
                          Last Post chbruno
                          by chbruno
                           
                          Working...
                          X