Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
arvicco committed Mar 14, 2012
1 parent 48104cb commit 807371a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 42 deletions.
26 changes: 26 additions & 0 deletions bin/generic_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby

# This script reproduces https://github.com/ib-ruby/ib-ruby/issues/12

require 'pathname'
LIB_DIR = (Pathname.new(__FILE__).dirname + '../lib/').realpath.to_s
$LOAD_PATH.unshift LIB_DIR unless $LOAD_PATH.include?(LIB_DIR)

require 'rubygems'
require 'bundler/setup'
require 'ib-ruby'

contract = IB::Models::Contract.new :symbol=> 'AAPL',
:exchange=> "Smart",
:currency=> "USD",
:sec_type=> IB::SECURITY_TYPES[:stock],
:description=> "Some stock"
ib = IB::Connection.new
ib.subscribe(:Alert) { |msg| puts msg.to_human }
ib.subscribe(:TickGeneric, :TickString, :TickPrice, :TickSize) { |msg| puts msg.inspect }
ib.send_message :RequestMarketData, :id => 123, :contract => contract

puts "\nSubscribed to market data"
puts "\n******** Press <Enter> to cancel... *********\n\n"
gets
puts "Cancelling market data subscription.."
6 changes: 3 additions & 3 deletions lib/ib-ruby/messages/incoming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def to_human
# This message is always sent by TWS automatically at connect.
# The IB::Connection class subscribes to it automatically and stores
# the order id in its @next_order_id attribute.
NextValidID = NextValidId = def_message( 9, [:order_id, :int])
NextValidID = NextValidId = def_message(9, [:order_id, :int])

NewsBulletins =
def_message 14, [:request_id, :int], # unique incrementing bulletin ID.
Expand Down Expand Up @@ -246,7 +246,7 @@ def to_human
[:tick_type, :int],
[:size, :int]

TickGeneric = def_message 45, AbstractTick,
TickGeneric = def_message [45, 6], AbstractTick,
[:ticker_id, :int],
[:tick_type, :int],
[:value, :decimal]
Expand All @@ -256,7 +256,7 @@ def to_human
[:tick_type, :int],
[:value, :string]

TickEFP = def_message 47, AbstractTick,
TickEFP = def_message [47, 6], AbstractTick,
[:ticker_id, :int],
[:tick_type, :int],
[:basis_points, :decimal],
Expand Down
119 changes: 80 additions & 39 deletions spec/integration/market_data_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
require 'integration_helper'
#!/usr/bin/env ruby

require 'ib-ruby'

#OPTS[:silent] = false
describe 'Request Market Data', :connected => true, :integration => true do

context 'when subscribed to :Tick... messages' do
require 'ib-ruby'
before(:all) { verify_account }

context 'US Stocks market', :if => :us_trading_hours do
before(:all) do
verify_account
@ib = IB::Connection.new OPTS[:connection].merge(:logger => mock_logger)

##TODO consider a follow the sun market lookup for windening the types tested
@ib.subscribe(:Alert, :TickPrice, :TickSize) {}
@ib.send_message :RequestMarketData, :id => 456,
:contract => IB::Symbols::Forex[:eurusd]

@ib.wait_for 3, :TickPrice, :TickSize
@contract = IB::Models::Contract.new(:symbol => 'AAPL',
:exchange => "Smart",
:currency => "USD",
:sec_type => IB::SECURITY_TYPES[:stock],
:description => "Apple"
)
@ib.send_message :RequestMarketData, :id => 456, :contract => @contract
@ib.wait_for 5, :TickSize, :TickString
end

after(:all) do
Expand All @@ -23,47 +29,82 @@

it_behaves_like 'Received Market Data'

it 'logs no warning about unhandled :Alert message' do
should_not_log /No subscribers for message .*:Alert/
end
context "received :TickString message" do
subject { @ib.received[:TickString].first }

it 'logs no warning about unhandled :Tick... messages' do
should_not_log /No subscribers for message .*:TickPrice/
it { should be_an IB::Messages::Incoming::TickString }
its(:tick_type) { should be_an Integer }
its(:type) { should be_a Symbol }
its(:value) { should be_a String }
its(:data) { should be_a Hash }
its(:ticker_id) { should == 456 } # ticker_id
its(:to_human) { should =~ /TickString/ }
end
end

it 'logs no warning about unhandled :Tick... messages', :if => :forex_trading_hours do
should_not_log /No subscribers for message .*:TickSize/
end
context 'FOREX market' do
context 'when subscribed to :Tick... messages' do

end # when subscribed to :Tick... messages
before(:all) do
@ib = IB::Connection.new OPTS[:connection].merge(:logger => mock_logger)

context 'when NOT subscribed to :Tick... messages', :slow => true do
##TODO consider a follow the sun market lookup for windening the types tested
@ib.subscribe(:Alert, :TickPrice, :TickSize) {}
@ib.send_message :RequestMarketData, :id => 456,
:contract => IB::Symbols::Forex[:eurusd]

before(:all) do
@ib = IB::Connection.new OPTS[:connection].merge(:logger => mock_logger)
@ib.wait_for 3, :TickPrice, :TickSize
end

@ib.send_message :RequestMarketData, :id => 456,
:contract => IB::Symbols::Forex[:eurusd]
@ib.wait_for 3, :TickPrice, :TickSize
end
after(:all) do
@ib.send_message :CancelMarketData, :id => 456
close_connection
end

after(:all) do
@ib.send_message :CancelMarketData, :id => 456
close_connection
end
it_behaves_like 'Received Market Data'

it 'logs warning about unhandled :Alert message' do
should_log /No subscribers for message .*:Alert/
end
it 'logs no warning about unhandled :Alert message' do
should_not_log /No subscribers for message .*:Alert/
end

it 'logs warning about unhandled :Tick... messages' do
should_log /No subscribers for message .*:TickPrice/
end
it 'logs no warning about unhandled :Tick... messages' do
should_not_log /No subscribers for message .*:TickPrice/
end

it 'logs warning about unhandled :Tick... messages', :if => :forex_trading_hours do
should_log /No subscribers for message .*:TickSize/
end
it 'logs no warning about unhandled :Tick... messages', :if => :forex_trading_hours do
should_not_log /No subscribers for message .*:TickSize/
end

end # when subscribed to :Tick... messages

context 'when NOT subscribed to :Tick... messages', :slow => true do

before(:all) do
@ib = IB::Connection.new OPTS[:connection].merge(:logger => mock_logger)

@ib.send_message :RequestMarketData, :id => 456,
:contract => IB::Symbols::Forex[:eurusd]
@ib.wait_for 3, :TickPrice, :TickSize
end

after(:all) do
@ib.send_message :CancelMarketData, :id => 456
close_connection
end

it 'logs warning about unhandled :Alert message' do
should_log /No subscribers for message .*:Alert/
end

it 'logs warning about unhandled :Tick... messages' do
should_log /No subscribers for message .*:TickPrice/
end

it 'logs warning about unhandled :Tick... messages', :if => :forex_trading_hours do
should_log /No subscribers for message .*:TickSize/
end

end # NOT subscribed to :Tick... messages
end # NOT subscribed to :Tick... messages
end

end # Request Market Data

0 comments on commit 807371a

Please sign in to comment.