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 NullPointStrategies, Today, 05:17 AM
    0 responses
    52 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    43 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    48 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X