Class: TechnicalAnalysis::Tsi

Inherits:
Indicator show all
Defined in:
lib/technical_analysis/indicators/tsi.rb

Overview

True Strength Index

Class Method Summary collapse

Methods inherited from Indicator

find, roster

Class Method Details

.calculate(data, low_period: 13, high_period: 25, price_key: :value) ⇒ Array<TsiValue>

Calculates the true strength index (TSI) for the data over the given period en.wikipedia.org/wiki/True_strength_index



54
55
56
57
58
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
95
96
97
98
99
100
101
102
# File 'lib/technical_analysis/indicators/tsi.rb', line 54

def self.calculate(data, low_period: 13, high_period: 25, price_key: :value)
  low_period = low_period.to_i
  high_period = high_period.to_i
  price_key = price_key.to_sym
  Validation.validate_numeric_data(data, price_key)
  Validation.validate_length(data, min_data_size(low_period: low_period, high_period: high_period))
  Validation.validate_date_time_key(data)

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

  high_emas = []
  high_multiplier = (2.0 / (high_period + 1.0))
  low_emas = []
  low_multiplier = (2.0 / (low_period + 1.0))
  momentum_values = []
  output = []
  prev_price = data.shift[price_key]

  data.each do |v|
    current_price = v[price_key]
    momentum = current_price - prev_price
    momentum_hash = { value: momentum, abs_value: momentum.abs }

    momentum_values << momentum_hash

    if momentum_values.size == high_period
      high_emas << process_ema(momentum_hash, momentum_values, high_multiplier, high_period, high_emas)

      if high_emas.size == low_period
        low_ema = process_ema(high_emas.last, high_emas, low_multiplier, low_period, low_emas)
        low_emas << low_ema

        output << TsiValue.new(
          date_time: v[:date_time],
          tsi: ((100 * (low_ema[:value] / low_ema[:abs_value])))
        )

        low_emas.shift if low_emas.size > 1 # Only need to retain the last low_ema
        high_emas.shift
      end

      momentum_values.shift
    end

    prev_price = current_price
  end
  
  output.sort_by(&:date_time).reverse
end

.indicator_nameString

Returns the name of the technical indicator



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

def self.indicator_name
  "True Strength Index"
end

.indicator_symbolString

Returns the symbol of the technical indicator



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

def self.indicator_symbol
  "tsi"
end

.min_data_size(low_period: 13, high_period: 25, **params) ⇒ Integer

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



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

def self.min_data_size(low_period: 13, high_period: 25, **params)
  low_period.to_i + high_period.to_i
end

.valid_optionsArray

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



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

def self.valid_options
  i(low_period high_period price_key)
end

.validate_options(options) ⇒ Boolean

Validates the provided options for this technical indicator



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

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