• 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, 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.
      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 ericson, Today, 08:14 PM
        0 responses
        5 views
        0 likes
        Last Post ericson
        by ericson
         
        Started by user1986, Today, 07:28 PM
        1 response
        7 views
        0 likes
        Last Post user1986  
        Started by jertrade, Today, 06:57 PM
        0 responses
        5 views
        0 likes
        Last Post jertrade  
        Started by Emma1, Today, 02:42 PM
        1 response
        17 views
        0 likes
        Last Post NinjaTrader_DrewO  
        Started by fx.practic, Today, 03:10 PM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X