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

@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