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

Price at certain time three days ago

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

    Price at certain time three days ago

    I want to get the close and the price at a certain time three days ago.

    This works fine:
    Bars.GetDayBar(3).Close

    But how do I get the price for example att time 15.55?


    Bars.GetDayBar(3).Time(15, 55, 0)


    But this doesn't work.. should I also input date or isn't that relevant?

    #2
    Yes, for a DateTime to be valid this would need to be Date and Time, so input date included as well - here's a full sample into this topic :

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thank you Bertrand.

      Now I have this, but when I activate the strategy in a chart.. It won't be activated. And when I run a backtest.. It backtests only the first month of selected time frame..

      Do I something wrong?

      PHP Code:
      #region Variables
              // Wizard generated variables
              
      private int myInput0 1// Default setting for MyInput0
              
      private DateTime    time1530;   // DateTime object to hold the time we're interested in.
              
      private int            closeyest     0
              
      // User defined variables (add any user defined variables below)
              #endregion 
      PHP Code:
              protected override void OnBarUpdate()
              {
              if (
      Bars.FirstBarOfSession && FirstTickOfBar)
                  {
                      
      /* If the day is Monday, and we want the value from Friday, we must subtract 3 days from the current date.
                         If the day is Tuesday-Friday, just subtract one day. */
                      
      if (Time[0].DayOfWeek == DayOfWeek.Monday)
                          
      time1530 = new DateTime(Time[0].YearTime[0].MonthTime[0].Day 315300);
                      else
                          
      time1530 = new DateTime(Time[0].YearTime[0].MonthTime[0].Day 115300);
                  }
               
                      
      closeyest GetBar(time1530);
                  if ( 
      Close[0] >  closeyest
                      
      && ToTime(Time[0]) == 153100
                      
      )
                  { 
      EnterShort(DefaultQuantity"");
                  }
                          if (  
                              
      ToTime(Time[0]) == 160000
                              
      )
                  {
      ExitShort("""");
                  }
              } 

      Comment


        #4
        Do you get any log error in the Control Center shown when you activate it?
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Yes I get this error:
          You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
          Now I've added this:

          if (closeyest != 0 && CurrentBar > 20){......}
          But now, it doesn't trade at all.

          Code now:

          PHP Code:
             protected override void OnBarUpdate()
                  {
                      if (
          closeyest != && CurrentBar 20){
                  if (
          Bars.FirstBarOfSession && FirstTickOfBar)
                      {
                          
          /* If the day is Monday, and we want the value from Friday, we must subtract 3 days from the current date.
                             If the day is Tuesday-Friday, just subtract one day. */
                          
          if (Time[0].DayOfWeek == DayOfWeek.Monday)
                              
          time1530 = new DateTime(Time[0].YearTime[0].MonthTime[0].Day 315300);
                          else
                              
          time1530 = new DateTime(Time[0].YearTime[0].MonthTime[0].Day 115300);
                      }
                   
                          
          closeyest GetBar(time1530);
                      if ( 
          Close[0] >  closeyest
                      
                          
          )
                      { 
          EnterShort(DefaultQuantity"");
                      }
                              if (  
                                  
          ToTime(Time[0]) == 160000
                                  
          )
                      {
          ExitShort("""");
                      }
                  }} 

          Comment


            #6
            Hi no111,

            What bar interval are you using for this? Is it a single or multiseries script?
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_RyanM View Post
              Hi no111,

              What bar interval are you using for this? Is it a single or multiseries script?
              Hi RyanM,

              What do you mean with multiseries? Multiple Data series, I guess?


              I'm using only one minute chart, ES 06-12 with standard CME US index RTH template.

              Comment


                #8
                OK, looks like you're not comparing the same type of thing. GetBar() returns the number of bars ago since the time you specify. You're comparing Close[0] (a price value) against a bars ago value .
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Ok, well this should work then, but it doesn't..
                  Now I compare the Close[0] with Close from yesterday on 15.31..
                  PHP Code:

                       
                  protected override void OnBarUpdate()
                          {
                              if (
                  closeyest != && CurrentBar 20){
                          if (
                  Bars.FirstBarOfSession && FirstTickOfBar)
                              {
                                  
                  /* If the day is Monday, and we want the value from Friday, we must subtract 3 days from the current date.
                                     If the day is Tuesday-Friday, just subtract one day. */
                                  
                  if (Time[0].DayOfWeek == DayOfWeek.Monday)
                                      
                  time1530 = new DateTime(Time[0].YearTime[0].MonthTime[0].Day 315310);
                                  else
                                      
                  time1530 = new DateTime(Time[0].YearTime[0].MonthTime[0].Day 115310);
                              }
                           
                                  
                  closeyest GetBar(time1530);
                              if ( 
                  Close[0] >  Close[closeyest]
                              
                                  )
                              { 
                  EnterShort(DefaultQuantity"");
                                   
                  BackColor Color.Black;
                              }
                                      if (  
                                          
                  ToTime(Time[0]) == 160000
                                          
                  )
                              {
                  ExitShort("""");
                              }
                          }} 

                  Comment


                    #10
                    Great, that looks better. We're unfortunately not able to debug this for you fully. I suggest simplifying and checking all values used in the condition by printing them. You can also use Print() to follow code flow, which should help you to follow what blocks of code are actually entered. Check order messages using TraceOrders output. See here for help using these debugging tools:
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      I understand, thanks.
                      I'll post my results if it works !!

                      Comment


                        #12
                        Looking through the above code, is this valid code?

                        time1530 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 3, 15, 31, 0);

                        specifically the part: Time[0].Day - 3

                        Is NT smart enough to distinguish changing from one month to another...?

                        Comment


                          #13
                          Hello nightriderx,
                          If the Day is the first, second or third day of month then you will get a negative or zero value if you subtract it by -3 which is not valid.

                          You have to hardcode any change in month. For example

                          Code:
                          if (Time[0].Month != time1530.Month)
                          {
                              //do something
                          }
                          Please let me know if I can assist you any further.
                          JoydeepNinjaTrader Customer Service

                          Comment


                            #14
                            Thanks.

                            So I need to get the price exactly 30 days ago (ignoring the fact that it could be a sat/sun) how would you recommend coding that? I believe .net offers a date subtraction tool, but is there anything like that in nt?
                            thanks again!

                            Comment


                              #15
                              nightriderx,

                              Yes, you can use dot net DateTime for this, and GetBar() to get the bars ago value for a specific date time. The point of reference is the bar time stamp. If you wanted to use your PC clock instead, replace Time[0] with DateTime.Now

                              Code:
                              DateTime thirtyDaysAgo = Time[0].AddDays(-30);
                              int barsAgo = GetBar(thirtyDaysAgo);
                              
                              if (barsAgo != CurrentBar)
                              {
                              	Print("BarsAgo: " + barsAgo);
                              	Print(CurrentBar + " Value: " + Close[barsAgo]);
                              }
                              A sample that can help using GetBar() is available here:
                              Last edited by NinjaTrader_RyanM1; 04-27-2012, 03:43 PM.
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jclose, Today, 09:37 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,414 views
                              0 likes
                              Last Post Traderontheroad  
                              Started by firefoxforum12, Today, 08:53 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post firefoxforum12  
                              Started by stafe, Today, 08:34 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post stafe
                              by stafe
                               
                              Started by sastrades, 01-31-2024, 10:19 PM
                              11 responses
                              169 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X