I need to create user functions for use in portfolio explorations/backtests/etc.
I decided to write a simple EMA user function to test. My code is included below. My problem is that my user function in exploration only returns the EMA values for the first symbol in the portfolio and not from all symbols. If I use the AB EMA function, the emas for all symbols in the portfolio are returned in exploration. How do I get all symbols returned in my user function exploration? Is a loop over symbols required? Or what?
I tried to write simple AFL array code instead of loops:
"Result = betaref(Result,1) + alpArray; "
But I got an error: "Not Initialized". I don't know how to initialize in this coding approach?
User functions work the same as built-in function and surely would work on ALL symbols, if only you change "Apply to:" setting to ALL symbols. Recommended reading: How do I debug my formula?
alpArray is NOT assigned to anywhere in your code, only alp so you have missing asterisk (*) and that is why you getting error
array code is just EMA(array, emaN ), or AMA( array, 2/(1+emaN) )
With the excellent help of a very knowledgeable local AB user, my problem has been solved. As others may have this insidious problem, the solution was to disable the pad and align (with SPY). As the standard AB EMA function worked, it seems obvious that the standard code checks for nulls.
I had extensively read the references you provided prior to coding. In particular, the AMA function. I want to code 2nd and 3rd order iir filters with both poles and zeros. The reason I used the ref() function is because my interpretation of AB documents is that the AMA() only works for standard 1st order ema (no zero). Am I correct? Do you have more information and/or documentation to illustrate?
This algorithm has been coded in Excel/VBA with an external C# dll for portfolio async/await data download and portfolio optimization. The dll optimization does not use multicore/multithreading code. But the dll optimization runs faster than my AB code with the standard AB 1st order EMA function (no loops, I think).
My understanding of your documentation is that loop code will run slower. So I am very interested in understanding AFL array coding to get better performance. Additional guidance on information and documentation to achieve better AB performance with array coding will be appreciated.
Code for an article by John Ehlers that introduces his Universal Oscillator that is based on filtering two-day price momentum using second-order infinite impulse response (IIR) filter http://www.amibroker.com/members/traders/01-2015.html
As @portfoliobuilder wrote, you should just use IIR function https://www.amibroker.com/guide/afl/iir.html for efficient implementation of 2nd and 3rd order filters. And yes pad and align will place Null for nonexisting data at start. That is why it is important to use techniques described here: How do I debug my formula? . Running exploration in many times is eye opening.