Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accumulate the value of an indicator

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

    Accumulate the value of an indicator

    Hi,

    I'm not a programmer therefore I was stucked in develop a new indicator
    I would like to develop the indicator with accumulate the value if some criteria is fullfiled:

    Example:

    // condition 1
    if (SMA(Close, fastMA)[0] > SMA(Close, slowMA)[0]
    {Ratio.Set(macs + 1);};

    // condition 2
    if (SMA(Close, fastMA*2)[0] > SMA(Close, slowMA*2)[0])
    {Ratio.Set(macs + 1);};

    In the above condition 1 & 2, my ideal result of "macs" should be "2" if condition 1 & 2 fulfilled at the same period. However, the result was "1".

    I understood I was missing some code in order to accumulate the value of "macs". Can anyone help? Thanks a lot!

    #2
    Originally posted by ManIp View Post
    Hi,

    I'm not a programmer therefore I was stucked in develop a new indicator
    I would like to develop the indicator with accumulate the value if some criteria is fullfiled:

    Example:

    // condition 1
    if (SMA(Close, fastMA)[0] > SMA(Close, slowMA)[0]
    {Ratio.Set(macs + 1);};

    // condition 2
    if (SMA(Close, fastMA*2)[0] > SMA(Close, slowMA*2)[0])
    {Ratio.Set(macs + 1);};

    In the above condition 1 & 2, my ideal result of "macs" should be "2" if condition 1 & 2 fulfilled at the same period. However, the result was "1".

    I understood I was missing some code in order to accumulate the value of "macs". Can anyone help? Thanks a lot!
    You have to accumulate somewhere. What you have done here is take a quantity and use it after adjusting it, but without ever storing the adjustment. You can either first store the value that you want to use, then use it, or you can accumulate in situ. In situ accumulation while performing another operation is generally frowned upon as bad practice because ultimately it encourages coding that can produce unintended side effects.

    Code:
    //Accumulate, then assign:
    //*****************************
    // condition 1                                    
    if (SMA(Close, fastMA)[0] > SMA(Close, slowMA)[0]
    {macs = macs + 1; //or macs += 1;
    Ratio.Set(macs);}
    
    // condition 2                        
    if (SMA(Close, fastMA*2)[0] > SMA(Close, slowMA*2)[0])
    {macs = macs + 1; //or macs += 1;
    Ratio.Set(macs);}
    Code:
    //Accumulate in situ:
    //**********************
    // condition 1                                    
    if (SMA(Close, fastMA)[0] > SMA(Close, slowMA)[0]
    {Ratio.Set(++macs);}
    
    // condition 2                        
    if (SMA(Close, fastMA*2)[0] > SMA(Close, slowMA*2)[0])
    {Ratio.Set(++macs);}
    Last edited by koganam; 06-01-2014, 03:07 PM. Reason: Corrected code to add incrementing value to "macs +=;"

    Comment


      #3
      Hello ManIp,

      Thank you for your post.

      Koganam is correct, the value is never stored thus the value is 1 as macs is likely 0 to start. So using a variable we can first store the additions based on the conditions and then set this as the DataSeries value.

      Comment


        #4
        THANKS VERY MUCH Koganam! I got what I want and I can further develop this indicator!

        Comment

        Latest Posts

        Collapse

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