TimeFrame functions use might need SetBarsRequired, as per KB on QuickAFL

Hello,

I am trying to calculate moving average of Volume in Daily timeframe when my base timeframe is one minute. I wrote below code. When I am trying to display Volume value it is not showing correct value in Printf statement whereas it is working fine in exploration. Please give your comments on this, thanks in advance.

Filter=1;

TimeFrameSet(inDaily);
ChkVol = IIf(V>3*MA(V,200),1,0);
TimeFrameRestore();

ChkVol = TimeFrameExpand(ChkVol,inDaily,expandFirst);

printf("ChkVol = %0.f\n",ChkVol[BarCount-1]);

AddColumn(ChkVol,"Check Vol");

Your printf statement is always using the ChkVol value from the last bar. I assume that's not what you intended, because it's not what your Exploration is doing. As per the documentation, when you specify an entire array, printf will use the array value from the currently selected bar. Try this instead:

printf("ChkVol = %0.f\n",ChkVol);

Thanks for your reply.
Still issue persist.

Thanks
Shiva

No one can help you if you don't provide details. Please review: How to ask a good question

I tried printf with both options (i.e. Array and Array element), below is the code.

Filter=1;

TimeFrameSet(inDaily);
ChkVol = IIf(V>3*MA(V,200),1,0);
TimeFrameRestore();

ChkVol = TimeFrameExpand(ChkVol,inDaily,expandFirst);

printf("Printf from array ChkVol = %0.f\n",ChkVol);
printf("Printf from array element ChkVol = %0.f\n",ChkVol[BarCount-1]);

AddColumn(ChkVol,"Check Vol");

Below is the output of printf and exploration which shows printf is not giving correct details whereas exploration showing correct output.

Thanks
Shiva

You are looking at DIFFERENT BARS as @mradtke told you.

Help yourself and learn basics first:

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

There is NO SINGLE VALUE, but array, you must know what bar you are looking at. Currently you don't know because you don't display timestamps or bar number. Your screenshots are also truncated and date/time column does not show the time because you did not resize it to show the content. You are wasting people time.
Do your homework. Send a screenshot that actually SHOWS, not hides things.

Guessing from this truncated "thing" that you sent you are looking at the FIRST result in the Analysis test but at the LAST BAR in the interpretation.

Do yourself a favour and display the timestamps of what you are looking at.

Thanks Tomasz for your inputs.
Before posting my query I have gone through How to do debug.

Sorry, earlier I forgot to mention that I used bar replay to get the output of printf which is same as exploration. See below screen shot. Issue is in printf it is showing NULL value whereas in exploration it is showing as 1.

Thanks
Shiva

If you use Bar Reply, DATA STOP at the "playback position" and it affects all places including Charts and Analysis.
You can easily fool yourself if you turn on everything. Do one thing at a time.

As I wrote you are comparing apples to oranges. You are looking at different bars. Re-read what was written here before. You need to OUTPUT TIMESTAMP to know what you are looking at.

https://www.amibroker.com/f?datetime
https://www.amibroker.com/f?datetimetostr

bar = ...whavever bar...
dt = DateTime();
// print TIMESTAMP and data, not just data
// because you don't know what you are looking at
printf("%s %g", DateTimeToStr( dt[ i ] ), array[ i ] );

and use DEBUGGER https://www.amibroker.com/guide/h_debug.html

Thanks Tomasz for your inputs.
Now I changed my code to add date and time (Copied below).

Filter=1;

dt = DateTime();

TimeFrameSet(inDaily);
ChkVol = IIf(V>3*MA(V,200),1,0);
TimeFrameRestore();

ChkVol = TimeFrameExpand(ChkVol,inDaily,expandFirst);
printf("Printf from array element ChkVol  = %0.f when date and time = %s\n",ChkVol[BarCount-1],DateTimeToStr( dt[ BarCount-1 ] ));
AddColumn(ChkVol,"Check Vol");

After that still issue persist (i.e. Printf showing wrong value and exploration showing correct). I used debug to see the results.

Exploration Screen shot:

Debug screen shot:

Thanks
Shiva

@DSK,

Only users with "Verified Badge" are allowed to post on this forum.

Search "Verified Badge" for more information on how to get verified.

@TrendSurfer
Just now I verified my licenses.

Thanks
Shiva

1 Like

You might need to add

SetBarsRequired( sbrAll );

at the top of the formula since you are referencing 200 days of daily from minute chart and that is as much as (60 * 24 * 200) = 288000 minute bars required to calculate just first bar of that moving average in daily interval.

Generally speaking a NULL value means "having no value at all" and you would get Null if not enough data is available to calculate say moving average. Analysis window by default uses all bars always (unless you turn on QuickAFL in Analysis) so it doesn't need SetBarsRequired as long as you don't turn on QuickAFL in analysis. Normally you would not need to use SetBarsRequired as requirements are in most cases automatically derived from the formula. However in certain cases such as looping and using TimeFrame functions those requirements might be underestimated.

See " IMPORTANT NOTES" in the documentation

Thanks a lot Tomasz. My issue is resolved with your inputs.

Shiva

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.