Indicator that colors bars not showing when using Drag and Drop

I'm testing the creation of a new indicator to see how it works when added to a chart using drag and drop.
I created a very simple indicator to set the bar color to red if the current bar is a down close.
I created this indicator in the Custom folder. When I drag and drop it onto a price chart the bars with down closes do not turn red.

The following is the AFL for the chart after the indicator "PaintTest" has been dropped onto it.
If I change the Price section to include the setting of the bar color it works just fine but I'd like to be able to drag and drop this indicator onto the chart in the same manner as I would, for example, a moving average.
Is this possible? Am I making a rookie mistake? I've tried many variation of the PaintTest indicator but cannot get it to work using drag and drop.

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("PaintTest");
Color = IIf(C < Ref(C,-1), colorRed, colorDefault );
Plot( C, "Close", Color, styleBar); 
_SECTION_END();

@rlewkov you are dropping a bar chart right on top of a candle chart.
Look at your codes.

Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Plot( C, "Close", Color, styleBar); 

So it "works" but it is difficult to see what is going on,

image

So you need to decide on one or the other, and not both on top of each other.

An alternative, not to "drag-and-drop" (just one of many possibilities)

_SECTION_BEGIN("PaintTest");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

BarColor = IIf(C < Ref(C,-1), colorRed, colorGreen );
Barthickness = Param("Bar Thickness", 3, 1, 6, 1);

PlotOHLC( Open, High, Low, Close, "", BarColor, styleBar, Null, Null, 0, 0, Barthickness);

_SECTION_END();

Just click "Apply".

2 Likes

My default bar style set via View, Price chart style is set to Bars. The following is a clip of the chart before I drag and drop the indicator on it (I hope this appears).

image

@rlewkov please re-read the @portfoliobuilder answer since he gave you a better way to do it (in general there is no need to plot 2 times the same data overlaying one to the other using the same type of chart style).

In any case, your code may work (with proper preferences settings), but to see your "painted bars" you have to invert the order of the 2 sections in your code:


_SECTION_BEGIN("PaintTest");
Color = IIf(C < Ref(C,-1), colorRed, colorDefault );
Plot( C, "Close", Color, styleBar); 
_SECTION_END();

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

The order of code execution in AmiBroker will decide then which will be the last drawn plot (overlaying the previous one).

A lot better way is to add to your Plot code a Z-order parameter:


_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("PaintTest");
Color = IIf(C < Ref(C,-1), colorRed, colorDefault );
Plot( C, "Close", Color, styleBar, Null, Null, 0, 1);  // added the Z order level to use for this plot
_SECTION_END();

But as said, in this case, it is better to follow @portfoliobuilder advice and use a single price custom indicator that will include your "paint" logic and use that instead of the standard one.

1 Like

Hello beppe,
Thanks for your reply. I hadn't thought about using z-order. I will mark this item as closed.
I'll try your suggestion re putting the Paint section first in the indicator. And yes, the suggestion of @portfoliobuilder works though my goal was to have a set of Paint indicators I could quickly drag and drop into a chart in order to highlight various bar patterns and not have to create a separate price indicator for each. I'll experiment and see what makes the most sense. One thing I tried as an alternative to painting the bar was to draw a symbol above/below the bar. This accomplished much of what I want to do and still allows a quick drag and drop. Thanks again