Skip to content

Commit

Permalink
Prepare Scanner data
Browse files Browse the repository at this point in the history
  • Loading branch information
Hartmut Bischoff committed May 18, 2015
1 parent 7084240 commit 53a9d67
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 77 deletions.
1 change: 0 additions & 1 deletion example/contract_details
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#
# This script gets details for specific contract from IB

require 'rubygems'
require 'bundler/setup'
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'yaml'
Expand Down
6 changes: 3 additions & 3 deletions example/option_data_1
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ if ( ib = IB::Gateway.tws).present?
# For each verified contract a hash-entry is created which will be later subject to a market-data-subscription

options = [IB::Option.new( symbol: 'RRD', strike: 20, expiry: EXPIRY, right: :put),
IB::Option.new( symbol: 'Z', strike: 50, expiry: EXPIRY, right: :put, exchange: 'SMART'),
IB::Option.new( symbol: 'SPY', strike: 75, expiry: EXPIRY, right: :call, exchange: 'SMART'), # all ( 4 or 5 weekly) call- contracts in the EXPIRY-Month
IB::Option.new( symbol: 'SPY', strike: 100, expiry: '2016', exchange: 'SMART'), # all available contracts (put+call) in 2016
IB::Option.new( symbol: 'Z', strike: 50, expiry: EXPIRY, right: :put),
IB::Option.new( symbol: 'SPY', strike: 75, expiry: EXPIRY, right: :call), # all ( 4 or 5 weekly) call- contracts in the EXPIRY-Month
IB::Option.new( symbol: 'SPY', strike: 100, expiry: '2016' ), # all available contracts (put+call) in 2016
IB::Option.new( symbol: 'DPW', expiry: EXPIRY, currency:'EUR', right: :put, exchange: 'DTB' ), # load the option-chain
IB::Option.new( symbol: 'BEI', strike: 80, expiry: EXPIRY, currency:'EUR', right: :put, exchange: 'DTB' )
]
Expand Down
5 changes: 4 additions & 1 deletion lib/ib/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ module IB
'15 mins' =>:min15,
'30 mins' =>:min30,
'1 hour' =>:hour1,
'1 day' => :day1
'2 hours' => :hour2,
'4 hours' => :hour4,
'1 day' => :day1,
'1 week' => :week1
}.freeze

# Enumeration of data types.
Expand Down
3 changes: 0 additions & 3 deletions lib/ib/tws_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def update_option_details wait_for_data: true, snapshot: false
ib = gw.tws
self.option_detail ||= IB::OptionDetail.new

# flag= { tick_option: false, close_price: false, ask_price: false, bid_price: false }
sub= ib.subscribe(:TickPrice, :TickSize, :TickOption, :TickString) do |msg|

if msg.ticker_id == con_id
Expand All @@ -191,14 +190,12 @@ def update_option_details wait_for_data: true, snapshot: false
attributes_to_transfer.each{|a| option_detail.update_attribute a, msg.data[a] }
option_detail.update_attribute :updated_at, Time.now # perform validations
option_detail.save # perform validations
# flag[:tick_option] = true

end
when IB::Messages::Incoming::TickPrice
option_detail.update_attribute msg.type.to_sym, msg.price
option_detail.update_attribute :updated_at, Time.now # perform validations
option_detail.save # perform validations
# flag[ msg.type.to_sym ] = true

end # case
end # branch
Expand Down
1 change: 1 addition & 0 deletions lib/models/ib/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def to_human
(trades ? "trades #{trades}" : "") + " vol #{volume} gaps #{has_gaps}>"
end


alias to_s to_human
end # class Bar
end # module IB
7 changes: 3 additions & 4 deletions spec/ib/messages/incoming/alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@
context 'Message received from IB', :connected => true do

before(:all) do
@ib = IB::Connection.new OPTS[:connection].merge(:logger => mock_logger, client_id:1290)
@ib.wait_for :Alert
pending 'No Alert received upon connect!' unless @ib.received? :Alert
gw = IB::Gateway.current.presence || IB::Gateway.new( OPTS[:connection].merge(logger: mock_logger, client_id:1056, connect:true, serial_array: false, host: 'localhost'))
@ib = gw.tws
end

after(:all) { close_connection }
after(:all) { IB::Gateway.current.disconnect }

subject { @ib.received[:Alert].first }

