CountIF (Function)
The CountIF function counts the number of true custom test condition occurrences over some number of bars.
The CountIF function can be used for data mining and testing historical statistical market action. For example, you can ask questions such as, "How many days over the last month have been up days with falling volume, and on the following day how many days were up or down?"
Syntax
CountIF(Test,Length)
Returns (Integer)
The number of true test condition occurrences.
Parameters
Name |
Type |
Description |
Test |
TrueFalse |
Specifies a bar or indicator condition (True/False expression) that must be TRUE. |
Length |
Numeric |
Sets the number of bars to consider for testing the condition. |
Remarks
The input parameters Test can be any bar or multi-data Condition such as Close > Open, High[1] < High[2], Close of Data1 – Close of data2 > 0, or Volume >= 100000. They can also be any mathematical calculation such as High + Low) / 2 > Average(TrueRange,10), or a numeric indicator function such as RSI(C,14) < 30, or ADX(C,14) > 25. It can also be another True/False function.
True/False conditions must contain one of the following operators: =, >=, <=, >, <, <>, crosses above, or crosses below. You can also test multiple conditions in one expression using: OR, and AND. Use parentheses (())to group conditions logically.
The value for the Length input parameter should always be a positive whole number greater than 0.
Examples
Assigns the number of bars where the close is greater than open for the last 12 bars to Value1, then plots Value1:
Value1 = CountIF(Close > Open, 12);
Plot1(Value1, "C > O");
Assigns to Value1 the number of bars where the close is greater than the close of one bar ago, and the volume is greater than the volume of one bar ago, for the last 24 bars, then plots Value1:
Value1 = CountIF(Close > Close[1] AND Volume > Volume[1], 24);
Plot1(Value1, "C+V+");