-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathconsole
More file actions
executable file
·95 lines (82 loc) · 3.09 KB
/
console
File metadata and controls
executable file
·95 lines (82 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/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 'ib_api'
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
# => ["<Bag: IECombo SMART USD legs: >", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: WFC USD>", "<Stock: WFC USD>"]
#
# 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-Core 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_host = ARGV[0] || 'Gateway'
host = case specified_host
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, host: host, connect: false do |c| # future use__ , optional_capacities: "+PACEAPI" do |c|
c.activate_plugin 'connection_tools'
c.activate_plugin 'verify'
c.activate_plugin 'managed_accounts'
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.initialize_managed_accounts
end
#C.logger.level = Logger::FATAL
unless C.received[:OpenOrder].blank?
puts "------------------------------- OpenOrders ----------------------------------"
puts C.received[:OpenOrder].to_human.join "\n"
end
puts "Connection established on #{host}, 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__)