How To - Draw Plots and Shapes on Layers

Because I want to show or hide various indicators on my chart, I use layers. I have discovered that while I can display my plots on a nonexistent layer, I cannot display shapes on the same nonexistent layer.

Does AFL have a function that returns the number of available layers; adding or removing a layer; and showing or hiding layers?

Heres a code snippet. Simply change the value of zOrder to a layer number greater than the number of Layers defined and insert the formula onto a chart window.

//Formula: Sandbox-Layers.afl zOrder = 16; Graph0=MACD(); Graph1=Signal(); Buy=Cross(Graph0, Graph1); Sell=Cross(Graph1, Graph0); Plot(Graph0,"MACD",colorRed, style = 1, layer=zOrder); Plot(Graph1,"Signal",colorGreen, style = 1, layer = ZOrder); PlotShapes( ( Buy * shapeUpArrow + Sell * shapeDownArrow), IIf( Buy, colorGreen, colorRed ), layer = zOrder );

ShowMACD = ParamToggle("MACD","Hide|Show",1);
ShowSignal = ParamToggle("Signal","Hide|Show",1);
ShowShapes = ParamToggle("Shapes","Hide|Show",1);
Graph0 = MACD();
Graph1 = Signal();
Buy = Cross( Graph0, Graph1 );
Sell = Cross( Graph1, Graph0 );

if( ShowMACD )
	Plot( Graph0, "MACD", colorRed, style = 1);
if( ShowSignal )
	Plot( Graph1, "Signal", colorGreen, style = 1);
if( ShowShapes )
	PlotShapes( ( Buy * shapeUpArrow + Sell * shapeDownArrow ), IIf( Buy, colorGreen, colorRed ));
1 Like

aWilson, thanks! This is a nice workaround for showing or hiding a plot. I’m afraid it does not address my question:

Does AFL have a function that returns the number of available layers; adding or removing a layer; and showing or hiding layers?

Why do you always think the other way round?

UI controls the program, not the other way round.
UI is steering wheel. Your program is a car.

It is the steering wheel that controls the car, not car controlling the steering wheel.

If later was allowed it leads to disaster (which is, by the way, precisely what is going to happen once
Google Cars will be released to the public use. The number of car accidents would sky rocket as long as any humans will be driving their cars too).

If AFL gave ability to control layers UI the first thing that would happen is that people would copy-paste formulas from somewhere and start complaining “layers don’t work” because formula would override any UI actions (“look steering wheel disappeared because formula writter called RemoveSteeringWheel() function”)

1 Like

LOL! :slight_smile:
We can agree on the autonomous vehicles!

The more I use Amibroker’s AFL, the more powerful it becomes! The latest version 6.22 is awesome! I am becoming addicted to the possibilities that AFL provides! Forgive me for wanting more!!!

Basically, I want to overcome the issue of inadvertently plotting shapes to a nonexistent layer. I want my program/formula to have a safety-net to avoid this in the future. Here’s a Hypothetical code snippet that includes a function to return the maximum number of layers.

Where:
GfxGetZOrder(0) returns the current layer number.
GfxGetZorder(1) returns a number representing the last layer.
GfxGetZorder(2) returns the friendly name for the current layer.

I believe this approach seems to be compliant with your desire to not add layers from the formulas.
Perhaps there is an alternative way or function to return the layer number.

But for now, I decided to hard code the maximum number of layers into my code.

layerCount = 10 Plot(MA(Close,20), "Shifted MA", colorRed, styleLine, Null, Null, 10, ZOrder = Min(myDesiredLayer, layerCount );

Thanks again Tomasz. You’re the best!

There is some misconception about the difference between layer and Z-order.
These two are different and separate.

Z-order represents just the order in which graphical elements are drawn by rendering engine. It is not connected to layers in any way.

Layer on the other hand is a UI-driven feature that allows to control visibility (but not the drawing order) of certain elements like trend-lines and shapes. It allows hiding those elements for example depending on selected chart interval (like for example hiding 1-minute trend lines on daily charts).

Manually drawn studies (like trend lines) can have Z-order and Layer selected independently. Two trend lines may share same Layer but use different Z-order or share same Z-order but use different layers, or use different layers and different z-orders.

But all PlotShape()-drawn items are always drawn with Z-order equal to 0 (zero).

So you don’t need to differentiate Z-order in Plot() calls based on which layer is active.

2 Likes