Add arrow above the 10 days, 20 days, 60 days and 120 days from today

Hi, Everyone

I am a newbie for amibroker user and I try to write some Afls.
The afl was shown as follows:

_SECTION_BEGIN("Q_Plotarrow last bars");
PositionToPlot = BarIndex() == BarCount -11;
BullCandleStick =C > 0 AND PositionToPlot;
BearCandleStick =C < 0 AND PositionToPlot;

Shape = BullCandleStick * shapeUpArrow + BearCandleStick * shapeDownArrow;

PlotShapes(Shape, IIf( BullCandleStick, colorGreen, colorRed),0, IIf(BullCandleStick, Low,High));

_SECTION_END();

PositionToPlot1 = BarIndex() == BarCount -21;
BullCandleStick =C > 0 AND PositionToPlot;
BearCandleStick =C < 0 AND PositionToPlot;

Shape = BullCandleStick * shapeUpArrow + BearCandleStick * shapeDownArrow;

PlotShapes(Shape, IIf( BullCandleStick, colorYellow, colorPink),0, IIf(BullCandleStick, Low,High));

_SECTION_END();

PositionToPlot2 = BarIndex() == BarCount -61;
BullCandleStick =C > 0 AND PositionToPlot;
BearCandleStick =C < 0 AND PositionToPlot;

Shape = BullCandleStick * shapeUpArrow + BearCandleStick * shapeDownArrow;

PlotShapes(Shape, IIf( BullCandleStick, colorPaleBlue, colorTan),0, IIf(BullCandleStick, Low,High));

_SECTION_END();

PositionToPlot3 = BarIndex() == BarCount -121;
BullCandleStick =C > 0 AND PositionToPlot;
BearCandleStick =C < 0 AND PositionToPlot;

Shape = BullCandleStick * shapeUpArrow + BearCandleStick * shapeDownArrow;

PlotShapes(Shape, IIf( BullCandleStick, colorBrown, colorWhite),0, IIf(BullCandleStick, Low,High));

_SECTION_END();

But it was not work,

Can anyone help me to correct it??

Many thanks

@roychan before answering your question I recommend you to learn how to post code examples in the forum.

A first (BAD code - still wrong) attempt to fix it could be:

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

_SECTION_BEGIN("Q_Plotarrow last bars");
PositionToPlot = BarIndex() == BarCount -11;
BullCandleStick =C > 0 AND PositionToPlot;
BearCandleStick =C < 0 AND PositionToPlot;
Shape = BullCandleStick * shapeUpArrow + BearCandleStick * shapeDownArrow;
PlotShapes(Shape, IIf( BullCandleStick, colorGreen, colorRed),0, IIf(BullCandleStick, Low,High));
_SECTION_END();

_SECTION_BEGIN("Q_Plotarrow last bars 1");
PositionToPlot1 = BarIndex() == BarCount -21;
BullCandleStick1 =C > 0 AND PositionToPlot1;
BearCandleStick1 =C < 0 AND PositionToPlot1;
Shape1 = BullCandleStick1 * shapeUpArrow + BearCandleStick1 * shapeDownArrow;
PlotShapes(Shape1, IIf( BullCandleStick1, colorYellow, colorPink),0, IIf(BullCandleStick1, Low,High));
_SECTION_END();

_SECTION_BEGIN("Q_Plotarrow last bars 2");
PositionToPlot2 = BarIndex() == BarCount -61;
BullCandleStick2 =C > 0 AND PositionToPlot2;
BearCandleStick2 =C < 0 AND PositionToPlot2;
Shape2 = BullCandleStick2 * shapeUpArrow + BearCandleStick2 * shapeDownArrow;
PlotShapes(Shape2, IIf( BullCandleStick2, colorPaleBlue, colorTan),0, IIf(BullCandleStick2, Low,High));
_SECTION_END();

_SECTION_BEGIN("Q_Plotarrow last bars 3");
PositionToPlot3 = BarIndex() == BarCount -121;
BullCandleStick3 =C > 0 AND PositionToPlot3;
BearCandleStick3 =C < 0 AND PositionToPlot3;
Shape3 = BullCandleStick3 * shapeUpArrow + BearCandleStick3* shapeDownArrow;
PlotShapes(Shape3, IIf( BullCandleStick3, colorBrown, colorWhite),0, IIf(BullCandleStick3, Low,High));
_SECTION_END();

In your code, there is a lot of repetition (it is very easy to forget to change a numeric suffix after some variable names).

It is better to define a single piece of code, included in _SECTION_BEGIN() and SECTION_END() markers to be used as a "drag & drop" indicator, changing the days to look back (and shape colors) with user-definable parameters.

(Moreover, in your code the markers are unbalanced: each _SECTION_BEGIN should have a corresponding _SECTION_END; this is not a fatal error, but it is to avoid anyway).

Another suggestion is always to verify why your code does not work as expected using all the debugging tools that AmiBroker offers. In particular, I'm fond of using explorations to inspect the values of any logic condition that uses the standard arrays.

For instance, what are the resulting arrays from the following 2 lines:

BullCandleStick  = C > 0;  //  AND PositionToPlot;
BearCandleStick  = C < 0; //  AND PositionToPlot;

As far as I know, closing price will NEVER be < 0.... In the same manner C > 0 will probably result True 100% (if a stock drops near to zero it will be delisted).

So you need to properly understand what your code does, and explorations will clearly show you that probably your code is wrong.

Maybe you should use something along:

BullCandleStick = C > Ref( C, -1 ) AND PositionToPlot;
BearCandleStick = C < Ref( C, -1 ) AND PositionToPlot;

The AFL Function Reference - Categorized list of functions is another very useful page to use to learn AFL.

There are still other issues to fix in your code, but it would be nice if you tried it yourself now.

4 Likes

Hi, Beppe

Thank you for your useful information. I understood that there are lots of replicates in afl.
If there is more than one parameter in a single afl, how to write?
Another example like William % R , Is it feasible that WR (14) or WR (21) present in single afl?

@roychan you should study with attention "How to use drag-and-drop charting interface"

When you create a piece of code to be used as an indicator, you can drag&drop it multiple time to a chart; for this reason, it is appropriate to use user-defined parameters. Each "section" can then have independent values when used in chart.

To see how it works, try to use a standard indicator like the MA (Simple Moving Average).

Overaly it 2/3 times to a price chart.

You can change each MA "Period" (colors, etc.) using the "Formula Properties" dialog (activated from the popup menu activated with a right mouse button click on the chart - selecting the "Parameters..." item).
Then use the "Edit formula" menu item (same popup menu).

The resulting formula will have multiple "MA" (progressively numbered) sections.

Note that only the sections "names" are actually changed.

The variables names do not have any extra suffixes like I did in the "BAD" example above.

Since in the MA default formula there are multiple "Param" functions, the user can customize each indicator independently.

So, you too should try to reduce your code to very few lines and make your "indicator" customizable with something similar to:

Period = Param( "Last bars to use", 10, 10, 120, 10 );
PositionToPlot = BarIndex() == BarCount - Period;

@roychan, another small hint re the above lines of code:

Period = Param( "Last bars to use", 10, 10, 120, 10 );
PositionToPlot = BarIndex() == BarCount - Period;

The second line, in certain instances, could trigger an error: "Subscript out of range".
Search the forum to learn more!