Module: Jpx::Price

Defined in:
lib/jpx/price.rb

Constant Summary collapse

SESSION_ID_DAY =
"999"
SESSION_DAY =
0
SESSION_NIGHT =
1

Class Method Summary collapse

Class Method Details

.near_term?(time, contract_month) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# File 'lib/jpx/price.rb', line 57

def near_term?(time, contract_month)
  date = time.to_date
  @sq_dates ||= sq_dates
  sq_date = @sq_dates.find { |sq_date| sq_date > date }
  near_contract_month = sprintf("%d%02d", sq_date.year, sq_date.month)

  contract_month == near_contract_month
end

.parse(path) ⇒ Object



12
13
14
15
16
17
18
19
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
# File 'lib/jpx/price.rb', line 12

def parse(path)
  data = CSV.read(path)[1..-1].map do |row|
    next if row.empty?

    # 取引日, _, 識別コード, セッション区分, 時刻, 始値, 高値, 安値, 終値, 出来高, VWAP, 約定回数, _, 限月(i = 13)
    datetime = Time.parse(row[0] + row[4])
    session = to_session(row[3])

    # 期近のみ取得
    next unless near_term?(datetime, row[13])

    # 大引けの場合15:15になるので、15:10として扱う
    datetime -= 60 * 5 if row[4] == "1515"

    if session == SESSION_NIGHT
      prev_date = TradingDayJp.prev(datetime.to_date)
      prev_datetime = Time.new(prev_date.year, prev_date.month, prev_date.day, datetime.hour, datetime.min)

      # ナイトセッションの場合、実際に取引が行われた日時に変換する
      datetime =
        if datetime.hour >= 16
          prev_datetime
        else
          prev_datetime + 60 * 60 * 24
        end
    end

    {
      datetime: datetime,
      session: session,
      open: row[5].to_i,
      high: row[6].to_i,
      low: row[7].to_i,
      close: row[8].to_i,
      volume: row[9].to_i,
    }
  end

  data.compact
end

.sq_datesObject

MSQ祝日は今のところない



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jpx/price.rb', line 67

def sq_dates
  from = Date.new(2006, 1, 1)
  to = Date.today + 120

  (from.year..to.year).map do |year|
    [3, 6, 9, 12].map do |month|
      start = Date.new(year, month, 1)
      dates = (start..(start + 14))
      first_firday = dates.find { |date| date.wday == 5 }
      second_friday = dates.find { |date| date > first_firday && date.wday == 5 }

      second_friday
    end
  end.flatten
end

.to_session(session) ⇒ Object



53
54
55
# File 'lib/jpx/price.rb', line 53

def to_session(session)
  session == SESSION_ID_DAY ? SESSION_DAY : SESSION_NIGHT
end