Amibroker - use calendar days in formula - Aussie Stock Forums

Thread: Amibroker - use calendar days in formula

1. Amibroker - use calendar days in formula

Hi,

Can some Amibroker expert help me out. I'm trying to get percentage difference between the close price today and close price as of 30 CALENDAR days ago. This formular will give me the difference 30 TRADING days ago. How can i change it so it give me the calendar day's price? Thanks you

growth = Close / Ref(Close, -30) <= 1.1;

2. Re: Amibroker - use calendar days in formula

There may be an easier way, but you can use the DateNum() function to get the actual dates of each bar, manually figure out what date 30 calendar days ago was (a bit of messing around with number of days in a month), and then search back through the array to find the nearest bar to that date (remembering that the actual date may not be there as it may have been a weekend or the stock may not have traded that day).

This will quite likely require the use of loops to do the date search.

GP

3. Re: Amibroker - use calendar days in formula

Code:
```SetBarsRequired(1000000,0);

function TotalDays()
{
yy = Year();
dy = DayOfYear();

leapyear = ( yy % 4 ) == 0 AND yy != 2000;

yearlen = IIf( leapyear, 366, 365 );

return yearlen * (yy - yy[ 0 ]) + (dy - dy[0]);
}

function RefDays( Array, Days )
{
td = TotalDays();

result = Null;

if( Days < 0 )
{
for( i = BarCount -1; i >= -Days; i = i - 1 )
{
for( j = 0; j < i; j++ )
{
if( td[ i - j ] <= td[ i ] + Days )
{
result[ i ] = Array[ i - j ];
j = i;
}
}
}
}
return result;
}

Plot( C, "C", colorRed );
Plot( RefDays( C, -30 ), "Close 30 days back", colorGreen );```

4. Re: Amibroker - use calendar days in formula

I think there are a couple of problems with your total days function there, although for only 30 days back, neither would be very noticeable.

Firstly, you multiply the total number of years since the first by the number of days in a year, which is determined by whether the current year is a leap year or not. So on leap years, all years are multiplied by 366 instead of just the leap years.

Secondly, you add dy-dy[0] for the days in the most recent year. But dy[0] is just the first day value in the array, and could be anything. So if dy[0] is 300, when you get back to Jan 1st, you'll have -299. However, since the final result is taking differences between dates, the dy[0] component will be cancelled out anyway. Still, if you want total days to be accurate, then you should probably use dy-1.

Cheers,
GP

5. Re: Amibroker - use calendar days in formula

GP,

Maybe you might like to point that out to Thomasz!

http://www.amibroker.com/kb/2007/03/...index/#more-59

6. Re: Amibroker - use calendar days in formula

Doesn't matter who wrote it, it still seems wrong to me. On closer inspection though, dy[0] is not a problem as I described it, but the leap year issue is and there's a slight problem when rolling from one year to the next in relation to leaps years as well.

And I might add that it's good form to acknowledge the source when you post someone else's work.

GP

7. Re: Amibroker - use calendar days in formula

After a bit of thought, here's a version of the TotalDays function that handles leap years correctly.

Code:
```function TotalDays()
{
yy = Year();
dy = DayOfYear();

LastLeapYear = (yy % 4) == 1 && yy != 2001;
YearChg = yy != Ref(yy, -1);
YearChg = IIf(IsNull(YearChg), False, YearChg);
YearLen = IIf(YearChg, IIf(LastLeapYear, 366, 365), 0);

return Cum(YearLen) + dy - dy[0];
}```
The main difference here is that the number of days in each year is summed, rather than multiplying the total years by a single value. However, to avoid having the same value added 365 or 366 times, the YearChg array is used to generate a True signal on only the first day of each year. Since that array will start with a null value, the next line converts the null value back to False.

Finally, the LastLeapYear variable indicates if the previous year was a leap year, rather than the current year. This is required since you want to add the number of days in the previous year when moving to a new year, not the number of days in the current year.

Anyway I've checked the results by writing every value from around 1/1/1997 to a file and manually checked that the number of days on the 1st January each year is what it should be. The file is attached if anyone wants to verify it themselves.

Cheers,
GP

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Aussie Stock Forums