Skip to content

Commit ac9eb46

Browse files
authored
Merge pull request #4 from ib-ruby/technical_analysis
Version 1.1
2 parents 4d55ca7 + 3b0f0be commit ac9eb46

File tree

16 files changed

+306
-318
lines changed

16 files changed

+306
-318
lines changed

bin/console

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ read_yml = -> (key) do
4848
puts "Namespace is IB ! "
4949
puts
5050
puts '-'* 45
51-
include LogDev
5251
include IB
5352
require 'irb'
5453
client_id = ARGV[1] || read_yml[:client_id]
@@ -63,16 +62,15 @@ read_yml = -> (key) do
6362
end
6463

6564
ARGV.clear
66-
logger = default_logger # Logger.new STDOUT
6765

6866
## The Block takes instructions which are executed after initializing all instance-variables
6967
## and prior to the connection-process
7068
## Here we just subscribe to some events
7169
C = Connection.new client_id: client_id, port: port do |c| # future use__ , optional_capacities: "+PACEAPI" do |c|
7270

73-
c.subscribe( :ContractData, :BondContractData) { |msg| logger.info { msg.contract.to_human } }
74-
c.subscribe( :Alert, :ContractDataEnd, :ManagedAccounts, :OrderStatus ) {| m| logger.info { m.to_human } }
75-
c.subscribe( :PortfolioValue, :AccountValue, :OrderStatus, :OpenOrderEnd, :ExecutionData ) {| m| logger.info { m.to_human }}
71+
c.subscribe( :ContractData, :BondContractData) { |msg| c.logger.info { msg.contract.to_human } }
72+
c.subscribe( :Alert, :ContractDataEnd, :ManagedAccounts, :OrderStatus ) {| m| c.logger.info { m.to_human } }
73+
c.subscribe( :PortfolioValue, :AccountValue, :OrderStatus, :OpenOrderEnd, :ExecutionData ) {| m| c.logger.info { m.to_human }}
7674
# c.subscribe :ManagedAccounts do |msg|
7775
# puts "------------------------------- Managed Accounts ----------------------------------"
7876
# puts "Detected Accounts: #{msg.accounts.account.join(' -- ')} "

bin/gateway

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LogLevel = Logger::DEBUG ##INFO # DEBUG # ERROR
1212
#require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
1313

1414
require 'ib-gateway'
15+
require 'ib/verify'
1516
require 'ib/market-price'
1617
require 'ib/option-chain'
1718
require 'ib/eod'
@@ -48,7 +49,6 @@ read_yml = -> (key) do
4849
puts
4950
puts "Namespace is IB ! "
5051
puts
51-
puts '-'* 45
5252

5353
include IB
5454
require 'irb'
@@ -74,14 +74,16 @@ read_yml = -> (key) do
7474
## and prior to the connection-process
7575
## Here we just subscribe to some events
7676
begin
77-
G = Gateway.new get_account_data: true, serial_array: true,
78-
client_id: client_id, port: port, logger: logger,
79-
watchlists: [:Spreads, :BuyAndHold]
77+
G = Gateway.new get_account_data: true, serial_array: true,
78+
client_id: client_id, port: port , logger: logger,
79+
watchlists: [:Spreads, :Stillhalter, :BuyAndHold]
8080
rescue IB::TransmissionError => e
8181
puts "E: #{e.inspect}"
8282
end
8383

8484
C = G.tws
85+
86+
C.subscribe(:TickGeneric, :TickOption, :TickRequestParameters, :MarketDataType, :TickString, :TickSize ){|x| x }
8587
unless C.received[:OpenOrder].blank?
8688
puts "------------------------------- OpenOrders ----------------------------------"
8789
puts C.received[:OpenOrder].to_human.join "\n"
@@ -98,7 +100,7 @@ read_yml = -> (key) do
98100

99101
begin
100102
IRB.start(__FILE__)
101-
rescue IB::OrderAttributeError => e
102-
puts "OrderAtttributeerror"
103+
#rescue IB::OrderAttributeError => e
104+
# puts "OrderAtttribute ERROR"
103105
end
104106

