Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

UserDefinedMethods

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

    UserDefinedMethods

    Hi,
    I looked through the "code breaking changes" section and didn't see a recommendation for how to approach what use to be the userdefinedmethods.cs partial classes. Is there a design pattern recommendation on how to replace this type of functionality?

    Also, I use to create all of my own private classes in a separate namespace so as not to muddy the ninjascript namespace. I'm not sure where these cs files should go in the new framework and how they can be edited using the ninjascript editor. Any recommendations?

    #2
    Yes, the user defined methods were removed as they caused problems with users sharing the files, we decided to just let users write their own custom partial classes. The following should be all you need.

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    	public partial class Indicator
    	{
    	
    	}
    }
    You should put your custom classes in the AddOns folder, otherwise if you place them in the Indicators folder, they may trigger wrapper generation incorrectly.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Hi. I can see NT8 is going to be a fair sized learning curve for me...

      I have some custom printing methods that I had in UserDefinedMethods (in Indicator AND in strategy areas - duplicated code) that I guess should go in an AddOn.

      See the simple line: MFPRINT this is the new code.

      Here is the file. It is a simple thing that will allow my to select what output tab to put my debug code, append the instrument name, calling function and then the string I pass in so I can debug my coding.

      1) I am using the default CCI indicator as a simple example: Here is how I would use this method normally:


      public class MFFTCCI : Indicator
      {
      private SMA sma;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionCCI;
      Name = "MFFTCCI";
      IsSuspendedWhileInactive = true;
      Period = 14;

      AddPlot(Brushes.Orange, "MFFTCCI");
      AddLine(Brushes.DarkGray, 200, "Level 2");
      AddLine(Brushes.DarkGray, 100, "Level 1");
      AddLine(Brushes.DarkGray, 0, "Zero line");
      AddLine(Brushes.DarkGray, -100, "Level -1");
      AddLine(Brushes.DarkGray, -200, "Level -2");


      // ok, try out the AddOn to print to the output window
      MFPrint (1, Instrument.FullName, Name, "time now", "MFFTCCI SetDefaults");
      }
      else if (State == State.Configure)
      sma = SMA(Typical, Period);
      }


      QUESTIONS:
      1) How do I create the AddOn so that the methods will be visible to ALL indicators and strategies?
      2) How do I call the AddOn Methods like in the example above?
      3) Is there a better way of doing what I want?

      Thanks in advance

      ADDON CODE:

      //This namespace holds Add ons in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.AddOns
      {
      public class MFPrint : NinjaTrader.NinjaScript.AddOnBase
      {
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Custom Print Routine to the output logs";
      Name = "MFPrint";
      OuputTab = 1;

      //Set this scripts Print() calls to the first output tab
      if (OuputTab == 1)
      PrintTo = PrintTo.OutputTab1;
      else
      PrintTo = PrintTo.OutputTab2;
      }
      else if (State == State.Configure)
      {
      Print(SecurityNamePeriod + ":" + CallingFunc + ":" + OutputString + ", " + Datestring);
      }
      }

      #region Properties
      [Range(1, int.MaxValue)]
      [NinjaScriptProperty]
      [Display(Name="OuputTab", Description="Output tab - 1 or 2", Order=1, GroupName="Parameters")]
      public int OuputTab
      { get; set; }

      [NinjaScriptProperty]
      [Display(Name="SecurityNamePeriod", Description="Calling function to put in the string", Order=2, GroupName="Parameters")]
      public string SecurityNamePeriod
      { get; set; }

      [NinjaScriptProperty]
      [Display(Name="CallingFunc", Description="Calling function to put in the string", Order=3, GroupName="Parameters")]
      public string CallingFunc
      { get; set; }

      [NinjaScriptProperty]
      [Display(Name="Datestring", Description="DateString to put on the end", Order=4, GroupName="Parameters")]
      public string Datestring
      { get; set; }

      [NinjaScriptProperty]
      [Display(Name="OutputString", Description="Output that will go to the tab", Order=5, GroupName="Parameters")]
      public string OutputString
      { get; set; }
      #endregion

      }
      }
      Attached Files

      Comment


        #4
        I don't believe this will work as an AddOn as you're expecting. In the context of the previous replies in this thread, we simply put the partial class in the AddOn folder to prevent any sort of default automatic NS wrapper generation which would break a partial class (i.e., if you had it in the indicator folder)

        To obtain to your goals, if you're working with a partial, you'll need to implement one for Indicators and one for Strategies…

        Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
        	public partial class Indicator
        	{		
                    public int DoAThing(int number) 
                    {
        		return number;
        	    }
        	}
        }
        
        namespace NinjaTrader.NinjaScript.Strategies
        {
            public partial class Strategy
            {
                public int DoAThing(int number) 
                {
        		return number;
        	}
            }
        }
        I think if you want your custom method to all available types without have to duplicate implementation, you should write this as an extension method.

        To do so you'll need to pass in the NinjaScript object which is calling the method as a prameter in order to access those base functions like Print, etc.

        Code:
        // some method definition in MyExtensions class
        public static void MyMethod(this NinjaScript ns)
        {
        	ns.Print(“some things”);
        }
        
        // call the method from an indicator or strategy
        MyExtensions.MyMethod(this);
        With this route, if you want to access a NinjaScript.Indicators object value, you can simply cast the NinjaScript parameter as an Indicator:

        Code:
        // another  method definition in MyExtensions class
        public static void PrintMySma(this NinjaScript ns, int period)
        {
                // cast the NinjaScript as an indicator to access SMA
        	double sma = (ns as Indicators.Indicator).SMA(period)[0];
        	
        	ns.Print(sma);			
        }
        
        // calling that method from an indicator or strategy
        MyExtensions.PrintMySma(this, 20);
        I've attached a copy of a class that implements your MFPrint() as an extension method. To call this method from an indicator or strategy, it would simply be:

        Code:
        MyExtensions.MfPrint(this, 2, Instrument.FullName, Name, "time now", "MFFTCCI SetDefaults");
        Attached Files
        MatthewNinjaTrader Product Management

        Comment


          #5
          Another approach would be to make your own "Singleton Log Class".

          Ultimately, create a singleton class which you can call from your strategy/indicator. I have started to like singleton's as they assure only one instance. You could add additional logging to file or dialog box as you choose...

          Comment


            #6
            Hi Matthew, thanks for this great reply. Couple more questions please.

            Matthew: "I think if you want your custom method to all available types without have to duplicate implementation, you should write this as an extension method.

            To do so you'll need to pass in the NinjaScript object which is calling the method as a prameter in order to access those base functions like Print, etc."

            1) Where do I put the code? Do I still create this as an AddOn?
            2) In order to reference it, do I need to include a line in the #region Using declarations" - IE: "using NinjaTrader.NinjaScript.DrawingTools;"

            Thanks again.

            Comment


              #7
              You're welcome, I hope this helps!

              1) The file I attached can be installed in the AddOns folder. There is not any way to generate this type of code through the NinjaScript editor at the moment, so to get started you must create the file from scratch.
              2) No you do not need access to the DrawingTools if you do not plan on using anything in that namespace.
              MatthewNinjaTrader Product Management

              Comment


                #8
                Hi Matthew. Thanks for this. I got the print to work, but now I have some struggles with finding the methods for Draw.Dot, TickSize and so on.

                I have attached the code, can you assist in showing me where I find the methods? I tried under ns, but those are not found.

                Thanks
                Attached Files

                Comment


                  #9
                  Hello DaFish,

                  TickSize is specific to the instrument you are working with so you either have to pass it directly to your method or grab it using something like:

                  Code:
                  int indexOfYourInstrument = NinjaTrader.Cbi.Instrument.Instruments.IndexOf("your instrument name");
                  
                  NinjaTrader.Cbi.Instrument.Instruments[indexOfYourInstrument].MasterInstrument.TickSize;
                  Please let me know if you have any further questions.
                  Last edited by NinjaTrader_MichaelM; 08-04-2015, 11:22 AM.
                  Michael M.NinjaTrader Quality Assurance

                  Comment


                    #10
                    Originally posted by NinjaTrader_MichaelM View Post
                    Hello DaFish,

                    Because DrawingTools is its own namespace within NinjaScript, you can reference it directly from your ns object. Please see the example below:
                    Code:
                    ns.DrawingTools.Draw.Dot(ns, IDPrefix+currbar.ToString()+dcounter.ToString(), true, barsback, lbloffset, brush);
                    This did NOT work. I still get errors compiling. I have attached the error grid. Please review and advise.

                    Thanks


                    Originally posted by NinjaTrader_MichaelM View Post
                    TickSize is specific to the instrument you are working with so you either have to pass it directly to your method or grab it using something like:

                    Code:
                    int indexOfYourInstrument = NinjaTrader.Cbi.Instrument.Instruments.IndexOf("your instrument name");
                    
                    NinjaTrader.Cbi.Instrument.Instruments[indexOfYourInstrument].MasterInstrument.TickSize;
                    This worked.... thanks
                    Attached Files

                    Comment


                      #11
                      Hello DaFish,

                      I apologize for the confusion. For drawing tools, please use the following instead:
                      Code:
                      DrawingTools.Draw.Dot(/*parameters*/);
                      It looks like you have some other issues with your code which you will need to square away before it will compile successfully but you can access all the drawing tools directly from the DrawingTools namespace.

                      Please let me know if I may be of further assistance.
                      Michael M.NinjaTrader Quality Assurance

                      Comment


                        #12
                        Originally posted by NinjaTrader_MichaelM View Post
                        Hello DaFish,

                        I apologize for the confusion. For drawing tools, please use the following instead:
                        Code:
                        DrawingTools.Draw.Dot(/*parameters*/);
                        That seemed to work....

                        Originally posted by NinjaTrader_MichaelM View Post
                        It looks like you have some other issues with your code which you will need to square away before it will compile successfully but you can access all the drawing tools directly from the DrawingTools namespace.

                        Please let me know if I may be of further assistance.
                        Unfortunately, I do need some more help to get this to compile. I don't think it can find Brush or Brushes....

                        I have included the code and the grid again. It is also complaining about system.time and I am not using that....
                        Attached Files

                        Comment


                          #13
                          Hello DaFish,

                          The issue is that NinjaScriptBase is not available from within the NinjaScript ns you pass into the method. Here is an example of a workaround:
                          Code:
                          namespace NinjaTrader.NinjaScript
                          {
                          	public static class TestMe
                          	{
                          		public static void TestMeDraw(this NinjaScript ns, NinjaScriptBase nsb, double TickSize, double y)
                          		{
                          			DrawingTools.Draw.Dot(nsb, "testDot", true, 0, TickSize + y, Brushes.Red);
                          		}
                          	}
                          }
                          and then in your indicator/strategy/whatever:
                          Code:
                          TestMe.TestMeDraw(this, this, TickSize, High[0]);
                          Please let me know if you have any further questions.
                          Michael M.NinjaTrader Quality Assurance

                          Comment


                            #14
                            Just FYI for people that are searching for UserDefinedMethods.

                            Michael has figured all this out for me in another thread too (Thanks again Michael).

                            Here is the link with a sample program for you to use so you can replicate the functionality we got in UserDefinedMethods from NT7

                            Comment


                              #15
                              Hello Michael,

                              I am trying to convert some scripts from NT7 to NT8 and now I found out that userdefinedmethods script is not part of the NT8. I see that I am able to my custom made indicators but my userdefinedmethod script was holding ints and other parameters so I do not need to introduce them again while referring from one indicator to another.

                              My question is if there is some way how to do it when userdefinedmethod script is no longer in NT8. I need to add that I am not an expert in C# programing just trying to convert some stuff from NT7 to NT8.

                              I can share some part of userdrfinedmethods script from NT7.

                              Miroslav

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Irukandji, Today, 09:34 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post Irukandji  
                              Started by TraderBCL, Today, 04:38 AM
                              3 responses
                              24 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              11 responses
                              1,423 views
                              0 likes
                              Last Post jculp
                              by jculp
                               
                              Started by RubenCazorla, Today, 09:07 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post RubenCazorla  
                              Started by BarzTrading, Today, 07:25 AM
                              2 responses
                              29 views
                              1 like
                              Last Post BarzTrading  
                              Working...
                              X