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

Button To Control Bool

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

    Button To Control Bool

    Good Afternoon,

    I am trying to create a button to control a bool which controls whether a drawing object is displayed or not. Within OnBarUpdate, I have drawing objects wrapped inside a bool condition. This bool is currently controlled by user input via properties checkbox.

    I would like to change that to being button controlled on the chart. I have created the button and the bool cycles true/false when the button is clicked, verified by print statement. However, the drawing object contained in that bool condition wrapper is not cycling on/off.

    Code:
    protected override void OnBarUpdate()
    {
    
       if (devButtonClicked)
       {
          HistDevs = true;
       }
    
       if (!devButtonClicked)
       {
          HistDevs= false;
       }
    
       if (HistDevs)
       {
          ...Draw stuff
       }
    }
    Code:
            
    private void OnButtonClick(object sender, RoutedEventArgs rea)
    {
       System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
                
       if (button == devButton && button.Name == "DevButton" && button.Content == "HideDevs")
       {
          if (!HistDevs)
             devButtonClicked = true;
                    
          if (HistDevs)
             devButtonClicked = false;
            
          return;
       }
    }​
    Above is how I am currently handling this. The "HistDevs" is the bool that wraps the drawing objects within OnBarUpdate. As mentioned, that bool is cycling properly, but the drawing objects are not cycling. I presume this has something to do with them already being drawn and cached, any advice is appreciated.

    Last edited by ChrisR; 02-09-2024, 03:11 PM.

    #2
    Hello ChrisR,

    If the purpose is to just toggle the bool the easiest way to do that would be to negate the bool like the following:

    Code:
    private void OnButtonClick(object sender, RoutedEventArgs rea)
    {
        System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
    
        if (button == devButton && button.Name == "DevButton" && button.Content == "HideDevs")
        {
             HistDevs = !HistDevs;  
        }
    }​
    The code in OnBarUpdate won't trigger immediately from the button event, it would need to wait for the next OnBarUpdate to see the updated value. The easiest way to test this would be to set the script to use OnEachTick so you can quickly see the button changing the bool.

    Another factor is that once an object is drawn it won't be removed unless you call RemoveDrawObject.

    I would suggest doing something like the following for efficiency:

    Code:
    private Dot myDot;
    protected override void OnBarUpdate()
    {
       if (HistDevs)
       {
          myDot = Draw.Dot(this, "tag1", true, 0, Low[0] - TickSize, Brushes.Red);
       }
       if(!HistDevs && myDot != null)
       {
           RemoveDrawObject("tag1");
           myDot = null;
       }
    }




    ​​
    JesseNinjaTrader Customer Service

    Comment


      #3
      @NinjaTrader_Jesse

      Thanks for the response. While this works for a basic application, I failed to mention I am using +CurrentBar in the tag name with a "Show Historical" switch. Additionally, since I am toggling showing historical bar events of that same drawing object, I am wrapping it in a time condition so it doesnt draw on every OnBarUpdate event and only at that specific time.

      Code:
      protected override void OnBarUpdate()
      {​
         if (ShowHistorical) BarNum = CurrentBar; else BarNum = 0;​
      
         if (Time[0] == sTime)
         {
            myDot = Draw.Dot(this, "tag1"+BarNum, true, 0, Low[0] - TickSize, Brushes.Red);
         }
      }
      The above is how the drawing object is handled, without any of the modifications we are speaking of. I think the fact that I am wrapping the object creation in a time object and using a variable addition for the tag name may be messing it up as to trying to remove the object with a bool toggle.
      Last edited by ChrisR; 02-09-2024, 05:35 PM.

      Comment


        #4
        Originally posted by NinjaTrader_Jesse View Post
        Hello ChrisR,

        If the purpose is to just toggle the bool the easiest way to do that would be to negate the bool like the following:
        .............
        [/CODE]​



        ​​
        Jesse,

        The below code I have working somewhat, but it is only removing the latest instance of LineO and not historical lines before the latest one. LineO is drawng using a "Tag"+CurrentBar, and is contained in a Time[0] == DateTime Object wrapper so it only draws at specific times and not every OnBarUpdate event.

        Code:
        if (Time[0] == sTime)
        {
           LineO = Draw.Line(this, "Open"+BarNum, false, sTime, sPrice, eTime, sPrice, newColor6, DashStyleHelper.Dash, 1, true);
        }​

        I'm not sure why its only hiding the latest instance of the line when I'm referencing the object itself and not specific tags.

        Code:
        protected override void OnBarUpdate()
        {​
           if (devButtonClicked)
           {
              foreach (DrawingTool draw in DrawObjects.ToList())
              {
                 if (draw is DrawingTools.Line)
                 {
                    LineO.IsVisible = false;
                 }
              }
                                    
              RefreshReset = true;
           }
                                
           if (!devButtonClicked)
           {
              foreach (DrawingTool draw in DrawObjects.ToList())
              {
                 if (draw is DrawingTools.Line)
                 {
                    LineO.IsVisible = true;
                 }
              }
                                    
              RefreshReset = false;
           }​
        }
        Code:
        private void OnButtonClick(object sender, RoutedEventArgs rea)
        {
           System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
                    
           if (button == devButton && button.Name == "DevButton" && button.Content == "HideDevs")
           {
              devButtonClicked = !devButtonClicked;
                        
              if (RefreshReset)
                 ForceRefresh();
                        
              return;
           }
        }​
        Last edited by ChrisR; 02-11-2024, 07:40 AM.

        Comment


          #5
          Hello ChrisR,

          The RemoveDrawObject method requires that you supply the tag name of the object to be removed. If you wanted to remove other objects you would need to collect those tags when you initially draw the object so you know what names were used later when you want to remove them.

          If you want to remove all historical objects you can just use RemoveDrawObjects(); instead which clears all objects.

          To collect the names of specific objects that you want to later remove you could use a List<string> and add the name of the object to the list each time you draw the object. Later when you wanted to remove those objects you could loop over the list, remove the objects and then clear the list to reset it.

          In the new code that you posted you have only a single instance of the object, the LineO variable represents the last object you set to that variable. To control multiple objects in that way you would need many variables or a better approach would be to use a List<Line> to store the objects you wanted to manipulate.



          ​​
          JesseNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello ChrisR,

            The RemoveDrawObject method requires that you supply the tag name of the object to be removed............
            ​​
            Jesse,

            I created a string loop to remove the objects by tag, this is working, however when the button is clicked a second time, which should allow them to be redrawn by toggling the bool back to false, they are not being redrawn. The "devButtonClicked" bool is toggling properly as verified with prints. I think this may have something to do with the time condition for LineO, but not sure. I however need the time condition that way I'm not drawing objects on every OnBarUpdate event and only when they are supposed to be drawn.

            Code:
            protected override void OnBarUpdate()
            {
            
               if (Time[0] == sTime && !devButtonClicked)
               {
                  LineO = Draw.Line(this, "Open"+CurrentBar, false, sTime, sPrice, eTime, sPrice, newColor6, DashStyleHelper.Dash, 1, true);
               }
            
               foreach (DrawingTool draw in DrawObjects.ToList())
               {
                  if (draw.Tag.Contains("Open"))
                  {
                     itemsToRemove.Add(draw.Tag);
                  }
               }
                                        
               if (devButtonClicked)
               {
                  foreach (DrawingTool draw in DrawObjects.ToList())
                  {
                     foreach (string value in itemsToRemove)
                     {
                        RemoveDrawObject(value);
                     }
                  }
               }
            }
            And here is the code for the button:

            Code:
            private void OnButtonClick(object sender, RoutedEventArgs rea)
            {
               System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
                            
               if (button == devButton && button.Name == "DevButton" && button.Content == "HideDevs")
               {
                  devButtonClicked = !devButtonClicked;
                                  
                  return;
               }
            }​
            Last edited by ChrisR; 02-12-2024, 10:13 AM.

            Comment


              #7
              Hello ChrisR,

              I would not be aware of any way to improve the performance in that use case. you can try using a Print to check if your code is being executed multiple times or looping more times than necessary.

              JesseNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Jesse View Post
                Hello ChrisR,

                I would not be aware of any way to improve the performance in that use case. you can try using a Print to check if your code is being executed multiple times or looping more times than necessary.
                But to confirm the assumption, it should redraw LineO, regardless if its wrapped in a time condition, correct? The code, as you see it, should remove/redraw LineO and all historical instances of LineO?

                Comment


                  #9
                  Hello ChrisR,

                  In the new code you posted you only draw when the time condition is true, that won't re draw any historical objects that were removed. You would have to create a loop that draws those objects again but that would be a more complex process because you would need to know the BarsAgo for the start and end anchors in relation to the current bar and also the prices for the anchors.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Jesse View Post
                    Hello ChrisR,

                    In the new code you posted you only draw when the time condition is true, that won't re draw any historical objects that were removed. You would have to create a loop that draws those objects again but that would be a more complex process because you would need to know the BarsAgo for the start and end anchors in relation to the current bar and also the prices for the anchors.
                    So if that is the case, than instead of removing/redrawing the objects, what about just toggling them with IsVisible? I can add a ForceRefresh when the button is clicked so it re-renders whatever is queued.

                    This would not be exactly what I want (being able to control showing latest or "latest + historical"), but would instead hide/show all LineO's, which I guess is good enough. Assuming this is possible and would function correct?
                    Last edited by ChrisR; 02-12-2024, 11:50 AM.

                    Comment


                      #11
                      Hello ChrisR,

                      Yes you can do that, your previous sample was toggling the visibility which would be another approach to hide the objects. If you wanted to seperate showing historical or current lines you could use a List<line> to collect lines that you draw and toggle all but the last added line in the list which would be the most recent line. If you wanted to toggle only the most recent line you would just toggle the last added line in the list.

                      JesseNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by ChartTourist, Today, 08:22 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post ChartTourist  
                      Started by LiamTwine, Today, 08:10 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post LiamTwine  
                      Started by Balage0922, Today, 07:38 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post Balage0922  
                      Started by JoMoon2024, Today, 06:56 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post JoMoon2024  
                      Started by Haiasi, 04-25-2024, 06:53 PM
                      2 responses
                      19 views
                      0 likes
                      Last Post Massinisa  
                      Working...
                      X