Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator- Accumulating data for Average

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

    Indicator- Accumulating data for Average

    I'm new to programming but I've been reading up a lot. My problem is that I have a specific function for a certain days price, but it also relies on averaging out based on a parameter. I want to make it so that I don't have to make a new indicator for every single time length in the same way a SMA works. But I'm not sure what to do. I was thinking a while loop but the problem is that you can't reference any variables inside the loop.

    What I'm trying to do:

    Parameter= Days

    General Coding:

    int Days = 3 ; // this is my initial parameter already set up from wizard

    double y= 1/2 * (High[Days]- Low[Days]) + Low[Days];
    // this is my function

    // My problem is that I don't know how to accumulate the y value based //on the parameter days total, as in if I wanted 4 to be my parameter
    // I want the sumtotal y value for today, yesterday, yesterday-1 and //yesterday -2
    //The while loop i tried to make didn't seem correct so I didn't include it

    double Final= y/days; // this gives me the average based on the y
    plot0.set(Final);

    Thanks, Will

    #2
    Hello,

    I'm not sure I know what you want to do, but it sounds like this might help:

    double y = 0;
    int days = 3;
    for (int d = 0; d < days; d++)
    {
    y= y + (
    1/2 * (High[days]- Low[days]) + Low[days]);
    }

    Let me know if you need help understanding what I did. This is not tested so be sure you test it first.

    Also, it is good practice to use a lower case letter for the first letter of your variables as I did with days.

    Last edited by NinjaTrader_Ben; 02-14-2010, 11:25 AM.
    DenNinjaTrader Customer Service

    Comment


      #3
      Doesn't appear to work

      I tried it the code as follows and it doesn't plot anything.

      protected override void Initialize()
      {
      Add(new Plot(Color.FromKnownColor(KnownColor.OrangeRed), PlotStyle.Line, "Plot0"));
      CalculateOnBarClose = true;
      Overlay = true;
      PriceTypeSupported = false;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      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.
      double y = 0;
      int days = 3;
      for (int d = 0; d < days; d++)
      {
      y= y + (1/2 * (High[days]- Low[days]) + Low[days]);
      }
      double Average = y / days;

      Plot0.Set(Average);
      All I'm trying to do is essentially get the moving average of the middle of the days price range for a time period in days. So if I wanted the 3 days to be my parameter, It would calculate the .5(high-low)+low each day for the number of days and then it would average them. Also can you reference a variable outside a loop if it is calculated inside the loop? Maybe that's the source of error, I don't know. It shouldn't be difficult.

      Comment


        #4
        remember it needs 3 bars first so add this

        protected override void OnBarUpdate()
        {
        if (CurrentBar < 3)
        {
        return;
        }

        Comment


          #5
          Thanks that Worked

          Thanks, that helped

          The last issue I have is that it plots the 3 previous days values instead of today, yesterday, and yesterday-1. I'm not sure why, as d is initialized at 0. Is there anyway to fix it so that I can include the present day in the average.

          if (CurrentBar < 20)
          {
          return;
          }
          double y = 0;
          int days = 3;
          for (int d = 0; d < days; d++)
          {
          y= y + (1/2 * (High[d]- Low[d]) + Low[d]);
          }
          double Average = y / days;

          Plot0.Set(Average);

          Comment


            #6
            You are likely using CalculateOnBarClose = true which does not process the current bar until the end of the bar.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              It seems nothing has changed. It still only records the current value for the next day. I did change the calculateonbarclose= false. Any other ideas? Here is all the code I have.


              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              protected override void Initialize()
              {
              Add(new Plot(Color.FromKnownColor(KnownColor.OrangeRed), PlotStyle.Line, "Plot0"));
              CalculateOnBarClose = false;
              Overlay = true;
              PriceTypeSupported = false;
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              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.
              if (CurrentBar < 20)
              {
              return;
              }
              double y = 0;
              int days = 3;
              for (int d = 0; d < days; d++)
              {
              y= y + (1/2 * (High[d]- Low[d]) + Low[d]);
              }
              double Average = y / days;

              Plot0.Set(Average);

              Comment


                #8
                Hello,

                I'm jumping in here, but is the issue that it won't plot on the right bars or the value you calculate is not correct?

                If its the value, try using Print() in your loop several times and print out all of the values and their result each time.

                if it is a plotting issue, the code below plots fine for me, though I did not check the values it was plotting.

                protectedoverridevoid Initialize()
                {


                AutoScale =
                false;
                Overlay =
                true;
                CalculateOnBarClose =
                false;

                }

                protectedoverridevoid OnBarUpdate()
                {
                if (CurrentBar < 20)
                {
                return;
                }
                double y = 0;
                int days = 3;
                for (int d = 0; d < days; d++)
                {
                y= y + (
                1/2 * (High[d]- Low[d]) + Low[d]);
                }
                double Average = y / days;
                Value.Set(Average);
                }
                DenNinjaTrader Customer Service

                Comment


                  #9
                  I figured it out

                  For some reason the 1/2 wasn't being perceived as 0.5 . I assume you can't multiply an integer times a double if you want a double as your output, or maybe there is another reason. I used .5 instead and it works now, so thanks to all that helped.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by einstein, Today, 06:20 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post einstein  
                  Started by op1031, Today, 05:51 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post op1031
                  by op1031
                   
                  Started by nighttrader99, Today, 04:34 PM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_ShawnB  
                  Started by Simon Investimens, Today, 02:51 PM
                  1 response
                  15 views
                  0 likes
                  Last Post NinjaTrader_ChrisL  
                  Started by Unsuitable, Today, 02:49 PM
                  0 responses
                  14 views
                  0 likes
                  Last Post Unsuitable  
                  Working...
                  X