• 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 thgamey, Today, 02:05 AM
        0 responses
        1 view
        0 likes
        Last Post thgamey
        by thgamey
         
        Started by tpdk2012, Today, 12:51 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_ChrisSch  
        Started by simonerg, Today, 12:13 AM
        0 responses
        7 views
        0 likes
        Last Post simonerg  
        Started by rickyblah12, 07-20-2019, 01:01 PM
        2 responses
        20 views
        0 likes
        Last Post NinjaTrader_ChrisSch  
        Started by edunwa, Yesterday, 04:11 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_EricB  
        Working...
        X