ib-ruby 0.7.11 → 0.7.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/HISTORY +4 -0
- data/VERSION +1 -1
- data/db/migrate/121_add_order_states.rb +6 -6
- data/db/migrate/141_add_combo_legs.rb +7 -6
- data/lib/ib-ruby/models/bag.rb +2 -7
- data/lib/ib-ruby/models/combo_leg.rb +8 -4
- data/lib/ib-ruby/models/contract.rb +23 -16
- data/lib/ib-ruby/models/model.rb +3 -3
- data/lib/ib-ruby/models/model_properties.rb +50 -50
- data/lib/ib-ruby/models/option.rb +1 -1
- data/spec/db_helper.rb +57 -54
- data/spec/ib-ruby/models/bag_spec.rb +38 -51
- data/spec/ib-ruby/models/bar_spec.rb +36 -35
- data/spec/ib-ruby/models/combo_leg_spec.rb +108 -31
- data/spec/ib-ruby/models/contract_detail_spec.rb +39 -47
- data/spec/ib-ruby/models/contract_spec.rb +57 -67
- data/spec/ib-ruby/models/execution_spec.rb +53 -56
- data/spec/ib-ruby/models/option_spec.rb +48 -48
- data/spec/ib-ruby/models/order_spec.rb +93 -95
- data/spec/ib-ruby/models/order_state_spec.rb +43 -56
- data/spec/ib-ruby/models/underlying_spec.rb +18 -25
- data/spec/integration/contract_info_spec.rb +4 -0
- data/spec/model_helper.rb +77 -32
- data/spec/test.rb +61 -0
- metadata +14 -34
data/spec/model_helper.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'db_helper'
|
3
3
|
|
4
|
+
[:props, :aliases, :errors, :assigns, :human, :associations, :collections].each do |aspect|
|
5
|
+
eval "def #{aspect}
|
6
|
+
(metadata[:#{aspect}] rescue example.metadata[:#{aspect}]) || {}
|
7
|
+
end"
|
8
|
+
end
|
9
|
+
|
4
10
|
def codes_and_values_for property
|
5
11
|
Hash[IB::VALUES[property].map { |code, value| [[code, value], value] }]
|
6
12
|
end
|
@@ -79,7 +85,6 @@ def test_assigns cases, prop, name
|
|
79
85
|
|
80
86
|
# For all test cases given as an Array [res1, res2] or Hash {val => res} ...
|
81
87
|
(cases.is_a?(Array) ? cases.map { |e| [e, e] } : cases).each do |values, result|
|
82
|
-
#p prop, cases
|
83
88
|
|
84
89
|
# For all values in this test case ...
|
85
90
|
[values].flatten.each do |value|
|
@@ -87,22 +92,31 @@ def test_assigns cases, prop, name
|
|
87
92
|
|
88
93
|
# Assigning this value to a property results in ...
|
89
94
|
case result
|
90
|
-
|
95
|
+
when Exception # ... Exception
|
96
|
+
|
97
|
+
it "#{prop} = #{value.inspect} #=> raises #{result}" do
|
91
98
|
expect { subject.send "#{prop}=", value }.
|
92
99
|
to raise_error result
|
100
|
+
end
|
101
|
+
|
102
|
+
when Regexp # ... Non-exceptional error, making model invalid
|
103
|
+
|
104
|
+
it "#{prop} = #{value.inspect} #=> error #{result.to_s}" do
|
93
105
|
|
94
|
-
when Regexp # ... Non-exceptional error, making model invalid
|
95
106
|
expect { subject.send "#{prop}=", value }.to_not raise_error
|
96
|
-
subject.valid? # just triggers validation
|
97
107
|
|
98
|
-
#
|
108
|
+
subject.valid? # just triggers validation
|
109
|
+
#pp subject.errors.messages
|
99
110
|
|
100
111
|
subject.errors.messages.should have_key name
|
101
112
|
subject.should be_invalid
|
102
113
|
msg = subject.errors.messages[name].find { |msg| msg =~ result }
|
103
114
|
msg.should =~ result
|
115
|
+
end
|
104
116
|
|
105
|
-
|
117
|
+
else # ... correct uniform assignment to result
|
118
|
+
|
119
|
+
it "#{prop} = #{value.inspect} #=> #{result.inspect}" do
|
106
120
|
|
107
121
|
was_valid = subject.valid?
|
108
122
|
expect { subject.send "#{prop}=", value }.to_not raise_error
|
@@ -112,10 +126,13 @@ def test_assigns cases, prop, name
|
|
112
126
|
subject.errors.messages.should_not have_key name
|
113
127
|
subject.should be_valid
|
114
128
|
end
|
129
|
+
end
|
115
130
|
|
116
|
-
|
131
|
+
if name != prop # additional asserts for aliases
|
117
132
|
|
133
|
+
it "#{prop} alias assignment changes #{name} property, and vice versa" do
|
118
134
|
# Assignment to alias changes property as well
|
135
|
+
subject.send "#{prop}=", value
|
119
136
|
subject.send("#{name}").should == result
|
120
137
|
|
121
138
|
# Unsetting alias unsets property as well
|
@@ -127,37 +144,44 @@ def test_assigns cases, prop, name
|
|
127
144
|
subject.send "#{name}=", value
|
128
145
|
subject.send("#{prop}").should == result
|
129
146
|
end
|
147
|
+
end
|
130
148
|
end
|
131
149
|
end
|
132
150
|
end
|
133
151
|
end
|
134
152
|
|
135
|
-
shared_examples_for 'Model' do
|
153
|
+
shared_examples_for 'Model with valid defaults' do
|
136
154
|
context 'instantiation without properties' do
|
137
155
|
subject { described_class.new }
|
156
|
+
let(:init_with_props?) { false }
|
138
157
|
|
139
|
-
it_behaves_like 'Model
|
158
|
+
it_behaves_like 'Model properties'
|
159
|
+
it_behaves_like 'Valid Model'
|
140
160
|
end
|
141
161
|
|
142
162
|
context 'instantiation with properties' do
|
143
163
|
subject { described_class.new props }
|
144
164
|
|
145
165
|
it_behaves_like 'Model instantiated with properties'
|
166
|
+
end
|
167
|
+
end
|
146
168
|
|
169
|
+
shared_examples_for 'Model with invalid defaults' do
|
170
|
+
context 'instantiation without properties' do
|
171
|
+
subject { described_class.new }
|
147
172
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
end
|
173
|
+
it_behaves_like 'Model instantiated empty'
|
174
|
+
end
|
175
|
+
|
176
|
+
context 'instantiation with properties' do
|
177
|
+
subject { described_class.new props }
|
178
|
+
|
179
|
+
it_behaves_like 'Model instantiated with properties'
|
156
180
|
end
|
157
181
|
end
|
158
182
|
|
159
183
|
shared_examples_for 'Self-equal Model' do
|
160
|
-
subject { described_class.new
|
184
|
+
subject { described_class.new props }
|
161
185
|
|
162
186
|
it 'is self-equal ' do
|
163
187
|
should == subject
|
@@ -169,16 +193,17 @@ shared_examples_for 'Self-equal Model' do
|
|
169
193
|
end
|
170
194
|
|
171
195
|
shared_examples_for 'Model instantiated empty' do
|
196
|
+
let(:init_with_props?) { false }
|
172
197
|
it { should_not be_nil }
|
173
198
|
|
174
199
|
it 'sets all properties to defaults' do
|
175
200
|
subject.default_attributes.each do |name, value|
|
176
201
|
#p name, value
|
177
202
|
case value
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
203
|
+
when Time
|
204
|
+
subject.send(name).should be_a Time
|
205
|
+
else
|
206
|
+
subject.send(name).should == value
|
182
207
|
end
|
183
208
|
end
|
184
209
|
end
|
@@ -188,6 +213,8 @@ shared_examples_for 'Model instantiated empty' do
|
|
188
213
|
end
|
189
214
|
|
190
215
|
shared_examples_for 'Model instantiated with properties' do
|
216
|
+
let(:init_with_props?) { true }
|
217
|
+
|
191
218
|
it 'auto-assigns all properties given to initializer' do
|
192
219
|
props.each do |name, value|
|
193
220
|
#p subject, name, value
|
@@ -195,6 +222,15 @@ shared_examples_for 'Model instantiated with properties' do
|
|
195
222
|
end
|
196
223
|
end
|
197
224
|
|
225
|
+
it 'has correct human-readeable format' do
|
226
|
+
case human
|
227
|
+
when Regexp
|
228
|
+
subject.to_human.should =~ human
|
229
|
+
else
|
230
|
+
subject.to_human.should == human
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
198
234
|
it_behaves_like 'Model properties'
|
199
235
|
it_behaves_like 'Valid Model'
|
200
236
|
end
|
@@ -222,21 +258,30 @@ shared_examples_for 'Model properties' do
|
|
222
258
|
}.to_not raise_error
|
223
259
|
end
|
224
260
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
261
|
+
props.each do |name, value|
|
262
|
+
it "#{name} = #{value.inspect} #=> does not raise" do
|
263
|
+
expect {
|
264
|
+
subject.send("#{name}=", value)
|
265
|
+
subject.send(name).should == value
|
266
|
+
}.to_not raise_error
|
267
|
+
end
|
268
|
+
end
|
229
269
|
|
270
|
+
assigns.each do |props, cases|
|
271
|
+
[props].flatten.each do |prop|
|
272
|
+
# For each given property ...
|
273
|
+
test_assigns cases, prop, prop
|
230
274
|
end
|
231
275
|
end
|
232
276
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
277
|
+
aliases.each do |alinames, cases|
|
278
|
+
name, aliases = *alinames
|
279
|
+
# For each original property or alias...
|
280
|
+
[name, aliases].flatten.each do |prop|
|
281
|
+
test_assigns cases, prop, name
|
238
282
|
end
|
239
283
|
end
|
284
|
+
|
240
285
|
end
|
241
286
|
|
242
287
|
shared_examples_for 'Valid Model' do
|
@@ -255,7 +300,7 @@ shared_examples_for 'Invalid Model' do
|
|
255
300
|
subject.should_not be_valid
|
256
301
|
subject.should be_invalid
|
257
302
|
subject.errors.should_not be_empty
|
258
|
-
subject.errors.messages.should == errors
|
303
|
+
subject.errors.messages.should == errors
|
259
304
|
end
|
260
305
|
|
261
306
|
it_behaves_like 'Invalid DB-backed Model'
|
data/spec/test.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This script retrieves list of all Orders from TWS
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'bundler/setup'
|
7
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
8
|
+
|
9
|
+
require 'yaml'
|
10
|
+
require 'pathname'
|
11
|
+
require 'ib-ruby/db'
|
12
|
+
|
13
|
+
# Load DB config, determine correct environment
|
14
|
+
db_file = Pathname.new(__FILE__).realpath.dirname + '../db/config.yml'
|
15
|
+
raise "Unable to find DB config file: #{db_file}" unless db_file.exist?
|
16
|
+
|
17
|
+
env = RUBY_PLATFORM =~ /java/ ? 'test' : 'test-mri'
|
18
|
+
db_config = YAML::load_file(db_file)[env]
|
19
|
+
|
20
|
+
# Establish connection to test DB
|
21
|
+
IB::DB.connect db_config
|
22
|
+
|
23
|
+
require 'ib-ruby'
|
24
|
+
|
25
|
+
# Connect to IB as 0 (TWS) to retrieve all Orders, including TWS-generated ones
|
26
|
+
ib = IB::Connection.new :client_id => 0 #, :port => 7496 # TWS
|
27
|
+
|
28
|
+
## Subscribe to TWS alerts/errors and order-related messages
|
29
|
+
#@counter = 0
|
30
|
+
#
|
31
|
+
#ib.subscribe(:Alert, :OrderStatus, :OpenOrderEnd) { |msg| puts msg.to_human }
|
32
|
+
#
|
33
|
+
#ib.subscribe(:OpenOrder) do |msg|
|
34
|
+
# @counter += 1
|
35
|
+
# puts "#{@counter}: #{msg.to_human}"
|
36
|
+
# #pp msg.order
|
37
|
+
#end
|
38
|
+
#
|
39
|
+
#ib.send_message :RequestAllOpenOrders
|
40
|
+
#
|
41
|
+
## Wait for IB to respond to our request
|
42
|
+
#ib.wait_for :OpenOrderEnd
|
43
|
+
#sleep 1 # Let printer do the job
|
44
|
+
|
45
|
+
combo = IB::Bag.new
|
46
|
+
|
47
|
+
google = IB::Option.new(:symbol => 'GOOG',
|
48
|
+
:expiry => 201301,
|
49
|
+
:right => :call,
|
50
|
+
:strike => 500)
|
51
|
+
|
52
|
+
combo.leg_contracts << google
|
53
|
+
p combo.leg_contracts
|
54
|
+
p combo.save
|
55
|
+
|
56
|
+
#combo.legs.should_not be_empty
|
57
|
+
p combo.leg_contracts
|
58
|
+
p google.combo
|
59
|
+
|
60
|
+
leg = combo.legs.first
|
61
|
+
p google.leg
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: ib-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.7.
|
5
|
+
version: 0.7.12
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Paul Legato
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2012-04-
|
14
|
+
date: 2012-04-27 00:00:00 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
@@ -35,83 +35,61 @@ dependencies:
|
|
35
35
|
version: 0.0.1
|
36
36
|
type: :runtime
|
37
37
|
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: activerecord-jdbcsqlite3-adapter
|
40
|
-
prerelease: false
|
41
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
|
-
requirements:
|
44
|
-
- - ">="
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: 1.2.2
|
47
|
-
type: :runtime
|
48
|
-
version_requirements: *id003
|
49
|
-
- !ruby/object:Gem::Dependency
|
50
|
-
name: jdbc-sqlite3
|
51
|
-
prerelease: false
|
52
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
-
none: false
|
54
|
-
requirements:
|
55
|
-
- - ">="
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version: 3.7.2
|
58
|
-
type: :runtime
|
59
|
-
version_requirements: *id004
|
60
38
|
- !ruby/object:Gem::Dependency
|
61
39
|
name: xml-simple
|
62
40
|
prerelease: false
|
63
|
-
requirement: &
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
64
42
|
none: false
|
65
43
|
requirements:
|
66
44
|
- - ">="
|
67
45
|
- !ruby/object:Gem::Version
|
68
46
|
version: 1.1.1
|
69
47
|
type: :runtime
|
70
|
-
version_requirements: *
|
48
|
+
version_requirements: *id003
|
71
49
|
- !ruby/object:Gem::Dependency
|
72
50
|
name: standalone_migrations
|
73
51
|
prerelease: false
|
74
|
-
requirement: &
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
75
53
|
none: false
|
76
54
|
requirements:
|
77
55
|
- - ">="
|
78
56
|
- !ruby/object:Gem::Version
|
79
57
|
version: "0"
|
80
58
|
type: :runtime
|
81
|
-
version_requirements: *
|
59
|
+
version_requirements: *id004
|
82
60
|
- !ruby/object:Gem::Dependency
|
83
61
|
name: database_cleaner
|
84
62
|
prerelease: false
|
85
|
-
requirement: &
|
63
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
86
64
|
none: false
|
87
65
|
requirements:
|
88
66
|
- - ">="
|
89
67
|
- !ruby/object:Gem::Version
|
90
68
|
version: 2.8.0
|
91
69
|
type: :development
|
92
|
-
version_requirements: *
|
70
|
+
version_requirements: *id005
|
93
71
|
- !ruby/object:Gem::Dependency
|
94
72
|
name: rspec
|
95
73
|
prerelease: false
|
96
|
-
requirement: &
|
74
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
97
75
|
none: false
|
98
76
|
requirements:
|
99
77
|
- - ">="
|
100
78
|
- !ruby/object:Gem::Version
|
101
79
|
version: 2.9.0
|
102
80
|
type: :development
|
103
|
-
version_requirements: *
|
81
|
+
version_requirements: *id006
|
104
82
|
- !ruby/object:Gem::Dependency
|
105
83
|
name: my_scripts
|
106
84
|
prerelease: false
|
107
|
-
requirement: &
|
85
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
108
86
|
none: false
|
109
87
|
requirements:
|
110
88
|
- - ">="
|
111
89
|
- !ruby/object:Gem::Version
|
112
90
|
version: "0"
|
113
91
|
type: :development
|
114
|
-
version_requirements: *
|
92
|
+
version_requirements: *id007
|
115
93
|
description: Ruby Implementation of the Interactive Brokers TWS API
|
116
94
|
email:
|
117
95
|
- pjlegato@gmail.com
|
@@ -212,6 +190,7 @@ files:
|
|
212
190
|
- spec/order_helper.rb
|
213
191
|
- spec/README.md
|
214
192
|
- spec/spec_helper.rb
|
193
|
+
- spec/test.rb
|
215
194
|
- spec/TODO
|
216
195
|
- spec/v.rb
|
217
196
|
- spec/ib-ruby/connection_spec.rb
|
@@ -301,6 +280,7 @@ test_files:
|
|
301
280
|
- spec/order_helper.rb
|
302
281
|
- spec/README.md
|
303
282
|
- spec/spec_helper.rb
|
283
|
+
- spec/test.rb
|
304
284
|
- spec/TODO
|
305
285
|
- spec/v.rb
|
306
286
|
- spec/ib-ruby/connection_spec.rb
|