Skip to content

Instantly share code, notes, and snippets.

@utahta
Created September 26, 2012 07:44
Show Gist options
  • Select an option

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

Select an option

Save utahta/3786629 to your computer and use it in GitHub Desktop.
優待・逆日歩損益計算
# coding:utf-8
require 'rubygems'
require 'jpstock'
# 証券コード => [優待に必要な株数, 優待額]
codes = {
'2131' => [5, 15000],
'4665' => [100, 1000],
'4839' => [1, 2000],
'7291' => [100, 1000],
'7616' => [500, 20000],
'9633' => [1000, 7200],
}
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|
codes[g.code].push(g.price)
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