Help logica for amibroker

Hallo anyone can help me

I want to create formula

Logica:
High > 3% (in 5 day)
And then
Example
Day1 high = 2%
Day2 high = 3%
Day3 high = 1%
Day4 high = 10%
Day5 high = 4%

High > 3% = 3
Result = 3/5day
= 60%

How to create logica for amibroker?? Help

Formula excel logica
= countif(A1:A5; “>3”)/5

Take a look at Sum() and IIf().

function CountIf_Percent(array, threshold, period) {		
	/// @link https://forum.amibroker.com/t/help-logica-for-amibroker/13262
	local count_if, if_true;
	if_true = IIf(array > threshold, 1, 0);
	count_if = Sum(if_true, period);
	return count_if / period;
}

days = 5;
threshold = 3;
array = /*your array here*/;
pcnt = CountIf_Percent(array, threshold, days);
1 Like

CountIf of array elements greater than 3 for 5 bars is simply

 Sum( array > 3, 5 );

You don't need to use IIf because comparison by itself gives 1 or 0 depending if condition is met or not.

2 Likes

Since it was me using IIf I just want to mention that it was added for better understanding since OP appears to be new to AB and many newbies having problems with (understanding) array (conditions)....

So @Jimmysaputraa, once you get familiar with how AFL/arrays work you can make code faster/less complicated by not adding redundant code doing things twice.

So as suggested by @Tomasz here is alternative without IIf() (Note: both if_true versions are correct).

function CountIf_Percent(array, threshold, period) {		
	/// @link https://forum.amibroker.com/t/help-logica-for-amibroker/13262
	local count_if, if_true;
	if_true = array > threshold;
	count_if = Sum(if_true, period);
	return count_if / period;
}

days = 5;
threshold = 3;
array = /*your array here*/;
pcnt = CountIf_Percent(array, threshold, days);

As aside also read here to understand difference between if-else statement and IIf function.

3 Likes

Thread is closed until original poster proves that he purchased the software.