ib-extensions.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
2727

2828

2929
spec.add_dependency "ox"
30+
spec.add_dependency "ib-api", "~> 972.4"
3031
spec.add_development_dependency "bundler", "~> 2.0"
3132
spec.add_development_dependency "rspec", "~> 3.0"
3233
spec.add_development_dependency 'rspec-collection_matchers'

lib/ib/alerts/base-alert.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def self.alert_134 msg
4040
# acts as prototype for any generated method
4141
#require 'active_support'
4242

43-
mattr_accessor :logger
4443

4544
def self.method_missing( method_id, msg , *args, &block )
4645
if msg.is_a? IB::Messages::Incoming::Alert
@@ -62,6 +61,9 @@ def self.method_missing( method_id, msg , *args, &block )
6261

6362

6463
class << self
64+
def logger
65+
IB::Connection.logger || IB::Gateway.logger
66+
end
6567

6668
def ignore_alert *codes
6769
codes.each do |n|

lib/ib/eod.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def eod start:nil, to: Date.today, duration: nil , what: :trades
9696
recieved = Queue.new
9797
r = nil
9898
# the hole response is transmitted at once!
99-
a= tws.subscribe(IB::Messages::Incoming::HistoricalData) do |msg|
99+
a = tws.subscribe(IB::Messages::Incoming::HistoricalData) do |msg|
100100
if msg.request_id == con_id
101101
# msg.results.each { |entry| puts " #{entry}" }
102102
r = block_given? ? msg.results.map{|y| yield y} : msg.results
@@ -134,17 +134,14 @@ def eod start:nil, to: Date.today, duration: nil , what: :trades
134134

135135
Timeout::timeout(5) do # max 5 sec.
136136
sleep 0.1
137-
last_time = recieved.pop # blocks until a message is ready on the queue
138-
loop do
139-
sleep 0.1
140-
break if recieved.closed? || recieved.empty? # finish if data received
141-
end
137+
recieved.pop # blocks until a message is ready on the queue
138+
break if recieved.closed? || recieved.empty? # finish if data received
139+
end
142140
tws.unsubscribe a
143141
tws.unsubscribe b
144142

145143
r # the collected result
146144

147-
end
148145
end # def
149146
end # class
150147
end # module

lib/ib/extensions/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module IB
22
module Extensions
3-
VERSION = "1.0"
3+
VERSION = "1.1"
44
end
55
end

lib/ib/gateway.rb

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,18 @@ module IB
7575
7676
=end
7777

78-
class Gateway
78+
class Gateway
7979

80-
include LogDev # provides default_logger
81-
include AccountInfos # provides Handling of Account-Data provided by the tws
82-
include OrderHandling
80+
include Support::Logging # provides default_logger
81+
include AccountInfos # provides Handling of Account-Data provided by the tws
82+
include OrderHandling
8383

84-
# include GWSupport # introduces update_or_create, first_or_create and intercept to the Array-Class
84+
# include GWSupport # introduces update_or_create, first_or_create and intercept to the Array-Class
8585

86-
# from active-support. Add Logging at Class + Instance-Level
87-
mattr_accessor :logger
88-
# similar to the Connection-Class: current represents the active instance of Gateway
89-
mattr_accessor :current
90-
mattr_accessor :tws
86+
# from active-support. Add Logging at Class + Instance-Level
87+
# similar to the Connection-Class: current represents the active instance of Gateway
88+
mattr_accessor :current
89+
mattr_accessor :tws
9190

9291

9392

@@ -100,16 +99,16 @@ def initialize port: 4002, # 7497,
10099
connect: true,
101100
get_account_data: false,
102101
serial_array: false,
103-
logger: default_logger,
102+
logger: nil,
104103
watchlists: [] , # array of watchlists (IB::Symbols::{watchlist}) containing descriptions for complex positions
105104
**other_agruments_which_are_ignored,
106105
&b
107106

