Class: TechnicalAnalysis::Momentum::Lane

Inherits:
Object
  • Object
show all
Defined in:
lib/technical_analysis/momentum/lane-stochastic.rb

Overview

Defines the Lane-Stochastics

         price   -     min(low)[period]
k =   -----------------------------------------------
        max(high)[period] -  min(low)[period]

The Stochastics may depend on

  • OHLC-Objects (IB::Bar).

Then “close” is taken as default-value of the current candle.

By specifiying the parameter “take” this can be customized.

Instance Method Summary collapse

Constructor Details

#initialize(period: 5, fast: 3, slow: 3, data: [], strict: true, take: :close) ⇒ Lane

attr :oversold, true

attr :overbought, true


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/technical_analysis/momentum/lane-stochastic.rb', line 24

def initialize period: 5,  fast: 3, slow: 3, data: [], strict: true, take: :close
  # note: strict is always true
  raise "Periods must be greater then one" if  period <= 1
  raise "fast must be smaller then slow" if  slow < fast 

  @queue = []
  @buffer = []
  @take =  take
  @smooth_2 = slow               # smooth-Const of StochasticsSlow
  @smooth_1 = fast               # smooth-Const of StochasticsFast
  @period = period               # period       of StochasticsLane
  @stochastics_fast = TechnicalAnalysis::MovingAverage::ExpMA.new( period: @smooth_1, strict: true)
  @stochastics_slow = TechnicalAnalysis::MovingAverage::ExpMA.new( period: @smooth_2, 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



45
46
47
48
49
50
51
52
53
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
# File 'lib/technical_analysis/momentum/lane-stochastic.rb', line 45

def add_item  value
  is_ohlc = nil
  @queue << if value.is_a? IB::Bar
              is_ohlc = true 
              value
  else
    value.to_f
  end

  if @queue.size < @period
    @buffer << nil
  else
    @queue.shift if @queue.size > @period
    the_high = is_ohlc ? @queue.map(&:high).max : @queue.max 
    the_low = is_ohlc ? @queue.map(&:low).min : @queue.min
    # the value is :close by default. But can be :wap, :typical_price aso. 
    the_value = is_ohlc ?  value.send( @take ) : value
    k = ( the_value - the_low )/ ( the_high - the_low )
    @stochastics_fast.add_item k

    out_1 = if @stochastics_fast.current.present?
              @stochastics_slow.add_item @stochastics_fast.current
              @stochastics_fast.current
            else
              nil
            end

    #puts "momentum:  #{@emas[:low].current} <--> #{@emas[:low_abs].current}"
    out_2 = if @stochastics_slow.current.present?   # warmup perioda is over
              @stochastics_slow.current
            else
              nil
            end
  end
  @buffer << { fast: out_1, slow: out_2 }

  current  #  return the last buffer value
end

#currentObject

returns the ema if the last computed item



92
93
94
95
96
97
98
99
# File 'lib/technical_analysis/momentum/lane-stochastic.rb', line 92

def current
  obj = @buffer.last
  if obj.values.compact.empty?
    nil
  else
    obj
  end
end

#stochasticsObject Also known as: lane

returns the ema-buffer



85
86
87
# File 'lib/technical_analysis/momentum/lane-stochastic.rb', line 85

def stochastics
  @buffer
end