Class: HistoricalQuote

Inherits:
Object
  • Object
show all
Defined in:
lib/pbyf.rb

Overview

making a class to do calculations using the historical data from yahoo finance maybe refactor so @high @low @close etc. columns are precalculated after initializing. Could be faster/cleaner, I dunno.

Constant Summary collapse

@@col =
{date: 0, open: 1, high: 2, low: 3, close: 4, volume: 5}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str_of_stock, from_date, to_date, interval = 'd') ⇒ HistoricalQuote

Returns a new instance of HistoricalQuote.



37
38
39
40
41
42
43
44
# File 'lib/pbyf.rb', line 37

def initialize str_of_stock, from_date, to_date, interval = 'd'
  @data = PBYF.get_hist_quote str_of_stock, from_date, to_date, interval = 'd'
  @data.slice!(0)
  @close = get_hist_column(@@col[:close])
  @high = get_hist_column(@@col[:high])
  @low = get_hist_column(@@col[:low])
  @stochastic = self.stochastic_oscillator
end

Instance Attribute Details

#closeObject

Returns the value of attribute close.



34
35
36
# File 'lib/pbyf.rb', line 34

def close
  @close
end

#highObject

Returns the value of attribute high.



34
35
36
# File 'lib/pbyf.rb', line 34

def high
  @high
end

#lowObject

Returns the value of attribute low.



34
35
36
# File 'lib/pbyf.rb', line 34

def low
  @low
end

#stochasticObject

Returns the value of attribute stochastic.



34
35
36
# File 'lib/pbyf.rb', line 34

def stochastic
  @stochastic
end

Instance Method Details

#find_kObject

this method should return an array of %K, with a value for every row of the data set (every date)



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pbyf.rb', line 59

def find_k
  # %K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100
  high = @high
  low = @low
  close = @close
  result = []

  close.each_index do |i|
    result << ((close[i]-low[i])/(high[i]-low[i]))*100
  end
  result
end

#get_data_from(date) ⇒ Object



46
47
48
49
# File 'lib/pbyf.rb', line 46

def get_data_from date
  #stuff here
  # return macd, stoch oscillator, high, low, and close for a specific date
end

#get_hist_column(col) ⇒ Object

get a column from the historical quotes CSV table



52
53
54
55
56
# File 'lib/pbyf.rb', line 52

def get_hist_column col
  closings = @data.map { |x| x[col] }
  closings.collect! {|i| i.to_f} unless col == 0
  closings
end

#stochastic_oscillator(interval = 3) ⇒ Object

This is the 3 day exponential moving average of %K, which is the convention



73
74
75
# File 'lib/pbyf.rb', line 73

def stochastic_oscillator interval = 3
  find_k.exp_moving_average(interval)
end