Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Call ReloadNinjaScript Action after hotkey is hit

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

    Call ReloadNinjaScript Action after hotkey is hit

    Hi, I have this script for a custom indicator, where I draw a rectangle and text on my chart, when hitting a hotkey:
    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class EMAChartToggle : Indicator
        {
            private SimpleFont    largeFont;        
    
            [NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "In Trade - Chart off", GroupName = "Make chart invisible", Order = 0)]
            public bool ChartOff { get; set; }
    
            [NinjaScriptProperty]
            [Display(Name = "Hotkey", GroupName = "Make chart invisible", Order = 1)]
            public string Hotkey { get; set; }
    
    
            protected override void OnStateChange()
            {
                    if (State == State.SetDefaults)
                    {
                    Name = "EMA Chart Toggle";
                    IsOverlay = true;
                    largeFont = new Gui.Tools.SimpleFont("Arial", 20);
                    ChartOff = false;
                    Hotkey = null;
                    Calculate = Calculate.OnBarClose;
                }
    
                else if (State == State.Configure)
                {
                    // Attach key press event to chart control
                    ChartControl chartControl = ChartPanel.ChartControl;
                    if (chartControl != null)
                    {
                        chartControl.PreviewKeyDown += OnPreviewKeyDown;
                    }
                }
                else if (State == State.Terminated)
                {            
                    largeFont = null;
                    if (ChartPanel != null && ChartPanel.ChartControl != null)
                    {
                    ChartPanel.ChartControl.PreviewKeyDown -= OnPreviewKeyDown;    
                    }
                }
            }
            protected override void OnBarUpdate()
            {
    
                if (CurrentBar < 100)
                {
                    return;
                }
    
                if (ChartOff)
                {
                    int barsAgo = 100;
                    double highestHigh = MAX(High, barsAgo)[0];
                    double lowestLow = MIN(Low, barsAgo)[0];
                    Draw.Rectangle(this, "ChartOffRectangle", false, barsAgo, lowestLow, -15, highestHigh, Brushes.Transparent, Brushes.White, 100);
                    Draw.TextFixed(this, "ChartOffText", " IN TRADE ", TextPosition.Center, Brushes.Black, largeFont, Brushes.Transparent, Brushes.Lime, 100);
    
                }
                else
                    {
                    RemoveDrawObject("ChartOffRectangle");
                    RemoveDrawObject("ChartOffText");
                }
            }
    
            private void OnPreviewKeyDown(object sender, KeyEventArgs e)
            {
                Key hotkey;
                if (Enum.TryParse(Hotkey, out hotkey) && e.Key == hotkey)
                {
                    ChartOff = !ChartOff;
                }
            }
        }
    }    ​​
    But the rectangle and text is not drawn until I relaod the NinjaScript on the chart. So I would like to add also the ReloadNinjaScript action to my code or any other function, that would relaod the Ninjascript after the designated hotkey is hit. I have checked various other threads in this and other forums but did not find anything that would do the job. There was also some advice to call the F5 key by using "SendKeys.SendWait("{F5}");" at the end of the code, which would reload NinjaScript on the chart. But that causes the compiler to stop with errors: "The name SendKeys does not exist in the current context."
    Can anyone help?

    Thanks
    Peter

    #2
    Hi cyberpete, thanks for your post. The reload will need to be completed using SendKeys because there is no other documented way to programmatically reload a script. You must add "using System.Windows.Forms" to your using directives at the top of the script to use this. There are a few examples that implement custom hot keys on the NinjaTrader Ecosystem website:



    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

    Comment


      #3
      Thank you Chris, for your reply. I added this line: System.Windows.Forms.SendKeys.SendWait("{F5}"); to my code. The compiler is OK with that. But the behavior on the chart is quite weird:
      I would expect, by hitting the designated hotkey, e.g. F7, the code would draw the rectangle and text onto the chart, as after the hotkey is hit under private void OnPreviewKeyDown(object sender, KeyEventArgs e), it also triggers the F5 key by this line: System.Windows.Forms.SendKeys.SendWait("{F5}"); and that would cause the script to reload on the chart. But if I hit the F7 key once, the rectangle and text appear for a very quick moment and dissapear again, as the script is reloaded to the State.SetDefaults, meaning ChartOff = false. But if I hit the F7 key quickly twice, then I get the desired result: quickly twice for the first time draws both the rectangle and the text onto the chart. Quickly twice hitting F7 (hotkey) for the second time removes both objects. I would expect that to work just for one keystroke. Any idea?

      Comment


        #4
        Hi Pete, unfortunately, this goes outside of the scope of support I am able to provide on the subject.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        571 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        330 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        548 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        548 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X