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

Create a Reusable Method

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

    Create a Reusable Method

    I have several pieces of code that I copy and paste into almost every strategy I develop. Is it possible to turn that code into a method which would be reusable and callable rather than cutting and pasting it into my strategy. The code requires components in variables, properties, and on bar update but I'm guessing that a properly written method would accommodate that. Can you point me to a reference or example of how to do this. for clarity I'm pasting in the code so you can see what I am talking about. The { brackets may not be correct since I lifted these components from a strategy that had a lot more stuff inside.

    Code:
    // This code constrains trading to only certain times during the day.
            #region Variables
    		// Timed Trading Variables
    		private int tt1_Start = 90000 ; // Start Time for first trading period
    		private int tt1_End = 120000 ; // End Time for first trading period
    		private int tt2_Start = 103000 ; // Start Time for second trading period
    		private int tt2_End = 103000 ; // end Time for second trading period
            #endregion
    
            protected override void OnBarUpdate()
            {
    	if ((ToTime(Time[0]) > TT1_Start && ToTime(Time[0]) < TT1_End ) || (ToTime(Time[0]) > TT2_Start && ToTime(Time[0]) < TT2_End )
    				&& Position.MarketPosition == MarketPosition.Flat)
    				{
                                     // Do Something Here
                                     }
             }
            #region Properties
            [Description("")]
            [Category("Parameters")]
            public int TT1_Start
            {
                get { return tt1_Start; }
                set { tt1_Start = Math.Max(000001, value); }
            }
    		
            [Description("")]
            [Category("Parameters")]
            public int TT1_End
            {
                get { return tt1_End; }
                set { tt1_End = Math.Max(000001, value); }
            }
    		
            [Description("")]
            [Category("Parameters")]
            public int TT2_Start
            {
                get { return tt2_Start; }
                set { tt2_Start = Math.Max(000001, value); }
            }
    		
            [Description("")]
            [Category("Parameters")]
            public int TT2_End
            {
                get { return tt2_End; }
                set { tt2_End = Math.Max(000001, value); }
    	}
            #endregion
    }
    Thanks
    DaveN

    #2
    Hello DaveN,

    You can create custom public methods in the UserDefinedMethods.cs that is in the Strategy folder

    This would allow you to access that method by simply calling it in the strategy and if you need to pass anything through it.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Method

      Originally posted by NinjaTrader_Cal View Post
      Hello DaveN,

      You can create custom public methods in the UserDefinedMethods.cs that is in the Strategy folder

      This would allow you to access that method by simply calling it in the strategy and if you need to pass anything through it.
      Any example I could look at? I tried populating the User Defined Methods as best I could but I get an error:
      'NinjaTraderStrategy.TradeTime OnBarUpdate' No suitable method found to overide.

      Here's the code as I wrote it:

      Code:
      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Drawing;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Strategy;
      #endregion
      
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
          /// <summary>
          /// This file holds all user defined strategy methods.
          /// </summary>
          partial class TradeTime
          {
              #region Variables
      		// Timed Trading Variables
      		private bool timetotrade = false;
      		private int tt1_Start = 0000 ; // Start Time for first trading period
      		private int tt1_End = 130000 ; // End Time for first trading period
      		private int tt2_Start = 103000 ; // Start Time for second trading period
      		private int tt2_End = 103000 ; // end Time for second trading period
              #endregion
      		
              protected override void OnBarUpdate()
              {
      			if ((ToTime(Time[0]) > TT1_Start && ToTime(Time[0]) < TT1_End ) || (ToTime(Time[0]) > TT2_Start && ToTime(Time[0]) < TT2_End ))
      			{
      			TimetoTrade = true;
      			}
      		}
              #region Properties		
              [Description("")]
              [Category("Parameters")]
              public int TT1_Start
              {
                  get { return tt1_Start; }
                  set { tt1_Start = Math.Max(000001, value); }
              }
      		
              [Description("")]
              [Category("Parameters")]
              public int TT1_End
              {
                  get { return tt1_End; }
                  set { tt1_End = Math.Max(000001, value); }
              }
      		
              [Description("")]
              [Category("Parameters")]
              public int TT2_Start
              {
                  get { return tt2_Start; }
                  set { tt2_Start = Math.Max(000001, value); }
              }
      		
              [Description("")]
              [Category("Parameters")]
              public int TT2_End
              {
                  get { return tt2_End; }
                  set { tt2_End = Math.Max(000001, value); }
              #endregion			
      		}
      	}
      }

      Comment


        #4
        You are going to have to build you own method here. You won't be able to use the OnBarUpdate() this way.
        Code:
        public bool MyMethod(int TT1_Start, int TT1_End, int TT2_Start, int TT2_End)
        {
             if ((ToTime(Time[0]) > TT1_Start && ToTime(Time[0]) < TT1_End ) || (ToTime(Time[0]) > TT2_Start && ToTime(Time[0]) < TT2_End ))
        	return true;
        			
             else
                return false;
        }
        You would then call that method from your strategy's OnBarUpdate
        Code:
        protected override void OnBarUpdate()
        {
        
             if(MyMethod(TT1_Start, TT1_End, TT2_Start, TT2_End))
             {
                 //do something
             }
        }
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Reusable Method

          So I don't need all that other stuff in the UserDefinedMethods container? Variables, properties, etc.?
          I cleaned it all out but now I'm getting an error:
          "TradeTime member names cannot be the same as the enclosing type"

          I'm guessing this is the problem:

          Code:
          namespace NinjaTrader.Strategy
          {
              /// <summary>
              /// This file holds all user defined strategy methods.
              /// </summary>
          [COLOR="Red"]    partial class TradeTime[/COLOR]
              {
          		
          	public bool TradeTime(int TT1_Start, int TT1_End, int TT2_Start, int TT2_End)
          		{
              	if ((ToTime(Time[0]) > TT1_Start && ToTime(Time[0]) < TT1_End ) || (ToTime(Time[0]) > TT2_Start && ToTime(Time[0]) < TT2_End ))
          		return true;
          			
              	 else
                  	return false;
          		}
          
          	}
          }
          Do I need a different name for the part highlighted in red?

          DaveN

          Comment


            #6
            Also, does the UserDefinedMethods code need it's own properties section or are those variables just passed in when I call the method, where I would put them in a variables section in the strategy calling the method?

            Comment


              #7
              You would need to use 'partial class Strategy'

              Additionally, those are just the int names that you are going to be passing into the method. You can name anything you want.

              The properties would be passed through the strategy itself.
              Cal H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by RookieTrader, Today, 07:41 AM
              1 response
              4 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by kujista, Today, 05:44 AM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by elderan, Yesterday, 08:03 PM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Started by samish18, Yesterday, 08:57 AM
              8 responses
              25 views
              0 likes
              Last Post samish18  
              Started by DJ888, 04-16-2024, 06:09 PM
              3 responses
              10 views
              0 likes
              Last Post NinjaTrader_Erick  
              Working...
              X