Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

RemoveDrawObjects does not work

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

    RemoveDrawObjects does not work

    Hi,

    I have a problem deleting Lines that i drawn with my indicator.

    The indicator draws new lines while the market is open (between 8:30am and 3:00pm), when the market closed i call RemoveDrawObjects(); to delete all Lines but it does not work.
    For example i am adding the indicator at 9:10pm and i saw the lines, so i put a log to see if the RemoveDrawObjects(); method is called and yes it is called but the lines stille appear.

    To draw the lines i use:
    NinjaTrader.NinjaScript.DrawingTools.Line myLine = Draw.Line(this, mylineName, true, closeStartTime, closeStartY, closeEndTime, closeEndY, true, "");
    the name of the line myLineName it is a concatenation of a string "myLine" + Time[0].ToString() to have different names with every Line.
    And to remove all lines i use RemoveDrawObjects();


    Click image for larger version  Name:	Image1.png Views:	0 Size:	111.2 KB ID:	1210131


    when I add the indicator at 9:10 pm since it is not between open market hours I am not supposed to see any lines, but....


    Click image for larger version  Name:	Image2.png Views:	0 Size:	82.1 KB ID:	1210132




    Thank you for your help!!
    Last edited by arbeydario; 07-28-2022, 08:25 PM.

    #2
    Hello arbeydario,

    Are you using the latest version? There was an issue with drawing objects not being removed in the prior version 8.0.26.0 and was corrected in 8.0.26.1. Could you double check that you are on 8.0.26.1?

    The only other item that sticks out is this comment:
    when I add the indicator at 9:10 pm since it is not between open market hours I am not supposed to see any lines, but....
    Are you saying that you re add the indicator after the trading session? If you are removing the objects based on some condition and this only happens when you re add the indicator that may be the condition to remove objects is not true in that specific use case. Re adding the script would let it reprocess the historical data and draw lines through the session.

    Comment


      #3

      Hi Jesse, yes, I add the indicator outside the session and what I would expect is that when NinjaTrader runs the history (last session) he might paint the lines but when the session closes he should delete the lines again (because i call RemoveDrawObjects() ) and leave the chart clean waiting the next session.

      And about the NinjaTrader version, my installed version is 8.0.26.1 64-bit
      Last edited by arbeydario; 07-29-2022, 06:17 PM.

      Comment


        #4
        Hello arbeydario,

        That really depends on what you coded, it sounds like whatever condition you made is not happening in that use case. you would have to use a Print and see which part of the condition for removing the objects is not happening.

        Comment


          #5
          Hi Jesse,

          I use the print and saw that i call RemoveDrawObjects(); but the objects still appear in the chart.
          I try to Remove with RemoveDrawObject("MyLineTag"), and with the print saw that i call it, but the objects still appear in the chart.

          Then, this methods dont work, and i dont know how to do...

          Comment


            #6
            Hello arbeydario,

            At what point in your logic are you calling the method? If there is no incoming data at that point it may be part of the problem. Do you see the lines remove if you do a manual action like dragging the chart around of maximizing the window to force it to repaint?

            Comment


              #7
              Hi Jesse,


              I think I found the problem, if the method RemoveDrawObjects() is called in State=Historical or some State !=Realtime, it doesnot work, but, in State=Realtime, i have to call the method RemoveDrawObjects() only inside OnBarUpdate(), because if i call the method OnStateChange() this does not work either.

              protected override void OnStateChange(){
              if (State == State.Realtime){
              DeleteAllIndicatorDrawObjects(); //This doesnot work...
              }
              }


              shouldn't work too in State=Historical ?? or other States?

              Comment


                #8
                Hello arbeydario,

                For that type of logic that should be in OnBarUpdate and not OnStateChange. Do you see that working if you use it from OnBarUpdate?



                Comment


                  #9
                  Hi Jesse,

                  i try to call the method in both sides (OnStateChange and OnBarUpdate).

                  I will try to explain what I found: when the indicator is running in state Historical, if i call the method RemoveDrawObjects() OnStateChange and OnBarUpdate, this doesnot work. When State change to Realtime, in OnStateChange i call the method and still does not work, but, when runs OnBarUpdate in state Realtime it works, my question is, it should not work also when i call it in onbaupdate with state = Historical?

                  Comment


                    #10
                    Hello arbeydario,

                    We can exclude the info about using OnStateChange, that is not the correct location for that code to be used for this type of use case.

                    Using that in OnBarUpdate should work for both historical and realtime. If you are seeing the lines still and calling RemoveDrawObjects in historical that would hint at the lines are being drawn sometime after you called RemoveDrawObjects. RemoveDrawObjects would need to be the last code called after all objects were drawn to be able to remove them.

                    For example the following works to remove the object in historical, this just ends up drawing nothing because the object is always removed. The code is not allowed to run in realtime so that would demonstrate the method works historically.

                    Code:
                    if(State == State.Historical)
                    {
                        Draw.Dot(this, "Test", true, 0, Close[0], Brushes.Red);
                        RemoveDrawObjects();
                    }
                    Last edited by NinjaTrader_Jesse; 08-02-2022, 10:27 AM.

                    Comment


                      #11

                      Hi Jesse, that's what I do, in OnBarUpdate I have a validation, if the current time has already exceeded the maximum validation time then it doesn't paint the lines and instead deletes them with removeDrawObjects(), all within OnBarUpdate, but at apparently it does not delete them if the state is Historical, only when it changes to Realtime if it deletes them



                      I want to explain my indicator a little, it draws lines at the open and close of a candle under certain conditions, but it does so only while in a range of times, that is, between 8:30am and 11:00am, all this logic is developed inside OnBarUpdate , if I add the indicator to the chart after 11:00am, it means that when state =Historical is executed RemoveDrawObjects and when state = realtime starts, the objects should have been deleted because the real time is after 11:00am ( when I add the indicator to the chart)


                      something like this (is not my code it is an example):

                      protected override void OnBarUpdate(){
                      //Estos dos if son super importantes ya que controlan que comience a ejecutarse mi codigo luego que tenga algun dato para cargar
                      if (BarsInProgress != 0)
                      return;

                      if (CurrentBars[0] < 1)
                      return;

                      if(isBetween8and11()){
                      drawNewLine();
                      }else{
                      if(!objectsDeleted){//To delete objects just one time
                      DeleteAllIndicatorDrawObjects(); //Here is the problem, if i add the indicator at 11:30am this line is executed when state=historical and doesnot remove the objects, then when
                      state=realtime is not called because de bool that executes just one time per day..., if i remove the if and always call RemoveDrawObjects this works
                      at the moment state=realtime, or if i re call the method but in state=realtime it works but i think that should be work at the first call when state = historical
                      objectsDeleted=true;
                      }
                      }
                      }


                      I think that if i re call the method again but when state=realtime it works but i want to know it is the real solution or if i have a bug with my code or it is a problem of the NinjaScript library...

                      Comment


                        #12
                        Hello arbeydario,

                        The pseudo code won't help here, you will need to debug your actual script to find the error. As mentioned in post 10 calling that method in historical works and I provided a simple way to demonstrate that. If your script is not working you will need to reduce the code and find out why. You can place Prints near your draw syntax and the remove syntax to see what order your logic is actually being executed in.



                        Comment


                          #13
                          Hi Jesse,

                          i found the code that makes it doesnot work, but, i dont know why does not work...

                          if (State == State.Historical && !DateTime.Now.ToString("yyyy-MM-dd").Equals(Time[0].ToString("yyyy-MM-dd"))){
                          return;
                          }

                          I add this line at the start of OnBarUpdate method, to avoid draw lines from previous days (my 5 minute candle chart can have loaded more of 10 days), I am only interested in those from the current day and if the hour or day is between to hours..., his helps the indicator load faster, but it makes RemoveDrawObjects fails or not work, and i dont know why...

                          i reduce the code and found that, could you check or try the code or have suggestions about it?

                          RemoveObjectsTest.cs

                          You just have to upload it to the ninja and add it to a 5 minute candlestick chart after 11am (if you do it before 11am you can change the time in the code), the idea is to add it to the chart after the configured range of hours 8 - 11 and if now hour is between 8-11 you can saw the lines but if now hour is before 8-11 you should not see the lines (but still saw the lines, and the RemoveDrawObjects, you can see it in the output window).


                          Sorry for my insistence and thanks for the help you can give me

                          Comment


                            #14
                            Hello arbeydario,

                            DateTime.Now is not used for NinjaScript, that is your PC clock and won't relate to bar times.

                            If you want to remove objects from yesterday or previous days from now in processing you should subtract a day from Time[0] instead of involving the PC clock. You can also just use the existing session information for this type of reset.

                            If you want to avoid drawing on previous days you can make your logic remove the objects on the first bar of the session and then allow the script to process during the session. That would limit it to one day of drawing.

                            Code:
                            if(Bars.IsFirstBarOfSession)
                            {
                                RemoveDrawObjects();
                            }else 
                            {
                            
                            // your normal drawing logic
                            }
                            In that use case the remove drawing happens on the first bar of the session, every bar after that could be used for drawing during that single day. On the next day the same process would happen again.



                            Comment

                            Latest Posts

                            Collapse

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