Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Local Variable Updating with Every Tick on Market Analyzer

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

    Local Variable Updating with Every Tick on Market Analyzer

    This is technically an indicator coding question, but the problem I have only appears in the market analyzer.

    Here's an example (abridged) indicator:

    // Local variable
    int counter = 0;

    // On Bar update

    if (Close[0] >= Low[1])
    counter = counter + 1;

    Value.Set(counter);

    The above indicator essentially keeps count of the times a bar closes above its previous low. Let's say going into today, the number (on a daily basis) is 30.

    If I put into a column of the market analyzer the above indicator on daily data, and set Calculate on Bar Close to true, it'll read 30, the number at previous day's close.

    If I set Calculate on Bar Close to false, I'm hoping that if the latest tick is above yesterday's low, it'll read 31 and if it isn't, it'll read 30.

    Instead, it appears that the local counter variable continues to increase with every tick as long as the latest tick is above yesterday's low. The indicator ends up reading several thousand (or however many ticks) instead of 30 or 31.

    Is there a way to get what I originally desired, ie. 30 or 31 depending on incoming tick?

    #2
    Hello pretender,

    Yes it would be possible, but it would require some more custom code with more conditions for your if() statement. For example you can check the check the CurrentBar to make sure that you code is only calling your condition once in the same bar instead of multiple times. Here is a link to our Help Guide that goes over CurrentBar.

    http://www.ninjatrader.com/support/h...currentbar.htm

    Let us know if we can be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      For example you can check the check the CurrentBar to make sure that you code is only calling your condition once in the same bar instead of multiple times
      That's exactly what I want, but I don't see how I would do that based on the info in the help guide.

      Comment


        #4
        Hello pretender,

        The Help Guide will give you what CurrentBar represents and what CurrentBar will return so that you can use it how you like. For example:

        Code:
        #region Variables
        private int		testVar =0;
        #endregion
        
        protected override void OnBarUpdate()
        {
        
        if(CurrentBar > testVar && Close[0] >= Low[1])
        {
        	counter = counter + 1;
        	testVar = CurrentBar;
        }

        Let us know if we can be of further assistance.
        JCNinjaTrader Customer Service

        Comment


          #5
          I went through your suggested code and found that it didn't serve my purpose. What happened was that the market analyzer would read one tick and only one tick. I wanted the indicator value to change based on the incoming tick value, without continuously changing the local variable value.

          I found my own solution, and am sharing it below:

          #region Variables
          double counter =0;
          // will see below why it needs to be double and not int
          #endregion

          protected override void OnBarUpdate()
          {
          if (CurrentBar < 1)
          Value.Set(
          0);

          elseif (CurrentBar >= 1)
          {
          int localcounter = 0;

          if (Close[0] >= Low[1])
          localcounter =
          1;


          counter = Value[
          1] + localcount;
          //Calling Value is the only way the counter doesn't continuously update
          //Value is a double type and that's why counter needs to be double too

          Value.Set(count);
          }



          }

          Comment


            #6
            Hello pretender,

            Thanks for sharing your solution for other users to be able to see.
            JCNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by firstlanetech, Today, 11:31 PM
            0 responses
            2 views
            0 likes
            Last Post firstlanetech  
            Started by AH369, 07-03-2024, 10:17 AM
            8 responses
            832 views
            1 like
            Last Post Karado58  
            Started by FAQtrader, 04-25-2024, 12:00 PM
            58 responses
            1,289 views
            0 likes
            Last Post rene69851  
            Started by kevtancw, Today, 07:09 PM
            0 responses
            4 views
            0 likes
            Last Post kevtancw  
            Started by LanceFulk, Today, 06:28 PM
            0 responses
            6 views
            0 likes
            Last Post LanceFulk  
            Working...
            X