Skip to content

Instantly share code, notes, and snippets.

@utahta
Last active December 15, 2015 11:29
Show Gist options
  • Select an option

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

Select an option

Save utahta/5253508 to your computer and use it in GitHub Desktop.
3月の優待結果
# coding:utf-8
require 'rubygems'
require 'jpstock'
# 証券コード => [優待に必要な株数, 優待額]
codes = {
'6875' => [100, 3000],
'7291' => [100, 1000],
'7313' => [100, 3000],
'1334' => [1000, 3000],
'9633' => [1000, 7200],
'8566' => [100, 3000],
'3794' => [100, 1000],
'6419' => [100, 1000],
'7239' => [100, 1000],
}
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
puts output
puts "合計: #{total_pl}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment