Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Amibroker AFL Code to Compute 10-Year Rolling Returns

1 min read

Rolling returns, also known as moving returns, are returns over a period of time that are recalculated every time a new data point is added to the data set. For example, in a 10-year rolling return, the returns for the last 10 years would be calculated and updated every year, based on the latest available data.

Here is the simple AFL code to compute the 10 year rolling returns for any script. Generally, Rolling Returns are computed for 3yr, 5yr, 10yr period. Rolling Returns are basically a performance measure of fund/index/stock over a period of time. Above chart shows current 10 year rolling returns of Nifty at 7.47%.

Rolling Returns are nothing but the computation of Historical CAGR. The formula for the calculation of CAGR is shown below.

CAGR = [(Ending Price ÷ Beginning Price)^1/n] – 1

where CAGR – Compound Annual Growth Rate
where “n” is the number of time periods (10 years for this example)

Note : To Compute 10 Year Rolling Returns historical data of 10+ years adjusted to split/bonus is required incase of stocks.

Here is the AFL code to compute 10 Year Rolling Returns. Code is designed to adapt to multiple timeframes like Daily, Weekly, and Monthly however it is recommended to use weekly/monthly for better accuracy.

Amibroker AFL Code – 10-Year Rolling Returns

//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();
Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Line Trading – OpenAlgo Automation Module for Amibroker Users

The line Trading Automation tool is designed for Manual traders who want to perform level-based trade execution faster and also bring some advanced trade...
Rajandran R
55 sec read

Mini Certification Course on Algorithmic Trading

Welcome to the Mini Certification on Algorithmic Trading using Amibroker and OpenAlgo! This comprehensive 4-part series is designed to equip you with the knowledge...
Rajandran R
1 min read

Pivot Reversal Strategy – Tradingview Pinescript to Amibroker AFL…

Pivot Reversal Strategy is a popular in-built strategy in Tradingview Platform. Here I had attempted to convert the pinescript v5 to Amibroker AFL Code....
Rajandran R
3 min read

5 Replies to “Amibroker AFL Code to Compute 10-Year Rolling Returns”

  1. i’m using amibroker 4.80 & have data sinse 2003.
    but getting error in Afl program writer ” Ln: 2, Col: 8 : error 30. Syntax error”

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More