# Amibroker: Barindex to Date conversion?

#### gito

##### New Member
Hi guys,

I have searched a lot but could not find this simple function:

"To convert Barindex to Date"

All I need is to call a function with Barindex and get date in return.

Anyone ?

Thanks for any Help.

#### LOVEENAJYOTHI

##### Well-Known Member
Try this
Code:
``````Bi=BarIndex();
DtN=DateNum();

function DateNumToStr(DtNum)
{
DayNm = round(frac(DtNum/100)*100);
MthNm = round(frac(DtNum/10000)*100);
YrNm = int(DtNum/10000)+1900;
return NumToStr(DayNm,1.0)+"/"+NumToStr(MthNm,1.0)+"/"+NumToStr(YrNm,1.0,False);
}

BiNum=Param("BiNum",1,1,1000000,1);//Select ur Bar Index number in parameter
BiDtNum=ValueWhen(Bi==BiNum,DtN);
BiDt=DateNumTostr(BiDtNum);

Title="BiDt: "+ "     "+BiDt; //will display Date in  DD/MM/YYYY  format on ur Chart Title for the Bar Index Number u have selected in parameter above``````
U can try Selectedvalue() instead of Param() with appropriate changes if that is ur intent.

Last edited:

#### gito

##### New Member
:thumb: Perfect! Thanks a lot. Here is how I modified it for my purpose:

function DateNumToStr(DtNum)
{
DayNm = round(frac(DtNum/100)*100);
MthNm = round(frac(DtNum/10000)*100);
YrNm = int(DtNum/10000)+1900;
return NumToStr(DayNm,1.0)+"/"+NumToStr(MthNm,1.0)+"/"+NumToStr(YrNm,1.0,False);
}

function BarIndexToDate(nBarindex)
{

Bi=BarIndex();
DtN=DateNum();
BiNum= nBarindex ;
BiDtNum=ValueWhen(Bi==BiNum,DtN);
BiDt=DateNumTostr(BiDtNum);

return BiDt;
}

Regards.

#### boufalo

##### Member
hello

Code:
``````[COLOR="Red"]function[/COLOR] BarIndexToDate(nBarindex)  [COLOR="Blue"]//Version 2[/COLOR]
{
Bi=BarIndex();
DtN=DateNum();
BiNum= nBarindex ;
BiDtNum=ValueWhen(Bi==BiNum,DtN);
BiDt=WriteIf( BiDtNum >= 1000000, "20", "19" ) + StrFormat("%06.0f", BiDtNum % 1000000 );

return BiDt;
}

printf( "\nBarIndexToDate= "+ BarIndexToDate(SelectedValue(BarIndex() ))   );

printf( "\n20th Bar= "+ BarIndexToDate(bar=20)   );``````
and this version3 is with diferent format.

Code:
``````[B][COLOR="Red"]function [/COLOR][/B]BarIndexToDate[B]3[/B](nBarindex)  [COLOR="Blue"]//Version 3[/COLOR]
{
Bi=BarIndex();
return  WriteIf(Bi==nBarindex,DateTimeToStr(nBarindex),"wrong bar");
}

printf( "\nBarIndexToDate3 = "+ BarIndexToDate3(SelectedValue(BarIndex())));
printf( "\n20th Bar= "+ BarIndexToDate3(bar=20)  +"\n" );``````

Last edited:

#### gito

##### New Member
hello

Code:
``````[COLOR="Red"]function[/COLOR] BarIndexToDate(nBarindex)  [COLOR="Blue"]//Version 2[/COLOR]
{
Bi=BarIndex();
DtN=DateNum();
BiNum= nBarindex ;
BiDtNum=ValueWhen(Bi==BiNum,DtN);
BiDt=WriteIf( BiDtNum >= 1000000, "20", "19" ) + StrFormat("%06.0f", BiDtNum % 1000000 );

return BiDt;
}

printf( "\nBarIndexToDate= "+ BarIndexToDate(SelectedValue(BarIndex() ))   );

printf( "\n20th Bar= "+ BarIndexToDate(bar=20)   );``````
and this version3 is with diferent format.

Code:
``````[B][COLOR="Red"]function [/COLOR][/B]BarIndexToDate[B]3[/B](nBarindex)  [COLOR="Blue"]//Version 3[/COLOR]
{
Bi=BarIndex();
return  WriteIf(Bi==nBarindex,DateTimeToStr(nBarindex),"wrong bar");
}

printf( "\nBarIndexToDate3 = "+ BarIndexToDate3(SelectedValue(BarIndex())));
printf( "\n20th Bar= "+ BarIndexToDate3(bar=20)  +"\n" );``````

Yes, this might work well too, but I prefer my approach because:
1. Simple code: easier to change (at least for me)
2. Separate functions that could be used elsewhere too

#### gito

