ib-extensions 1.0

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.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +6 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +9 -0
  7. data/Gemfile.lock +112 -0
  8. data/Guardfile +24 -0
  9. data/README.md +99 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +96 -0
  12. data/bin/console.yml +3 -0
  13. data/bin/gateway.rb +97 -0
  14. data/bin/setup +8 -0
  15. data/changelog.md +31 -0
  16. data/examples/cancel_orders +74 -0
  17. data/examples/eod +35 -0
  18. data/examples/input.rb +475 -0
  19. data/examples/market_price +57 -0
  20. data/examples/option_chain +67 -0
  21. data/examples/place_and_modify_order +162 -0
  22. data/examples/place_bracket_order +62 -0
  23. data/examples/place_butterfly_order +104 -0
  24. data/examples/place_combo_order +70 -0
  25. data/examples/place_limit_order +82 -0
  26. data/examples/place_the_limit_order +145 -0
  27. data/examples/volatility_research +139 -0
  28. data/examples/what_if_order +90 -0
  29. data/ib-extensions.gemspec +37 -0
  30. data/lib/ib-gateway.rb +5 -0
  31. data/lib/ib/alerts/base-alert.rb +128 -0
  32. data/lib/ib/alerts/gateway-alerts.rb +15 -0
  33. data/lib/ib/alerts/order-alerts.rb +68 -0
  34. data/lib/ib/eod.rb +152 -0
  35. data/lib/ib/extensions.rb +9 -0
  36. data/lib/ib/extensions/contract.rb +37 -0
  37. data/lib/ib/extensions/version.rb +5 -0
  38. data/lib/ib/flex.rb +150 -0
  39. data/lib/ib/gateway.rb +425 -0
  40. data/lib/ib/gateway/account-infos.rb +115 -0
  41. data/lib/ib/gateway/order-handling.rb +150 -0
  42. data/lib/ib/market-price.rb +134 -0
  43. data/lib/ib/models/account.rb +329 -0
  44. data/lib/ib/models/spread.rb +159 -0
  45. data/lib/ib/option-chain.rb +198 -0
  46. data/lib/ib/option-greeks.rb +88 -0
  47. data/lib/ib/order-prototypes.rb +110 -0
  48. data/lib/ib/order_prototypes/abstract.rb +67 -0
  49. data/lib/ib/order_prototypes/combo.rb +46 -0
  50. data/lib/ib/order_prototypes/forex.rb +40 -0
  51. data/lib/ib/order_prototypes/limit.rb +177 -0
  52. data/lib/ib/order_prototypes/market.rb +116 -0
  53. data/lib/ib/order_prototypes/pegged.rb +173 -0
  54. data/lib/ib/order_prototypes/premarket.rb +31 -0
  55. data/lib/ib/order_prototypes/stop.rb +202 -0
  56. data/lib/ib/order_prototypes/volatility.rb +39 -0
  57. data/lib/ib/spread-prototypes.rb +62 -0
  58. data/lib/ib/spread_prototypes/butterfly.rb +79 -0
  59. data/lib/ib/spread_prototypes/calendar.rb +85 -0
  60. data/lib/ib/spread_prototypes/stock-spread.rb +48 -0
  61. data/lib/ib/spread_prototypes/straddle.rb +75 -0
  62. data/lib/ib/spread_prototypes/strangle.rb +96 -0
  63. data/lib/ib/spread_prototypes/vertical.rb +84 -0
  64. data/lib/ib/verify.rb +226 -0
  65. metadata +206 -0
