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

Re: [amibroker] programming 101



PureBytes Links

Trading Reference Links

Sidney,

One thing that has to be noted is a fundamental difference between
TS and AB in way of executing formula.

TS has a hidden loop (not editable by the user) that iterates
through all bars so your TS formula is executed not once but N times
where N is equal to number of bars.

In TS when you write 

if condition then a = b

this single statement will be executed N times for every bar separately.
This is so because TS has a hidden loop in form

for( bar = 0; bar < BarCount; bar++ )   /// <- this loop is generated by TS indirectly and can not be modified by the user
{
  
  /// YOUR TRADESTATION FORMULA HERE


}

(this 'super-loop' is not editable by the user and the way of operation
can not be changed by the user in Tradestation).

AmiBroker is more flexible.
Generally speaking it executes formula ONCE. It allows arrays to be processed
in one step (see User's Guide: Tutorial: Understanding how AFL works).

but... at the same time, thanks to looping statements it allows to
operate also on bar-by-bar manner as Tradestation. The difference
is that in AFL you have full control over bar loop. You can have more than
one loop, etc. Full control is given to the user.

Having said that in AFL there are two ways of achieving your goals:

1. Emulate TS behaviour by writing a loop:
==========================

num_of_occurrences = 0;

for( bar = 0; bar < BarCount; bar++ )
{ 
  /* your question 1 */
  if( condition[ bar ] )
  {
    a[ bar ] = b[ bar ];
   }  

  /* your question 2 */
  if( condition[ bar ] )
  {
    a[ bar ] = c[ bar ]; // assign new value
  }
  else
  {
    a[ bar ] = a[ bar - 1 ]; // keep previous value otherwise
  }


  /* your question 3 */
  if( condition[ bar ] )
  {
    num_of_occurrences++; // increase the counter
  }

}

Please note subscript operators used to access array elements at given bar.

2. Use ValueWhen, Cum, and other AFL special functions to achieve
the same effect without looping
=============================================

/* questions 1 and 2 */
it depends what you want to get when condition is NOT met. 
If you want to keep previous value then indeed you should write:

a = ValueWhen( condition, c );

if you want not to modify previous value (this is how I understand your question 1)
then

a = IIF( condition, b, a ); 

Note that a must be initialized before for this to work

/* your question 3 */
It is very simple:

counter = Cum( condition );

(running sum from the beginning)

or if you want to reset it by 'reset condition':
counter = Cum( condition );
counter_resetted = counter - Nz( ValueWhen( reset_condition, counter ) );

Best regards,
Tomasz Janeczko
amibroker.com

> (1)  IF condition then a=b // trying to use IIF , would that be: a = 
> IIF(condition, b, a);  ie: the true condition has a new value but ?? on the 
> value to be used for the false condition.
> 
> 
> (2)  IF condition then a=c  // Where the value for "a" needs to persist 
> until it is changed by the program.  Does that involve the use of 
> ValueWhen(condition, c);   and I would think that the condition needs to 
> change to a cross  function to pick up the first value where condition is 
> true.  Multi part question I guess, but the details are important to poor 
> programmers like myself.
> 
> 
> (3)  How do I set up a counter in AFL to keep track of the number of 
> occurrences of some condition.
> IF condition then increment counter // Again the value of the counter 
> needs to persist until it is used perhaps many bars later and then is 
> probably reset.
> 
> TIA
> Sid



----- Original Message ----- 
From: "Sidney Kaiser" <s9kaiser@xxxxxxxxxxx>
To: <AmiBroker@xxxxxxxxxxxxxxx>
Sent: Friday, May 16, 2003 8:07 PM
Subject: [amibroker] programming 101


> 
> 
> 
> 
> 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 http://docs.yahoo.com/info/terms/ 
> 
> 


--------------------------------------------------------------------------------


> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.481 / Virus Database: 277 - Release Date: 5/13/2003
> 

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get A Free Psychic Reading! Your Online Answer To Life's Important Questions.
http://us.click.yahoo.com/Lj3uPC/Me7FAA/uetFAA/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 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/