lib/signal_tools/stock.rb in signal_tools-0.2.0 vs lib/signal_tools/stock.rb in signal_tools-0.2.1

- old
+ new

@@ -13,39 +13,43 @@ to_date = Date.parse(to_date) unless to_date.is_a?(Date) @ticker = ticker @stock_data = SignalTools::StockData.new(ticker, from_date, to_date) end - def dates - @stock_data.dates - end - # Takes a period of days over which to average closing prices and returns the exponential moving average for each day. def ema(period=10) - trim_data_to_range(ema_points(period, @stock_data.close_prices)) + trim_data_to_range!(ema_points(period, @stock_data.close_prices)) end def macd(fast=8, slow=17, signal=9) - trim_data_to_range(macd_points(fast, slow, signal)) + trim_data_to_range!(macd_points(fast, slow, signal)) end def fast_stochastic(k=14, d=5) - trim_data_to_range(fast_stochastic_points(k, d)) + trim_data_to_range!(fast_stochastic_points(k, d)) end def slow_stochastic(k=14, d=5) - trim_data_to_range(slow_stochastic_points(k, d)) + trim_data_to_range!(slow_stochastic_points(k, d)) end def atr(period=14) - trim_data_to_range(average_true_ranges(period)) + trim_data_to_range!(average_true_ranges(period)) end def adx(period=14) - trim_data_to_range(average_directional_indexes(period)) + trim_data_to_range!(average_directional_indexes(period)) end + def dates + @stock_data.dates + end + + def close_prices + @close_prices = trim_data_to_range(@stock_data.close_prices) + end + private #### EMA methods def ema_points(period, data, type=:default) @@ -261,19 +265,23 @@ end #### Misc Utility Methods # Returns only the points specific to the date range given. - def trim_data_to_range(data) + def trim_data_to_range!(data) if data.is_a? Array - data.slice!(0...(-dates.size)) + data.slice!(0..(-dates.size-1)) elsif data.is_a? Hash - data.each { |k,v| v = v.slice!(0...(-dates.size)) } + data.each { |k,v| v = v.slice!(0..(-dates.size-1)) } end data end + def trim_data_to_range(data) + data.slice((-dates.size+1)..-1) + end + # Gets the first 0...period of numbers from data and returns a simple average. def default_simple_average(data, period) SignalTools.average(data.slice(0...period)) end @@ -296,14 +304,8 @@ while((index + period - 1) < points.size) collection << get_for_period(points, index, (index + period - 1), method) index += 1 end collection - end - - def matching_dates(array) - dates = @stock_data.dates.dup - SignalTools.truncate_to_shortest!(dates, array) - @dates = dates end end end