indicators 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  A gem for calculating technical analysis indicators.
4
4
 
5
- [![Build Status](https://secure.travis-ci.org/Nedomas/indicators.png)](http://travis-ci.org/Nedomas/indicators)
6
-
7
5
  Current functionality demo of Indicators and Securities gems synergy can be tested at http://strangemuseum.heroku.com.
8
6
 
7
+ [![Build Status](https://secure.travis-ci.org/Nedomas/indicators.png)](http://travis-ci.org/Nedomas/indicators)
8
+
9
9
  ## Installation
10
10
 
11
11
  Add this line to your application's Gemfile:
@@ -71,6 +71,14 @@ Variables have to be specified as an array [faster periods, slower periods, sign
71
71
 
72
72
  The more data it has, the more accurate RSI is.
73
73
 
74
+ # Full Stochastic Oscillator => :sto
75
+
76
+ my_data.calc(:type => :sto, :variables => [14, 3, 5])
77
+
78
+ Variables have to be specified as an array [lookback period, the number of periods to slow %K, the number of periods for the %D moving average] => [%K1, %K2, %D].
79
+
80
+ Stochastic output is [fast %K, slow %K, full %D].
81
+
74
82
  ## To Do
75
83
 
76
84
  * Make defaults mechanism more versatile.
@@ -82,7 +90,10 @@ The more data it has, the more accurate RSI is.
82
90
  * More moving averages (CMA, WMA, MMA).
83
91
  * ROC.
84
92
  * CCI.
85
- * Stochastics.
93
+ * Williams %R.
94
+ * ADX.
95
+ * Parabolic SAR.
96
+ * StochRSI.
86
97
 
87
98
  ## Contributing
88
99
 
@@ -2,7 +2,7 @@ module Indicators
2
2
  class Data
3
3
 
4
4
  attr_reader :data, :results
5
- INDICATORS = [:sma, :ema, :bb, :macd, :rsi]
5
+ INDICATORS = [:sma, :ema, :bb, :macd, :rsi, :sto]
6
6
  # Error handling
7
7
  class DataException < StandardError
8
8
  end
@@ -21,6 +21,7 @@ module Indicators
21
21
  when :bb then bb(Indicators::Parser.parse_data(stock_data), parameters[:variables])
22
22
  when :macd then macd(Indicators::Parser.parse_data(stock_data), parameters[:variables])
23
23
  when :rsi then rsi(Indicators::Parser.parse_data(stock_data), parameters[:variables])
24
+ when :sto then sto(Indicators::Parser.parse_data(stock_data), parameters[:variables])
24
25
  end
25
26
  # Parser returns in {:date=>[2012.0, 2012.0, 2012.0], :open=>[409.4, 410.0, 414.95],} format
26
27
  end
@@ -32,6 +33,7 @@ module Indicators
32
33
  when :bb then bb(data, parameters[:variables])
33
34
  when :macd then macd(data, parameters[:variables])
34
35
  when :rsi then rsi(data, parameters[:variables])
36
+ when :sto then raise MainException, "You cannot calculate Stochastic Oscillator on closing prices. Use Securities hash instead."
35
37
  end
36
38
  end
37
39
  return results
@@ -242,6 +244,57 @@ module Indicators
242
244
  return rsi
243
245
  end
244
246
 
247
+ #
248
+ # Full Stochastic Oscillator
249
+
250
+ # %K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100
251
+ # %D = 3-day SMA of %K
252
+ # Lowest Low = lowest low for the look-back period
253
+ # Highest High = highest high for the look-back period
254
+ # %K is multiplied by 100 to move the decimal point two places
255
+ #
256
+ # Full %K = Fast %K smoothed with X-period SMA
257
+ # Full %D = X-period SMA of Full %K
258
+ #
259
+ # Input 14, 3, 5
260
+ # Returns [full %K, full %D]
261
+ def self.sto data, variables
262
+ k1_periods = get_variables(variables, 0, 14)
263
+ k2_periods = get_variables(variables, 1, 3)
264
+ d_periods = get_variables(variables, 2, 1)
265
+ high_data = Array.new
266
+ low_data = Array.new
267
+ adj_close_data = Array.new
268
+ high_data = get_data(data, k1_periods, :high)
269
+ low_data = get_data(data, k1_periods, :low)
270
+ adj_close_data = get_data(data, k1_periods, :adj_close)
271
+
272
+ k1 = []
273
+ k2 = []
274
+ d = []
275
+ sto = []
276
+ adj_close_data.each_with_index do |adj_close, index|
277
+ from = index-k1_periods+1
278
+ if index+1 >= k1_periods
279
+ k1[index] = (adj_close - low_data[from..index].min) / (high_data[from..index].max - low_data[from..index].min) * 100
280
+ if index+2 >= k1_periods + k2_periods
281
+ k2[index] = sma(k1[(k1_periods-1)..index], k2_periods).last
282
+ else
283
+ k2[index] = nil
284
+ end
285
+ if index+3 >= k1_periods + k2_periods + d_periods
286
+ d[index] = sma(k2[(k1_periods + k2_periods - 2)..index], d_periods).last
287
+ else
288
+ d[index] = nil
289
+ end
290
+ else
291
+ k1[index] = nil
292
+ end
293
+ sto[index] = [k1[index], k2[index], d[index]]
294
+ end
295
+ return sto
296
+ end
297
+
245
298
 
246
299
  #
247
300
  # Helper methods for RSI
@@ -1,3 +1,3 @@
1
1
  module Indicators
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indicators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-01 00:00:00.000000000 Z
12
+ date: 2012-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -95,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  segments:
97
97
  - 0
98
- hash: 3875007528278561270
98
+ hash: 1434689323242091745
99
99
  required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  none: false
101
101
  requirements:
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  segments:
106
106
  - 0
107
- hash: 3875007528278561270
107
+ hash: 1434689323242091745
108
108
  requirements: []
109
109
  rubyforge_project:
110
110
  rubygems_version: 1.8.24