• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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, 02: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.
      Patrick H.NinjaTrader Customer Service

      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 surgeless, Today, 12:42 AM
        0 responses
        2 views
        0 likes
        Last Post surgeless  
        Started by JasonH, Yesterday, 11:03 PM
        0 responses
        2 views
        0 likes
        Last Post JasonH
        by JasonH
         
        Started by NeuTronite, Yesterday, 08:40 PM
        0 responses
        1 view
        0 likes
        Last Post NeuTronite  
        Started by tvaughan4, Yesterday, 06:00 PM
        0 responses
        5 views
        0 likes
        Last Post tvaughan4  
        Started by Lisa Marshall, Yesterday, 04:32 PM
        0 responses
        2 views
        0 likes
        Last Post Lisa Marshall  
        Working...
        X