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

Toggling another indicator's visibility

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

    Toggling another indicator's visibility

    By using:


    Code:
    private void ToggleIndicatorVisibility()
    {
    
    var indicator = ChartControl.Indicators.FirstOrDefault(indi => indi.Name == "MyCustomIndicator");
    
    if (indicator != null)
    {
    indicator.IsVisible = !indicator.IsVisible; // Toggle visibility
    
    }
    ForceRefresh();
    }

    I've able to turn on and off any indicator I want, but changing the name "MyCustomIndicator" There's only one catch.
    I can not remove the label.

    If I want the annoying top left corner text to disappear and I go into the indicator's properties here:

    Click image for larger version  Name:	Properties Example.png Views:	0 Size:	10.4 KB ID:	1264392

    If I remove the Label,

    then the above code won't be able to turn off and on the Indicator's visibility anymore.

    All of this to ask, is there an alternative for this line:

    Code:
    var indicator = ChartControl.Indicators.FirstOrDefault(indi => indi.Name == "MyCustomIndicator");
    Thank you for your advice in advance.

    I also tried using:

    Code:
    private void ToggleIndicatorVisibility()
    {
    if (myObj != null && myObj.Name == "MyCustomIndicator")
    {
    myObj.IsVisible = !myObj.IsVisible; // Toggle visibility
    Print(myObj.IsVisible ? "Show Indicator" : "Hide Indicator");
    }
    ForceRefresh();
    }

    based on other discussion here in the forum, but I keep getting unhandled error exception.
    Last edited by davydhnz; 08-12-2023, 11:57 PM.

    #2
    Hello davydhnz,

    Thanks for your post.

    You should be setting the indicator.IsVisible = false when you do not want the indicator plot and label displayed on the chart window. When you do want the indicator plot and label to display on the chart, you would need to set indicator.IsVisible = true.

    I have created a simple indicator using the code you shared. I modified the code and tested it on my end. When testing the script, the indicator plot and label becomes invisible when we have an up (green) bar on the chart. When we have a down (red) bar on the chart, the HMA indicator plot and label becomes visible.

    See this demonstration video: https://brandonh-ninjatrader.tinytak...wNjgyMw​
    Attached Files
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      I appreciate you trying to help NinjaTrader_BrandonH

      I loaded your indicator and confirmed it worked as you said.
      Then I turned off the label, and it couldn't "see" the indicator to turn it off and on anymore.

      I also tried changing
      "var indicator = ChartControl.Indicators.FirstOrDefault(indi => indi.Name == "HMA");"
      To another indicator and confirmed the same behaviour occurs when controlling another indicator too.

      The essential functionality I'm looking for is to be able to turn off the label in an indicator properties and yet still access it.
      That's why I attached an image of Label with MyCustomIndicator and talked about removing it and my code not working anymore.

      I'm looking for a solution to being able to turn off a label and yet still access it somehow by Ninjatrader script name, not the properties label.

      Thank you for your help.
      Last edited by davydhnz; 08-14-2023, 04:58 AM.

      Comment


        #4
        Hello davydhnz,

        Thanks for your notes.

        In the indicator you are trying to toggle visible/invisible you could try overriding the DisplayName with an empty string if you still want the name shown in the Indicators window.

        See this help guide page for more information about overriding the DisplayName: https://ninjatrader.com/support/help...isplayname.htm
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thanks NinjaTrader_BrandonH

          I'll give that a go.

          Comment


            #6
            NinjaTrader_BrandonH

            I got the error:
            Code:
            Property or indexer 'NinjaTrader.NinjaScript.NinjaScript.DisplayName' cannot be assigned to -- it is read only    CS0200    789    13 ​
            Is that because I'm trying to change a copyrighted closed source Indicator's display name?

            I tried:
            Code:
            public class MyIndicator : Indicator
            
                 private IndicatorBase TDUMarketStructure;
            
            protected override void OnBarUpdate()
            {
            if (TDUMarketStructure != null)
            {
            // If the target indicator exists, hide its label
            TDUMarketStructure.DisplayName = "";
            }
            }​

            Comment


              #7
              Hello davydhnz,

              Thanks for your notes.

              The DisplayName cannot be set in OnBarUpdate().

              You would have to use the DisplayName override if you want to specify the the text display on the chart panel.

              See the second section of sample code on the DisplayName help guide page for an example of this.

              DisplayName: https://ninjatrader.com/support/help...isplayname.htm


              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                NinjaTrader_BrandonH

                I got the error:

                NinjaTrader.NinjaScript.Indicators.MyIndicator.TDU MarketStructure': cannot override because 'NinjaTrader.NinjaScript.Indicators.Indicator.TDUM arketStructure(bool, NinjaTrader.Data.BarsPeriodType, int, NinjaTrader.Data.TradingHours, bool, TDUMarketStructureDisplayType, int, TDUMarketStructurePullbackMode, int, double, double, double, bool, bool, TDUMarketStructureDisplayType, int, bool, bool, TDUMarketStructureFibonacciType, double, double, double, double, double, double, bool, int, int, bool, bool, bool, int, int, int, double, double, bool)' is not a property CS0544 46 25

                ​Thanks for trying anyway.

                Comment


                  #9
                  Hello davydhnz,

                  Thanks for your notes.

                  How exactly are you defining the DisplayName override in your script?

                  Please write in to support[at]ninjatrader[dot]com to send us your log and trace files by following the steps below along with a simple reduced exported test script that demonstrates the behavior in question. Also, send the exact steps and settings your are using to reproduce the behavior when running the test script.

                  In the subject of your email include ATTN: Brandon H and in the body of the email include a link to this forum post.

                  Follow the steps below to manually attach your log and trace files to your response so I may investigate this matter further.
                  • Open your NinjaTrader folder under, "Documents" (sometimes called, "My Documents")
                  • Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
                  • Send the 2 compressed folders as attachments to this email.
                  • Once complete, you can delete these compressed folders.
                  ​To export a NinjaScript, go to Tools > Export > NinjaScript AddOn.

                  Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed.

                  To create a copy of your script to modify, open a New > NinjaScript Editor, select your script, right-click in the Editor, select 'Save as', name the script, and click OK.​
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    NinjaTrader_BrandonH,

                    Thank you for the offer, but I found a solution.

                    I just had to rename the closed source indicator to a blank unicode space
                    Code:
                    " "
                    and use

                    Code:
                    private Dictionary<Key, string> indicatorShortcutTags = new Dictionary<Key, string>
                    {
                    { Key.S, " " },
                    // You can add more keys and their associated tags here
                    };
                    
                    private void OnKeyUpForIndicator(object sender, KeyEventArgs e)
                    {
                    if (indicatorShortcutTags.ContainsKey(e.Key) && Keyboard.IsKeyDown(Key.LeftCtrl))
                    {
                    ToggleIndicatorsVisibility(indicatorShortcutTags[e.Key]);
                    Print("Toggled visibility for indicator with Display Name: " + indicatorShortcutTags[e.Key]);
                    }
                    }
                    
                    private void ToggleIndicatorsVisibility(string targetDisplayName)
                    {
                    var indicator = ChartControl.Indicators.FirstOrDefault(indi => indi.Name == targetDisplayName);
                    
                    if (indicator != null)
                    {
                    indicator.IsVisible = !indicator.IsVisible; // Toggle visibility
                    }
                    // ChartControl.InvalidateVisual();
                    ForceRefresh();
                    }
                    ​And it works perfectly, but thank you for trying though. You've helped me before with advice.

                    Kind regards!

                    Comment


                      #11
                      Hi
                      you could also try this in the EcoSystem to see if it does what you want it to do.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by jxs_xrj, 01-12-2020, 09:49 AM
                      6 responses
                      3,290 views
                      1 like
                      Last Post jgualdronc  
                      Started by Touch-Ups, Today, 10:36 AM
                      0 responses
                      9 views
                      0 likes
                      Last Post Touch-Ups  
                      Started by geddyisodin, 04-25-2024, 05:20 AM
                      11 responses
                      62 views
                      0 likes
                      Last Post halgo_boulder  
                      Started by Option Whisperer, Today, 09:55 AM
                      0 responses
                      8 views
                      0 likes
                      Last Post Option Whisperer  
                      Started by halgo_boulder, 04-20-2024, 08:44 AM
                      2 responses
                      25 views
                      0 likes
                      Last Post halgo_boulder  
                      Working...
                      X