• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to*determine if the current chart is the active chart?

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

    How to*determine if the current chart is the active chart?

    How do you determine if the current chart is the active chart?

    I have a timer I built into a strategy to draw different items on the chart it is attached to. I would like to try to save some computer resources by filtering when the draw actions actually occur by limiting it to only when the chart is active or in the forefront. Which way would be better, the best I could I figure out is below.

    Also, I wanted to know which method to use to bring a chart that is in the background or a chart that is open as a tab but not the active tab, and bring it to the forefront?


    Code:
    public bool ChartActive()
                {
    
                if(ChartControl != null)
                {
                     NinjaTrader.Gui.Chart.Chart chart = Window.GetWindow(this.ChartControl) as NinjaTrader.Gui.Chart.Chart;
    
                    if(chart.Focus())
                    {
                        //Draw stuff
                    }
    
                    if(chart.IsActive())
                    {
                        //Draw stuff
                    }
                 return true;
                }
                else
                    return false;
                }
    Last edited by cutzpr; 03-29-2019, 09:24 PM.

    #2
    Originally posted by cutzpr View Post
    How do you determine if the current chart is the active chart?

    I have a timer I built into a strategy to draw different items on the chart it is attached to. I would like to try to save some computer resources by filtering when the draw actions actually occur by limiting it to only when the chart is active or in the forefront. Which way would be better, the best I could I figure out is below.

    Also, I wanted to know which method to use to bring a chart that is in the background or a chart that is open as a tab but not the active tab, and bring it to the forefront?


    Code:
    public bool ChartActive()
    {
    
    if(ChartControl != null)
    {
    NinjaTrader.Gui.Chart.Chart chart = Window.GetWindow(this.ChartControl) as NinjaTrader.Gui.Chart.Chart;
    
    if(chart.Focus())
    {
    //Draw stuff
    }
    
    if(chart.IsActive())
    {
    //Draw stuff
    }
    return true;
    }
    else
    return false;
    }
    1. You probably want to separate your chart identification from your actual drawing activity.
    2. The way that you have it bracketed means that it will always return true if ChartControl is not null.
    3. You seem to be unconditionally drawing your objects once you have determined that the chart is active, however you determine that. So, you may want to write your code itself a bit more efficiently.
    Code:
    if (ChartActive())
    {
    //Draw stuff
    }
    Code:
    public bool ChartActive()
    {
    if(ChartControl != null) {
    NinjaTrader.Gui.Chart.Chart chart = Window.GetWindow(this.ChartControl) as NinjaTrader.Gui.Chart.Chart; if(chart.Focus() || chart.IsActive()) return true;
    } return false;
    }
    Even better, make your code more explicit and understandable by always setting and returning a controlled value.
    Code:
    public bool ChartActive()
    {
    bool returnValue = false;
    
    if(ChartControl != null)
    {
    NinjaTrader.Gui.Chart.Chart chart = Window.GetWindow(this.ChartControl) as NinjaTrader.Gui.Chart.Chart; if(chart.Focus() || chart.IsActive()) returnValue = true;
    }
    return returnValue; }
    In this particular case, of course, it was trivial. I am just saying that you might want to get into the habit of knowing exactly what you are returning, without having to follow the logic of the code. It makes it much easier to read and modify when you come back to it one year later, when you have forgotten exactly what you were thinking at the time that you wrote the code. Writing this way makes at least your code returns pretty much self-documenting. Just saying.

    Of course, if you are drawing different objects, depending on how you determine that the chart is active, we shall have to modify the code a bit. Let me know.
    Last edited by koganam; 04-01-2019, 12:04 AM. Reason: Corrected grammar and punctuation.

    Comment


      #3
      Hello cutzpr,

      I feel that koganam has provided you good information. I also wanted to note that this would let you know if the chart is in focus but not specifically the tab in the chart.

      You may find you want to know which tab is selected as well. Below are a few examples that display for the selected tab only.
      http://ninjatrader.com/support/forum...327#post499327
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Tradarr, Today, 06:25 PM
      2 responses
      13 views
      0 likes
      Last Post Tradarr
      by Tradarr
       
      Started by cocopod, Today, 04:47 PM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_JoshG  
      Started by digibob, Today, 02:47 AM
      2 responses
      14 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by mntemel, Today, 03:31 PM
      4 responses
      23 views
      0 likes
      Last Post mntemel
      by mntemel
       
      Started by mrlucky1x, Today, 03:14 PM
      2 responses
      7 views
      0 likes
      Last Post mrlucky1x  
      Working...
      X