How to Get Date info of the close price of n-period ago from using ref(C,-5)

I try using Ref function easily as like https://www.amibroker.com/guide/afl/ref.html
https://www.amibroker.com/guide/afl/ref.html

Typically, it returns the array as Array parameter specification like C (close) but I need more information like DateTime of such an array.

Simply put, I would like to know how to get DateTime from Bar without looping as my need.
I am implementing this code in the exploration section as the code below.

Filter=1;

AddColumn(Close,"Close Price",1.2);

AddColumn(Ref(Close,-5),"Close Price 5 days ago",1.2);
//AddColumn(null,"Date of Close Price 5 days ago",1.2); what is function to achieve that???
AddColumn(Ref(Close,5),"Close Price 5 days ahead",1.2);
//AddColumn(Null,"Date Close Price 5 days ahead",1.2); what is function to achieve that???

In the sample below is 1Aug2018 The following figure show desired result.

1

2

Please kindly advise me.
Pongthorn

Go through following guide:
https://www.amibroker.com/guide/afl/datetime.html

You can use DateTime(), it will reference the timestamp of that bar.

DateTime(Ref(Close,5)) ?

It seem that Datetime function is no parameter.

DateTime() is an Array just like O,H,L,C.

you can directly

Ref(DateTime(), -5);  // 5 periods ago

Just correlate the logic of Timeframes, the timestamp of any bar is function of current active TimeFrame, selected either by Periodicity or TimeFrameSet()

The timestamp of O or C or H or L is the same for that bar.

https://www.amibroker.com/guide/w_preferences.html

Intraday tab, Timestamp, whether First or Last Tick of bar needs to set here if required.

3 Likes

I know it's an old topic but I wanted to do the same thing within a loop and couldn't find how to do it when I searched.

I had an if statement in my loop and could assign a variable to the O, C, H and L value for a bar in the loop but couldn't work out how to get the date.

Eventually I worked out I had to calculate the date outside of the loop. I stored the loop index then used it to calculate the periods in the Ref function.

Hope this code helps the next person looking.

for ( i = 0 ; i< BarCount; i++ ) 
  if( Close[i] <= 22 ) // Logic you are searching for 
{
  ibar = i ;
  iOpen = Open[i] ;
  iClose = Close[i] ;
}
 
OffSet = BarIndex()- iBar ; // Loop event triggered Offset bars ago  

iDate = Ref( Datetime(), - Offset ) ; // Date of loop event

Age = DateTimeDiff( DateTime(), iDate  ) / ( 60 * 60 * 24 ) ; // Calendar days from loop event to today 

1 Like

Thank you Clark !
I need a way to find begin date for a 5 period rate of change and you provide me the solution

AddTextColumn(FullName(), "Name");
AddColumn (ROC (C, 4), "4 wk ROC", 1);
AddColumn (Ref (DateTime(), -4), "4wk ROC Date", formatDateTime);