Module: AccountInfos

Defined in:
lib/ib/account-infos.rb

Overview

module

Instance Method Summary collapse

Instance Method Details

#all_contractsObject



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   *accounts,  watchlists: []

  logger.progname = 'Gateway#get_account_data'

  @account_data_subscription ||=   

  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 |
     =  ac.is_a?( IB:: ) ?  ac  : clients.find{|x| x. == ac } 
    error( "No Account detected " )  unless .is_a? IB::
    # don't repeat the query until 170 sec. have passed since the previous update
    if .last_updated.nil?  || ( Time.now - .last_updated ) > 170 # sec   
      logger.debug{ "#{account.account} :: Requesting AccountData " }
      .update_attribute :connected, false  # indicates: AccountUpdate in Progress
      # reset account and portfolio-values
      .portfolio_values =  []
      . =  []
      send_message :RequestAccountData, subscribe: true, account_code: .
      Timeout::timeout(3, IB::TransmissionError, "RequestAccountData failed (#{account.account})") do
#          initialize requests sequencially         
        loop{ sleep 0.1; break if .connected  }
      end
      if watchlists.present?
        watchlists.each{|w| error "Watchlists must be IB::Symbols--Classes :.#{w.inspect}" unless w.is_a? IB::Symbols }
        .organize_portfolio_positions watchlists  
      end
      send_message :RequestAccountData, subscribe: false  ## do this only once
    else
      logger.info{ "#{account.account} :: Using stored AccountData " }
    end
  end
end