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.
- data/.gitignore +3 -0
- data/HISTORY +3 -0
- data/README.md +29 -9
- data/VERSION +1 -1
- data/app/assets/javascripts/ib/bars.js +2 -0
- data/app/assets/javascripts/ib/executions.js +2 -0
- data/app/assets/javascripts/ib/order_states.js +2 -0
- data/app/assets/stylesheets/ib/bars.css +4 -0
- data/app/assets/stylesheets/ib/executions.css +4 -0
- data/app/assets/stylesheets/ib/order_states.css +4 -0
- data/app/controllers/ib/bars_controller.rb +87 -0
- data/app/controllers/ib/combo_legs_controller.rb +87 -0
- data/app/controllers/ib/contract_details_controller.rb +87 -0
- data/app/controllers/ib/contracts_controller.rb +87 -0
- data/app/controllers/ib/executions_controller.rb +87 -0
- data/app/controllers/ib/order_states_controller.rb +87 -0
- data/app/controllers/ib/orders_controller.rb +87 -0
- data/app/helpers/ib/bars_helper.rb +4 -0
- data/app/helpers/ib/combo_legs_helper.rb +4 -0
- data/app/helpers/ib/contract_details_helper.rb +4 -0
- data/app/helpers/ib/contracts_helper.rb +4 -0
- data/app/helpers/ib/executions_helper.rb +4 -0
- data/app/helpers/ib/order_states_helper.rb +4 -0
- data/app/helpers/ib/orders_helper.rb +4 -0
- data/app/views/ib/bars/_form.html.erb +57 -0
- data/app/views/ib/bars/edit.html.erb +6 -0
- data/app/views/ib/bars/index.html.erb +41 -0
- data/app/views/ib/bars/new.html.erb +5 -0
- data/app/views/ib/bars/show.html.erb +55 -0
- data/app/views/ib/combo_legs/_form.html.erb +53 -0
- data/app/views/ib/combo_legs/edit.html.erb +6 -0
- data/app/views/ib/combo_legs/index.html.erb +39 -0
- data/app/views/ib/combo_legs/new.html.erb +5 -0
- data/app/views/ib/combo_legs/show.html.erb +55 -0
- data/app/views/ib/contract_details/_form.html.erb +141 -0
- data/app/views/ib/contract_details/edit.html.erb +6 -0
- data/app/views/ib/contract_details/index.html.erb +83 -0
- data/app/views/ib/contract_details/new.html.erb +5 -0
- data/app/views/ib/contract_details/show.html.erb +160 -0
- data/app/views/ib/contracts/_form.html.erb +77 -0
- data/app/views/ib/contracts/edit.html.erb +6 -0
- data/app/views/ib/contracts/index.html.erb +53 -0
- data/app/views/ib/contracts/new.html.erb +5 -0
- data/app/views/ib/contracts/show.html.erb +85 -0
- data/app/views/ib/executions/_form.html.erb +77 -0
- data/app/views/ib/executions/edit.html.erb +6 -0
- data/app/views/ib/executions/index.html.erb +51 -0
- data/app/views/ib/executions/new.html.erb +5 -0
- data/app/views/ib/executions/show.html.erb +80 -0
- data/app/views/ib/order_states/_form.html.erb +93 -0
- data/app/views/ib/order_states/edit.html.erb +6 -0
- data/app/views/ib/order_states/index.html.erb +59 -0
- data/app/views/ib/order_states/new.html.erb +5 -0
- data/app/views/ib/order_states/show.html.erb +100 -0
- data/app/views/ib/orders/_form.html.erb +353 -0
- data/app/views/ib/orders/edit.html.erb +6 -0
- data/app/views/ib/orders/index.html.erb +193 -0
- data/app/views/ib/orders/new.html.erb +5 -0
- data/app/views/ib/orders/show.html.erb +435 -0
- data/bin/scaffold.rb +29 -0
- data/config/routes.rb +8 -1
- data/db/config.yml +5 -0
- data/db/migrate/101_add_ib_executions.rb +3 -0
- data/db/migrate/111_add_ib_bars.rb +6 -0
- data/db/migrate/121_add_ib_order_states.rb +8 -0
- data/db/migrate/131_add_ib_orders.rb +24 -0
- data/db/migrate/141_add_ib_combo_legs.rb +6 -0
- data/db/migrate/151_add_ib_underlyings.rb +3 -0
- data/db/migrate/161_add_ib_contract_details.rb +11 -0
- data/db/migrate/171_add_ib_contracts.rb +8 -1
- data/db/schema.rb +6 -5
- data/lib/ib/base.rb +3 -1
- data/lib/ib/base_properties.rb +26 -15
- data/lib/ib/connection.rb +3 -2
- data/lib/ib/errors.rb +16 -9
- data/lib/ib/extensions.rb +6 -4
- data/lib/ib/symbols.rb +10 -3
- data/lib/ib/symbols/forex.rb +49 -64
- data/lib/ib/symbols/futures.rb +2 -3
- data/lib/ib/symbols/options.rb +2 -1
- data/lib/ib/symbols/stocks.rb +2 -1
- data/lib/models/ib/bar.rb +6 -2
- data/lib/models/ib/combo_leg.rb +4 -3
- data/lib/models/ib/contract.rb +9 -5
- data/lib/models/ib/contract_detail.rb +1 -1
- data/lib/models/ib/execution.rb +3 -1
- data/lib/models/ib/order.rb +22 -20
- data/lib/models/ib/order_state.rb +4 -2
- data/lib/models/ib/underlying.rb +3 -4
- data/spec/README.md +12 -10
- data/spec/ib/symbols/symbols_spec.rb +10 -0
- data/spec/integration_helper.rb +1 -1
- data/spec/model_helper.rb +9 -8
- data/spec/models/ib/combo_leg_spec.rb +29 -28
- data/spec/models/ib/contract_detail_spec.rb +4 -1
- data/spec/models/ib/contract_spec.rb +66 -58
- data/spec/models/ib/execution_spec.rb +50 -44
- data/spec/models/ib/order_spec.rb +2 -2
- data/spec/order_helper.rb +5 -4
- data/spec/spec_helper.rb +3 -3
- metadata +59 -35
- data/app/models/ib/underlying.rb +0 -5
- data/bin/account_info +0 -29
- data/bin/cancel_orders +0 -27
- data/bin/contract_details +0 -35
- data/bin/depth_of_market +0 -43
- data/bin/fa_accounts +0 -22
- data/bin/fundamental_data +0 -39
- data/bin/historic_data +0 -51
- data/bin/historic_data_cli +0 -182
- data/bin/list_orders +0 -28
- data/bin/market_data +0 -41
- data/bin/option_data +0 -44
- data/bin/place_combo_order +0 -58
- data/bin/place_order +0 -27
- data/bin/template +0 -18
- data/bin/tick_data +0 -26
- data/bin/time_and_sales +0 -53
data/bin/market_data
DELETED
@@ -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 }
|
data/bin/option_data
DELETED
@@ -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 }
|
data/bin/place_combo_order
DELETED
@@ -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
|
data/bin/place_order
DELETED
@@ -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
|
data/bin/template
DELETED
@@ -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
|
-
|
data/bin/tick_data
DELETED
@@ -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.."
|
data/bin/time_and_sales
DELETED
@@ -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 }
|