Skip to content

Instantly share code, notes, and snippets.

@marketcalls
Created December 16, 2016 03:03
Show Gist options
  • Save marketcalls/8435aa28c0e24d12750854671b40b0f1 to your computer and use it in GitHub Desktop.
Save marketcalls/8435aa28c0e24d12750854671b40b0f1 to your computer and use it in GitHub Desktop.
Rolling Returns Computation
//AFL Code to Compute Rolling Returns
//Recommended to use it on Weekly/Monthly Charts with Historical Data more than 10 Years
//Minor Variations in the Rolling Returns might be there because of timeframe selection.
//Coded by Rajandran R - Author www.marketcalls.in
_SECTION_BEGIN("Rolling Returns");
SetChartOptions(0,chartShowArrows|chartShowDates);
Years = Param("Years", 10, 1, 50, 1 );
if(Interval() == inDaily)
{
// 250x10 == 2500 Days Rolling Returns Calculated. i.e 10 Years Rolling Returns calculated by default.
Plot( ((C/Ref(C,-Years*250))^(1/Years)-1)*100, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
}
if(Interval() == inWeekly)
{
// 52x10 == 52 Weeks Rolling Returns Calculated. i.e 10 Years Rolling Returns calculated by default.
Plot( ((C/Ref(C,-Years*52))^(1/Years)-1)*100, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
}
if(Interval() == inMonthly)
{
// 12x10 == 120 Months Rolling Returns Calculated. i.e 10 Years Rolling Returns calculated by default.
Plot( ((C/Ref(C,-Years*12))^(1/Years)-1)*100, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
}
_SECTION_END();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment