Hello,
AFL offers actually superset of
capabilities of "general purpose"
language like Pascal as it allows both "normal"
(i.e. scalar)
constructs like Pascal AND array based
operations (not available in Pascal).
It is UP TO YOU which one you use. There are NO
limitations and
of course in your case j variabel CAN be used as
moving average period.
The only problem is YOUR SKILL, not the
language.
Correct way to code thing like this
"// this
checks whether the price is above its moving av 52 bars ago
and if so
checks whether the close crossed mov av during the last 52
bars for a
range of moving av periods and return the moing av period
value when it
crosses"
is ( I am showing ONE OF MANY possible ways to
code this,
specifially I am showing the way how to code it
WITHOUT REF() statement
just to show you how you can use subscript
operator (as in Pascal))
// this checks
whether the price is above its moving av 52 bars ago
//AND
if so checks whether the Close crossed mov av during the last 52
//bars
for a range of moving av periods AND return the moing av period
//value
when it crosses
function CheckMACross( period, Lookback )
{
result = False;
Cma =
MA( C, period );
bar =
BarCount -
1 - Lookback;
if( Close[ bar ] < Cma[ bar ] )
{
while( bar < BarCount )
{
if( Close[ bar ] > Cma[ bar ] )
{
result =
True;
}
}
}
return result;
}
function WCBelowMALine()
{
found
= False;
for ( period =
52; j < 3 AND NOT found; j--)
{
found = CheckMACross( j, 52 );
}
return result;
}
Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
Sent: Sunday, March 26, 2006 1:44
PM
Subject: [amibroker] How to code For
loops without refering to array elements
Hi all
For up trending stoks, I want to find the
shortest Simple moving
average period values that do not cross the price
within any 52 bar
period for a portfolio of securities. Obviously
different stocks
will have different period values at any given point in
time and
also they will very with time within a stock.
I started
coding the following. But it seems the variables that we
can use
in a For loop can only be used for Array element and not
the way
general computer languages like Pascal work.
I wold appreciate
if someone can help me to get this code to work -
it appears that the j
variable cannot be used to increment the
moving av period
!!
Thanks in
advance
//==================================================
//
this checks whether the price is above its moving av 52 bars ago
and if
so checks whether the close crossed mov av during the last 52
bars for a
range of moving av periods and return the moing av period
value when it
crosses
function WCBelowMALine(C)
{
result = 0;
for (j=52;
j<3; j--)
{
result = IIf(Ref(C,-52) < Ref(MA(C,j),-52),
-1,IIf(Hold(Cross(MA
(C,J),C),52),j+1 ,9));
}
return result;
}
//==================================================