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

How to create custom class with access to NT's methods?

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

    How to create custom class with access to NT's methods?

    I have some questions about how best to organize my custom classes and methods so that they have access to NinjaScript's instance methods while still being able to create instances of my own class.

    I'm running into problems in creating a class that holds, imports, and exports bar data (BarDataIO). Specifically, I'm getting CS0038 or CS0120 errors because I'm either creating a new instance of it in OnBarUpdate() or moving it out of MyStrategy : Strategy and losing access to things like Close[0].

    Could you please provide some guidance and an outline of how and where to put custom classes within namespace NinjaTrader.NinjaScript.Strategies and MyStrategy : Strategy?

    I'm new to coding so I might be overlooking something simple like having to create a new instance of my strategy or inheriting from something else but I'm just guessing. I did create an instance of my strategy but then Close[0] seemed to be out of range...

    Thanks!

    #2
    Hello hillborne,

    May I ask for more details about what it is you are trying to do exactly? For example are you trying to access a data series calculated in an indicator via a strategy?

    Perhaps you could share the strategy/indicator you are working on so I may better answer your question.

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,

      For a very minimal example (because I want to record and work with a lot more data) here is an example class:

      Code:
      public class BarDataIO
      	{
      		string message = "This is the information to write: ";
      			
      		int barNumber;
      			
      		public void Report()
      		{
      			barNumber = CurrentBar;
      			Print(message + barNumber);
      		}
      	}
      If I have it within MyStrategy : Strategy, I get CS0038 errors: Cannot access a non-static member of outer type 'NinjaTrader.NinjaScript.NinjaScriptBase' via nested type.

      If I move it out of MyStrategy, then CurrentBar, Close[0], Print, etc. don't have context or an object to work on.

      Any suggestions on how to create a 'data holding' class used to import/export bar data?

      Comment


        #4
        Hello hillborne,

        You should move the class outside your strategy class.

        Below is a link for an indicator I created which has a custom public class, (line 581 of the code), which stores data time and open interest data, for which you could reference.

        http://ninjatrader.com/support/forum...d=7&linkid=922

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Here is one of the ways:
          http://ninjatrader.com/support/forum...11&postcount=8
          fx.practic
          NinjaTrader Ecosystem Vendor - fx.practic

          Comment


            #6
            Originally posted by NinjaTrader_AlanP View Post
            Hello hillborne,

            You should move the class outside your strategy class.

            Below is a link for an indicator I created which has a custom public class, (line 581 of the code), which stores data time and open interest data, for which you could reference.

            http://ninjatrader.com/support/forum...d=7&linkid=922

            Please let us know if you need further assistance.
            Hi, the above link just brings me to the general page of the User App Share. Can you provide the name of the indicator?

            Thanks.

            Comment


              #7
              Hello Rainmakersg,

              The old links to the forum AddOns redirect to the User App Share and if there are not any context clues, it can be difficult to find that script on the App Share.

              The link below is to an example that describes creating helper methods within the AddOn folder.

              https://ninjatrader.com/support/foru...est#post733956

              If you want to access items from that strategy inside the helper method or helper class, you can include an argument for NinjaScriptBase, which would allow you to pass the NinjaScript that calls the method to that helper method/class.

              I.E.

              Code:
              void MyHelper(NinjaScriptBase owningNinjaScript)
              {
                  Print(owningNinjaScript.CurrentBar);
              }
              JimNinjaTrader Customer Service

              Comment


                #8
                Hi Jim, thanks for your reply. I was actually looking for the indicator which "has a custom public class, (line 581 of the code), which stores data time and open interest data". Do you have any indicator available that has a custom public class for me to reference?

                Thanks so much.

                Comment


                  #9
                  Hello Rainmakersg,

                  I had a look for his Open Interest indicator, which I have linked below. He may have updated the code since his last post here, because it looks like the public class he is mentioning is on line 750.

                  https://ninjatraderecosystem.com/use...nterest-daily/

                  The link above is publicly available.

                  The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
                  I have also included some publicly available C# materials on classes.


                  https://www.tutorialsteacher.com/csharp/csharp-class

                  https://docs.microsoft.com/en-us/dot.../types/classes

                  JimNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Ousher, Yesterday, 11:12 PM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by You4Life, Yesterday, 08:20 PM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by focus333, Yesterday, 05:56 PM
                  3 responses
                  16 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by itsthefriz, Yesterday, 04:19 PM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_ShawnB  
                  Started by johnMoss, Yesterday, 04:06 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post johnMoss  
                  Working...
                  X