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

Object Oriented Programming in NT

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

    Object Oriented Programming in NT

    I recently opened my dusty C# for Dummies book and have learned that the programming I’ve done in NT is more “procedural” in nature than “object oriented”.

    (Sigh) This bothers me a bit because I thought since I have methods I have objects. That doesn’t seem to be the case…
    Anyway I’m going to change my code from flow chart to more OOP. I need the flexibility of being able to add new things as they come up. Also, being able to call objects as opposed to having multiple variables that do the same thing will serve me well.

    Anyway, I was looking for confirmation, in my indicators code I was going to create different classes for the objects I have and go from there. I quickly created a blank class and it compiled without any issues.
    Are people here with larger programs doing that?

    The indicator itself obviously will always be the main class (public class MyClass: Indicator) with OnBarUpdate being where the action is. But if we can create methods anywhere we can do the same with classes and then create objects from them?

    Thx
    Irvin

    https://github.com/sempf/CSharpForDummies

    #2
    Irvin,

    NinjaTrader is based on the .NET framework which uses C#. You can create your own classes for use with indicators, etc. and there are many developers doing so.

    NinjaScript is setup as sort of a more functional / procedural programming language just for ease of use by inexperienced programmers but OOP should be supported as outlined in the .NET framework. You just need to keep track of the namespaces, etc. engineering part of NinjaTrader when you program your own classes for use in particular indicators or strategies.

    Please let me know if I may assist further.
    Last edited by NinjaTrader_AdamP; 06-04-2012, 11:23 AM.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hi Ryan

      Thanks for your reply.

      All my methods are in UserDefinedMethods so I will create my objects there.

      Btw, where is the code for NinjaTrader.Data?
      That is a namespace?
      That is where say, the code for DataSeries would be?

      Thx
      Irvin

      Comment


        #4
        Irvin,

        That would be a global class that would be accessible in both strategy and indicator namespaces.

        Please let me know if I may assist further.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_AdamP View Post
          Irvin,

          That would be a global class that would be accessible in both strategy and indicator namespaces.

          Please let me know if I may assist further.
          What would be global?

          I thought you could only have code in UserDefinedMethods for indicators and then use the UserDefinedMethods for strategies?

          Currently I copy my code from indicators UserDefined to strategies userdefined...

          Comment


            #6
            Irvin,

            Apologies, I misunderstood. The NinjaTrader.Data is accessible in both namespaces, however isn't exposed for editing/viewing.

            The My Documents / NinjaTrader 7 / bin / custom / indicator and strategy directories each have their own UserDefinedMethods.cs you can use for each respectively.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              How do you add a new public class in the UserDefinedMethods.cs?

              If I want to add a public class in any of the indicators I have:

              public class MyNewClass : [name of indicator]
              {
              }

              With the same approach in UserDefinedMethods file I get an error saying:

              NinjaTrader.Indicator.MyClass does not implement inherited abstract member ... Initialize()

              Code:

              publicclass MyClass : Indicator
              {

              publicbool MyRed()
              {
              if(Close[0] < Close[1])
              returntrue;
              else
              returnfalse;
              }
              }

              Comment


                #8
                Irvin,

                Here is an example of something I programmed for indicators in the UserDefinedMethods.cs that works currently.

                Code:
                namespace NinjaTrader.Indicator
                {
                    /// <summary>
                    /// This file holds all user defined indicator methods.
                    /// </summary>
                    partial class Indicator
                    {
                    }
                	
                	public class MergeSort 
                	{
                		public static void Sort(double[] data, int left, int right)
                		{
                			if(left < right)
                			{
                				int middle = (left+right)/2;
                				Sort(data,left,middle);
                				Sort(data,middle+1,right);
                				Merge(data,left,middle,middle+1,right);
                			}
                		}
                		
                		public static void Merge(double[] data, int left, int middle, int middle1, int right)
                		{
                			int oldPosition = left;
                			int size = right - left + 1;
                			double[] temp = new double[size];
                			int i = 0;
                			
                			while (left<=middle && middle1<=right)
                			{
                				if(data[left] <= data[middle1])
                				{
                					temp[i++]=data[left++];
                				}
                				else
                				{
                					temp[i++]=data[middle1++];
                				}
                			}
                			
                			if(left>middle)
                			{
                				for(int j = middle1; j<=right; j++)
                				{
                					temp[i++]=data[middle1++];
                				}
                			}
                			else
                			{
                				for(int j = left; j<=middle; j++)
                				{
                					temp[i++]=data[left++];
                				}
                			}
                			
                			Array.Copy(temp,0,data,oldPosition,size);
                		}
                	}
                }
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_AdamP View Post
                  Irvin,

                  Here is an example of something I programmed for indicators in the UserDefinedMethods.cs that works currently.

                  Code:
                   
                  namespace NinjaTrader.Indicator
                  {
                      /// <summary>
                      /// This file holds all user defined indicator methods.
                      /// </summary>
                      partial class Indicator
                      {
                      }
                   
                      public class MergeSort 
                      {
                          public static void Sort(double[] data, int left, int right)
                          {
                              if(left < right)
                              {
                                  int middle = (left+right)/2;
                                  Sort(data,left,middle);
                                  Sort(data,middle+1,right);
                                  Merge(data,left,middle,middle+1,right);
                              }
                          }
                   
                          public static void Merge(double[] data, int left, int middle, int middle1, int right)
                          {
                              int oldPosition = left;
                              int size = right - left + 1;
                              double[] temp = new double[size];
                              int i = 0;
                   
                              while (left<=middle && middle1<=right)
                              {
                                  if(data[left] <= data[middle1])
                                  {
                                      temp[i++]=data[left++];
                                  }
                                  else
                                  {
                                      temp[i++]=data[middle1++];
                                  }
                              }
                   
                              if(left>middle)
                              {
                                  for(int j = middle1; j<=right; j++)
                                  {
                                      temp[i++]=data[middle1++];
                                  }
                              }
                              else
                              {
                                  for(int j = left; j<=middle; j++)
                                  {
                                      temp[i++]=data[left++];
                                  }
                              }
                   
                              Array.Copy(temp,0,data,oldPosition,size);
                          }
                      }
                  }
                  What about if I want to use data from a specific bar?

                  i.e. Close[0] etc..

                  Comment


                    #10
                    Irvin,

                    I would suggest passing it as an argument. I don't believe that the Close dataseries would be accessible outside of an indicator/strategy.

                    I used that class to make a simple moving median as an exercise when I was in training many many months ago, even though NinjaTrader can use another median finding method. Also note that this indicator is inefficient as it doesn't really use a dataseries like it should. I leave it here for educational purposes only.

                    Here is the indicator OnBarUpdate() that I made using that MergeSort class.

                    Code:
                     protected override void OnBarUpdate()
                            {
                    			if (CurrentBar < Period) return;
                    			
                    			double Median = 0;
                    			double[] SortArray;
                    			SortArray = new double[Period];
                    			
                    			for(int barsAgo = 0; barsAgo < Period ; barsAgo++)
                    			{
                    				SortArray[barsAgo]=Input[barsAgo];
                    			}
                                
                    			//MergeSort
                    			MergeSort.Sort(SortArray,0,SortArray.Length-1);
                    			
                    			#region Get the medians from sorted array
                    			double pos = Period/2;
                    			if(Period==1)
                    			{
                    				Median=SortArray[0];
                    			}
                    			else if(Period%2 == 0)
                    			{
                    				Median=(SortArray[(int)pos-1]+SortArray[(int)pos])/2;
                    			}
                    			else
                    			{
                    				Median=SortArray[(int)Math.Ceiling(pos)];
                    			}
                    			#endregion
                    			
                                Plot0.Set(Median);
                            }
                    Last edited by NinjaTrader_AdamP; 06-04-2012, 12:29 PM.
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      It does not compile. It says "The name Close does not exist in the current context."

                      So basically to sum it up - you are able to create different classes and objects but they can't be DataSeries or Indicators that we normally use from NT. (Close, High, Low, PriorDay, etc)

                      They would have to be a part of regular C# ....

                      I was just looking to separate the multiple things I do into different claseses and have them as objects...

                      Comment


                        #12
                        Hello,

                        This is correct you can create your own classes.

                        What you can do however is pass the reference of the NinjaTrader Strategy or Indicator class to the secondary class and you can use that reference to then access all the features of the strategy.

                        This is all not officially supported by us but I can show you how you would use this and you can go from there.

                        An example would be.

                        Instantiate your own class in your strategy passing in the reference to this strategy Instance.

                        NewClass NewClass = new NewClass(this);


                        then in the new class parameter would be setup like this to take the reference:

                        class NewClass(NinjaTrader.Strategy.(YOURSTRATNAME) o)

                        or

                        class NewClass(object o)
                        {
                        YourStratName StrategyReference = o;

                        StrategyReference.Print("This Works!" + StrategyReference.Close[0];

                        }

                        Comment


                          #13
                          Originally posted by ij001 View Post
                          I recently opened my dusty C# for Dummies book and have learned that the programming I’ve done in NT is more “procedural” in nature than “object oriented”.

                          (Sigh) This bothers me a bit because I thought since I have methods I have objects. That doesn’t seem to be the case…
                          Anyway I’m going to change my code from flow chart to more OOP. I need the flexibility of being able to add new things as they come up. Also, being able to call objects as opposed to having multiple variables that do the same thing will serve me well.

                          Anyway, I was looking for confirmation, in my indicators code I was going to create different classes for the objects I have and go from there. I quickly created a blank class and it compiled without any issues.
                          Are people here with larger programs doing that?

                          The indicator itself obviously will always be the main class (public class MyClass: Indicator) with OnBarUpdate being where the action is. But if we can create methods anywhere we can do the same with classes and then create objects from them?

                          Thx
                          Irvin

                          https://github.com/sempf/CSharpForDummies
                          Not quite. NT indicators and strategies are composed as classes. The code inside a class is ultimately always precedural. Even if you nest classes, your innermost class will have to be procedural. After all, all methods, even if they use class constructs, are themselves procedural.

                          NT seems to be using the now generally accepted standard convention of one class per file, thereby turning each file itself in effect into an object. The code in the object, consisting of methods, variables and other objects will itself be procedural.

                          OOP refers to how the data structures are closely entwined with the code for manipulating said data. OOP is not a reference to a coding style totally devoid of procedural elements. Indeed, it is actually impossible to write such code, because the internal methods of the classes must necessarily be procedural.

                          Just my $0.02.
                          Last edited by koganam; 06-05-2012, 10:34 AM. Reason: Corrected spelling

                          Comment


                            #14
                            Originally posted by NinjaTrader_AdamP View Post
                            Irvin,

                            Here is an example of something I programmed for indicators in the UserDefinedMethods.cs that works currently.

                            Code:
                             
                            namespace NinjaTrader.Indicator
                            {
                                /// <summary>
                                /// This file holds all user defined indicator methods.
                                /// </summary>
                                partial class Indicator
                                {
                                }
                             
                                public class MergeSort 
                                {
                                    public static void Sort(double[] data, int left, int right)
                                    {
                                        if(left < right)
                                        {
                                            int middle = (left+right)/2;
                                            Sort(data,left,middle);
                                            Sort(data,middle+1,right);
                                            Merge(data,left,middle,middle+1,right);
                                        }
                                    }
                             
                                    public static void Merge(double[] data, int left, int middle, int middle1, int right)
                                    {
                                        int oldPosition = left;
                                        int size = right - left + 1;
                                        double[] temp = new double[size];
                                        int i = 0;
                             
                                        while (left<=middle && middle1<=right)
                                        {
                                            if(data[left] <= data[middle1])
                                            {
                                                temp[i++]=data[left++];
                                            }
                                            else
                                            {
                                                temp[i++]=data[middle1++];
                                            }
                                        }
                             
                                        if(left>middle)
                                        {
                                            for(int j = middle1; j<=right; j++)
                                            {
                                                temp[i++]=data[middle1++];
                                            }
                                        }
                                        else
                                        {
                                            for(int j = left; j<=middle; j++)
                                            {
                                                temp[i++]=data[left++];
                                            }
                                        }
                             
                                        Array.Copy(temp,0,data,oldPosition,size);
                                    }
                                }
                            }
                            Hi Adam

                            Revisiting your example above in quotes, I am having problems accessing methods I wrote in the UserDefinedMethods.cs file before I created a new class like yours.

                            Error: "An object reference is required for the non-static field, method, or ... "

                            So how do you access your methods that are created in just Indicator class (below the MergeSort class) for use inside MergeSort class?

                            i.e. Accessing a method SetName() for the MergeSort class but it is within only the class Indicator?

                            Comment


                              #15
                              Hello,

                              This is more general c# support vs NinjaScript support however I believe you need a static class MergeSort when you declare the class.

                              -Brett

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by WHICKED, Today, 12:45 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by samish18, Today, 01:01 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post samish18  
                              Started by WHICKED, Today, 12:56 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Started by Spiderbird, Today, 12:15 PM
                              2 responses
                              11 views
                              0 likes
                              Last Post Spiderbird  
                              Started by FrazMann, Today, 11:21 AM
                              2 responses
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Working...
                              X