Class: TechnicalAnalysis::Ichimoku

Inherits:
Indicator
  • Object
show all
Defined in:
lib/technical_analysis/indicators/ichimoku.rb

Overview

Ichimoku Kinko Hyo

Class Method Summary collapse

Methods inherited from Indicator

find, roster

Class Method Details

.calculate(data, low_period: 9, medium_period: 26, high_period: 52) ⇒ Array<IchimokuValue>

Calculates the 5 points of Ichimoku Kinko Hyo (Ichimoku) for the data over the given period

1. tenkan_sen    (Conversion Line)
2. kijun_sen     (Base Line)
3. senkou_span_a (Leading Span A)
4. senkou_span_b (Leading Span B)
5. chickou_span  (Lagging Span)

en.wikipedia.org/wiki/Ichimoku_Kink%C5%8D_Hy%C5%8D

Parameters:

  • Array of hashes with keys (:date_time, :high, :low, :close)

  • (defaults to: 9)

    The given period to calculate tenkan_sen (Conversion Line)

  • (defaults to: 26)

    The given period to calculate kijun_sen (Base Line), senkou_span_a (Leading Span A), and chikou_span (Lagging Span)

  • (defaults to: 52)

    The given period to calculate senkou_span_b (Leading Span B)

Returns:

  • An array of IchimokuValue instances



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/technical_analysis/indicators/ichimoku.rb', line 59

def self.calculate(data, low_period: 9, medium_period: 26, high_period: 52)
  low_period = low_period.to_i
  medium_period = medium_period.to_i
  high_period = high_period.to_i
  Validation.validate_numeric_data(data, :high, :low, :close)
  Validation.validate_length(data, min_data_size(high_period: high_period, medium_period: medium_period))
  Validation.validate_date_time_key(data)

  data = data.sort_by { |row| row[:date_time] }

  index = high_period + medium_period - 2
  output = []

  while index < data.size
    date_time = data[index][:date_time]

    tenkan_sen = calculate_midpoint(index, low_period, data)
    kinjun_sen = calculate_midpoint(index, medium_period, data)
    senkou_span_a = calculate_senkou_span_a(index, low_period, medium_period, data)
    senkou_span_b = calculate_senkou_span_b(index, medium_period, high_period, data)
    chikou_span = calculate_chikou_span(index, medium_period, data)

    output << IchimokuValue.new(
      date_time: date_time,
      tenkan_sen: tenkan_sen,
      kijun_sen: kinjun_sen,
      senkou_span_a: senkou_span_a,
      senkou_span_b: senkou_span_b,
      chikou_span: chikou_span
    )

    index += 1
  end

  output.sort_by(&:date_time).reverse
end

.indicator_nameString

Returns the name of the technical indicator

Returns:

  • A string of the name of the technical indicator



15
16
17
# File 'lib/technical_analysis/indicators/ichimoku.rb', line 15

def self.indicator_name
  "Ichimoku Kinko Hyo"
end

.indicator_symbolString

Returns the symbol of the technical indicator

Returns:

  • A string of the symbol of the technical indicator



8
9
10
# File 'lib/technical_analysis/indicators/ichimoku.rb', line 8

def self.indicator_symbol
  "ichimoku"
end

.min_data_size(medium_period: 26, high_period: 52, **params) ⇒ Integer

Calculates the minimum number of observations needed to calculate the technical indicator

Parameters:

  • The options for the technical indicator

Returns:

  • Returns the minimum number of observations needed to calculate the technical indicator based on the options provided



41
42
43
# File 'lib/technical_analysis/indicators/ichimoku.rb', line 41

def self.min_data_size(medium_period: 26, high_period: 52, **params)
  high_period.to_i + medium_period.to_i - 1
end

.valid_optionsArray

Returns an array of valid keys for options for this technical indicator

Returns:

  • An array of keys as symbols for valid options for this technical indicator



22
23
24
# File 'lib/technical_analysis/indicators/ichimoku.rb', line 22

def self.valid_options
  i(low_period medium_period high_period)
end

.validate_options(options) ⇒ Boolean

Validates the provided options for this technical indicator

Parameters:

  • The options for the technical indicator to be validated

Returns:

  • Returns true if options are valid or raises a ValidationError if they’re not



31
32
33
# File 'lib/technical_analysis/indicators/ichimoku.rb', line 31

def self.validate_options(options)
  Validation.validate_options(options, valid_options)
end