The ib-api Gem includes the Connecton application.
Include
 
require 'ib-api' 
in your scripts.

IB::Connection

IB::Connection.new instantiates the connection.

The Connection-instance provides

connect connected? disconnect

to manage the connection.

IB::Gateway

In the 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 in IB::Connection.connect, 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. IB-Gateway does handle most common failures.

The ib-extensions Gem includes the Gateway application.
Include
 
require 'ib-api' 
require 'ib/gateway' 
in your scripts.

IB::Gateway#Connect 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.

If a Connection has been established, one cannot be sure, that its not interrupted by IB. Then an error-message is transmitted:

05:37:29 TWS System 1102: Connectivity between IB and Trader Workstation has been restored - data maintained.

The standard answer of the IB::Gateway is defined in the Alert-class

   def IB::Alert.alert_2102
       sleep 0.1  #  no need to wait too long.
       if IB::Gateway.current.check_connection
          IB::Gateway.logger.debug { "Alert 2102: Connection stable"  }
       else
         IB::Gateway.current.reconnect
       end
    end

Simple and efficient.

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: