How close was it, exactly 3 months ago?

I want to create an indicator that represents the value of Close, 3 months ago.
Not 60/70 days ago, but exactly 3 months ago
If today is March 31, I will have to publish the close value at 31 December

I create an indicator that represents in DateNumber format, the Date of 3 months ago,

Fn_Year  = IIf( Month() >= 4, Year(), Year() - 1);
Fn_Month = IIf( Month() >= 4, Month() - 3, (Month() - 3) + 12);
DN_3M_Ago = (Fn_Year-1900)*10000 + Fn_Month*100 + Day();

Plot(DN_3M_Ago, "Fn_Year", colorDefault, styleLine);

I now do not know how to compile the array with the Close, which reads in that precise DateNumber the Close value.
I tried with ValueWhen (with ValueWhen, if I position the cursor in the chart, after making the plot, the past value changes. I probably do not use it, recall, how it would be used.)

and also with LookUp but I did not succeed.

The problem is to calculate the market days between today's date and the synthesized date of 3 months ago.

I tried to use the user function found in this link
http://www.amibroker.org/userkb/index.php?s=daycount
but it does not work because I'm using it because it gives me a mistake :sleepy:
Anyone kindly can give me some help, guidance on how to overcome this seemingly banal problem, but complicated for me?

thanks you very much

Isn’t there a flaw in your logic? What value will you get when the month ends on a Saturday or Sunday (or market closed holiday)?

There will be no “Close” that day. So how do you calculate this odd indicator on approximately 2/7 month ends that have no Close?

As @portfoliobuilder mentioned, pretty often exactly 3 months back means Sunday or Saturday.
But if you wanted just the close at the end of each month, 3 months back, you can get it quite easily

TimeFrameGetPrice( "C", inMonthly, -3 );

But I guess that you are not interested in just monthly close.

Getting close 3 months back on daily (bar by bar) basis is tricky because each bar you need to lookup back different number of bars and if exact match is missing you need to get predecessor. This is quite a bit of work. One of possible solutions look like this:

dt = DateTime();

// get date that is exactly 3 months back
dt3m = DateTimeAdd( dt, -3, inMonthly );

for( i = 0; i < BarCount; i++ )
{
  cl3m[ i ] = Lookup( Close, dt3m[ i ], -1 );
}

Plot( Cl3m, "Close 3 months back", colorRed );

Title = "Now = " + DateTimeToStr( SelectedValue( dt ) ) +
        " 3 months back = " +  DateTimeToStr( SelectedValue( dt3m ) ) + 
        " Close 3 months back " + cl3m;
9 Likes

Thank you so much!
It’s amazing how @Tomasz, with only one line created the array with the back date of 3 months
It’s even more amazing as with the loop that invokes Lookup has solved

After a year working in AFL I feel very small, but I will grow
Thank you !!

Let it grow, let it grow,
Let it blossom, let it flow
In the sun, the rain, the snow,
Love is lovely, let it grow

1 Like