Get past yearly gains

Is there a convenient AFL function to get the past yearly gain of a security?

I would like to get the yearly gain of last 2 years using AFL. The yearly gain is defined as price gain on last date of year in December compared to first date of year in January. One way to get this is to view the chart in yearly mode. However, I want the yearly returns to be obtained in AFL.

That is incorrect.
That would not be yearly gain, but SINGLE BAR gain between Dec 31 and January 2nd bars, something like

NewYear = Year() != Ref( Year(), -1 );
SingleBarGain = C - Ref( C, -1 );
JanDecGain = ValueWhen( NewYear, SingleBarGain );

but again this is NOT called yearly gain.

Yearly gain is a gain from a period equal to one year (i.e. 365 calendar years or 252 trading bars). The simplest way is to use 252 bar approximation:

YearlyGainPercent = ROC( C, 252 );
YearyGainPoint = C - Ref( C, -252 );

For other flavour of yearly gain (that is kept constant for entire year) you can use ValueWhen

NewYear = Year() != Ref( Year(), -1 );
Constant1YearGain = ValueWhen( NewYear, Close, 1 ) - ValueWhen( NewYear, Close, 2 );

Or for yet another flavor you can use TimeFrameGetPrice()

// Example 2. get year to date gain
YearToDate = Close - TimeFrameGetPrice( "C", inYearly, -1 ); 
5 Likes

I decided to use this approach because the code looks simplest.

However, there is a problem when retrieving price more than 1 year ago. Here is my code;

x = TimeFrameGetPrice( "C", inYearly, -1 );
v = TimeFrameGetPrice( "C", inYearly, -2 ); //this line causes problem. It returns null in array.
gain = ((x-v)/v)*100;

I tested on other securities. Same problem. I have no problem when the argument in TimeFrameGetPrice function is inWeekly. This problem seems only to occur when the argument is inYearly. When I turn on debugging and look at the array, it seems the maximum array size is up to 200. Could this be the cause? To retrieve price more than 1 year ago, the array size needs to be more than 200. The security has at least 5 years worth of price data.

1 Like

Choosing something for sole reason that it “looks simplest” is bad idea.
These formulas DIFFER from one another. Year-to-date gain is not the same as yearly gain. Bike, motorcycle and car are different vehicles and you don’t always choose bike, only because it “looks simplest”. You choose a car when you want to move fast and safe. Same with formulas, the right one for given purpose should be used.

What you picked is YTD gain: https://en.wikipedia.org/wiki/Year-to-date

and by the way this

YearyGainPoint = C - Ref( C, -252 );

is simpler, but it is not YTD, but actual yearly gain. Hint: RE-READ my original reply.

To your question: use Knowledge Base. All answers are already there. Your current question is also answered in the KB:

http://www.amibroker.com/kb/2014/09/22/do-not-make-assumptions-on-number-of-bars/
and
http://www.amibroker.com/kb/2008/07/03/quickafl/

Sorry for false alarm. I just discovered my code actually works. The problem only happens in debugging mode because array size is limited to 200.