Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Programmatically access and change the properties for a Strategy while it is running

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

    Programmatically access and change the properties for a Strategy while it is running

    Hello:

    I have a custom strategy with a few Properties (Inputs). I have also developed an AddOn in C# and it works well. Will i be able to access the instance of my custom strategy that is running from the AddOn code, say for example a button click and change the values of the Properties.

    I see there is a StrategyBase object and it has properties like Variable0, Variable1, ... But it is not clear to me on how to use this StrategyBase object to manipulate the particular instance of my custom strategy that is currently running.


    Thanks
    Ram
    Last edited by ntram; 07-06-2023, 05:10 PM.

    #2
    Hello Ram,

    You have posted in the NinjaTrader 7 section of the forums, is this script for NinjaTrader 7?

    Unfortunately, there is not documented or supported code to access running NinjaScripts from an addon. With NinjaTrader 8, there is a unsupported code sample of finding active indicators that might work for strategies. Let me know if you have interest in NinjaTrader 8.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      Thank you for your response. I didnt realize I posted my message in NinjaTrader 7 section. I am actually using NinjaTrader version 8.1.1.7. Please give me more details about what you were referring to as the undocumented approach. I am very interested.

      Thanks

      Comment


        #5
        Hello Chelsea,

        Thank you very much for pointing me in the right direction - this approach worked for strategies too - It is a shame this is an undocumented approach and could all stop working someday. I would have spent endless hours without realizing that the ChartControl has all the strategy references. It is quite well documented in the help docs, but it is in a very unstructured manner that makes it almost impossible to find. Thank you again.

        If anyone else is interested here is a snippet of code that you can use to get a reference to the running strategy and then manipulate its properties. The key is to declare your reference as dynamic. This will allow you to set properties and call methods on your strategy object while still being able to compile in Visual Studio.


        Code:
        private dynamic myStrat = null;
        private string strategyName = "MyTestStrategy";
        
        private async void GetStrategyReference()
        {
            foreach (Window window in Globals.AllWindows)
            {
                NTWindow w = (NTWindow)window;
                await window.Dispatcher.InvokeAsync(new Action(() =>
                {
                    if (w.Caption.Contains("Chart"))
                    {
                        foreach (ChartControl cc in FindVisualChildren<ChartControl>(w))
                        {
                            myStrat = cc.Strategies.Where(x => x.Name == strategyName).FirstOrDefault();
                            if (myStrat != null)
                            {
                                break;
                            }
                        }
                    }
                }));
            }
        
            myStrat.Print("Found Strategy - StrategyId: " + myStrat.Id.ToString());
        }
        
        
        private void TestStrategyAccess()
        {
            //now you can access the properties and methods of your custom strategy
            myStrat.TrailingTicks = 30;
        }​​

        Comment

        Latest Posts

        Collapse

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