Peak instruction doesn't allow change as array

#1
Peak instruction doesn't allow 'change%' argument as array

Dear all,
I need to assign the Peak 'Change' argument in relation to the value of the current bar array value:

Change= IIf( x<0.4, 2, 4);
PK= Peak(H, Change, 1);

but this doesnt work cause IIF instruction deals Change as an array so it is not used properly as peak argument that has to be a number.

So the question is: How can I retrive a specific value of an array and assign it to a variable (not array)?

thank you

Cippo
 
Last edited:

KelvinHand

Well-Known Member
#2
Re: Peak instruction doesn't allow 'change%' argument as array

Dear all,
I need to assign the Peak 'Change' argument in relation to the value of the current bar array value:

Change= IIf( x<0.4, 2, 4);
PK= Peak(H, Change, 1);

but this doesnt work cause IIF instruction deals Change as an array so it is not used properly as peak argument that has to be a number.

So the question is: How can I retrive a specific value of an array and assign it to a variable (not array)?

thank you

Cippo
Ex1.
y= MACD();
x = lastvalue(y);
z = lastvalue(ref(y, -10));


Ex2.
y= RSI();
x = Y[BarCount - 10];
 
#3
Re: Peak instruction doesn't allow 'change%' argument as array

Ex1.
y= MACD();
x = lastvalue(y);
z = lastvalue(ref(y, -10));


Ex2.
y= RSI();
x = Y[BarCount - 10];
ok, thank you kelvin, but I need to extract from an array a different value for each bar to assign it to 'Change' peak argument. So '-10' in your example should be barindex(). But it doesnt work if I put an array between square bracket.
 

KelvinHand

Well-Known Member
#4
Re: Peak instruction doesn't allow 'change%' argument as array

ok, thank you kelvin, but I need to extract from an array a different value for each bar to assign it to 'Change' peak argument. So '-10' in your example should be barindex(). But it doesnt work if I put an array between square bracket.
In Ex2 Anything in the [...] bracket had to be number. You need to figure how to convert the array into number by using the lastvalue().

y = RSI() ==> 'Change' peak array
x = Y[BarCount - 10]; ==> is to extract the 10th item change peak argument from the current.

Try Ex1, z= your Change. Ex1 is more metastock way.
 
Last edited:
#5
Re: Peak instruction doesn't allow 'change%' argument as array

In Ex2 Anything in the [...] bracket had to be number. You need to figure how to convert the array into number by using the lastvalue().

y = RSI() ==> 'Change' peak array
x = Y[BarCount - 10]; ==> is to extract the 10th item change peak argument from the current.

Try Ex1, z= your Change. Ex1 is more metastock way.
thank you kelvin but I need to choose, bar by bar, a different value of change mining it from the change array (each day the relative value of change array). But it doesn't work if I use an array instead of -10 value as in your example:
Ex1.
y= MACD();
x = lastvalue(y);
z = lastvalue(ref(y, -10));

for example
z = lastvalue(ref(y, barindex()-countbar));

It seems that barindex is not scanned bar by bar but it is processed in the same string up to its last value, so it results just one value for z.

thank you for you help

cippo
 

KelvinHand

Well-Known Member
#6
Re: Peak instruction doesn't allow 'change%' argument as array

thank you kelvin but I need to choose, bar by bar, a different value of change mining it from the change array (each day the relative value of change array). But it doesn't work if I use an array instead of -10 value as in your example:
Ex1.
y= MACD();
x = lastvalue(y);
z = lastvalue(ref(y, -10));

for example
z = lastvalue(ref(y, barindex()-countbar));

It seems that barindex is not scanned bar by bar but it is processed in the same string up to its last value, so it results just one value for z.

thank you for you help

cippo
I don't know what u want. i think i had already provide you the vehicle but you don't know how to drive.

function simuChange()
{
return RSI()/100;
}

// Assign Variable Y to Change which is from item 0.... Barcount-1
y = simuChange();

x = Y[Barcount-1]; //==> which means i get the Change Number from the current
x = Y[Barcount-2]; //==> which means i get the Change Number from ref(Y, -1)
....etc.


Assume Y[Barcount-10] =0.5; //which is ref(Y, -9)

So once your x array is assign Y[Barcount-10], which means that the array X become a array fill with 0.5 from item 0 ... barcount-1 and this is how AFL know x is a number.

Change= IIf( x<0.4, 2, 4); //then your change will accept the 4 as number because array change is filled with 4 from item 0.... barcount-1

PK= Peak(H, Change, 1);


There is a function call _Trace() to debug your program step by step
http://www.amibroker.com/guide/afl/_trace.html
 
Last edited:

trash

Well-Known Member
#7
Simply read https://www.amibroker.com/guide/a_varperiods.html

But with some tricks and use of other functions you can add variable period feature to built in functions not being part in upper list.

I.e. shown by T.J. making variable period ema and variable period atr.
Variable period EMA can be very easily coded using AMA

function cEMA( array, period )
{
return AMA( array, 2 / (period + 1 ) );
}

Also it does not make sense to hand-code ATR.

Just use AMA for single-period ATR and correct wilders smoothing

function cATR( period )
{
TR = ATR( 1 ); // true range is ATR with period == 1
return AMA( TR, 1 / period ); // correct variable period Wilders smoothing
}

http://www.amibroker.com/guide/afl/ama.html
There are other tricks.
 
#8
Re: Peak instruction doesn't allow 'change%' argument as array

I don't know what u want. i think i had already provide you the vehicle but you don't know how to drive.

function simuChange()
{
return RSI()/100;
}

// Assign Variable Y to Change which is from item 0.... Barcount-1
y = simuChange();

x = Y[Barcount-1]; //==> which means i get the Change Number from the current
x = Y[Barcount-2]; //==> which means i get the Change Number from ref(Y, -1)
....etc.


