Skip to content

Instantly share code, notes, and snippets.

@utahta
Created March 27, 2014 09:15
Show Gist options
  • Select an option

  • Save utahta/9803548 to your computer and use it in GitHub Desktop.

Select an option

Save utahta/9803548 to your computer and use it in GitHub Desktop.
3月 優待
# coding:utf-8
require 'rubygems'
require 'jpstock'
# 取得価額から逆日歩最高料率を計算して、
# 優待額との差をだし、
# お得かどうか判断します
# 逆日歩最高料率の計算
def max_gyakuhibu(price, lot)
m = price * lot
g = (m / 50000.0).ceil * 100
g = g / lot.to_f
if g <= 1
g = 1.5
else
g = g.ceil
end
return g
end
# 注意喚起銘柄 倍率返す
def chuuikanki(code)
if code == "7291"
return 2
end
return 1
end
GYAKUHIBU_DAY_NUM = 1 # 逆日歩日数
class YutaiData
attr_accessor :code, :name, :benefit, :gyakuhibu, :cost, :loss, :rate
def initialize(code, name, price, lot, yutai_lot, benefit)
@code = code
@name = name
@price = price
@lot = lot
@benefit = benefit
@gyakuhibu = max_gyakuhibu(price, lot) * 4 * GYAKUHIBU_DAY_NUM * chuuikanki(code) # 権利日 * 日数 * 注意喚起
@cost = (@gyakuhibu * yutai_lot).to_i # 逆日歩最高料率 * 優待取得に必要な株数
@loss = @benefit - @cost
@rate = (@loss / @benefit.to_f * 100).round(1)
end
def debug()
print "コード:#{@code} 会社名:#{@name} 株価:#{@price} 単元:#{@lot}\n"
print "逆日歩:#{@gyakuhibu} コスト:#{@cost} 損失:#{@loss}\n"
end
end
# 証券コード => [優待に必要な株数, 優待額]
codes = {
1722 => [100, 1000],
3794 => [100, 1000],
4548 => [100, 1000],
5449 => [100, 1000],
6151 => [100, 2000],
6419 => [100, 1000],
6875 => [100, 3000],
7212 => [100, 1000],
7291 => [100, 1000],
7510 => [100, 1500],
7942 => [100, 3000],
8424 => [100, 3000],
8425 => [100, 3000],
8439 => [100, 2000],
8566 => [100, 3000],
8697 => [100, 3000],
9633 => [1000, 4800],
}
quotes = JpStock.quote(:code => codes.keys)
results = []
quotes.each do |q|
results.push(YutaiData.new(q.code,
q.company_name,
q.close,
q.round_lot,
codes[q.code.to_i][0],
codes[q.code.to_i][1]))
end
output = <<EOS
<table>
<tr>
<th>コード</th><th>企業名</th><th>逆日歩</th><th>優待額</th><th>コスト</th><th>損益</th><th>率</th>
</tr>
EOS
results.sort{|a, b|
a.rate <=> b.rate
}.reverse.each{|d|
output += <<EOS
<tr>
<td>#{d.code}</td><td>#{d.name}</td><td>#{d.gyakuhibu}</td><td>#{d.benefit}</td><td>#{d.cost}</td>
EOS
output += d.loss >= 0 ? "<td><font color=red>+#{d.loss}</font></td>" : "<td><font color=blue>#{d.loss}</font></td>"
output += "<td>#{d.rate}</td>"
output += "</tr>"
}
output += <<EOS
</table>
EOS
print output
puts ""
results.sort{ |a, b|
a.rate <=> b.rate
}.reverse.each do |d|
puts "#{d.code}:#{d.name} 逆日歩:#{d.gyakuhibu} 優待額:#{d.benefit} コスト:#{d.cost} 損益:#{d.loss} 率:#{d.rate}"
end
# coding:utf-8
require 'rubygems'
require 'jpstock'
# 実際の逆日歩から優待額との差をだして、
# いくら相当プラス・マイナスになったか出力します
# 逆日歩データがとれなくなるので、権利取得翌日(3月27日中)にやること
# 証券コード => [優待に必要な株数, 優待額]
codes = {
"1722" => [100, 1000],
"3794" => [100, 1000],
"4548" => [100, 1000],
"5449" => [100, 1000],
"6151" => [100, 2000],
"6419" => [100, 1000],
"6875" => [100, 3000],
"7212" => [100, 1000],
"7291" => [100, 1000],
"7510" => [100, 1500],
"7942" => [100, 3000],
"8424" => [100, 3000],
"8425" => [100, 3000],
"8439" => [100, 2000],
"8566" => [100, 3000],
"8697" => [100, 3000],
"9633" => [1000, 4800],
}
codes.map{|k, v| v.push(0.0)}
class YutaiData
attr_accessor :code, :name, :gyakuhibu, :benefit, :cost, :pl
def initialize(code, name, lot, gyakuhibu, benefit)
@code = code
@name = name
@gyakuhibu = gyakuhibu
@benefit = benefit
@cost = (lot * gyakuhibu).ceil
@pl = @benefit - @cost
end
end
gyakuhibus = JpStock.nipd(:code=>codes.keys)
gyakuhibus.each do |g|
if g
codes[g.code][2] = g.price
end
end
quotes = JpStock.quote(:code=>codes.keys)
results = []
quotes.each{|quote|
code = quote.code
results.push(YutaiData.new(
code.to_i,
quote.company_name,
codes[code][0],
codes[code][2],
codes[code][1]
))
}
total_pl = 0
output = <<EOS
<table>
<tr>
<th>コード</th><th>企業名</th><th>逆日歩</th><th>優待額</th><th>損益</th>
</tr>
EOS
results.sort{|a, b|
a.code <=> b.code
}.each{|d|
output += <<EOS
<tr>
<td>#{d.code}</td><td>#{d.name}</td><td>#{d.gyakuhibu}</td><td>#{d.benefit}</td>
EOS
output += d.pl >= 0 ? "<td><font color=red>+#{d.pl}</font></td>" : "<td><font color=blue>#{d.pl}</font></td>"
output += "</tr>"
total_pl += d.pl
}
output += <<EOS
</table>
EOS
results.sort{|a, b|
a.code <=> b.code
}.each{|d|
puts "#{d.code}:#{d.name} 逆日歩:#{d.gyakuhibu} 優待額:#{d.benefit} 損益:#{d.pl}"
}
puts output
puts "合計: #{total_pl}"
fee = codes.keys.length * 166
puts "手数料: #{fee}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment