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

Custom Types/Class in NinjaScript?

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

    Custom Types/Class in NinjaScript?

    Hi,

    Totally new to NinjaTrader and have a requirement to re-develop an existing custom MT4/5 EA/Indicator for NT.

    The MT application is developed as a full OOP based implementation using their psuedo-C++ language and MT base classes. The EA/Indicator template(s) allows me to instantiate the necessary custom objects that do all of the work, as opposed to using libraries of functions in the global memory of the EA (or Indicator) template file.

    I've looked through the NinjaScript documentation but I cannot see that NinjaScript allows you to create custom C# classes/structs? What does NinjaScript provide that would allow me to create my own encapsulated data/functionality?

    Many thanks indeed for any help and advice.

    #2
    Hello Geester,

    Welcome to the NinjaTrader support forum.

    As NinjaScript is just C#, you can use classes and other C# types just like any other C# application. NT8 uses .net 4.5 and can compile standard .net 4.5 code in addition to supporting dll references. NinjaScript or NinjaTrader I should say doesnt generally document standard C# concepts like OOP in C# as these are best documented with MSDN or other tutorial websites.

    NinjaTrader does do other bacground processes such as XML serialization so you will need to test whatever you create to make sure there are no errors in the various use cases. The Control Center log tab can be used to check for errors after saving worksapces, running scripts, etc.

    I would advise some caution at using complex inheritance for your items. When exporting either as source or a compiled assembly this can complicate the exporting process from the platform. This is another item to reguraly check while you develop the item to make sure it can export successfully without error. In some cases when using OOP and extra classes or enums, you will need to use the fully qualified names instead of using statements to avoid export errors. This is a good resource for exporting information: https://ninjatrader.com/support/help...stribution.htm

    Outside of these considerations, using OOP in NinajScript is exactly the same as it would be in a standard C# application.

    One example we provide that has some OOP is the following level2 data example which includes a class: https://ninjatrader.com/support/foru...ead.php?t=3478

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

    Comment


      #3
      Hi Jesse,

      Many thanks indeed for your reply. I note now that if I developed my own C# classes, it appears that NT could reference them and use their functionality. In MetaTrader, when you develop a custom class, you have access to TimeSeries arrays and other general MT functions. If you develop C# classes, how do you access NT data from inside a custom class? Is it just a matter of referencing NT namespaces in the custom classes?

      One major reason that I had to use OOP in MT was simply that a struct couldn't reference certain datatypes (strings/arrays). If I wanted to essentially create "records" (tuples) containing mixed data, for example, a "pivot" or "fractal" candidate, I would need to hold certain pieces of information about the pivot/fractal. Because these pivot candidate objects were only confirmed when certain "rules" had been confirmed several bars after the pivot, I needed to be able to iterate over a collection of these pivot candidate objects to see whether, on the close of subsequent bars, the candidate confimed or failed. Using these candidate objects, trend lines would be created. This task is very much like the classic "ZigZag" indicator except that the fractals need to be subject to rules based on subsequent bar formations. Also, some fractals/trendlines are later deleted if, for example, a higher fractal develops without a confirmed lower fractal occuring - that sort of thing.

      The only collection type in basic MT is the array that can be of a certain data type. Given the limitations around MT structs, I therefore had to create classes to effectively represent data types which then allowed collections of said custom types.

      I can't imagine that I am doing anything that different than many complex NT indicators. Is there a more straightforward way to accomplish the sort of thing I am trying to do in NinjaScript without using custom C# classes? IOW, is there some way of creating custom types for using in C# generic lists/arrays? eg., var myTypes = new List<MyCustomType>();

      Sorry for the length of this reply. I just thought it might be beneficial to outline what I am currently doing before going off down a rabbit hole only to find there is a neat solution/technique in NinjaScript for the type of thing(s) I am currently using OOP for in MT.

      Looking forward to you reply.

      Regards and thanks!

      Comment


        #4
        Hello Geester,

        Thank you for the reply.

        how do you access NT data from inside a custom class? Is it just a matter of referencing NT namespaces in the custom classes?
        Depending on what you are accessing, you will either need a reference to an object or if the item in question is avaliable as a static method, you can call the method directly. This will be no different than any other C# application and accessing objects. If you take a close look at an Indciator this will provide a good example of this concept. all indicators classes inherit from : Indciator. This means they have access to all the specific indicator variables and methods such as Print().

        As another example: Consider that you have a custom class with a method that your indicator will call. Inside that method it uses the NinjaScript Print function. This would fail because Print is inherited from Indicator. Your class cannot inherit from Indciator without becoming an indicator its self which is not what we want, so the solution would be either to use the static print method which is Output.Process or to pass a reference to the parent indicator. You could add a parameter to the method that accepts the type Indciator and you could pass "this" as the methods overload. In the method, you would then use the passed in indicator such as indicator.Print(...); to call the inherited NinjaScript method.

        There are utility functions built into NinjaScript such as Output.Process which allows for some NinjaScript items to be accessed all over. Here is one example of Output.Process:



        Is there a more straightforward way to accomplish the sort of thing I am trying to do in NinjaScript without using custom C# classes?
        Yes very likely. It sounds like in MT you were very limited on types that could be used which forced the creation of the structure you ended up with. Because you are now using C#, you have virtually limitless types or generics that can be used.

        As with any API, you will need to read the manual to become aware of the tools you have available in NinjaScript. What I always suggest is to first study the existing NinjaScript items in the NinjaScript editor and also the Help guide so you know what tools are avaialbe for you to use. This would help in planning your structures and how you need to access things.

        You will very likely need to form a plan on what your current code base accomplishes and look for items in the platform that can account for parts of the total logic does. You could then piece together what is needed in a more effecient way using C# and NinjaScript types.

        Regarding the syntax provided, that would actually be valid:
        Code:
        List<MyCustomType> myTypes = new List<MyCustomType>();
        if MyCustomType is a class with some properties, you could make a List from it and populate it.


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

        Comment


          #5
          Hi Jesse,

          Regarding the syntax provided, that would actually be valid:
          Code:
          List<MyCustomType> myTypes = new List<MyCustomType>();
          if MyCustomType is a class with some properties, you could make a List from it and populate it.
          Right, so, in standard NinjaScript, I can create a C# class and be able to use standard C# generics. Is there a specific place in NinjaScript that you locate classes of this type?

          If I can create C# classes for use as generic types, I could also create classes that encapsulate behaviours too. I didn't want to have to create subclasses of everything in NinjaTrader - just use regular NinjaTrader but be able to use my own classes in the mix.

          Also, if there are static methods in the NinjaTrader classes or as you said, I can pass a reference to a NinjaTrader object in to my own custom class(es), that would also be a fantastic solution.

          Many thanks indeed.

          --Gary

          Comment


            #6
            Hello Geester,
            Thank you for the reply.

            Right, so, in standard NinjaScript, I can create a C# class and be able to use standard C# generics. Is there a specific place in NinjaScript that you locate classes of this type?
            Perhaps I am not understanding this question, the Class would be located where you placed it. For example if you make MyCustomClass inside of your indicators class, MyCustomClass can be located inside of that indicator class by using the full namespace name to it.

            NinjaTrader.NinjaScript.Indicators.MyIndicatorsNam e.MyCustomClass
            Is this what you are asking?


            If I can create C# classes for use as generic types, I could also create classes that encapsulate behaviours too. I didn't want to have to create subclasses of everything in NinjaTrader - just use regular NinjaTrader but be able to use my own classes in the mix.
            Yes I wouldnt suggest trying to create subclasses for existing types if it can be avoided or re invent the wheel so to speak. I would still suggest reviewing the existing items before you start creating structures. NinjaScript is set up to allow the user to use as few lines of code as possible to accomplish tasks. You can create complicated structures if nessasary but in most cases, you dont need to in NinjaScript. There are many existing classes and functions along with system/custom indicators to accomplish a variety of tasks.

            Most often it would be suggested to "encapuslate" your various calculation functions into different indicators as there is already a system in place for NinjaScript to call other indicators with no extra structures needed.

            If you approach this conversion as taking what you have one for one into NinjaTrader, you will likely miss a lot of the NinjaScript specific API methods meaning more work and less effeciency when run in NinjaTrader.
            If you instead look at this as an oppurtinuity to use a new set of tools to re imagine your idea, you will likely be able to use more of the NinjaScript tools and workflows that already exist to your advantage.

            There will likely be cases where you do need to directly copy concepts, and for those cases I can suggest looking on google at C# ways to accomplish those tasks. The support forum is also an excellent resource to ask how you may execute a specific concept in NinjaScript, often we have a sample.

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

            Comment


              #7
              Hi Jesse,

              When I have worked with C# in the past, I nearly always created a C# class in an individual file and those files were created in folders inside of the VS project. Because I assume I wouldn't be using VS but, a NinjaSript IDE, I just wondered whether there was a specific location to put such files so that NinjaScript could locate them at the compilation stage.

              I think you are saying that you can create an indicator class in NinjaScript and in the same file, specify the custom class definition(s). If so, that too would be OK - it's just like I mentioned, the coding standards in previous C# projects dictate a single class per file. In VS, you can add classes pretty much anywhere in the project and VS "knows" where they are at the compile/build stage. Sorry to labour this point! I think we are talking about the same thing

              One final thing before I get stuck in to this task - does the Ninja editor support debugging?

              Cheers!

              --Gary

              Comment


                #8
                Hello Geester,

                When I have worked with C# in the past, I nearly always created a C# class in an individual file and those files were created in folders inside of the VS project. Because I assume I wouldn't be using VS but, a NinjaSript IDE, I just wondered whether there was a specific location to put such files so that NinjaScript could locate them at the compilation stage.
                I see, thanks for rewording that. Yes in NinjaTrader you do the same thing actually. Your code can be in individual .cs files or inside of your indicator/NinjaScript file, so long as it is the NinjaTrader.Custom project it is compiled.

                To get a little more techical, If you use VS already you may be interested to view how the NinjaTrader.Custom project is composed. In reality when using NinjaTrader we are always using a VS project. All of the NinjaScript code and references are part of a Visual Studio Project that NinjaTrader interfaces with. This project is used to compile in the platform, and also allows for VS to be used with NinjaTrader. This also allows for debugging of NinjaScript using Visual Studio, the NinjaScript editor can only report errors but not debug.

                You can view the project in VIsual Studio by opening a NijnaScript editor, and click the Visual Studio toolbar button to launch the project. Alternatively, you can open the project manually from: Documents\NinjaTrader 8\bin\Custom\NinjaTrader.Custom.csproj



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

                Comment


                  #9
                  Hi Jesse,

                  Many thanks indeed for clarifying everything. It seems so obvious when I finally make myself clear!

                  I'm now very certain that I can easily achieve what I need to do. C# is indeed a very nice, modern language and it is extremely powerful right off the bat. The MT C++ code is powerful but it still uses arcane pointers (descriptors) instead of just being able to pass "this" around, and for every type I created, I had to create copy constructors and overload the "=" operator just so objects can be assigned. Really laborious and when in trouble, it's not actually a true C++ implementation so, you can't just look at MS C++ resources for solutions.

                  Anyhow, I've taken enough of your time for now. You've been awesome and I very much appreciate your time and patience. I suspect most of my forthcoming questions will be "run of the mill" NinjaScripts basics around how to interact with NT itself!

                  Cheers

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by kujista, Today, 05:44 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post kujista
                  by kujista
                   
                  Started by ZenCortexCLICK, Today, 04:58 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post ZenCortexCLICK  
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  172 responses
                  2,281 views
                  0 likes
                  Last Post sidlercom80  
                  Started by Irukandji, Yesterday, 02:53 AM
                  2 responses
                  18 views
                  0 likes
                  Last Post Irukandji  
                  Started by adeelshahzad, Today, 03:54 AM
                  0 responses
                  8 views
                  0 likes
                  Last Post adeelshahzad  
                  Working...
                  X