Thetaris - Theta.Suite

Forum Navigation:


FORUMS > Programming and Software Forum < refresh >
Topic Title: Bloomberg / Jython Example
Created On Wed Jun 02, 10 03:08 PM
Topic View:

View thread in raw text format


afoster
Senior Member

Posts: 308
Joined: Jul 2002

Wed Jun 02, 10 03:08 PM
User is offline View users profile

I've just knocked up this example straight out of the Bloomberg V3 java documentation as a proof of concept using jython. Nothing fancy - just straight conversion of java example to python ...


blp_path = 'C:/blp/API/APIv3/JavaAPI/v3.3.1.1/lib/blpapi3.jar'


import sys
sys.path.append(blp_path)

from com.bloomberglp.blpapi import CorrelationID
from com.bloomberglp.blpapi import Event
from com.bloomberglp.blpapi import Message
from com.bloomberglp.blpapi import MessageIterator
from com.bloomberglp.blpapi import Request
from com.bloomberglp.blpapi import Service
from com.bloomberglp.blpapi import Session
from com.bloomberglp.blpapi import SessionOptions

from java.lang import System

def main():
    sessionOptions = SessionOptions()
    sessionOptions.setServerHost("localhost")
    sessionOptions.setServerPort(8194)
    session = Session(sessionOptions)

    if not session.start():
        System.out.println("Could not start session.")
        System.exit(1)

    if not session.openService("//blp/refdata"):
        System.out.println("Could not open service //blp/refdata")
        System.exit(1)
        
    requestID = CorrelationID(1)
    refDataSvc = session.getService("//blp/refdata")
    request = refDataSvc.createRequest("ReferenceDataRequest")

    request.getElement("securities").appendValue("IBM US Equity")
    request.getElement("fields").appendValue("PX_LAST")
    request.getElement("fields").appendValue("DS002")
    
    session.sendRequest(request, requestID)
    
    continueToLoop = True
    while continueToLoop == True:
        
        event = session.nextEvent()
        evt = event.eventType().intValue()
        
        if evt == Event.EventType.Constants.RESPONSE:   # final event
            handleResponseEvent(event)
            continueToLoop = False                      # fall through
            print 'final'
        elif evt == Event.EventType.Constants.PARTIAL_RESPONSE:
            print 'response'
            handleResponseEvent(event)
            # break
        else:
            print 'other'
            handleOtherEvent(event)
            # break


def handleResponseEvent(event):
    System.out.println("EventType =%s" % event.eventType())
    iter = event.messageIterator()
    while iter.hasNext():
        message = iter.next()
        System.out.println("correlationID=%s" % message.correlationID())
        System.out.println("messageType=%s" % message.messageType())
        message.print(System.out)


def handleOtherEvent(event):
    System.out.println("EventType=%s" % event.eventType())
    iter = event.messageIterator()
    while iter.hasNext():
        message = iter.next()
        System.out.println("correlationID=%s" % message.correlationID())
        System.out.println("messageType=%s" % message.messageType())
        message.print(System.out)
        if (Event.EventType.Constants.SESSION_STATUS == event.eventType().intValue() and 
            "SessionStopped" == message.messageType().toString()):
            
            System.out.println("Terminating: %s" % message.messageType())
            System.exit(1)
            
if __name__ == "__main__":
    main()
            
            
            
            
            


-------------------------
There are No Stupid Questions. But there a LOT of Inquisitive Idiots

 
Reply
   
Quote
   
Top
   
Bottom
     

View thread in raw text format
FORUMS > Programming and Software Forum < refresh >

Forum Navigation:

© All material, including contents and design, copyright Wilmott Electronic Media Limited - FuseTalk 4.01 © 1999-2010 FuseTalk Inc. Terms & Conditions