# Use first bar with data

The following custom function is returning null. I believe it is because the first bar has no data. How can I edit this so input[0] calls the first bar with data?

``````function EmaFraction( input, period )
{
result[ 0 ] = input[ 0 ];
Multiplier = 2/(period + 1);

for( i = 1; i < BarCount; i++ )
{
result[ i ] = (input[ i ] - result[ i - 1 ]) *
Multiplier + result[ i - 1 ];

}

return result;
}
``````
``````function EmaFraction( input, period )
{
Multiplier = 2/(period + 1);

start = NullCount(input);
result[ start ] = input[ start ];

for( i = start+1; i < BarCount; i++ )
result[ i ] = (input[ i ] - result[ i - 1 ]) * Multiplier + result[ i - 1 ];

for( i = 0; i <= Min(start, BarCount-1); i++ )
result[ i ] = Null;

return result;
}

x = MA( C, 20 );

Plot( EmaFraction( x, 50 ), "Price", colorDefault );
``````
4 Likes

That did it. Thank you fxshrat.

# Just don't do that. Do not re-invent the wheel.

Do not write dozens of lines for something that is done in single line

Do NOT use loops for reimplementing things that are NATIVELY done 100 faster using existing functions The algorithm you are trying to do is already available as EMA. Also if you really want recursive IIR filter, there are functions that do just that called AMA()/AMA2() and IIR() see http://www.amibroker.com/f?ama

``````function EmaFraction( input, period )
{
return AMA( input, 2/(period + 1 ) );
}
``````

That is ALL. Anything array based is 100x FASTER than looping code.

One should never use loops for any kind of IIR (infinite impulse response) filter in AFL.

3 Likes

@Tomasz, the depth of AmiBroker continues to astound me. It is an amazing body of work.

2 Likes

I do not understand the extra big letters (and they were even bigger before).
We simply do not know whether @burger was just doing exercise(s) playing around with AFL and with loops and with whatever else there is... and trying to understand why this or that is happening. Yes, single line using array function is faster than AFL loop but since we are humans it is part of human nature to explore things (such as "What is content of that black box?", "What is going on in there step by step?", ...). But @burger, keep Tomasz's advise in mind to always look for inbuilt array way after doing your exercises and black box explorations.

BTW: "Do not touch that hot plate. Just don't do that!" does (did) not help much. I did it anyway decades ago. Sorry, I am human.

2 Likes

Big letters are for the reason: the key advantage and difference of AFL over ALL other languages is its native parallel array processing. Everyone knows how to use loops and there are zillions of "programming tutorials" that show loops. Yet very few people are proficient in vector processing.
What this forum needs is focusing on advantages (speed AND simplicity) of ARRAY (vector) processing, not on things that are done in 1980-way (loops).

Our brains are massively parallel. The future of computing is massive parallelism.

There is second reason why publishing of slow code should be avoided - there are many "copy-paste artists" that simply take published code and paste to the program without knowing/thinking whenever given code is optimally written or not. They just run "slow" code thinking that it must be this way. While the truth is that it can be 100x faster.
Slow codes like that do not give AmiBroker justice it deserves.

1 Like

The forum already focuses on array processing a lot and we totally conform to making people look for array solution as best as they can.

My point is you won't prevent people from trying out certain things and from trying to find out how certain things work (including loops). Especially not with giant lettered "Don't do that". And I do not think that everyone knows how to use loops. (As aside, speaking from personal experience... even if one shows a one liner (some) people still want to dig into trying to replicate that one liner. Human curiosity... can't do much about it).

Anyway next time at similar cases together with fixed loop I will include array alternative to point out difference and advantage. vs

2 Likes

I disagree. There is about 1% of cases of human curiosity. 99% of cases is "give me ANY code NOW I need to copy-paste". That is why it is important to provide optimum solutions or at least mentioning that using loops is not the way to go. I have seen way too many cases of bad re-inventing the wheel and I won't keep quiet when I see that. The entropy of the universe grows all around, and poor solutions replace good solutions universally, information technology evolution went all wrong so at least in this micro-universe (the forum) I would like to see at least minimum trace of beauty.

4 Likes