ib-ruby 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/.gitignore +3 -0
  2. data/HISTORY +3 -0
  3. data/README.md +29 -9
  4. data/VERSION +1 -1
  5. data/app/assets/javascripts/ib/bars.js +2 -0
  6. data/app/assets/javascripts/ib/executions.js +2 -0
  7. data/app/assets/javascripts/ib/order_states.js +2 -0
  8. data/app/assets/stylesheets/ib/bars.css +4 -0
  9. data/app/assets/stylesheets/ib/executions.css +4 -0
  10. data/app/assets/stylesheets/ib/order_states.css +4 -0
  11. data/app/controllers/ib/bars_controller.rb +87 -0
  12. data/app/controllers/ib/combo_legs_controller.rb +87 -0
  13. data/app/controllers/ib/contract_details_controller.rb +87 -0
  14. data/app/controllers/ib/contracts_controller.rb +87 -0
  15. data/app/controllers/ib/executions_controller.rb +87 -0
  16. data/app/controllers/ib/order_states_controller.rb +87 -0
  17. data/app/controllers/ib/orders_controller.rb +87 -0
  18. data/app/helpers/ib/bars_helper.rb +4 -0
  19. data/app/helpers/ib/combo_legs_helper.rb +4 -0
  20. data/app/helpers/ib/contract_details_helper.rb +4 -0
  21. data/app/helpers/ib/contracts_helper.rb +4 -0
  22. data/app/helpers/ib/executions_helper.rb +4 -0
  23. data/app/helpers/ib/order_states_helper.rb +4 -0
  24. data/app/helpers/ib/orders_helper.rb +4 -0
  25. data/app/views/ib/bars/_form.html.erb +57 -0
  26. data/app/views/ib/bars/edit.html.erb +6 -0
  27. data/app/views/ib/bars/index.html.erb +41 -0
  28. data/app/views/ib/bars/new.html.erb +5 -0
  29. data/app/views/ib/bars/show.html.erb +55 -0
  30. data/app/views/ib/combo_legs/_form.html.erb +53 -0
  31. data/app/views/ib/combo_legs/edit.html.erb +6 -0
  32. data/app/views/ib/combo_legs/index.html.erb +39 -0
  33. data/app/views/ib/combo_legs/new.html.erb +5 -0
  34. data/app/views/ib/combo_legs/show.html.erb +55 -0
  35. data/app/views/ib/contract_details/_form.html.erb +141 -0
  36. data/app/views/ib/contract_details/edit.html.erb +6 -0
  37. data/app/views/ib/contract_details/index.html.erb +83 -0
  38. data/app/views/ib/contract_details/new.html.erb +5 -0
  39. data/app/views/ib/contract_details/show.html.erb +160 -0
  40. data/app/views/ib/contracts/_form.html.erb +77 -0
  41. data/app/views/ib/contracts/edit.html.erb +6 -0
  42. data/app/views/ib/contracts/index.html.erb +53 -0
  43. data/app/views/ib/contracts/new.html.erb +5 -0
  44. data/app/views/ib/contracts/show.html.erb +85 -0
  45. data/app/views/ib/executions/_form.html.erb +77 -0
  46. data/app/views/ib/executions/edit.html.erb +6 -0
  47. data/app/views/ib/executions/index.html.erb +51 -0
  48. data/app/views/ib/executions/new.html.erb +5 -0
  49. data/app/views/ib/executions/show.html.erb +80 -0
  50. data/app/views/ib/order_states/_form.html.erb +93 -0
  51. data/app/views/ib/order_states/edit.html.erb +6 -0
  52. data/app/views/ib/order_states/index.html.erb +59 -0
  53. data/app/views/ib/order_states/new.html.erb +5 -0
  54. data/app/views/ib/order_states/show.html.erb +100 -0
  55. data/app/views/ib/orders/_form.html.erb +353 -0
  56. data/app/views/ib/orders/edit.html.erb +6 -0
  57. data/app/views/ib/orders/index.html.erb +193 -0
  58. data/app/views/ib/orders/new.html.erb +5 -0
  59. data/app/views/ib/orders/show.html.erb +435 -0
  60. data/bin/scaffold.rb +29 -0
  61. data/config/routes.rb +8 -1
  62. data/db/config.yml +5 -0
  63. data/db/migrate/101_add_ib_executions.rb +3 -0
  64. data/db/migrate/111_add_ib_bars.rb +6 -0
  65. data/db/migrate/121_add_ib_order_states.rb +8 -0
  66. data/db/migrate/131_add_ib_orders.rb +24 -0
  67. data/db/migrate/141_add_ib_combo_legs.rb +6 -0
  68. data/db/migrate/151_add_ib_underlyings.rb +3 -0
  69. data/db/migrate/161_add_ib_contract_details.rb +11 -0
  70. data/db/migrate/171_add_ib_contracts.rb +8 -1
  71. data/db/schema.rb +6 -5
  72. data/lib/ib/base.rb +3 -1
  73. data/lib/ib/base_properties.rb +26 -15
  74. data/lib/ib/connection.rb +3 -2
  75. data/lib/ib/errors.rb +16 -9
  76. data/lib/ib/extensions.rb +6 -4
  77. data/lib/ib/symbols.rb +10 -3
  78. data/lib/ib/symbols/forex.rb +49 -64
  79. data/lib/ib/symbols/futures.rb +2 -3
  80. data/lib/ib/symbols/options.rb +2 -1
  81. data/lib/ib/symbols/stocks.rb +2 -1
  82. data/lib/models/ib/bar.rb +6 -2
  83. data/lib/models/ib/combo_leg.rb +4 -3
  84. data/lib/models/ib/contract.rb +9 -5
  85. data/lib/models/ib/contract_detail.rb +1 -1
  86. data/lib/models/ib/execution.rb +3 -1
  87. data/lib/models/ib/order.rb +22 -20
  88. data/lib/models/ib/order_state.rb +4 -2
  89. data/lib/models/ib/underlying.rb +3 -4
  90. data/spec/README.md +12 -10
  91. data/spec/ib/symbols/symbols_spec.rb +10 -0
  92. data/spec/integration_helper.rb +1 -1
  93. data/spec/model_helper.rb +9 -8
  94. data/spec/models/ib/combo_leg_spec.rb +29 -28
  95. data/spec/models/ib/contract_detail_spec.rb +4 -1
  96. data/spec/models/ib/contract_spec.rb +66 -58
  97. data/spec/models/ib/execution_spec.rb +50 -44
  98. data/spec/models/ib/order_spec.rb +2 -2
  99. data/spec/order_helper.rb +5 -4
  100. data/spec/spec_helper.rb +3 -3
  101. metadata +59 -35
  102. data/app/models/ib/underlying.rb +0 -5
  103. data/bin/account_info +0 -29
  104. data/bin/cancel_orders +0 -27
  105. data/bin/contract_details +0 -35
  106. data/bin/depth_of_market +0 -43
  107. data/bin/fa_accounts +0 -22
  108. data/bin/fundamental_data +0 -39
  109. data/bin/historic_data +0 -51
  110. data/bin/historic_data_cli +0 -182
  111. data/bin/list_orders +0 -28
  112. data/bin/market_data +0 -41
  113. data/bin/option_data +0 -44
  114. data/bin/place_combo_order +0 -58
  115. data/bin/place_order +0 -27
  116. data/bin/template +0 -18
  117. data/bin/tick_data +0 -26
  118. data/bin/time_and_sales +0 -53
