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:	171
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:
    I have class called Logger... I want to use this class inside indicator or strategy (ninjatrader objects). This Logger class should write text using Time[0],


    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 bortz, 11-06-2023, 08:04 AM
              47 responses
              1,603 views
              0 likes
              Last Post aligator  
              Started by jaybedreamin, Today, 05:56 PM
              0 responses
              8 views
              0 likes
              Last Post jaybedreamin  
              Started by DJ888, 04-16-2024, 06:09 PM
              6 responses
              18 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by Jon17, Today, 04:33 PM
              0 responses
              4 views
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              12 views
              0 likes
              Last Post Javierw.ok  
              Working...
              X