[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Newbie, basic AFL questions



PureBytes Links

Trading Reference Links




Arold,
 
1. Please read:
Tutorial: Understanding how AFL works.
 
It is fundamental reading. It explains that AFL is array based 
language.
 
2. To get close 15 days ago use Ref( C, -15 );  
Buy = Ref( C, -15 ) < MA( C, 50 );
 
3. Your problem is explained 
in the User's Guide: AFL language reference :

More information
Please read also <A 
href="">Understanding 
how AFL works article to learn more. 
Common misunderstandings
"New if-else problem"
Question: 
Why I get the syntax error when I write: if( H > Ref(H,-1) ) 
Answer: 
if-else statement changes flow of execution (opposite to IIF function that 
evaluates all arguments and works on arrays) and you can not really 
writeif ( H >Ref(H,-1) )because it has no meaning. It would 
translate to " If high array is higher than high array shifted one bar" (see 
tutorial below). Flow control statement (such as if-else) has to get SINGLE 
boolean value to make decision which execution path should be taken. If you 
write H (or High) it means ARRAY (entire array).if you write H[ i ] - it 
means i-th element of the array. The subscript operator [ ] allows you to access 
individual array elements.Instead you should write:for( i = 1; i 
< BarCount; i++ ){   if ( High[ i ] > High[ i - 1 ] 
)   {      x[ i ] = High[ i 
];   }   else   { 
      x[ i ] = Low[ i 
];   } }this will translate to correct one 
"for EVERY BAR 'i' assign i-th element of high array to the i-th element of x 
array if i-th element of high array is higher than the previous element, 
otherwise assign i-th of low array to the i-th element of x array". The rule is: 
new if-else and while statements need single boolean value 
(not array) to decide which execution path should be taken. If you want to use 
them with arrays you have to iterate through bars using for loop (as 
shown above).
On the other hand this can be implemented in single line using old-style 
array operations and IIF function:
x = IIF( High > Ref( High, -1 ), High, Low );
This works because IIF operates on ARRAYS as described in the <A 
href="">tutorial.As 
you can see in many cases old-style AFL provides much more compact form. I 
always tried to explain this advantage of AFL but only a few realised that. New 
control statements should be used where it is better to use them. As I tried to 
explain during last years in 80% of cases 'old-style' AFL provides the shortest 
formula. Only remaining 20% of cases needed <A 
href="">script. 
Those 'script-only' cases now can be coded in native AFL thanks to new 
for/while/if-else statements. And this is correct usage of them - to replace 
script parts.
 
Hope this helps.
 
Best regards,Tomasz Janeczkoamibroker.com
----- Original Message ----- 
From: "Arold" <<A 
href=""><FONT 
size=2>arold_ite@xxxxxxxxx>
To: <<A 
href=""><FONT 
size=2>amibroker@xxxxxxxxxxxxxxx>
Sent: Wednesday, November 12, 2003 11:59 AM
Subject: [amibroker] Newbie, basic AFL 
questions
First of all, this discussion 
group (and the software) is GREAT.  It's so encouraging that such a 
group of like-minded peopleexist.As soon as I'm up to speed with 
AFL, I will make sure tocontribute in any way that I can.Anyway, I'm 
just starting out with AFL and I'm having troublegetting my head around a 
few basic concepts.  I should say thatI've been programming for 15 
years, but that obviously hasn'thelped me here!I've trawled through 
the historical discussions and also read the help files, etc, but I still 
can't seem to grasp the language properly.Here are my 
questions…1. Boolean ExpressionsI'm trying to write an 
Automatic Analysis Script (sorry, Idon't know the correct technical term for 
this) for issuing buyand sell signals.I understand that I need to 
initiate a `Buy' signal byassigning a `True' Boolean expression to the 
implicit`Buy'variable such as this…Buy = Close[15] > 
MA(Close,50);I think I have understood this concept, but please correct 
me if not.However, what I can't grasp is why AFL considers the 
aboveBoolean expression to be valid in the given scenario, but it does NOT 
consider it to be a valid Boolean expression in the following 
scenario…if ( Close[15] > MA(Close,50) )Buy = 
1;For this `If' statement, I am given the following 
error…Error 3.Condition in IF, WHILE, FOR statementshas to be 
Numeric or Boolean type.You can not use array here,please use [] (array 
subscript operator)to access array elementsCan somebody explain to 
me why AFL considers the same expression to be a valid Boolean expression in 
the first example but not the second?2. Previous Closing 
PriceHow do I retrieve the closing price for 15 bars ago?  I want 
to say… "Is this closing price of 15 bars ago greater than the50 day 
moving average of today's bar?"I appreciate that the code I've given so 
far is clearly wrongbecause the it's using the index 15 (i.e. 
Close[15]).  What Iwant to do is something like this…If ( 
Close[CurrentBar – 15] > MA(Close,50) )But I can't figure out how to 
get a handle to the index of the current bar so that I can subtract 15 from 
it?3. Boolean FunctionsHow do I write a Boolean 
function?I want to write something like this…function 
myBooleanFunction(){myReturnValue = 0;if ( expression )if ( 
expression )if ( expression )myReturnValue = 1;return 
myReturnValue;}if ( myBooleanFunction() == 1 )Buy = 
1;elseSell = 1;Is this the correct way to write and use a 
boolean function?Thanks for your 
helpArold------------------------ Yahoo! Groups 
Sponsor ---------------------~-->Buy Ink Cartridges or Refill Kits for 
your HP, Epson, Canon or LexmarkPrinter at MyInks.com. Free s/h on orders 
$50 or more to the US & Canada.<A 
href=""><FONT 
size=2>http://www.c1tracking.com/l.asp?cid=5511<A 
href=""><FONT 
size=2>http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/GHeqlB/TM<FONT 
size=2>---------------------------------------------------------------------~->Send 
BUG REPORTS to <FONT 
size=2>bugs@xxxxxxxxxxxxxSend SUGGESTIONS to 
<FONT 
size=2>suggest@xxxxxxxxxxxxx<FONT 
size=2>-----------------------------------------Post AmiQuote-related 
messages ONLY to: <FONT 
size=2>amiquote@xxxxxxxxxxxxxxx (Web page: <A 
href=""><FONT 
size=2>http://groups.yahoo.com/group/amiquote/messages/<FONT 
size=2>)--------------------------------------------Check group FAQ at: 
<A 
href=""><FONT 
size=2>http://groups.yahoo.com/group/amibroker/files/groupfaq.html<FONT 
size=2> Your use of Yahoo! Groups is subject to <A 
href=""><FONT 
size=2>http://docs.yahoo.com/info/terms/ 







Yahoo! Groups Sponsor


  ADVERTISEMENT 









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



Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.