December 22, 2014
Text output in Explorations
Explorations allow to display not only numerical data but also text, there are however certain restrictions what can and can’t be displayed in the exploration result list as a text.
AddTextColumn() function allows to display strings, so we can use it for displaying e.g. full name of the symbol or category assignment information:
Filter = 1;
AddTextColumn( FullName(), "FullName");
AddTextColumn( SectorID(1), "Sector");
AddTextColumn( IndustryID(1), "Industry");
AddColumn( Close, "Close")
It is worthwhile to note that these strings displayed above do not vary across historical bars. That is important, because there is no such structure in AFL as an ‘array of strings’, therefore an attempt to generate a text, which varies on each bar will not work. Instead a string representing selected array value (or last value) will be displayed.
Let us check such a formula to illustrate the above statement:
condition = Close > Open;
Filter = 1;
AddColumn( Open, "Open" );
AddColumn( Close, "Close" );
AddColumn( condition, "Condition", 1.0);
// WriteIf returns a SINGLE STRING representing condition at last bar of selected range
text = WriteIf( condition, "Close above Open", "Close below Open" );
// the text variable represents value AT THE LAST BAR of selected range
AddTextColumn( text, "text" )
If we look at the output over more than one bar, then we can see that the condition from the last bar determines the text output in the column:
Therefore, such approach as above can only be used in situations where we run the exploration applied e.g. to 1-Recent bar, because it’s the last bar from the range which determines the text displayed in the column in such situation.
If you want to display the value for other bars than last bar of selected range, you need an extra column, like this:
condition = Close > Open;
Filter = 1;
AddColumn( Open, "Open" );
AddColumn( Close, "Close" );
AddColumn( condition, "Condition", 1.0);
// WriteIf returns a SINGLE STRING representing condition at last bar of selected range
text = WriteIf( condition, "Close above Open", "Close below Open" );
AddTextColumn( text, "Last bar text" );
// Note that we are now using Ref() function to reference previous bar data
text2 = WriteIf( Ref( condition, -1 ), "Close above Open", "Close below Open" );
AddTextColumn( text2, "Previous bar text" )
You can use functions like Ref() or ValueWhen() to refer to other bar’s data, or you can use array subscript operator like this condition[ 1 ] to get value of condition at bar with index 1.
There is an alternative method to display values that change on bar by bar basis as letters though. Instead of displaying full string we can display single characters in a column using formatChar parameter, as shown in the code below:
Version( 5.90 ); // only works for version 5.90 and above
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Filter = Buy OR Sell;
AddColumn( IIf( Buy, 'B', 'S' ), "Signal", formatChar )
Note: If you are using version older than 5.90, you need to use Asc function instead of single-character literals, as shown below:
Buy = Cross(MACD(),Signal());
Sell = Cross(Signal(),MACD());
Filter = Buy OR Sell;
AddColumn( IIf( Buy, Asc("B"), Asc("S")), "Signal", formatChar )
More information about explorations can be found in the manual:
http://www.amibroker.com/guide/h_exploration.html
Filed by Tomasz Janeczko at 4:40 am under Exploration
Comments Off on Text output in Explorations