Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate gap between current and previous day

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

    Calculate gap between current and previous day

    Hello,

    I am trying to calculate the gap between each bars for the close price. I am seeking the difference between previous close and current opening of the bar.

    i tried this double dif = Open[0] - Close[1];

    but its not what i am looking for. Any tips? e.g if previous close was 10 and opening at 8 the difference is 2. I want each gap for all bars in the chart, do i need to for loop?

    TY

    #2
    Hello frankduc,

    I am trying to calculate the gap between each bars for the close price. I am seeking the difference between previous close and current opening of the bar.

    i tried this double dif = Open[0] - Close[1];
    What you have would be the difference between the current bars open and the previous bars close. What was the result and what were the values used for the subtraction when you printed it?

    I want each gap for all bars in the chart, do i need to for loop?
    No, for loops are expensive and rarely suggested. NinjaScript already processes from left to right for every bar. You already loop over the data once to do your calculations when the script processes so there would be no need to use another loop on top of that to do this type of calculation. If you need to keep the data to access it later you could use a Plot or Series.

    I would very likely suggest to use a Print to better understand the result you are seeing with the subtraction you used. You could include the Time and the values so you know what was used:

    Code:
    Print(Time[0] + " : " + Open[0] + " - " + Close[1] + " = " + dif);


    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,

      These are strange result from : Print(Time[0] + " : " + Open[1] + " - " + Close[0] + " = " + dif);

      2021-01-27 17:00:00 : 3845 - 3805,75 = 2,625
      2021-01-27 09:29:00 : 3803 - 3803 = 0
      2021-01-27 17:00:00 : 3845 - 3805,75 = 2,625
      2021-01-27 09:29:00 : 3803 - 3803 = 0
      2021-01-27 17:00:00 : 3845 - 3805,75 = 2,625
      2021-01-27 09:29:00 : 3803 - 3803 = 0
      2021-01-27 17:00:00 : 3845 - 3805,75 = 2,625
      2021-01-27 09:29:00 : 3803 - 3803 = 0
      2021-01-27 17:00:00 : 3845 - 3805,75 = 2,62

      First, how come 3845 - 3805,75 = 2,62?

      Second it only returns the same date 2021-01-27. I would like to get the differences for all bars in the chart from 2020-01-27 to 2021-01-27.

      Will it make a difference if its RTH or instrument settings. Because i need the difference between 4pm and 9:30.

      Ty

      Comment


        #4
        Hello frankduc,

        The print I provided is not doing any math, just printing your variable. The resulting value would be due to whatever you are doing with that variable before that print.

        Second it only returns the same date 2021-01-27
        That would be based on where you put the print and where this logic is used. You are going to need to make a simple example and attach it so we have the details surrounding your questions. Without knowing what you made we could guess about this for many posts.

        The alternative would be to make a simple test which is not in your current script. If you are doing anything more complex than the following you likely are causing the result you are seeing in some way:

        Code:
        protected override void OnBarUpdate()
        {
          if(CurrentBar < 1) return;
         double diff = Open[1] - Close[0];
         Print(Time[0] + " : " + Open[1] + " - " + Close[0] + " = " + diff);
        }

        1/27/2021 8:01:00 AM : 3779.25 - 3779.75 = -0.5
        1/27/2021 8:01:30 AM : 3777.25 - 3781.25 = -4
        1/27/2021 8:02:00 AM : 3780 - 3784.5 = -4.5
        1/27/2021 8:02:30 AM : 3781.25 - 3783.75 = -2.5

        Please let me know if I may be of further assistance.


        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse,

          My goal like i said is to find every difference gap between each bars.

          E.g. if there is only 3 bars on the chart:

          double diff1/27/2021 = Open[1] - Close[2];
          double diff1/26/2021 = Open[2] - Close[3];
          double diff1/25/2021 = Open[3] - Close[4];

          If there is a thousand bars on the chart i dont want to reproduce 1000 times variable diff, i want one variable "diff" that will give the result of each gap align from 2020-01-27 to 2021-01-27.

          Because that:

          [CODE]
          if(CurrentBar < 1)
          {
          double diff = Open[1] - Close[2];
          Print(Time[0] + " : " + Open[1] + " - " + Close[2] + " = " + diff);


          return;
          }
          [/CODE

          Return:

          2021-01-27 10:25:00 : 3797 - 3795,5 = -0,25
          2021-01-27 10:25:00 : 3797 - 3795,75 = -0,25
          2021-01-27 10:25:00 : 3797 - 3795,75 = -0,25
          2021-01-27 10:25:00 : 3797 - 3795,75 = -0,25
          2021-01-27 10:25:00 : 3797 - 3796 = -0,25
          2021-01-27 10:25:00 : 3797 - 3796 = -0,25
          2021-01-27 10:25:00 : 3797 - 3796,25 = -0,25
          2021-01-27 10:25:00 : 3797 - 3796,25 = -0,25
          Its a daily chart of Emini from 2020-01-27 to 2021-01-27.

          It should look like:

          2021-01-27 : 3797 - 3795,5 = 1.5
          2021-01-26 : 3798 - 3795,75 = 2.5
          2021-01-25 : 3799 - 3795,75 = 3,5

          TY

          Comment


            #6
            Hello frankduc,

            You don't need to make more than 1 variable, the platform already loops over the data and calls OnBarUpdate for every bar. The variable would be re calculated each OnBarUpdate.

            Did you try the sample that I provided in a new empty script like I had asked? If not please do that before we continue.

            What I provided would do exactly what you asked for "My goal like i said is to find every difference gap between each bars."

            Here is a print from a daily chart instead to prove what I provided works:

            Code:
            1/13/2021 3:00:00 PM : 3794.25 - 3803.75 = -9.5
            1/14/2021 3:00:00 PM : 3792 - 3791.25 = 0.75
            1/15/2021 3:00:00 PM : 3808.5 - 3762.25 = 46.25
            1/19/2021 3:00:00 PM : 3793 - 3790.5 = 2.5
            1/20/2021 3:00:00 PM : 3750 - 3845 = -95
            1/21/2021 3:00:00 PM : 3796.75 - 3846 = -49.25
            1/22/2021 3:00:00 PM : 3841.5 - 3834.25 = 7.25
            1/25/2021 3:00:00 PM : 3846.75 - 3848.5 = -1.75
            1/26/2021 3:00:00 PM : 3835.5 - 3842.5 = -7

            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Jesse,

              The code you provided works fine, i was trying to make it work in HeikenAshi8. I thought it was possible to use 2 consecutive times if (CurrentBar < (ChartBars.ToIndex-barsAgo)) and if(CurrentBar < 1). Seems like its not possible.

              I was working on HeikenAshi8 because i want to get rid of the gap between each opening days, i did it with the last trading day as you can see on the chart attach.

              Now i was thinking about creating a new series of realigned data where gaps between days dont exist.

              I tried something like :

              Code:
               if (diff > 8)
              {
              Print(Time[0] + " : " + Open[1] + " - " + Close[0] + " = " + diff);
              }
              to sort out every gap larger than 8 pts. I wonder how it will be possible to realigh differences in prices every 9:30 to fit the rest of the series.
              Anyone has done this before or something similar?

              Ty
              Attached Files

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by jleira, Today, 07:08 PM
              0 responses
              1 view
              0 likes
              Last Post jleira
              by jleira
               
              Started by varsha, Today, 06:33 PM
              0 responses
              15 views
              0 likes
              Last Post varsha
              by varsha
               
              Started by NtFan, Today, 04:12 PM
              1 response
              8 views
              0 likes
              Last Post NtFan
              by NtFan
               
              Started by futurenow, Today, 01:44 PM
              2 responses
              38 views
              0 likes
              Last Post voltation  
              Started by Gerik, Today, 12:44 PM
              1 response
              15 views
              0 likes
              Last Post NinjaTrader_Jim  
              Working...
              X