ib-technical-analysis 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ module TechnicalAnalysis
2
+ module MovingAverage
3
+ SMA = Struct.new :time, :value
4
+
5
+
6
+ # Calculates the exponential moving average (EMA) for the data over the given period
7
+ # https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
8
+ #
9
+ # Takes a block which replaces the _smooth-constant_
10
+ #
11
+ # z = Symbols::Futures.mini_dax.eod( duration: '90 d')
12
+ # TechnicalAnalysis::MovingAverage::ExpMA.new( data=z.map(&:close))
13
+ #
14
+ # returns an array with the calculated moving-average data
15
+ #
16
+ # ema = TechnicalAnalysis::MovingAverage::ExpMA.new( default_value = z.first[:close])
17
+ # moving_average = z.map{|x| EMA.new x.time, ema.add_item(x.close)}
18
+ #
19
+ # returns an array of EMA-Objects
20
+ #
21
+
22
+ class SimpleMA < Base
23
+ def initialize period: 15, data: [], strict: false
24
+
25
+ super
26
+
27
+ data.map{|d| add_item d }
28
+ end
29
+
30
+ # adds item, calculates the sma, puts value to the buffer and returns the result
31
+ def add_item value
32
+
33
+ @queue << value.to_f
34
+ @queue.shift if @buffer.size > @period
35
+
36
+ @buffer << @queue.sum / @queue.size # @queue.sum is always a float.
37
+
38
+ current # return the last buffer value
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,25 @@
1
+ module TechnicalAnalysis
2
+ module MovingAverage
3
+
4
+ WMA = Struct.new :time, :value
5
+
6
+ class Wma < Base
7
+ def initialize period: 15, data: [], strict: true
8
+ super
9
+ @denominator = (1..@period).sum
10
+ data.map{|d| add_item d }
11
+ end
12
+
13
+ # adds item, calculates the sma, puts value to the buffer and returns the result
14
+ def add_item value
15
+
16
+ @queue << value.to_f
17
+ @queue.shift if @queue.size > @period
18
+ weights = (1..@queue.size).to_a
19
+ nominator = weights.zip(@queue).map { |w, x| w * x }.sum
20
+ @buffer << nominator / @denominator
21
+ current # return the last buffer value
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,85 @@
1
+ module TechnicalAnalysis
2
+
3
+ module MovingAverage
4
+ WMA = Struct.new :time, :value
5
+
6
+
7
+
8
+ # Calculates the weighted moving average
9
+ #
10
+ # Parameter is the data-array.
11
+ #
12
+ # Takes an optional Block. Specify a method name that returns the data item
13
+ #
14
+ # Symbols::Futures.mini_dax.eod( duration: '30 d').map do | z |
15
+ # TechnicalAnalysis::MovingAverage.wma( z ){ :close }
16
+ # end
17
+ #
18
+ def self.wma( current_value=nil, wma_source=[], period=0)
19
+ wma_source.push current_value if current_value.present?
20
+ if wma_source.size < period
21
+ current_value
22
+ else
23
+ wma_source = wma_source[ -period , period ] if wma_source.size > period && !period.zero?
24
+ denominator = wma_source.size * (wma_source.size + 1) / 2.0
25
+
26
+ wma_source.map.with_index do |d, i|
27
+ d = d.send( yield ).to_f if block_given?
28
+ d * (i + 1) / denominator
29
+ end.sum
30
+ end
31
+ end
32
+
33
+
34
+ # calculates the simple moving average of the current value using the data as reference
35
+ #
36
+ # if current_value is
37
+ def self.sma current_value=nil, data=[], period=15
38
+ sma_source = if current_value.present?
39
+ data.push current_value # add it as last element
40
+ else
41
+ data
42
+ end
43
+ sma_source = sma_source[ -period , period ] if sma_source.size > period
44
+ sma_source.sum / sma_source.size .to_f
45
+
46
+ end
47
+
48
+ # https://corporatefinanceinstitute.com/resources/knowledge/trading-investing/kaufmans-adaptive-moving-average-kama/
49
+ def self.kama current_value=nil, kama_source=[], period=10, fast=2, slow= 30, prev_kama= nil
50
+ # kama_source: Array of values (length: period)
51
+ # They are used to calculate trend and volatility
52
+
53
+ # build the SmootingFactor (alpha)
54
+ # fast = 2 / GDperiod(fast) +1 ; slow = 2 / GDperiod(slow) +1
55
+
56
+ # kama_source is updated.
57
+
58
+ smoothConst_fast = 2 / (fast +1).to_f
59
+ smoothConst_slow = 2 / (slow +1).to_f
60
+
61
+ if current_value.present?
62
+ kama_source.push current_value
63
+ else
64
+ current_value = kama_source.last
65
+ end
66
+ if prev_kama.nil?
67
+ kama_source.sum / kama_source.size.to_f # return Average --> sma
68
+ else
69
+ kama_source = kama_source[ -period , period ] if kama_source.size > period
70
+ # define the Effiency Ratio to be used by the "kaufmans Adaptive Moving Average" : kama
71
+ # ER is calculated by dividing the absolute difference between the
72
+ # current price and the price at the beginning of the period by the sum
73
+ # of the absolute difference between each pair of closes during the
74
+ # period.
75
+ # | x(t) - x(t-n) |
76
+ # er = ----------------------------
77
+ # sum | x(i) - x(i-1) |
78
+ er= (kama_source.first - kama_source.last).abs /
79
+ (1..kama_source.size-1).map{|x| (kama_source[x] - kama_source[x-1]).abs }.sum # rescue 1
80
+ alpha = (er * ( smoothConst_fast - smoothConst_slow ) + smoothConst_slow ) ** 2
81
+ prev_kama + alpha * (current_value - prev_kama)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module TechnicalAnalysis
3
+ VERSION = "0.2"
4
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ib-technical-analysis
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Hartmut Bischoff
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ib-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ib-extensions
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-collection_matchers
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - topofocus@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - Guardfile
135
+ - LICENSE
136
+ - README.md
137
+ - bin/console
138
+ - bin/console.yml
139
+ - bin/gateway
140
+ - bin/history.irb
141
+ - ib-technical-analysis.gemspec
142
+ - lib/calculation_helpers.rb
143
+ - lib/ib/bar.rb
144
+ - lib/ta_support.rb
145
+ - lib/technical-analysis.rb
146
+ - lib/technical_analysis/bar_calculation.rb
147
+ - lib/technical_analysis/lane.rb
148
+ - lib/technical_analysis/momentum/lane-stochastic.rb
149
+ - lib/technical_analysis/momentum/rsi.rb
150
+ - lib/technical_analysis/momentum/tsi.rb
151
+ - lib/technical_analysis/moving-average/base.rb
152
+ - lib/technical_analysis/moving-average/exp-ma.rb
153
+ - lib/technical_analysis/moving-average/ka-ma.rb
154
+ - lib/technical_analysis/moving-average/simple-ma.rb
155
+ - lib/technical_analysis/moving-average/weighted-ma.rb
156
+ - lib/technical_analysis/moving_average.rb
157
+ - lib/technical_analysis/version.rb
158
+ homepage: https://ib-ruby.github.io/ib-doc/
159
+ licenses: []
160
+ metadata:
161
+ homepage_uri: https://ib-ruby.github.io/ib-doc/
162
+ source_code_uri: https://github.cm/ib-ruby/ib-technical-analysis
163
+ changelog_uri: https://github.cm/ib-ruby/ib-technical-analysis/changelog.md
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 3.0.0
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubygems_version: 3.2.3
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: ".Tools to perform technical analysis on financial data ."
183
+ test_files: []