Announcement

Collapse
No announcement yet.

Cumulative sum, Moving sum

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

  • Cumulative sum, Moving sum

    What is the proper syntax to perform a continuous sum function
    that will give you a total back to the begining of the data base.

    Also can I perform a task that will give a sum for the prior 30
    bars.

    If a close - close.1 function returns values of 100, -3 that does
    not mean that the value of our indicator dropped 103 points.

    It only dropped 3 points and our indicator line should drop to a value of 97 not -3.

    Any help would be appreciated.

    thanks, John

  • #2
    Re: Cumulative sum, Moving sum

    Hello John,

    Originally posted by xoprofittaker
    What is the proper syntax to perform a continuous sum function
    that will give you a total back to the begining of the data base.
    The easiest thing to do is use a global variable and add up the data bar-by-bar as the formula is loading. Then add the real time bar's data to the global variable after it closes. The following example accomplishes this for the volume values.

    PHP Code:
    var nSum 0;

    function 
    main() {
        var 
    nVol volume(-1);
        if (
    nVol == null) return;
        
        if (
    getBarState() == BARSTATE_NEWBAR) {
            
    nSum += nVol;
        }
     
        return 
    nSum;

    Also can I perform a task that will give a sum for the prior 30
    bars.
    Yes, one way to accomplish this is with a for loop.

    PHP Code:
    var nSum 0;

    function 
    main() {
        if (
    volume(-30) == null) return;
        if (
    getBarState() == BARSTATE_NEWBAR) {
            
    nSum 0;
            for (var 
    1<= 30i++) {
                
    nSum += volume(-i);
            }
        }
        
        return 
    nSum;

    If a close - close.1 function returns values of 100, -3 that does
    not mean that the value of our indicator dropped 103 points.

    It only dropped 3 points and our indicator line should drop to a value of 97 not -3.

    Any help would be appreciated.

    thanks, John
    Your last question is difficult to answer without seeing your code. The expression, close - close.1, is not valid EFS syntax. In looking at the basic math you've described a value of 100 minus a value of -3 is equal to 103.

    debugPrintln(100 - -3); // prints 103

    If you want that result to become 97 you would need to take the absolute value of -3 and subtract that from 100. See the abs() method of the Math Object.

    debugPrintln(100 - Math.abs(-3)); // prints 97
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment

    Working...
    X