ib-ruby 0.4.3 → 0.4.20

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 (50) hide show
  1. data/.gitignore +32 -0
  2. data/HISTORY +68 -0
  3. data/README.rdoc +9 -6
  4. data/VERSION +1 -1
  5. data/bin/account_info +29 -0
  6. data/bin/contract_details +37 -0
  7. data/bin/depth_of_market +43 -0
  8. data/bin/historic_data +62 -0
  9. data/bin/{RequestHistoricData → historic_data_cli} +46 -91
  10. data/bin/market_data +49 -0
  11. data/bin/option_data +45 -0
  12. data/bin/template +21 -0
  13. data/bin/time_and_sales +63 -0
  14. data/lib/ib-ruby/connection.rb +166 -0
  15. data/lib/ib-ruby/constants.rb +91 -0
  16. data/lib/ib-ruby/messages/incoming.rb +807 -0
  17. data/lib/ib-ruby/messages/outgoing.rb +573 -0
  18. data/lib/ib-ruby/messages.rb +8 -1445
  19. data/lib/ib-ruby/models/bar.rb +26 -0
  20. data/lib/ib-ruby/models/contract.rb +335 -0
  21. data/lib/ib-ruby/models/execution.rb +55 -0
  22. data/lib/ib-ruby/models/model.rb +20 -0
  23. data/lib/ib-ruby/models/order.rb +262 -0
  24. data/lib/ib-ruby/models.rb +11 -0
  25. data/lib/ib-ruby/socket.rb +50 -0
  26. data/lib/ib-ruby/symbols/forex.rb +32 -72
  27. data/lib/ib-ruby/symbols/futures.rb +47 -68
  28. data/lib/ib-ruby/symbols/options.rb +30 -0
  29. data/lib/ib-ruby/symbols/stocks.rb +23 -0
  30. data/lib/ib-ruby/symbols.rb +9 -0
  31. data/lib/ib-ruby.rb +7 -8
  32. data/lib/legacy/bin/account_info_old +36 -0
  33. data/lib/legacy/bin/historic_data_old +81 -0
  34. data/lib/legacy/bin/market_data_old +68 -0
  35. data/lib/legacy/datatypes.rb +485 -0
  36. data/lib/legacy/ib-ruby.rb +10 -0
  37. data/lib/legacy/ib.rb +226 -0
  38. data/lib/legacy/messages.rb +1458 -0
  39. data/lib/version.rb +2 -3
  40. data/spec/ib-ruby/models/contract_spec.rb +261 -0
  41. data/spec/ib-ruby/models/order_spec.rb +64 -0
  42. data/spec/ib-ruby_spec.rb +0 -131
  43. metadata +106 -76
  44. data/bin/AccountInfo +0 -67
  45. data/bin/HistoricToCSV +0 -111
  46. data/bin/RequestMarketData +0 -78
  47. data/bin/SimpleTimeAndSales +0 -98
  48. data/bin/ib-ruby +0 -8
  49. data/lib/ib-ruby/datatypes.rb +0 -400
  50. data/lib/ib-ruby/ib.rb +0 -242
