0
How to calculate moving averages of shares for preceding n rows based on closing price
moving averages..
2 Respuestas
+ 1
Here a sample:
Array.prototype.sma = function(periods) {
var sum = 0;
if(this.length<periods) return -1;
for(var i=0;i<periods;i++) {
sum += this[i];
}
return sum/periods;
}
var closingPrices = [2.34, 2.10, 2.12, 2.11,2.12, 2.10, 2.13, 2.15, 2.14, 2.16,2.12, 2.14,2.13,2.13, 2.22, 2.14,2.16,2.18,2.15,2.13,2.15,2.14,2.10,2.15];
console.log("Moving average: " + closingPrices.sma(20).toFixed(3));
https://code.sololearn.com/WRBCtMtBampM/?ref=app
0
I just made a Mysql query for Moving Average table.
Check out this query that form MA table from recent MSFT ohlc data.
SELECT @'Moving Average Period' := 5;
SELECT a.id, a.date, a.close,
Round( ( SELECT SUM(b.close) / COUNT(b.close)
FROM stock_price_msft AS b
WHERE DATEDIFF(a.date, b.date) BETWEEN 0 AND (@'Moving Average Period' - 1)
), 2 ) AS 'Moving Average'
FROM stock_price_msft AS a
ORDER BY a.date;
Query a stock data here:
http://www.sqlfiddle.com/#!9/9c7996/1