Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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/[B]13[/B]/2021 3:00:00 PM : 3794.25 - 3803.75 = -9.5
            1/[B]14[/B]/2021 3:00:00 PM : 3792 - 3791.25 = 0.75
            1/[B]15[/B]/2021 3:00:00 PM : 3808.5 - 3762.25 = 46.25
            1/[B]19[/B]/2021 3:00:00 PM : 3793 - 3790.5 = 2.5
            1/[B]20[/B]/2021 3:00:00 PM : 3750 - 3845 = -95
            1/[B]21[/B]/2021 3:00:00 PM : 3796.75 - 3846 = -49.25
            1/[B]22[/B]/2021 3:00:00 PM : 3841.5 - 3834.25 = 7.25
            1/[B]25[/B]/2021 3:00:00 PM : 3846.75 - 3848.5 = -1.75
            1/[B]26[/B]/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 DJ888, 04-16-2024, 06:09 PM
              4 responses
              12 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by terofs, Today, 04:18 PM
              0 responses
              11 views
              0 likes
              Last Post terofs
              by terofs
               
              Started by nandhumca, Today, 03:41 PM
              0 responses
              7 views
              0 likes
              Last Post nandhumca  
              Started by The_Sec, Today, 03:37 PM
              0 responses
              3 views
              0 likes
              Last Post The_Sec
              by The_Sec
               
              Started by GwFutures1988, Today, 02:48 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Working...
              X