{"id":1012,"date":"2014-12-22T04:40:32","date_gmt":"2014-12-22T09:40:32","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1012"},"modified":"2015-01-06T05:06:30","modified_gmt":"2015-01-06T10:06:30","slug":"text-output-in-explorations","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/12\/22\/text-output-in-explorations\/","title":{"rendered":"Text output in Explorations"},"content":{"rendered":"
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.<\/p>
AddTextColumn() function allows to display strings, so we can use it for displaying e.g. full name of the symbol or category assignment information:<\/p> <\/p> 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.<\/p> Let us check such a formula to illustrate the above statement:<\/p> 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:<\/p> <\/p> Therefore, such approach as above can only<\/em> 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.<\/p> If you want to display the value for other bars than last bar of selected range, you need an extra column, like this:<\/p> You can use functions like Ref<\/a>() or ValueWhen<\/a>() 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.<\/p> 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:<\/p> <\/p> Note: If you are using version older than 5.90, you need to use Asc function instead of single-character literals, as shown below:<\/p>Filter <\/span>= <\/span>1<\/span>;
<\/span>AddTextColumn<\/span>( <\/span>FullName<\/span>(), <\/span>"FullName"<\/span>);
<\/span>AddTextColumn<\/span>( <\/span>SectorID<\/span>(<\/span>1<\/span>), <\/span>"Sector"<\/span>);
<\/span>AddTextColumn<\/span>( <\/span>IndustryID<\/span>(<\/span>1<\/span>), <\/span>"Industry"<\/span>);
<\/span>AddColumn<\/span>( <\/span>Close<\/span>, <\/span>"Close"<\/span>)<\/code>condition <\/span>= <\/span>Close <\/span>> <\/span>Open<\/span>;
<\/span>Filter <\/span>= <\/span>1<\/span>;
<\/span>AddColumn<\/span>( <\/span>Open<\/span>, <\/span>"Open" <\/span>);
<\/span>AddColumn<\/span>( <\/span>Close<\/span>, <\/span>"Close" <\/span>);
<\/span>AddColumn<\/span>( <\/span>condition<\/span>, <\/span>"Condition"<\/span>, <\/span>1.0<\/span>);
<\/span>\/\/ WriteIf returns a SINGLE STRING representing condition at last bar of selected range
<\/span>text <\/span>= <\/span>WriteIf<\/span>( <\/span>condition<\/span>, <\/span>"Close above Open"<\/span>, <\/span>"Close below Open" <\/span>);
<\/span>\/\/ the text variable represents value AT THE LAST BAR of selected range
<\/span>AddTextColumn<\/span>( <\/span>text<\/span>, <\/span>"text" <\/span>)<\/code>condition <\/span>= <\/span>Close <\/span>> <\/span>Open<\/span>;
<\/span>Filter <\/span>= <\/span>1<\/span>;
<\/span>AddColumn<\/span>( <\/span>Open<\/span>, <\/span>"Open" <\/span>);
<\/span>AddColumn<\/span>( <\/span>Close<\/span>, <\/span>"Close" <\/span>);
<\/span>AddColumn<\/span>( <\/span>condition<\/span>, <\/span>"Condition"<\/span>, <\/span>1.0<\/span>);
<\/span>\/\/ WriteIf returns a SINGLE STRING representing condition at last bar of selected range
<\/span>text <\/span>= <\/span>WriteIf<\/span>( <\/span>condition<\/span>, <\/span>"Close above Open"<\/span>, <\/span>"Close below Open" <\/span>);
<\/span>AddTextColumn<\/span>( <\/span>text<\/span>, <\/span>"Last bar text" <\/span>);
<\/span>\/\/ Note that we are now using Ref() function to reference previous bar data
<\/span>text2 <\/span>= <\/span>WriteIf<\/span>( <\/span>Ref<\/span>( <\/span>condition<\/span>, -<\/span>1 <\/span>), <\/span>"Close above Open"<\/span>, <\/span>"Close below Open" <\/span>);
<\/span>AddTextColumn<\/span>( <\/span>text2<\/span>, <\/span>"Previous bar text" <\/span>)<\/code>Version<\/span>( <\/span>5.90 <\/span>); <\/span>\/\/ only works for version 5.90 and above
<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>Sell <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );
<\/span>Filter <\/span>= <\/span>Buy <\/span>OR <\/span>Sell<\/span>;
<\/span>AddColumn<\/span>( <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>'B'<\/span>, <\/span>'S' <\/span>), <\/span>"Signal"<\/span>, <\/span>formatChar <\/span>)<\/code>Buy <\/span>= <\/span>Cross<\/span>(<\/span>MACD<\/span>(),<\/span>Signal<\/span>());
<\/span>Sell <\/span>= <\/span>Cross<\/span>(<\/span>Signal<\/span>(),<\/span>MACD<\/span>());
<\/span>Filter <\/span>= <\/span>Buy <\/span>OR <\/span>Sell<\/span>;
<\/span>AddColumn<\/span>( <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>Asc<\/span>(<\/span>"B"<\/span>), <\/span>Asc<\/span>(<\/span>"S"<\/span>)), <\/span>"Signal"<\/span>, <\/span>formatChar <\/span>)<\/code>