@@ -1,41 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # This script connects to IB API and subscribes to market data for specific symbols
4
-
5
- require 'rubygems'
6
- require 'bundler/setup'
7
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
8
- require 'ib-ruby'
9
-
10
- # Definition of what we want market 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
- @market = {123 => IB::Symbols::Forex[:gbpusd],
14
- 456 => IB::Symbols::Forex[:eurusd],
15
- 789 => IB::Symbols::Forex[:usdcad]}
16
-
17
- # First, connect to IB TWS.
18
- ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS
19
-
20
- ## Subscribe to TWS alerts/errors
21
- ib.subscribe(:Alert) { |msg| puts msg.to_human }
22
-
23
- # Subscribe to TickerPrice and TickerSize events. The code passed in the block will
24
- # be executed when a message of that type is received, with the received message as its
25
- # argument. In this case, we just print out the tick. NB: The description field is not
26
- # from IB TWS. It is defined locally in forex.rb, and is just arbitrary text.
27
- ib.subscribe(:TickPrice, :TickSize) do |msg|
28
- puts @market[msg.ticker_id].description + ": " + msg.to_human
29
- end
30
-
31
- # Now we actually request market data for the symbols we're interested in.
32
- @market.each_pair do |id, contract|
33
- ib.send_message :RequestMarketData, :ticker_id => id, :contract => contract
34
- end
35
-
36
- puts "\nSubscribed to market data"
37
- puts "\n******** Press <Enter> to cancel... *********\n\n"
38
- STDIN.gets
39
- puts "Cancelling market data subscription.."
40
-
41
- @market.each_pair { |id, _| ib.send_message :CancelMarketData, :id => id }
@@ -1,44 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # This script subscribes to market data for a list of Options
4
-
5
- require 'rubygems'
6
- require 'bundler/setup'
7
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
8
- require 'ib-ruby'
9
-
10
- # Definition of what we want market 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
- @market = {13 => IB::Symbols::Options[:wfc20],
14
- 15 => IB::Symbols::Options[:z50],
15
- 17 => IB::Symbols::Options[:spy75],
16
- 19 => IB::Symbols::Options[:spy100]}
17
-
18
- # First, connect to IB TWS. Arbitrary :client_id is used to identify your script
19
- ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS
20
-
21
- ## Subscribe to TWS alerts/errors
22
- ib.subscribe(:Alert) { |msg| puts msg.to_human }
23
-
24
- # Subscribe to Ticker... events. The code passed in the block will be executed when
25
- # any message of that type is received, with the received message as its argument.
26
- # In this case, we just print out the tick.
27
- #
28
- # (N.B. The description field is not from IB TWS. It is defined
29
- # locally in forex.rb, and is just arbitrary text.)
30
- ib.subscribe(:TickPrice, :TickSize, :TickOption, :TickString) do |msg|
31
- puts @market[msg.ticker_id].description + ": " + msg.to_human
32
- end
33
-
34
- # Now we actually request market data for the symbols we're interested in.
35
- @market.each_pair do |id, contract|
36
- ib.send_message :RequestMarketData, :ticker_id => id, :contract => contract
37
- end
38
-
39
- puts "\nSubscribed to market data"
40
- puts "\n******** Press <Enter> to cancel... *********\n\n"
41
- STDIN.gets
42
- puts "Cancelling market data subscription.."
43
-
44
- @market.each_pair { |id, contract| ib.send_message :CancelMarketData, :id => id }
@@ -1,58 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # This script places GOOG option butterfly combo order
4
-
5
- require 'rubygems'
6
- require 'bundler/setup'
7
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
8
- require 'ib-ruby'
9
-
10
- # Utility method that helps us build multi-legged (BAG) Orders
11
- def butterfly symbol, expiry, right, *strikes
12
- raise 'No Connection!' unless @ib && @ib.connected?
13
-
14
- legs = strikes.zip([1, -2, 1]).map do |strike, weight|
15
- # Create contract
16
- contract = IB::Option.new :symbol => symbol,
17
- :expiry => expiry,
18
- :right => right,
19
- :strike => strike
20
- # Find out contract's con_id
21
- @ib.clear_received :ContractData, :ContractDataEnd
22
- @ib.send_message :RequestContractData, :id => strike, :contract => contract
23
- @ib.wait_for :ContractDataEnd, 3
24
- con_id = @ib.received[:ContractData].last.contract.con_id
25
-
26
- # Create Comboleg from con_id and weight
27
- IB::ComboLeg.new :con_id => con_id, :weight => weight
28
- end
29
-
30
- # Create new Combo contract
31
- IB::Bag.new :symbol => symbol,
32
- :currency => "USD", # Only US options in combo Contracts
33
- :exchange => "SMART",
34
- :legs => legs
35
- end
36
-
37
- # First, connect to IB TWS. Arbitrary :client_id is used to identify your script
38
- @ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS
39
-
40
- # Subscribe to TWS alerts/errors and order-related messages
41
- @ib.subscribe(:Alert, :OpenOrder, :OrderStatus) { |msg| puts msg.to_human }
42
- @ib.wait_for :NextValidId
43
-
44
- # Create multi-legged option Combo using utility method above
45
- combo = butterfly 'GOOG', '201301', 'CALL', 500, 510, 520
46
-
47
- # Create Order stub
48
- order = IB::Order.new :total_quantity => 10, # 10 butterflies
49
- :limit_price => 0.01, # at 0.01 x 100 USD per contract
50
- :action => 'BUY',
51
- :order_type => 'LMT'
52
-
53
- @ib.place_order order, combo
54
-
55
- @ib.wait_for [:OpenOrder, 3], [:OrderStatus, 2]
56
-
57
- puts "\n******** Press <Enter> to cancel... *********\n\n"
58
- gets
@@ -1,27 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # This script places WFC buy order for 100 lots
4
-
5
- require 'rubygems'
6
- require 'bundler/setup'
7
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
8
- require 'ib-ruby'
9
-
10
- # First, connect to IB TWS. Arbitrary :client_id is used to identify your script
11
- ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS
12
-
13
- # Subscribe to TWS alerts/errors and order-related messages
14
- ib.subscribe(:Alert, :OpenOrder, :OrderStatus) { |msg| puts msg.to_human }
15
-
16
- wfc = IB::Symbols::Stocks[:wfc]
17
- buy_order = IB::Order.new :total_quantity => 100,
18
- :limit_price => 1 + rand().round(2),
19
- :action => 'BUY',
20
- :order_type => 'LMT'
21
- ib.wait_for :NextValidId
22
- ib.place_order buy_order, wfc
23
-
24
- ib.send_message :RequestAllOpenOrders
25
-
26
- puts "\n******** Press <Enter> to cancel... *********\n\n"
27
- STDIN.gets
@@ -1,18 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # Your script description here...
4
-
5
- require 'rubygems'
6
- require 'bundler/setup'
7
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
8
- require 'ib-ruby'
9
-
10
- # First, connect to IB TWS. Arbitrary :client_id is used to identify your script
11
- ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS
12
-
13
- # Subscribe to TWS alerts/errors
14
- ib.subscribe(:Alert) { |msg| puts msg.to_human }
15
-
16
- # Put your code here
17
- # ...
18
-
@@ -1,26 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # This script reproduces https://github.com/ib-ruby/ib-ruby/issues/12
4
-
5
- require 'rubygems'
6
- require 'bundler/setup'
7
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
8
- require 'ib-ruby'
9
-
10
- contract = IB::Contract.new :symbol=> 'AAPL',
11
- :exchange=> "Smart",
12
- :currency=> "USD",
13
- :sec_type=> :stock,
14
- :description=> "Some stock"
15
-
16
- # First, connect to IB TWS. Arbitrary :client_id is used to identify your script
17
- ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS
18
-
19
- ib.subscribe(:Alert) { |msg| puts msg.to_human }
20
- ib.subscribe(:TickGeneric, :TickString, :TickPrice, :TickSize) { |msg| puts msg.inspect }
21
- ib.send_message :RequestMarketData, :id => 123, :contract => contract
22
-
23
- puts "\nSubscribed to market data"
24
- puts "\n******** Press <Enter> to cancel... *********\n\n"
25
- gets
26
- puts "Cancelling market data subscription.."
@@ -1,53 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # This script connects to IB API and subscribes to market data for Forex symbols.
4
- # It then prints out all trades that exceed certain size.
5
-
6
- require 'rubygems'
7
- require 'bundler/setup'
8
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
9
- require 'ib-ruby'
10
-
11
- # Define the symbols we're interested in.
12
- @market = {
13
- 123 => IB::Symbols::Futures[:gbp],
14
- 234 => IB::Symbols::Futures[:jpy]
15
- }
16
-
17
- # To determine when the timeout has passed.
18
- @last_msg_time = Time.now.to_i + 2
19
-
20
- # First, connect to IB TWS. Arbitrary :client_id is used to identify your script
21
- ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS
22
-
23
- # Subscribe to TWS alerts/errors
24
- ib.subscribe(:Alert) { |msg| puts msg.to_human }
25
-
26
- # This method filters out non-:last type events, and filters out any sale < MIN_SIZE.
27
- # Note that we have to look the ticker id of each incoming message
28
- # up in local memory to figure out what it's for.
29
- # (N.B. The description field is not from IB TWS. It is defined
30
- # locally in forex.rb, and is just arbitrary text.)
31
- def show_sales_and_size(msg)
32
- #return if msg.type != :last_price || msg.data[:size] < MIN_SIZE
33
- puts @market[msg.data[:id]].description + ": " +
34
- (msg.is_a?(IB::Messages::Incoming::TickPrice) ?
35
- "#{msg.data[:size]} at #{msg.data[:price]}" : msg.to_human)
36
- end
37
-
38
- # Now, subscribe to TickerPrice and TickerSize events. The code passed in the block
39
- # will be executed when a message of that type is received, with the received message
40
- # as its argument. In this case, we just print out the tick.
41
- ib.subscribe(:TickPrice, :TickSize, :TickString) { |msg| show_sales_and_size(msg) }
42
-
43
- # Now we actually request market data for the symbols we're interested in.
44
- @market.each_pair do |id, contract|
45
- ib.send_message :RequestMarketData, :id => id, :contract => contract
46
- end
47
-
48
- puts "\nSubscribed to TWS market data"
49
- puts "\n******** Press <Enter> to cancel... *********\n\n"
50
- STDIN.gets
51
- puts "Unsubscribing from TWS market data.."
52
-
53
- @market.each_pair { |id, contract| ib.send_message :CancelMarketData, :id => id }