pbyf 0.0.2

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.
Files changed (2) hide show
  1. data/lib/pbyf.rb +142 -0
  2. metadata +78 -0
data/lib/pbyf.rb ADDED
@@ -0,0 +1,142 @@
1
+ #Pause Break Yahoo Finance gem
2
+
3
+ require 'rest-client'
4
+ require 'csv'
5
+
6
+ class PBYF
7
+ #these are the base urls for querying various parts of yahoo finance
8
+ @@quote_url = 'http://download.finance.yahoo.com/d/quotes.csv?s='
9
+ @@historical_quote_url = 'http://ichart.yahoo.com/table.csv?s='
10
+
11
+ #str_of_stocks should be formatted according to yahoo finance api docs
12
+ # might need to do work here on the formatting when multiple stocks are requested
13
+ def self.get_quote str_of_stocks, str_of_options
14
+ query_url = @@quote_url + str_of_stocks + '&f=' + str_of_options
15
+ arr_of_arrays = CSV.parse(RestClient.get query_url)
16
+ end
17
+
18
+ # one cannot get multiple stocks for historical quotes, you must get them separately
19
+ # for now dates will be assumed to be the format Month-Day-Year, i.e. 3-19-1988
20
+ #this should return an array of arrays of [Date, Open, High, Low, Close, Volume, Adj Close]
21
+ def self.get_hist_quote str_of_stock, from_date, to_date, interval = 'd'
22
+ from = from_date.split('-')
23
+ to = to_date.split('-')
24
+ from_to_params = "&a=#{from[0].to_i-1}" + "&b=#{from[1]}" + "&c=#{from[2]}" + "&d=#{to[0].to_i-1}" + "&e=#{to[1]}" + "&f=#{to[2]}"
25
+ query_url = @@historical_quote_url + str_of_stock + from_to_params + "&g=#{interval}"
26
+ arr_of_arrays = CSV.parse(RestClient.get query_url)
27
+ end
28
+
29
+ end
30
+
31
+ # making a class to do calculations using the historical data from yahoo finance
32
+ # maybe refactor so @high @low @close etc. columns are precalculated after initializing. Could be faster/cleaner, I dunno.
33
+ class HistoricalQuote
34
+ attr_accessor :close, :high, :low, :stochastic
35
+ @@col = {date: 0, open: 1, high: 2, low: 3, close: 4, volume: 5}
36
+
37
+ def initialize str_of_stock, from_date, to_date, interval = 'd'
38
+ @data = PBYF.get_hist_quote str_of_stock, from_date, to_date, interval = 'd'
39
+ @data.slice!(0)
40
+ @close = get_hist_column(@@col[:close])
41
+ @high = get_hist_column(@@col[:high])
42
+ @low = get_hist_column(@@col[:low])
43
+ @stochastic = @close.stochastic_oscillator
44
+ end
45
+
46
+ def get_data_from date
47
+ #stuff here
48
+ # return macd, stoch oscillator, high, low, and close for a specific date
49
+ end
50
+
51
+ #get a column from the historical quotes CSV table
52
+ def get_hist_column col
53
+ closings = @data.map { |x| x[col] }
54
+ closings.collect! {|i| i.to_f} unless col == 0
55
+ closings
56
+ end
57
+
58
+ # this method should return an array of %K, with a value for every row of the data set (every date)
59
+ def find_k
60
+ # %K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100
61
+ high = @high
62
+ low = @low
63
+ close = @close
64
+ result = []
65
+
66
+ close.each_index do |i|
67
+ result << ((close[i]-low[i])/(high[i]-low[i]))*100
68
+ end
69
+ result
70
+ end
71
+
72
+ # This is the 3 day exponential moving average of %K, which is the convention
73
+ def stochastic_oscillator interval = 3
74
+ find_k.exp_moving_average(interval)
75
+ end
76
+
77
+ end
78
+
79
+ class Array
80
+
81
+ # measures diference between closing value and moving average. Defaults to 20 but use whatever you want
82
+ def pvma interval=20
83
+ array = self
84
+ result =[]
85
+ mvgavg = self.moving_average(interval)
86
+
87
+ @close.each_index do |i|
88
+ result << (@close[i] - mvavg[i])
89
+ end
90
+ result
91
+ end
92
+
93
+ def macd short, long
94
+ data = self
95
+ raise 'Your historical data array is too short!' if (data.size < long)
96
+
97
+ b = data.exp_moving_average(long)
98
+
99
+ a = data.exp_moving_average(short)
100
+ a.slice!(0, long-short)
101
+
102
+ i=0
103
+ result = []
104
+ a.each do |ele|
105
+ result << (ele-b[i])
106
+ i+=1
107
+ end
108
+ result
109
+ end
110
+
111
+ def moving_average interval
112
+ return self if interval == 1
113
+ a = self.dup.map{|i| i.to_f}
114
+ result = []
115
+ i=0
116
+ while(i <= a.size-interval)
117
+ data = a.slice(i,interval)
118
+ result << data.average
119
+ i+=1
120
+ end
121
+
122
+ result
123
+ end
124
+
125
+ def exp_moving_average interval
126
+ return self if interval == 1
127
+ a = self.dup.map{|i| i.to_f}
128
+ result = []
129
+ result << a.moving_average(interval)[0]
130
+ k = 2/(interval.to_f+1)
131
+
132
+ (a.size-interval).times do |i|
133
+ ema = ((a[i+interval]-result[i])*k + result[i])
134
+ result << ema
135
+ end
136
+ result
137
+ end
138
+
139
+ def average
140
+ self.inject(:+)/(self.size.to_f)
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pbyf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Wickman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: csv
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Use this gem to pull data from the Yahoo Finance site, and do some basic
47
+ calculations with it.
48
+ email: wickman.matthew@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/pbyf.rb
54
+ homepage: http://github.com/mwickman/pbyf
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.24
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A gem for use with Yahoo Finance
78
+ test_files: []