Class: TechnicalAnalysis::Momentum::Tsi

Inherits:
Object
  • Object
show all
Defined in:
lib/technical_analysis/momentum/tsi.rb

Instance Method Summary collapse

Constructor Details

#initialize(low: 13, high: 25, data: [], strict: true) ⇒ Tsi

ExpMA(low) ( ExpMA(high ) prices )

TSI  =   ----------------------------------------------
          ExpMA(low) ( ExpMA(high ) prices.abs  )

While the TSI output is bound between 1 and −1, most values fall between 0.25 and −0.25. Blau suggests interpreting these values as overbought and oversold levels, respectively, at which point a trader may anticipate a market turn.

Trend direction is indicated by the slope of the TSI; a rising TSI suggests an up-trend in the market, and a falling TSI suggests a down-trend



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/technical_analysis/momentum/tsi.rb', line 21

def initialize low: 13, high: 25, data: [], strict: true
  # strict is always true

  raise "Periods must be greater then one" if  low <= 1 || high <= 1
  raise "high must be greater then low" if  low > high 

  @queue = []
  @buffer = []
  @low= low
  @high = high

  @emas = { high:  TechnicalAnalysis::MovingAverage::ExpMA.new( period: high, strict: true ),
            low:       TechnicalAnalysis::MovingAverage::ExpMA.new( period: low, strict: true  ),
            high_abs:  TechnicalAnalysis::MovingAverage::ExpMA.new( period: high, strict: true  ),
            low_abs:   TechnicalAnalysis::MovingAverage::ExpMA.new(  period: low, strict: true )  }

  if !data.empty?
    data.map{|d| add_item d }
  end
end

Instance Method Details

#add_item(value) ⇒ Object

adds item, calculates the ema, puts value to the buffer and returns the result



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/technical_analysis/momentum/tsi.rb', line 43

def add_item  value
  @queue << value.to_f

  if @queue.size < 2
    @buffer << nil
  else
    momentum =  @queue.last - @queue[-2]
    @emas[:high].add_item momentum
    @emas[:high_abs].add_item momentum.abs

    if @emas[:high].current.present?
      @emas[:low].add_item @emas[:high].current
      @emas[:low_abs].add_item @emas[:high_abs].current
    end

    #puts "momentum:  #{@emas[:low].current} <--> #{@emas[:low_abs].current}"
    if @emas[:low].current.present?   # warmup perioda is over
      @buffer << @emas[:low].current / @emas[:low_abs].current
    end

    current  #  return the last buffer value
  end
end

#currentObject

returns the ema if the last computed item



73
74
75
# File 'lib/technical_analysis/momentum/tsi.rb', line 73

def current
   @buffer.last
end

#momentumObject

returns the ema-buffer



68
69
70
# File 'lib/technical_analysis/momentum/tsi.rb', line 68

def momentum 
  @buffer
end