Skip to content

Commit ecc29da

Browse files
author
Hartmut Bischoff
committed
Fixing Error in fetching ContractDetail when strikes below 1 where entered
1 parent 045bb23 commit ecc29da

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

lib/models/ib/contract.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ def default_attributes # :nodoc:
136136

137137
def serialize *fields # :nodoc:
138138
print_default = ->(field, default="") { field.blank? ? default : field }
139-
print_not_zero = ->(field, default="") { field.to_i.zero? ? default : field }
139+
## Non numeric entries are passed untouched, only 0 is converted to the default value
140+
## Thus: a Zero-Strike-Option has to be defined with «strike: "0"»
141+
print_not_zero = ->(field, default="") { field.is_a?(Numeric) && field.zero? ? default : field }
140142
[(con_id.present? && !con_id.is_a?(Symbol) && con_id.to_i > 0 ? con_id : ""),
141143
print_default[symbol],
142144
print_default[self[:sec_type]],
143145
( fields.include?(:option) ?
144146
[ print_default[expiry],
145-
print_not_zero[strike],
147+
print_default[strike],
146148
print_default[self[:right]],
147149
print_default[multiplier]] : nil ),
148150
print_default[exchange],

spec/contract_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def request_con_id sample: SAMPLE
2626
end
2727

2828
RSpec.shared_examples 'a complete Contract Object' do
29-
subject{ the_contract }
29+
# subject{ the_contract }
3030
it_behaves_like 'a valid Contract Object'
3131
it { is_expected.to be_an IB::Contract }
3232
its( :contract_detail ){ is_expected.to be_a IB::ContractDetail }

spec/ib/messages/incoming/contract_data_spec.rb

+22-10
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,41 @@
44

55
RSpec.describe IB::Messages::Incoming::ContractData do
66

7+
before(:all) do
8+
establish_connection
9+
end
10+
11+
after(:all) { close_connection }
12+
713
context IB::Stock do
814
before(:all) do
9-
establish_connection
1015
ib = IB::Connection.current
11-
ib.send_message :RequestContractDetails, contract: IB::Contract.new( sec_type: 'STK', symbol: 'GE', currency: 'USD', exchange:'SMART' )
16+
ib.send_message :RequestContractDetails, contract: IB::Stock.new( symbol: 'GE', currency: 'USD', exchange:'SMART' )
1217
ib.wait_for :ContractDetailsEnd
1318
end
1419

15-
after(:all) { close_connection }
20+
after(:all){ IB::Connection.current.clear_received :ContractData }
21+
1622

1723
# it_behaves_like 'ContractData Message' do
1824
# let( :the_message ){ IB::Connection.current.received[:ContractData].first }
1925
# end
26+
context "Basics" do
27+
subject{ IB::Connection.current.received[:ContractData].contract.last }
2028

21-
it "inspects" do # debugging
22-
ib = IB::Connection.current
23-
contract = ib.received[:ContractData].contract.last
24-
contract_details = ib.received[:ContractData].contract_details.last
25-
26-
puts contract.inspect
27-
puts contract_details.inspect
29+
it_behaves_like 'a complete Contract Object'
30+
its( :sec_type ){is_expected.to eq :stock}
31+
its( :symbol ){is_expected.to eq 'GE'}
32+
its( :con_id ){is_expected.to eq 7516}
2833
end
2934

35+
context "received a single contract" do
36+
subject{ IB::Connection.current.received[:ContractData] }
37+
it{ is_expected.to be_a Array }
38+
its(:size){is_expected.to eq 1 }
39+
end
3040
end
41+
42+
3143
end # describe IB::Messages:Incoming
3244

0 commit comments

Comments
 (0)