ib-api 972.2 → 972.3

Sign up to get free protection for your applications and to get access to all the features.
data/example/list_orders DELETED
@@ -1,45 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # This script retrieves list of all Orders from TWS
4
-
5
- require 'bundler/setup'
6
- require 'ib-api'
7
-
8
- # Connect to IB as 0 (TWS) to retrieve all Orders, including TWS-generated ones
9
- ib = IB::Connection.new :client_id => 0 do |gw| # , :port => 7497 # TWS
10
-
11
- # Subscribe to TWS alerts/errors and order-related messages
12
- counter = 0
13
-
14
- gw.subscribe(:Alert, :ManagedAccounts, :OrderStatus, :OpenOrderEnd) { |msg| puts msg.to_human }
15
-
16
- gw.subscribe(:OpenOrder) do |msg|
17
- counter += 1
18
- puts "#{counter}: #{msg.to_human}"
19
-
20
- # Get rid of logging verbosity
21
- gw.logger.level = Logger::FATAL
22
- end
23
- end
24
-
25
- ## fire request
26
- ib.send_message :RequestAllOpenOrders
27
-
28
- # Wait for IB to respond to our request
29
- ib.wait_for :OpenOrderEnd
30
- sleep 1 # Let printer do the job
31
-
32
- =begin
33
- ---> expected output
34
- 1: <OpenOrder: <Bag: GOOG SMART USD legs: 269330045|1,269562569|1,301964119|-2 > <Order: LMT GTC buy 10.0 0.06 PreSubmitted #9/561364885 from 1112/DU167349 fee 0.0>>
35
- <OrderStatus: <OrderState: PreSubmitted #9/561364885 from 1112 filled 0.0/10.0 at 0.0/0.0 why_held locate>>
36
- 2:<OpenOrder: <Stock: WFC USD> <Order: LMT DAY sell 100.0 34.05 PreSubmitted #19/561364895 from 1112/DU167349 fee 0.0>>
37
- <OrderStatus: <OrderState: PreSubmitted #19/561364895 from 1112 filled 0.0/100.0 at 0.0/0.0 why_held child,locate>>
38
- 3: <OpenOrder: <Stock: WFC USD> <Order: STP DAY sell 100.0 0.0 PreSubmitted /33.55#18/561364894 from 1112/DU167349 fee 0.0>>
39
- <OrderStatus: <OrderState: PreSubmitted #18/561364894 from 1112 filled 0.0/100.0 at 0.0/0.0 why_held child,locate,trigger>>
40
- 4: <OpenOrder: <Bag: GOOG SMART USD legs: 269330045|1,269562569|1,301964119|-2 > <Order: LMT GTC buy 10.0 0.06 PreSubmitted #8/1562725198 from 1112/DU167349 fee 0.0>>
41
- <OrderStatus: <OrderState: PreSubmitted #8/1562725198 from 1112 filled 0.0/10.0 at 0.0/0.0 why_held locate>>
42
- 5: <OpenOrder: <Stock: WFC USD> <Order: LMT GTC buy 100.0 1.72 Submitted #3/1562725193 from 1112/DU167349 fee 0.0>>
43
- <OrderStatus: <OrderState: Submitted #3/1562725193 from 1112 filled 0.0/100.0 at 0.0/0.0 why_held >>
44
- <OpenOrderEnd: >
45
- =end
@@ -1,81 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # This script exports your IB portfolio in a CSV format. Usage:
4
- # $ example/portfolio_csv [account] > portfolio.csv
5
-
6
- require 'bundler/setup'
7
- require 'ib-ruby'
8
-
9
- # Only for Advisors: you need to provide account id such as U666777
10
- account = ARGV[0] || ''
11
- accounts = []
12
-
13
- # Connect to IB TWS and subscribe to events
14
- ib = IB::Connection.new( :client_id => 1112 , port: 4002 ) do |gw| # , :port => 7496 # TWS
15
-
16
- # Redirect TWS alerts/errors to STDERR to keep output file clean
17
- #gw.subscribe(:Alert) { |msg| STDERR.puts msg.to_human }
18
-
19
- # Subscribe to TWS alerts/errors and account-related messages
20
- # that TWS sends in response to account data request
21
- gw.subscribe(:Alert) do |msg|
22
- ## if an account is not given. but required, (Error 321 indicates this)
23
- ## fetch data from the last account detected. (The first is most probably the Advisor-Account)
24
- if msg.code == 321
25
- account = accounts.last
26
- gw.send_message :RequestAccountData, :subscribe => true, :account_code => account
27
- else
28
- puts msg.to_human
29
- end
30
- end
31
- # Silently ignore account-related messages other than :PortfolioValue
32
- gw.subscribe(:AccountUpdateTime, :AccountValue, :ManagedAccounts, :AccountDownloadEnd) {}
33
- # Get rid of logging verbosity
34
- gw.logger.level = Logger::FATAL
35
-
36
- ## Just in case: put account-names into accounts-array
37
- gw.subscribe(:ManagedAccounts){ |msg| accounts = msg.accounts_list.split ',' }
38
- end
39
-
40
- # Output CSV headers
41
- puts %w[Symbol
42
- SecType
43
- Expiry
44
- Strike
45
- Right
46
- Currency
47
- LocalSymbol
48
- Position
49
- MarketPrice
50
- MarketValue
51
- AvgCost
52
- UnrealizedPNL
53
- RealizedPNL
54
- Account].map {|val| "\"#{val}\""}.join(",")
55
-
56
- # Output each portfolio position as a single line in CSV format
57
- ib.subscribe(:PortfolioValue) do |msg|
58
- contract = msg.contract
59
- csv = [ contract.symbol,
60
- IB::CODES[:sec_type][contract.sec_type],
61
- contract.expiry,
62
- contract.strike == 0 ? "" : contract.strike,
63
- contract.right == :none ? "" : contract.right,
64
- contract.currency,
65
- contract.local_symbol,
66
- msg.position,
67
- msg.market_price,
68
- msg.market_value,
69
- msg.average_cost,
70
- msg.unrealized_pnl,
71
- msg.realized_pnl,
72
- msg.account_name
73
- ].map {|val| "\"#{val}\""}.join(",")
74
- puts csv
75
- end
76
-
77
- # Request account data, wait for its end, unsubscribe
78
- ib.send_message :RequestAccountData, :subscribe => true, :account_code => account
79
- ib.wait_for :AccountDownloadEnd, 30
80
- ib.send_message :RequestAccountData, :subscribe => false, :account_code => account
81
- sleep 0.5
data/example/scanner_data DELETED
@@ -1,62 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # This Script needs testing. remove this line after sucessfully using the scanner facility
3
- #
4
- # This script setups a scan request and retreives the results.
5
-
6
- require 'rubygems'
7
- require 'bundler/setup'
8
- require 'ib-api'
9
-
10
-
11
-
12
- # Connect to IB TWS.
13
- ib = IB::Connection.new :client_id => 1112 , port: 7496 do | gw | #, :port => 7497 # TWS
14
-
15
-
16
- # Subscribe to TWS alerts/errors
17
- gw.subscribe(:Alert) { |msg| puts msg.to_human }
18
-
19
-
20
- # Subscribe to ScannerData incoming events. The code passed in the block
21
- # will be executed when a message of that type is received, with the received
22
- # message as its argument. In this case, we just print out the data.
23
- #
24
- gw.subscribe(IB::Messages::Incoming::ScannerData) do |msg|
25
- puts "ID: #{msg.request_id} : #{msg.count} items:"
26
- msg.results.each { |entry| puts " #{entry}" }
27
- end
28
- end
29
- id = 42
30
- # Now we actually request scanner data for the type of scan we are interested.
31
- # Some scan types can be found here: http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/tables/available_market_scanners.htm
32
- mess = IB::Messages::Outgoing::RequestScannerSubscription.new(
33
- :request_id => id,
34
- :number_of_rows => 20,
35
- :instrument => "STK",
36
- :location_code => 'STK.US.MAJOR',
37
- :scan_code => 'TOP_PERC_GAIN',
38
- :above_price => 4.0,
39
- :below_price => Float::MAX,
40
- :above_volume => 5000,
41
- :market_cap_above => 100000000,
42
- :market_cap_below => Float::MAX,
43
- :moody_rating_above => "",
44
- :moody_rating_below => "",
45
- :sp_rating_above => "",
46
- :sp_rating_below => "",
47
- :maturity_date_above => "",
48
- :maturity_date_below => "",
49
- :coupon_rate_above => Float::MAX,
50
- :coupon_rate_below => Float::MAX,
51
- :exclude_convertible => "",
52
- :average_option_volume_above => "", # ?
53
- :scanner_setting_pairs => "Annual,true",
54
- :stock_type_filter => "Stock"
55
- )
56
-
57
- ib.send_message( mess )
58
-
59
- # IB does not send any indication when all data is done being delivered.
60
- # So we need to interrupt manually when our request is answered.
61
- puts "\n******** Press <Enter> to exit... *********\n\n"
62
- STDIN.gets
data/example/template DELETED
@@ -1,19 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # Your script description here...
4
-
5
- require 'bundler/setup'
6
- require 'ib-api'
7
-
8
- # First, connect to IB TWS and subscribe for events.
9
- ib = IB::Connection.new :client_id => 1112 do | gw | #, :port => 7497 # TWS
10
-
11
- # Subscribe to TWS alerts/errors
12
- gw.subscribe(:Alert, :ManagedAccounts) { |msg| puts msg.to_human }
13
- # Set log level
14
- gw.logger.level = Logger::FATAL # DEBUG -- INFO -- WARN -- ERROR -- FATAL
15
-
16
- end
17
- # Put your code here
18
- # ...
19
-
data/example/tick_data DELETED
@@ -1,28 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # This script reproduces https://github.com/ib-ruby/ib-ruby/issues/12
4
-
5
- require 'bundler/setup'
6
- require 'ib-api'
7
-
8
- contract = IB::Stock.new :symbol=> 'AAPL'
9
-
10
- # First, connect to IB Gateway.
11
- ib = IB::Connection.new :client_id => 1112 # id to identify your script
12
- # :port => 7497 # TWS connection (instead of default Gateway)
13
- # :received => false # Do not keep all received messages in memory
14
-
15
- ib.subscribe(:Alert) { |msg| puts msg.to_human }
16
- ib.subscribe(:MarketDataType) { |msg| puts msg.to_human }
17
- ib.subscribe(:TickGeneric, :TickString, :TickPrice, :TickSize) { |msg| puts msg.inspect }
18
- ib.send_message :RequestMarketDataType, :market_data_type => :delayed
19
- ib.send_message :RequestMarketData, id: 123, contract: contract
20
-
21
- puts "\nSubscribed to market data"
22
- puts "\n******** Press <Enter> to cancel... *********\n\n"
23
- gets
24
- puts "Cancelling market data subscription.."
25
-
26
- ib.send_message :CancelMarketData, id: 123
27
- sleep 1
28
- puts "Done."