BloomBerg Java API调用实例
首先,先问度娘介绍一下Bloomberg
彭博(Bloomberg)是全球商业、金融信息和财经资讯的领先提供商,由纽约市市长迈克尔·布隆伯格于1981年创立的,总部位于美国纽约市曼哈顿,现有超过1万3千员工,业务遍及全球185多个国家与地区。2012年彭博资讯集团全球营业收入达76亿美元,成为全球最大的财经资讯服务提供商。
我来补充总结几句,相比路透(即路透社的那个路透),彭博可能让人觉得有点陌生,不过作为全球与路透不相上下的信息提供商,在金融领域,是使用非常普遍的。
其实Bloomberg和Reuters的Extra3000(现在的EIKON)一样,都在客户端提供了非常好用的Excel插件工具用来抓取刷新各种信息。但是数据抓取了之后,还只是停留在Excel上,并不能为系统所直接使用,因此怎么把数据抓到系统中,成为了一个需要解决的问题。
有幸,当年通过不断搜索,发现Bloomberg竟然有着完整的JAVA SDK,还算擅长这种语言的我自然就想到了直接调用该API来解决面临的实际问题:
当然首先该SDK的首要条件是需要你有一台可以使用的Bloomberg客户端;
下载完成的SDK 如下:
还是比较显而易见的,在工程中引入bin包中的 jar,examples目录有相关的查询实例,doc文档有详细的说明;
以下以查询CHF,TWD,SGD,NZD的开盘价为例(修改自官方Example 中的SimpleSubscriptionExample )
1 /* 2 * Copyright 2012. Bloomberg Finance L.P. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to 6 * deal in the Software without restriction, including without limitation the 7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 * sell copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: The above 10 * copyright notice and this permission notice shall be included in all copies 11 * or substantial portions of the Software. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 * IN THE SOFTWARE. 20 */ 21 package com.bloomberglp.blpapi.examples; 22 23 import java.util.ArrayList; 24 import java.util.HashMap; 25 import java.util.List; 26 import java.util.Map; 27 28 import com.bloomberglp.blpapi.CorrelationID; 29 import com.bloomberglp.blpapi.Element; 30 import com.bloomberglp.blpapi.Event; 31 import com.bloomberglp.blpapi.Message; 32 import com.bloomberglp.blpapi.MessageIterator; 33 import com.bloomberglp.blpapi.Name; 34 import com.bloomberglp.blpapi.Session; 35 import com.bloomberglp.blpapi.SessionOptions; 36 import com.bloomberglp.blpapi.Subscription; 37 import com.bloomberglp.blpapi.SubscriptionList; 38 39 40 public class SimpleSubscriptionExample { 41 //查询 当日开盘价 42 private Name OPEN_TDY = new Name("OPEN_TDY"); 43 44 //定义货币 45 String chfCcy = new String("CHF Curncy"); 46 String twdCcy = new String("TWD Curncy"); 47 String sgdCcy = new String("SGD Curncy"); 48 String nzdCcy = new String("NZD Curncy"); 49 public static Map ccyMap; 50 51 int count=0; 52 53 /** 54 * @param args 55 */ 56 public static void main(String[] args) throws Exception { 57 ccyMap = new HashMap(); 58 //定义货币对,查询数据源 59 ccyMap.put("CHF Curncy", "USDCHF=SAEC"); 60 ccyMap.put("TWD Curncy", "USDTWD=SAEC"); 61 ccyMap.put("SGD Curncy", "USDSGD=SAEC"); 62 ccyMap.put("NZD Curncy", "NZDUSD=SAEC"); 63 64 System.out.println("SimpleSubscriptionExample"); 65 SimpleSubscriptionExample example = new SimpleSubscriptionExample(); 66 example.run(args); 67 68 System.exit(0); 69 } 70 71 public String getCcyPair(String ccyCode) { 72 String dRtn = ""; 73 dRtn = (String) ccyMap.get(ccyCode.trim()); 74 return dRtn; 75 } 76 77 private void run(String[] args) throws Exception { 78 //该代码测试为本机,即客户端机 79 String serverHost = "127.0.0.1"; 80 //端口号 81 int serverPort = 8194; 82 String serviceName = "//blp/mktdata"; 83 84 SessionOptions sessionOptions = new SessionOptions(); 85 sessionOptions.setServerHost(serverHost); 86 sessionOptions.setServerPort(serverPort); 87 88 System.out.println("Connecting to " + serverHost + ":" + serverPort); 89 Session session = new Session(sessionOptions); 90 if (!session.start()) { 91 System.err.println("Failed to start session."); 92 return; 93 } 94 95 System.out.println("Connected successfully."); 96 if (!session.openService(serviceName)) { 97 System.err.println("Failed to open " + serviceName); 98 session.stop(); 99 return; 100 } 101 102 103 SubscriptionList subscriptions = new SubscriptionList(); 104 105 subscriptions.add(new Subscription(chfCcy, "OPEN_TDY", "", 106 new CorrelationID(chfCcy))); 107 subscriptions.add(new Subscription(twdCcy, "OPEN_TDY", "", 108 new CorrelationID(twdCcy))); 109 subscriptions.add(new Subscription(sgdCcy, "OPEN_TDY", "", 110 new CorrelationID(sgdCcy))); 111 subscriptions.add(new Subscription(nzdCcy, "OPEN_TDY", "", 112 new CorrelationID(nzdCcy))); 113 114 115 System.out.println("Subscribing..."); 116 session.subscribe(subscriptions); 117 118 while (count<5) { 119 Event event = session.nextEvent(); 120 MessageIterator msgIter = event.messageIterator(); 121 while (msgIter.hasNext()) { 122 123 Message msg = msgIter.next(); 124 125 if (event.eventType() == Event.EventType.SUBSCRIPTION_DATA 126 || event.eventType() == Event.EventType.SUBSCRIPTION_STATUS) { 127 128 String topic = (String) msg.correlationID().object(); 129 if (msg.hasElement(OPEN_TDY)) { 130 Element field = msg.getElement(OPEN_TDY); 131 System.out.println(topic + "||" + ": " + " = " 132 + field.getValueAsString()); 133 134 135 System.out.println(topic + "success "); 136 137 } 138 } 139 } 140 141 } 142 } 143 }
还是很简单的就可以调用了,接下来需要插入数据库或者使用WebServcie插入数据都可以,当然此类实现的方法多种多样,此文仅为抛砖引玉,为大家提供思路。