Expand Down
53 changes: 28 additions & 25 deletions spec/ib/messages/incoming/open_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@
its(:local_id) { should be_an Integer }
its(:status) { should =~ /Submit/ }
its(:to_human) { should =~
/<OpenOrder: <Contract: WFC stock NYSE USD> <Order: LMT DAY buy 100 9.13 .*Submit.* #\d+\/\d+ from 1111/ }
/<OpenOrder: <Stock: WFC USD> <Order: LMT DAY buy 100 @ 9.13 .*Submit.* #\d+\/\d+ from 1111/ }

it 'has proper contract accessor' do
c = subject.contract
c.should be_an IB::Contract
c.symbol.should == 'WFC'
c.exchange.should == 'NYSE'
expect( c ).to be_an IB::Contract
expect( c.symbol).to eq 'WFC'
expect( c.exchange).to eq 'NYSE'
end

it 'has proper order accessor' do
o = subject.order
o.should be_an IB::Order
o.client_id.should == 1111
o.parent_id.should == 0
o.local_id.should be_an Integer
o.perm_id.should be_an Integer
o.order_type.should == :limit
o.tif.should == :day
o.status.should =~ /Submit/
expect( o ).to be_an IB::Order
expect( o.client_id ).to eq 1111
expect( o.parent_id ).to be_zero
expect( o.local_id ).to be_an Integer
expect( o.perm_id ).to be_an Integer
expect( o.order_type).to eq :limit
expect( o.tif).to eq :day
expect( o.status).to match /Submit/
end

it 'has proper order_state accessor' do
os = subject.order_state
os.local_id.should be_an Integer
os.perm_id.should be_an Integer
os.client_id.should == 1111
os.parent_id.should == 0
os.status.should =~ /Submit/
expect( os.local_id).to be_an Integer
expect( os.perm_id).to be_an Integer
expect( os.client_id).to eq 1111
expect( os.parent_id).to be_zero
expect( os.status).to match /Submit/
end

it 'has class accessors as well' do
subject.class.message_id.should == 5
subject.class.version.should == 30 # Message versions supported
subject.class.message_type.should == :OpenOrder
expect( subject.class.message_id).to eq 5
expect( subject.class.version).to eq 30 # Message versions supported
expect( subject.class.message_type).to eq :OpenOrder
end

end
Expand Down Expand Up @@ -80,15 +80,18 @@
it_behaves_like 'OpenOrder message'
end

context 'received from IB' do
context 'received from IB', :pending => "order statement unclear" do
before(:all) do
verify_account
@ib = IB::Connection.new OPTS[:connection].merge(:logger => mock_logger)
@ib.wait_for :NextValidId
place_order IB::Symbols::Stocks[:wfc]
gw = IB::Gateway.current.presence || IB::Gateway.new( OPTS[:connection].merge(logger: mock_logger, client_id:1056, connect:true, serial_array: false, host: 'localhost'))
gw.connect if !gw.tws.connected?
if gw.advisor.test_environment?
@ib=gw.tws

place_order IB::Stock[:wfc]
@ib.wait_for :OpenOrder, 3
@ib.received?(:OpenOrder).should be_true
end
end

after(:all) { close_connection } # implicitly cancels order

Expand Down
11 changes: 6 additions & 5 deletions spec/ib/messages/outgoing/market_data_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
describe "Request Market Data Type", :connected => true, :integration => true do

before(:all) do
verify_account
@ib = IB::Connection.new OPTS[:connection].merge(:logger => mock_logger)
gw = IB::Gateway.current.presence || IB::Gateway.new( OPTS[:connection].merge(logger: mock_logger, client_id:1056, connect:true, serial_array: false, host: 'beta'))
gw.connect if !gw.tws.connected?
@ib=gw.tws
end

after(:all) { close_connection }
after(:all) { IB::Gateway.current.disconnect }

context "switching to real_time streaming after-hours" do
before(:all) do
@ib.send_message :RequestMarketDataType, :market_data_type => :real_time
@ib.wait_for 2 # sec
end

after(:all) { clean_connection }
# after(:all) { clean_connection }

it 'just works' do
end
Expand All @@ -35,7 +36,7 @@
@ib.wait_for 2 # sec
end

after(:all) { clean_connection }
# after(:all) { clean_connection }

it 'just works' do
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/account_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
its(:key){ is_expected.to be_a Symbol }
end

describe "Request Account Data", :connected => true, :integration => true do
describe "Require Account Data", :connected => true, :integration => true do

before(:all) do
gw = IB::Gateway.current.presence || IB::Gateway.new( OPTS[:connection].merge(logger: mock_logger, client_id:1056, connect:true, serial_array: false))
Expand Down
29 changes: 14 additions & 15 deletions spec/integration/contract_db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
before(:all) do
verify_account
#IB::Contract.all{|x| x.destroy }
raise "works only in db-backed mode " unless IB.db_backed?
end

# after(:all) { IB::Contract.all{|x| x.destroy }}#close_connection }
Expand All @@ -23,20 +22,20 @@

it{ expect( list_of_symbols ).to have_exactly(8).items }
context "BuildDB" do
let!( :number_of_datasets ){ list_of_symbols.size }


let( :db ){ list_of_symbols.map{|s| FactoryGirl.create( :default_stock, symbol:s).update_contract } }
let( :agu) { FactoryGirl.create( :default_stock, symbol:list_of_symbols.first )}
let( :agu_canada) { FactoryGirl.create :default_stock, symbol:'AGU', currency:'CAD' }
it{ expect( db ).to have_exactly(number_of_datasets).items }
it{ expect( IB::Contract.count).to eq number_of_datasets }
it "update contract_data " do
IB::Contract.all.each do |y|
expect{ y.update_contract }.not_to change{ y }
end
end
# it "try to save duplicate Dataset" do
let!( :number_of_datasets ){ list_of_symbols.size }


let( :db ){ list_of_symbols.map{|s| is= IB::Stock.new(symbol:s); is.verify; is } }
let( :agu) { IB::Stock.new( symbol:list_of_symbols.first )}
let( :agu_canada) { IB::Stock.new symbol:'AGU', currency:'CAD' }
it{ expect( db ).to have_exactly(number_of_datasets).items }
it{ expect( IB::Contract.count).to eq number_of_datasets }
it "update contract_data ", focus:true do
db.each do |y|
expect{ y.verify }.not_to change{ y }
end
end
# it "try to save duplicate Dataset" do
# expect{ agu.read_contract_from_tws }.to raise_error ActiveRecord::RecordNotUnique
# # important: ensure that invalid datasets are destroyed immediately
# agu.destroy
Expand Down
30 changes: 11 additions & 19 deletions spec/integration/contract_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

after(:all) { close_connection }

context "Request Stock data" do
context "Request Stock data - the manual way" do

before(:all) do
@contract = IB::Stock.new :symbol => 'AAPL'
Expand Down Expand Up @@ -35,7 +35,7 @@
contract = msg.contract
detail = msg.contract_detail

expect( contract.symbol).to eq 'AAPL'
expect( contract.symbol).to eq 'AAPL'
expect( contract.local_symbol).to match /AAPL|APC/
expect( contract.con_id).to be_an Integer
expect( contract.expiry).to eq ''
Expand All @@ -57,7 +57,7 @@
end
end # Stock

context "Request Option contract data" do
context "Request Option contract data via verify" do
before(:all) do
@contract= IB::Option.new( symbol:'F', strike:15, right: :put, expiry:'201509' )
@contract.verify
Expand Down Expand Up @@ -99,29 +99,21 @@
context "Request Forex contract data" do

before(:all) do
@contract = IB::Contract.new :symbol => 'EUR', # EURUSD pair
:currency => "USD",
:exchange => "IDEALPRO",
:sec_type => :forex
@ib.send_message :RequestContractData, :id => 135, :contract => @contract
@ib.wait_for :ContractDataEnd, 3 # sec
@contract = IB::Forex.new( symbol:'EUR')
@contract.verify
end

after(:all) { clean_connection } # Clear logs and message collector

subject { @ib.received[:ContractData].first }
subject { @contract }
it { is_expected.to be_a IB::Forex }
it { is_expected.to be_valid }

it { @ib.received[:ContractData].should have_exactly(1).contract_data }
it { @ib.received[:ContractDataEnd].should have_exactly(1).contract_data_end }

it 'receives Contract Data for requested contract' do
subject.request_id.should == 135
subject.contract.should be_valid
end

it 'receives Contract Data with extended fields' do
contract = subject.contract
detail = subject.contract_detail
contract = @contract
detail = contract.contract_detail

expect( contract.symbol).to eq 'EUR'
expect( contract.local_symbol).to eq 'EUR.USD'
Expand All @@ -144,7 +136,7 @@
end
end # Request Forex data

context "Request Futures contract data" do
context "Request Futures contract datai – the manual way" do

before(:all) do
@contract = IB::Symbols::Futures[:ym] # Mini Dow Jones Industrial
Expand Down

0 comments on commit 53a9d67

Please sign in to comment.