Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating a running total

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

    Creating a running total

    Hi,

    I feel like I've spent the past few hours on what is probably an easy fix. I'm trying to create a running total (cumulative) of (Close[0] - Close[1]).

    So if...

    (Close[0] - Close[1]) = 0.1

    ...new bar...

    (Close[0] - Close[1]) (+ 0.1) = 0.6

    ...new bar...

    (Close[0] - Close[1]) (+ 0.6) = 0.9


    Thanks for any help,
    James

    #2
    Hello,

    Thank you for the question.

    If you are simply trying to accumulate the value, you would need to use a variable. Here is a simple example:

    Code:
    double accumulatedTotal = 0;
    		
    protected override void OnBarUpdate()
    {
    	if(CurrentBar > 0)
    	{
                accumulatedTotal += (Close[0] - Close[1]);
    	}
    }
    This would add the Close[0] - Close[1] to the existing value of accumulatedTotal .

    I look forward to being of further assistance.

    Comment


      #3
      That's a great suggestion, Jesse. I didn't know about the

      +=
      way of achieving this.

      James, I'm not sure if you want the sum of the actual (pos or neg) values or the 'absolute' value.

      There may be a simpler way of proceeding. If you want the 'actual' going back to bar[3], say, then:

      Code:
      Close[0] [COLOR="Red"]- Close[1] + Close[1][/COLOR][COLOR="Blue"] - Close[2] + Close[2][/COLOR] - Close[3] = [B]Close[0] - Close[3][/B]
      so you only need

      Code:
      [B][B]Close[0] - Close[3][/B][/B]
      and you don't need a variable as the other values cancel out.

      If you want to sum the absolute values, then change to:

      Code:
      {
                  accumulatedTotal += [B]Math.Abs[/B]( (Close[0] - Close[1]) );
      }
      Last edited by arbuthnot; 09-07-2015, 07:45 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      648 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      369 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      108 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      572 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      574 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X