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

Building a class with NT indicator

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

    Building a class with NT indicator

    Hello together,
    guess I have a stupid question, but I am not that experienced in building classes.

    I have a working method and I try to put this method into a class. Everything works fine till I get to the line with the ZZ Indicator.

    public class Class_ZZ_Test{

    private Indicator Indicator = new Indicator();

    public void DefineZigZag(){

    Indicator.Log("Test Begin",LogLevel.Information);
    int ZZhigh_ago = Indicator.ZigZag(DeviationType.Points, 0.1, true).HighBar(0, 1, 100);
    int ZZlow_ago = Indicator.ZigZag(DeviationType.Points, 0.1, true).LowBar(0, 1, 100);
    Indicator.Log("Test End",LogLevel.Information);

    double ZZhighValue = Indicator.ZigZag(DeviationType.Points, 0.1, true).ZigZagHigh[ZZhigh_ago];
    double ZZlowValue = Indicator.ZigZag(DeviationType.Points, 0.1, true).ZigZagLow[ZZlow_ago];
    }


    ~Class_ZZ_Test() { }


    }
    I get an log entry "Test Begin", but the line:
    int ZZhigh_ago = Indicator.ZigZag(DeviationType.Points, 0.1, true).HighBar(0, 1, 100);
    throws an exception, which is so hard, that I have to use try / catch. Otherwise NT 8 crash.

    What is my mistake? Is it not possible to call the ZZ Indicator inside a class?

    Thanks for your help
    uupsa
    Last edited by uupsa; 02-10-2016, 10:13 AM.

    #2
    Hello,

    Thank you for the question.

    It is hard to say what specifically is happening based on what has been provided, I do see you are creating a new instance of a the base indicator class Indicator:

    Code:
    private Indicator Indicator = new Indicator();
    and later using the new instance and calling the ZigZag from it:

    Code:
    Indicator.ZigZag
    I am unsure on this specifically as this would not be the standard approach at calling an indicator so it could potentially be causing the problem.

    Could you create a simple example of just this logic and export it so I could run on my end to see what may be occurring?

    I would like to see exactly how you are trying to use the class from where it is being called and so on to better understand what may be causing the crash.

    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      thanks for your reply. I tried to export an example, but it didn´t work. Get an error message. Playing with the situation I have following conclusion:

      It is possible to include the "Log" function into a class via the terms:
      private Indicator Indicator = new Indicator();
      or
      private Strategy Strategy = new Strategy();
      That´s why the Log function works inside a class. See "Test Begin"

      But it is not possible to include any indicator, instrument data, etc.. To include such data into the class, I have to do it via the "set" term. For example:
      Class_ZZ_Test.nameInstrument = Instrument.MasterInstrument.Name;
      or calculate the ZigZags outside the class and send it to the class via a list like:
      Class_ZZ_Test.ZigZagList = ListZigZag;
      Is there any possibility to work with methods like Indicators or Instrument data inside the class? So that I can do calculations inside the class?

      Perhaps with inherits like:

      Class_ZZ_Test : Strategy;
      Thanks for your help!

      Regards
      uupsa
      Last edited by uupsa; 02-11-2016, 05:07 AM.

      Comment


        #4
        Hello,

        Thank you for the reply.

        Without further context of this class you are speaking of and where you are calling it from its hard to say if that would be possible.

        You are avoiding the internal indicator logic by creating a new instance yourself, because it was not created by internal processes like the indicators menu or calling it without the new modifier, the object likely lacks most of its inherited properties or they are null and not associated with a specific object like a chart.

        If you are calling this class from a strategy or indicator, you could potentially use the overloads and pass in the indicator when you create your class using its constructor.

        You would need to provide a sample of the structure you are using for a more clear answer on this, for support purposes we don't need to see any of your custom code but just the structure you are trying to accomplish. If you could create a new sample script that only contains the basic class structure and how you are calling that class it would be more apparent what the answer is.

        Additionally if you would like we could review the error message you had on export to see what that is about if you would like to post the log message you received.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          here is an example. Basically I am looking for a possibility to work with indicators, bardata, etc. in custom made classes. So far only the log function works...

          Thanks
          uupsa
          Attached Files

          Comment


            #6
            Originally posted by uupsa View Post
            Hello Jesse,

            here is an example. Basically I am looking for a possibility to work with indicators, bardata, etc. in custom made classes. So far only the log function works...

            Thanks
            uupsa
            Yeah, but your custom made class should inherit from Indicator or from Strategy ...

            You may not have the NinjaTrader call model well understood.

            You do not directly call new to obtain a reference for an strategy or indicator object in your class. That's not how it works.

            The user of the NinjaTrader application creates these objects themselves by interacting with the application. For ex, a user right clicks and adds an indicator to a chart. It is NinjaTrader's job to instantiate that object -- the indicator provides a well-defined set of call back methods with very specific names (such as OnBarUpdate), and NinjaTrader itself calls those methods through the indicator reference.

            Same goes for a strategy. You, the programmer, do not instantiate the strategy object. The user does this through their use of the NinjaTrader application.

            Classes that inherit from Indicator and Strategy are treated more like plugins.

            There are many references and examples in the forum.
            Have you specific questions about any of those examples?

            If you want to "extend" the Strategy or Indicator class by creating a "middle man" class, then just use .NET inheritance and do that,

            Let's say you want a class called CoolStrategy that contains all your utility functions, so we start with,

            Code:
            namespace NinjaTrader.Strategy
            {
                abstract public class CoolStrategy : Strategy
                {
                   protected void SayHello()
                   {
                      Print("Hello World");
                   }
                }
            }
            This class "sits in the middle" of your real class.

            Code:
            namespace NinjaTrader.Strategy
            {
                public class MyStrategy : CoolStrategy
                {
                   protected override void Initialize()
                   {
                      SayHello();
                   }
                }
            }
            NinjaTrader creates an object of type "MyStrategy" which inherits from "CoolStrategy" but which ultimately inherits from "Strategy". The CoolStrategy class is a sort of a "middle man" class because it extends the Strategy class for the benefit of the MyStratgy class -- its sits in the middle of the other two.

            Is this what you're asking?
            Last edited by bltdavid; 02-13-2016, 10:51 AM.

            Comment


              #7
              If you want to get a reference to the indicator from inside a strategy, see my example in this post:

              Comment


                #8
                Hello David,

                sometimes it is so easy! Thanks for your help!

                Regards
                uupsa!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by usazencort, Today, 01:16 AM
                0 responses
                1 view
                0 likes
                Last Post usazencort  
                Started by kaywai, 09-01-2023, 08:44 PM
                5 responses
                603 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by xiinteractive, 04-09-2024, 08:08 AM
                6 responses
                22 views
                0 likes
                Last Post xiinteractive  
                Started by Pattontje, Yesterday, 02:10 PM
                2 responses
                21 views
                0 likes
                Last Post Pattontje  
                Started by flybuzz, 04-21-2024, 04:07 PM
                17 responses
                230 views
                0 likes
                Last Post TradingLoss  
                Working...
                X