#!/usr/bin/env ruby ### loads the active-orient environment ### and starts an interactive shell ### ### Parameter: t)ws | g)ateway (or number of port ) Default: Gateway , ### client_id , Default 2000 ### ### Define Parameter in file console.yml ### require 'bundler/setup' require 'yaml' require 'logger' require "ib-api" require 'ib/extensions' require 'ib/symbols' class Array # enables calling members of an array. which are hashes by it name # i.e # # 2.5.0 :006 > C.received[:OpenOrder].local_id # => [16, 17, 21, 20, 19, 8, 7] # 2.5.0 :007 > C.received[:OpenOrder].contract.to_human # => ["", "", "", "", "", "", ""] # # its included only in the console, for inspection purposes def method_missing(method, *key) unless method == :to_hash || method == :to_str #|| method == :to_int return self.map{|x| x.public_send(method, *key)} end end end # Array # read items from console.yml read_yml = -> (key) do YAML::load_file( File.expand_path('../console.yml',__FILE__))[key] end puts puts ">> IB-Ruby Interactive Console <<" puts '-'* 45 puts puts "Namespace is IB ! " puts puts '-'* 45 include IB require 'irb' client_id = ARGV[1] || read_yml[:client_id] specified_port = ARGV[0] || 'Gateway' port = case specified_port when Integer specified_port # just use the number when /^[gG]/ read_yml[:gateway] when /^[Tt]/ read_yml[:tws] end ARGV.clear ## The Block takes instructions which are executed after initializing all instance-variables ## and prior to the connection-process ## Here we just subscribe to some events C = Connection.new client_id: client_id, port: port do |c| # future use__ , optional_capacities: "+PACEAPI" do |c| c.subscribe( :ContractData, :BondContractData) { |msg| c.logger.info { msg.contract.to_human } } c.subscribe( :Alert, :ContractDataEnd, :ManagedAccounts, :OrderStatus ) {| m| c.logger.info { m.to_human } } c.subscribe( :PortfolioValue, :AccountValue, :OrderStatus, :OpenOrderEnd, :ExecutionData ) {| m| c.logger.info { m.to_human }} # c.subscribe :ManagedAccounts do |msg| # puts "------------------------------- Managed Accounts ----------------------------------" # puts "Detected Accounts: #{msg.accounts.account.join(' -- ')} " # puts # end c.subscribe( :OpenOrder){ |msg| "Open Order detected and stored: C.received[:OpenOrders] " } c.logger.level = Logger::INFO end unless C.received[:OpenOrder].blank? puts "------------------------------- OpenOrders ----------------------------------" puts C.received[:OpenOrder].to_human.join "\n" end puts "Connection established on Port #{port}, client_id #{client_id} used" puts puts "----> C points to the connection-instance" puts puts "some basic Messages are subscribed and accordingly displayed" puts '-'* 45 IRB.start(__FILE__)