Assume Y[Barcount-10] =0.5; //which is ref(Y, -9)

So once your x array is assign Y[Barcount-10], which means that the array X become a array fill with 0.5 from item 0 ... barcount-1 and this is how AFL know x is a number.

Change= IIf( x<0.4, 2, 4); //then your change will accept the 4 as number because array change is filled with 4 from item 0.... barcount-1

PK= Peak(H, Change, 1);

There is a function call _Trace() to debug your program step by step
http://www.amibroker.com/guide/afl/_trace.html
Thank you Kelvin, now I can send a 'Change' value and not a change array to Peak. But I need to send for each bar the related value of the original change array (SimuChange in your last example). So I need Y[1] for bar 1, Y[2] for bar 2 ... so I need to use an array related to bars, i.e. barindex(), instead of the fixed value.
I tried this:

function simuChange()
{
return RSI()/100;
}
y=simuchange();
x = lastvalue(ref(y, barindex()-barcount));


but unfortunately, the result of this script is that X is always the same, it doesnt change in relation of barindex(). I dont know why. 'Ref' is able to use array as period (REf (array, array)) and in fact I dont receive an error but it seems anyway AFL doesnt process this function as I expect.

Another question: why do you assign simuchange to Y? cannot we deal directly with simuchange()?
x = lastvalue(ref(simuchange(), barindex()-barcount));
I achieve just the same results.

thank you for your perseverance with me.

Cippo
 

KelvinHand

Well-Known Member
#9
Re: Peak instruction doesn't allow 'change%' argument as array

Thank you Kelvin, now I can send a 'Change' value and not a change array to Peak. But I need to send for each bar the related value of the original change array (SimuChange in your last example). So I need Y[1] for bar 1, Y[2] for bar 2 ... so I need to use an array related to bars, i.e. barindex(), instead of the fixed value.
I tried this:

function simuChange()
{
return RSI()/100;
}
y=simuchange();
x = lastvalue(ref(y, barindex()-barcount));


but unfortunately, the result of this script is that X is always the same, it doesnt change in relation of barindex(). I dont know why. 'Ref' is able to use array as period (REf (array, array)) and in fact I dont receive an error but it seems anyway AFL doesnt process this function as I expect.

Another question: why do you assign simuchange to Y? cannot we deal directly with simuchange()?
x = lastvalue(ref(simuchange(), barindex()-barcount));
I achieve just the same results.

thank you for your perseverance with me.

Cippo

Code:
bi=BarIndex();

_Trace("C[-1] byRef  = " + Ref(C, -1));
_Trace("C[-1] byBarIndex = " + C[selectedvalue(bi-1)-bi[0]] );

sh= -RSI();
_Trace("shift =" + sh);
_Trace("C[shift] =" + Ref(C, sh));
Go and try out the above example and fixed your own problem.

Here the reference
http://www.amibroker.com/guide/afl/ref.html

From the Syntax: Which means that the period parameter had to be a number

However in the Function Description
....The function accepts periods parameter that can be constant as well as time-variant (array).

- for constant : ref(C, -1)

- for array:
shift = -1 // which is a constant number
ref(C, shift);


With array specification, then possible of doing numeric formula calculation such as sh*5-10 ...etc easily in the AFL (interpreter).
No so easily and need a lot of work to do the same in the compiler like plug-in.


However with the array specification, you give the change array with no clear indication of the intend location, the ref will not give you the correct answer.

BarIndex() created a array of numbering the bar no from previous to current =>0...Barcount-1.
Ref() do from current to previous 0, -1, -2, -3..... (ignore future)

you can see the example, i just need to use the selectedvalue to get the position of the cursor location
C[selectedvalue(bi-1)-bi[0]]

will be the same result using Ref(C, -1)


Once you know how the barindex work out, then you should be able to figure out how to find your intended location by change the -1 to x location, and come out your formula to access the nth item. Then the barcount may come into play for you. the lastvalue(), ref() may not necessary




why i assign simuchange to Y? cannot we deal directly with simuchange()?
You can do it as many time as you want.

But each time you call, you are getting 1 memory block of 0...barcount -1 of data.
if your statement is doing once and for all, then it ok.

but if you need to access many time the individual item, then better store into a array, and access it one by one.


_Trace() is the way that you can find the bug and solve your problem yourself if you know how to analysis the issue.


I stop here. If you got further question, you can ask trash, he is more resourceful in AFL then me. He always work out the AFL more simple then me.
good luck.
 
Last edited:
#10
Re: Peak instruction doesn't allow 'change%' argument as array

Code:
Once you know how the barindex work out, then you should be able to figure out how to find your intended location by change the -1 to x location, and come out your formula to access the nth item. Then the barcount may come into play for you. the lastvalue(), ref() may not necessary

_Trace() is the way that you can find the bug and solve your problem yourself if you know how to analysis the issue.

good luck.
dear Kelvin,
thank you for your suggestion about _Trace, I tried it, but anyway I prefer 'Plot' instead of _Trace to find out bugs of my scripts.

And I eventually understood where is the problem with your solution (lastvalue function) ... although I have not been able to solve it ... but maybe there is no solution.

you suggested me to use 'lastvalue' function to transform an array in constant. This works!
x = lastvalue(ref(y, -10));

but when I try to replace the '-10' with an array,
z= barindex()-BarCount;
x = lastvalue(ref(y, z));

it happen that 'lastvalue' find the last item of this second array too!

so the final result is not an 'x' that change at each bar but an 'x', always the same for each bar, and referring for each bar to the value of lastvalue of y and lastvalue of z.

so I conclude that in this case 'lastvalue' cannot be the solution.

thank you so much anyway.

ciao,

cippo
 
Last edited:

Similar threads