module Jpx::Price

Constants

SESSION_DAY
SESSION_ID_DAY
SESSION_NIGHT

Public Class Methods

near_term?(time, contract_month) click to toggle source
# 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) click to toggle source
# 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_dates() click to toggle source

MSQ祝日は今のところない

# 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) click to toggle source
# File lib/jpx/price.rb, line 53
def to_session(session)
  session == SESSION_ID_DAY ? SESSION_DAY : SESSION_NIGHT
end