Module: AccountInfos
- Defined in:
- lib/ib/account-infos.rb
Overview
module
Instance Method Summary collapse
- #all_contracts ⇒ Object
-
#get_account_data(*accounts, watchlists: []) ⇒ 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
70 71 72 |
# File 'lib/ib/account-infos.rb', line 70 def all_contracts clients.map(&:contracts).flat_map(&:itself).uniq(&:con_id) end |
#get_account_data(*accounts, watchlists: []) ⇒ 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.
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 recieved-
31 32 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 |
# File 'lib/ib/account-infos.rb', line 31 def get_account_data *accounts, watchlists: [] logger.progname = 'Gateway#get_account_data' @account_data_subscription ||= subscribe_account_updates accounts = clients if accounts.empty? logger.warn{ "No active account present. AccountData are NOT requested" } if accounts.empty? # Account-infos have to be requested sequencially. # subsequent (parallel) calls kill the former once 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 logger.debug{ "#{account.account} :: Requesting AccountData " } account.update_attribute :connected, false # indicates: AccountUpdate in Progress # reset account and portfolio-values account.portfolio_values = [] account.account_values = [] :RequestAccountData, subscribe: true, account_code: account.account Timeout::timeout(3, IB::TransmissionError, "RequestAccountData failed (#{account.account})") do # initialize requests sequencially loop{ sleep 0.1; break if account.connected } end if watchlists.present? watchlists.each{|w| error "Watchlists must be IB::Symbols--Classes :.#{w.inspect}" unless w.is_a? IB::Symbols } account.organize_portfolio_positions watchlists end :RequestAccountData, subscribe: false ## do this only once else logger.info{ "#{account.account} :: Using stored AccountData " } end end end |