Study values change unexpectedly

Hi;

For years I've been using the Study function and getting the X,Y values for various calculations. Recently, I created a function that gets the X,Y data of a study but the Study X,Y values change as you scroll the study. As noted, I've been doing this for a while and have other AFL that don't change values as I scroll.

Below is the code of the AFL that changes values as you scroll and attached are two png's showing the problem. What am I doing wrong that is causing the Study X,Y values to change? Thanks!!

_SECTION_BEGIN("Test");
Plot( C, "Close", colorBlueGrey,styleBar|styleThick|styleNoLabel|styleNoTitle);

DW1 = Study("D1",GetChartID());
DW1X1 = LastValue(ValueWhen(ExRem(DW1,0),BarIndex()));
DW1X2 = LastValue(ValueWhen(DW1,BarIndex()));
DW1Y1 = LastValue(ValueWhen(ExRem(DW1,0),DW1));
DW1Y2 = LastValue(ValueWhen(DW1,DW1));
DW1Close = C[DW1X1];

printf("X1/Y1/Close: "+NumToStr(DW1X1)+","+NumToStr(DW1Y1)+","+NumToStr(DW1Close)+"\n");

_SECTION_END();

Study%201|690x391 study%202

There is nothing wrong with the code, it's a replica of Getting X, Y co-ordinates of Study(). Since you are saying:

Could you close that chart window entirely where this is happening, and Open a New blank chart > Put that code in a New Formula > Insert it to the blank chart. And see whether the issue persist or not.

Which "value" changes? Are you referring to the change of BarIndex() as you scroll? Change of BarIndex() is normal.

1 Like

This ("Why Barindex changes...") has been asked several times already.


It is because of QuickAFL.
QuickAFL has been there already for many many years already.
So it has not changed.

If you want to print index of entire array then use Status function with status code quickaflfirstdatabar as seen in second link.

_SECTION_BEGIN("Test");
Plot( C, "Close", colorBlueGrey,styleBar|styleThick|styleNoLabel|styleNoTitle);

bi = BarIndex();

DW1 = Study("D1",GetChartID());
DW1X1 = LastValue(ValueWhen(ExRem(DW1,0),BarIndex()));
DW1X2 = LastValue(ValueWhen(DW1,BarIndex()));
DW1Y1 = LastValue(ValueWhen(ExRem(DW1,0),DW1));
DW1Y2 = LastValue(ValueWhen(DW1,DW1));
DW1Close = C[DW1X1];

qfdb = Status("QuickAFLFirstDataBar");
quickAFL_DW1X1 = qfdb + DW1X1;

printf("X1:%g, quickAFL_X1:%g, Y1:%g, Close:%g\n", DW1X1, quickAFL_DW1X1, DW1Y1, DW1Close);
_SECTION_END();

Instead of using Barindex for study's initial start/end X values you might rather use datetime and then using lookup. That way you have both worlds... datetimes and indexes.

_SECTION_BEGIN("Test");
Plot( C, "Close", colorBlueGrey,styleBar|styleThick|styleNoLabel|styleNoTitle);

bi = BarIndex();
dt = DateTime();

DW1 = Study("D1",GetChartID());
DW1X1_dt = LastValue(ValueWhen(ExRem(DW1,0),dt));
DW1X2_dt = LastValue(ValueWhen(DW1,dt));
DW1Y1 = LastValue(ValueWhen(ExRem(DW1,0),DW1));
DW1Y2 = LastValue(ValueWhen(DW1,DW1));

DW1X1 = LookUp(bi, DW1X1_dt);
DW1X2 = LookUp(bi, DW1X2_dt);
DW1Close = LookUp(C, DW1X1_dt);

qfdb = Status("QuickAFLFirstDataBar");
quickAFL_DW1X1 = qfdb + DW1X1;


printf("X1_dt:%s, X2_dt:%s\n", DateTimeToStr(DW1X1_dt), DateTimeToStr(DW1X2_dt));
printf("X1:%g, quickAFL_X1:%g, Y1:%g, Close:%g\n", DW1X1, quickAFL_DW1X1, DW1Y1, DW1Close);
_SECTION_END();

