Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with getting close price

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

    Problem with getting close price

    If I do this, I can get the close price:
    int barsAgo = CurrentBar - Bars.GetBar(new DateTime (2010,12,30,16,15,0));
    Print(
    "The close price on the 4:15 PM bar was: " + Close[barsAgo].ToString());

    What I would like to do is get the close of the bar from a DateTime variable. I have one which I checked is correctly formed (by printing it)and has the same information as the example above. I don't know how to place it in a statement. The following gives me the error, "The name 'dtPreviousClose' does not exist in the current context."

    int barsAgo = CurrentBar - Bars.GetBar(dtPreviousClose);
    Print(
    "The close price on the 4:15 PM bar was: " + Close[barsAgo].ToString());

    I've tried all sorts of variations. If you could tell me how to go about this, I would be appreciative.

    #2
    Can you please share how you defined your custom DateTime variable? Please also keep in min NS is case sensitive, so dtPreviousClose is not DtPreviousClose.

    Comment


      #3
      Thank you, Bertrand.
      The code follows. TradeDate is entered by the user. dtPreviousClose prints out as 12/29/2010 4:15:00 PM

      protectedoverridevoid OnStartUp()
      {
      Print(TradeDate);
      DateTime dtTradeDate = DateTime.Parse(TradeDate);
      DateTime dtDatePrevTradeDate = dtTradeDate -
      new TimeSpan(24,0,0);
      Print(dtDatePrevTradeDate);
      DateTime dtPreviousClose = dtDatePrevTradeDate +
      new TimeSpan(16,15,0);
      Print(dtPreviousClose);
      }
      ///<summary>
      /// Called on each bar update event (incoming tick)
      ///</summary>
      protectedoverridevoid OnBarUpdate()
      {
      int barsAgo = CurrentBar - Bars.GetBar(new DateTime (dtPreviousClose));
      Print(
      "The close price on the 4:15 PM bar was: " + Close[barsAgo].ToString());
      }
      Last edited by sgordet; 12-31-2010, 07:10 AM.

      Comment


        #4
        Your variable definition is local only to OnStartUp(), please try defining it globally under Variables and then assign the value as you had it before and then recheck.

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          Your variable definition is local only to OnStartUp(), please try defining it globally under Variables and then assign the value as you had it before and then recheck.
          The compiler is now telling me that the best overloaded method match has some invalid arguments (in OnBarUpdate. The revised code with the DateTime variable initialization in the Variables region follows:

          #region Variables
          privatestring tradeDate = @"01/01/2011"; // Default setting for TradeDate
          // User defined variables (add any user defined variables below)
          // Initialize new DateTime
          DateTime dtPreviousClose = DateTime.Now;
          #endregion
          protectedoverridevoid Initialize()
          {
          CalculateOnBarClose =
          false;
          }
          protectedoverridevoid OnStartUp()
          {
          Print(TradeDate);
          DateTime dtTradeDate = DateTime.Parse(TradeDate);
          DateTime dtDatePrevTradeDate = dtTradeDate -
          new TimeSpan(24,0,0);
          Print(dtDatePrevTradeDate);
          dtPreviousClose = dtDatePrevTradeDate +
          new TimeSpan(16,15,0);
          Print(dtPreviousClose);
          }
          protectedoverridevoid OnBarUpdate()
          {
          int barsAgo = CurrentBar - Bars.GetBar(new DateTime (dtPreviousClose));
          Print(
          "The close price on the 4:15 PM bar was: " + Close[barsAgo].ToString());
          }

          Comment


            #6
            Hello,

            Thanks for your note.

            Issue is most likely here, but without running your code on my side its hard to tell.

            Bars.GetBar(new DateTime (dtPreviousClose));

            Should be:

            Bars.GetBar(dtPreviousClose);

            Let me know if I can be of further assistance.





            BrettNinjaTrader Product Management

            Comment


              #7
              Originally posted by NinjaTrader_Brett View Post
              Hello,

              Thanks for your note.

              Issue is most likely here, but without running your code on my side its hard to tell.

              Bars.GetBar(new DateTime (dtPreviousClose));

              Should be:

              Bars.GetBar(dtPreviousClose);

              Let me know if I can be of further assistance.



              Thank you, everything now compiles and runs properly.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              639 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              366 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              107 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              569 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              572 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X