user function, please help me

#1
dear all,
I'm trying to write my first user-function. But it doesnt work.

let me use the manual function example to explane you my problem:


function IIR2( input, f0, f1, f2 )
{
result[ 0 ] = input[ 0 ];
result[ 1 ] = input[ 1 ];
for( i = 2; i < BarCount; i++ )
{
result[ i ] = f0 * input[ i ] +
f1 * result[ i - 1 ] +
f2 * result[ i - 2 ];
}
return result;
}


Now, it works very well if I call it inside the same backtest in which there is the function, but it doesn't work if I call it from another backtest (after have saved it as function with the name IIR2.afl in custom directory).

This instruction (again from manual) :


Plot( Close, "Price", colorBlack, styleCandle );
Plot( IIR2( Close, 0.2, 1.4, -0.6 ), "function example", colorRed );


give the following error about the second row:
sintax error: unexpected '(' expecting ')' or ','

thank you in advance,

Cippo
 
#2
dear all,
I'm trying to write my first user-function. But it doesnt work.

let me use the manual function example to explane you my problem:


function IIR2( input, f0, f1, f2 )
{
result[ 0 ] = input[ 0 ];
result[ 1 ] = input[ 1 ];
for( i = 2; i < BarCount; i++ )
{
result[ i ] = f0 * input[ i ] +
f1 * result[ i - 1 ] +
f2 * result[ i - 2 ];
}
return result;
}


Now, it works very well if I call it inside the same backtest in which there is the function, but it doesn't work if I call it from another backtest (after have saved it as function with the name IIR2.afl in custom directory).

This instruction (again from manual) :


Plot( Close, "Price", colorBlack, styleCandle );
Plot( IIR2( Close, 0.2, 1.4, -0.6 ), "function example", colorRed );


give the following error about the second row:
sintax error: unexpected '(' expecting ')' or ','

thank you in advance,

Cippo
Place IIR2.afl in the include folder.

Place the #include in the afl script



#include <IIR2.afl>
Plot( Close, "Price", colorBlack, styleCandle );
Plot( IIR2( Close, 0.2, 1.4, -0.6 ), "function example", colorRed );
 
#3
tks AmiSmart

it works now,

I just have the feeling backtest is a little bit slower when I use external functions
 
Last edited:
#4
tks AmiSmart

it works now,

I just have the feeling backtest is a little bit slower when I use external functions
not the issue of external function.
The issue of looping inside your external function causing it.
You can use the performance counter measure it
 
#6
Another question about user function:
if I was interested in more than one result of the function (e.g. a MACD function that produces MACD, signal and histogram) how can I call the 3 variables from the same function?

It seems I cannot write in the end of the function, for example:
return (macd, signal, histogram);

Do I need to multiply the function changing every time the variable has to be returned?

thank you

cippo
 
Last edited:
#7
Another question about user function:
if I was interested in more than one result of the function (e.g. a MACD function that produces MACD, signal and histogram) how can I call the 3 variables from the same function?

It seems I cannot write in the end of the function, for example:
return (macd, signal, histogram);

Do I need to multiply the function changing every time the variable has to be returned?

thank you

cippo
PHP:
procedure vMACD(fast, slow, Sig)
{
 //----Am I Smart ----
 global rcMACD, rcSig, rcDiff;

  rcMacd = MACD(fast, slow);
  rcSig  = Signal(fast, slow, Sig);
  rcDiff = rcMacd - rcSig;
}

vMACD(12,26,9);

Plot(rcMacd, "Macd", colorAqua);
Plot(rcSig, "Signal", colorRed);
Plot(rcDiff, "Diff", IIf(rcDiff>0, colorGreen, colorRed), styleHistogram);
 
#8
ok tks my smart mentor,

may I use the same procedure in the same backtest, sending different parameters?

for example:


procedure vMACD(fast, slow, Sig)
{
//----Am I Smart ----
global rcMACD, rcSig, rcDiff;

rcMacd = MACD(fast, slow);
rcSig = Signal(fast, slow, Sig);
rcDiff = rcMacd - rcSig;
}

vMACD(12,26,9);

buy = Signal > 0;

vMACD(6,13,9);

sell = Signal <0;




tks,

cippo
 
#9
ok tks my smart mentor,

may I use the same procedure in the same backtest, sending different parameters?

for example:




tks,

cippo

/*
Call: procedure vMACD(fast, slow, Sig)
Return: rcMacd, rcSig, rcDiff

*/
procedure vMACD(fast, slow, Sig)
{
//----Am I Smart ----
global rcMACD, rcSig, rcDiff;

rcMacd = MACD(fast, slow);
rcSig = Signal(fast, slow, Sig);
rcDiff = rcMacd - rcSig;
}



vMACD(12,26,9); //==> return (rcMacd, rcSig, rcDiff)


buy = rcSig > 0;

vMACD(6,13,9);

sell = rcSig <0;


Amibroker do not allow return more than 1 variable through function.
Therefore implement procedure/function to return global variables for returning multiple variables.

Although it work using Signal, but it is advisible to do a proper way
by defining the return variables such as rcMacd, rcSig and rcDiff.

The reason of doing these is you want when modification of the function internal statements and not affect the external statements.
Reduce the errors and efforts.

for example, I implement the MACD method based on SMA method instead of EMA
PHP:
procedure vMACD(fast, slow, Sig)
{
 //----Am I Smart ----
 global rcMACD, rcSig, rcDiff;

  rcMacd = ma(c, fast) - ma(c,slow);
  rcSig  =   ma(rcMacd, Sig);
  rcDiff = rcMacd - rcSig;
}
 

Similar threads