##### New Member
:thumb: Perfect! Thanks a lot. Here is how I modified it for my purpose:

function DateNumToStr(DtNum)
{
DayNm = round(frac(DtNum/100)*100);
MthNm = round(frac(DtNum/10000)*100);
YrNm = int(DtNum/10000)+1900;
return NumToStr(DayNm,1.0)+"/"+NumToStr(MthNm,1.0)+"/"+NumToStr(YrNm,1.0,False);
}

function BarIndexToDate(nBarindex)
{

Bi=BarIndex();
DtN=DateNum();
BiNum= nBarindex ;
BiDtNum=ValueWhen(Bi==BiNum,DtN);
BiDt=DateNumTostr(BiDtNum);

return BiDt;
}

Regards.

ok, now there is something wrong with this which I can't understand ?

1. Before when I tested it was showing the date, but what I didn't noticed was that it was showing the same date regardless!
2. Now when I tested the DateNumber /BiDtNum , for some reason it is always returning zero ?

so even I pass BarIndex+10 , it still returns zero. So it seems there is something wrong with the function where it returns datenum and I'm confused as to why? I hope you are not as confused as me!

#### jayesh123

##### New Member
Dear Members,

I need a afl which can calculate the below formula.

Record High Percent = {New 52 week Highs / (New 52 week high Highs + New 52 week Lows)} x 100

High-Low Index = 10-day SMA of Record High Percent

Which can plot a line in amibroker

#### LOVEENAJYOTHI

##### Well-Known Member

ok, now there is something wrong with this which I can't understand ?

1. Before when I tested it was showing the date, but what I didn't noticed was that it was showing the same date regardless!
2. Now when I tested the DateNumber /BiDtNum , for some reason it is always returning zero ?

so even I pass BarIndex+10 , it still returns zero. So it seems there is something wrong with the function where it returns datenum and I'm confused as to why? I hope you are not as confused as me!
No Confusion as to the code originally posted by me.
But I do have confusion about ur actual requirement,so reply to the two queries below and then lets proceed together.

1.You need every bar clicked on chart to return corresponding date for the Bar (BarIndex) clicked ?
for example say u click Bi[107] and u expect to return/display the Date of that particular Bar 107 ?
(This makes sense in case of a huge DataBase of Higher/Daily TimeFrame and this can be achieved thru SELECTEDVALUE() as told u already)

2.What is the base TimeFrame of ur DataBase and the TimeFrame of ur Chart/Bars ?

#### gito

##### New Member
1. I don't necessarily need to click the bar, what i need is that i pass any BarIndex and get the corresponding date. (which the code seems to do, but couldn't understand why its not)

2. Database is daily, my chart is also daily.

Thx.

##### New Member
PLZ HELP TO ADD/AMEND THE BELOW CODE (this gives successfully the last five trades in the title section) TO SHOW THE CORRESPONDING TIME TO THE LAST 5 TRADES WHEN THEY ACTUALLY HAPPENED.PLZ REPLY TO: [email protected]
//////////////////////////////////////////////////
// Calculate the Last Five Trades Profit/Losses //
//////////////////////////////////////////////////
Signum = Cum( Buy ) + Cum( Short );
Signumstart1 = LastValue( SigNum ) - ( tradesback - 1 );
Signumstart2 = LastValue( SigNum ) - ( tradesback - 2 );
Signumstart3 = LastValue( SigNum ) - ( tradesback - 3 );
Signumstart4 = LastValue( SigNum ) - ( tradesback - 4 );
Signumstart5 = LastValue( SigNum ) - ( tradesback - 5 );

bi = BarIndex();
bistart = ValueWhen( signum == signumstart1, bi );
bicond = bi >= bistart AND bi <= LastValue( bi );

SellPL = IIf( Sell AND bicond, C-Buyprice, 0 );
CovPL = IIf( Cover AND bicond, ShortPrice - C,0 );

cumPL = SellPL + CovPL;

lsince = lowestSince(Sell OR Cover, cumPL, 0);
hsince = highestSince(Sell OR cover, CumPL, 0);

vs= IIf(lsince==0,hsince,lsince);

PL1 = ValueWhen( signum == signumstart1 , vs,1 );
PL2 = ValueWhen( signum == signumstart2 , vs,1 );
PL3 = ValueWhen( signum == signumstart3 , vs,1 );
PL4 = ValueWhen( signum == signumstart4 , vs,1 );
PL5 = ValueWhen( signum == signumstart5, vs,1 );
Title = "\n\n\nLast 5 Trade Results:"
+"\nTrade 5= " + PL1+" at"
+"\n"+ "Trade 4= " + PL2
+"\n"+ "Trade 3= " + PL3
+"\n"+ "Trade 2= " + PL4
+"\n"+ "Trade 1= " + PL5+
///////////////////////////////////////////////////////