Class: PointAndFigure::OutputDataGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/point_and_figure/output_data_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_point, base_turn, data_set) ⇒ OutputDataGenerator

Returns a new instance of OutputDataGenerator.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/point_and_figure/output_data_generator.rb', line 3

def initialize(base_point, base_turn, data_set)
  @base_point = base_point
  @base_turn = base_turn
  @data_set = data_set
  @round_unit = PointAndFigure.calc_round_unit base_point
  @output_data = {
    base_point: @base_point,
    base_turn: @base_turn,
    data_set: []
  }
  @prev_value = @data_set[0]
  @current_trend = nil
  @current_line = 1
  @current_max = @data_set[0].floor @round_unit
  @current_min = @data_set[0].ceil @round_unit
end

Instance Method Details

#generateObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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/point_and_figure/output_data_generator.rb', line 20

def generate
  @data_set.each.with_index do |value, i|
    # 初期値か
    next if i.zero?

    # 現在の枠が☓か○か
    if up_trend?
      # 高値を1枠以上更新したか
      if (value.floor(@round_unit) - @current_max).round(@round_unit) >= @base_point
        generator = PointAndFigure::PointGenerator.new @base_point, value.floor(@round_unit), @current_max, @current_line, @output_data
        @output_data = generator.generate trend: @current_trend
        @current_max = value.floor @round_unit
        next
      else
        # 高値の枠より基準枠以上価格が下がったか
        if ((@current_max - value.ceil(@round_unit)) / @base_point).round > @base_turn
          @current_trend = :down
          @current_line += 1
          @current_min = value.ceil @round_unit
          generator = PointAndFigure::PointGenerator.new @base_point, @current_min, @current_max, @current_line, @output_data
          @output_data = generator.generate trend: @current_trend, trend_changed: true
          next
        else
          next
        end
      end
    elsif down_trend?
      # 安値を1枠以上更新したか
      if (@current_min - value.ceil(@round_unit)).round(@round_unit) >= @base_point
        generator = PointAndFigure::PointGenerator.new @base_point, value.ceil(@round_unit), @current_min, @current_line, @output_data
        @output_data = generator.generate trend: @current_trend
        @current_min = value.ceil @round_unit
        next
      else
        # 安値の枠より基準枠以上価格が上がったか
        if ((value.floor(@round_unit) - @current_min) / @base_point).round > @base_turn
          @current_trend = :up
          @current_line += 1
          @current_max = value.floor @round_unit
          generator = PointAndFigure::PointGenerator.new @base_point, @current_max, @current_min, @current_line, @output_data
          @output_data = generator.generate trend: @current_trend, trend_changed: true
          next
        else
          next
        end
      end
    else
      # 高値or安値を1枠以上更新したか
      if (value.floor(@round_unit) - @current_max).round(@round_unit) >= @base_point
        @current_trend = :up
        generator = PointAndFigure::PointGenerator.new @base_point, value.floor(@round_unit), @current_max, @current_line, @output_data
        @output_data = generator.generate trend: @current_trend
        @current_max = value.floor @round_unit
      elsif (@current_min - value.ceil(@round_unit)).round(@round_unit) >= @base_point
        @current_trend = :down
        generator = PointAndFigure::PointGenerator.new @base_point, value.ceil(@round_unit), @current_min, @current_line, @output_data
        @output_data = generator.generate trend: @current_trend
        @current_min = value.ceil @round_unit
      end
    end
  end
  @output_data
end