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

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()
    {[INDENT]if(ChartControl != null)
    {[/INDENT][INDENT=2]NinjaTrader.Gui.Chart.Chart chart = Window.GetWindow(this.ChartControl) as NinjaTrader.Gui.Chart.Chart;
    
    if(chart.Focus() || chart.IsActive()) return true;[/INDENT][INDENT]}
    
    return false;[/INDENT]
     
    
    
    }
    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)[INDENT]{[/INDENT][INDENT=2]
    NinjaTrader.Gui.Chart.Chart chart = Window.GetWindow(this.ChartControl) as NinjaTrader.Gui.Chart.Chart;
    
    if(chart.Focus() || chart.IsActive()) returnValue = true;[/INDENT][INDENT]}[/INDENT]
     
    
    
    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 WHICKED, Today, 12:45 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by samish18, Today, 01:01 PM
      0 responses
      5 views
      0 likes
      Last Post samish18  
      Started by WHICKED, Today, 12:56 PM
      0 responses
      8 views
      0 likes
      Last Post WHICKED
      by WHICKED
       
      Started by Spiderbird, Today, 12:15 PM
      2 responses
      11 views
      0 likes
      Last Post Spiderbird  
      Started by FrazMann, Today, 11:21 AM
      2 responses
      8 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Working...
      X