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

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 Haiasi, Today, 06:53 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by ScottWalsh, Today, 06:52 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by ScottW, Today, 06:09 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by ftsc2022, 10-25-2022, 12:03 PM
            5 responses
            256 views
            0 likes
            Last Post KeyonMatthews  
            Started by Board game geek, 10-29-2023, 12:00 PM
            14 responses
            244 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Working...
            X