Skip to content

Instantly share code, notes, and snippets.

@tgmerritt
Created November 29, 2017 17:04
Show Gist options
  • Save tgmerritt/6f2141acc6f175c990768c633a0fcd36 to your computer and use it in GitHub Desktop.
Save tgmerritt/6f2141acc6f175c990768c633a0fcd36 to your computer and use it in GitHub Desktop.
Mutual Fund Sector Weights without an API
#!/usr/bin/env ruby
require 'mechanize'
require 'csv'
tickers = ["EWX","EZM","FMIJX","IGM","IGV","ITOT","POGRX","PRHSX","SCZ","SPEM","SPVU","SYE","VEA","XSLV","TRBIX","VEMIX","VIIIX","VWILX","VSCIX"]
mechanize = Mechanize.new
puts "Creating new CSV"
csv = CSV.open("/tmp/mutual_fund_data_as_of_#{Date.today.to_s}.csv", 'w',{:col_sep => ",", :quote_char => '\'', :force_quotes => true})
tickers.each do |t|
puts "Getting data for #{t}"
page = mechanize.get("http://portfolios.morningstar.com/fund/summary?t=#{t}&region=usa&culture=en-US")
data = page.xpath('//table[@id="equity_style_tab"]/tbody').text.split(/[\n\t\s*]+/)
data = data.reject { |c| c.empty? }
ticker = [t]
# Market Cap section
category = ["Market Capitalization"]
headers = ["Size", "% of Portfolio", "Benchmark", "Category Average"]
data = data.each_slice(4).to_a
# write to the CSV
csv << ticker
csv << category
csv << headers
data.each do |c|
csv << c
end
# Sector Weightings section
data = page.xpath('//table[@id="sector_we_tab"]/tbody').text.split(/[\n\t]+/)
data.each do |s|
s.gsub!(/\s+/,'')
end
data = data.reject { |c| c.empty? }
data = data.each_slice(17).to_a
csv << [nil]
category = ["Sector Weightings"]
headers = ["Sector", "% Stocks", "Benchmark", "Category Avg"]
# write to the csv
csv << category
csv << headers
data.each do |d|
csv << [d[0]]
d.shift
new_d = d.each_slice(4).to_a
new_d.each do |c|
csv << c
end
end
csv << [nil]
end
csv.close
puts "Finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment