The ib-api Gem includes the Connecton application.
Include
 
require 'ib-api' 
c = IB::Connection.new host: 'localhost:4002'
in your scripts.

IB::Connection

IB::Connection.new instantiates a new connection to the TWS-Server.

Without active plugins the Connection-object is just able to connect to the TWS and to disconnect the socket afterwards.

ib = IB::Connection.new
ib.try_connect!
ib.ready?           => true

ib.disconnect!
ib.disconnected?   => true
ib.ready?          => false

The Connection-instance provides several states that specify its status and behavior.

Plugin ConnectionTools

In a WAN-World many things can go terribly wrong.
Even a super stable cable connection can suddenly vanish. Then a reconnection may be necessary. ib-api implements the basics to connect to the TWS-Server.

This is performed through try_connection, simply by opening a port

self.socket = IBSocket.open(@host, @port)  

IBSocket is in fact a specialized TCPSocket. It inherits its complete error-handling repertoire.

If the connection cannot be established, Errno::ECONNREFUSED is fired. Its up to the user to handle this error.

require 'ib-api' 

ib = IB::Connection.new 
ib.activate_plugin :connection_tools
ib.try_connection!
ib.ready?               =>  true

The plugin covers Errno::ECONNREFUSED, Errno::EHOSTUNREACH and the unspecified SocketError. It’s designed to manage uncertain and often interrupted connections via DSL, WLAN or even GSM.

If the Host is not reached, it tries to reconnect for at least one hour.

Its safe to call IB::Gateway#CheckConnection prior to every critical message, as order-placement or contract-verification. It delays the process by approx. 7 ms.

Tags: