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.



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

        Comment


          #5
          Here is one of the ways:
          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.



            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.



              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.



                  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.


                  Learn what is class in object-oriented programming. Learn how to define classes and create objects in C#.




                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Jim

                    Comment


                      #11
                      Originally posted by NinjaTrader_Jim View Post
                      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.


                      Learn what is class in object-oriented programming. Learn how to define classes and create objects in C#.


                      https://docs.microsoft.com/en-us/dot.../types/classes
                      Does this indicator work on a m15 chart?
                      Looked at it.
                      See that this url does not work
                      https://blog.quandl.com/getting-star...the-quandl-api
                      Last edited by tradingforaliving; 10-14-2021, 08:21 AM.

                      Comment


                        #12
                        Welcome to the forums tradingforaliving!

                        The indicator was built to work with daily data and higher intervals. It looks like the API source has changed since Alan shared the indicator. I'm asking Alan more about what changes would need to be made to update the indicator.

                        If it is not too difficult to update, we will see about getting the indicator updated with new API instruction. If the changes are too difficult, we will see about having the indicator delisted, or to have an advisory note added in the description that the API link is no longer functional.
                        JimNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Perr0Grande, Today, 08:16 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post Perr0Grande  
                        Started by elderan, Today, 08:03 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post elderan
                        by elderan
                         
                        Started by algospoke, Today, 06:40 PM
                        0 responses
                        10 views
                        0 likes
                        Last Post algospoke  
                        Started by maybeimnotrader, Today, 05:46 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post maybeimnotrader  
                        Started by quantismo, Today, 05:13 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post quantismo  
                        Working...
                        X