我正在开发Node.js项目,我需要用Bloomberg API为一些股票定价。我发现这个API有一个NPM包,所以我安装了它并开始根据https://github.com/bloomberg/blpapi-node进行测试,但我没有得到任何回复。
这是我的代码:
var blpapi = require('blpapi');
var bloombergPricing = function ()
{
var session = new blpapi.Session({ host: '127.0.0.1', port: 8194 });
session.on('SessionStarted', function(m) {
console.log('bonjou');
session.openService('//blp/mktdata', 1);
});
var securities = [
{ security: 'AAPL US Equity', correlation: 0, fields: ['LAST_TRADE'] },
{ security: 'GOOG US Equity', correlation: 1, fields: ['LAST_TRADE'] }
];
session.on('ServiceOpened', function(m) {
console.log(session);
if (m.correlations[0].value == service_id) {
console.log(session);
session.subscribe(securities);
}
});
session.on('MarketDataEvents', function(m) {
if (m.data.hasOwnProperty('LAST_TRADE')) {
console.log(securities[m.correlations[0].value].security,
'LAST_TRADE', m.data.LAST_TRADE);
}
});
}
这个套餐还在运作吗?如果没有,如何从Node.js调用Java Bloomberg API?
非常感谢。
答案 0 :(得分:1)
我认为你错过了session.start();在功能的最后。这将触发连接。
编辑以包含适合我的代码:
var blpapi = require('blpapi');
var bloombergPricing = function ()
{
var session = new blpapi.Session({ host: '127.0.0.1', port: 8194 });
session.on('SessionStarted', function(m) {
console.log('bonjou');
session.openService('//blp/mktdata', 1);
});
var securities = [
{ security: 'AAPL US Equity', correlation: 0, fields: ['LAST_TRADE'] },
{ security: 'GOOG US Equity', correlation: 1, fields: ['LAST_TRADE'] }
];
session.on('ServiceOpened', function(m) {
console.log(session);
if (m.correlations[0].value == 1) {
console.log(session);
session.subscribe(securities);
}
});
session.on('MarketDataEvents', function(m) {
if (m.data.hasOwnProperty('LAST_TRADE')) {
console.log(securities[m.correlations[0].value].security,
'LAST_TRADE', m.data.LAST_TRADE);
}
});
session.start();
}
bloombergPricing();