Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,24 @@
|
|||
function simple_moving_averager(period) {
|
||||
var nums = [];
|
||||
return function(num) {
|
||||
nums.push(num);
|
||||
if (nums.length > period)
|
||||
nums.splice(0,1); // remove the first element of the array
|
||||
var sum = 0;
|
||||
for (var i in nums)
|
||||
sum += nums[i];
|
||||
var n = period;
|
||||
if (nums.length < period)
|
||||
n = nums.length;
|
||||
return(sum/n);
|
||||
}
|
||||
}
|
||||
|
||||
var sma3 = simple_moving_averager(3);
|
||||
var sma5 = simple_moving_averager(5);
|
||||
var data = [1,2,3,4,5,5,4,3,2,1];
|
||||
for (var i in data) {
|
||||
var n = data[i];
|
||||
// using WSH
|
||||
WScript.Echo("Next number = " + n + ", SMA_3 = " + sma3(n) + ", SMA_5 = " + sma5(n));
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
// single-sided
|
||||
Array.prototype.simpleSMA=function(N) {
|
||||
return this.map(
|
||||
function(el,index, _arr) {
|
||||
return _arr.filter(
|
||||
function(x2,i2) {
|
||||
return i2 <= index && i2 > index - N;
|
||||
})
|
||||
.reduce(
|
||||
function(current, last, index, arr){
|
||||
return (current + last);
|
||||
})/index || 1;
|
||||
});
|
||||
};
|
||||
|
||||
g=[0,1,2,3,4,5,6,7,8,9,10];
|
||||
console.log(g.simpleSMA(3));
|
||||
console.log(g.simpleSMA(5));
|
||||
console.log(g.simpleSMA(g.length));
|
||||
Loading…
Add table
Add a link
Reference in a new issue