ruby-technical-analysis 0.1.1 → 1.0.4
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.
- checksums.yaml +4 -4
- data/lib/ruby-technical-analysis.rb +1 -0
- data/lib/ruby_technical_analysis/indicator.rb +41 -0
- data/lib/ruby_technical_analysis/indicators/bollinger_bands.rb +50 -0
- data/lib/ruby_technical_analysis/indicators/chaikin_money_flow.rb +47 -0
- data/lib/ruby_technical_analysis/indicators/chande_momentum_oscillator.rb +56 -0
- data/lib/ruby_technical_analysis/indicators/commodity_channel_index.rb +70 -0
- data/lib/ruby_technical_analysis/indicators/envelopes_ema.rb +48 -0
- data/lib/ruby_technical_analysis/indicators/intraday_momentum_index.rb +45 -0
- data/lib/ruby_technical_analysis/indicators/macd.rb +86 -0
- data/lib/ruby_technical_analysis/indicators/mass_index.rb +70 -0
- data/lib/ruby_technical_analysis/indicators/moving_averages.rb +71 -0
- data/lib/ruby_technical_analysis/indicators/pivot_points.rb +75 -0
- data/lib/ruby_technical_analysis/indicators/price_channel.rb +48 -0
- data/lib/ruby_technical_analysis/indicators/qstick.rb +44 -0
- data/lib/ruby_technical_analysis/indicators/rate_of_change.rb +40 -0
- data/lib/ruby_technical_analysis/indicators/relative_momentum_index.rb +79 -0
- data/lib/ruby_technical_analysis/indicators/relative_strength_index.rb +74 -0
- data/lib/ruby_technical_analysis/indicators/statistical_methods.rb +40 -0
- data/lib/ruby_technical_analysis/indicators/stochastic_oscillator.rb +99 -0
- data/lib/ruby_technical_analysis/indicators/volume_oscillator.rb +50 -0
- data/lib/ruby_technical_analysis/indicators/volume_rate_of_change.rb +48 -0
- data/lib/ruby_technical_analysis/indicators/wilders_smoothing.rb +47 -0
- data/lib/ruby_technical_analysis/indicators/williams_percent_r.rb +69 -0
- data/lib/ruby_technical_analysis.rb +24 -7
- data/spec/ruby_technical_analysis/indicator_spec.rb +76 -0
- data/spec/ruby_technical_analysis/indicators/bollinger_bands_spec.rb +67 -0
- data/spec/ruby_technical_analysis/indicators/chaikin_money_flow_spec.rb +63 -0
- data/spec/ruby_technical_analysis/indicators/chande_momentum_oscillator_spec.rb +59 -0
- data/spec/ruby_technical_analysis/indicators/commodity_channel_index_spec.rb +66 -0
- data/spec/ruby_technical_analysis/indicators/envelopes_ema_spec.rb +69 -0
- data/spec/ruby_technical_analysis/indicators/intraday_momentum_spec.rb +63 -0
- data/spec/ruby_technical_analysis/indicators/macd_spec.rb +61 -0
- data/spec/ruby_technical_analysis/indicators/mass_index_spec.rb +67 -0
- data/spec/ruby_technical_analysis/indicators/moving_averages_spec.rb +81 -0
- data/spec/ruby_technical_analysis/indicators/pivot_points_spec.rb +43 -0
- data/spec/ruby_technical_analysis/indicators/price_channel_spec.rb +64 -0
- data/spec/ruby_technical_analysis/indicators/qstick_spec.rb +59 -0
- data/spec/ruby_technical_analysis/indicators/rate_of_change_spec.rb +59 -0
- data/spec/ruby_technical_analysis/indicators/relative_momentum_index_spec.rb +67 -0
- data/spec/ruby_technical_analysis/indicators/relative_strength_index_spec.rb +59 -0
- data/spec/ruby_technical_analysis/indicators/statistical_methods_spec.rb +91 -0
- data/spec/ruby_technical_analysis/indicators/stochastic_oscillator_spec.rb +106 -0
- data/spec/ruby_technical_analysis/indicators/volume_oscillator_spec.rb +98 -0
- data/spec/ruby_technical_analysis/indicators/volume_rate_of_change_spec.rb +67 -0
- data/spec/ruby_technical_analysis/indicators/wiilders_smoothing_spec.rb +67 -0
- data/spec/ruby_technical_analysis/indicators/williams_percent_r_spec.rb +71 -0
- data/spec/spec_helper.rb +1 -0
- metadata +100 -40
- data/.rubocop.yml +0 -34
- data/CHANGELOG.md +0 -5
- data/CODE_OF_CONDUCT.md +0 -84
- data/Gemfile +0 -12
- data/LICENSE.txt +0 -21
- data/README.md +0 -36
- data/Rakefile +0 -16
- data/lib/ruby-technical-analysis/indicators/bollinger_bands.rb +0 -25
- data/lib/ruby-technical-analysis/indicators/chaikin_money_flow.rb +0 -70
- data/lib/ruby-technical-analysis/indicators/chande_momentum_oscillator.rb +0 -34
- data/lib/ruby-technical-analysis/indicators/commodity_channel_index.rb +0 -64
- data/lib/ruby-technical-analysis/indicators/envelopes_ema.rb +0 -24
- data/lib/ruby-technical-analysis/indicators/intraday_momentum_index.rb +0 -48
- data/lib/ruby-technical-analysis/indicators/macd.rb +0 -47
- data/lib/ruby-technical-analysis/indicators/mass_index.rb +0 -73
- data/lib/ruby-technical-analysis/indicators/pivot_points.rb +0 -23
- data/lib/ruby-technical-analysis/indicators/price_channel.rb +0 -37
- data/lib/ruby-technical-analysis/indicators/qstick.rb +0 -40
- data/lib/ruby-technical-analysis/indicators/rate_of_change.rb +0 -18
- data/lib/ruby-technical-analysis/indicators/relative_momentum_index.rb +0 -66
- data/lib/ruby-technical-analysis/indicators/relative_strength_index.rb +0 -63
- data/lib/ruby-technical-analysis/indicators/stochastic_oscillator.rb +0 -65
- data/lib/ruby-technical-analysis/indicators/volume_oscillator.rb +0 -38
- data/lib/ruby-technical-analysis/indicators/volume_rate_of_change.rb +0 -26
- data/lib/ruby-technical-analysis/indicators/wilders_smoothing.rb +0 -27
- data/lib/ruby-technical-analysis/indicators/williams_percent_r.rb +0 -52
- data/lib/ruby-technical-analysis/moving_averages.rb +0 -85
- data/lib/ruby-technical-analysis/statistical_methods.rb +0 -24
- data/lib/ruby-technical-analysis/version.rb +0 -5
- data/sig/ruby-technical-analysis.rbs +0 -4
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
-
|
5
|
-
# Volume Oscillator indicator
|
6
|
-
# Returns a single value
|
7
|
-
module VolumeOscillator
|
8
|
-
def volume_oscillator(short_ma, long_ma)
|
9
|
-
if size < long_ma
|
10
|
-
raise ArgumentError,
|
11
|
-
"Volumes array passed to Volume Oscillator cannot be less than the Long Moving Average argument."
|
12
|
-
end
|
13
|
-
|
14
|
-
if long_ma <= short_ma
|
15
|
-
raise ArgumentError,
|
16
|
-
"Long Moving Average parameter must be greater than Short Moving Average parameter in Volume Oscillator."
|
17
|
-
end
|
18
|
-
|
19
|
-
short_ma_a = []
|
20
|
-
long_ma_a = []
|
21
|
-
|
22
|
-
(0..(length - short_ma)).each do |i|
|
23
|
-
short_ma_a << self[i..(i + short_ma - 1)].sma(short_ma)
|
24
|
-
end
|
25
|
-
|
26
|
-
(0..(length - long_ma)).each do |i|
|
27
|
-
long_ma_a << self[i..(i + long_ma - 1)].sma(long_ma)
|
28
|
-
end
|
29
|
-
|
30
|
-
sml = (short_ma_a.last - long_ma_a.last).round(2)
|
31
|
-
|
32
|
-
((sml.to_f / long_ma_a.last) * 100).round(2)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class Array
|
37
|
-
include VolumeOscillator
|
38
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Volume Rate of Change indicator
|
4
|
-
# Returns a single value
|
5
|
-
module VolumeRateOfChange
|
6
|
-
def volume_rate_of_change(period)
|
7
|
-
if size < (period + 1)
|
8
|
-
raise ArgumentError,
|
9
|
-
"Volumes array passed to Volume Rate of Change cannot be less than period parameter + 1."
|
10
|
-
end
|
11
|
-
|
12
|
-
delta_volume = []
|
13
|
-
vol_shifted = []
|
14
|
-
|
15
|
-
(0..(length - period - 1)).each do
|
16
|
-
vol_shifted << at(length - period - 1)
|
17
|
-
delta_volume << at(-1) - vol_shifted[-1]
|
18
|
-
end
|
19
|
-
|
20
|
-
((delta_volume[-1].to_f / vol_shifted[-1]) * 100).round(4)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class Array
|
25
|
-
include VolumeRateOfChange
|
26
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
-
|
5
|
-
# Wilders Smoothing indicator
|
6
|
-
# Returns a singular current value
|
7
|
-
module WildersSmoothing
|
8
|
-
def wilders_smoothing(period)
|
9
|
-
if size < period
|
10
|
-
raise ArgumentError,
|
11
|
-
"Closes array passed to Wilders Smoothing cannot be less than the period argument."
|
12
|
-
end
|
13
|
-
|
14
|
-
ws = []
|
15
|
-
ws << first(period).sma(period)
|
16
|
-
|
17
|
-
(0..(size - period - 1)).each do |i|
|
18
|
-
ws << ((at(i + period) - ws[i]) * (1.0 / period)) + ws[i]
|
19
|
-
end
|
20
|
-
|
21
|
-
ws[-1]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class Array
|
26
|
-
include WildersSmoothing
|
27
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Williams Percent R indicator
|
4
|
-
# Returns a single value
|
5
|
-
module WilliamsPercentR
|
6
|
-
def williams_percent_r(period)
|
7
|
-
highs = []
|
8
|
-
lows = []
|
9
|
-
closes = []
|
10
|
-
|
11
|
-
each do |i|
|
12
|
-
highs << i[0]
|
13
|
-
lows << i[1]
|
14
|
-
closes << i[2]
|
15
|
-
end
|
16
|
-
|
17
|
-
if highs.size < period
|
18
|
-
raise ArgumentError,
|
19
|
-
"High array passed to Williams Percent R cannot be less than the period argument."
|
20
|
-
end
|
21
|
-
|
22
|
-
if lows.size < period
|
23
|
-
raise ArgumentError,
|
24
|
-
"Low array passed to Williams Percent R cannot be less than the period argument."
|
25
|
-
end
|
26
|
-
|
27
|
-
if closes.size < period
|
28
|
-
raise ArgumentError,
|
29
|
-
"Close array passed to Williams Percent R cannot be less than the period argument."
|
30
|
-
end
|
31
|
-
|
32
|
-
highest_highs = []
|
33
|
-
lowest_lows = []
|
34
|
-
hh_min_close = []
|
35
|
-
hh_min_ll = []
|
36
|
-
pct_r = []
|
37
|
-
|
38
|
-
(0..highs.length - period).each do |i|
|
39
|
-
highest_highs << highs[i..period - 1 + i].max
|
40
|
-
lowest_lows << lows[i..period - 1 + i].min
|
41
|
-
hh_min_close << (highest_highs[-1] - closes[period - 1 + i]).round(2)
|
42
|
-
hh_min_ll << (highest_highs[-1] - lowest_lows[-1]).round(2)
|
43
|
-
pct_r << ((hh_min_close[-1].to_f / hh_min_ll[-1]) * -100).round(2)
|
44
|
-
end
|
45
|
-
|
46
|
-
pct_r[-1]
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
class Array
|
51
|
-
include WilliamsPercentR
|
52
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# module MovingAverages contains some common moving averages and those used in other technical functions
|
4
|
-
# it is built upon the array class and all methods assume the use of an array
|
5
|
-
module MovingAverages
|
6
|
-
# Simple Moving Average
|
7
|
-
def sma(period)
|
8
|
-
if period <= 0
|
9
|
-
raise ArgumentError,
|
10
|
-
"The period parameter cannot be less than 1."
|
11
|
-
end
|
12
|
-
|
13
|
-
if size < period
|
14
|
-
raise ArgumentError,
|
15
|
-
"The array size is less than the period size for the simple moving average."
|
16
|
-
end
|
17
|
-
|
18
|
-
(last(period).reduce(:+).to_f / period)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Exponential Moving Average
|
22
|
-
def ema(period)
|
23
|
-
if period < 1
|
24
|
-
raise ArgumentError,
|
25
|
-
"The period parameter cannot be less than 1."
|
26
|
-
end
|
27
|
-
if size < period
|
28
|
-
raise ArgumentError,
|
29
|
-
"The array size is less than the period size for the exponential moving average."
|
30
|
-
end
|
31
|
-
|
32
|
-
if period == 1
|
33
|
-
last
|
34
|
-
else
|
35
|
-
case period
|
36
|
-
when 12
|
37
|
-
last_obs_pct = 0.846154
|
38
|
-
ma_pct = 0.153846
|
39
|
-
when 26
|
40
|
-
last_obs_pct = 0.925926
|
41
|
-
ma_pct = 0.074074
|
42
|
-
else
|
43
|
-
last_obs_pct = 2.0 / (period + 1)
|
44
|
-
ma_pct = 1.0 - last_obs_pct
|
45
|
-
end
|
46
|
-
|
47
|
-
ma_array = []
|
48
|
-
|
49
|
-
last(period).each_with_index do |num, i|
|
50
|
-
ma_array << if i.zero?
|
51
|
-
num
|
52
|
-
else
|
53
|
-
(num * last_obs_pct) + (ma_array[i - 1] * ma_pct)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
ma_array.last
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
# Weighted Moving Average
|
62
|
-
def wma(period)
|
63
|
-
if period <= 0 || size < period
|
64
|
-
raise ArgumentError,
|
65
|
-
"The period of the weighted moving average supplied must be greater than 0 and less than or equal to the
|
66
|
-
size of the array."
|
67
|
-
end
|
68
|
-
|
69
|
-
true_periods = 0
|
70
|
-
(1..period).each do |i|
|
71
|
-
true_periods += i
|
72
|
-
end
|
73
|
-
|
74
|
-
sigma_periods = 0.0
|
75
|
-
last(period).each_with_index do |num, i|
|
76
|
-
sigma_periods += ((i + 1) * num)
|
77
|
-
end
|
78
|
-
|
79
|
-
(sigma_periods.to_f / true_periods)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
class Array
|
84
|
-
include MovingAverages
|
85
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Statistical methods used in calculations
|
4
|
-
module StatisticalMethods
|
5
|
-
def standard_deviation
|
6
|
-
if size <= 1
|
7
|
-
raise ArgumentError,
|
8
|
-
"Array must contain at least 2 numbers for standard deviation."
|
9
|
-
end
|
10
|
-
|
11
|
-
mean = reduce(:+).to_f / size
|
12
|
-
sq_dist = 0
|
13
|
-
|
14
|
-
each do |n|
|
15
|
-
sq_dist += (n - mean).abs2
|
16
|
-
end
|
17
|
-
|
18
|
-
Math.sqrt(sq_dist.to_f / size)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
class Array
|
23
|
-
include StatisticalMethods
|
24
|
-
end
|