108107
host, port = (host+':'+port.to_s).split(':')
109108

110-
self.logger = logger
111-
logger.info { '-' * 20 +' initialize ' + '-' * 20 }
112-
logger.tap{|l| l.progname = 'Gateway#Initialize' }
109+
self.class.configure_logger logger
110+
111+
self.logger.info { '-' * 20 +' initialize ' + '-' * 20 }
113112

114113
@connection_parameter = { received: serial_array, port: port, host: host, connect: false, logger: logger, client_id: client_id }
115114

@@ -124,8 +123,6 @@ def initialize port: 4002, # 7497,
124123
Thread.report_on_exception = true
125124
# https://blog.bigbinary.com/2018/04/18/ruby-2-5-enables-thread-report_on_exception-by-default.html
126125
Gateway.current = self
127-
# establish Alert-framework
128-
IB::Alert.logger = logger
129126
# initialise Connection without connecting
130127
prepare_connection &b
131128
# finally connect to the tws
@@ -182,7 +179,6 @@ def update_local_order order
182179
def connect maximal_count_of_retry=100
183180

184181
i= -1
185-
logger.progname = 'Gateway#connect'
186182
begin
187183
tws.connect
188184
rescue Errno::ECONNREFUSED => e
@@ -229,7 +225,6 @@ def connect maximal_count_of_retry=100
229225

230226

231227
def reconnect
232-
logger.progname = 'Gateway#reconnect'
233228
if tws.present?
234229
disconnect
235230
sleep 1
@@ -239,7 +234,6 @@ def reconnect
239234
end
240235

241236
def disconnect
242-
logger.progname = 'Gateway#disconnect'
243237

244238
tws.disconnect if tws.present?
245239
@accounts = [] # each{|y| y.update_attribute :connected, false }
@@ -256,7 +250,6 @@ def disconnect
256250
=end
257251

258252
def send_message what, *args
259-
logger.tap{|l| l.progname = 'Gateway#SendMessage' }
260253
begin
261254
if check_connection
262255
tws.send_message what, *args
@@ -275,7 +268,6 @@ def send_message what, *args
275268

276269
def cancel_order *orders
277270

278-
logger.tap{|l| l.progname = 'Gateway#CancelOrder' }
279271

280272
orders.compact.each do |o|
281273
local_id = if o.is_a? (IB::Order)
@@ -345,7 +337,7 @@ def account_data account_or_id=nil
345337

346338
def prepare_connection &b
347339
tws.disconnect if tws.is_a? IB::Connection
348-
self.tws = IB::Connection.new **@connection_parameter
340+
self.tws = IB::Connection.new **@connection_parameter.merge( logger: self.logger )
349341
@accounts = @local_orders = Array.new
350342

351343
# prepare Advisor-User hierachy
@@ -378,7 +370,6 @@ def initialize_managed_accounts
378370
end
379371

380372
man_id = tws.subscribe( :ManagedAccounts ) do |msg|
381-
logger.progname = 'Gateway#InitializeManagedAccounts'
382373
if @accounts.empty?
383374
# just validate the message and put all together into an array
384375
@accounts = msg.accounts_list.split(',').map do |a|
@@ -396,7 +387,6 @@ def initialize_alerts
396387

397388
tws.subscribe( :AccountUpdateTime ){| msg | logger.debug{ msg.to_human }}
398389
tws.subscribe(:Alert) do |msg|
399-
logger.progname = 'Gateway#Alerts'
400390
logger.debug " ----------------#{msg.code}-----"
401391
# delegate anything to IB::Alert
402392
IB::Alert.send("alert_#{msg.code}", msg )

lib/ib/gateway/account-infos.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ module AccountInfos
3030
=end
3131
def get_account_data *accounts, watchlists: []
3232

33-
logger.progname = 'Gateway#get_account_data'
3433

3534
@account_data_subscription ||= subscribe_account_updates
3635

0 commit comments

Comments
 (0)