• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MySharedMethods: pass Highs/Lows,..., Instrument

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

    MySharedMethods: pass Highs/Lows,..., Instrument

    Hi, there is a reference to mySharedMethods script that can be placed in AddOns folder. I got that to work https://ninjatrader.com/support/foru...956#post733956

    How do you extend this file to allow it to use things like Highs, Lows without having to pass them like following all the time,
    ISeries<double> high, ISeries<double> low
    tedious to write and as it seems these objects cannot be passed by reference, possibly a performance hit. I like them available automatically without needing to invoke them in every method.

    Also how do I pass other important things like:
    Bars
    Instrument

    Thanks a lot. I created a new topic since the Subject of the referenced topic is non-related.

    #2
    Hello stevenev1,

    Thank you for your post.

    The link provided is a demonstration of using static in C#, this is a concept which you can use to expose items in C#. As this is a C# concept, I generally suggest to also read more about this subject from external C# resources as this is a complex subject. You can use a search engine to look up "static in C#" for some more information.

    This sample does not allow you to use the calling type directly like you are asking so you would need to pass in any objects you want to use as a parameter to that method. This sample is created this way specifically to share between an indicator and a strategy, is that your requirement as well or is this being used to share between two of the same type? The example you have linked cannot be extended to avoid passing in parameters you will need to pass in some values at the least. A partial class could avoid that requirement however that is limited to the same type such as two indicators.

    To pass any other types of objects, you would just need to create a parameter in the methods overloads (.... ) which accept the type you want to pass such as Bars.
    Code:
    MyMethodName(Bars myBars)
    For this question, I would also suggest researching C# Methods and Static in C# from external resources as that will help further explain how this is working along with the structure needed to form methods in C#.

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

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello stevenev1,

      ... is that your requirement as well or is this being used to share between two of the same type? The example you have linked cannot be extended to avoid passing in parameters you will need to pass in some values at the least. A partial class could avoid that requirement however that is limited to the same type such as two indicators.

      To pass any other types of objects, you would just need to create a parameter in the methods overloads (.... ) which accept the type you want to pass such as Bars.
      Code:
      MyMethodName(Bars myBars)
      Hi Jesse, thank you very much for detailed explanation. I just need to share common methods between indicators (not indicators and strategies).

      1.) Can you please provide a sample of a partial class that allows indicators to call common methods without needing to pass parameters such as High, Low. myPrint below is a good example. I use it in all my scripts and I wish for it to be in a common file.

      2) I very much appreciate the example MyMethodName(Bars myBars)

      I started with NT7. I had UserDefinedMethods and it was a simple matter to write methods in it, and everything was available. In NT8, no one has been able to tell me how I can share a simple method like the following across indicators, so I don't have to rewrite it or pass parameters. I would think I am no alone in needing to store common methods in a shared place without need to pass extensive list of parameters?

      private void myPrint( string Msg ) {
      Print( "[" + Instrument.FullName + "] " + Msg + " " + Time[0] );
      }
      Last edited by stevenev1; 03-15-2019, 11:05 AM.

      Comment


        #4
        Hello stevenev1,

        Thank you for your reply.

        Yes, in this case, using a partial class is much more simple because you can make a partial class of the base Indicator type which inherits the items you are using. UserDefinedMethods is just a partial class, this is the same concept NT8 just doesn't include that file any longer because you can just create this structure in a file named what you want.

        To start, create a new indicator and give it any name.
        Once the file opens, remove all of the code leaving only the namespace and using statements.
        In the namespace define a partial class:

        Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
            public partial class Indicator {
        
            }
        }
        You can then define methods or other items here:

        Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
            public partial class Indicator {
                public double MyCalcMethod(){
                    return Close[0];
                }
            }
        }
        In the indicator, you can just call the method:

        Code:
        protected override void OnBarUpdate()
        {
            Print(MyCalcMethod());
        }
        Keep in mind you need to include this file with your exports, and also this can be problematic if you plan to export as compiled assemblies in separate assemblies.

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

        Comment


          #5
          Hi Jesse,

          Your solution worked to perfection. Kudos to you and thank you. FYI, I have been chasing this solution for a year (as one can see from my prior posts), and finally you provided me the perfect answer. This issue was the single reason I was staying with NT7, and now I am fully committed to NT8 - a much improved software I hope others benefit from it as well.

          Comment


            #6
            I try to create a workaround for that:

            Code:
             
             if (DrawObject("PASignalBarBreakoutFailureTick_Test") == null)
            Code:
            // This namespace holds the classes for indicators
            namespace NinjaTrader.NinjaScript.Indicators
            {
                public partial class Indicator
                {
                      // Loops through the DrawObjects collection via a threadsafe list copy
                    public static string DrawObject(string DrawObjectTag)
                    {
                        foreach (DrawingTool draw in DrawObjects.ToList())
                        {
                            if (draw.Tag == DrawObjectTag)
                            {
                                return DrawObjectTag;
                            }
                        }
                        return null;
                    }
                }
            }
            this file MySharedMethods.cs resides in the indicators/My folder, is that correct
            I'm going to export all my indicators into a DLL

            Note: Methods within partial classes should be use the "public" and "static" modifiers, to allow for any other classes to invoke the methods without requiring an instance of the partial class.
            As soon as I add static to

            Code:
             
             public static string DrawObject(string DrawObjectTag)
            I get the following error:
            An object reference is required for the non-static field, method, or property 'NinjaTrader.Gui.NinjaScript.IndicatorRenderBase.D rawObjects.get'

            and what about this
            From NT website?
            Warning: If a partial class is saved in one of the folders used for specific NinjaScript objects other than AddOns (e.g., Indicators folder), auto-generated NinjaScript code may be appended to the end of the class by the NinjaScript Editor when compiled, which will cause a compilation error. Saving these files in the AddOns folder will ensure they are still accessible and will not generate code which may be cause conflicts.

            Comment


              #7
              Hello td_910,

              this file MySharedMethods.cs resides in the indicators/My folder, is that correct
              Yes if this is an indicator, you can place the partial class inside the folder for its type however it is generally suggested to just place these items in the addons folder. You asked another question about this so I will detail that more in a moment.

              Note: Methods within partial classes should be use the "public" and "static" modifiers, to allow for any other classes to invoke the methods without requiring an instance of the partial class.
              I am not certain off-hand where this comment comes from, however, if this was from one of the Drawing object documentation pages, this would not apply to this situation. The Drawing objects use the Draw class which needs to be made static for that use case, where you call Draw.SomeToolName to create it. The error states the problem in your use case:
              An object reference is required for the non-static field, method, or property 'NinjaTrader.Gui.NinjaScript.IndicatorRenderBase.D rawObjects.get'
              We can read this as:
              An Instance is needed to use the non-static property DrawObjects.
              There is no instance being used here because the method is static, but the DrawObjects property being used require an instance of some indicator to be used.

              If you think about it this way, the partial class will assume the properties of the indicator calling it but when you make this method static that tells the compiler we dont need any indicator here to call the method. What would you expect DrawObjects apply to in that case where there is no specific indicator instance being used? You would instead need an indicator instance for an inherited property like DrawObjects or CurrentBar etc.. to be used in that context.

              The solution is just removing static from the method because this use case does not require it. I also suggest reviewing materials surrounding using static in C# to better understand when this should or should not be used if it is still foggy when this is needed, most often it will not be used.


              Warning: If a partial class is saved in one of the folders used for specific NinjaScript objects other than AddOns...
              This is correct although I do not currently see this happening when I test this. This may not occur for every script and is still relevant toward some use cases so I would suggest to heed the help guide warnings. Because you can use namespaces to delegate where a class is in your assembly, you can place the .cs files for most types in any of the folders (aside from indicators) and have it compile. The Addon's folder is a good place to put extra files to ensure NinjaTrader does not try to append indicator code to them. The note is specifically talking about a situation where the indicators NinjaScript generated code is added to your partial class file where it is not needed.

              I look forward to being of further assistance.



              JesseNinjaTrader Customer Service

              Comment


                #8
                Hi Jesse,

                thanks for answering my question and shedding some light onto "static". Yes, I'll always call, that method from inside an indicator.

                This was on the code breaking changes page https://www.screencast.com/t/EGwgWcTn
                I try to workaround this error: https://www.screencast.com/t/ZIRh1dh3

                I've put this code into file in the addon folder and it compiles without any error :-)

                Code:
                // This namespace holds the classes for indicators
                namespace NinjaTrader.NinjaScript.Indicators
                {
                    public partial class Indicator
                    {
                          // Loops through the DrawObjects collection via a threadsafe list copy
                        public string DrawObjectCheck(string DrawObjectTag)
                        {
                            foreach (DrawingTool draw in DrawObjects.ToList())
                            {
                                if (draw.Tag == DrawObjectTag)
                                {
                                    return DrawObjectTag;
                                }
                            }
                            return null;
                        }
                    }
                }

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by stoner, Today, 11:47 AM
                1 response
                2 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by bankavaselina, Today, 10:38 AM
                1 response
                5 views
                0 likes
                Last Post NinjaTrader_ChrisL  
                Started by dieterp, Today, 09:40 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Heath  
                Started by bankavaselina, Today, 08:51 AM
                1 response
                16 views
                0 likes
                Last Post NinjaTrader_PatrickG  
                Started by ballboy11, Today, 09:05 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Jim  
                Working...
                X