data/bin/AccountInfo DELETED
@@ -1,67 +0,0 @@
1
- #!/usr/bin/env ruby -w
2
- #
3
- # Copyright (C) 2009 Wes Devauld
4
- #
5
- # This library is free software; you can redistribute it and/or modify
6
- # it under the terms of the GNU Lesser General Public License as
7
- # published by the Free Software Foundation; either version 2.1 of the
8
- # License, or (at your option) any later version.
9
- #
10
- # This library is distributed in the hope that it will be useful, but
11
- # WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- # Lesser General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU Lesser General Public
16
- # License along with this library; if not, write to the Free Software
17
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
- # 02110-1301 USA
19
-
20
- require File.expand_path(
21
- File.join(File.dirname(__FILE__), '..', 'lib', 'ib-ruby'))
22
-
23
- # First, connect to IB TWS.
24
- ib = IB::IB.new
25
-
26
- # Uncomment this for verbose debug messages:
27
- # IB::IBLogger.level = Logger::Severity::DEBUG
28
-
29
- ## Subscribe to the messages that TWS sends in response to a request
30
- ## for account data.
31
-
32
- ib.subscribe(IB::IncomingMessages::AccountValue, lambda {|msg|
33
- puts msg.to_human
34
- })
35
-
36
- ib.subscribe(IB::IncomingMessages::PortfolioValue, lambda {|msg|
37
- puts msg.to_human
38
- })
39
-
40
- ib.subscribe(IB::IncomingMessages::AccountUpdateTime, lambda {|msg|
41
- puts msg.to_human
42
- })
43
-
44
-
45
-
46
- msg = IB::OutgoingMessages::RequestAccountData.new({
47
- :subscribe => true,
48
- :account_code => ''
49
- })
50
- ib.dispatch(msg)
51
-
52
-
53
- puts "\n\n\t******** Press <Enter> to quit.. *********\n\n"
54
-
55
- gets
56
-
57
- puts "Cancelling account data subscription.."
58
-
59
- msg = IB::OutgoingMessages::RequestAccountData.new({
60
- :subscribe => false,
61
- :account_code => ''
62
- })
63
- ib.dispatch(msg)
64
-
65
-
66
- puts "Done."
67
-
data/bin/HistoricToCSV DELETED
@@ -1,111 +0,0 @@
1
- #!/usr/bin/env ruby -w
2
- #
3
- # Copyright (C) 2009 Wes Devauld
4
- #
5
- # This library is free software; you can redistribute it and/or modify
6
- # it under the terms of the GNU Lesser General Public License as
7
- # published by the Free Software Foundation; either version 2.1 of the
8
- # License, or (at your option) any later version.
9
- #
10
- # This library is distributed in the hope that it will be useful, but
11
- # WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- # Lesser General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU Lesser General Public
16
- # License along with this library; if not, write to the Free Software
17
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
- # 02110-1301 USA
19
- #
20
- # TODO Fix the Historical command line client
21
- #
22
-
23
- require File.expand_path(
24
- File.join(File.dirname(__FILE__), '..', 'lib', 'ib-ruby'))
25
-
26
- ### Configurable Options
27
-
28
- # if Quiet == false, status data will be printed to STDERR
29
- Quiet = false
30
-
31
- # How long to wait when no messages are received from TWS before
32
- # exiting, in seconds
33
- Timeout = 10
34
-
35
- SymbolToRequest = IB::Symbols::Forex[:gbpusd]
36
-
37
- ### end options
38
-
39
-
40
- #
41
- # Definition of what we want market data for. We have to keep track
42
- # of what ticker id corresponds to what symbol ourselves, because the
43
- # ticks don't include any other identifying information.
44
- #
45
- # The choice of ticker ids is, as far as I can tell, arbitrary.
46
- #
47
- # Note that as of 4/07 there is no historical data available for forex spot.
48
- #
49
- @market =
50
- {
51
- 123 => SymbolToRequest
52
- }
53
-
54
- # To determine when the timeout has passed.
55
- @last_msg_time = Time.now.to_i + 2
56
-
57
- # Connect to IB TWS.
58
- ib = IB::IB.new
59
-
60
- # Uncomment this for verbose debug messages:
61
- # IB::IBLogger.level = Logger::Severity::DEBUG
62
-
63
- #
64
- # Now, subscribe to HistoricalData incoming events. The code
65
- # passed in the block will be executed when a message of that type is
66
- # received, with the received message as its argument. In this case,
67
- # we just print out the data.
68
- #
69
- # Note that we have to look the ticker id of each incoming message
70
- # up in local memory to figure out what it's for.
71
- #
72
- # (N.B. The description field is not from IB TWS. It is defined
73
- # locally in forex.rb, and is just arbitrary text.)
74
-
75
-
76
- ib.subscribe(IB::IncomingMessages::HistoricalData, lambda {|msg|
77
-
78
- STDERR.puts @market[msg.data[:req_id]].description + ": " + msg.data[:item_count].to_s("F") + " items:" unless Quiet
79
-
80
- msg.data[:history].each { |datum|
81
-
82
- @last_msg_time = Time.now.to_i
83
-
84
- STDERR.puts " " + datum.to_s("F") unless Quiet
85
- STDOUT.puts "#{datum.date},#{datum.open.to_s("F")},#{datum.high.to_s("F")},#{datum.low.to_s("F")},#{datum.close.to_s("F")},#{datum.volume}"
86
- }
87
- })
88
-
89
- # Now we actually request historical data for the symbols we're
90
- # interested in. TWS will respond with a HistoricalData message,
91
- # which will be received by the code above.
92
-
93
- @market.each_pair {|id, contract|
94
- msg = IB::OutgoingMessages::RequestHistoricalData.new({
95
- :ticker_id => id,
96
- :contract => contract,
97
- :end_date_time => Time.now.to_ib,
98
- :duration => (360).to_s, # how long before end_date_time to request in seconds - this means 1 day
99
- :bar_size => IB::OutgoingMessages::RequestHistoricalData::BarSizes.index(:hour),
100
- :what_to_show => :trades,
101
- :use_RTH => 0,
102
- :format_date => 2
103
- })
104
- ib.dispatch(msg)
105
- }
106
-
107
-
108
- while true
109
- exit(0) if Time.now.to_i > @last_msg_time + Timeout
110
- sleep 1
111
- end
@@ -1,78 +0,0 @@
1
- #!/usr/bin/env ruby -w
2
- #
3
- # Copyright (C) 2009 Wes Devauld
4
- #
5
- # This library is free software; you can redistribute it and/or modify
6
- # it under the terms of the GNU Lesser General Public License as
7
- # published by the Free Software Foundation; either version 2.1 of the
8
- # License, or (at your option) any later version.
9
- #
10
- # This library is distributed in the hope that it will be useful, but
11
- # WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- # Lesser General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU Lesser General Public
16
- # License along with this library; if not, write to the Free Software
17
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
- # 02110-1301 USA
19
- #
20
-
21
- require File.expand_path(
22
- File.join(File.dirname(__FILE__), '..', 'lib', 'ib-ruby'))
23
-
24
- #
25
- # Definition of what we want market data for. We have to keep track
26
- # of what ticker id corresponds to what symbol ourselves, because the
27
- # ticks don't include any other identifying information.
28
- #
29
- # The choice of ticker ids is, as far as I can tell, arbitrary.
30
- #
31
- @market =
32
- {
33
- 123 => IB::Symbols::Forex[:gbpusd],
34
- 456 => IB::Symbols::Forex[:eurusd],
35
- 789 => IB::Symbols::Forex[:usdcad]
36
- }
37
-
38
-
39
- # First, connect to IB TWS.
40
- ib = IB::IB.new
41
-
42
-
43
- #
44
- # Now, subscribe to TickerPrice and TickerSize events. The code
45
- # passed in the block will be executed when a message of that type is
46
- # received, with the received message as its argument. In this case,
47
- # we just print out the tick.
48
- #
49
- # Note that we have to look the ticker id of each incoming message
50
- # up in local memory to figure out what it's for.
51
- #
52
- # (N.B. The description field is not from IB TWS. It is defined
53
- # locally in forex.rb, and is just arbitrary text.)
54
-
55
- ib.subscribe(IB::IncomingMessages::TickPrice, lambda {|msg|
56
- puts @market[msg.data[:ticker_id]].description + ": " + msg.to_human
57
- })
58
-
59
- ib.subscribe(IB::IncomingMessages::TickSize, lambda {|msg|
60
- puts @market[msg.data[:ticker_id]].description + ": " + msg.to_human
61
- })
62
-
63
-
64
- # Now we actually request market data for the symbols we're interested in.
65
-
66
- @market.each_pair {|id, contract|
67
- msg = IB::OutgoingMessages::RequestMarketData.new({
68
- :ticker_id => id,
69
- :contract => contract
70
- })
71
- ib.dispatch(msg)
72
- }
73
-
74
-
75
- puts "Main thread going to sleep. Press ^C to quit.."
76
- while true
77
- sleep 2
78
- end
@@ -1,98 +0,0 @@
1
- #!/usr/bin/env ruby -w
2
- #
3
- # Copyright (C) 2009 Wes Devauld
4
- #
5
- # This library is free software; you can redistribute it and/or modify
6
- # it under the terms of the GNU Lesser General Public License as
7
- # published by the Free Software Foundation; either version 2.1 of the
8
- # License, or (at your option) any later version.
9
- #
10
- # This library is distributed in the hope that it will be useful, but
11
- # WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- # Lesser General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU Lesser General Public
16
- # License along with this library; if not, write to the Free Software
17
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
- # 02110-1301 USA
19
- #
20
-
21
- require File.expand_path(
22
- File.join(File.dirname(__FILE__), '..', 'lib', 'ib-ruby'))
23
-
24
- # First, connect to IB TWS.
25
- ib = IB::IB.new
26
-
27
- # Uncomment this for verbose debug messages:
28
- # IB::IBLogger.level = Logger::Severity::DEBUG
29
-
30
- # Define the symbols we're interested in.
31
- @market =
32
- {
33
- 123 => IB::Symbols::Futures[:gbp],
34
- 234 => IB::Symbols::Futures[:jpy]
35
- }
36
-
37
-
38
- # This method filters out non-:last type events, and filters out any
39
- # sale < MIN_SIZE.
40
- MIN_SIZE = 0
41
-
42
- def showSales(msg)
43
- return if msg.data[:type] != :last || msg.data[:size] < MIN_SIZE
44
- puts @market[msg.data[:ticker_id]].description + ": " + msg.data[:size].to_s("F") + " at " + msg.data[:price].to_s("F")
45
- end
46
-
47
- def showSize(msg)
48
- puts @market[msg.data[:ticker_id]].description + ": " + msg.to_human
49
- end
50
-
51
-
52
- #
53
- # Now, subscribe to TickerPrice and TickerSize events. The code
54
- # passed in the block will be executed when a message of that type is
55
- # received, with the received message as its argument. In this case,
56
- # we just print out the tick.
57
- #
58
- # Note that we have to look the ticker id of each incoming message
59
- # up in local memory to figure out what it's for.
60
- #
61
- # (N.B. The description field is not from IB TWS. It is defined
62
- # locally in forex.rb, and is just arbitrary text.)
63
-
64
- ib.subscribe(IB::IncomingMessages::TickPrice, lambda {|msg|
65
- showSales(msg)
66
- })
67
-
68
- ib.subscribe(IB::IncomingMessages::TickSize, lambda {|msg|
69
- showSize(msg)
70
- })
71
-
72
-
73
- # Now we actually request market data for the symbols we're interested in.
74
-
75
- @market.each_pair {|id, contract|
76
- msg = IB::OutgoingMessages::RequestMarketData.new({
77
- :ticker_id => id,
78
- :contract => contract
79
- })
80
- ib.dispatch(msg)
81
- }
82
-
83
-
84
- puts "\n\n\t******** Press <Enter> to quit.. *********\n\n"
85
-
86
- gets
87
-
88
- puts "Unsubscribing from TWS market data.."
89
-
90
- @market.each_pair {|id, contract|
91
- msg = IB::OutgoingMessages::CancelMarketData.new({
92
- :ticker_id => id,
93
- })
94
- ib.dispatch(msg)
95
- }
96
-
97
- puts "Done."
98
-
data/bin/ib-ruby DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require File.expand_path(
4
- File.join(File.dirname(__FILE__), %w[.. lib ib-ruby]))
5
-
6
- # Put your code here
7
-
8
- # EOF