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


        #4
        Hello Ram,

        Below are links to the unsupported code samples.
        https://forum.ninjatrader.com/forum/...943#post687943
        https://forum.ninjatrader.com/forum/...883#post717883
        Chelsea B.NinjaTrader Customer Service

        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 Schiavo1, Today, 04:51 PM
          0 responses
          3 views
          0 likes
          Last Post Schiavo1  
          Started by volIQ, Today, 04:20 PM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_Emily  
          Started by Dr Kerry, 11-27-2023, 04:22 AM
          7 responses
          56 views
          0 likes
          Last Post Dr Kerry  
          Started by tkaboris, Yesterday, 01:10 PM
          10 responses
          44 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by BIOK.NT, Today, 02:36 AM
          3 responses
          30 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X