Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

H to O Avg Net Change of Daily Bar (on lower timeframe)

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

    H to O Avg Net Change of Daily Bar (on lower timeframe)

    Hello,

    I'm running into a snag trying to build something. On an intraday chart, I want to look at the last 60 days. In those 60 days, only days where the net change was positive. Than grab the percent change from High to Open, and take an average of that.

    Here is what I have so far, though this is not printing the correct value as I did this manually in excel. Any assistance is appreciated.




    if (BarsInProgress == 1)
    {
    if (CurrentBars[1] <= 60) return;

    for (int i = 0; i < 60; i++)
    {
    if (Closes[1][i] > Opens[1][i])
    {
    PosBarHi = (Highs[1][i] - Opens[1][i]) / Opens[1][i];
    sumPosBarHi += PosBarHi;
    PosCount++;
    }
    }

    Print(sumPosBarHi / PosCount);

    }​
    Last edited by ChrisR; 12-09-2024, 12:10 PM.

    #2
    Hello ChrisR,

    If you are comparing against excel the best way to know what is happening would be to print out the values you are calculating in code and see if that is similar to what you are doing in the spreadsheet. Depending on that comparison you can make whatever adjustment is needed to match the math you are doing.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello ChrisR,

      If you are comparing against excel the best way to know what is happening would be to print out the values you are calculating in code and see if that is similar to what you are doing in the spreadsheet. Depending on that comparison you can make whatever adjustment is needed to match the math you are doing.
      Thanks for the reply Jesse,

      That is what I have done already, see below.

      If I use a daily chart with a 60 bar lookback, and print the following print statement:
      Code:
      if (Close[0] > Open[0]) Print((High[0] - Open[0]) / Open[0]);
      This returns 37 net change values, which I can grab the average from using excel.

      However, I am trying replicate this entire sequence on a intraday chart.
      When using a 5min chart, with a 100 day lookback, just to ensure there is enough bar days vs calendar days, I run the following code:

      Code:
      if (BarsInProgress == 1)
      {
         if (CurrentBars[1] < 60) return;
      
         for (int i = 0; i < 60; i++)
         {
            if (Closes[1][i] > Opens[1][i])
            {
               PosBarHi = (Highs[1][i] - Opens[1][i]) / Opens[1][i];
               sumPosBarHi += PosBarHi;
               PosCount++;
      
               Print((Highs[1][i] - Opens[1][i]) / Opens[1][i]);
      
            }
         }
      }
      ​The above prints the same 37 net changes, giving the same average once calculated in excel. So it is working up until this point.

      Now I just want it to calculate the average in the code. Here is the code I am using.

      Code:
      if (BarsInProgress == 1)
      {
         if (CurrentBars[1] < 60) return;
      
         for (int i = 0; i < 60; i++)
         {
            if (Closes[1][i] > Opens[1][i])
            {
               PosBarHi = (Highs[1][i] - Opens[1][i]) / Opens[1][i];
               sumPosBarHi += PosBarHi;
               PosCount++;
            }
         }
      }
      
      Print(sumPosBarHi/PosCount);
      This print statement does not print the correct average, it does not match the two averages previously calculated.​ When printing sumPosBarHi and PosCount separately, they are way off. PosCount for example is returning 2962, when the intent is for it to be 37 to match the print count as in the second code snippet.
      Last edited by ChrisR; 12-09-2024, 01:44 PM.

      Comment


        #4
        Hello ChrisR,

        I am unsure what math excel uses for an average so potentially the math you are doing is different, you could research if microsoft posts the actual equation used to see if you are doing something similar.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello ChrisR,

          I am unsure what math excel uses for an average so potentially the math you are doing is different, you could research if microsoft posts the actual equation used to see if you are doing something similar.
          Thats not the problem though. As mentioned in my previous reply, if I add a print statement inside the
          Code:
          if (Closes[1][i] > Opens[1][i])
          {....}
          and print out PosCount, this is returning 2962. However, there are only 37 times where the close > open in that sequence. So PosCount should be 37 not 2962..there isnt even 2962 days loaded on the chart.

          Comment


            #6
            I identified the issue, just not sure how to solve it.

            On a 5min NQ chart set for 87 day lookback, this actually provides 60 trading days. With this set, the script returns the proper average. But if I change it to 88 days lookback, this now loads 61 trading days. The script now calculates the 60 days search cycle twice, essentially doubling PosCount and sumPosBarHi.

            How can I make this only calculate once, at the last/latest bar? Currently, everytime there is a new 60 days window, it calculates, and than increases the variables, I need those variables to either reset after each calculation or need the script to only calculate once at the last chart bar.

            Appreciate it.
            Last edited by ChrisR; 12-09-2024, 03:22 PM.

            Comment


              #7
              Hello ChrisR,

              You would need to implement some type of counting logic for that purpose, for example count the number of bars you require and then execute the code once at that point and reset the counter. The counter would need to be incremented for each bar so in the future when enough bars happens again you could repeat the action.
              JesseNinjaTrader Customer Service

              Comment


                #8
                I ended up figuring this out. Here is the section of code within OnBarUpdate for those looking for something similar.
                Code:
                if (BarsInProgress == 1)
                {
                   if (CurrentBars[1] < 60) return;
                   PosCount = 0;
                   sumPosBarHi = 0;
                
                   for (int i = 0; i < 60; i++)
                   {
                      if (Closes[1][i] > Opens[1][i])
                      {
                         PosBarHi = (Highs[1][i] - Opens[1][i]) / Opens[1][i];
                         sumPosBarHi += PosBarHi;
                
                         PosCount++;
                      }
                   }
                }

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Tagliareni, 01-18-2025, 10:04 AM
                2 responses
                25 views
                0 likes
                Last Post Tagliareni  
                Started by Buttony, 01-18-2025, 12:10 PM
                10 responses
                51 views
                0 likes
                Last Post rockmanx00  
                Started by frslvr, 04-11-2024, 07:26 AM
                19 responses
                725 views
                1 like
                Last Post Zinovate  
                Started by johnMoss, Today, 04:00 PM
                0 responses
                5 views
                0 likes
                Last Post johnMoss  
                Started by HappyTrader76, Today, 03:56 PM
                0 responses
                5 views
                0 likes
                Last Post HappyTrader76  
                Working...
                X