@@ -0,0 +1,3 @@
1
+ :gateway: 4002
2
+ :tws: 7496
3
+ :client_id: 2000
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+ ### loads the active-orient environment
3
+ ### and starts an interactive shell
4
+ ###
5
+ ### Parameter: t)ws | g)ateway (or number of port ) Default: Gateway ,
6
+ ### client_id , Default 2000
7
+ require 'bundler/setup'
8
+ require 'yaml'
9
+
10
+ require 'logger'
11
+ LogLevel = Logger::DEBUG ##INFO # DEBUG # ERROR
12
+ #require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
13
+
14
+ require 'ib-gateway'
15
+ require 'ib/market-price'
16
+ require 'ib/option-chain'
17
+ require 'ib/eod'
18
+ require 'ib/symbols'
19
+ class Array
20
+ # enables calling members of an array. which are hashes by it name
21
+ # i.e
22
+ #
23
+ # 2.5.0 :006 > C.received[:OpenOrder].local_id
24
+ # => [16, 17, 21, 20, 19, 8, 7]
25
+ # 2.5.0 :007 > C.received[:OpenOrder].contract.to_human
26
+ # => ["<Bag: IECombo SMART USD legs: >", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: WFC USD>", "<Stock: WFC USD>"]
27
+ #
28
+ # its included only in the console, for inspection purposes
29
+
30
+ def method_missing(method, *key)
31
+ unless method == :to_hash || method == :to_str #|| method == :to_int
32
+ return self.map{|x| x.public_send(method, *key)}
33
+ end
34
+
35
+ end
36
+ end # Array
37
+
38
+ # read items from console.yml
39
+ read_yml = -> (key) do
40
+ YAML::load_file( File.expand_path('../console.yml',__FILE__))[key]
41
+ end
42
+
43
+
44
+
45
+ puts
46
+ puts ">> I B – G A T E W A Y Interactive Console <<"
47
+ puts '-'* 45
48
+ puts
49
+ puts "Namespace is IB ! "
50
+ puts
51
+ puts '-'* 45
52
+
53
+ include IB
54
+ require 'irb'
55
+ client_id = ARGV[1] || read_yml[:client_id]
56
+ specified_port = ARGV[0] || 'Gateway'
57
+ port = case specified_port
58
+ when Integer
59
+ specified_port # just use the number
60
+ when /^[gG]/
61
+ read_yml[:gateway]
62
+ when /^[Tt]/
63
+ read_yml[:tws]
64
+ end
65
+
66
+ ARGV.clear
67
+ logger = Logger.new STDOUT
68
+ logger.formatter = proc do |level, time, prog, msg|
69
+ "#{time.strftime('%H:%M:%S')} #{msg}\n"
70
+ end
71
+ logger.level = Logger::INFO
72
+
73
+ ## The Block takes instructions which are executed after initializing all instance-variables
74
+ ## and prior to the connection-process
75
+ ## Here we just subscribe to some events
76
+ 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]
80
+ rescue IB::TransmissionError => e
81
+ puts "E: #{e.inspect}"
82
+ end
83
+
84
+ C = G.tws
85
+ unless C.received[:OpenOrder].blank?
86
+ puts "------------------------------- OpenOrders ----------------------------------"
87
+ puts C.received[:OpenOrder].to_human.join "\n"
88
+ end
89
+ puts "Connection established on Port #{port}, client_id #{client_id} used"
90
+ puts
91
+ puts "----> G points to the Gateway-Instance"
92
+ puts "----> C points to the Connection-Instance"
93
+ puts
94
+ puts "some basic Messages are subscribed and accordingly displayed"
95
+ puts '-'* 45
96
+
97
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+
2
+ * lib/gateway.rb
3
+ * rename `active_accounts` to `clients`
4
+ Clients are just tradeable `IB::Accounts`.
5
+
6
+ * rename ' for_active_accounts' to `account_data`
7
+ Account-data provides a thead-safe access to contrats, portfolio- and account-values auf a given account.
8
+
9
+ * rename `simple_account_data_scan` to `account_Data_scan`
10
+
11
+ * connect: raises an error if connection is not possible because of using a not unique client_id
12
+
13
+ * initialize:: tolerates arguments other then specified.
14
+
15
+ * made methods that never should call by users private:
16
+ check_connection, initialize_alerts, initialize_managed_accounts, prepare_connection
17
+
18
+ * lib/ib/gateway
19
+
20
+ Directory contains gateway related stuff
21
+
22
+ * account-infos.rb
23
+ * order-handling.rb
24
+
25
+ * lib/ib/alerts
26
+
27
+ * included specific alert-definitions in this directory:
28
+ order-alerts, gateway-alerts
29
+
30
+
31
+
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This script allows you to cancel either a set of open Orders by their ids,
4
+ # or ALL Orders opened via IB API at once. The latter is useful when your
5
+ # robot goes crazy and opens gazillions of wrong limit orders.
6
+ #
7
+ # Accepts the port to connect to as first command-parameter, T)ws or G)ateway, which is the default
8
+ #
9
+ # Numeric Parameter are interpreted as keys for specific orders to kill
10
+
11
+ require 'bundler/setup'
12
+ require 'ib-api'
13
+ specified_port = ARGV[0] || 'Gateway'
14
+ port = case specified_port
15
+ when /^[gG]/
16
+ ARGV.shift # consume the first argument
17
+ 4002
18
+ when /^[Tt]/
19
+ ARGV.shift # consume the first argument
20
+ 7497
21
+ end
22
+
23
+ # First, connect to IB TWS.
24
+ ib = IB::Connection.new client_id: 1111, port: port do | gw |
25
+
26
+ # Subscribe to TWS alerts/errors and order-related messages
27
+ gw.subscribe(:Alert, :OpenOrder, :OrderStatus, :OpenOrderEnd) { |msg| puts msg.to_human }
28
+ # Set log level
29
+ gw.logger.level = Logger::FATAL # DEBUG -- INFO -- WARN -- ERROR -- FATAL
30
+ end
31
+
32
+ if ARGV.empty?
33
+ ib.send_message :RequestGlobalCancel
34
+ else
35
+ puts "ARGV: #{ARGV}"
36
+ # Will only work for Orders placed under the same :client_id
37
+ ib.cancel_order *ARGV
38
+ puts '-'*55
39
+ puts "Remaining Orders"
40
+ puts '-'*55
41
+ end
42
+
43
+ puts '-'*55
44
+ puts "Remaining Orders"
45
+ ib.send_message :RequestAllOpenOrders
46
+ puts '-'*55
47
+
48
+ sleep 3
49
+
50
+
51
+ ## Expected output
52
+ #12:20:25.154 Connected to server, version: 137,
53
+ # connection time: 2018-02-27 12:20:25 +0000 local, 2018-02-27T12:20:25+00:00 remote.
54
+ #12:20:25.156 Got message 5 (IB::Messages::Incoming::OpenOrder)
55
+ #<OpenOrder: <Stock: WFC USD> <Order: LMT GTC buy 100.0 1.13 Submitted #1/1562725191 from 1112/DU167348 fee 0.0>>
56
+ #12:20:25.158 Got message 3 (IB::Messages::Incoming::OrderStatus)
57
+ #<OrderStatus: <OrderState: Submitted #1/1562725191 from 1112 filled 0.0/100.0 at 0.0/0.0 why_held >>
58
+ #12:20:25.197 Got message 53 (IB::Messages::Incoming::OpenOrderEnd)
59
+ #<OpenOrderEnd: >
60
+ #12:20:25.197 Got message 15 (IB::Messages::Incoming::ManagedAccounts)
61
+ #12:20:25.197 No subscribers for message IB::Messages::Incoming::ManagedAccounts!
62
+ #12:20:25.197 Got message 9 (IB::Messages::Incoming::NextValidId)
63
+ #12:20:25.197 Got next valid order id: 2.
64
+ #12:20:25.254 Got message 5 (IB::Messages::Incoming::OpenOrder)
65
+ #<OpenOrder: <Stock: WFC USD> <Order: LMT GTC buy 100.0 1.13 PendingCancel #1/1562725191 from 1112/DU167348 fee 0.0>>
66
+ #12:20:25.256 Got message 3 (IB::Messages::Incoming::OrderStatus)
67
+ #<OrderStatus: <OrderState: PendingCancel #1/1562725191 from 1112 filled 0.0/100.0 at 0.0/0.0 why_held >>
68
+ #12:20:25.297 Got message 53 (IB::Messages::Incoming::OpenOrderEnd)
69
+ #<OpenOrderEnd: >
70
+ #12:20:25.342 Got message 3 (IB::Messages::Incoming::OrderStatus)
71
+ #<OrderStatus: <OrderState: Cancelled #1/1562725191 from 1112 filled 0.0/100.0 at 0.0/0.0 why_held >>
72
+ #12:20:25.343 Got message 4 (IB::Messages::Incoming::Alert)
73
+ #TWS Error 202: Order Canceled - reason:
74
+ ##
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This script downloads historic data for specific symbols from IB
4
+
5
+ require 'bundler/setup'
6
+ require 'ib-api'
7
+ require 'ib/symbols'
8
+ require 'ib/eod'
9
+
10
+ # Definition of what we want data for. We have to keep track of what ticker id
11
+ # corresponds to what symbol ourselves, because the ticks don't include any other
12
+ # identifying information. The choice of ticker ids is, as far as I can tell, arbitrary.
13
+ # Predefined Symbol-definitions --> lib/ib/symbols
14
+ contracts = [ IB::Symbols::Futures.mini_dax,
15
+ IB::Stock.new( symbol: 'BAS', currency: 'EUR') , # BASF
16
+ IB::Symbols::Forex.eurusd
17
+ ]
18
+
19
+
20
+ # Connect to IB TWS.
21
+ #ib = IB::Connection.new( :client_id => 1102, port: 7496, host: '10.222.148.177' ) do | gw |
22
+ ib = IB::Connection.new :client_id => 1112, :port => 7496 do | gw | #, :port => 7497 # TWS
23
+
24
+ # Subscribe to TWS alerts/errors
25
+ gw.subscribe(:Alert, :ManagedAccounts) { |msg| puts msg.to_human }
26
+
27
+ end
28
+ contracts.each do | c |
29
+ c.eod( duration: '10 d', to: Date.today){|y| y.each{|z| puts z.to_human}}
30
+ end
31
+
32
+ puts "\n******** Press <Enter> to exit... *********\n\n"
33
+ STDIN.gets
34
+
35
+
@@ -0,0 +1,475 @@
1
+
2
+ require 'bundler/setup'
3
+ require 'ib/symbols'
4
+ require 'yaml'
5
+
6
+
7
+ # First, connect to IB TWS and subscribe for events.
8
+
9
+ IB::Symbols.allocate_collection :w500
10
+ IB::Symbols::W500.purge_collection
11
+
12
+ IB::Symbols.allocate_collection :w500
13
+
14
+
15
+ symbols = %w(
16
+ DD
17
+ MMM
18
+ WBAI
19
+ WUBA
20
+ EGHT
21
+ AHC
22
+ ATEN
23
+ AAC
24
+ AIR
25
+ AAN
26
+ ABB
27
+ ABT
28
+ ABBV
29
+ ANF
30
+ ACP
31
+ JEQ
32
+ ABM
33
+ AKR
34
+ ACN
35
+ ACCO
36
+ ATV
37
+ ATU
38
+ AYI
39
+ GOLF
40
+ ADX
41
+ PEO
42
+ AGRO
43
+ ADNT
44
+ ADT
45
+ ATGE
46
+ AAP
47
+ ADSW
48
+ WMS
49
+ ASIX
50
+ AAV
51
+ AVK
52
+ AGC
53
+ LCM
54
+ ACM
55
+ ANW
56
+ AEB
57
+ AED
58
+ AEG
59
+ AEH
60
+ AER
61
+ HIVE
62
+ AJRD
63
+ AET
64
+ AMG
65
+ AFL
66
+ MITT
67
+ AGCO
68
+ A
69
+ AEM
70
+ ADC
71
+ AL
72
+ APD
73
+ AYR
74
+ AKS
75
+ ALG
76
+ AGI
77
+ ALK
78
+ AIN
79
+ ALB
80
+ AA
81
+ ALEX
82
+ ALX
83
+ ARE
84
+ AQN
85
+ BABA
86
+ Y
87
+ ATI
88
+ ALLE
89
+ AGN
90
+ ALE
91
+ AKP
92
+ ADS
93
+ AFB
94
+ AOI
95
+ AWF
96
+ AB
97
+ LNT
98
+ CBH
99
+ NCV
100
+ NCZ
101
+ ACV
102
+ NIE
103
+ NFJ
104
+ ALSN
105
+ ALLY
106
+ AGD
107
+ AWP
108
+ AOD
109
+ AYX
110
+ ATUS
111
+ MO
112
+ ACH
113
+ AMBR
114
+ ABEV
115
+ AMC
116
+ AEE
117
+ AMRC
118
+ AMOV
119
+ AMX
120
+ AAT
121
+ AXL
122
+ ACC
123
+ AEO
124
+ AEP
125
+ AEL
126
+ AXP
127
+ AFG
128
+ AFGE
129
+ AFGH
130
+ AMH
131
+ AIG
132
+ AMID
133
+ ARL
134
+ ARA
135
+ AWR
136
+ AMT
137
+ AVD
138
+ AWK
139
+ COLD
140
+ APU
141
+ AMP
142
+ ABC
143
+ ANFI
144
+ AMN
145
+ AP
146
+ APH
147
+ AXR
148
+ AME
149
+ AFSS
150
+ AFST
151
+ AEUA
152
+ APC
153
+ ANDV
154
+ ANDX
155
+ AU
156
+ BUD
157
+ AXE
158
+ NLY
159
+ AMGP
160
+ AM
161
+ AR
162
+ ANTM
163
+ ANH
164
+ AON
165
+ APA
166
+ AIV
167
+ ARI
168
+ APO
169
+ AIY
170
+ AFT
171
+ AIF
172
+ APLE
173
+ AIT
174
+ ATR
175
+ APTV
176
+ WTR
177
+ AQ
178
+ WAAS
179
+ ARMK
180
+ ABR
181
+ ARC
182
+ MT
183
+ ARCH
184
+ ADM
185
+ AROC
186
+ ARNC
187
+ ARCO
188
+ RCUS
189
+ ARD
190
+ ASC
191
+ AFC
192
+ ACRE
193
+ ARDC
194
+ ARES
195
+ AGX
196
+ ANET
197
+ AI
198
+ AIC
199
+ AIW
200
+ AHH
201
+ ARR
202
+ AFI
203
+ AWI
204
+ ARW
205
+ AJG
206
+ APAM
207
+ ASA
208
+ ABG
209
+ ASX
210
+ ASGN
211
+ AHT
212
+ ASH
213
+ APB
214
+ ASPN
215
+ AHL
216
+ ASB
217
+ AC
218
+ AIZ
219
+ AIZP
220
+ AGO
221
+ AZN
222
+ HOME
223
+ T
224
+ TBB
225
+ ATTO
226
+ ATH
227
+ ATKR
228
+ AT
229
+ ATO
230
+ AUO
231
+ ATHM
232
+ ALV
233
+ AN
234
+ AZO
235
+ AVB
236
+ AGR
237
+ AVYA
238
+ AVY
239
+ AVH
240
+ AVA
241
+ AVT
242
+ AVP
243
+ AVX
244
+ AXTA
245
+ AXS
246
+ AZUL
247
+ AZRE
248
+ AZZ
249
+ BGS
250
+ BW
251
+ BGH
252
+ BMI
253
+ BHGE
254
+ BBN
255
+ BLL
256
+ BANC
257
+ BBVA
258
+ BBD
259
+ BBDO
260
+ BCH
261
+ BLX
262
+ BSMX
263
+ BSBR
264
+ BSAC
265
+ SAN
266
+ CIB
267
+ BXS
268
+ BAC
269
+ NTB
270
+ BK
271
+ BNS
272
+ BKU
273
+ BCS
274
+ MCI
275
+ MPV
276
+ BNED
277
+ BKS
278
+ B
279
+ ABX
280
+ BAS
281
+ BAX
282
+ BTE
283
+ BBT
284
+ BFR
285
+ BBX
286
+ BCE
287
+ BZH
288
+ BDX
289
+ BDXA
290
+ BDC
291
+ BXE
292
+ BEL
293
+ BMS
294
+ BHE
295
+ BHLB
296
+ BERY
297
+ BBY
298
+ BSTI
299
+ BGCA
300
+ BHP
301
+ BBL
302
+ BIG
303
+ BH
304
+ BHVN
305
+ BIO
306
+ BITA
307
+ BKH
308
+ BKHU
309
+ BKI
310
+ BSM
311
+ BB
312
+ BGIO
313
+ BJZ
314
+ BFZ
315
+ CII
316
+ BHK
317
+ HYT
318
+ BTZ
319
+ DSU
320
+ BGR
321
+ BDJ
322
+ EGF
323
+ FRA
324
+ BFO
325
+ BGT
326
+ BOE
327
+ BME
328
+ BAF
329
+ BKT
330
+ BGY
331
+ BKN
332
+ BTA
333
+ BIT
334
+ MUI
335
+ MNE
336
+ MUA
337
+ BPK
338
+ BKK
339
+ BBK
340
+ BBF
341
+ BYM
342
+ BFK
343
+ BTT
344
+ MEN
345
+ MUC
346
+ MUH
347
+ MHD
348
+ MFL
349
+ MUJ
350
+ MHN
351
+ MUE
352
+ MUS
353
+ MVT
354
+ MYC
355
+ MCA
356
+ MYD
357
+ MYF
358
+ MFT
359
+ MIY
360
+ MYJ
361
+ MYN
362
+ MPA
363
+ MQT
364
+ MYI
365
+ MQY
366
+ BNJ
367
+ BNY
368
+ BLH
369
+ BQH
370
+ BSE
371
+ BCX
372
+ BST
373
+ BSD
374
+ BUI
375
+ BLK
376
+ BGB
377
+ BGX
378
+ BSL
379
+ APRN
380
+ BCRH
381
+ BXG
382
+ BXC
383
+ BWP
384
+ BA
385
+ BCC
386
+ BCEI
387
+ BOOT
388
+ BAH
389
+ BWA
390
+ SAM
391
+ BXP
392
+ BSX
393
+ BOX
394
+ BYD
395
+ BPMP
396
+ BP
397
+ BPT
398
+ BRC
399
+ BHR
400
+ BDN
401
+ BWG
402
+ LND
403
+ BAK
404
+ BRFS
405
+ BPI
406
+ BGG
407
+ BFAM
408
+ BEDU
409
+ BSA
410
+ BSIG
411
+ EAT
412
+ BCO
413
+ BMY
414
+ BRS
415
+ BTI
416
+ BRX
417
+ BR
418
+ BKD
419
+ BAM
420
+ BBU
421
+ INF
422
+ BIP
423
+ RA
424
+ BEP
425
+ BRO
426
+ BRT
427
+ BC
428
+ BT
429
+ BPL
430
+ BKE
431
+ BVN
432
+ BBW
433
+ BG
434
+ BURL
435
+ BWXT
436
+ BY
437
+ CJ
438
+ GYB
439
+ PFH
440
+ CABO
441
+ CBT
442
+ COG
443
+ CACI
444
+ WHD
445
+ CADE
446
+ CAE
447
+ CAI
448
+ CAL
449
+ CRC
450
+ CWT
451
+ CALX
452
+ ELY
453
+ CPE
454
+ CBM
455
+ CPT
456
+ CCJ
457
+ CPB
458
+ CWH
459
+ GOOS
460
+ CM
461
+ CNI
462
+ CNQ
463
+ CP
464
+ CNNE
465
+ CAJ
466
+ CMD
467
+ COF
468
+ CSU
469
+ BXMT
470
+ CIC
471
+ CMO )
472
+
473
+ symbols.each_with_index{ |sy,i| IB::Symbols::W500.add_contract i, IB::Stock.new( symbol: sy) }
474
+
475
+ puts IB::Symbols::W500.size