Help with PlotText function

Can someone point me in the right direction of understanding the PlotText function? The example in the formula reference and every example I have found in the formula library uses PlotText in a for/if loop, but the formula reference doesn't explain why and I am too stupid to figure it out.

To identify patterns, I want to use PlotText like PlotShapes, ie, if ShortPatternA I want to plot "A" in red above the high, and if LongPatternA I want to plot "A" in green below the Low and so on for pattern B, C,etc. No need for a loop in my non-programmer mind. I have read the common coding mistakes, tried WriteIf, etc. but I cannot get the syntax right. I'm stuck.

TIA,
James

Just look at AFL reference guide and examples there.
https://www.amibroker.com/guide/afl/plottext.html
https://www.amibroker.com/guide/afl/plottextsetfont.html

x and y (2nd and 3rd arguments of PlotText() function and 4th and 5th argument of PlotTextSetFont() function) have to be numbers but not array. There isn't array of strings. You have to iterate through array to place a variable or constant string at one or multiple x-y coordinates (->elements) of x-y arrays. If you do not iterate then you plot at just a single x-y position.

What are elements of array -> Understanding how AFL works.

28

BTW the same applies to GfxTextOut and GfxDrawText

4 Likes

Thanks fxshrat, I still don't understand this but I am going to read more tonight. In your reply you state the arguments have to be numbers but not an array and then in red you state x and y have to be numbers or elements of an array. This is confusing to me.

  1. Why do you not have to iterate in PlotShapes?
  2. Why can't the y-coordinate be High * 1.1 or Low * .99?
  3. Likewise, why couldn't the x-coordinate be the BarCount when the condition was true?

That way you have numbers in arrays that could be plugged into x and y.

Oh and one more question. I read in one of your replies in the forum you do not want to iterate from the very beginning, just the visual bars. If you start scrolling the chart backwards in time, will the formula automatically recalculate for the new visible bars? I know it's frustrating to try to explain these concepts to newbies, but I really appreciate it.

Please read carefully... An element of an array of numbers is a number. An array of numbers is a list of numbers (which are elements of array). Of course a list may contain just a single number but in such case the type of such list is still array. And to access elements of array there are several ways (again read "Understanding how AFL works" as well as "subscripting" below).

  1. Plotshapes applies to entire array at once. Plotshapes does not plot text. There isn't array of strings. If using plot text functions you have to place single string at single x-y coordinate. And to place multiple strings at multiple x-y coordinates of array you have to iterate through array.

  2. You can use high and low but you have to use subscripting (or functions such as LastValue) since you have to access elements of High/Low/... array if using PlotText() function's y-argument. Look at the links to documentation and examples in my upper post.

  3. It can. Look at PlottextSetFont example of my first post's 2nd link! (Same applies to the other three functions named in my upper post.) It is all there.

Buy = BarIndex() == LastValue(BarIndex());// just an example!

if (LastValue(Buy))
    PlotText( "BuySig @last bar",  BarCount, Close[ BarCount - 1 ], colorRed, colorDefault, -20 );

Besides I have written above already that if you do not iterate then there is just single PlotText call and text will be plotted at just a single x-y position. Please read carefully I will not repeat again.


Yes, while scrolling visible area will be recalculated automatically.

PS: I typed via Smartphone and have not checked for typos.

2 Likes

Hello James, Iwonder

Simple way to understand difference between functions is

  1. PlotText expects a scalar input
  2. PlotShapes expects a vector input

Therefore 99% of time, PlotText will be inside a loop and PlotShapes will be outside.
But, it does not mean that PlotText cannot be used outside the loop

Here is successful example of AFL code that will plot text on screen
indicating where the stop loss is along with its value. As user keeps scrolling left and write, newer and more relevant stop loss values will be plotted

        LOMD = Flip(Buy, Short OR Sell); SOMD = Flip(Short, Buy or Cover); 
	abc = IIf(Sell OR LOMD, HHV(Low, 10), IIf(Cover or SOMD, LLV(High, 10), Null));
	if (NOT(IsNull(SelectedValue(abc))))
                PlotText("STOP LOSS\n" + NumToStr(SelectedValue(abc), 1.2, 0), SelectedValue(BarIndex()), SelectedValue(abc));