/*
 *  An informative chart title - worked example for Part 10.
 *
 *  Assumptions
 *    - Chart pane only. Title is an Indicator-context reserved variable; it has
 *      no effect in a Scan, an Exploration or a Backtest.
 *    - Any interval. The date line switches itself between date-only and
 *      date-and-time because DateTimeToStr mode 3 omits the time part on
 *      end-of-day records.
 *    - Every number reported is taken at the SELECTED bar - the bar under the
 *      vertical selection line - which is not necessarily the last bar.
 */

_SECTION_BEGIN( "Informative title" );

AtrPeriod  = Param( "ATR periods", 14, 2, 200, 1 );
TrendSpan  = Param( "Trend reference periods", 200, 20, 500, 5 );
Compact    = ParamToggle( "Compact (small screens)", "No|Yes", 0 );

// styleNoTitle keeps the price plot's own value out of the title, because this
// formula writes the whole title itself.
Plot( Close, "Price", colorDefault, styleCandle | styleNoTitle );

// Mode 2 sets a flag on an existing pane. Mode 0 would only apply the first
// time the chart was inserted, which is why editing a mode-0 call looks broken.
SetChartOptions( 2, chartWrapTitle );

ChangePercent = 100 * ( Close - Ref( Close, -1 ) ) / Ref( Close, -1 );
AtrPercent    = 100 * ATR( AtrPeriod ) / Close;

// SelectedValue returns a single NUMBER, so it is safe inside if().
// An array condition would need IIf() instead.
SelectedChange = SelectedValue( ChangePercent );
if( SelectedChange >= 0 ) ChangeTint = colorGreen; else ChangeTint = colorRed;

// EncodeColor returns a STRING. It is concatenated into the title with "+",
// never passed to Plot() as a colour.
HeadLine = EncodeColor( colorDefault )
         + Name() + "   " + Interval( 2 ) + "   "
         + DateTimeToStr( SelectedValue( DateTime() ), 3 );

// StrFormat takes %f, %e and %g for numbers and %s for strings. %d does not
// work, because AFL has no integers. A literal percent sign must be written %%.
PriceLine = StrFormat( "O %g   H %g   L %g   C %g",
                       SelectedValue( Open ), SelectedValue( High ),
                       SelectedValue( Low ),  SelectedValue( Close ) );

ChangeLine = EncodeColor( ChangeTint )
           + NumToStr( SelectedChange, 1.2 ) + "%"
           + EncodeColor( colorDefault );

VolatilityLine = "ATR(" + NumToStr( AtrPeriod, 1.0 ) + ") "
               + NumToStr( SelectedValue( AtrPercent ), 1.2 ) + "% of price";

// WriteIf returns a string chosen by the SELECTED value of the condition,
// so it reads as one word rather than as an array.
TrendLine = "Close is "
          + WriteIf( Close > MA( Close, TrendSpan ), "above", "below" )
          + " the " + NumToStr( TrendSpan, 1.0 ) + "-bar average";

// _N() stops the assembled string from also being echoed into the Commentary
// or Interpretation window.
if( Compact )
    _N( Title = HeadLine + "\n"
              + "C " + NumToStr( SelectedValue( Close ), 1.2 ) + "   " + ChangeLine );
else
    _N( Title = HeadLine + "\n"
              + PriceLine + "   " + ChangeLine + "\n"
              + VolatilityLine + "   |   " + TrendLine );

_SECTION_END();
