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

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 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
        8 views
        0 likes
        Last Post Touch-Ups  
        Started by geddyisodin, 04-25-2024, 05:20 AM
        8 responses
        61 views
        0 likes
        Last Post NinjaTrader_Gaby  
        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
        24 views
        0 likes
        Last Post halgo_boulder  
        Working...
        X