Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Proper use of Close[CurrentBar] ?

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

    Proper use of Close[CurrentBar] ?

    I am seeing things? I created a test indicator. The relevant settings/code is here:

    Code:
     
    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    
    
    CalculateOnBarClose = true;
    
    Overlay = true;
    PriceTypeSupported = false;
     
    _testTag = UniqueID();
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar > 0)
    {
    int currentBarMinus1 = CurrentBar - 1;
    DrawTextFixed(_testTag, CurrentBar.ToString() + " " + Close[currentBarMinus1].ToString() + " " + Close[CurrentBar].ToString() + " " + (Close[currentBarMinus1] < Close[CurrentBar]).ToString(), TextPosition.TopRight); 
    }
    I am seeing the exact same value displayed for current and current minus 1 -- they never change (though my bar count does.) The values do not match the chart. I have tried this live, historic, simulator, etc. Am I using the Close value incorrectly? What is wrong with this code?

    I do own the lifetime, multivendor version, and I am connected to ZenFire ES-12-08. I am completely up to date. The test chart is set at 1m and there are no other indicators loaded.




    #2
    Please check and see if the Control Center logs has any errors.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      No Errors

      No errors whatsoever in the log.

      Comment


        #4
        What is this _testTag? Bring it down to its simplest form. Instead of using DrawTextFixed just use Print() and look at the Output Window.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Responses

          _testTag was just a unique ID to tag the graphic. In this case, a GUID.

          Code:
           
          Print(CurrentBar.ToString() + " " + Close[currentBarMinus1].ToString() + " " + Close[CurrentBar].ToString() + " " + (Close[currentBarMinus1] < Close[CurrentBar]).ToString());
          The above code always prints:

          3532 867.5 867 False

          3532: Current Bar
          867.5: Close[currentBarMinus1]
          867: Close[CurrentBar]False: (Close[currentBarMinus1] < Close[CurrentBar]).ToString())

          Note: The current market close on the chart is 827.

          This occurs for every bar that is drawn. There is NO other code in the indicator--it is barebones.

          Comment


            #6
            Ok maybe I am misunderstanding. If you don't mind could you just post the indicator so I can test on my end? Thank you.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Attached...

              Attached...
              Attached Files

              Comment


                #8
                moflaherty,

                This is what is going on. Remember CurrentBar increments in a forward fashion. You pass that into a barsAgo indexing. Because CurrentBar keeps incrementing you essentially end up constantly referencing the exact same bar.

                Let us give values for this example.
                CurrentBar = 0, close = 100, Close[CurrentBar] = Close[0] = 100
                CurrentBar = 1, close = 125, Close[CurrentBar] = Close[1] = 100
                CurrentBar = 2, close = 150, Close[CurrentBar] = Close[2] = 100
                etc.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Question

                  So just to be clear, I should be doing this every time the method is called?

                  Print(Close[0].ToString() + " " + Close[1].ToString() + " " + (Close[0] < Close[1]).ToString());

                  Comment


                    #10
                    Right. If you are trying to get the most recent bar vs. the previous bar that is what you would do.

                    Doing CurrentBar vs CurrentBar - 1 would be the first bar vs the second bar on the chart.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Last Question

                      OK, thank you for your help!

                      Just to get my mind around the "index" approach, is there a way to get the Close by using CurrentBar?

                      Again, thanks!

                      Comment


                        #12
                        Close of which bar? Just keep in mind CurrentBar increments forward while the [] index goes backwards.

                        If you wanted say close at a very specific point in time you could use something like GetBar() to get the index and then just pass in the return from GetBar() into Close[].
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Response

                          Let me ask it like this. Is there a way to reference a bar by index? For example, if CurrentBar is equal to 3640, how can I reference it?

                          Any plans to add Linq? It would be cool to query all the bars with lamda expressions...

                          Comment


                            #14
                            moflaherty,

                            I really don't understand what you mean. CurrentBar = 3640 is the same as going Close[0]. Close[0] is always the last bar. CurrentBar is always the number of bars on the chart. If you go Close[CurrentBar] you will always go back to the beginning of the chart. You can access any bar inbetween by passing in the appropriate barsAgo parameter.

                            You will need to custom program your own LinQ.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              ok, sounds interesting

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,607 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              19 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              16 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X