Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

need help with a small code

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

    need help with a small code

    Hello,

    I am trying to draw horizontal lines 1.5 ATR above and below the close of the last bar on the chart. The issue is that I am able to draw the lines with respect to the second last bar on the chart but not the las bar on the chart. Here is the code I am using and a chart screenshot of the issue. you can see that the center of the lines is the close of last bar minus one and not the last bar.

    The data I am using is an offline EOD data that I manually upload at the end of each trading day.

    Code:
    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.
                double upperLimit, lowerLimit;
    			if (CurrentBar==Bars.Count-2)
    			{
    				upperLimit=Close[0]+ATR(aTRPeriod)[0]*aTRMultiplier;
    				lowerLimit=Close[0]-ATR(aTRPeriod)[0]*aTRMultiplier;
    				DrawLine("UpATR", false, Time[1], upperLimit, Time[1].AddDays(15), upperLimit, Color.Orange, DashStyle.Solid, 1);
    				DrawLine("DownATR", false, Time[1], lowerLimit, Time[1].AddDays(15), lowerLimit, Color.Orange, DashStyle.Solid, 1);
    				
    			}
            }
    Attached Files
    Last edited by Eklavya; 06-25-2010, 07:17 AM.

    #2
    Hi Eklavya,

    What CalculateOnBar close setting are you using in the Initialize() section of you code.

    Try setting this to false, if you are still seeing the same behavior, try editing the ATR indicator, saving it by a different name (will have to call by that name too) and remove the "CalculateOnBarClose = true;" line.
    TimNinjaTrader Customer Service

    Comment


      #3
      Hi Tim..I did this but it did not resolve the issue. This was to be expected.

      Modifying the ATR indicator just gave me latest ATR values. But the reference level from which the distance should be calculated has not changed. It remains the close of last but one bar.

      I need the reference level to be the close of the last bar and not the second last bar.

      I think the problem might lie in the line
      if (CurrentBar==Bars.Count-2)
      Bars.Count has a value of 250. CurrentBar never gets to the value of 249.But again, I am pretty new to this so I might be completely off.
      Last edited by Eklavya; 06-25-2010, 08:47 AM.

      Comment


        #4
        Hi Eklavya,

        In your code I see...

        if (CurrentBar==Bars.Count-2)

        Which is saying "if the current bar is equal to the number of bars on the chart minus two", which is only true for the bar values of the bar 2 bars ago.

        Instead, try something like...

        if (CurrentBar < 2)
        return;
        Last edited by NinjaTrader_Tim; 06-25-2010, 09:33 AM.
        TimNinjaTrader Customer Service

        Comment


          #5
          Hi Tim,

          This will not resolve the problem. I want the indicator code to execute just once, at the end of the last(rightmost) bar on my chart. And it needs to draw two lines at an equal distance on either side of the close of the last bar.

          When I use If (CurrentBar<2){...}, it will execute the code at the end of the first bar and at the end of the second bar on the chart, these being the two leftmost bars.

          Comment


            #6
            Hi Eklavya,

            Please see my edits, I added this assuming you originally had the Current bar code due to the behavior described at - http://www.ninjatrader.com/support/f...ead.php?t=3170
            TimNinjaTrader Customer Service

            Comment


              #7
              Hi Tim,

              I did resolve the problem though in a very inelegant way. Was really hoping I could have come up with an elegant solution.

              I was not having the behavior you mentioned in your earlier post.

              Here is my code that resolves the problem. Please see that I am having to unnecessarily draw and remove lines after each tick whereas drawing the line only at the end of very last tick on the chart will have resolved my problem.

              Code:
              protected override void OnBarUpdate()
                      {
                          double upperLimit, lowerLimit;
              			Print(CurrentBar);
              			if (CurrentBar<2)
              				return;
              			RemoveDrawObject("UpATR");
              			RemoveDrawObject("DownATR");
              			upperLimit=Close[0]+ATRContinuous(aTRPeriod)[0]*aTRMultiplier;
              			lowerLimit=Close[0]-ATRContinuous(aTRPeriod)[0]*aTRMultiplier;
              			DrawLine("UpATR", false, Time[1], upperLimit, Time[1].AddDays(15), upperLimit, Color.Orange, DashStyle.Solid, 1);
              			DrawLine("DownATR", false, Time[1], lowerLimit, Time[1].AddDays(15), lowerLimit, Color.Orange, DashStyle.Solid, 1);
                      }

              Comment


                #8
                Hi Eklavya,

                Have you tried this without the removals? If the same tag is used, they should be overwritten.
                TimNinjaTrader Customer Service

                Comment


                  #9
                  Sorry..I spoke too soon..My problem is still not resolved.

                  I am beginning to think that the problem cannot be resolved with the way NT works. Please advice if there is a workaround. Please let me know if you need me to explain the problem again.

                  In a nutshell the issue is -

                  My indicator code is not executing at the end of the right most bar on my chart. So if the number of bars on my chart is 250, the variable CurrentBar never gets to a value of 249. What changes do I have to make such that my indicator code executes at the end of the rightmost bar on my chart.

                  Comment


                    #10
                    Hi Eklavya,

                    This should certainly be possible, please confrim you are using....

                    if (CurrentBar<2)
                    return;
                    upperLimit=Close[0]+ATRContinuous(aTRPeriod)[0]*aTRMultiplier;
                    lowerLimit=Close[0]-ATRContinuous(aTRPeriod)[0]*aTRMultiplier;
                    DrawLine("UpATR", false, Time[1], upperLimit, Time[1].AddDays(15), upperLimit, Color.Orange, DashStyle.Solid, 1);
                    DrawLine("DownATR", false, Time[1], lowerLimit, Time[1].AddDays(15), lowerLimit, Color.Orange, DashStyle.Solid, 1);

                    AND

                    CalculateOnBarClose = false;
                    TimNinjaTrader Customer Service

                    Comment


                      #11
                      yes..this is the code I am using. attaching the source code of the indicator.
                      Attached Files

                      Comment


                        #12
                        Hi Eklavya,

                        This indicator did not compile as it is missing a method or indicator source code.

                        Please export using "File>Utilities" so that all dependencies will be attached.
                        TimNinjaTrader Customer Service

                        Comment


                          #13
                          Here it is...I created ATRContinuos and made calculateonbarclose=false
                          Attached Files

                          Comment


                            #14
                            Hi TIm,

                            Don't worry..the indicator is working properly...I just had to remove and attach it again.

                            Thanks for all the help...if you can just help me avoid the redrawing of the line after every bar, I'll appreciate it. However this is not a showstopper issue..so don't bother too much about it.

                            Comment


                              #15
                              Hi Eklavya,

                              If you use the same tag, the previous drawing will automatically be overwritten. What behavior are you seeing, and what are you trying to achieve?
                              TimNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

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