Table of Contents
AmiBroker
All base con AmiBroker 5.7
refer:
AmiBroker Formula(AFL file)
refer:
- AFL Functions Tutorials:
- AFL files:
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.
Bar | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | Open | 1,23 | 1,24 | 1,21 | 1,26 | 1,24 | 1,29 | 1,33 | 1,32 | 1,35 | 1,37 |
- Sum 2 arrays:
MyVariable = ( High + Low )/2;
Bar | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | High (built-in array) | 1,24 | 1,27 | 1,25 | 1,29 | 1,25 | 1,29 | 1,35 | 1,35 | 1,37 | 1,29 |
2 | Low (built-in array) | 1,20 | 1,21 | 1,19 | 1,20 | 1,21 | 1,24 | 1,30 | 1,28 | 1,31 | 1,27 |
3 | High+Low (temporary array created during evaluation) | 2,44 | 2,48 | 2,44 | 2,49 | 2,46 | 2,53 | 2,65 | 2,63 | 2,68 | 2,46 |
4 | ( High+Low ) /2 (gets assigned to MyVariable) | 1,22 | 1,24 | 1,22 | 1,245 | 1,23 | 1,265 | 1,325 | 1,315 | 1,34 | 1,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;
Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | Open | 1,23 | 1,24 | 1,21 | 1,26 | 1,24 | 1,29 | 1,33 | 1,32 | 1,35 | 1,37 |
2 | High | 1,24 | 1,27 | 1,25 | 1,29 | 1,25 | 1,29 | 1,35 | 1,35 | 1,37 | 1,29 |
3 | Low | 1,20 | 1,21 | 1,19 | 1,20 | 1,21 | 1,24 | 1,30 | 1,28 | 1,31 | 1,27 |
4 | Close | 1,23 | 1,26 | 1,24 | 1,28 | 1,25 | 1,25 | 1,31 | 1,30 | 1,32 | 1,28 |
5 | Volume | 8310 | 3021 | 5325 | 2834 | 1432 | 5666 | 7847 | 555 | 6749 | 3456 |
6 | Ref( Volume, -1 ) (temporary array created during eval) | Null | 8310 | 3021 | 5325 | 2834 | 1432 | 5666 | 7847 | 555 | 6749 |
7 | MA( Close, 3 ) (temporary array created during eval) | Null | Null | 1,243 | 1,260 | 1,257 | 1,260 | 1,270 | 1,287 | 1,310 | 1,300 |
8 | Cond1 = Close < MA(close,3) (gives 1 (or true) if condition met, zero otherwise) | Null | Null | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 1 |
9 | Cond2 = Volume > Ref(volume,-1) | Null | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
10 | Buy = Cond1 AND Cond2 | Null | Null | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
11 | Sell = High > 1.30 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 |
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:
- Step1: Goto Amibroker Analysis→New Analysis and select Apply to All Symbols and Range From-To dates
- Step2: Select Formula from AmiBroker\Formulas\Exploration\
- 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
- Step4: click button Report to see summary of Analysic
Configure Filter for stocks which you want to run analysic:
Favorite Analysics
- 1443-double-top-and-bottom-detection.afl
- 2464-bollinger-band-squeeze-highlighter-and-exploration.afl