Module: AccountInfos
- Included in:
- IB::Gateway
- Defined in:
- lib/ib/gateway/account-infos.rb
Overview
module
Instance Method Summary collapse
- #all_contracts ⇒ Object
-
#get_account_data(*accounts, **compatibily_argument) ⇒ Object
Queries the tws for Account- and PortfolioValues The parameter can either be the account_id, the IB::Account-Object or an Array of account_id and IB::Account-Objects.
Instance Method Details
#all_contracts ⇒ Object
87 88 89 |
# File 'lib/ib/gateway/account-infos.rb', line 87 def all_contracts clients.map(&:contracts).flat_map(&:itself).uniq(&:con_id) end |
#get_account_data(*accounts, **compatibily_argument) ⇒ Object
Queries the tws for Account- and PortfolioValues The parameter can either be the account_id, the IB::Account-Object or an Array of account_id and IB::Account-Objects.
Resets Account#portfolio_values and -account_values
Raises an IB::TransmissionError if the account-data are not transmitted in time (1 sec)
Raises an IB::Error if less then 100 items are received.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ib/gateway/account-infos.rb', line 33 def get_account_data *accounts, **compatibily_argument subscription = subscribe_account_updates( continuously: false ) download_end = nil # declare variable accounts = clients if accounts.empty? IB::Connection.logger.warn{ "No active account present. AccountData are NOT requested" } if accounts.empty? # Account-infos have to be requested sequentially. # subsequent (parallel) calls kill the former on the tws-server-side # In addition, there is no need to cancel the subscription of an request, as a new # one overwrites the active one. accounts.each do | ac | account = ac.is_a?( IB::Account ) ? ac : clients.find{|x| x.account == ac } error( "No Account detected " ) unless account.is_a? IB::Account # don't repeat the query until 170 sec. have passed since the previous update if account.last_updated.nil? || ( Time.now - account.last_updated ) > 170 # sec IB::Connection.logger.debug{ "#{account.account} :: Erasing Account- and Portfolio Data " } IB::Connection.logger.debug{ "#{account.account} :: Requesting AccountData " } q = Queue.new download_end = tws.subscribe( :AccountDownloadEnd ) do | msg | q.push true if msg.account_name == account.account end # reset account and portfolio-values account.portfolio_values = [] account.account_values = [] # Data are gathered asynchron through the active subscription defined in `subscribe_account_updates` :RequestAccountData, subscribe: true, account_code: account.account th = Thread.new{ sleep 10 ; q.close } # close the queue after 10 seconds q.pop # wait for the data (or the closing event) if q.closed? error "No AccountData received", :reader else q.close tws.unsubscribe download_end end account.organize_portfolio_positions unless IB::Gateway.current.active_watchlists.empty? else IB::Connection.logger.info{ "#{account.account} :: Using stored AccountData " } end end tws. :RequestAccountData, subscribe: false ## do this only once tws.unsubscribe subscription rescue IB::TransmissionError => e tws.unsubscribe download_end unless download_end.nil? tws.unsubscribe subscription raise end |