Comments on: Moving Average Custom Function for Google Spreadsheets https://www.justinsilver.com/technology/moving-average-custom-function-for-google-spreadsheets/?utm_source=rss&utm_medium=rss&utm_campaign=moving-average-custom-function-for-google-spreadsheets Technology, Travel, and Pictures Tue, 23 Apr 2019 11:06:59 +0000 hourly 1 https://wordpress.org/?v=6.0.1 By: Jesse Vonk https://www.justinsilver.com/technology/moving-average-custom-function-for-google-spreadsheets/#comment-2674 Tue, 23 Apr 2019 11:06:59 +0000 http://justinsilver.com/?p=3898#comment-2674 Hey Justin,
I’m currently using your function for a stat arb model and i really like the convenience and simplicity of it. I noticed however that if your dataset is organized from most recent date in the first row to latest date in the last – like my data set is – the MA suffers from lookahead bias. I made a few changes and thought I would share it for anyone with the similarly ordered dataset as I have.

Much thanks for your work!

Code:

function MOVING_AVERAGE(column, days, dateColumn){
  // defaults
  days = ( typeof days == 'undefined' )? 30 : days;
  dateColumn = ( typeof dateColumn == 'undefined' )? 'A' : dateColumn;
  column = ( typeof column == 'undefined' )? 'B' : column;
  var now = (new Date()).getTime();
   
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var lastRow = SpreadsheetApp.getActiveSheet().getMaxRows()-1;
  var dates = sheet.getRange(dateColumn+"2:"+dateColumn).getValues();
  var values = sheet.getRange(column+"2:"+column).getValues();
  for (; values[lastRow - 1] == "" && lastRow > 0; lastRow--) {}
  var title = sheet.getRange(column+"1").getValue();
  var results = [ days + " period MA " + title ];
  for ( var rowIndex = 0; rowIndex < lastRow; rowIndex++ ){
    // we are after "today" 
    var inTheFuture = (now+3600 <= (new Date(dates[rowIndex])).getTime());
     
    if (inTheFuture){
      results.push(0);
    } else { 
      var altLookback = lastRow - rowIndex - 1;
      var lookback = ( altLookback < days )? altLookback : days-1;
      var startRow = rowIndex;
      var endRow = rowIndex + lookback;
       
      var total = 0;
      for ( var i=startRow; i<=endRow; i++ ){
        total += parseFloat( values[i] );
      }
       
      results.push(parseFloat(total/((endRow - startRow)+1)));
    }
  }
   
  return results;
}
]]>
By: Yuriy https://www.justinsilver.com/technology/moving-average-custom-function-for-google-spreadsheets/#comment-2520 Thu, 01 Feb 2018 16:32:52 +0000 http://justinsilver.com/?p=3898#comment-2520 In reply to Sunil Y.

Hi couldn’t get this to work for a while (I kept passing it a Range of cells) the author does not give any practical examples…

A . | B . | C
———————————————————–
Timestamp . Series1 . Series2
2015-05-18 . 2 4
2015-05-25 . 3 1
2015-06-01 . 4 4
2015-06-08 . 2 5

In output cell use Function =MOVING_AVERAGE(“B”, 2, “A”)

The parameters are 1st (“B”) is a character representing the column to pick if you look at the code he skips cell B1 and starts from B2,

Parameter 2 is the window size of the moving average in our case it’s 2

Parameter 3 is the column representing the dates in our case its column A, passed as “A” in the function.

]]>
By: Sunil Y https://www.justinsilver.com/technology/moving-average-custom-function-for-google-spreadsheets/#comment-2342 Fri, 14 Apr 2017 19:59:08 +0000 http://justinsilver.com/?p=3898#comment-2342 hi Justin,
I tried to use your code to get moving average of a stock. But am not able to make it work. I am not a software engineer and don’t know any programming languages. Could you please help me on how to use your code to get the moving average of a stock for past so many days (where I can change the number of days). Your help would be very much appreciated.

]]>