Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entering X days before

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

    Entering X days before

    I am looking for a way to make a strategy that will look at today's date and determine if it is "x" amount of days before another date that I have specified. I attempted this with the ToDay(Time[0]) function and was able to enter in the position when i wanted yet, if I wanted the strategy to hold the stock through to the next month It would always exit my position on the first day of the month. Is there any idea on how to get the Ninja script to not exit my position on the first of the month?

    #2
    Hi cboat,

    Can you provide a snippet of how you are using ToDay(Time[0])
    TimNinjaTrader Customer Service

    Comment


      #3
      if (ToDay(Time[0]) + xdays == 20090915 )
      {
      Enter Position
      }

      20090915 is the date I want to set. Then xdays is the number of days before hand when I would like to enter.

      if (ToDay(Time[0]) - ydays > 20090915 )
      {
      Exit Position
      }
      The problem occurs here when the number of Y days would take you into another month. It always exits you on the first days of the month.

      Comment


        #4
        Hi cboat,

        You add days with DateTime.Today.AddDays()

        As an example...

        Code:
        Print("Today + : " + DateTime.Today.AddDays(28));
        Prints "Today + 28 days: 6/23/2010 12:00:00 AM"
        TimNinjaTrader Customer Service

        Comment


          #5
          Thank you. My next question would be if i declare a variable using DateTime will it update everyday the strategy runs. I.E.

          DateTime date1 = DateTime.Today;
          DateTime date 2 = DateTime.Today.AddDays(3);

          ultimately what i am trying to do is have the strategy enter on date1. which will be a certain amount of days before a given date (ex. November 12 20XX). Then Exit a certain amount of days after the given date. So it would enter on the 9th and then exit on the 14th.

          Comment


            #6
            Hello cboat,

            DateTime.Today will grab your current system date from your PC Clock. The variable is initialized with this date and then it will depend on how you use it later on. If the value is assigned in OnBarUpdate() then it will update each time there is a new bar or tick.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              That sounds good. What I want to do though is a dividend capture trade. Where I can optimize on the number of days before the ex-dividend date. I can get my strategy to enter on the day that I want yet It will not exit when I desire.

              Comment


                #8
                Hi cboat,

                Sounds like an interesting strategy!


                You can set up a calender selection variable with

                Something like...
                Code:
                private DateTime entryDate = new DateTime(2010,01,01);
                TimNinjaTrader Customer Service

                Comment


                  #9
                  Excellent Thank you very much. Finally can you compare to DateTime variables in an If Statement?

                  Comment


                    #10
                    Also I am getting an error message saying that I cannot reference a nonstatic field. I.E. divDate. here is a view of my code

                    private DateTime divDate = new DateTime (2010, 09, 10);
                    private DateTime today = DateTime.Today;
                    private DateTime entryDay = divDate.AddDays(-2);
                    private DateTime exitDay = divDate.AddDays(2);

                    Comment


                      #11
                      Hi cboat,

                      For you first question, I believe so, have you tried this?

                      Have you added the appropriate get/set in the properties section of the code?

                      Something like...

                      Code:
                      [Description("")]
                      [GridCategory("Parameters")]
                      
                      public DateTime EntryDate
                      {
                      	get { return entryDate;}
                      	set { entryDate = value;}
                      }
                      TimNinjaTrader Customer Service

                      Comment


                        #12
                        Yes, Yet I still get the error message stating A field initializer cannot reference the nonstatic field, method, or property. CS0236 is the error number

                        Comment


                          #13
                          Hi cboat,

                          Since some of your variables include others in their calculation, first, declare the variables...
                          Code:
                          private DateTime divDate = new DateTime (2010, 09, 10);
                          private DateTime today = DateTime.Today;
                          private DateTime entryDay;
                          private DateTime exitDay;
                          Then make any adjustment under OnBarUpdate...
                          Code:
                          entryDay = divDate.AddDays(-2);
                          exitDay = divDate.AddDays(2);
                          TimNinjaTrader Customer Service

                          Comment


                            #14
                            Excellent Thank you very the strategy is compiling without errors. My final question. Is when I backtest on the strategy, since I am using DateTime.Today the strategy is always working off my system clock today. This makes it difficult for me to make sure it is running correctly. Is there a small fix I can use to get around this problem?

                            Comment


                              #15
                              Hi cboat,

                              Can you clarify? Are you concerned it will not actually use today's date?

                              You can print the value using Print()
                              More info at - http://www.ninjatrader-support.com/H...ide.html?Print

                              Or you can set this in the code as a specific date.
                              TimNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              580 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              335 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              102 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              554 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              552 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X