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

BarsInProgress: Array[0] and Array[1]

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

    BarsInProgress: Array[0] and Array[1]

    Greetings.
    Attached pics help explain the problem.
    In Multi-TimeFrame I want to update 30Min(Array[1]) blue & yellow lines only on the half hour – they are updating on the Primary 500 Tick bars. For Array1 data, when I call for: if(BarsInProgress == 1) { and calc the lines }, I get no data. When I call for: if(BarsInProgress == 0) { and calc the lines }, the data is generated but the update time frame is wrong. All the other indicator information is correct and calculated on the Primary Array.
    After reading the forum for hours, I’ve tried various FirstTickOfBar commands without success, what I would need is “FirstTickOfBar[1]” but that doesn’t fly.
    If I press update, the Array1 data is corrected.
    Suggestions? Thanks!
    Attached Files

    #2
    Hello JulieC,

    Thank you for your post.

    Can you provide the lines of code used to set the plot on the 30 minute bar series?

    Comment


      #3
      Hi PatrickH - thanks for your fast response.
      This is the code applied to a 500 Tick chart (Primary) in the example given:
      protectedoverridevoid Initialize()
      {
      Add(PeriodType.Minute,30);
      }
      protectedoverridevoid OnBarUpdate()
      {
      if((CurrentBars[0] <= 21) || (CurrentBars[1] <= 21))
      return;
      else
      {
      // examples of code to arrive at plots (more complicated)
      //if(BarsInProgress == 1) { (I tried this but get no data)
      double vBlue = SMA(Closes[1],5)[0];
      double vYellow = SMA(Closes[1],6)[0];
      // set the lines
      yBlue.Set(vBlue);
      BlueLine.Set(yBlue[0]);
      yYellow.Set(vYellow);
      YellowLine.Set(yYellow[0]);
      //}
      // Primary Array Plots – example of code (more complicated)
      if(BarsInProgress == 0) {
      double vWhite = SMA(Closes[0],4)[0];
      yWhiteRaw.Set(vWhite);
      WhiteLine.Set(yWhite[0]);
      }

      The Primary 500Tick plots with or without the BarsInProgress statement.
      Thanks for your help!

      Comment


        #4
        Originally posted by JulieC View Post
        Hi PatrickH - thanks for your fast response.
        This is the code applied to a 500 Tick chart (Primary) in the example given:
        protectedoverridevoid Initialize()
        {
        Add(PeriodType.Minute,30);
        }
        protectedoverridevoid OnBarUpdate()
        {
        if((CurrentBars[0] <= 21) || (CurrentBars[1] <= 21))
        return;
        else
        {
        // examples of code to arrive at plots (more complicated)
        //if(BarsInProgress == 1) { (I tried this but get no data)
        double vBlue = SMA(Closes[1],5)[0];
        double vYellow = SMA(Closes[1],6)[0];
        // set the lines
        yBlue.Set(vBlue);
        BlueLine.Set(yBlue[0]);
        yYellow.Set(vYellow);
        YellowLine.Set(yYellow[0]);
        //}
        // Primary Array Plots – example of code (more complicated)
        if(BarsInProgress == 0) {
        double vWhite = SMA(Closes[0],4)[0];
        yWhiteRaw.Set(vWhite);
        WhiteLine.Set(yWhite[0]);
        }

        The Primary 500Tick plots with or without the BarsInProgress statement.
        Thanks for your help!
        You must synchronize your secondary timeframe.

        ref: http://www.ninjatrader.com/support/f...ead.php?t=3572

        Comment


          #5
          Hello JulieC,

          Thank you for your response and thanks to Koganam for pointing this item out.

          Koganam is correct, here. The link to the reference sample he provides shows how to sync a custom data series to the secondary bar series using an indicator (does not matter which one). For your code it would look similar to the following:
          Code:
          			if (yBlue == null)
          			{
          				yBlue = new DataSeries(SMA(BarsArray[1], 50));
          			}
          			if (yYellow == null)
          			{
          				yYellow = new DataSeries(SMA(BarsArray[1], 50));
          			}

          Comment


            #6
            Thank you PatrickH and Koganam,
            I know I am very close to solution with your help.
            Crazy thing, I am not able to download the SampleDataSeries Zip file - my pc thinks it is "attachment.php" and it is blank. I've downloaded zip files and imported several times before, no problem. I've turned off the antivirus. . . suggestions?
            (I've tried many other zip files from this forum, all are downloaded as "attachment.php."

            Comment


              #7
              Hello JulieC,

              Thank you for your response.

              Please try another internet browser to log into the Support Forum and download the .zip file.

              Comment


                #8
                Not yet, I haven’t quite got it to work yet. Here’s what I have coded:

                protectedoverridevoid Initialize()
                {
                Add(PeriodType.Minute,30);
                }
                protectedoverridevoid OnBarUpdate()
                {
                if((CurrentBars[0] <= 21) || (CurrentBars[1] <= 21))
                return;
                else
                {
                if (yBlue == null) {
                yBlue = new DataSeries(SMA(BarsArray[1], 50));
                }
                if (yYellow == null) {
                yYellow = new DataSeries(SMA(BarsArray[1], 50));
                }
                // Thereafter I code the complicated calculation of the blue and yellow plots.
                // i've tried placing this sync statement below the calculations also...
                // each time i F5 then the data is perfect; it remains historically correct,
                // but the current bar immediately modifies the [1]value.

                In the sync code, does the "SMA" literally set up a new SMA? What does the "50" represent? Do I have the placement wrong?

                Thanks...
                (PatrickH - you were absolutely right about changing browsers in order to download the .zip file. Thanks!)

                Comment


                  #9
                  Hello,

                  This is Brett responding:

                  protectedoverridevoid Initialize()
                  {
                  Add(PeriodType.Minute,30);
                  yYellow = new DataSeries(SMA(BarsArray[1], 50));
                  yBlue = new DataSeries(SMA(BarsArray[1], 50));
                  }
                  protectedoverridevoid OnBarUpdate()
                  {
                  if((CurrentBars[0] <= 21) || (CurrentBars[1] <= 21))
                  return;
                  else
                  {
                  if (!yBlue.ContainsValue(0)) {
                  yYellow.Set();
                  }
                  if (!yYellow.ContainsValue(0)) {
                  yBlue.Set();
                  }


                  I modified your code above, basically the SMA and creating the data series is a dummy value and doesn't matter, you could use any indicator. What is important is that the indicator is running on BarsArray[1]. Which means that the DataSeries is "synced" so it has the exacnt number of spots needed for that time series. You only need to do this sync once and once done you would use the data series and set it as normal.

                  Comment


                    #10
                    Wow, that was an experience. Crashes NinjaTrader. Thankgoodness Ninja re-started on an old Workspace and I removed the code. So, where did I go wrong… here is the step by step that leads to crashes:

                    In Initialize() I added: yBlue = new DataSeries(SMA(BarsArray[1], 50));
                    This compiles ok, but when refreshed to the chart the log error code is:
                    “’BarsArray’ property can’t be accessed from within ‘Initialize’ method.”
                    In one instance this error just removed the indicator from the chart, in other instances Ninja crashes.

                    Comment


                      #11
                      Julie,

                      My apologies, I gave you wrong information.

                      I recalled the sample wrong and that lead me to supply you with incorrect information.

                      How you had it to start was absolutely correct. Please revert that change I suggested as I misunderstood the question. Stepping in the middle here for patrick who is out sick today.

                      To answer you question on the sync code SMA value its absolutely arbitrary and could be anything the number you use doesn't matter. Whats important is that it is running on that Bar Series buy inputting the BarsArray[1]. This is so that when the DataSeries is created which is an array of values that it has the correct number of array values allocated. As a 1 minute chart would have more array values then a 5 minute chart for example as there is just simply more data and this is what the sync does.

                      The sample is here if not already supplied by patrick: http://www.ninjatrader.com/support/f...ead.php?t=3572

                      Comment


                        #12
                        Thanks Brett, PatrickH, koganam; things are back to normal – which is still wrong and the reason I started this thread in the beginning. I frustrate myself with my inability to communicate well. Let me try again.
                        Problem Statement: I have a plot from Array[1] in a multi-time indicator (and strategy) that continuously updates with each Primary[0] bar OnBarUpdate. I don’t want it to update during the course of the Primary bar.

                        In English: this is what I want to tell my indicator: “Plot the value from Array[1] onto my Primary chart. Use either BarClose[1][1] (and hold that value for an entire Array[1] bar) or use the FirstTickOfBar[1] (and hold that value for an entire Array[1] bar). Don’t wiggle. Don’t move. I’m going to trade based on a cross of this line.

                        On a simple single time frame chart: This code gives me the exact value I need and does not wiggle during the CurrentBar:
                        // Directly from my indicator (same calc on my strategy)
                        // i use many calcs like:
                        // double a05 = SMA(Close,5)[0];
                        double vBlue = (a01+a02+a03+a04+a05+a06) / 6;
                        yBlueRaw.Set(vBlue);
                        yBlue.Set(yBlueRaw[1]); // here i set it back [1] so it doesn't wiggle
                        BlueLine.Set(yBlue[0]);

                        On a multi-time frame chart: This code gives me the exact value I desire when I Print fromwithin the BarsInProgress.
                        if(BarsInProgress == 1) {
                        // i use many calcs like:
                        // double a15 = SMA(Closes[1],5)[0];
                        double vBlue = (a11+a12+a13+a14+a15+a16) / 6;
                        yBlueRaw.Set(vBlue);
                        yBlue.Set(yBlueRaw[1]); // here i set it back [1] so it doesn't wiggle
                        BlueLine.Set(yBlue[0]);
                        Print(yBlue[0]);
                        } // completes BarsInProgress


                        HOWEVER: there is no plot output. Once I drop the BarsInProgress I get a plot . . . but it is the wrong value because it is updated during the course of the Primary bar. That is why I named this thread “BarsInProgress: Array[0] and Array[1]”

                        If I drop the BarsInProgress: The line wiggles, but a F5 brings it immediately back to where it belongs. Please take a look at the pics in the original #1 post.

                        Possibilities of Cause:
                        1: It is a “sync” issue. If so, I’ve already followed the example given in SampleDataSeries .Zip; see my specific code as detailed in reply #8 this thread. Did I miss something?
                        2: It is a BarsInProgess problem.
                        3: It is yet another problem within my code.
                        4: NinjaTrader can’t make a multi-time plot from Array[1] that doesn’t wiggle as a consequence of Primary bars.

                        Other lines created in the Primary Array must be CalculateOnBarClose = false; So the entire strategy & indicator must be CalculateOnBarClose = false.

                        Suggestions on something else to try?? THANKS!

                        Comment


                          #13
                          Originally posted by JulieC View Post
                          Thanks Brett, PatrickH, koganam; things are back to normal – which is still wrong and the reason I started this thread in the beginning. I frustrate myself with my inability to communicate well. Let me try again.
                          Problem Statement: I have a plot from Array[1] in a multi-time indicator (and strategy) that continuously updates with each Primary[0] bar OnBarUpdate. I don’t want it to update during the course of the Primary bar.

                          In English: this is what I want to tell my indicator: “Plot the value from Array[1] onto my Primary chart. Use either BarClose[1][1] (and hold that value for an entire Array[1] bar) or use the FirstTickOfBar[1] (and hold that value for an entire Array[1] bar). Don’t wiggle. Don’t move. I’m going to trade based on a cross of this line.

                          On a simple single time frame chart: This code gives me the exact value I need and does not wiggle during the CurrentBar:
                          // Directly from my indicator (same calc on my strategy)
                          // i use many calcs like:
                          // double a05 = SMA(Close,5)[0];
                          double vBlue = (a01+a02+a03+a04+a05+a06) / 6;
                          yBlueRaw.Set(vBlue);
                          yBlue.Set(yBlueRaw[1]); // here i set it back [1] so it doesn't wiggle
                          BlueLine.Set(yBlue[0]);

                          On a multi-time frame chart: This code gives me the exact value I desire when I Print fromwithin the BarsInProgress.
                          if(BarsInProgress == 1) {
                          // i use many calcs like:
                          // double a15 = SMA(Closes[1],5)[0];
                          double vBlue = (a11+a12+a13+a14+a15+a16) / 6;
                          yBlueRaw.Set(vBlue);
                          yBlue.Set(yBlueRaw[1]); // here i set it back [1] so it doesn't wiggle
                          BlueLine.Set(yBlue[0]);
                          Print(yBlue[0]);
                          } // completes BarsInProgress


                          HOWEVER: there is no plot output. Once I drop the BarsInProgress I get a plot . . . but it is the wrong value because it is updated during the course of the Primary bar. That is why I named this thread “BarsInProgress: Array[0] and Array[1]”

                          If I drop the BarsInProgress: The line wiggles, but a F5 brings it immediately back to where it belongs. Please take a look at the pics in the original #1 post.

                          Possibilities of Cause:
                          1: It is a “sync” issue. If so, I’ve already followed the example given in SampleDataSeries .Zip; see my specific code as detailed in reply #8 this thread. Did I miss something?
                          2: It is a BarsInProgess problem.
                          3: It is yet another problem within my code.
                          4: NinjaTrader can’t make a multi-time plot from Array[1] that doesn’t wiggle as a consequence of Primary bars.

                          Other lines created in the Primary Array must be CalculateOnBarClose = false; So the entire strategy & indicator must be CalculateOnBarClose = false.

                          Suggestions on something else to try?? THANKS!
                          Are they any errors in your log? If so, please post the exact text of them.

                          Comment


                            #14
                            Hi koganam!!!
                            Nope - there are zero errors in the log (not since the crash incidents as detailed in reply #10 below). I just opened a new chart and loaded both the indicator and strategy to be sure. No errors.
                            Thanks for taking the time to consider this issue.

                            Comment


                              #15
                              Originally posted by JulieC View Post
                              Hi koganam!!!
                              Nope - there are zero errors in the log (not since the crash incidents as detailed in reply #10 below). I just opened a new chart and loaded both the indicator and strategy to be sure. No errors.
                              Thanks for taking the time to consider this issue.
                              In that case, we are going to have to use a crude sledgehammer approach to see if we are throwing an untrapped exception. Modify your code like this:
                              Code:
                              [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New](BarsInProgress == [/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New]) {[/FONT][/COLOR]
                              [COLOR=green][FONT=Courier New]// i use many calcs like: [/FONT][/COLOR]
                              [COLOR=green][FONT=Courier New]// double a15 = SMA(Closes[1],5)[0];[/FONT][/COLOR]
                              [COLOR=blue][FONT=Courier New]double[/FONT][/COLOR][COLOR=black][FONT=Courier New] vBlue = (a11+a12+a13+a14+a15+a16) / [/FONT][/COLOR][COLOR=purple][FONT=Courier New]6[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR]
                              Print(null);
                              Print("CurrentBar: " + CurrentBar);
                              Print("CP1");
                              [COLOR=black][FONT=Courier New]yBlueRaw.Set(vBlue);[/FONT][/COLOR]
                              [COLOR=black][FONT=Courier New]Print("CP2");
                              yBlue.Set(yBlueRaw[[/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New]]); [/FONT][/COLOR][COLOR=green][FONT=Courier New]// here i set it back [1] so it doesn't wiggle[/FONT][/COLOR]
                              Print("CP3");
                              [COLOR=black][FONT=Courier New]BlueLine.Set(yBlue[[/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New]]);[/FONT][/COLOR]
                              [COLOR=black][FONT=Courier New]Print("CP4");
                              [/FONT][/COLOR][COLOR=black][FONT=Courier New]Print(yBlue[[/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New]]);[/FONT][/COLOR]
                              [COLOR=black][FONT=Courier New]} [/FONT][/COLOR][COLOR=green][FONT=Courier New]// completes BarsInProgress[/FONT][/COLOR]
                              Then open the Output Window and let us see which checkpoint does not return.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by synthhokie, Today, 10:24 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by RaddiFX, Today, 10:15 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by 21laienoch, Today, 10:21 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post 21laienoch  
                              Started by Rogers101, 05-05-2024, 11:30 AM
                              23 responses
                              68 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by Human#102, Today, 09:54 AM
                              1 response
                              2 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X