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

Volumetric Bars as Secondary Data Series

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

    Volumetric Bars as Secondary Data Series

    I've an example below that I'm using to access Volumetric Bars based on the following link. It gives me an error. Here's the link:



    My example is below:

    What am I missing in this code to get it to work?

    Code:
    else if (State == State.Configure)
    {
    AddVolumetric(Instrument.FullName, BarsPeriodType.Volume, UserDefinedVolume, VolumetricDeltaType.BidAsk, 1);
    // additional data series
    }
    
    protected override void OnBarUpdate()
    {
    if (Bars == null)
              return;
    
    if (CurrentBars[0] < 24 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1 || CurrentBars[4] < 1)
    return;
    
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType barsType = BarsArray[1].BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType;
    
    if (barsType == null)
              return;
    
             double price;
    
    var deltaSL = Math.Abs(barsType.Volumes[CurrentBar].DeltaSl);
    var deltaSH = Math.Abs(barsType.Volumes[CurrentBar].DeltaSh);
    
    var deltaThisBar = barsType.Volumes[CurrentBar].BarDelta;
    var deltaLastBar = barsType.Volumes[CurrentBar-1].BarDelta;

    #2
    Hello AgriTrdr,

    Thanks for your post.

    You mentioned that you are getting an error but you did not mention what exactly that error is.

    What exactly does the error message report?

    If possible please send a screenshot of the full error message so I may accurately assist.
    • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
    • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.

    When using the AddVolumetric() method, you must hardcode all the arguments into the method.

    From the help guide: "Arguments supplied to AddVolumetric() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."

    This means that you would need to hardcode the instrument into the method instead of using Instrument.FullName. You also need to hardcode the basePeriodType argument instead of using UserDefinedVolume.

    See this help guide page for more information about AddVolumetric() and sample code: https://ninjatrader.com/support/help...volumetric.htm

    See the sample code on this help guide page that demonstrates accessing OrderFlowVolumetricBars values, such as BarDelta: https://ninjatrader.com/support/help...tric_bars2.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,

      Thanks for your response. The error that I'm getting is:

      Error on calling 'OnBarUpdate' method on bar 24: Index was outside the bounds of the array.

      Thanks

      Comment


        #4
        Hello AgriTrdr,

        Thanks for your notes.

        This error message indicates that you are trying to reference an index in an array where the index does not exist.

        As one example, calling Close[1] when CurrentBar is 0 (the very first bar) will cause this error.

        The reason for this is that there will not be a bar ago; essentially there is not enough data at bar 0 to look back one bar.

        The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling OnBarUpdate() for each bar. In this specific example, it would be needed to wait for the second bar to be built (from all series if there are multiple series added), to ensure that you have enough data to call an index 1 bar ago.

        Below is a link to the help guide on this specific example of an indexing error.
        https://ninjatrader.com/support/help...nough_bars.htm

        Also, see the help guide links below on CurrentBar and CurrentBars.
        http://ninjatrader.com/support/helpG...currentbar.htm
        https://ninjatrader.com/support/help...urrentbars.htm

        Indexing errors can also occur when using an invalid index with arrays or collections. Below are public links to 3rd party educational sites on arrays and index out-of-range errors.
        Create and loop over a string array. Access array Length and get elements at indexes.


        What was the exact index you tried to use that was invalid?

        Did you ensure the Count of that object is equal to or greater than the index used?

        Debugging prints should be added to the script that prints out CurrentBars[0], CurrentBars[1], CurrentBars[2], CurrentBars[3], CurrentBars[4], and the indexes being used in your script.

        ​Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          I did some debugging and realize the issue is with referencing the Volumetric Bars in the strategy. I modified the code inside OnBarUpdate. I changed the Volumetric Bars to BarsArray.Length -1 and added BarsInProgress. The strategy is enabling now without errors but it doesn't run. What am I missing here?

          Code:
          if (Bars == null)
                    return;
          
          
          if (CurrentBars[0] < 24 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1 || CurrentBars[4] < 1)
          return;
          
          int idx = BarsArray.Length - 1;
          
          NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType barsType = BarsArray[idx].BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType;
          
          if (barsType == null || BarsInProgress != idx)
                    return;
          
          double price;
          
          var deltaSL = Math.Abs(barsType.Volumes[CurrentBar].DeltaSl);
          var deltaSH = Math.Abs(barsType.Volumes[CurrentBar].DeltaSh);
          
          var deltaThisBar = barsType.Volumes[CurrentBar].BarDelta;
          var deltaLastBar = barsType.Volumes[CurrentBar-1].BarDelta;
          ​

          Comment


            #6
            Hello AgriTrdr,

            Thanks for your notes.

            I do not see anything specific in the code you shared that that stands out as incorrect. However, you need to ensure that you are passing the correct BarsArray to the NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType. For example, if the added volumetric series is the first added series in the script, BarsArray[1].BarsType should be used.

            Do you see any error messages in the Log tab of the Control Center when running the script? If so, what does the error message report.

            If the script is not behaving as expected, you would need to further debug the script by adding prints to determine exactly how the script is behaving and processing logic.

            ​Below is a link to a forum post that demonstrates how to use prints to understand behavior.
            https://ninjatrader.com/support/foru...121#post791121

            I have attached a simple reference sample demonstrating how to access the BarDelta value from OrderFlowVolumetricBars that you could view.

            Attached Files
            Last edited by NinjaTrader_BrandonH; 07-17-2023, 08:47 AM.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              i used your code but i got error message as below.how can i fix?

              The name 'bars type' does not exist in the current context. code: CS0103

              Comment


                #8
                Hello f.saeidi,

                Thanks for your notes.

                To clarify, are you stating the OFVolumetricBarsExample script shared in post # 6 is throwing an error?

                Or, is the error being thrown by your own custom script?

                When testing the OFVolumetricBarsExample script on my end I do not see any error messages occuring.

                Please share a screenshot of the error message and the line of code causing the error so we may accurately assist.
                • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
                • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
                If this error is being thrown by your own custom script you could be missing a Using statement in your code.

                Ensure that you have the Using statement below added to the Using Declarations section of your script.

                using NinjaTrader.Data;

                We look forward to assisting further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  thnx for your help.
                  i guess my pc makes that error and today after restart i guess its ok.
                  now i am working on ninja script and need reference with many examples.
                  now i have only nt8 user manual pdf but i need indicator strategy examples.
                  its make me happy hear your advice.\
                  Best regard.

                  Comment


                    #10
                    here can i see bar delta in screen?
                    after complie cant see anything.

                    }
                    else if (State == State.Configure)
                    {
                    AddVolumetric("NQ 06-24", BarsPeriodType.Minute, 1, VolumetricDeltaType.BidAsk, 1);
                    }
                    }

                    protected override void OnBarUpdate()
                    {
                    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = BarsArray[1].BarsType as
                    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;
                    Draw.TextFixed(this, "", "Delta for bar:"+ barsType.Volumes[CurrentBar].BarDelta, TextPosition.TopLeft);​

                    Comment


                      #11
                      Hello f.saeidi,

                      Thanks for your notes.

                      You could find NinjaScript reference samples on the help guide page linked below.

                      Reference Samples: https://ninjatrader.com/support/help...ce_samples.htm

                      I am also linking you to the Educational Resources section of the Help Guide to help you get started with NinjaScript:


                      If you are new to C#, to get a basic foundation for the concepts and syntax used in NinjaScript I would recommend this section of articles in our help guide first:


                      And the MSDN (Microsft Developers Network) C# Language Reference.


                      "here can i see bar delta in screen? after complie cant see anything.​"

                      If you have modified code in your script and compiled to save changes then you should remove the indicator from the chart and re-add the indicator to the chart to ensure any changes made take effect.

                      Do you see any error messages occurring on the screen? Are there any error messages in the Log tab of the Control Center? If so, what exactly do they report?

                      When running a script you are creating it is best practice to keep an eye on the Log tab of the Control Center for any errors that might occur.

                      If the script is not behaving as expected, debugging prints should be added to the script to understand how the logic is evaluating.

                      Below is a link to a forum post that demonstrates how to use prints to understand behavior.


                      We look forward to assisting further.
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        below is all program and i cant see any error.i send here nazdaq with load this indicator since i did new indicator.


                        region Using declarations
                        using System;
                        using System.Collections.Generic;
                        using System.ComponentModel;
                        using System.ComponentModel.DataAnnotations;
                        using System.Linq;
                        using System.Text;
                        using System.Threading.Tasks;
                        using System.Windows;
                        using System.Windows.Input;
                        using System.Windows.Media;
                        using System.Xml.Serialization;
                        using NinjaTrader.Cbi;
                        using NinjaTrader.Gui;
                        using NinjaTrader.Gui.Chart;
                        using NinjaTrader.Gui.SuperDom;
                        using NinjaTrader.Gui.Tools;
                        using NinjaTrader.Data;
                        using NinjaTrader.NinjaScript;
                        using NinjaTrader.Core.FloatingPoint;
                        using NinjaTrader.NinjaScript.DrawingTools;
                        #endregion

                        //This namespace holds Indicators in this folder and is required. Do not change it.
                        namespace NinjaTrader.NinjaScript.Indicators
                        {
                        public class Volumetric : Indicator
                        {
                        protected override void OnStateChange()
                        {
                        if (State == State.SetDefaults)
                        {
                        Description = @"Enter the description for your new custom Indicator here.";
                        Name = "Volumetric";
                        Calculate = Calculate.OnPriceChange;
                        IsOverlay = false;
                        DisplayInDataBox = true;
                        DrawOnPricePanel = true;
                        DrawHorizontalGridLines = true;
                        DrawVerticalGridLines = true;
                        PaintPriceMarkers = true;
                        ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                        //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                        //See Help Guide for additional information.
                        IsSuspendedWhileInactive = true;
                        }
                        else if (State == State.Configure)
                        {
                        AddVolumetric("NQ 06-24", BarsPeriodType.Minute, 1, VolumetricDeltaType.BidAsk, 1);
                        }
                        }

                        protected override void OnBarUpdate()
                        {
                        NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = BarsArray[1].BarsType as
                        NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;
                        Draw.TextFixed(this, "", "Delta for bar:"+ barsType.Volumes[CurrentBar].BarDelta, TextPosition.TopLeft);
                        }
                        }
                        }

                        region NinjaScript generated code. Neither change nor remove.

                        namespace NinjaTrader.NinjaScript.Indicators
                        {
                        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                        {
                        private Volumetric[] cacheVolumetric;
                        public Volumetric Volumetric()
                        {
                        return Volumetric(Input);
                        }

                        public Volumetric Volumetric(ISeries<double> input)
                        {
                        if (cacheVolumetric != null)
                        for (int idx = 0; idx < cacheVolumetric.Length; idx++)
                        if (cacheVolumetric[idx] != null && cacheVolumetric[idx].EqualsInput(input))
                        return cacheVolumetric[idx];
                        return CacheIndicator<Volumetric>(new Volumetric(), input, ref cacheVolumetric);
                        }
                        }
                        }

                        namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                        {
                        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                        {
                        public Indicators.Volumetric Volumetric()
                        {
                        return indicator.Volumetric(Input);
                        }

                        public Indicators.Volumetric Volumetric(ISeries<double> input )
                        {
                        return indicator.Volumetric(input);
                        }
                        }
                        }

                        namespace NinjaTrader.NinjaScript.Strategies
                        {
                        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                        {
                        public Indicators.Volumetric Volumetric()
                        {
                        return indicator.Volumetric(Input);
                        }

                        public Indicators.Volumetric Volumetric(ISeries<double> input )
                        {
                        return indicator.Volumetric(input);
                        }
                        }
                        }

                        #endregion

                        Click image for larger version  Name:	volumetric pic.jpg Views:	0 Size:	97.0 KB ID:	1295370
                        Last edited by f.saeidi; 03-12-2024, 08:20 AM.

                        Comment


                          #13
                          its possible tell me what is problem in script?
                          thnx.

                          Comment


                            #14
                            Hello f.saeidi,

                            Thanks for your notes.

                            I see you do not have a CurrentBars check in the code you shared to ensure there are enough bars processing before the script begins calculations.

                            A CurrentBars check should be used in your indicator's logic to ensure that a certain number of bars have processed before the indicator begins calculation.

                            See the help guide documentation below for more information.

                            CurrentBars - https://ninjatrader.com/support/help...urrentbars.htm
                            Make sure you have enough bars - https://ninjatrader.com/support/help...nough_bars.htm

                            You also need to add a null check for Bars and for barstype in the script. This is seen in the sample code on the Order Flow Volumetric help guide page linked below.

                            Order Flow Volumetric Bars: https://ninjatrader.com/support/help...tric_bars2.htm

                            When making these changes to the script I see the text drawn on the chart. See the attached screenshot.
                            Attached Files
                            Brandon H.NinjaTrader Customer Service

                            Comment


                              #15
                              its possible you upload script that you created? i mean source c# file.
                              thnx

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, Today, 10:57 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Today, 09:29 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Belfortbucks  
                              Started by zstheorist, Today, 07:52 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post zstheorist  
                              Started by pmachiraju, 11-01-2023, 04:46 AM
                              8 responses
                              151 views
                              0 likes
                              Last Post rehmans
                              by rehmans
                               
                              Working...
                              X