Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to drawtext for the last 100 bars only

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

    How to drawtext for the last 100 bars only

    I have a question concerning the following code:
    DrawText("tag1" + CurrentBar, "Text to draw", 0, 1000, Color.Black);

    With the code above, I would be able to draw text from the 1st bar of the session to the last bar of the session.

    If I only need to draw text from 100 bars ago till the last bar of the session, how do I do it?

    Thank you!

    #2
    The region of code you highlighted does not have any bearing on where the DrawText is placed. That part only allows you to have individual object names for each of your draw objects. If you share the same object name as a prior object then instead of having two objects you will simply modify the first.

    DrawText(string tag, string text,int barsAgo, double y, Color color)

    The place you determine where your object will go is defined by the int barsAgo parameter.

    If you wanted to draw for the last 100 bars you would need to create a loop and just cycle the barsAgo parameter from 0 to 100. Keep in mind you need 100 unique object names.

    Untested code, but you get the idea.
    Code:
    for (int x = 0; x < 100; x++)
    {
         DrawText("text" + x, "Text to Draw", x, 1000, Color.Black);
    }
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick reply.

      I don't have problems in setting where the drawtext is placed.

      I just tried your code idea, it would drawtext on the last 100 bars, but with exactly same text that is drawn on the last bar. When a new bar appears, the old texts will be replaced with the new one.

      With "+ CurrentBar", it would not erase nor replace any text drawn previously. It has been working perfectly for months until last few days where the volatility is so high and I have a lot more bars with text to be drawn, the program would crash out or become very slow prior to close. That's why I try to limit the drawtext to the last set of bars only.

      How do I do it?

      Thank you!



      Originally posted by NinjaTrader_Josh View Post
      The region of code you highlighted does not have any bearing on where the DrawText is placed. That part only allows you to have individual object names for each of your draw objects. If you share the same object name as a prior object then instead of having two objects you will simply modify the first.

      DrawText(string tag, string text,int barsAgo, double y, Color color)

      The place you determine where your object will go is defined by the int barsAgo parameter.

      If you wanted to draw for the last 100 bars you would need to create a loop and just cycle the barsAgo parameter from 0 to 100. Keep in mind you need 100 unique object names.

      Untested code, but you get the idea.
      Code:
      for (int x = 0; x < 100; x++)
      {
           DrawText("text" + x, "Text to Draw", x, 1000, Color.Black);
      }

      Comment


        #4
        If you do + CurrentBar you end up drawing on every single future bar. You'll end up with text on far more than 100 bars after a day's trading session. I guess I do not know what you want. In your original post I was under the impression you only wanted to have drawings on the LAST 100 bars. The code I provided gives you the last 100 bars. When you roll forward the 100th bar is no longer the last 100 so that object is removed and pushed forward to the current 100 bars.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I pratically have different texts on every bar, they were saved on the chart till the last bar of the session. + CurrentBar does that perfectly. However, due to the volatility in last few days, with a lot more bars and texts, the program started slowing down and eventually crashed out. Now I want to keep the texts only for the last set of bars and erase all the texts prior to the set, so that the program can run smoothly through out the entire trading session.

          Maybe I can keep the original code and use RemoveDrawObject?
          Last edited by gamevoodoo; 10-08-2008, 04:38 PM.

          Comment


            #6
            Yes that will work for you. Keep your original code and just go through and RemoveDrawObject() on the old string tag values for your old bars. You may also try adding this statement to the beginning of your OnBarUpdate():
            Code:
            if (Historical)
                 return;
            This way you will only draw text on real-time built bars. Note: This may influence your other calculations so you will need to determine if it is suitable or not on your own.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Sorry for being a coding noob.

              Please show me an example of using RemoveDrawObject to remove texts from n bars ago. In this case, we want to remove the texts drawn 100 bars ago. Also, we have two texts drawn there, I need to remove both of them.

              Thank you!

              Comment


                #8
                According to your current naming scheme an object 100 bars ago would have a string ID of CurrentBar - 100.

                Code:
                int barNum = CurrentBar - x;
                RemoveDrawObject("tag1" + barNum);
                To remove the second object there you simply need to know the string name. Create yourself a loop and just cycle through the prior string ID names to remove them all.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you very much Josh!

                  It's now working exactly the way I want.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by varun12343, Today, 03:09 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Marco  
                  Started by Lance El Camino, Yesterday, 04:14 PM
                  2 responses
                  26 views
                  0 likes
                  Last Post NT-Roland  
                  Started by MatthewLesko, Yesterday, 11:13 PM
                  1 response
                  15 views
                  0 likes
                  Last Post NT-Roland  
                  Started by kevinenergy, Today, 01:44 AM
                  0 responses
                  8 views
                  0 likes
                  Last Post kevinenergy  
                  Started by jtrading82, Yesterday, 04:56 PM
                  1 response
                  14 views
                  0 likes
                  Last Post NinjaTrader_ShawnB  
                  Working...
                  X