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

Simple loop to fill array with last 10 values of Low[]

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

    Simple loop to fill array with last 10 values of Low[]

    Below are some newbie C# questions for looping in NT8.

    If I want to fill the array called "arr[]" with the last 10 values of Low[] looking back from the CurrentBar, would the following work


    Code:
    int len = 10;
    
    protected override void OnBarUpdate()
            {
    
                if (CurrentBar<len)
                     return;
                
                size=0;
                for (int i=CurrentBar-len; i<=CurrentBar; i++)
                {
                    arr[size]=Low[i];  //may not work for 10-bar look back from, bar 63 (i=53,54,...,62)
                    size+=1;
                }
    If there are only 10 bars on a chart (0,1,...9), would Low[0] be equal to the value of Low for the 9th bar or the Low for the 0th bar (first) bar?

    Or should I loop backward to pull values of Low[0], Low[1], etc, like this:

    Code:
                size=0;
                for (int i=CurrentBar-len; i<=CurrentBar; i++)
                {
                    arr[size]=Low[size];   //this would always fetch Low[0],Low[1],etc
                    size+=1;
                }
    In addition, do I need to subtract 1 from the upper loop limit, like:

    Code:
    for (int i=CurrentBar-len; i<=CurrentBar-1; i++)
                {
                    arr[size]=Low[size];   
                    size+=1;
                }
    Last, within OBU, does CurrentBar ramp up from 0,1,...,9 or 1,2,...,10?
    Last edited by pel11; 07-24-2018, 06:16 PM.

    #2
    Hello pel11,

    Thanks for your post.

    With any question about how something work, keep in mind that you can always add Print statements to your code to prove the performance when you test it yourself. The more you use Print the better you will be able to debug your code an better understand what is happening. For further debugging tips please see: https://ninjatrader.com/support/foru...ead.php?t=3418

    The CurrentBar starts at bar 0 which is the first bar of data loaded, so if your chart has 5 days of data loaded, bar 0 is the first bar of the first day. CurrentBar is then incremented bar by bar going towards the right edge of the chart. Reference: https://ninjatrader.com/support/help...currentbar.htm

    Regarding Low[0], the [0] is the "bars ago" index, so a 0 (zero) would be the current bar being processed or just completed (depending on the CalculateOnBarClose setting). An index of [1] would be the previous bar, [2] the bar before that, [3] the bar before that, etc. etc.

    Populating your array may be a bit more straightforward like this:

    [I]if (CurrentBar < 10) return;

    for (int i = 0; i < 10; i++)
    {
    arr[i] = Low[i];
    Print ("BarsAgo i = "+i+" Low[i] = "+Low); // optional statement if you want to check
    }


    In this example, arr[0] would contain the most recent Low, arr[1] the bar before that, etc. etc.

    If you want to know exactly where you are, you can either print the (close) time[0] of the bar or the CurrentBar number or both, for example:

    [I]Print ("CB: "+CurrentBar+" Time:"+Time[i].TimeOfDay+"BarsAgo i = "+i+" Low[i] = "+Low);
    Using the above would result in something like (I used 15 minute bars):

    CB: 334 Time:07:30:00 BarsAgo i = 0 Low[i] = 2817.75
    CB: 334 Time:07:15:00 BarsAgo i = 1 Low[i] = 2817.25
    CB: 334 Time:07:00:00 BarsAgo i = 2 Low[i] = 2817
    CB: 334 Time:06:45:00 BarsAgo i = 3 Low[i] = 2815.75
    CB: 334 Time:06:30:00 BarsAgo i = 4 Low[i] = 2816.25
    CB: 334 Time:06:15:00 BarsAgo i = 5 Low[i] = 2815.25
    CB: 334 Time:06:00:00 BarsAgo i = 6 Low[i] = 2814.5
    CB: 334 Time:05:45:00 BarsAgo i = 7 Low[i] = 2814
    CB: 334 Time:05:30:00 BarsAgo i = 8 Low[i] = 2816
    CB: 334 Time:05:15:00 BarsAgo i = 9 Low[i] = 2816.5

    The print statements allow you to better see what data is actually being processed.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the useful comments, and the Print descriptions -- I'll use the Print function to debug.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by algospoke, Yesterday, 06:40 PM
      2 responses
      19 views
      0 likes
      Last Post algospoke  
      Started by ghoul, Today, 06:02 PM
      3 responses
      14 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by jeronymite, 04-12-2024, 04:26 PM
      3 responses
      45 views
      0 likes
      Last Post jeronymite  
      Started by Barry Milan, Yesterday, 10:35 PM
      7 responses
      20 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by AttiM, 02-14-2024, 05:20 PM
      10 responses
      180 views
      0 likes
      Last Post jeronymite  
      Working...
      X