/*
 *  Plot styles, scales and layers - worked example for Part 10.
 *
 *  Assumptions
 *    - Chart pane formula. Apply it to the price pane, or insert it as its own
 *      pane; the same code works in both places, it simply looks different.
 *    - Any interval, any symbol. Symbols with no volume data draw an empty
 *      volume band rather than failing.
 *    - Nothing here is a trading rule. The formula exists to make the drawing
 *      mechanics visible: argument slots, own scaling, layering and hiding.
 */

_SECTION_BEGIN( "Price and layers" );

// GraphXSpace is a percentage of extra room added ABOVE AND BELOW the graph.
// The default when it is not set is 2%.
GraphXSpace = 6;

// A per-bar colour array. Plot()'s third argument accepts either a single
// colour number or one colour per bar, which is where conditional colouring
// comes from.
BarTint = IIf( Close >= Open,
               ParamColor( "Up bar", colorGreen ),
               ParamColor( "Down bar", colorRed ) );

// SetBarFillColor must PRECEDE the Plot() it applies to, and it only affects
// styleCandle, styleBar, styleArea and styleCloud. Here it fills the candle
// bodies while the outline keeps the colour passed to Plot().
SetBarFillColor( ColorBlend( BarTint, GetChartBkColor(), 0.55 ) );

// Argument slots, in documented order:
//   array, name, color, style, minvalue, maxvalue, XShift, ZOrder, width
Plot( Close, "Price", BarTint,
      ParamStyle( "Price style", styleCandle, maskPrice ) | GetPriceStyle(),
      Null, Null, 0, 1 );

MaPeriod = Param( "MA periods", 50, 2, 400, 1 );

// ZOrder 2 puts this line in front of the price plot. Within one ZOrder layer
// the LAST Plot() call is drawn FIRST, i.e. furthest back, so relying on call
// order alone is how plots end up hidden behind each other.
Plot( MA( Close, MaPeriod ), "MA " + NumToStr( MaPeriod, 1.0 ),
      ParamColor( "MA colour", colorBlue ), styleLine | styleThick,
      Null, Null, 0, 2 );

// A volume band pinned to the bottom of the pane. minvalue/maxvalue are
// documented as being used by styleOwnScale plots only; setting the ceiling to
// five times the tallest recent bar keeps volume inside the lowest fifth of the
// pane. ZOrder -1 draws it behind the grid, because ZOrder 0 is where the grid
// itself sits.
VolumeCeiling = Max( 5 * LastValue( HHV( Volume, 250 ) ), 1 );

Plot( Volume, "Volume",
      ColorBlend( ParamColor( "Volume colour", colorBlueGrey ), GetChartBkColor(), 0.5 ),
      styleArea | styleOwnScale | styleNoLabel | styleNoTitle,
      0, VolumeCeiling, 0, -1 );

// Not drawn at all, but the values are available in the Data window and the
// data tooltip. This is how you inspect an intermediate series without adding
// another line to an already busy chart.
Distance = 100 * ( Close - MA( Close, MaPeriod ) ) / MA( Close, MaPeriod );
Plot( Distance, "Distance from MA (%)", colorDefault, styleHidden );

// A constant level is cheaper to draw with PlotGrid than with Plot, and the
// official guide recommends it explicitly. Pattern 10 is a solid line.
PlotGrid( LastValue( MA( Close, MaPeriod ) ), colorGrey40, 10, 1, False );

_SECTION_END();
