Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 Sparkyboy, Today, 10:57 AM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by swestendorf, Today, 11:14 AM
        1 response
        2 views
        0 likes
        Last Post swestendorf  
        Started by TheMarlin801, 10-13-2020, 01:40 AM
        21 responses
        3,917 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by timmbbo, 07-05-2023, 10:21 PM
        3 responses
        156 views
        0 likes
        Last Post grayfrog  
        Started by Lumbeezl, 01-11-2022, 06:50 PM
        30 responses
        812 views
        1 like
        Last Post grayfrog  
        Working...
        X