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

Intermediate between StrategyBase and IndicatorBase

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

    Intermediate between StrategyBase and IndicatorBase

    Hi.
    I am creating custom library class, where I want to define custom "ToTime()" like function.

    i.e.

    Code:
    public int ToTime(DateTime dt){
        return ToTime(dt)/100;
    }
    however, of course IDE crashes, as `ToTime()` is unknown. Seems I have to pass the reference, like:

    Code:
    public int ToTime(IndicatorsBase ind, DateTime dt){
        return ind.ToTime(dt)/100;
    }
    however, ok, I can use that from indicators:

    Code:
    myClass.ToTime(this, Time[0]);
    however, when I call that from strategies, i get error (because `this` wont match IndicatorsBase, but it's now StrategyBase).

    SO, is there any intermediate for both of those, like:

    Code:
    public int ToTime(NinjaScriptBase ind_or_strat, DateTime dt){
        return ind_or_strat.ToTime(dt)/100;
    }

    #2
    Hello TazoTodua,

    You have a function that is calling itself.

    This is recursion.

    You should be returning int as the type is declared as int.

    ToTime is already a method declared in the NinjaScript base. I would recommend that you call your method something else.

    Also, I would recommend that you do not use any method or property names that have already been used to avoid ambiguity.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      thanks, but you answered only different thing that the main question.
      ok lets say the method name is `myToTime`.

      Comment


        #4
        Hello TazoTodua,

        With a method named MyToTime() that returns an int I would not be expecting any errors.

        May I have a screenshot of the error message you are getting using the updated method name and return?

        May I also have a screenshot of the code or may I test an export of a reduced script with only this code on my end?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Chelsea,
          I will make it more clear.

          I have an addon (library of methods there).

          there I have:

          myToTime(IndicatorBase obj, DateTime x){
          return obj.ToTime(dt)/100;
          }
          and I can call it from Indicators (onBarUpdate) like this:

          addons.mylib.myToTime(this, Time[0]);


          however I can't call that from strategies, because I get error, that the method Expects IndicatorBase, and passed StrategyBase. So, I wanted the method to be usable from both Indicators and Strategies.

          Comment


            #6
            Hi TazoTodua,

            This custom c# code is outside of what is supported by NinjaTrader.

            However, have tried creating a method that doesn't require the IndicatorBase or StrategyBase and simply reproduces the ToTime() method logic?

            Have you tried using the NinjaScriptBase instead?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Chelsea,, thank you, thats what I meant with the term "intermediate"... that's NinjaScriptBase, yeah ! thanks

              (but unfortunately, NinjaScriptBase seems not to have ChartControl property (in NT8) and I have still to create two duplicate methods, one with default .Indicator arguments and one for .Strategy...
              Last edited by ttodua; 04-12-2018, 03:26 AM.

              Comment


                #8
                Originally posted by TazoTodua View Post
                Chelsea,, thank you, thats what I meant with the term "intermediate"... that's NinjaScriptBase, yeah ! thanks

                (but unfortunately, NinjaScriptBase seems not to have ChartControl property (in NT8) and I have still to create two duplicate methods, one with default .Indicator arguments and one for .Strategy...
                Or you could just pass the method an object, and then in the method body check what kind of object was passed in and branch appropriately?

                Comment


                  #9
                  hm, interesting. So, you say declaration should be like :

                  public void myMethod(object indi_or_strat) {

                  if(indi_or_strat instanceOf Indicator) {

                  }

                  }
                  ?

                  Comment


                    #10
                    can you remind me, where is `NinjaScriptBase` namespace? i cant find that.

                    Comment


                      #11
                      Hello TazoTodua,

                      That was my mistake. NinjaScriptBase is for NinjaTrader 8 not NinjaTrader 7.

                      NT7 uses NinjaTrader.Indicator.IndicatorBase and NinjaTrader.Strategy.StrategyBase.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        yes, I was thinking about that, couldnt find.
                        ok, thanks.

                        so there is no intermediate in NT7

                        Comment


                          #13
                          Originally posted by TazoTodua View Post
                          when I call that from strategies
                          This suggestion requires a fair amount of .NET & object oriented knowledge and some advanced coding skills.

                          Using NT7, take a careful look at the code in "bin/Custom/Strategy/@Strategy.cs".

                          Inside this file you will find a variable "_indicator" which happens to be of type "ADX".

                          That specific detail does not matter, the idea is not to access the parts of the ADX class through the "_indicator" variable, per se, but access the parts of ADX's base class, which happens to be "Indicator" class -- (that's the purpose of instantiating "_indicator" as ADX, you have to instantiate something, right? any class derived from "Indicator" would work, nothing special about choosing ADX over say, EMA) -- the ADX class is just a middle man class in order to get to the "Indicator" base class properties.

                          But you can also get to Indicator base class methods, too, from a strategy, via use of that "_indicator" global variable.

                          If you put your personal library methods in "bin/Custom/Indicator/UserDefinedMethods.cs" -- you should be able to access those same methods from a strategy (using the "_indicator." prefix) from file "bin/Custom/Strategy/UserDefinedMethods.cs".

                          The idea is: keep your personal code in file "Indicator/UserDefinedMethods.cs" (this file is already supplied just for this purpose) then write short wrappers that call that personal code via the variable "_indicator." in file "Strategy/UserDefinedMethods.cs".

                          Again, look at @Strategy.cs for some examples of using the "_indicator." prefix to access methods and properties from the "Indicator" class.

                          As a final recommendation, uh, don't use the actual filenames UserDefinedMethods.cs. Use two different filenames, for ex, try using something like,

                          bltBaseIndicator.cs - locate this file in bin/Custom/Indicator
                          bltBaseStrategy.cs - locate this file in bin/Custom/Strategy

                          (the 'blt' prefix is a placeholder, just pick a personal prefix, such as your initials, this helps to define personalized versions of files, methods, properties, etc)

                          These files contain your personal methods and properties you wish to define.

                          So why avoid using UserDefinedMethods.cs directly?
                          The idea is: do not pollute UserDefinedMethods.cs, just in case someday something else overwrites or resets this file. Copy the contents of what you would have put in "Indicator/UserDefinedMethods.cs" into a brand new file, then create a Strategy version of that file that has wrappers that call your personal Indicator methods and properties deferenced through the "_indicator" Strategy variable. Use custom filenames for your custom personal code is simply the better way to go.

                          I'm sure most of this is somewhat unsupported from the point of view of NinjaTrader support, but the two files UserDefinedMethods.cs should absolutely pre-exist and the concept of what they provide is absolutely supported.

                          You just have to have the programming skills to implement it yourself.
                          Last edited by bltdavid; 05-09-2018, 01:23 PM.

                          Comment


                            #14
                            1) bltDavid, thanks for your suggestion. i without question agree with custom file-naming. however, i have to mention that I have only 1 file (My_Methods.cs) in `indicators` folder, and i never needed (and no-one needs to say frankly) to have a separate `.cs` in strategy folder, as opposed to what you suggeted..
                            in `Indicators/My_Methods.cs` i have all namespaces inside, which i need, and i call them from strategies too. I dont know what is the point to have separate one in `strategy` folder.

                            2) ChelseaB (or AnyOne) can you answer 1 additional question?

                            I use the intermediate `NinjaScriptBase`, like this:

                            public void xyz(NinjaTrader.NinjaScript.NinjaScriptBase ns)
                            {
                            ns.Print("hello");
                            }


                            and i can call that from Indicators/strategies, like this:

                            xyz(this);

                            however, `ns` instance is a bit poor than `NinjaTrader.NinjaScript.IndicatorBase` type. for example:

                            public void xyz(NinjaTrader.NinjaScript.NinjaScriptBase ns)
                            {
                            ns.ChartControl <----- error. ChartControl doesnt exist;

                            (ns as NinjaTrader.NinjaScript.IndicatorBase).ChartContro l <------- ok!
                            }



                            I am a bit annoyed writing the same methods double-time - one for specifically indicator, and one for strategies, like:

                            public void xyz(NinjaTrader.NinjaScript.NinjaScriptBase ns)
                            {
                            ChartControl ch = null;
                            if (ns is NinjaTrader.NinjaScript.IndicatorBase) ch =( (NinjaTrader.NinjaScript.IndicatorBase) ns).ChartControl;
                            if (ns is NinjaTrader.NinjaScript.StrategyBase) ch =( (NinjaTrader.NinjaScript.StrategyBase) ns).ChartControl;

                            //now i can call any properties of methods of `ch `)
                            ch.ActualHeight...
                            ch.RenderTransform()...



                            UserControlCollection uc = null;
                            if (ns is NinjaTrader.NinjaScript.IndicatorBase) uc = ((NinjaTrader.NinjaScript.IndicatorBase) ns).UserControlCollection ;
                            if (ns is NinjaTrader.NinjaScript.StrategyBase) uc = ((NinjaTrader.NinjaScript.StrategyBase) ns).UserControlCollection ;

                            //now i can call any properties of methods of `ch `)
                            uc.Insert (or use dispatcher with this, to inert buttons on chart)...

                            }



                            My question, or maybe feature suggestion is, if it's possible to have 1 intermediate class, which will ALSO CONTAIN THE INDICATOR/STRATEGY SPECIFIC FUNCTIONS, LIKE "UserControlCollection", "ChartControl" and etc... (like NinjaScriptBase already contains the `Print` or other shared methods). so, we wouldnt have to write same codes 2 times.

                            Comment


                              #15
                              Hello TazoTodua,

                              NT7 was not designed for this.. It's intended that the indicator and strategy methods would be separate..

                              This thread will remain open for any community members that would like to assist.

                              You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.


                              That said, I am further researching to see if this is possible.

                              My current thinking is the series will need to be passed as a parameter. But if there is a higher class that we can extend from I'm fairly sure I will find it.
                              I will let you know anything I find.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by funk10101, Today, 08:14 AM
                              2 responses
                              3 views
                              0 likes
                              Last Post funk10101  
                              Started by samish18, Yesterday, 08:57 AM
                              11 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by yertle, Today, 08:38 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by love2code2trade, Yesterday, 01:45 PM
                              3 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by trilliantrader, Today, 08:16 AM
                              2 responses
                              7 views
                              0 likes
                              Last Post trilliantrader  
                              Working...
                              X