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 a library

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

    How to create a library

    Hello,

    I wish to create a library of methods that I can call from within various different strategies.

    What would be the best way to create/store the file? If I create it as an indicator, it will auto-generate Ninjascript code that prevents compilation.

    I could create this in Visual Studio by targeting framework 3.5, but prefer to have it inside NT.

    Thank you.

    #2
    Hello ivb73077,

    Thank you for your note.

    You would create the methods in the UserDefinedMethods: http://ninjatrader.com/support/helpG...ed_methods.htm

    Comment


      #3
      Thank you. I have undertaken to do exactly that.

      As a result, I have a follow-up question:

      How can I access the methods stored in the new partial class from within another strategy? For example, say I name the partial class file MyMethods and this file contains a method called myUDM which returns a string.

      If I type "this." from within the calling strategy, the new partial class file does not appear. In other words, I cannot access the Intellisense. My code compiles without issue, but I cannot see any reference to MyMethods class from within the calling strategy file.

      Comment


        #4
        Hello ivb73077,

        Thank you for your response.

        Can you attach the .cs file for the UserDefinedMethods to your response?

        Comment


          #5
          Attached is a sample library. It is incomplete, but sufficient for this example.

          If, within the calling strategy, I type "this.", I get a list of the methods contained within the library. However, as there will be several such files, I wondered if it would be possible to call using something like "this.MyLibraryName.MyMethodName()".

          It is simply a matter of convenience as the method names become tedious to recall across numerous relatively large libraries.

          Thank you.
          Attached Files

          Comment


            #6
            Hello,

            Using your example, you can access this currently using just the name or like it is part of this script:

            Code:
            protected override void OnBarUpdate()
            {
            	MarketName name = MarketName.US;
            	futureTickerMonthCode(1);		
            }
            If you want to instead access it by an instance or easy to remember name, you could create a class instead of a partial class. You would not be inheriting from the strategy in this case, so there would not be access to items such as Print(). If you don't need anything from the Strategy base, a class would be a easy way to create a custom or easy to remember name for the collection of methods. Otherwise the partial class makes these basically a part of the current class because it is inheriting from strategy.

            A simple class would be exactly the same as your current script, except you would give it a name and remove partial, or:

            Code:
            namespace NinjaTrader.Strategy
            {
                 public class TestStrat : Strategy
                 {
                      private MyCustomMethods customMethods = new MyCustomMethods();
            
                      protected override void OnBarUpdate()
                      {
                           bool isGreater = customMethods.IsGreaterThanZero(CurrentBar);
                      }
                 }
            }
            	
            namespace NinjaTrader.Strategy
            {
                 public class MyCustomMethods
                 {
                      public bool IsGreaterThanZero(int num)
                      {
                           return num > 0 ? true : false;	
                      }
                 }
            }
            I look forward to being of further assistance.
            Last edited by NinjaTrader_Jesse; 09-13-2015, 03:21 PM.
            JesseNinjaTrader Customer Service

            Comment


              #7
              This is very helpful. Thank you.

              Comment


                #8
                Jesse,

                Following up on your reply, I wondered if it is possible to have the value of properties within a partial strategy class be updated by the user-input values assigned from within the calling strategy.

                As as example, assume I have a main strategy (Strategy A) that contains my entry/exit code. I then have a partial class called Strategy B that contains a variety of utility methods. In Strategy B, I have a property called myProperty. Assume it is an int variable with a default value of 1.

                Can I have a property within Strategy A that the user may change, which would then filter through to the corresponding property in Strategy B? If so, how would I do this?

                Just trying to understand the relationships between items in the partial Strategy class and the primary strategy.

                Thank you.

                Comment


                  #9
                  Hello,

                  Thank you for the question.

                  The partial class is good for utility methods that would be shared or would be used by more than one script to be more correct about wording.

                  In general with strategies in NinjaTrader, there are no supported ways to link two strategies or make them see each other. Each strategy is its own instance with its own Position and reporting, there would not be a way to correctly keep information synced between these two separate instances to ensure logic works as you expect it to.

                  Instead, if you have strategyA doing some logic and StraegyB doing other logic, it would instead be suggested to combine them into a single strategy to prevent any errors. This is especially important if both strategies are on the same account and trading the same instrument, you could run into un managed positions or unexpected errors when going this route. NinjaScript provides the ability to send orders to multiple instruments or multiple timeframes from within a single script so there is rarely a reason to split a strategy into two.

                  Can you provide a description of the reason for separation? If the above covers the reason, I would suggest to not try to link the strategies and instead use the supported approach and combine the logic.

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

                  Comment


                    #10
                    Thank you.

                    However, Strategy B is a bit of a misnomer. It derives from the Strategy class but performs no trading actions. It simply holds a library of methods that perform calculations or retrieve static (i.e. historical) data.

                    That said, my idea would be something like this:

                    Strategy A has an int property called myInt which the user may modify. Strategy B has a property called _myInt.

                    Can I then, from within Strategy A, make the following assignment:

                    _myInt = myInt;

                    so that the Strategy B variable gets assigned the value of its Strategy A counterpart?

                    Comment


                      #11
                      Hello,

                      Thank you for the explanation.

                      This would not be something I could support or say what is expected, I am unsure exactly the outcome without testing that situation specifically.

                      In general, if you have the instance of the class you are referring to, and assign a value to a property of that class, it would hold the value and you could retrieve it, that instance would also see the value and use it. Weather or not that works as expected between the two strategies in NInjaTrader would need tested. you could certainly try this and see if the information is passed correctly but I would not suggest breaking a file into two parts like this if the main strategy will be the only script using it. Instead I would suggest taking all of strategy B and place it in strategy A to avoid this type of logic entirely.

                      You can certainly use a separate class as shown in post 6, but as I said this would only be good for items that do not require the strategy base, it sounds like you want to take that and also inherit from strategy base from the extra class. While you can do this, that base would not be added to the chart or where the other half of the strategy is loaded.

                      For example if I create a class that inherits from indicator(indicatorB) and create an instance of that class in indicator A, because I did not load the indicatorB using the indicator panel, none of the override methods are called and the indicator is not in sync with the chart, this would likely be the same case for the strategy.

                      I would just suggest testing this, if it works, you would need to test all situations to ensure it does not "break" or throw errors.

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

                      Comment


                        #12
                        Thank you.

                        The code is Strategy B will be used by numerous strategies. That is why I wanted to pull it into its own class - so that any changes can be made in 1 file and not N strategy files.

                        In reality, the only reason I need to inherit from the strategy class is to access the Print and Plot methods.

                        Comment


                          #13
                          Hello,

                          If Print and Plot are all you need from the base, I would suggest returning values from Strategy B and Printing or Plotting in StrategyA, that way you could just use the Class example from 6 with no additional hang ups.

                          it may be more code in each strategyA, but less headache and experimenting, that would be one solution but in coding there are many so whichever works best for you will be the solution.

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

                          Comment


                            #14
                            I will have to experiment a bit and see what is cleaner and more efficient.

                            Thank you for all your assistance.

                            Comment


                              #15
                              Jesse,

                              I have created several UserDefinedMethod partial classes and integrated them with my strategy files. Everything is working perfectly.

                              I now wish to share these files with my colleague but came across a post stating that UserDefinedMethod .cs files cannot be exported.

                              I suspect I can circumvent this limitation by simply sending the .cs files and having my colleague copy them to the //NinjaTrader7/bin/Custom/Strategy folder. Do you know if this approach would work? Am I missing anything?

                              Thank you.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by algospoke, Yesterday, 06:40 PM
                              2 responses
                              23 views
                              0 likes
                              Last Post algospoke  
                              Started by ghoul, Today, 06:02 PM
                              3 responses
                              14 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              45 views
                              0 likes
                              Last Post jeronymite  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              7 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              10 responses
                              181 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X