7


As for inserting code (and others).. must read HERE.

3 Likes

Cougar! Thanks for your comments and you idea worked!

I created a New Chart, with nothing in it and added the suspect AFL code. The X,Y values of the study did not change, as expected. I also went back and created a new tab and New Pane in the existing Chart and the Study X,Y values changed.

So what is the problem? Is it an internal memory problem? The existing chart is rather complex with a lot of AFL lines of code. It appears I may have reached a limit of some kind. Should I just rebuild everything in a New Chart or is there some other way around the problem?

Thank you and please advise.

1 Like

Cougar! I forgot to answer your question on what values change. As you scroll backwards in time, the X value changes, but that always does. What's new is that the Y(start in this case) value changes but only as you scroll back towards the current time, very right side of the graph. It only seems to do this during the last couple of weeks of the chart.

You can see the changes in the PNG files provided; Interpretation Window on left. Study1.PNG is scrolled left and back in time where the study is; X1=90,Y1=48.914 (this is the correct data). Study2.PNG is after having scrolled all the way to the right; X1=0 and Y1=50.895.

What's interesting is that X1 does not go to zero until you scroll right and towards the end. I mentioned I have other AFL with the same Study code and no problem exists. The X1 value does NOT go to zero when you scroll all the way to the right.

Hope this helps!

1 Like

Hi fxshrat

OK, so your direction on QuickAFL fixed the problem. I add SetBarsRequired(1000,0); and the problem stopped. The correct Y value carries through the entire scroll, left and right.

Much appreciated. Thank you!

Note: I've been a long time AmiBroker user and write a fair amount of AFL code. This is the first time I've bumped up against QuickAFL. I remember reading about this years ago but never thought much of it until now.

@DonGordon,

Thank you for your kind words!

Not specific to this Study Values problem, generally speaking, when you directly Insert a formula from Chart Menu onto a Blank chart window, an instance of that formula gets stored in [Your Install Directory]\AmiBroker\Formulas\Drag-drop hidden folder. Logically it makes sense, otherwise, how else would you Overlay or Drag-Drop one formula on another.

Now the issue occurs, when you Edit the formula directly from the Charts. If not careful, you might land-up editing a Formula located in the Drag-drop folder, instead of actually making changes in the AFL located in Formulas\Custom folder or in some other directory, contrary to the presumption.

In this scenario, there remains a possibility that AmiBroker has already saved the previous instance of the AFL (before the edit) in its Default Layout. Now when it loads next time, it would probably load that previous AFL onto the Chart but not the edited one.

Thus, safest practice is to always Edit an AFL from Charts Menu, remove the previous instance(s) of the Formula(s) from the chart(s) and Apply the AFL from the Formula Editor. This way we always ensure that our charts contain the most updated AFL.

If the errors are re-occurring, then please do so. But before anything else, please keep a backup. May I also suggest you to take a look at this post of Tomasz:


I have noticed that often complex things can be easily solved in just few lines. So, it would be worth the time spent for debugging and rectifying code redundancies.

1 Like

Many thanks Cougar for all the help and advice.

As noted in my one reply, the suggestion that this problem was related to QuickAFL proved to be correct and the solution. When I set required bars to 1000, and now 500, as suggested, the problem completely disappeared and it is working perfectly. This is the first time I created a problem related to QuickAFL.

You are correct in that AFL is a great environment and its flexibility can get you into coding trouble. Streamlining code is always best. Normally, once I get a function working the way I want, I go back and rewrite it to simplify it. Sometimes I'll rewrite a function a couple of times.

FYI ... While not the best coder in the world, far from it, I've been coding (professional and personal) for over 30 years (degree in computer science) and I've been coding AFL for 10 years (+/-).

Thanks again for the help. It's great to have such a supportive community available to help solve problems. Truly fantastic !!!

1 Like