ib-api 972.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.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +105 -0
- data/Guardfile +24 -0
- data/LICENSE +674 -0
- data/README.md +65 -0
- data/Rakefile +11 -0
- data/VERSION +1 -0
- data/api.gemspec +43 -0
- data/bin/console +95 -0
- data/bin/console.yml +3 -0
- data/bin/setup +8 -0
- data/changelog.md +7 -0
- data/example/README.md +76 -0
- data/example/account_info +54 -0
- data/example/account_positions +30 -0
- data/example/account_summary +88 -0
- data/example/cancel_orders +74 -0
- data/example/fa_accounts +25 -0
- data/example/fundamental_data +40 -0
- data/example/historic_data_cli +186 -0
- data/example/list_orders +45 -0
- data/example/portfolio_csv +81 -0
- data/example/scanner_data +62 -0
- data/example/template +19 -0
- data/example/tick_data +28 -0
- data/lib/extensions/class-extensions.rb +87 -0
- data/lib/ib-api.rb +7 -0
- data/lib/ib/base.rb +103 -0
- data/lib/ib/base_properties.rb +160 -0
- data/lib/ib/connection.rb +450 -0
- data/lib/ib/constants.rb +393 -0
- data/lib/ib/errors.rb +44 -0
- data/lib/ib/logger.rb +26 -0
- data/lib/ib/messages.rb +99 -0
- data/lib/ib/messages/abstract_message.rb +101 -0
- data/lib/ib/messages/incoming.rb +251 -0
- data/lib/ib/messages/incoming/abstract_message.rb +116 -0
- data/lib/ib/messages/incoming/account_value.rb +78 -0
- data/lib/ib/messages/incoming/alert.rb +34 -0
- data/lib/ib/messages/incoming/contract_data.rb +102 -0
- data/lib/ib/messages/incoming/delta_neutral_validation.rb +23 -0
- data/lib/ib/messages/incoming/execution_data.rb +50 -0
- data/lib/ib/messages/incoming/historical_data.rb +84 -0
- data/lib/ib/messages/incoming/market_depths.rb +44 -0
- data/lib/ib/messages/incoming/next_valid_id.rb +18 -0
- data/lib/ib/messages/incoming/open_order.rb +277 -0
- data/lib/ib/messages/incoming/order_status.rb +85 -0
- data/lib/ib/messages/incoming/portfolio_value.rb +78 -0
- data/lib/ib/messages/incoming/real_time_bar.rb +32 -0
- data/lib/ib/messages/incoming/scanner_data.rb +54 -0
- data/lib/ib/messages/incoming/ticks.rb +268 -0
- data/lib/ib/messages/outgoing.rb +437 -0
- data/lib/ib/messages/outgoing/abstract_message.rb +88 -0
- data/lib/ib/messages/outgoing/account_requests.rb +112 -0
- data/lib/ib/messages/outgoing/bar_requests.rb +250 -0
- data/lib/ib/messages/outgoing/place_order.rb +209 -0
- data/lib/ib/messages/outgoing/request_marketdata.rb +99 -0
- data/lib/ib/messages/outgoing/request_tick_data.rb +21 -0
- data/lib/ib/model.rb +4 -0
- data/lib/ib/models.rb +14 -0
- data/lib/ib/server_versions.rb +114 -0
- data/lib/ib/socket.rb +185 -0
- data/lib/ib/support.rb +160 -0
- data/lib/ib/version.rb +6 -0
- data/lib/models/ib/account.rb +85 -0
- data/lib/models/ib/account_value.rb +33 -0
- data/lib/models/ib/bag.rb +55 -0
- data/lib/models/ib/bar.rb +31 -0
- data/lib/models/ib/combo_leg.rb +105 -0
- data/lib/models/ib/condition.rb +245 -0
- data/lib/models/ib/contract.rb +415 -0
- data/lib/models/ib/contract_detail.rb +108 -0
- data/lib/models/ib/execution.rb +67 -0
- data/lib/models/ib/forex.rb +13 -0
- data/lib/models/ib/future.rb +15 -0
- data/lib/models/ib/index.rb +15 -0
- data/lib/models/ib/option.rb +78 -0
- data/lib/models/ib/option_detail.rb +55 -0
- data/lib/models/ib/order.rb +519 -0
- data/lib/models/ib/order_state.rb +152 -0
- data/lib/models/ib/portfolio_value.rb +64 -0
- data/lib/models/ib/stock.rb +16 -0
- data/lib/models/ib/underlying.rb +34 -0
- data/lib/models/ib/vertical.rb +96 -0
- data/lib/requires.rb +12 -0
- metadata +203 -0
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# ib-api
|
2
|
+
Ruby interface to Interactive Brokers' TWS API
|
3
|
+
|
4
|
+
Reimplementation of the basic functions of ib-ruby
|
5
|
+
|
6
|
+
|
7
|
+
----
|
8
|
+
`ib-ruby` offers a modular access to the TWS-API-Interface of Interactive Brokers.
|
9
|
+
|
10
|
+
`ib-api` provides a simple interface to low-level TWS API-calls.
|
11
|
+
|
12
|
+
----
|
13
|
+
|
14
|
+
In its plain vanilla usage, it just exchanges messages with the TWS. The user is responsible for any further data processing.
|
15
|
+
|
16
|
+
|
17
|
+
Even then, it needs just a few lines of code to place an order
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
# connect with default parameters
|
21
|
+
ib = IB::Connection.new
|
22
|
+
|
23
|
+
# define a contract to deal with
|
24
|
+
the_stock = IB::Stock.new symbol: 'TAP'
|
25
|
+
|
26
|
+
# order 100 shares for 35 $
|
27
|
+
limit_order = IB::Order.new limit_price: 35, order_type: 'LMT', total_quantity: 100, action: :buy
|
28
|
+
ib.send_message :PlaceOrder,
|
29
|
+
:order => limit_order,
|
30
|
+
:contract => the_stock,
|
31
|
+
:local_id => ib.next_local_id
|
32
|
+
|
33
|
+
# wait until the orderstate message returned
|
34
|
+
ib.wait_for :OrderStatus
|
35
|
+
|
36
|
+
# print the Orderstatus
|
37
|
+
puts ib.recieved[:OrderStatus].to_human
|
38
|
+
|
39
|
+
# => ["<OrderState: Submitted #17/1528367295 from 2000 filled 0.0/100.0 at 0.0/0.0 why_held >"]
|
40
|
+
|
41
|
+
```
|
42
|
+
## Minimal TWS-Version
|
43
|
+
|
44
|
+
`ib-api` is tested via the _stable ib-Gateway_ (Version 9.72)
|
45
|
+
|
46
|
+
## Tests
|
47
|
+
|
48
|
+
are invoked by
|
49
|
+
|
50
|
+
```
|
51
|
+
bundle exec guard
|
52
|
+
# or
|
53
|
+
bundle exec rake spec
|
54
|
+
```
|
55
|
+
Integration tests on order-placements are not included. To run the test suite its thus safe to use a _real Account_.
|
56
|
+
You have to edit `spec/spec.yml` and replace the `:account`-Setting with your own `AccountID`, even if you connect to a single account.
|
57
|
+
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ib-api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
62
|
+
|
63
|
+
## Code of Conduct
|
64
|
+
|
65
|
+
Everyone interacting in the Core project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ib-api/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
972.0
|
data/api.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "ib/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ib-api"
|
8
|
+
spec.version = IB::VERSION
|
9
|
+
spec.authors = ["Hartmut Bischoff"]
|
10
|
+
spec.email = ["topofocus@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby Implementation of the Interactive Brokers TWS API}
|
13
|
+
spec.description = %q{Ruby Implementation of the Interactive Brokers TWS API}
|
14
|
+
spec.homepage = "https://github.com/ib-ruby"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/ib-ruby/ib-api"
|
23
|
+
# spec.metadata["changelog_uri"] = "https://github.com/ib-ruby/ib-api/CHANGELOG.md"
|
24
|
+
else
|
25
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
"public gem pushes."
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = "exe"
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ["lib"]
|
37
|
+
|
38
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
39
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
40
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
41
|
+
spec.add_dependency 'activesupport', '>= 6.0'
|
42
|
+
spec.add_dependency 'activemodel'
|
43
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,95 @@
|
|
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
|
+
###
|
8
|
+
### Define Parameter in file console.yml
|
9
|
+
###
|
10
|
+
require 'bundler/setup'
|
11
|
+
require 'yaml'
|
12
|
+
|
13
|
+
require 'logger'
|
14
|
+
|
15
|
+
require 'ib-api'
|
16
|
+
|
17
|
+
class Array
|
18
|
+
# enables calling members of an array. which are hashes by it name
|
19
|
+
# i.e
|
20
|
+
#
|
21
|
+
# 2.5.0 :006 > C.received[:OpenOrder].local_id
|
22
|
+
# => [16, 17, 21, 20, 19, 8, 7]
|
23
|
+
# 2.5.0 :007 > C.received[:OpenOrder].contract.to_human
|
24
|
+
# => ["<Bag: IECombo SMART USD legs: >", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: GE USD>", "<Stock: WFC USD>", "<Stock: WFC USD>"]
|
25
|
+
#
|
26
|
+
# its included only in the console, for inspection purposes
|
27
|
+
|
28
|
+
def method_missing(method, *key)
|
29
|
+
unless method == :to_hash || method == :to_str #|| method == :to_int
|
30
|
+
return self.map{|x| x.public_send(method, *key)}
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end # Array
|
35
|
+
|
36
|
+
|
37
|
+
# read items from console.yml
|
38
|
+
read_yml = -> (key) do
|
39
|
+
YAML::load_file( File.expand_path('../console.yml',__FILE__))[key]
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
puts
|
44
|
+
puts ">> IB-Core Interactive Console <<"
|
45
|
+
puts '-'* 45
|
46
|
+
puts
|
47
|
+
puts "Namespace is IB ! "
|
48
|
+
puts
|
49
|
+
puts '-'* 45
|
50
|
+
include LogDev
|
51
|
+
include IB
|
52
|
+
require 'irb'
|
53
|
+
client_id = ARGV[1] || read_yml[:client_id]
|
54
|
+
specified_port = ARGV[0] || 'Gateway'
|
55
|
+
port = case specified_port
|
56
|
+
when Integer
|
57
|
+
specified_port # just use the number
|
58
|
+
when /^[gG]/
|
59
|
+
read_yml[:gateway]
|
60
|
+
when /^[Tt]/
|
61
|
+
read_yml[:tws]
|
62
|
+
end
|
63
|
+
|
64
|
+
ARGV.clear
|
65
|
+
logger = default_logger # Logger.new STDOUT
|
66
|
+
|
67
|
+
## The Block takes instructions which are executed after initializing all instance-variables
|
68
|
+
## and prior to the connection-process
|
69
|
+
## Here we just subscribe to some events
|
70
|
+
C = Connection.new client_id: client_id, port: port do |c| # future use__ , optional_capacities: "+PACEAPI" do |c|
|
71
|
+
|
72
|
+
c.subscribe( :ContractData, :BondContractData) { |msg| logger.info { msg.contract.to_human } }
|
73
|
+
c.subscribe( :Alert, :ContractDataEnd, :ManagedAccounts, :OrderStatus ) {| m| logger.info { m.to_human } }
|
74
|
+
c.subscribe( :PortfolioValue, :AccountValue, :OrderStatus, :OpenOrderEnd, :ExecutionData ) {| m| logger.info { m.to_human }}
|
75
|
+
# c.subscribe :ManagedAccounts do |msg|
|
76
|
+
# puts "------------------------------- Managed Accounts ----------------------------------"
|
77
|
+
# puts "Detected Accounts: #{msg.accounts.account.join(' -- ')} "
|
78
|
+
# puts
|
79
|
+
# end
|
80
|
+
|
81
|
+
c.subscribe( :OpenOrder){ |msg| "Open Order detected and stored: C.received[:OpenOrders] " }
|
82
|
+
c.logger.level = Logger::INFO
|
83
|
+
end
|
84
|
+
unless C.received[:OpenOrder].blank?
|
85
|
+
puts "------------------------------- OpenOrders ----------------------------------"
|
86
|
+
puts C.received[:OpenOrder].to_human.join "\n"
|
87
|
+
end
|
88
|
+
puts "Connection established on Port #{port}, client_id #{client_id} used"
|
89
|
+
puts
|
90
|
+
puts "----> C points to the connection-instance"
|
91
|
+
puts
|
92
|
+
puts "some basic Messages are subscribed and accordingly displayed"
|
93
|
+
puts '-'* 45
|
94
|
+
|
95
|
+
IRB.start(__FILE__)
|
data/bin/console.yml
ADDED
data/bin/setup
ADDED
data/changelog.md
ADDED
data/example/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
## IB-RUBY EXAMPLES:
|
2
|
+
|
3
|
+
This folder contains sample scripts that demonstrate common ib-ruby use cases.
|
4
|
+
The scripts show you how to access account info, print real time quotes, retrieve
|
5
|
+
historic or fundamental data, request options calculations, place, list, and cancel orders.
|
6
|
+
You may also want to look into `spec/integration` directory for more scenarios,
|
7
|
+
use cases and examples of handling IB messages.
|
8
|
+
|
9
|
+
Normally you run these examples like this:
|
10
|
+
|
11
|
+
$ ruby example/list_orders
|
12
|
+
|
13
|
+
The examples are executable.
|
14
|
+
If you're on Unix-like platform, they can be called directly ( » ./{script} «)
|
15
|
+
|
16
|
+
The Examples assume a running Gateway-Application on Localhost. Please use a Demo-Account or run
|
17
|
+
the API in »read-only-mode«
|
18
|
+
|
19
|
+
## EXAMPLE DESCRIPTION:
|
20
|
+
|
21
|
+
*account_info* - Request your account info, current positions, portfolio values and so on
|
22
|
+
|
23
|
+
$ ruby example/account_info # or cd example; ./account_info
|
24
|
+
|
25
|
+
For Financial Advisors, you need to add the managed account you want info for:
|
26
|
+
|
27
|
+
$ ruby example/account_info U123456
|
28
|
+
|
29
|
+
*cancel_orders* - Cancel either all open orders, or specific order(s), by their (local) ids. Examples:
|
30
|
+
|
31
|
+
$ ruby example/cancel_orders
|
32
|
+
$ ruby example/cancel_orders 492 495
|
33
|
+
|
34
|
+
*contract_details* - Obtain detailed descriptions for specific Stock, Option, Forex, Futures and Bond contracts.
|
35
|
+
|
36
|
+
*contract_sample details* - Define Contract samples provided by java and python API-implementations
|
37
|
+
|
38
|
+
*depth_of_market* - Receive a stream of MarketDepth change events for Stock, Forex and Future symbols.
|
39
|
+
|
40
|
+
*fa_accounts* - Get info about accounts under management (for Financial Advisors).
|
41
|
+
|
42
|
+
*flex_query* - Run a pre-defined Flex query, receive and print Flex report. Example:
|
43
|
+
|
44
|
+
$ ruby example/flex_query 12345 # Flex query id pre-defined in Account Management
|
45
|
+
|
46
|
+
*fundamental_data* - Request and print fundamental data report for a specific stock.
|
47
|
+
|
48
|
+
*historic_data* - Receive 5 days of 1-hour trade data for Stock, Forex and Future symbols.
|
49
|
+
|
50
|
+
*historic_data_cli* - CLI script for historic data downloading. It has many options, for detailed help run:
|
51
|
+
|
52
|
+
$ ruby example/historic_data_cli
|
53
|
+
|
54
|
+
*list_orders* - List all open API orders.
|
55
|
+
|
56
|
+
*market_data* - Receive a stream of trade events for multiple Forex symbols.
|
57
|
+
|
58
|
+
*option_data* - Receive a stream of underlying, price and greeks calculations for multiple Option symbols.
|
59
|
+
|
60
|
+
*place_braket_order* - Place a braket order for Stock.
|
61
|
+
|
62
|
+
*place_combo_order* - Place an Option combo order (Google butterfly).
|
63
|
+
|
64
|
+
*place_order* - Place a simple limit order to buy Stock.
|
65
|
+
|
66
|
+
*portfolio_csv* - Exports your IB portfolio in a CSV format. Usage:
|
67
|
+
|
68
|
+
$ ruby example/portfolio_csv [account] > my_portfolio.csv
|
69
|
+
|
70
|
+
*real_time_data* - Subscribe to real time data for specific symbol.
|
71
|
+
|
72
|
+
*template* - A blank example to start a new script (setting up all the dependencies).
|
73
|
+
|
74
|
+
*tick_data* - Subscribe to extended tick types for a single stock symbol.
|
75
|
+
|
76
|
+
*time_and_sales* - Print out Time&Sales format for Futures symbols.
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This script connects to IB API, subscribes to account info and prints out
|
4
|
+
# messages received from IB (update every 3 minute or so)
|
5
|
+
|
6
|
+
require 'bundler/setup'
|
7
|
+
require 'ib-api'
|
8
|
+
|
9
|
+
# Only for Advisor accounts: you can provide account_code such as U666777 (otherwise the last account is used)
|
10
|
+
account_code = ARGV[0] || ''
|
11
|
+
|
12
|
+
include IB
|
13
|
+
# Connect to IB TWS
|
14
|
+
accounts = []
|
15
|
+
ib = Connection.new client_id: 1113, port: 4002 do | gw | # :port => 7497 # TWS
|
16
|
+
|
17
|
+
# Subscribe to TWS alerts/errors and account-related messages
|
18
|
+
# that TWS sends in response to account data request
|
19
|
+
gw.subscribe(:Alert) do |msg|
|
20
|
+
## if an account is not given. but required, (Error 321 indicates this)
|
21
|
+
## fetch data from the last account detected. (The first is most probably the Advisor-Account)
|
22
|
+
if msg.code == 321
|
23
|
+
gw.send_message :RequestAccountData, :subscribe => true, :account_code => accounts.last
|
24
|
+
else
|
25
|
+
puts msg.to_human
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
## Print Account+Portfolio-Values
|
30
|
+
gw.subscribe(:AccountValue,
|
31
|
+
:PortfolioValue, :AccountUpdateTime) { |msg| puts msg.to_human }
|
32
|
+
|
33
|
+
## Just in case: put account-names into accounts-array
|
34
|
+
gw.subscribe(:ManagedAccounts){ |msg| accounts = msg.accounts_list.split ',' }
|
35
|
+
|
36
|
+
# Set log level
|
37
|
+
gw.logger.level = Logger::FATAL # DEBUG #FATAL
|
38
|
+
end
|
39
|
+
|
40
|
+
ib.send_message :RequestAccountData, :subscribe => true, :account_code => account_code
|
41
|
+
|
42
|
+
puts "\nSubscribing to IB account data"
|
43
|
+
|
44
|
+
## print message after recieving account-data
|
45
|
+
Thread.new do
|
46
|
+
sleep 3
|
47
|
+
puts "\n******** Press <Enter> to quit *********\n\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
STDIN.gets
|
51
|
+
puts "Cancelling account data subscription.."
|
52
|
+
|
53
|
+
ib.send_message :RequestAccountData, :subscribe => false
|
54
|
+
sleep 2
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This script connects to IB API and subscribes to Position Values which are updated regulary
|
4
|
+
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'ib-api'
|
7
|
+
|
8
|
+
|
9
|
+
# connect to IB TWS.
|
10
|
+
|
11
|
+
|
12
|
+
ib = IB::Connection.new( :client_id => 1110) do | gw | #, :port => 7496 # TWS)
|
13
|
+
## Subcribe to forseable events before the connection is completed
|
14
|
+
## Subscribe to TWS alerts/errors
|
15
|
+
gw.subscribe(:Alert, :PositionData){ |msg| puts msg.to_human }
|
16
|
+
gw.logger.level = Logger::FATAL # DEBUG -- INFO -- WARN -- ERROR -- FATAL
|
17
|
+
end
|
18
|
+
|
19
|
+
Thread.new do
|
20
|
+
sleep 1
|
21
|
+
puts "\n******** Press <Enter> to quit *********\n\n"
|
22
|
+
end
|
23
|
+
# request the AccountSummary
|
24
|
+
ib.send_message :RequestPositions
|
25
|
+
|
26
|
+
STDIN.gets
|
27
|
+
puts "\n *** canceling Reqest ..."
|
28
|
+
ib.send_message :CancelPositions
|
29
|
+
sleep 1
|
30
|
+
puts "done."
|
@@ -0,0 +1,88 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This script connects to IB API and subscribes to Account Summary Updates for certain tags
|
4
|
+
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'ib-api'
|
7
|
+
|
8
|
+
# Definition of what we want listed
|
9
|
+
# possible tags
|
10
|
+
# NetLiquidation,
|
11
|
+
# TotalCashValue - Total cash including futures pnl
|
12
|
+
# SettledCash - For cash accounts, this is the same as
|
13
|
+
# TotalCashValue
|
14
|
+
# AccruedCash - Net accrued interest
|
15
|
+
# BuyingPower - The maximum amount of marginable US stocks the
|
16
|
+
# account can buy
|
17
|
+
# EquityWithLoanValue - Cash + stocks + bonds + mutual funds
|
18
|
+
# PreviousDayEquityWithLoanValue,
|
19
|
+
# GrossPositionValue - The sum of the absolute value of all stock
|
20
|
+
# and equity option positions
|
21
|
+
# RegTEquity,
|
22
|
+
# RegTMargin,
|
23
|
+
# SMA - Special Memorandum Account
|
24
|
+
# InitMarginReq,
|
25
|
+
# MaintMarginReq,
|
26
|
+
# AvailableFunds,
|
27
|
+
# ExcessLiquidity,
|
28
|
+
# Cushion - Excess liquidity as a percentage of net liquidation value
|
29
|
+
# FullInitMarginReq,
|
30
|
+
# FullMaintMarginReq,
|
31
|
+
# FullAvailableFunds,
|
32
|
+
# FullExcessLiquidity,
|
33
|
+
# LookAheadNextChange - Time when look-ahead values take effect
|
34
|
+
# LookAheadInitMarginReq,
|
35
|
+
# LookAheadMaintMarginReq,
|
36
|
+
# LookAheadAvailableFunds,
|
37
|
+
# LookAheadExcessLiquidity,
|
38
|
+
# HighestSeverity - A measure of how close the account is to liquidation
|
39
|
+
# DayTradesRemaining - The Number of Open/Close trades a user
|
40
|
+
# could put on before Pattern Day Trading is detected. A value of "-1"
|
41
|
+
# means that the user can put on unlimited day trades.
|
42
|
+
# Leverage - GrossPositionValue / NetLiquidation
|
43
|
+
# $LEDGER - Single flag to relay all cash balance tags*, only in base
|
44
|
+
# currency.
|
45
|
+
# $LEDGER:CURRENCY - Single flag to relay all cash balance tags*, only in
|
46
|
+
# the specified currency.
|
47
|
+
# $LEDGER:ALL - Single flag to relay all cash balance tags* in all
|
48
|
+
#
|
49
|
+
tags = %w( NetLiquidation InitMarginReq DayTradesRemaining )
|
50
|
+
# connect to IB TWS.
|
51
|
+
|
52
|
+
|
53
|
+
ib = IB::Connection.new( :client_id => 1112) do | gw | #, :port => 7496 # TWS)
|
54
|
+
## Subcribe to forseable events before the connection is completed
|
55
|
+
## Subscribe to TWS alerts/errors
|
56
|
+
gw.subscribe(:Alert, :AccountSummary){ |msg| puts msg.to_human }
|
57
|
+
gw.logger.level = Logger::FATAL # DEBUG -- INFO -- WARN -- ERROR -- FATAL
|
58
|
+
end
|
59
|
+
|
60
|
+
Thread.new do
|
61
|
+
sleep 1
|
62
|
+
puts "\n******** Press <Enter> to quit *********\n\n"
|
63
|
+
end
|
64
|
+
# request the AccountSummary
|
65
|
+
request_id = ib.send_message :RequestAccountSummary, tags: tags.join(',')
|
66
|
+
|
67
|
+
STDIN.gets
|
68
|
+
puts "\n *** canceling Request ..."
|
69
|
+
ib.send_message :CancelAccountSummary, id: request_id
|
70
|
+
sleep 1
|
71
|
+
puts "done."
|
72
|
+
|
73
|
+
|
74
|
+
=begin
|
75
|
+
Expected Output
|
76
|
+
|
77
|
+
delta:~/workspace/ib-ruby/example$ ./account_summary
|
78
|
+
<AccountSummary: request_id 8111, account DU167349, tag DayTradesRemaining, value -1.0, currency >
|
79
|
+
<AccountSummary: request_id 8111, account DU167349, tag InitMarginReq, value 93402.16, currency EUR >
|
80
|
+
<AccountSummary: request_id 8111, account DU167349, tag NetLiquidation, value 990842.34, currency EUR >
|
81
|
+
<AccountSummary: request_id 8111, account DF167347, tag InitMarginReq, value 0.0, currency EUR >
|
82
|
+
<AccountSummary: request_id 8111, account DF167347, tag NetLiquidation, value 0.0, currency EUR >
|
83
|
+
<AccountSummary: request_id 8111, account DU167348, tag DayTradesRemaining, value -1.0, currency >
|
84
|
+
<AccountSummary: request_id 8111, account DU167348, tag InitMarginReq, value 135317.73, currency EUR >
|
85
|
+
<AccountSummary: request_id 8111, account DU167348, tag NetLiquidation, value 949599.26, currency EUR >
|
86
|
+
|
87
|
+
******** Press <Enter> to quit *********
|
88
|
+
=end
|