Class: Bberg::Requests::HistoricalDataRequest

Inherits:
RefdataRequestBase show all
Defined in:
lib/bberg/requests/historical_data_request.rb

Overview

A class for preforming historical data requets.

Constant Summary collapse

DEFAULT_OPTIONS =
Hash[
:fields => ["PX_SETTLE"]

Instance Method Summary collapse

Methods inherited from RefdataRequestBase

#convert_to_rb_date, #convert_to_rb_time, #convert_value_to_bberg, #create_ref_data_service, #get_correlation_id, #hash_merge_concat, #perform_request, #retrieve_response

Constructor Details

#initialize(session_options, identifiers, start_time, end_time, options_arg = {}) ⇒ HistoricalDataRequest

Create new instance.

Parameters:

  • session_options (Bberg::Native::SessionOptions)

    to specify how to connect session.

  • identifiers (#each|String)

    a list of identifiers for this request

  • start_time (Time)

    start of historical range

  • end_time (Time)

    end of historical range

  • options_arg (Hash) (defaults to: {})

    specification of what fields or other parameters to use for the request.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bberg/requests/historical_data_request.rb', line 21

def initialize(session_options, identifiers, start_time, end_time, options_arg = {})
  @session_options = session_options
  
  @identifiers = unless identifiers.respond_to? 'each'
    [identifiers]
  else
    identifiers
  end
  
  @start_time = start_time
  @end_time = end_time
  
  @options = DEFAULT_OPTIONS.merge(options_arg)
end

Instance Method Details

#create_requestObject

Create a historical data request.



37
38
39
40
41
42
43
44
45
46
# File 'lib/bberg/requests/historical_data_request.rb', line 37

def create_request
  request = @svc.createRequest("HistoricalDataRequest")
  request.set("startDate", @start_time.strftime("%Y%m%d"))
  request.set("endDate", @end_time.strftime("%Y%m%d"))
  request.set("periodicitySelection", @options[:frequency])
  request.set("returnEids", false)
  @identifiers.each {|identifier| request.append("securities", identifier) }
  @options[:fields].each {|f| request.append("fields", f) }
  @request = request
end

#parse_response(event) ⇒ Hash

Parse event for HistoricalDataResponse.

Returns:

  • (Hash)

    event parsed into a Hash format.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bberg/requests/historical_data_request.rb', line 50

def parse_response(event)
  iter = event.messageIterator()
  result = Hash.new
  
  while iter.hasNext()
    
    message = iter.next()
    raise Bberg::BbergException.new("Got a response with incorrect correlation id!") if message.correlationID != @req_id
    msg_type = message.messageType().toString()
    raise Bberg::BbergException.new("Expected message of type HistoricalDataResponse but got #{msg_type}") if msg_type != "HistoricalDataResponse"
    
    field_data = message.getElement("securityData").getElement("fieldData")
    security_name = message.getElement("securityData").getElementAsString("security")
    
    result[security_name] ||= []
    
    (0..(field_data.numValues - 1)).each do |field_num|
      field_values = get_field_values(field_data, field_num)
      result[security_name] << field_values
    end
  end
  result  
end