User Tools

Site Tools


my:amibroker

AmiBroker

AmiBroker Formula(AFL file)

Understanding how AFL works

Introduce AFL

One of most important aspects of AFL is that it is an array processing language. It operates on arrays (or rows/vectors) of data. This way of operation is quite similar to the way how popular spreadsheets work (like Microsoft Excel). Anyone familiar with MS Excel should have no trouble quickly picking up AFL. - In fact all the examples in this article were all created using MS Excel.

Array in AFL

Arrays in AFL: An array is simply a list (or row) of values. In some books it may be referred to as a vector. Each numbered row of values in the example represents an individual array. Amibroker has stored in its database 6 arrays for each symbol. One for opening price, one for the low price, one for the high price, one for the closing price and one for volume (see the rows labelled 1-5 below) and one for open interest. These can be referenced in AFL as open, low, high, close, volume, openint or o, l, h, c, v, oi.

Bar12345678910
1Open1,231,241,211,261,241,291,331,321,351,37
  • Sum 2 arrays:
    MyVariable = ( High + Low )/2;
Bar12345678910
1High (built-in array) 1,241,271,251,291,251,291,351,351,371,29
2Low (built-in array) 1,201,211,191,201,211,241,301,281,311,27
3High+Low (temporary array created during evaluation)2,442,482,442,492,462,532,652,632,682,46
4( High+Low ) /2 (gets assigned to MyVariable) 1,221,241,221,2451,231,2651,3251,3151,341,23
  • Moving averages, conditional statements: Let us now consider the following code:
    Cond1 = Close > MA( Close, 3 );
    Cond2 = Volume > Ref( Volume, -1 );
    Buy = Cond1 AND Cond2;
    Sell = High > 1.30;
Day12345678910
1Open1,231,241,211,261,241,291,331,321,351,37
2High1,241,271,251,291,251,291,351,351,371,29
3Low1,201,211,191,201,211,241,301,281,311,27
4Close1,231,261,241,281,251,251,311,301,321,28
5Volume831030215325283414325666784755567493456
6Ref( Volume, -1 ) (temporary array created during eval)Null83103021532528341432566678475556749
7MA( Close, 3 ) (temporary array created during eval)NullNull1,2431,2601,2571,2601,2701,2871,3101,300
8Cond1 = Close < MA(close,3) (gives 1 (or true) if condition met, zero otherwise)NullNull10110001
9Cond2 = Volume > Ref(volume,-1)Null010011010
10Buy = Cond1 AND Cond2NullNull10010000
11Sell = High > 1.300000001110

Conditional Funcion and Loop

Conditional Function:

IIF(condition, truepart, falsepart)

Loop:

Period = ... some calculation
 
vaexp[ 0 ] = Close[ 0 ]; // initialize first value
 
for( i = 1; i < BarCount; i++ )
{
  // calculate the value of smoothing factor
  Factor = 2/(Period[ i ] + 1 );
 
  // calculate the value of i-th element of array
  // using this bar close ( close[ i ] ) and previous average value ( vaexp[ i - 1 ] )
  vaexp[ i ] = Factor * Close[ i ] + ( 1 - Factor ) * vaexp[ i - 1 ];
}

Add Condition to scaning stocks

Edit 1443-double-top-and-bottom-detection.afl to add condition for scanning: change original code

doubletop = PK AND abs( peakdiff - 1 ) < validdiff AND (Xpk1 -Xpk2)>mindistance AND High > HHV( Ref( H, fwdcheck ), fwdcheck - 1 ); 
doubleBot=tr AND abs( troughdiff - 1 ) < validdiff AND (Xtr1 -Xtr2)>mindistance AND Low < LLV( Ref( L, fwdcheck ), fwdcheck - 1 ); 

to add condition Volume > 100000 for scanning

doubletop = PK AND abs( peakdiff - 1 ) < validdiff AND (Xpk1 -Xpk2)>mindistance AND High > HHV( Ref( H, fwdcheck ), fwdcheck - 1 ) AND Volume > 100000; 
doubleBot=tr AND abs( troughdiff - 1 ) < validdiff AND (Xtr1 -Xtr2)>mindistance AND Low < LLV( Ref( L, fwdcheck ), fwdcheck - 1 ) AND Volume > 100000; 

Exploration stocks with Filter in AFL

refer: https://www.amibroker.com/guide/h_exploration.html

One of the most useful features of the Analysis window is called “Exploration”. Basically, an exploration works in a similar way to scan but instead of looking for and reporting just buy/sell signals it allows you to generate customizable screening (or exploration) report that can give you much more information than simple scan.

  • Volume >= 100000:
    Filter = Volume >= 100000;
  • Close ⇐ 5:
    Filter = Close <= 5;
  • Both conditions above
    Filter = Volume >= 100000 AND Close <= 5;

Run Analysic

Basic steps:

  1. Step1: Goto Amibroker Analysis→New Analysis and select Apply to All Symbols and Range From-To dates
  2. Step2: Select Formula from AmiBroker\Formulas\Exploration\
  3. Step3: Click Scan or Explore or Backtest to see results
    • Scan → run from date to dates in form
    • Explore → run from date to dates in form
    • Backtest → run from begin time of market to current date
  4. Step4: click button Report to see summary of Analysic

Configure Filter for stocks which you want to run analysic:

  1. Step1: click icon below to display filter window:
  2. Step2: select List stocks was included in filter, in below case was list MyStocks:

Favorite Analysics

  • 1443-double-top-and-bottom-detection.afl
  • 2464-bollinger-band-squeeze-highlighter-and-exploration.afl
my/amibroker.txt · Last modified: 2022/10/29 16:15 by 127.0.0.1