Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/ruby_technical_analysis/indicators/moving_averages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def sma
def ema
return series.last if period == 1

series.last(period).each_with_object([]) do |num, result|
series.each_with_object([]) do |num, result|
result << if result.empty?
num
else
Expand Down Expand Up @@ -59,8 +59,8 @@ def valid?
def _ema_percentages
@_ema_percentages ||=
case period
when 12 then [0.846154, 0.153846]
when 26 then [0.925926, 0.074074]
when 12 then [0.153846, 0.846154]
when 26 then [0.074074, 0.925926]
else
last_obs_pct = 2.0 / (period + 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def valid?
private

def _smooth_coef_one
@_smooth_coef_one ||= (1.0 / period).round(4)
@_smooth_coef_one ||= (1.0 / period)
end

def _smooth_coef_two
Expand All @@ -39,7 +39,7 @@ def _smooth_coef_two

def calculate_channels(cla)
period.times.map do |index|
diff = (cla.at(index) - cla.at(index + 1)).round(4)
diff = (cla.at(index) - cla.at(index + 1))

[diff.negative? ? diff.abs : 0, diff.positive? ? diff : 0]
end.transpose
Expand All @@ -53,8 +53,8 @@ def calculate_initial_smoothing(up_ch, down_ch)
end

def calculate_subsequent_smoothing(up_ch, down_ch)
@smooth_up << (_smooth_coef_one * up_ch.last + _smooth_coef_two * @smooth_up.last).round(4)
@smooth_down << (_smooth_coef_one * down_ch.last + _smooth_coef_two * @smooth_down.last).round(4)
@smooth_up << (_smooth_coef_one * up_ch.last + _smooth_coef_two * @smooth_up.last)
@smooth_down << (_smooth_coef_one * down_ch.last + _smooth_coef_two * @smooth_down.last)
end

def calculate_smoothing(up_ch, down_ch)
Expand All @@ -67,7 +67,7 @@ def calculate_rsi
up_ch, down_ch = calculate_channels(cla)

calculate_smoothing(up_ch, down_ch)
@rsi << (100.00 - (100.00 / ((@smooth_up.last.to_f / @smooth_down.last) + 1))).round(4)
@rsi << (100.00 - (100.00 / ((@smooth_up.last.to_f / @smooth_down.last) + 1)))
end.last
end
end
Expand Down