Friday, January 9, 2009

Proof Of Concept - 7 of 8 : NetBeans Ruby Plugins

Let's review what we have so far for my development environment. Java SDK is installed and IB's Java API successfully compiles in NetBeans. I was able to compile and run IB's test client program and create a JAR file of IB's API. JRuby is installed and runs on top of Java SDK. So now I'm ready to write Ruby code and call IB's Java API.

Before I can do that though, I need to have a development environment for Ruby. Fortunately, NetBeans also supports Ruby development along with Java. To enable Ruby support, go to Tools | Plugins | Available Plugins tab. Choose Ruby and JRuby plugins and click install. It may take a while to fr NetBeans to download all the libraries.


Test out Ruby plugin by creating a Hello World program. Go to File | New Project | Ruby Application. Name it HelloWorld.



Run the application by pressing F6 and 'Hello World' should display in the output window.


Success!! Ruby development is enabled in NetBeans.




Next step is to see if Java classes are accessible. This is a sample Ruby code that calls Java class TreeSet. Note how seamless it is to call Java classes using JRuby.


require 'java'
include_class 'java.util.TreeSet'

set = TreeSet.new
set.add "foo"
set.add "Bar"
set.add "baz"
set.each do |v|
puts "value: #{v}"
end


Press F6 will produce the following in the output window.




Another way of testing this is (just plain JRuby, not NetBeans) to run it via command line. It produces the same output:






02/22/09 Update


JRuby plugin automatically installs version 1.1.4. This is an old version and NetBeans must be manually configured to use version 1.1.6 (installed in Proof Of Concept - 6 of 8). Please see Proof Of Concept 8 of 8b : Historical Quote for how to do this.




Last step is to see if IB's Java API archived file is accessible. Copy file TwsApi.jar (created in Proof Of Concept - 5 of 8) and put it in HelloWorld\lib folder. Run TWS in paper trading mode as this is necessary for for the API to connect to.

Code the following in main.rb. This is really simple code to connect and disconnect from TWS using IB's API.

 
require 'java'
require 'TwsApi.jar'
include_class 'ib.client.EClientSocket'

mySocket = EClientSocket.new(self)
begin
mySocket.eConnect("127.0.0.1", 7496, 0) # api
puts 'connected ...'
mySocket.eDisconnect
puts 'disconnected'
rescue
Exception => e
puts 'can not connect'
puts e.message
end

Press F6 will produce the following output:

Server Version:43TWS Time at connection:20090113 15:27:27 ESTconnected ...disconnected


Running it via command line produces the same output:



At this point, the development environment is set up and Java and API classes are accessible. It is now time to start using IB's API to see its full power.

3 comments:

Robbie said...

really interesting start for a new project. I'm currently implementing the same project but i choose to mix SwingX with Spring instead of Netbeans for the bases and Groovy instead of JRuby.

Good luck in your project ! i will read you often.

Regards, Robert

Michael Gruen said...

Thanks for posting this - great work!

Best,
Michael

System Trader said...

Robert and Mike,
Thanks for your kind words. Sorry I didn't read your comments sooner. I'm new to this blogging thing and didn't have it set to send comments to my email.