Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Past bar values

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

    Past bar values

    Hi,

    I have been trying for hours to calculate and plot a simple indicator based on the concept of calculating the difference of the close value of the current and previous bar (i.e. Close[0] - Close[1]). After hours and hours I cannot get the following code to plot.

    What I really want to do is the plot the formula which is commented but I cannot get even Close[3] to plot, anything in the past won't plot but Close[0] will. Everything compiles OK...

    I must be doing something trivially wrong... please help me out! Thanks!

    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.

    //Plot0.Set(VOL()[0] * (Close[0] - Close[1]));

    if (CurrentBar < 1)
    return;

    double argum = Close[3];
    DrawTextFixed("tag1", argum.ToString(), TextPosition.TopRight);
    Plot0.Set(argum);
    }

    #2
    Yes, you're doing something trivially wrong. Your problem is that you are accessing a previous bar when CurrentBar=0. This causes the indicator to abort and nothing will plot. This mistakes bites me all the time.

    Your statement "if (CurrentBar < 1) return;" should be ABOVE the line of code where you refer to the previous bar. Put it at the top of OnBarUpdate(), above everything else. In fact, if you refer to Close[3] as you're doing, you should test for CurrentBar<4 instead.
    -Alex
    Last edited by anachronist; 10-24-2008, 05:23 PM.

    Comment


      #3
      Many many thanks! I was getting so frustrated that I moved the Close to [3] but forget the if clause at 1. Now it plots.

      But I keep having a problem with what I actually want to do which is to plot a SMA of the commented code, i.e. the following:

      protected override void OnBarUpdate()
      {
      // Use this method for calculating your indicator values. Assign a value to each
      // plot below by replacing 'Close[0]' with your own formula.

      //Plot0.Set();

      if (CurrentBar < 8)
      return;

      Plot0.Set( SMA( (VOL()[0] * (Close[0] - Close[1])), 7)[0] );
      }

      This even does not compile and I have tried both versions, the SMA with and without the [0]...

      Comment


        #4
        You need to stuff the value of this calculation into a DataSeries object and pass in the DataSeries object as input into the SMA() method.

        VOL()[0] * (Close[0] - Close[1])

        Here is a reference sample for more information - http://www.ninjatrader-support.com/v...ead.php?t=7299
        RayNinjaTrader Customer Service

        Comment


          #5
          Like Ray says, SMA() requires the first parameter to be a data series, not the numeric result of a calculation. You'd do something like this:

          (variables)
          private DataSeries vwcd; // volume weighted close difference

          (initialize method)
          vwcd = new DataSeries(this);

          (onbarupdate method)
          vwcd.Set(VOL()[0] * (Close[0] - Close[1])));
          Plot0.Set( SMA(vwcd, 7)[0] );

          If you came to NinjaTrader from TradeStation, I can understand the confusion. TradeStation automatically makes every result and every variable a data series. NinjaTrader makes a distinction between a data series and single values.
          -Alex

          Comment


            #6
            Many thanks to all of you!!! I must say this product has the best support compared to any other product I have ever used!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            579 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            334 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            551 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X