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

User Preference for Time

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

    User Preference for Time

    I know I can reference time as a condition in the strategy wizard (so only to trade from 9am to 12noon for instance), but can I make the time variable which is definable in the backtest parameters, rather than having to go and change in the main code every time I want to change time?

    #2
    Hello,
    You can create an integer to create a variable to change the parameter for your time filter.
    I have provided an example of how you can achieve this below:
    Code:
    #region Variables
    private int beginTime = 93000;
    private int endTime = 113000;
    #endregion
    
    protected override void OnBarUpdate()
    {
    	if ((ToTime(Time[0]) >= beginTime && ToTime(Time[0]) < endTime)
    	{
    		EnterLong();
    	}
    				
    }	
    
    #region Properties	
    [Description("")]
    [GridCategory("Parameters")]
    public int BeginTime
    {
          get { return beginTime; }
          set { myInput0 = Math.Max(1, value); }
    }
    [Description("")]
    [GridCategory("Parameters")]
    public int EndTime
    {
          get { return endTime; }
          set { myInput0 = Math.Max(1, value); }
    }
    #endregion
    Please let me know if you have any questions on my example.
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      Understood- Am I correct in thinking I can't do this from the strategy wizard? Id prefer not to unlock the code, as I am liking the training wheels.


      Originally posted by NinjaTrader_CodyB View Post
      Hello,
      You can create an integer to create a variable to change the parameter for your time filter.
      I have provided an example of how you can achieve this below:
      Code:
      #region Variables
      private int beginTime = 93000;
      private int endTime = 113000;
      #endregion
      
      protected override void OnBarUpdate()
      {
      	if ((ToTime(Time[0]) >= beginTime && ToTime(Time[0]) < endTime)
      	{
      		EnterLong();
      	}
      				
      }	
      
      #region Properties	
      [Description("")]
      [GridCategory("Parameters")]
      public int BeginTime
      {
            get { return beginTime; }
            set { myInput0 = Math.Max(1, value); }
      }
      [Description("")]
      [GridCategory("Parameters")]
      public int EndTime
      {
            get { return endTime; }
            set { myInput0 = Math.Max(1, value); }
      }
      #endregion
      Please let me know if you have any questions on my example.

      Comment


        #4
        Hello,
        You would be able to do this within the strategy wizard. You will create user defined inputs for the integer time variables.

        When adding the condition you will need to select the user defined input for the right hand side selection to compare against the time of the current bar.

        I have attach screenshots of how you could set up the user defined inputs and then compare the time against the input.

        If we can be of any other assistance please let us know.
        Attached Files
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          When I do this, I get an error message...

          Screenshots attached.



          Originally posted by NinjaTrader_CodyB View Post
          Hello,
          You would be able to do this within the strategy wizard. You will create user defined inputs for the integer time variables.

          When adding the condition you will need to select the user defined input for the right hand side selection to compare against the time of the current bar.

          I have attach screenshots of how you could set up the user defined inputs and then compare the time against the input.

          If we can be of any other assistance please let us know.
          Attached Files

          Comment


            #6
            Hello,
            Upon further review this does not appear to be available to be accomplished through the strategy wizard. You would need to unlock the strategy and use custom programming such as my previous example.
            Cody B.NinjaTrader Customer Service

            Comment


              #7
              OK. Thanks for confirming.

              Originally posted by NinjaTrader_CodyB View Post
              Hello,
              Upon further review this does not appear to be available to be accomplished through the strategy wizard. You would need to unlock the strategy and use custom programming such as my previous example.

              Comment


                #8
                I´m attempting this in unlocked code mode, as instructed - I used && as it is an additional condition. I have an error code coming up saying that && is invalid expression? (on line 53 which is the ToTime line)

                How can I fix this?

                Code:
                // Condition set 1
                            if (GetCurrentBid() > High[1]
                                && Flip == false)
                                && ToTime(Time[0]) >= startTime && ToTime(Time[0]) < endTime;
                            {
                                ExitShort("", "");
                                EnterLong(DefaultQuantity, "");
                            }
                
                            // Condition set 2
                Last edited by ScottieDog; 04-02-2016, 01:18 PM.

                Comment


                  #9
                  Hello,
                  You would not use the ToTime() method on Time[0]. ToTime() converts an integer value so that it returns a DateTime object. The Time[] series returns a datetime values already.
                  If you have set startTime and endTime as DateTime variables then you could do a direct comparison of Time[0] to these values, such as if(Time[0] > startTime)
                  If they are integers you would need to use the ToTime() method on these variables such as if(Time[0] > ToTime(startTime)
                  Cody B.NinjaTrader Customer Service

                  Comment


                    #10
                    I was just copying the code you gave as example in #2.

                    I´m not sure where I have gone wrong.



                    Code:
                    #region Variables
                    private int beginTime = 93000;
                    private int endTime = 113000;
                    #endregion
                    
                    protected override void OnBarUpdate()
                    {
                    	if ((ToTime(Time[0]) >= beginTime && ToTime(Time[0]) < endTime)
                    	{
                    		EnterLong();
                    	}
                    				
                    }	
                    
                    #region Properties	
                    [Description("")]
                    [GridCategory("Parameters")]
                    public int BeginTime
                    {
                          get { return beginTime; }
                          set { myInput0 = Math.Max(1, value); }
                    }
                    [Description("")]
                    [GridCategory("Parameters")]
                    public int EndTime
                    {
                          get { return endTime; }
                          set { myInput0 = Math.Max(1, value); }
                    }
                    #endregion

                    Comment


                      #11
                      Hello,
                      My apologies upon further review you would use ToTime(Time[0]) to compare to an integer value. http://ninjatrader.com/support/helpG...nt7/totime.htm

                      Upon further review of the code it would apper that you have a parantheses after Flip == false which end the if Statement before the && expression.
                      Cody B.NinjaTrader Customer Service

                      Comment


                        #12
                        I see that ) error now, that makes sense. thanks.

                        Comment


                          #13
                          I made the change and it is compiling without error now, but the time filter is not working for some reason. Strategy is running but totally ignoring the time filter.

                          Im not sure what to do next? What would be the next step to troubleshoot this?

                          This time filter works

                          Code:
                           if (GetCurrentAsk() < Low[1]
                                          && Once == false
                                          && Flip == true
                                          && ToTime(Time[0]) >= ToTime(9, 0, 0)
                                          && ToTime(Time[0]) <= ToTime(10, 0, 0)
                                      {
                                          ExitShort("", "");
                                          EnterLong(DefaultQuantity, "");
                                          DrawDot("My dot" + CurrentBar, false, 0, Low[0] + -10 * TickSize, Color.Green);
                                      }
                          When I update the code to try to use the strategy parameter setting for time, the filter stops working (but does now compile)

                          Code:
                          if (GetCurrentAsk() < Low[1]
                                          && Once == false
                          		&& Flip == true
                                          && ToTime(Time[0]) >= (startTime) && ToTime(Time[0]) < (endTime));
                                      {
                                          ExitShort("", "");
                                          EnterLong(DefaultQuantity, "");
                                          DrawDot("My dot" + CurrentBar, false, 0, Low[0] + -10 * TickSize, Color.Green);
                                      }
                          Last edited by ScottieDog; 04-04-2016, 12:50 PM.

                          Comment


                            #14
                            Hello,
                            Do you have startTime and endTime defined as integers?
                            If so please provide how you are setting these variables.
                            Cody B.NinjaTrader Customer Service

                            Comment


                              #15
                              Yes, I have them set as follows::

                              Thanks for the support!


                              Code:
                               #region Variables
                                     
                                      private int profitTarget = 200; // Default setting for ProfitTarget
                                      private int stopLoss = 200; // Default setting for StopLoss
                                      private bool flip = false; // Default setting for Flip
                                      private int startTime = 0900000; // Default setting for StartTime
                                      private int endTime = 1000000; // Default setting for EndTime
                                      private bool once = false; // Default setting for Once
                                     
                                      #endregion




                              Code:
                               #region Properties
                                   
                                      [Description("Trading Start")]
                                      [GridCategory("Parameters")]
                                      public int StartTime
                                      {
                                          get { return startTime; }
                                          set { startTime = Math.Max(1, value); }
                                      }
                              
                                      [Description("Trading End")]
                                      [GridCategory("Parameters")]
                                      public int EndTime
                                      {
                                          get { return endTime; }
                                          set { endTime = Math.Max(1, value); }
                                      }
                                      #endregion
                              Last edited by ScottieDog; 04-04-2016, 01:12 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              238 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              382 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Started by oviejo, Today, 12:28 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post oviejo
                              by oviejo
                               
                              Started by pechtri, 06-22-2023, 02:31 AM
                              10 responses
                              125 views
                              0 likes
                              Last Post Leeroy_Jenkins  
                              Started by judysamnt7, 03-13-2023, 09:11 AM
                              4 responses
                              59 views
                              0 likes
                              Last Post DynamicTest  
                              Working...
                              X