🍓
J-Quants API のRubyサンプル
J-Quants API のサンプルコードがCurlとPythonのみだったのでRubyのサンプルコードを作ってみました。
J-Quants APIについての詳細は以下を参照して下さい。
require 'uri'
require 'net/http'
require "json"
EMAIL_ADDRESS = <Your email address>
PASSWORD = <Your password>
CODE = '2503' #銘柄コード
DATE = '2023-11-29' #基準日付
DATE_FROM = '2023-12-01' #from日付
DATE_TO = '2023-12-05' #to日付
HOLIDAYDIVISION = 1
def jquants()
p '--- リフレッシュトークン取得 ---'
refresh_token = auth_user(EMAIL_ADDRESS, PASSWORD)
p '--- IDトークン取得 ---'
id_token = auth_refresh(refresh_token)
p '--- 上場銘柄一覧 ---'
p listed_info(id_token, CODE, DATE)
p '--- 株価四本値 ---'
p daily_quotes(id_token, CODE, DATE, DATE, nil)
p '--- 財務情報 ---'
p fins_statements(id_token, CODE, DATE)
p '--- 決算発表予定日 ---'
p fins_announcement(id_token)
p '--- 取引カレンダー ---'
p trading_calendar(id_token, HOLIDAYDIVISION, DATE_FROM, DATE_TO)
end
jquants
リフレッシュトークン取得(/token/auth_user)
def auth_user(mailaddress, password)
uri = URI.parse('https://api.jquants.com/v1/token/auth_user')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
params = { mailaddress: mailaddress, password: password }
response = http.post(uri.path, params.to_json)
refreshToken = ''
refreshToken = JSON.parse(response.body)['refreshToken'] if response.code == '200'
end
IDトークン取得(/token/auth_refresh)
def auth_refresh(refresh_token)
uri = URI.parse("https://api.jquants.com/v1/token/auth_refresh?refreshtoken=#{refresh_token}")
req = Net::HTTP::Post.new(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
response = http.request(req)
idToken = JSON.parse(response.body)['idToken'] if response.code == '200'
end
上場銘柄一覧(/listed/info)
def listed_info(id_token, code=nil, date=nil)
uri = URI.parse("https://api.jquants.com/v1/listed/info")
if !code.nil? or !date.nil?
params = {}
params.store(:code, code) if !code.nil?
params.store(:date, date) if !date.nil?
uri.query = URI.encode_www_form(params)
end
headers = {Authorization: "Bearer #{id_token}" }
req = Net::HTTP::Get.new(uri)
req.initialize_http_header(headers)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
response = http.request(req)
JSON.parse(response.body) if response.code == '200'
end
株価四本値(/prices/daily_quotes)
def daily_quotes(id_token, code=nil, date=nil, from=nil, to=nil)
uri = URI.parse("https://api.jquants.com/v1/prices/daily_quotes")
if !code.nil? or !date.nil? or !from.nil? or !to.nil?
params = {}
params.store(:code, code) if !code.nil?
params.store(:date, date) if !date.nil?
params.store(:from, from) if !from.nil?
params.store(:to, to) if !to.nil?
uri.query = URI.encode_www_form(params)
end
headers = {Authorization: "Bearer #{id_token}" }
req = Net::HTTP::Get.new(uri)
req.initialize_http_header(headers)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
response = http.request(req)
JSON.parse(response.body) if response.code == '200'
end
財務情報(/fins/statements)
def fins_statements(id_token, code=nil, date=nil)
uri = URI.parse("https://api.jquants.com/v1/fins/statements")
if !code.nil? or !date.nil?
params = {}
params.store(:code, code) if !code.nil?
params.store(:date, date) if !date.nil?
uri.query = URI.encode_www_form(params)
end
headers = {Authorization: "Bearer #{id_token}" }
req = Net::HTTP::Get.new(uri)
req.initialize_http_header(headers)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
response = http.request(req)
JSON.parse(response.body) if response.code == '200'
end
決算発表予定日(/fins/announcement)
def fins_announcement(id_token)
uri = URI.parse("https://api.jquants.com/v1/fins/announcement")
headers = {Authorization: "Bearer #{id_token}" }
req = Net::HTTP::Get.new(uri)
req.initialize_http_header(headers)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
response = http.request(req)
JSON.parse(response.body) if response.code == '200'
end
取引カレンダー(/markets/trading_calendar)
def trading_calendar(id_token, holidaydivision=nil, from=nil, to=nil)
uri = URI.parse("https://api.jquants.com/v1/markets/trading_calendar")
if !holidaydivision.nil? or !from.nil? or !to.nil?
params = {}
params.store(:holidaydivision, holidaydivision) if !holidaydivision.nil?
params.store(:from, from) if !from.nil?
params.store(:to, to) if !to.nil?
uri.query = URI.encode_www_form(params)
end
headers = {Authorization: "Bearer #{id_token}" }
req = Net::HTTP::Get.new(uri)
req.initialize_http_header(headers)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
response = http.request(req)
JSON.parse(response.body) if response.code == '200'
end
Discussion