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.