FAQ Database Discussion Community

Calculating EMA from an array of closing prices (nodejs)?

arrays,node.js,average,stocks,moving-average
I have an array of daily closing prices for a stock (historical data) formatted as such: var close = [39, 40.133, 38.23, .... , 45.38] these are in order chronologically by date. I am trying to make an array of EMA (exponential moving average) values of the closing price to...

Create a 7-day rolling mean by Group

r,function,data.table,moving-average
I want to create a 7-day rolling mean for a daily physical training load over a year calculated for each athlete in a training group. The data set is called 'daily.load'. The vectors are: 'Date', 'Name', 'Total.Load' The ideal end result would be a rolling 7-day mean for 'Total.Load' as...

SQL workaround to substitute FOLLOWING / PRECEEDING in PostgreSQL 8.4

sql,postgresql,window-functions,postgresql-8.4,moving-average
I have a query that does a basic moving average using the FOLLOWING / PRECEDING syntax of PostgreSQL 9.0. To my horror I discovered our pg server runs on 8.4 and there is no scope to get an upgrade in the near future. I am therefore looking for the simplest...

How to efficiently compute average on the fly (moving average)?

algorithm,math,average,moving-average
I come up with this n=1; curAvg = 0; loop{ curAvg = curAvg + (newNum - curAvg)/n; n++; } I think highlights of this way are: - It avoids big numbers (and possible overflow if you would sum and then divide) - you save one register (not need to store...

Why is zoo::rollmean slow compared to a simple Rcpp implementation?

r,zoo,moving-average
zoo::rollmean is a helpful function that returns the rolling mean of a time series; for vector x of length n and window size k it returns the vector c(mean(x[1:k]), mean(x[2:(k+1)]), ..., mean(x[(n-k+1):n])). I noticed that it seemed to be running slowly for some code I was developing, so I wrote...

VHDL average of Array through for loop

arrays,for-loop,vhdl,moving-average
I have an Array of X Integer values in VHDL declared as a variable inside a process. I would like to calculate the average of all Values in a for loop. If I write it out for 3 Values manually everything works fine (tested on hardware): entity MyEntity is Port(...

rollmean length of timeseries

r,time-series,mean,moving-average
I have a time series of regular 15 minute data intervals and I want to take a rolling average of the data for every 2 hours, hence the width of the rollmean interval would be 8. The only issue is that the length of the original time series that I...

I tried coding my own simple moving average in C++

c++,algorithm,while-loop,nested-loops,moving-average
I want a function that works. I believe my logic is correct, thus my (vector out of range error) must be coming from the lack of familiarity and using the code correctly. I do know that there is long code out there for this fairly simple algorithm. Please help...

What's wrong with my nested for loop in R?

r,for-loop,matrix,average,moving-average
fname = file.choose() two = read.csv(fname.header=T) rec = two\$Receipt del = two\$Delivery date = two\$Date net = rec-del yrec = matrix(rec,nrow=365,ncol=4,byrow=F) ydel = matrix(del,nrow=365,ncol=4,byrow=F) ynet = matrix(net,nrow=365,ncol=4,byrow=F) yrecsum = 0 yrecavg = 0 for(i in 1:4) { for(j in 1:365) { yrecsum[i] = yrecsum[i]+yrec[j,i] } yrecavg[i] = yrecsum[i]/365 } So...