advanced_math 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/simple_moving_average.rb +53 -0
- metadata +65 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
# Simple Moving Average (SMA) calculator
|
2
|
+
# Created: 2011-06-24
|
3
|
+
# Author: G Nagel
|
4
|
+
# Company: Mercury Wireless Software LLC
|
5
|
+
|
6
|
+
module Math
|
7
|
+
|
8
|
+
class SimpleMovingAverage
|
9
|
+
|
10
|
+
###
|
11
|
+
# Initialize the members:
|
12
|
+
# range:
|
13
|
+
# number of values to average
|
14
|
+
# sum:
|
15
|
+
# current sum of all values in the array
|
16
|
+
# values:
|
17
|
+
# array of values used as temporary storage
|
18
|
+
###
|
19
|
+
def initialize(range)
|
20
|
+
@range = range.to_i;
|
21
|
+
@sum = 0;
|
22
|
+
@values = Array.new();
|
23
|
+
end
|
24
|
+
|
25
|
+
###
|
26
|
+
# Add a value to the list.
|
27
|
+
# If the list is < @range, then return nil.
|
28
|
+
# Otherwise compute the SMA and return the value.
|
29
|
+
###
|
30
|
+
def add(value)
|
31
|
+
# add the value to the end of the array.
|
32
|
+
@values.push(value);
|
33
|
+
|
34
|
+
# Calculate the sum of the array
|
35
|
+
@sum += value.to_f;
|
36
|
+
|
37
|
+
# Is the array less than the range?
|
38
|
+
length = @values.length;
|
39
|
+
if (length < @range)
|
40
|
+
return nil;
|
41
|
+
end
|
42
|
+
|
43
|
+
# Is the array larger than the range?
|
44
|
+
if (length > @range)
|
45
|
+
@sum -= @values.shift.to_f();
|
46
|
+
end
|
47
|
+
|
48
|
+
# Compute the average
|
49
|
+
return @sum.to_f / @range.to_f;
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: advanced_math
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- G Nagel
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-24 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A simple gem for advanced and financial math calcualtions.
|
22
|
+
email: glenn@mercury-wireless.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/simple_moving_average.rb
|
31
|
+
homepage: https://github.com/gnagel/mercury-wireless-public
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A simple gem for advanced and financial math calcualtions.
|
64
|
+
test_files: []
|
65
|
+
|