PureBytes Links
Trading Reference Links
|
Hi,
I'm puzzled by AFL's Array processing more than by looping. So let
me give some comments on the loops (with 'i' as loop variable) and
hopefully I will understand array processing better as we go along:
1. Loops should process the elements of the arrray seperately. Ref()
operates on the array. It should NOT be used in a loop.
instead of Ref(array,-j) use array[i-j]
2. Here's how I would code an EMA with a loop. I avoided all
functions that do array processing.I got a match to AB's EMA for low
values of n.
The code is:
function MN(n)
{
//initialize
result = MA(C,n);
factor = 2/(n+1);
for (i=n+1;i<BarCount;i++)
{
result[i] = factor*C[i]+(1-factor)*result[i-1];
}
return result;
}
Plot(MN(2),"MN(2)", colorBlue);
Plot(EMA(C,2),"MA(2)",colorRed);
See also the description of DEMA in the user's guide. Tomasz
provides sample code for this indicator using a loop.
3. Here's my problem. As far as I understand array processing, the
loop is not really needed in afl. My first try:
result = (n-1)/n*ref(result,-1) + 1/n*C;
No luck. What's wrong? The problem seems that result is on both
sides of the definition. But EMA is a recursive function-can one
program it without loops?
4. I try AMA:
result = ama(c,1/n);
Better, but the problem now is the inizialization. So, try again:
function MNarray(n)
{
movav = MA(C,n);
// use a local dummy array for the initialization
dummy = IIf( IsEmpty(Ref(movav,-1)) ,movav, C);
result = AMA(dummy,2/(n+1));
return result;
}
Yes!
Peter
--- In amibroker@xxxxxxxxxxxxxxx, "treliff" <treliff@xxxx> wrote:
> Excellent points. In this case I may have been rushing to the hard
> part (for me, that is, the loop-code) before getting the basics
> right.
>
> Btw I am not so sure about using "result" on both sides of the
> definition, in general that is, as I used an earlier message code
> assistance (msg. 60062) as a guideline (admittedly a completely
> different function).
>
> Thanks for your input.
>
> treliff
>
>
> --- In amibroker@xxxxxxxxxxxxxxx, "DIMITRIS TSOKAKIS"
> <TSOKAKIS@xxxx> wrote:
> > It would be better, before the exercise, to check the formula
you
> use.
> > a.
> > In your basic
> > result = ((i-1)/i)*result + (1/i)*Ref(C,-(i-1)) ;
> > "result" can not be in both sides of the definition.
> > On the other side, a simple MA is not a recursive function.
> > For the simple MA(C,3) the last value is
> > M0=(C0+C1+C2)/3
> > The previous value is
> > M1=(C1+C2+C3)/3
> > As you see, C0 is not included in M1 calculation.
> > b.
> > Since result is a function of i, there should be something like
> > result[i]=...
> > for the ith element of the result array
> > c.
> > there is a confusion in Ref(C,-(i-1)) .
> > The 801th element of a MA(C,3) in a 1000-bar Close array can not
> be a
> > function of the Ref(C,-(801-1)) or the Ref(C,-800) !!!
> > It would help to read the EMA, AMA analytic formulas from the
HELP
> > manual.
> > Dimitris Tsokakis
> > --- In amibroker@xxxxxxxxxxxxxxx, "treliff" <treliff@xxxx> wrote:
> > > Looping stuff.... I've had some guidance from the forum and TJ
> > > and
> > > thought I got it, but every new loop I take on gets stuck.
> > >
> > > I went back to the basics, as an exercise trying to write a
> > function
> > > loop for a simple moving average:
> > >
> > > function MN(n)
> > > {
> > > // local result;
> > > result = C ;
> > > for (i=2;i<BarCount;i++)
> > > {
> > > result = ((i-1)/i)*result + (1/i)*Ref(C,-(i-1)) ;
> > > }
> > > return result;
> > > }
> > > Plot(MN(2),"MN(2)", colorBlue);
> > > Plot(MA(C,2),"MA(2)",colorRed);
> > >
> > > Just doesn't work, my MN(n) is empty for every n. What's
> > > wrong?
> > > Thanks for your patience with me.
> > >
> > > treliff
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/GHeqlB/TM
---------------------------------------------------------------------~->
Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|