How to count calendar days in AB? Is it possible to replicate this trendline feature?


This chart from investing.com shows no of bars, and calender days between the 2 bars. In amibroker Trendline we get only no. of bars. Can we get Calender days also?

Yes , it's possible and you can see the result in interpretation window

Plot(C,"",colorDefault,styleBar);

Mx = Month();
Yx = Year();


M1 = 31;		M2 = IIf(frac(Year()/4) == 0,28,29);
M3 = 31;		M4 = 30;		M5 = 31;		M6 = 30;		M7 = 31;		
M8 = 31;		M9 = 30;		M10 = 31;		M11 = 30;		M12 = 31;

X1 = BeginValue(VarGet("M"+BeginValue(Mx)) - Day());

Bv = BeginValue(Mx+12*Yx+1);
Ev = EndValue(Mx+12*Yx-1);

OO = EV - BV + 1 ;

X2 = 0;

if(OO > 0)
{
	for ( i = 1 ; i <= OO  ; i++ )
	{
		if ( frac((i+BeginValue(Mx))/12) != 0 ) { z = 12*frac((i+BeginValue(Mx))/12);} else { z = 12; }
	    X2[i] = LastValue((VarGet("M"+z))); 
	}
}


X3 = EndValue(Day());
VD = LastValue(X1+Cum(X2)+X3);

printf("Number of Calender Days" + EncodeColor(colorGreen) + " \nFrom       %s\n"+ EncodeColor(colorRed) +"To\t%s\n" + EncodeColor(colorBlack) + "= %g",DateTimeToStr(BeginValue(DateTime())),DateTimeToStr(EndValue(DateTime())),VD);

2 Likes

Thanks Sebastian sir,

IS Show When Chart 1 date and Last date days in Interpations.
not show trend line start and end days.

sorry but i did not see any trendline on the chart you have attached above .. so which trend line you are talking about ?

also i should point to some mistakes on my codes
1 - M2 should equal to 28
2- as a consequence you should

// Add this line to the code 
LP = exrem(frac(year()/4) == 0,frac(year()/4) != 0)
LP = Leap_Year = cum(LP);

// And modifying these lines respectively
z     = 12*frac((i+BeginValue(Mx))/12);
X2[i] = LastValue((VarGet("M"+z))); 
VD    = LastValue(X1+Cum(X2)+X3);

// to become
z     = round(12*frac((i+BeginValue(Mx))/12)); // to overcome floating number issues
X2[i] = VarGet("M"+z));                       // m2 become scalar variable now
VD    = LastValue(X1+Cum(X2)+X3+LP);         // adding days of LP 
1 Like

@Sebastian thanks for helping @vipul :+1:

I would like to mention, that there is also an article in AB Knowledge base in which Tomasz is showing how calendar days can be counted in Amibroker: http://www.amibroker.com/kb/2007/03/15/calendar-day-index/

For instance this code counts calendar days between BeginValue() and EndValue() (range markers):

function TotalDays()
{
 d = DaysSince1900();

 return d - d[ 0 ];
}

td = TotalDays();
Title = "Number of days in selected range = " + ( EndValue( td ) - BeginValue( td ) );
Plot( C, "", colorDefault, styleCandle );

It uses DaysSince1900() function
https://www.amibroker.com/guide/afl/dayssince1900.html

Moderator comment: To avoid using obsolete code, I removed code from obsolete KB article and replaced with DaysSince1900 function.

3 Likes

FWIW,

That custom function is not required because there is inbuilt array function DaysSince1900() since AB 5.20, but article is of 2007.

Anyway use this instead

td = DaysSince1900();
Title = "Number of days in selected range = " + ( EndValue( td ) - BeginValue( td ) );
Plot( C, "", colorDefault, styleCandle );

Compare https://www.timeanddate.com/date/duration.html

7 Likes

Thanks @fxshrat. This is the best (and the simplest) solution.

1 Like

FWIW I have to delete/update KB article as it is no longer relevant.

Update is now done.

3 Likes

Thanks @Milosz ,@Fxshrat and @Tomasz
i am gonna to read this valuable article and read the instructions relating to daysince1900() function , because i am not familiar with it .. thank you all :hibiscus::hibiscus::hibiscus:

@VIPUL
Now ,you have a perfect and complete answer for your question

2 Likes

NIFTY%20DATE%20RANGE
In This Picture We See How Many Bars Are There.
Can We Get CALENDAR DAYS With This And Can We See Every Time When We Draw It.
After Few Seconds This Study Gone.
Can We See All Time When We Study Some Detail With Time And Price Co Relations?

You have a custom need and it wouldn't make sense turning it into a generic feature.

A simple way is to use GFX functions and plot text in whichever way you want.
Also, it will not timeout like a mouse hover, you can have permanent display.

Just write a code using DaysSince1900 http://www.amibroker.com/f?dayssince1900. It isn't difficult. You need to use Study function http://www.amibroker.com/f?study to get hand-drawn study, find out it where it begins (changes from Null to non-Null) and where it ends (changes from non-Null to Null) and use ValueWhen http://www.amibroker.com/f?valuewhen on this points with DaysSince1900 as input.

Almost ready-to-use code is in the Knowledge Base: http://www.amibroker.com/kb/2006/03/07/getting-x-y-co-ordinates-of-study/ the only difference is replacing DateTime with DaysSince1900.

3 Likes