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

Range Charts, Indicator and Kinetick

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

    Range Charts, Indicator and Kinetick

    I have this simple bit of code in an indicator. The purpose is to draw horizontal lines where the current Range bar might end. Its not fancy.

    This code is read on every tick, but I use a simple boolean flag to get it to only be printed once per range bar (on the first tick of the next range bar, the lines are removed).

    Code:
    if(FirstTickOfBar)
    {
    if(drawingProjectedEndPoints && BarsPeriod.Id == PeriodType.Range)
        {
    	{
    		RemoveDrawObject(string.Format("high_{0}", CurrentBar-1));
    		RemoveDrawObject(string.Format("low_{0}", CurrentBar-1));
    		drawingProjectedEndPoints = false;
    	}
        }
    }
    
    if(Bars.Period.Id == PeriodType.Range)
    {
    	double currentRange = High[0] - Low[0];
            if(currentRange == ((Bars.Period.Value*Instrument.MasterInstrument.TickSize) - Instrument.MasterInstrument.TickSize))
    	{
    		if(!drawingProjectedEndPoints)
    	        {
    							DrawHorizontalLine(string.Format("high_{0}", CurrentBar), true, High[0] + 2*Instrument.MasterInstrument.TickSize, Color.FromKnownColor(KnownColor.DarkGoldenrod), DashStyle.Dot, 2);
    							DrawHorizontalLine(string.Format("low_{0}", CurrentBar), true, Low[0] - 2*Instrument.MasterInstrument.TickSize, Color.FromKnownColor(KnownColor.DarkGoldenrod), DashStyle.Dot, 2);
    							drawingProjectedEndPoints = true;
    		}
    	}
    }
    When I am connected to Kinetick, real-time. This code works great for the ES and YM. But it dosen't work at all for the CL. I can't "see" why. Is it possible there is something in the Kinetick tick feed for CL vs. ES/YM that would make this not work?

    #2
    Hello,

    Thanks for your post.

    I am using a 4 Range ES 03-14 chart in replay mode but I am not seeing that this indicator has drawn for all of Feb 12th.

    What interval are you using? When was the last time this indicator drew on the ES?

    I am also printing the values:
    Print(Instrument.FullName + " | " + currentRange + " - " + ((Bars.Period.Value*Instrument.MasterInstrument.Ti ckSize) - Instrument.MasterInstrument.TickSize));
    with the CL 03-14 and I am seeing prints, however, these two values are never equal, the same seems to be true with the ES 03-14 yesterday.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      Not sure if this will help but the "RemoveDrawObject" only requires the name of the tag to remove, it does not need a location...

      Actually after thinking about it for a moment, if all you are doing is drawing two lines every time you get a new bar, you can make it really simple by using the same tag each time. You do not even need the removedrawobject because every time you draw the line and you use the same name it will automatically remove or update the line.

      So simply draw the high line and the low line and use static names such has "high" and "low" and you should be good to go.

      Something like:

      if (FirstTickOfBar)
      {
      DrawHorizontalLine ("high",....)
      DrawHorizontalLine ("low",...)
      }


      Hope this helps.
      Last edited by Tasker-182; 02-13-2014, 12:34 PM.

      Comment


        #4
        Can you can use replay mode to test this, as it is my understanding that there is some compression in the data? Maybe I'm wrong about that, but I know when I've tested other tick by tick code in replay, that data is compressed.

        In the ES, 3-14, I'm using range 8 bars, Extended trading hrs. In the CL 3-14, Extended trading hrs, I'm using range 16 bars. Kinetick, Real-Time connection.

        For the ES, in some cases, they will print, and then be immediately cleared as it moves 2 ticks and opens a new range bar. If the market is moving a little "slower" they definitively print. This idea behind this code is not a perfect solution, but I figured it would work most of the time.

        Comment


          #5
          Hello,

          Really there isn't much difference between the Market Replay which is a recording of live data, and the real data feed. The indicator should work the same.

          I am re-testing using 8 Range this time.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            It just printed them on the most recent ES, range 8 bar.
            Attached Files

            Comment


              #7
              I would agree that is how market replay is suppose to work. So maybe my prior experience is because I usually download the replay data from Ninjatrader instead of recording it myself.

              Comment


                #8
                Hi ,

                I think you should add a print to your code and open the output window. I'm running this over the ES 03-14 8 Range and I'm still not getting a match with the currentRange.

                Code:
                if(Bars.Period.Id == PeriodType.Range)
                {
                double currentRange = High[0] - Low[0];
                Print(Instrument.FullName + " | " + currentRange + " - " + ((Bars.Period.Value*Instrument.MasterInstrument.TickSize) - Instrument.MasterInstrument.TickSize));
                if(currentRange == ((Bars.Period.Value*Instrument.MasterInstrument.TickSize) - Instrument.MasterInstrument.TickSize))
                {
                Print(Instrument.FullName + " -  " + drawingProjectedEndPoints.ToString());
                if(!drawingProjectedEndPoints)
                {
                DrawHorizontalLine(string.Format("high_{0}", CurrentBar), true, High[0] + 2*Instrument.MasterInstrument.TickSize, Color.FromKnownColor(KnownColor.DarkGoldenrod), DashStyle.Dot, 2);
                DrawHorizontalLine(string.Format("low_{0}", CurrentBar), true, Low[0] - 2*Instrument.MasterInstrument.TickSize, Color.FromKnownColor(KnownColor.DarkGoldenrod), DashStyle.Dot, 2);
                drawingProjectedEndPoints = true;
                }
                }
                }
                I'm getting pretty consistently in the output window
                CL 03-14 | 0.039999999999992 - 0.03
                ES 03-14 | 2 - 1.75

                This means the lines are not being drawn because the condition statement is not evaluating to true.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Ok, I did as you suggested and I found the problem.

                  I noticed that when you print out the value of High[0]-Low[0] for CL, you get a decimal number with values out in like the 16th place....but when you print that difference for ES, you get numbers like 1.5, 1.75, 2, etc...

                  So as a test, I rounded changed the code to this:

                  double currentRange = Math.Round(High[0]-Low[0], 2)

                  And then this worked as expected in crude oil (I got a print statement when then currentRange was 0.15, with range 16 bars).

                  So, why would it be necessary to round CL but not ES?

                  Comment


                    #10
                    Hello ,

                    The issue is the floating point arithmetic done by C#.

                    Below is a link to a description of this.
                    http://www.ninjatrader.com/support/f...ead.php?t=3929

                    Rounding to ticksize or to the hundredths place should do the job.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Yes, interesting. Thanks for your help. I will modify my script accordingly so that it works for things like NG and ZN as well.

                      But one thing that does make sense to me, even after taking into consideration how C# handles floating point math....why does it work for without rounding for ES, at least for me?

                      Comment


                        #12
                        Hi ATX_tdr,

                        I do not know specifically why the CL, but I do know it has something to do with the tick size and point value.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Never check a double for equality - this is forbidden

                          Checking a variable of type double for equality with another double value is an error by itself and should be avoided at all cost. The floating point arithmetic usually does not yield exact results, therefore you should only use a test for equality with integers or booleans.

                          In your case, you can use a grid. As the value on the right side of your equation can only take values representing the multiple of the tick size, a simple way to perform the check would be

                          Code:
                          if (currentRange > (Bars.Period.Value - 1.5) * TickSize 
                              && currentRange < (Bars.Period.Value - 0.5) * TickSize)
                          {
                             .......
                          }
                          For variables of type double you need to check for inequalities and never check for equality!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by PaulMohn, Today, 05:00 AM
                          0 responses
                          8 views
                          0 likes
                          Last Post PaulMohn  
                          Started by ZenCortexAuCost, Today, 04:24 AM
                          0 responses
                          6 views
                          0 likes
                          Last Post ZenCortexAuCost  
                          Started by ZenCortexAuCost, Today, 04:22 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post ZenCortexAuCost  
                          Started by SantoshXX, Today, 03:09 AM
                          0 responses
                          16 views
                          0 likes
                          Last Post SantoshXX  
                          Started by DanielTynera, Today, 01:14 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post DanielTynera  
                          Working...
                          X