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

inheritance

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

    inheritance

    Click image for larger version

Name:	Q_10.png
Views:	26
Size:	19.2 KB
ID:	1216485
    Hello.
    I have one class D (strategy) that is responsible for managing orders (opening/closing positions).
    the logic of my program requires that I have strategies with different names ("one", "two", "three").
    that is, the code in the classes is the same except for the name of the strategy.

    if I could inherit from my main abstract class and only pass the name of my strategy to the constructor of the heir,
    then the matter could be considered solved.
    but all strategies already have inheritance in the form of the Strategy class.
    How does implement this class interaction in code?


    #2
    Hi Danila, thanks for writing in. Using any further inheritance structures goes outside of the scope of support we are able to provide. If you have code that you would like to use between multiple strategy files, consider creating this code as a partial class of Strategy. Here is an example using the Indicator class, you would change that to Strategy, and then you can use all of the code you write in the partial class in any script that inherits the strategy class:
    https://ninjatrader.com/support/foru...egy-into-class

    Kind regards,
    -ChrisL

    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris. Thanks for the quick response.
      I'm sorry I was not precise in my question.
      class D is the strategy that I want to put in another strategy,
      which would be just a wrapper for strategy D
      and give the opportunity to change the name of the underlying strategy D.
      or classes A B C are not Strategy. but class D is exactly Strategy.
      I find variants to pass class D(strategy) with different names to control panel.
      Click image for larger version  Name:	Q_10.png Views:	0 Size:	22.4 KB ID:	1216495
      Last edited by Danila; 09-21-2022, 09:47 AM.

      Comment


        #4
        Hi Danila, thanks for your reply. When you launch a strategy you will notice that the platform automatically creates Strategy objects for you, hence, we never call "new Strategy()" anywhere in our code. The system is expecting the object inherit from Strategy and Strategy only, so adding any more layers to this is unsupported and can break with little direction on what the error is because of the lack of open documentation on this subject.

        Kind regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris, thanks for your answer.
          could you be so kind as to tell me how can I avoid code duplication?
          my strategies differ only in name, and the rest of the 700-line code section is identical.
          And if let's say I have 10 strategies and I need to make changes, I need to make changes in 10 strategies instead of one.
          It says about the wrong architecture
          I cannot use the abstract class.
          Based on your answer composition too.
          How can I avoid code duplication?​

          Comment


            #6
            Hi Danila, You can avoid code duplication by making a partial Strategy class. All of the custom coded added to the Strategy class can be accessed for all strategies e.g.

            //in the Addons folder, add a .cs file with the code:

            Code:
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.ComponentModel.DataAnnotations;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows;
            using System.Windows.Input;
            using System.Windows.Media;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Gui;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Gui.SuperDom;
            using NinjaTrader.Gui.Tools;
            using NinjaTrader.Data;
            using NinjaTrader.NinjaScript;
            using NinjaTrader.Core.FloatingPoint;
            using NinjaTrader.NinjaScript.Indicators;
            using NinjaTrader.NinjaScript.DrawingTools;
            
            namespace NinjaTrader.NinjaScript.Strategies
            {
                 public partial class Strategy
                 {
                     public void ManipNumbers(StrategyBase sb)
                     {
                        NinjaTrader.Code.Output.Process("CurrentBar " + sb.CurrentBar, PrintTo.OutputTab1);
                        NinjaTrader.Code.Output.Process("Values " + (sb.High[0] - sb.Low[0]), PrintTo.OutputTab1);
                     }
                 }
            }​
            This method is now a part of the Strategy class, so we can use it in any strategy:
            Code:
            protected override void OnBarUpdate()
            {
                this.ManipNumbers(this);
            }
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Hi Chris! Thank You so much.
              You really helped me.
              Thanks again for the competent answer.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by futureszone, Today, 02:26 PM
              0 responses
              2 views
              0 likes
              Last Post futureszone  
              Started by ttodua, Today, 01:39 PM
              0 responses
              3 views
              0 likes
              Last Post ttodua
              by ttodua
               
              Started by carnitron, Today, 10:45 AM
              2 responses
              35 views
              0 likes
              Last Post carnitron  
              Started by dtaylor, Today, 01:13 PM
              0 responses
              6 views
              0 likes
              Last Post dtaylor
              by dtaylor
               
              Started by advgal, Today, 09:25 AM
              1 response
              17 views
              0 likes
              Last Post Soup Kitchen Jim  
              Working...
              X