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

Synchronizing Custom 2000 Tick Bar End Times with Chart in NinjaTrader

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

    Synchronizing Custom 2000 Tick Bar End Times with Chart in NinjaTrader

    Hello,


    I'm working on a custom indicator in NinjaTrader 8 that calculates 2000 tick bars using 1 tick data. However, I'm encountering an issue where my calculated 2000 tick bar end times are not aligning with the 2000 tick bars displayed on the chart. Specifically, my custom bar stats print with a consistent offset, showing 300 ticks remaining on the chart's 2000 tick bar. It will be 300 ticks different every bar, but then when I rerun the indicator it will be a different, consistent offset, so I can't just hard code a certain number. I would like to do this without adding the 2000 tick data as a secondary series if possible.

    Here is my code for reference:

    Code:
    using System;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using System.Windows.Media;
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class TestIndicator : Indicator
        {    
            private double currBarHigh = double.MinValue;
            private double currBarLow = double.MaxValue;
            private double currBarOpen = 0;
            private double currBarClose = 0;
            
            private int tickCounter = 0;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Custom Indicator";
                    Name = "TestIndicator";
                    Calculate = Calculate.OnEachTick;
                    IsOverlay = true;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(Data.BarsPeriodType.Tick, 1); // Adding tick data series with 2000 ticks per bar
                }
            }
            
            protected override void OnBarUpdate()
            {
                // Ensure the script runs on the primary data series (BarsInProgress == 0)
                if (BarsInProgress != 0)
                    return;
    
                // Ensure there are enough bars for lookback
                if (CurrentBar < 5)
                    return;
    
                double currPrice = Close[0]; // Price of the current tick
                
                tickCounter++; // Increase tick by one until 2000 are reached
                if (tickCounter == 1)
                {
                    currBarOpen = currPrice; // Set open for new 2000 tick bar
                }
    
                currBarHigh = Math.Max(currBarHigh, currPrice); // Check each tick to see if new high for the bar that is currently printing
                currBarLow = Math.Min(currBarLow, currPrice);
    
                if (tickCounter >= 2000)
                {
                    currBarClose = Close[0]; // Save close value on the 2000th tick
                    Print($"ending value for bar open: {currBarOpen}");
                    Print($"ending value for bar high: {currBarHigh}");
                    Print($"ending value for bar low: {currBarLow}");                
                    Print($"ending value for bar close: {currBarClose}");
                    
                    // Reset for the next bar
                    tickCounter = 0;
                    currBarHigh = double.MinValue;
                    currBarLow = double.MaxValue;
                }
            }
        }
    }
    ​

    #2
    Hello LordNoah,

    Thank you for your post.

    Are you noticing this happen in real-time, historical, or both?

    Have you enabled Tick Replay?

    What is the primary data series you are running this script on? It's not calculating based on the 1-tick series since you have if (BarsInProgress != 0) return;​
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hi Gaby,

      Thank you for your quick response.

      This is happening for both real-time and historical bars.

      I do not believe I have tick-replay enabled. Is that what I am missing?

      I am running the indicator on the ES 2000 tick chart. I've been testing the indicator using market replay. I'm not sure why I had if (BarsInProgress != 0) return; in that code I posted. When using if (BarsInProgress != 1) return; the same issue happens.

      When testing live, the "ending value for bar close: xxxx.xx" value is the correct current price, but the bar on the chart is in the middle of printing. The historical values are off too so I am assuming it is the same issue for both.


      Here is the entire code for reference, thanks again:

      Code:
      using System;
      using System.ComponentModel.DataAnnotations;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Data;
      using System.Windows.Media;
      
      
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class TestIndicator : Indicator
          {    
              private double currBarHigh = double.MinValue;
              private double currBarLow = double.MaxValue;
              private double currBarOpen = 0;
              private double currBarClose = 0;
              
              private int tickCounter = 0;
              
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description = @"Custom Indicator";
                      Name = "TestIndicator";
                      Calculate = Calculate.OnEachTick;
                      IsOverlay = true;
                  }
                  else if (State == State.Configure)
                  {
                      AddDataSeries(Data.BarsPeriodType.Tick, 1); // Adding tick data series with 2000 ticks per bar
                  }
              }
      
              
              protected override void OnBarUpdate()
              {
                  if (BarsInProgress != 1)
                      return; // Ensure the script runs on the main data series. If data series isn't 1, return
      
                  if (CurrentBar < 5)
                      return; // Ensure there are enough bars for lookback
      
      
                  double currPrice = Close[0]; // Price of the curent tick
                  
                  
                  tickCounter++;     // Increase tick by one until 2000 are reached
                  if (tickCounter == 1)
                  {
                      currBarOpen = currPrice;    // Set open for new 2000 tick bar
                  }
                  
                  currBarHigh = Math.Max(currBarHigh, currPrice);   // Check each tick to see if new high for the bar that is currently printing
                  currBarLow = Math.Min(currBarLow, currPrice);
      
                  if (tickCounter >= 2000)
                  {
                      currBarClose = Close[0];  // Save close value on 2000th tick            
                      Print($"ending value for bar close: {currBarClose}");
                      // ^^^^ This is where the newly made 2000 tick bar prints its stats
                      // This prints with a consistent offset to the end of the true 2000 tick bars displayed on my chart
                      // i.e. This prints with 300 ticks still remaining for the chart 2000 tick bar
      
                      tickCounter = 0;
                      currBarHigh = double.MinValue;
                      currBarLow = double.MaxValue;
                  }
              }
           }
        }​

      Comment


        #4
        i just enabled tick replay from the control center and on the data series but am having the same problem.

        Comment


          #5
          Hello LordNoah,

          Thank you for your response.

          I recommend debugging the script using prints. To understand why the script is behaving as it is, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

          In the script add prints (outside of any conditions) that print the values of every variable used in every condition.

          The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.



          Save the output from the output window to a text file. Let me know if you need assistance creating a print or analyzing the output. ​​
          Gaby V.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by crewtrades, 06-13-2024, 10:50 AM
          4 responses
          30 views
          0 likes
          Last Post crewtrades  
          Started by kingsteven, Today, 08:34 PM
          1 response
          3 views
          0 likes
          Last Post bltdavid  
          Started by biegea, Today, 08:54 PM
          0 responses
          5 views
          0 likes
          Last Post biegea
          by biegea
           
          Started by rene69851, Today, 08:36 PM
          0 responses
          2 views
          0 likes
          Last Post rene69851  
          Started by chartish, 04-11-2019, 12:40 PM
          15 responses
          3,832 views
          0 likes
          Last Post joselube001  
          Working...
          X