2009-10-22

Simple Ruby and Bloomberg

I have been looking for very simple Bloomberg code.  I have used this post from bgimpert on Wilmott as a starting point.  I have copied at the bottom of this post in case it disappears.

My very simplest code is showing both historical data and static data is:

require 'win32ole'
bb =WIN32OLE.new('Bloomberg.Data.1')
bb.BLPGetHistoricalData2(["SPX Index"],["PX_LAST"],Time.now,"USD")

bb.BLPSubscribe(["EUE LN Equity"],["ID_ISIN"])

which returns the latest date and value pair.

Digging around is helped by inspection eg

bb.ole_get_methods

bgimper post on Wilmott
Or in Ruby, with no worries about dependencies or references or DLL's:
require 'win32ole'
class Bloomberg
ONE_YEAR = 60 * 60 * 24 * 365.25
DAILY_PERIODICITY = 1
WEEKLY_PERIODICITY = 6
MONTHY_PERIODICITY = 7
ANNUAL_PERIODICITY = 9
OMIT_NON_TRADING_DAYS = 0
WEEK_NON_TRADING_DAYS = 64
ALL_CALENDAR_NON_TRADING_DAYS = 128
BLOOMBERG_HANDLES_NON_TRADING_DAY_VALUE = 0
PREVIOUS_DAYS_NON_TRADING_DAY_VALUE = 256
SHOW_NO_NUMBER_NON_TRADING_DAY_VALUE = 512
def initialize
@bloomberg = WIN32OLE.new('Bloomberg.Data.1')
@bloomberg["ActivateRealtime"] = false
@bloomberg["Periodicity"] = DAILY_PERIODICITY
@bloomberg["DisplayNonTradingDays"] = OMIT_NON_TRADING_DAYS
@bloomberg["NonTradingDayValue"] = PREVIOUS_DAYS_NON_TRADING_DAY_VALUE
end
def get_stock_price_history(ticker, start_time = Time.now - ONE_YEAR)
prices = @bloomberg.BLPGetHistoricalData2(["#{ticker} UN Equity"], ["PX_LAST"], start_time, "USD")
prices.map do |price_ar|
day_s, price = price_ar
if price_ar.empty?
nil
else
[DateTime.parse(day_s), price.to_f]
end
end.compact
end
end
bb = Bloomberg.new
bb.get_stock_price_history("GE").each do |ar|
day, price = ar
puts "day = #{day}, price = #{price}"
end

2 comments:

ICE1210 said...

I have tried to use the WIN32OLE.new('Bloomberg.Data.1') however i'm receiving the following error:

WIN32OLERuntimeError: failed to create WIN32OLE object from `Bloomberg.Data.1'
HRESULT error code:0x80040154
Class not registered

Would you have any insight into why?

RubyPane said...

Its been a while! I think you have to install Bloomberg on your PC. I don't have access to bloomberg any longer