Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

RemoveDrawObject within For loop

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

    RemoveDrawObject within For loop

    Hi NT,

    I have a condition (A) where I number bars using "Draw.Text".
    I simply add a '1', '2' ....'3' on top of bars, these bars are not necessarily consecutive.
    Each time this condition is true and I add a number of the top of the bar and I add the "CurrentBar" to a List object:

    Code:
    private List<int> ListSell;
    ....
    
    if condition A is true
    {
    Draw.Text(this, "tag.CD.S " + CurrentBar, true, "" + counter, 0, High[0] + (((ATR(14))[0])*1.75), 20, Brushes.Red, new SimpleFont("Arial", 12),TextAlignment.Center,Brushes.Transparent, Brushes.Transparent, 0);	
    				
    ListSell.Add(CurrentBar)
    }
    A few bars later, when another condition (B) becomes true, I want to delete these numbers....1', '2' ....'3' on top of bars.

    In order to do this....I use a For loop which runs through the list object to pull all the List values (CurrentBars) which I can use for RemoveDrawObject argument

    Code:
    If condition B is true
    {
    for (int i = 1; i == ListSell.Count; i ++)
    	{
    		RemoveDrawObject( "tag.CD.S " + (ListSell[i]) );					
    	}
    }

    However, this doesnt seem to delete the annotations.
    Do you see any obvious issues with this technique ?

    1) If I use: RemoveDrawObject( "tag.CD.S 196"), where i actually type the bar number, I have no issues and the individual drawing object is removed, but if i use RemoveDrawObject( "tag.CD.S " + (ListSell[i]) ), then the drawing object isn't removed.

    2) I thought perhaps I need to use a "ToString", but this doesn't delete the drawing either:

    Code:
    RemoveDrawObject( "tag.CD.S " + (ListSell[i].ToString()) );

    Any thoughts and insight would be appreciated
    Thanks
    A
    Last edited by akvevo; 11-28-2017, 03:31 PM.

    #2
    Hello akvevo,

    Thanks for your inquiry.

    You will need to place print statements within your for loop to see if RemoveDrawObject() is getting called and your for loop controlling it is functioning properly. Your for loop literally translates to:

    "i" starts at 1. While "i" is equal to ListSell.Count do the following... Then increment "i" by 1 after each iteration.

    Is "i" iterating at all?

    Code:
    Print(i);
    Using prints to monitor your logic will be necessary to verify if your logic is written properly, and will show you what would need to be fixed.

    For the thread's reference, I've included a link to our forum's debugging tips - https://ninjatrader.com/support/foru...ead.php?t=3418

    If this does not resolve your inquiry, please write back with the prints from the NinjaScript Output Window so we can provide further insight.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks Jim,

      1)
      While "i" is equal to ListSell.Count
      I dont think this is correct, in a for loop the second argument refers to "finish test", so my for loop says:

      "i" starts at 1. Up Until "i" is equal to ListSell.Count do the following... Then increment "i" by 1 after each iteration.


      2)
      I have added the print statement:
      for (int i = 1; i == ListSell.Count; i ++)
      {
      Print("i is: " + i);
      RemoveDrawObject( "tag.CD.S " + ( ListSell[i].ToString() ) );
      }
      My output window stays blank, which implies i don't get into the the For loop statement...
      and right now, I dont see why..

      Comment


        #4
        Hello,

        Thank you for the reply.

        You are correct about the for loop. This should likely be changed to < instead of just ==. In your current syntax, you are saying i equals 1 to start, while i is equal to the lists count. Unless the list has only 1 element, this should not iterate. If your list has 1 object, it should iterate one time.

        Also, you are not covering the whole list in the loop by starting at 1 as the list index starts at 0 and ends at the lists Count() -1. This is the reason for < and not <=, if you are trying to iterate each item the loop should look like the following:

        Code:
        private List<string> ListSell = new List<string> { "item1", "item2", "item3" };
        protected override void OnBarUpdate()
        {
                Print("List count" + ListSell.Count.ToString());
        	for (int i = 0; i < ListSell.Count; i ++)
        	{
        		Print("i is: " + i + " ListSell is " + ListSell[i]);
                        RemoveDrawObject( "tag.CD.S " + ListSell[i] );
        	}
        }
        which outputs:

        List count3
        i is: 0 ListSell is item1
        i is: 1 ListSell is item2
        i is: 2 ListSell is item3


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Spot on ! Thanks Jesse

          Comment


            #6
            follow up

            Hi Jesse/NT,

            In my intial post below, i explained how i increment counter if a condition (A) is true....
            and i add the counter number ('1', '2' ....'7'....) above the bars using "Draw.Text".
            Each time this condition is true and I add a number on top of the bar and I add the "CurrentBar" to a List object (List X)... which I can call on later.


            if condition A is true
            {
            Draw.Text(this, "tag.CD.S " + CurrentBar, true, "" + counter, 0, High[0] + (((ATR(14))[0])*1.75), 20, Brushes.Red, new SimpleFont("Arial", 12),TextAlignment.Center,Brushes.Transparent, Brushes.Transparent, 0);

            ListSell.Add(CurrentBar)
            }
            Since then, I have come to the conclusion that I want/need to use Calculate.OnPriceChange.. this makes things more complicated.

            If condition A is no longer True on a given Price Change , which can happen 'intra-bar', I want to delete this 'visual' number count..
            and so I use a For Loop statement which runs through the List object (List X) to pull all the List values (CurrentBars) which I can use for "RemoveDrawObject" argument

            If condition A is no longer True
            {
            for (int i = 0; i < ListSell.Count; i ++)
            {
            RemoveDrawObject( "tag.CD.S " + (ListSell[i]) );
            }
            }
            So far so good...

            However... since i am using calculate.OnPriceChange.. its very possible that condition A becomes True again (intra bar) on the next price change,
            in which case I want to RE-DRAW all those "Draw.Text"(s) that i just deleted...

            I am still thinking about the best way to do this, and keen to get an opinion on how to best solve this issue using Ninja Script.
            I am currently thinking of setting a bool (justDeleted) to True whenever I use the RemoveDrawObject.. and then something like this:

            Code:
            if condition A is true
            {
            Draw.Text(this, "tag.CD.S " + CurrentBar, true, "" + counter, 0, High[0] + (((ATR(14))[0])*1.75), 20, Brushes.Red, new SimpleFont("Arial", 12),TextAlignment.Center,Brushes.Transparent, Brushes.Transparent, 0);	
            				
            ListSell.Add(CurrentBar)
            
            	if justDeleted == true
            	{
            		for ( int i = 0; i < ListSetupBuy.Count; i ++ ) 
            		{				
            			//re-draw the stuff i just deleted		
            			Draw.Text(this, "tag.CD.S " + (ListSetupBuy[i].ToString()), true, "" + counter, 0, High[0] + (((ATR(14))[0])*1.75), 20, Brushes.Red, new SimpleFont("Arial", 12),TextAlignment.Center,Brushes.Transparent, Brushes.Transparent, 0);	
            		}
            
            justDeleted == false //set this back to false
            
            	}
            }

            I am concious that I cant use counter in that Draw.Text line above.. because I don't actually want to use the counter, I just want to redraw the numbers I deleted.
            Any pointers on how to accomplish this ?

            Thanks in advance
            AK
            Last edited by akvevo; 01-02-2018, 11:43 PM.

            Comment


              #7
              Hi NT,

              Let me know your thoughts on this one, and whether i've even delivered the dilemma in a clear and understandable manner

              Thanks
              A

              Comment


                #8
                Hello,

                Thank you for the post.

                In this case, using a bool to toggle the drawing or using an if/else condition would likely be what you would need to do.

                Without testing, it would be hard to say what you need for your logic specifically, but using RemoveDrawObject would likely be the easiest way if you have the tag. If you are using the CurrentBar in the tag, during 1 bar the CurrentBar value will not change so you could remove, re-add, remove etc.. within the same bar using the same tag.

                Here is a very simple if else example that would achieve that:

                Code:
                if(Close[0] > Open[0])
                {
                    Draw.Text(this, "tag.CD.S " + CurrentBar, .....
                } else {
                    RemoveDrawObject( "tag.CD.S " + CurrentBar)
                }
                Assuming the condition toggles from being true to false within the same bar, the object that was drawn on this bar would be removed. If the condition were to become true again within the same bar, it would re-add the drawing.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by frankthearm, Yesterday, 09:08 AM
                14 responses
                47 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by gentlebenthebear, Today, 01:30 AM
                2 responses
                13 views
                0 likes
                Last Post gentlebenthebear  
                Started by Kaledus, Today, 01:29 PM
                2 responses
                8 views
                0 likes
                Last Post Kaledus
                by Kaledus
                 
                Started by PaulMohn, Today, 12:36 PM
                2 responses
                16 views
                0 likes
                Last Post PaulMohn  
                Started by Conceptzx, 10-11-2022, 06:38 AM
                2 responses
                56 views
                0 likes
                Last Post PhillT
                by PhillT
                 
                Working...
                X