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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    90 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    135 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    120 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    69 views
    0 likes
    Last Post PaulMohn  
    Working...
    X