Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Best way to determine if Close was below an Indicator for Period of Time/Bars

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

    Best way to determine if Close was below an Indicator for Period of Time/Bars

    Hello,

    Is there a cleaner way to keep track of how many bars closed below and indicator. This is what I'm currently doing.

    Code:
    				if (Close[0] < SMA1[0])			
    				{ 
    					BelowSMA = BelowSMA + 1;
    				}
    				else
    				{
    					BelowSMA = 0;
    				}
    Thank You!

    #2
    Hello Dragon989,

    Thank you for the question.

    If you are simply trying to count the number of occurrences, that would likely be the most simple way to do that. The only other simplification that could happen here would be to instead use

    Code:
    if (Close[0] < SMA1[0])			
    { 
    	BelowSMA++;
    }
    If you are trying to get a number of bars between the last occurrence of the condition and now, you could also use the CurrentBar for that.


    Code:
    if (Close[0] < SMA1[0])			
    { 
    	BelowSMA = CurrentBar; //bar where the event happened
    }
    
    // then where you want to check the number of bars you would subtract the variable from the CurrentBar
    
    Print("Bars Since Last Event: " + (CurrentBar - BelowSMA ));

    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    43 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    29 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    46 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    38 views
    0 likes
    Last Post CarlTrading  
    Working...
    X