{"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>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>

\"Exploration<\/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>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>

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>

\"Exploration<\/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>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>

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>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>

\"Exploration<\/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>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>

More information about explorations can be found in the manual:
http:\/\/www.amibroker.com\/guide\/h_exploration.html<\/a><\/p>","protected":false},"excerpt":{"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.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 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[53,14,28],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1012"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=1012"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1012\/revisions"}],"predecessor-version":[{"id":1016,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1012\/revisions\/1016"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}