ruby-technical-analysis 0.1.1
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.
- checksums.yaml +7 -0
- data/.rubocop.yml +34 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +36 -0
- data/Rakefile +16 -0
- data/lib/ruby-technical-analysis/indicators/bollinger_bands.rb +25 -0
- data/lib/ruby-technical-analysis/indicators/chaikin_money_flow.rb +70 -0
- data/lib/ruby-technical-analysis/indicators/chande_momentum_oscillator.rb +34 -0
- data/lib/ruby-technical-analysis/indicators/commodity_channel_index.rb +64 -0
- data/lib/ruby-technical-analysis/indicators/envelopes_ema.rb +24 -0
- data/lib/ruby-technical-analysis/indicators/intraday_momentum_index.rb +48 -0
- data/lib/ruby-technical-analysis/indicators/macd.rb +47 -0
- data/lib/ruby-technical-analysis/indicators/mass_index.rb +73 -0
- data/lib/ruby-technical-analysis/indicators/pivot_points.rb +23 -0
- data/lib/ruby-technical-analysis/indicators/price_channel.rb +37 -0
- data/lib/ruby-technical-analysis/indicators/qstick.rb +40 -0
- data/lib/ruby-technical-analysis/indicators/rate_of_change.rb +18 -0
- data/lib/ruby-technical-analysis/indicators/relative_momentum_index.rb +66 -0
- data/lib/ruby-technical-analysis/indicators/relative_strength_index.rb +63 -0
- data/lib/ruby-technical-analysis/indicators/stochastic_oscillator.rb +65 -0
- data/lib/ruby-technical-analysis/indicators/volume_oscillator.rb +38 -0
- data/lib/ruby-technical-analysis/indicators/volume_rate_of_change.rb +26 -0
- data/lib/ruby-technical-analysis/indicators/wilders_smoothing.rb +27 -0
- data/lib/ruby-technical-analysis/indicators/williams_percent_r.rb +52 -0
- data/lib/ruby-technical-analysis/moving_averages.rb +85 -0
- data/lib/ruby-technical-analysis/statistical_methods.rb +24 -0
- data/lib/ruby-technical-analysis/version.rb +5 -0
- data/lib/ruby_technical_analysis.rb +8 -0
- data/sig/ruby-technical-analysis.rbs +4 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5daf0842421c382edf92ab3091c0dd0bb07182b78133724bdb5f3a0c602194bb
|
4
|
+
data.tar.gz: 8d7e60360f663a0156dec18fd28112e0f1ab5db87d3d8c1786ffc6d3a443a168
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 47fbd9e1e23df84ded3562c94379e506eb64170407f3aaa104c584fbf8dfb518b1ae0648ae8cc1f6e50bf27674e6cc8faffea056ca2155bf33557b572f98f6ea
|
7
|
+
data.tar.gz: bac115dfd0edf9518aaf6ccd78a017e877ef7085472202b972deb8cbca6984ac045d03ced9c668570cd18bd1ca2ed7f2a08e9026f76d89af5f5cddedbc50735b
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/ClassLength:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/PerceivedComplexity:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Metrics/AbcSize:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Metrics/CyclomaticComplexity:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Metrics/BlockLength:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Naming/VariableNumber:
|
34
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at brad.saterfiel@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Brad Saterfiel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# RubyTechnicalAnalysis
|
2
|
+
|
3
|
+
Lightweight and easy technical analysis toolkit for Ruby.
|
4
|
+
|
5
|
+
[Homepage](https://rubytechnicalanalysis.com), [Chart Demo](https://rubytechnicalanalysis/chart), [Docs](https://rubytechnicalanalysis.com/docs)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem by executing:
|
10
|
+
|
11
|
+
In your Gemfile:
|
12
|
+
|
13
|
+
gem 'ruby-technical-analysis'
|
14
|
+
|
15
|
+
bundle install
|
16
|
+
|
17
|
+
Plain install:
|
18
|
+
|
19
|
+
gem install 'ruby-technical-analysis'
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
All methods inherit from the Array class. Any numerical array will work. Easy. For more
|
24
|
+
information please see [docs on the site](https://rubytechnicalanalysis.com/docs).
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
[Bug reports and pull requests are welcome on GitHub](https://github.com/johnnypaper/ruby-technical-analysis). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/johnnypaper/ruby-technical-analysis/blob/master/CODE_OF_CONDUCT.md).
|
29
|
+
|
30
|
+
## License
|
31
|
+
|
32
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
33
|
+
|
34
|
+
## Code of Conduct
|
35
|
+
|
36
|
+
Everyone interacting in the RubyTechnicalAnalysis project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/johnnypaper/ruby-technical-analysis/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
+
require_relative "../../ruby-technical-analysis/statistical_methods"
|
5
|
+
|
6
|
+
# Bollinger Bands indicator
|
7
|
+
# Returns an array containing the current upper, middle, and lower bands of the series
|
8
|
+
module BollingerBands
|
9
|
+
def bollinger_bands(period)
|
10
|
+
if size < period
|
11
|
+
raise ArgumentError,
|
12
|
+
"Array passed to Bollinger Bands cannot be less than the period argument."
|
13
|
+
end
|
14
|
+
closes = last(period)
|
15
|
+
middle = closes.sma(period)
|
16
|
+
twice_sd = (2 * closes.standard_deviation).truncate(4)
|
17
|
+
upper = twice_sd + middle
|
18
|
+
lower = middle - twice_sd
|
19
|
+
[upper, middle, lower]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Array
|
24
|
+
include BollingerBands
|
25
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Chaikin Money Flow indicator
|
4
|
+
# Returns a current singular value
|
5
|
+
module ChaikinMoneyFlow
|
6
|
+
def chaikin_money_flow(period)
|
7
|
+
highs = []
|
8
|
+
lows = []
|
9
|
+
closes = []
|
10
|
+
volumes = []
|
11
|
+
|
12
|
+
each do |h, l, c, v|
|
13
|
+
highs << h
|
14
|
+
lows << l
|
15
|
+
closes << c
|
16
|
+
volumes << v
|
17
|
+
end
|
18
|
+
|
19
|
+
if highs.size < period
|
20
|
+
raise ArgumentError,
|
21
|
+
"High array passed to Chaikin Money Flow cannot be less than the period argument."
|
22
|
+
end
|
23
|
+
|
24
|
+
if lows.size < period
|
25
|
+
raise ArgumentError,
|
26
|
+
"Low array passed to Chaikin Money Flow cannot be less than the period argument."
|
27
|
+
end
|
28
|
+
|
29
|
+
if closes.size < period
|
30
|
+
raise ArgumentError,
|
31
|
+
"Close array passed to Chaikin Money Flow cannot be less than the period argument."
|
32
|
+
end
|
33
|
+
|
34
|
+
if volumes.size < period
|
35
|
+
raise ArgumentError,
|
36
|
+
"Volume array passed to Chaikin Money Flow cannot be less than the period argument."
|
37
|
+
end
|
38
|
+
|
39
|
+
if size < period
|
40
|
+
raise ArgumentError,
|
41
|
+
"Array passed to Bollinger Bands cannot be less than the period argument."
|
42
|
+
end
|
43
|
+
|
44
|
+
highs = highs.last(period)
|
45
|
+
lows = lows.last(period)
|
46
|
+
closes = closes.last(period)
|
47
|
+
volumes = volumes.last(period)
|
48
|
+
|
49
|
+
num_sum = 0
|
50
|
+
vol_sum = 0
|
51
|
+
|
52
|
+
(0..(period - 1)).each do |i|
|
53
|
+
vol_sum += volumes[i]
|
54
|
+
|
55
|
+
cml = closes[i] - lows[i]
|
56
|
+
hmc = highs[i] - closes[i]
|
57
|
+
hml = highs[i] - lows[i]
|
58
|
+
|
59
|
+
num_sum += ((cml - hmc).to_f / hml) * volumes[i]
|
60
|
+
end
|
61
|
+
|
62
|
+
cmf = num_sum.to_f / vol_sum
|
63
|
+
|
64
|
+
cmf.round(5)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class Array
|
69
|
+
include ChaikinMoneyFlow
|
70
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Chaikin Money Flow indicator
|
4
|
+
# Returns a current singular value
|
5
|
+
module ChandeMomentumOscillator
|
6
|
+
def chande_momentum_oscillator(period)
|
7
|
+
if size < period + 1
|
8
|
+
raise ArgumentError,
|
9
|
+
"Array size is less than the minimum size of the period + 1 for the Chande Momentum Oscillator."
|
10
|
+
end
|
11
|
+
|
12
|
+
closes = last(period + 1)
|
13
|
+
|
14
|
+
up_change_sum = 0
|
15
|
+
down_change_sum = 0
|
16
|
+
|
17
|
+
(1..period).each do |i|
|
18
|
+
if closes[i] >= closes[i - 1]
|
19
|
+
up_change_sum += (closes[i] - closes[i - 1])
|
20
|
+
else
|
21
|
+
down_change_sum += (closes[i - 1] - closes[i])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
up_sum_minus_down_sum = up_change_sum - down_change_sum
|
26
|
+
up_sum_plus_down_sum = up_change_sum + down_change_sum
|
27
|
+
|
28
|
+
((up_sum_minus_down_sum.to_f / up_sum_plus_down_sum) * 100).round(4)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Array
|
33
|
+
include ChandeMomentumOscillator
|
34
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
+
|
5
|
+
# Commodity Channel Index indicator
|
6
|
+
# Returns a current singular value
|
7
|
+
module CommodityChannelIndex
|
8
|
+
def commodity_channel_index(period)
|
9
|
+
min_size = ((period * 2) - 1)
|
10
|
+
|
11
|
+
highs = []
|
12
|
+
lows = []
|
13
|
+
closes = []
|
14
|
+
|
15
|
+
each do |h, l, c|
|
16
|
+
highs << h
|
17
|
+
lows << l
|
18
|
+
closes << c
|
19
|
+
end
|
20
|
+
|
21
|
+
if highs.size < period
|
22
|
+
raise ArgumentError,
|
23
|
+
"High array passed to Chaikin Money Flow cannot be less than the period argument."
|
24
|
+
end
|
25
|
+
|
26
|
+
if lows.size < period
|
27
|
+
raise ArgumentError,
|
28
|
+
"Low array passed to Chaikin Money Flow cannot be less than the period argument."
|
29
|
+
end
|
30
|
+
|
31
|
+
if closes.size < period
|
32
|
+
raise ArgumentError,
|
33
|
+
"Close array passed to Chaikin Money Flow cannot be less than the period argument."
|
34
|
+
end
|
35
|
+
|
36
|
+
highs = highs.last(min_size)
|
37
|
+
lows = lows.last(min_size)
|
38
|
+
closes = closes.last(min_size)
|
39
|
+
|
40
|
+
typical_prices = []
|
41
|
+
tp_sma = []
|
42
|
+
period_sum = 0
|
43
|
+
|
44
|
+
(0..(min_size - 1)).each do |i|
|
45
|
+
typical_prices << (highs[i] + closes[i] + lows[i]) / 3
|
46
|
+
end
|
47
|
+
|
48
|
+
(0..(period - 1)).each do |i|
|
49
|
+
tp_sma << typical_prices[i..(i + period - 1)].sma(period)
|
50
|
+
end
|
51
|
+
|
52
|
+
typical_prices.last(period).each do |tp|
|
53
|
+
period_sum += (tp_sma[-1] - tp).abs
|
54
|
+
end
|
55
|
+
|
56
|
+
ps_next = (period_sum.to_f / period) * 0.015
|
57
|
+
tp_sma_min_tp = typical_prices[-1] - tp_sma[-1]
|
58
|
+
(tp_sma_min_tp.to_f / ps_next)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class Array
|
63
|
+
include CommodityChannelIndex
|
64
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
+
|
5
|
+
# Envelopes EMA indicator
|
6
|
+
# Returns an array of current high, middle and low eema values
|
7
|
+
module EnvelopesEma
|
8
|
+
def envelopes_ema(period, percent)
|
9
|
+
if size < period
|
10
|
+
raise ArgumentError,
|
11
|
+
"Close array passed to Envelopes EMA cannot be less than the period argument."
|
12
|
+
end
|
13
|
+
|
14
|
+
eema = last(period).ema(period)
|
15
|
+
eema_up = (eema.round(3) * ((100 + percent))) / 100
|
16
|
+
eema_down = (eema.round(3) * ((100 - percent))) / 100
|
17
|
+
|
18
|
+
[eema_up.truncate(3), eema.truncate(3), eema_down.truncate(3)]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Array
|
23
|
+
include EnvelopesEma
|
24
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Intraday Momentum Index indicator
|
4
|
+
# Returns a singular current value
|
5
|
+
module IntradayMomentumIndex
|
6
|
+
def intraday_momentum_index(period)
|
7
|
+
opens = []
|
8
|
+
closes = []
|
9
|
+
|
10
|
+
each do |o, c|
|
11
|
+
opens << o
|
12
|
+
closes << c
|
13
|
+
end
|
14
|
+
|
15
|
+
if opens.size < period
|
16
|
+
raise ArgumentError,
|
17
|
+
"Opens array passed to Intraday Momentum Index cannot be less than the period argument."
|
18
|
+
end
|
19
|
+
|
20
|
+
if closes.size < period
|
21
|
+
raise ArgumentError,
|
22
|
+
"Closes array passed to Intraday Momentum Index cannot be less than the period argument."
|
23
|
+
end
|
24
|
+
|
25
|
+
closes = closes.last(period)
|
26
|
+
opens = opens.last(period)
|
27
|
+
|
28
|
+
gsum = 0.0
|
29
|
+
lsum = 0.0
|
30
|
+
|
31
|
+
(0..(period - 1)).each do |i|
|
32
|
+
cmo = (closes[i] - opens[i]).abs
|
33
|
+
if closes[i] > opens[i]
|
34
|
+
gsum += cmo
|
35
|
+
else
|
36
|
+
lsum += cmo
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
gsum_plus_lsum = gsum + lsum
|
41
|
+
|
42
|
+
((gsum.to_f / gsum_plus_lsum) * 100).round(4)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Array
|
47
|
+
include IntradayMomentumIndex
|
48
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
+
|
5
|
+
# Moving Average Convergence Divergence (MACD) indicator
|
6
|
+
# Returns an array of current macd value and signal value
|
7
|
+
module Macd
|
8
|
+
def macd(fast_period, slow_period, signal_period)
|
9
|
+
if size < (slow_period + signal_period)
|
10
|
+
raise ArgumentError,
|
11
|
+
"Closes array passed to MACD cannot be less than the (slow period + signal period) arguments."
|
12
|
+
end
|
13
|
+
|
14
|
+
fast_pct = (2.0 / (fast_period + 1)).truncate(6)
|
15
|
+
slow_pct = (2.0 / (slow_period + 1)).truncate(6)
|
16
|
+
sig_pct = (2.0 / (signal_period + 1)).truncate(6)
|
17
|
+
|
18
|
+
fast_arr = []
|
19
|
+
slow_arr = []
|
20
|
+
|
21
|
+
seed = true
|
22
|
+
each do |i|
|
23
|
+
if seed
|
24
|
+
fast_arr << i
|
25
|
+
slow_arr << i
|
26
|
+
seed = false
|
27
|
+
else
|
28
|
+
fast_arr << ((i * fast_pct) + ((fast_arr[-1]) * (1 - fast_pct))).round(3)
|
29
|
+
slow_arr << ((i * slow_pct) + ((slow_arr[-1]) * (1 - slow_pct))).round(3)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
sig_arr = []
|
34
|
+
|
35
|
+
(0..signal_period - 1).each do |i|
|
36
|
+
sig_arr << (fast_arr[slow_period + i - 1] - slow_arr[slow_period + i - 1]).round(3)
|
37
|
+
end
|
38
|
+
|
39
|
+
signal = ((sig_arr[-1] * sig_pct) + ((sig_arr[-2]) * (1 - sig_pct))).round(3)
|
40
|
+
|
41
|
+
[(fast_arr[-1] - slow_arr[-1]).round(4), signal, (fast_arr[-1] - slow_arr[-1]).round(4) - signal]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Array
|
46
|
+
include Macd
|
47
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
+
|
5
|
+
# Mass Index indicator
|
6
|
+
# Returns a singular current value
|
7
|
+
module MassIndex
|
8
|
+
def mass_index(period)
|
9
|
+
highs = []
|
10
|
+
lows = []
|
11
|
+
|
12
|
+
each do |i|
|
13
|
+
highs << i[0]
|
14
|
+
lows << i[1]
|
15
|
+
end
|
16
|
+
|
17
|
+
if highs.size < period
|
18
|
+
raise ArgumentError,
|
19
|
+
"High array passed to Mass Index cannot be less than (2 * period + 1)."
|
20
|
+
end
|
21
|
+
|
22
|
+
if lows.size < period
|
23
|
+
raise ArgumentError,
|
24
|
+
"Low array passed to Mass Index cannot be less than (2 * period + 1)."
|
25
|
+
end
|
26
|
+
|
27
|
+
full_period = (2 * period + 1)
|
28
|
+
highs = highs.last(full_period)
|
29
|
+
lows = lows.last(full_period)
|
30
|
+
|
31
|
+
hml_arr = []
|
32
|
+
hml_ema_arr = []
|
33
|
+
hml_ema_ema_arr = []
|
34
|
+
|
35
|
+
(0..(highs.size - 1)).each do |i|
|
36
|
+
hml_arr << highs[i] - lows[i]
|
37
|
+
end
|
38
|
+
|
39
|
+
low_multiple = (2.0 / (period + 1)).truncate(4)
|
40
|
+
high_multiple = 1 - low_multiple
|
41
|
+
|
42
|
+
(0..hml_arr.length - 1).each do |i|
|
43
|
+
hml_ema_arr << if i.zero?
|
44
|
+
hml_arr[0].truncate(4)
|
45
|
+
else
|
46
|
+
((hml_arr[i] * low_multiple) + (hml_ema_arr[i - 1] * high_multiple)).truncate(4)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
(0..period + 1).each do |i|
|
51
|
+
hml_ema_ema_arr << if i.zero?
|
52
|
+
hml_ema_arr[period - i - 1]
|
53
|
+
else
|
54
|
+
((hml_ema_arr[period + i - 1] * 0.2) + (hml_ema_ema_arr[-1] * 0.8)).round(4)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
ema_period_two_div_ema_period = []
|
59
|
+
|
60
|
+
mi = 0.0
|
61
|
+
(0..2).each do |i|
|
62
|
+
ema_period_two_div_ema_period <<
|
63
|
+
((hml_ema_arr[(period * 2) + i - 2]) / (hml_ema_ema_arr[period + i - 1])).round(4)
|
64
|
+
mi += ((hml_ema_arr[(period * 2) + i - 2]) / (hml_ema_ema_arr[period + i - 1])).round(4)
|
65
|
+
end
|
66
|
+
|
67
|
+
mi.round(4)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Array
|
72
|
+
include MassIndex
|
73
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Pivot Points indicator
|
4
|
+
# Returns an array of the current pivot points for the provided H, L, C array
|
5
|
+
module PivotPoints
|
6
|
+
def pivot_points
|
7
|
+
h = at(0)
|
8
|
+
l = at(1)
|
9
|
+
c = at(2)
|
10
|
+
pp = ((h + l + c) / 3.0).round(2)
|
11
|
+
r1 = ((pp * 2) - l).round(2)
|
12
|
+
s1 = ((pp * 2) - h).round(2)
|
13
|
+
r2 = (pp + (h - l)).round(2)
|
14
|
+
s2 = (pp - (h - l)).round(2)
|
15
|
+
r3 = (h + (2 * (pp - l))).round(2)
|
16
|
+
s3 = (l - (2 * (h - pp))).round(2)
|
17
|
+
[s3, s2, s1, pp, r1, r2, r3]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Array
|
22
|
+
include PivotPoints
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Price Channel indicator
|
4
|
+
# Returns an array containing the current upper and lower values of the series
|
5
|
+
module PriceChannel
|
6
|
+
def price_channel(period)
|
7
|
+
highs = []
|
8
|
+
lows = []
|
9
|
+
|
10
|
+
each do |i|
|
11
|
+
highs << i[0]
|
12
|
+
lows << i[1]
|
13
|
+
end
|
14
|
+
|
15
|
+
if highs.size < period + 1
|
16
|
+
raise ArgumentError,
|
17
|
+
"The highs array size is less than the period + 1 size required."
|
18
|
+
end
|
19
|
+
|
20
|
+
if lows.size < period + 1
|
21
|
+
raise ArgumentError,
|
22
|
+
"The lows array size is less than the period + 1 size required."
|
23
|
+
end
|
24
|
+
|
25
|
+
highs = highs.last(period + 1)
|
26
|
+
lows = lows.last(period + 1)
|
27
|
+
|
28
|
+
upper_pc = (highs[0..period - 1]).max
|
29
|
+
lower_pc = (lows[0..period - 1]).min
|
30
|
+
|
31
|
+
[upper_pc, lower_pc]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Array
|
36
|
+
include PriceChannel
|
37
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Qstick indicator
|
4
|
+
# Returns a single value
|
5
|
+
module Qstick
|
6
|
+
def qstick(period)
|
7
|
+
opens = []
|
8
|
+
closes = []
|
9
|
+
|
10
|
+
each do |i|
|
11
|
+
opens << i[0]
|
12
|
+
closes << i[1]
|
13
|
+
end
|
14
|
+
|
15
|
+
if opens.size < period
|
16
|
+
raise ArgumentError,
|
17
|
+
"Opens array passed to Qstick cannot be less than the period argument."
|
18
|
+
end
|
19
|
+
|
20
|
+
if closes.size < period
|
21
|
+
raise ArgumentError,
|
22
|
+
"Closes array passed to Qstick cannot be less than the period argument."
|
23
|
+
end
|
24
|
+
|
25
|
+
opens = opens.last(period)
|
26
|
+
closes = closes.last(period)
|
27
|
+
|
28
|
+
cmo_sum = 0.0
|
29
|
+
|
30
|
+
(0..(period - 1)).each do |i|
|
31
|
+
cmo_sum += closes[i] - opens[i]
|
32
|
+
end
|
33
|
+
|
34
|
+
(cmo_sum.to_f / period).round(4)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Array
|
39
|
+
include Qstick
|
40
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# RateOfChange indicator
|
4
|
+
# Returns a single value
|
5
|
+
module RateOfChange
|
6
|
+
def rate_of_change(period)
|
7
|
+
if size < (period + 1)
|
8
|
+
raise ArgumentError,
|
9
|
+
"Closes array passed to RateOfChange cannot be less than the period argument + 1."
|
10
|
+
end
|
11
|
+
|
12
|
+
(((self[-1] - last(period + 1)[0]).to_f / last(period + 1)[0]) * 100).round(2)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Array
|
17
|
+
include RateOfChange
|
18
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "wilders_smoothing"
|
4
|
+
|
5
|
+
# Relative Momentum Index indicator
|
6
|
+
# Returns a single value
|
7
|
+
module RelativeMomentumIndex
|
8
|
+
def relative_momentum_index(period_mom, period_rmi)
|
9
|
+
pmpr = period_mom + period_rmi
|
10
|
+
|
11
|
+
if size < pmpr
|
12
|
+
raise ArgumentError,
|
13
|
+
"Closes array passed to Relative Momentum Index cannot be less than the period mom + period rmi arguments."
|
14
|
+
end
|
15
|
+
|
16
|
+
rmi = []
|
17
|
+
rmi_intermediate = []
|
18
|
+
wilders_is_set = false
|
19
|
+
smooth_up = []
|
20
|
+
smooth_down = []
|
21
|
+
|
22
|
+
smooth_coef_one = (1.0 / period_rmi).round(4)
|
23
|
+
smooth_coef_two = (1 - smooth_coef_one)
|
24
|
+
|
25
|
+
(0..(size - pmpr)).each do |i|
|
26
|
+
cla = self[i..(i + pmpr - 1)]
|
27
|
+
up_ch = []
|
28
|
+
down_ch = []
|
29
|
+
|
30
|
+
(0..period_rmi - 1).each do |m|
|
31
|
+
cur_close = cla[m]
|
32
|
+
prev_close = cla[period_mom + m]
|
33
|
+
diff = (cur_close - prev_close).round(4)
|
34
|
+
|
35
|
+
if diff.negative?
|
36
|
+
up_ch << diff.abs
|
37
|
+
down_ch << 0.00
|
38
|
+
elsif diff.positive?
|
39
|
+
up_ch << 0.00
|
40
|
+
down_ch << diff
|
41
|
+
else
|
42
|
+
up_ch << 0.00
|
43
|
+
down_ch << 0.00
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
if wilders_is_set
|
48
|
+
smooth_up << (smooth_coef_one * up_ch[-1] + smooth_coef_two * smooth_up[-1]).round(4)
|
49
|
+
smooth_down << (smooth_coef_one * down_ch[-1] + smooth_coef_two * smooth_down[-1]).round(4)
|
50
|
+
else
|
51
|
+
smooth_up << up_ch.wilders_smoothing(period_rmi)
|
52
|
+
smooth_down << down_ch.wilders_smoothing(period_rmi)
|
53
|
+
wilders_is_set = true
|
54
|
+
end
|
55
|
+
|
56
|
+
rmi_intermediate << (smooth_up[-1].to_f / smooth_down[-1])
|
57
|
+
rmi << ((rmi_intermediate[-1].to_f / (1 + rmi_intermediate[-1])) * 100).round(4)
|
58
|
+
end
|
59
|
+
|
60
|
+
rmi[-1]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Array
|
65
|
+
include RelativeMomentumIndex
|
66
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "wilders_smoothing"
|
4
|
+
|
5
|
+
# Relative Momentum Index indicator
|
6
|
+
# Returns a single value
|
7
|
+
module RelativeStrengthIndex
|
8
|
+
def rsi(period)
|
9
|
+
if size < (period + 1)
|
10
|
+
raise ArgumentError,
|
11
|
+
"Closes array passed to Relative Strength Index cannot be less than the period + 1 argument."
|
12
|
+
end
|
13
|
+
|
14
|
+
rsi = []
|
15
|
+
wilders_is_set = false
|
16
|
+
smooth_up = []
|
17
|
+
smooth_down = []
|
18
|
+
|
19
|
+
smooth_coef_one = (1.0 / period).round(4)
|
20
|
+
smooth_coef_two = (1 - smooth_coef_one)
|
21
|
+
|
22
|
+
(0..(size - period - 1)).each do |k|
|
23
|
+
cla = self[k..k + period]
|
24
|
+
|
25
|
+
up_ch = []
|
26
|
+
down_ch = []
|
27
|
+
|
28
|
+
(1..period).each do |i|
|
29
|
+
cur_close = cla[i]
|
30
|
+
prev_close = cla[i - 1]
|
31
|
+
close_diff = cur_close - prev_close
|
32
|
+
|
33
|
+
if close_diff > 0.00
|
34
|
+
up_ch << close_diff
|
35
|
+
down_ch << 0.00
|
36
|
+
elsif close_diff < 0.00
|
37
|
+
up_ch << 0.00
|
38
|
+
down_ch << close_diff.abs
|
39
|
+
else
|
40
|
+
up_ch << 0.00
|
41
|
+
down_ch << 0.00
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
if wilders_is_set
|
46
|
+
smooth_up << (smooth_coef_one * up_ch[-1] + smooth_coef_two * smooth_up[-1]).round(4)
|
47
|
+
smooth_down << (smooth_coef_one * down_ch[-1] + smooth_coef_two * smooth_down[-1]).round(4)
|
48
|
+
else
|
49
|
+
smooth_up << up_ch.last(period).wilders_smoothing(period)
|
50
|
+
smooth_down << down_ch.last(period).wilders_smoothing(period)
|
51
|
+
wilders_is_set = true
|
52
|
+
end
|
53
|
+
|
54
|
+
rsi << (100.00 - (100.00 / ((smooth_up[-1].to_f / smooth_down[-1]) + 1))).round(4)
|
55
|
+
end
|
56
|
+
|
57
|
+
rsi[-1]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class Array
|
62
|
+
include RelativeStrengthIndex
|
63
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Stochastic Oscillator indicator
|
4
|
+
# Returns a single value
|
5
|
+
module StochasticOscillator
|
6
|
+
def stochastic_oscillator(k_periods, k_slow_periods, d_periods)
|
7
|
+
highs = []
|
8
|
+
lows = []
|
9
|
+
closes = []
|
10
|
+
|
11
|
+
each do |i|
|
12
|
+
highs << i[0]
|
13
|
+
lows << i[1]
|
14
|
+
closes << i[2]
|
15
|
+
end
|
16
|
+
|
17
|
+
if highs.size < k_periods
|
18
|
+
raise ArgumentError,
|
19
|
+
"High array passed to Stochastic Oscillator cannot be less than the k_periods argument."
|
20
|
+
end
|
21
|
+
|
22
|
+
if lows.size < k_periods
|
23
|
+
raise ArgumentError,
|
24
|
+
"Low array passed to Stochastic Oscillator cannot be less than the k_periods argument."
|
25
|
+
end
|
26
|
+
|
27
|
+
if closes.size < k_periods
|
28
|
+
raise ArgumentError,
|
29
|
+
"Close array passed to Stochastic Oscillator cannot be less than the k_periods argument."
|
30
|
+
end
|
31
|
+
|
32
|
+
lowest_lows = []
|
33
|
+
highest_highs = []
|
34
|
+
close_minus_ll = []
|
35
|
+
hh_minus_ll = []
|
36
|
+
|
37
|
+
ks_sums_close_min_ll = []
|
38
|
+
ks_sums_hh_min_ll = []
|
39
|
+
ks_div_x_100 = []
|
40
|
+
d_periods_sma = []
|
41
|
+
|
42
|
+
(0..(highs.length - k_periods)).each do |i|
|
43
|
+
lowest_lows << lows[i..(i + k_periods - 1)].min
|
44
|
+
highest_highs << highs[i..(i + k_periods - 1)].max
|
45
|
+
close_minus_ll << (closes[i + k_periods - 1] - lowest_lows.last).round(4)
|
46
|
+
hh_minus_ll << (highest_highs.last - lowest_lows.last).round(4)
|
47
|
+
if close_minus_ll.length >= k_slow_periods
|
48
|
+
ks_sums_close_min_ll << close_minus_ll.last(k_slow_periods).inject(:+).round(4)
|
49
|
+
ks_sums_hh_min_ll << hh_minus_ll.last(k_slow_periods).inject(:+).round(4)
|
50
|
+
ks_div_x_100 << ((ks_sums_close_min_ll.last.to_f / ks_sums_hh_min_ll.last) * 100).round(4)
|
51
|
+
end
|
52
|
+
d_periods_sma << if ks_div_x_100.length >= d_periods
|
53
|
+
(ks_div_x_100.last(d_periods).reduce(:+).to_f / d_periods).round(4)
|
54
|
+
else
|
55
|
+
-1000
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
d_periods_sma[-1]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Array
|
64
|
+
include StochasticOscillator
|
65
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
+
|
5
|
+
# Volume Oscillator indicator
|
6
|
+
# Returns a single value
|
7
|
+
module VolumeOscillator
|
8
|
+
def volume_oscillator(short_ma, long_ma)
|
9
|
+
if size < long_ma
|
10
|
+
raise ArgumentError,
|
11
|
+
"Volumes array passed to Volume Oscillator cannot be less than the Long Moving Average argument."
|
12
|
+
end
|
13
|
+
|
14
|
+
if long_ma <= short_ma
|
15
|
+
raise ArgumentError,
|
16
|
+
"Long Moving Average parameter must be greater than Short Moving Average parameter in Volume Oscillator."
|
17
|
+
end
|
18
|
+
|
19
|
+
short_ma_a = []
|
20
|
+
long_ma_a = []
|
21
|
+
|
22
|
+
(0..(length - short_ma)).each do |i|
|
23
|
+
short_ma_a << self[i..(i + short_ma - 1)].sma(short_ma)
|
24
|
+
end
|
25
|
+
|
26
|
+
(0..(length - long_ma)).each do |i|
|
27
|
+
long_ma_a << self[i..(i + long_ma - 1)].sma(long_ma)
|
28
|
+
end
|
29
|
+
|
30
|
+
sml = (short_ma_a.last - long_ma_a.last).round(2)
|
31
|
+
|
32
|
+
((sml.to_f / long_ma_a.last) * 100).round(2)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Array
|
37
|
+
include VolumeOscillator
|
38
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Volume Rate of Change indicator
|
4
|
+
# Returns a single value
|
5
|
+
module VolumeRateOfChange
|
6
|
+
def volume_rate_of_change(period)
|
7
|
+
if size < (period + 1)
|
8
|
+
raise ArgumentError,
|
9
|
+
"Volumes array passed to Volume Rate of Change cannot be less than period parameter + 1."
|
10
|
+
end
|
11
|
+
|
12
|
+
delta_volume = []
|
13
|
+
vol_shifted = []
|
14
|
+
|
15
|
+
(0..(length - period - 1)).each do
|
16
|
+
vol_shifted << at(length - period - 1)
|
17
|
+
delta_volume << at(-1) - vol_shifted[-1]
|
18
|
+
end
|
19
|
+
|
20
|
+
((delta_volume[-1].to_f / vol_shifted[-1]) * 100).round(4)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Array
|
25
|
+
include VolumeRateOfChange
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../ruby-technical-analysis/moving_averages"
|
4
|
+
|
5
|
+
# Wilders Smoothing indicator
|
6
|
+
# Returns a singular current value
|
7
|
+
module WildersSmoothing
|
8
|
+
def wilders_smoothing(period)
|
9
|
+
if size < period
|
10
|
+
raise ArgumentError,
|
11
|
+
"Closes array passed to Wilders Smoothing cannot be less than the period argument."
|
12
|
+
end
|
13
|
+
|
14
|
+
ws = []
|
15
|
+
ws << first(period).sma(period)
|
16
|
+
|
17
|
+
(0..(size - period - 1)).each do |i|
|
18
|
+
ws << ((at(i + period) - ws[i]) * (1.0 / period)) + ws[i]
|
19
|
+
end
|
20
|
+
|
21
|
+
ws[-1]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Array
|
26
|
+
include WildersSmoothing
|
27
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Williams Percent R indicator
|
4
|
+
# Returns a single value
|
5
|
+
module WilliamsPercentR
|
6
|
+
def williams_percent_r(period)
|
7
|
+
highs = []
|
8
|
+
lows = []
|
9
|
+
closes = []
|
10
|
+
|
11
|
+
each do |i|
|
12
|
+
highs << i[0]
|
13
|
+
lows << i[1]
|
14
|
+
closes << i[2]
|
15
|
+
end
|
16
|
+
|
17
|
+
if highs.size < period
|
18
|
+
raise ArgumentError,
|
19
|
+
"High array passed to Williams Percent R cannot be less than the period argument."
|
20
|
+
end
|
21
|
+
|
22
|
+
if lows.size < period
|
23
|
+
raise ArgumentError,
|
24
|
+
"Low array passed to Williams Percent R cannot be less than the period argument."
|
25
|
+
end
|
26
|
+
|
27
|
+
if closes.size < period
|
28
|
+
raise ArgumentError,
|
29
|
+
"Close array passed to Williams Percent R cannot be less than the period argument."
|
30
|
+
end
|
31
|
+
|
32
|
+
highest_highs = []
|
33
|
+
lowest_lows = []
|
34
|
+
hh_min_close = []
|
35
|
+
hh_min_ll = []
|
36
|
+
pct_r = []
|
37
|
+
|
38
|
+
(0..highs.length - period).each do |i|
|
39
|
+
highest_highs << highs[i..period - 1 + i].max
|
40
|
+
lowest_lows << lows[i..period - 1 + i].min
|
41
|
+
hh_min_close << (highest_highs[-1] - closes[period - 1 + i]).round(2)
|
42
|
+
hh_min_ll << (highest_highs[-1] - lowest_lows[-1]).round(2)
|
43
|
+
pct_r << ((hh_min_close[-1].to_f / hh_min_ll[-1]) * -100).round(2)
|
44
|
+
end
|
45
|
+
|
46
|
+
pct_r[-1]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class Array
|
51
|
+
include WilliamsPercentR
|
52
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# module MovingAverages contains some common moving averages and those used in other technical functions
|
4
|
+
# it is built upon the array class and all methods assume the use of an array
|
5
|
+
module MovingAverages
|
6
|
+
# Simple Moving Average
|
7
|
+
def sma(period)
|
8
|
+
if period <= 0
|
9
|
+
raise ArgumentError,
|
10
|
+
"The period parameter cannot be less than 1."
|
11
|
+
end
|
12
|
+
|
13
|
+
if size < period
|
14
|
+
raise ArgumentError,
|
15
|
+
"The array size is less than the period size for the simple moving average."
|
16
|
+
end
|
17
|
+
|
18
|
+
(last(period).reduce(:+).to_f / period)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Exponential Moving Average
|
22
|
+
def ema(period)
|
23
|
+
if period < 1
|
24
|
+
raise ArgumentError,
|
25
|
+
"The period parameter cannot be less than 1."
|
26
|
+
end
|
27
|
+
if size < period
|
28
|
+
raise ArgumentError,
|
29
|
+
"The array size is less than the period size for the exponential moving average."
|
30
|
+
end
|
31
|
+
|
32
|
+
if period == 1
|
33
|
+
last
|
34
|
+
else
|
35
|
+
case period
|
36
|
+
when 12
|
37
|
+
last_obs_pct = 0.846154
|
38
|
+
ma_pct = 0.153846
|
39
|
+
when 26
|
40
|
+
last_obs_pct = 0.925926
|
41
|
+
ma_pct = 0.074074
|
42
|
+
else
|
43
|
+
last_obs_pct = 2.0 / (period + 1)
|
44
|
+
ma_pct = 1.0 - last_obs_pct
|
45
|
+
end
|
46
|
+
|
47
|
+
ma_array = []
|
48
|
+
|
49
|
+
last(period).each_with_index do |num, i|
|
50
|
+
ma_array << if i.zero?
|
51
|
+
num
|
52
|
+
else
|
53
|
+
(num * last_obs_pct) + (ma_array[i - 1] * ma_pct)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ma_array.last
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Weighted Moving Average
|
62
|
+
def wma(period)
|
63
|
+
if period <= 0 || size < period
|
64
|
+
raise ArgumentError,
|
65
|
+
"The period of the weighted moving average supplied must be greater than 0 and less than or equal to the
|
66
|
+
size of the array."
|
67
|
+
end
|
68
|
+
|
69
|
+
true_periods = 0
|
70
|
+
(1..period).each do |i|
|
71
|
+
true_periods += i
|
72
|
+
end
|
73
|
+
|
74
|
+
sigma_periods = 0.0
|
75
|
+
last(period).each_with_index do |num, i|
|
76
|
+
sigma_periods += ((i + 1) * num)
|
77
|
+
end
|
78
|
+
|
79
|
+
(sigma_periods.to_f / true_periods)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class Array
|
84
|
+
include MovingAverages
|
85
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Statistical methods used in calculations
|
4
|
+
module StatisticalMethods
|
5
|
+
def standard_deviation
|
6
|
+
if size <= 1
|
7
|
+
raise ArgumentError,
|
8
|
+
"Array must contain at least 2 numbers for standard deviation."
|
9
|
+
end
|
10
|
+
|
11
|
+
mean = reduce(:+).to_f / size
|
12
|
+
sq_dist = 0
|
13
|
+
|
14
|
+
each do |n|
|
15
|
+
sq_dist += (n - mean).abs2
|
16
|
+
end
|
17
|
+
|
18
|
+
Math.sqrt(sq_dist.to_f / size)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Array
|
23
|
+
include StatisticalMethods
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-technical-analysis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brad Saterfiel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-04-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- brad.saterfiel@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rubocop.yml"
|
21
|
+
- CHANGELOG.md
|
22
|
+
- CODE_OF_CONDUCT.md
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/ruby-technical-analysis/indicators/bollinger_bands.rb
|
28
|
+
- lib/ruby-technical-analysis/indicators/chaikin_money_flow.rb
|
29
|
+
- lib/ruby-technical-analysis/indicators/chande_momentum_oscillator.rb
|
30
|
+
- lib/ruby-technical-analysis/indicators/commodity_channel_index.rb
|
31
|
+
- lib/ruby-technical-analysis/indicators/envelopes_ema.rb
|
32
|
+
- lib/ruby-technical-analysis/indicators/intraday_momentum_index.rb
|
33
|
+
- lib/ruby-technical-analysis/indicators/macd.rb
|
34
|
+
- lib/ruby-technical-analysis/indicators/mass_index.rb
|
35
|
+
- lib/ruby-technical-analysis/indicators/pivot_points.rb
|
36
|
+
- lib/ruby-technical-analysis/indicators/price_channel.rb
|
37
|
+
- lib/ruby-technical-analysis/indicators/qstick.rb
|
38
|
+
- lib/ruby-technical-analysis/indicators/rate_of_change.rb
|
39
|
+
- lib/ruby-technical-analysis/indicators/relative_momentum_index.rb
|
40
|
+
- lib/ruby-technical-analysis/indicators/relative_strength_index.rb
|
41
|
+
- lib/ruby-technical-analysis/indicators/stochastic_oscillator.rb
|
42
|
+
- lib/ruby-technical-analysis/indicators/volume_oscillator.rb
|
43
|
+
- lib/ruby-technical-analysis/indicators/volume_rate_of_change.rb
|
44
|
+
- lib/ruby-technical-analysis/indicators/wilders_smoothing.rb
|
45
|
+
- lib/ruby-technical-analysis/indicators/williams_percent_r.rb
|
46
|
+
- lib/ruby-technical-analysis/moving_averages.rb
|
47
|
+
- lib/ruby-technical-analysis/statistical_methods.rb
|
48
|
+
- lib/ruby-technical-analysis/version.rb
|
49
|
+
- lib/ruby_technical_analysis.rb
|
50
|
+
- sig/ruby-technical-analysis.rbs
|
51
|
+
homepage: https://rubytechnicalanalysis.com
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
homepage_uri: https://rubytechnicalanalysis.com
|
56
|
+
source_code_uri: https://github.com/johnnypaper/ruby-technical-analysis
|
57
|
+
changelog_uri: https://github.com/johnnypaper/ruby-technical-analysis/blob/master/CHANGELOG.md
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 2.6.0
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.2.32
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Lightweight and flexible technical analysis toolkit for Ruby.
|
77
|
+
test_files: []
|