Module: IB

Defined in:
lib/ib/orient-gateway.rb,
lib/version.rb,
lib/ib/account-init.rb,
lib/ib/account-infos.rb,
lib/alerts/base-alert.rb,
lib/ib/setup-orientdb.rb,
lib/models/ib/account.rb,
lib/models/ib/contract.rb,
lib/alerts/order-alerts.rb,
lib/models/ib/financials.rb,
lib/alerts/gateway-alerts.rb,
lib/models/ib/account_value.rb,
lib/models/ib/portfolio_value.rb

Overview

These Alerts are always active

Defined Under Namespace

Modules: AccountInfos, OrientDB, Setup Classes: Account, AccountValue, Alert, Contract, Financials, OrientGateway, PortfolioValue

Instance Method Summary collapse

Instance Method Details

#all_contractsObject



105
106
107
# File 'lib/ib/account-init.rb', line 105

def all_contracts
clients.map(&:contracts).flat_map(&:itself).uniq(&:con_id)
end

#get_account_dataObject

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 received-



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ib/account-init.rb', line 73

def  

  logger.progname = 'Gateway#get_account_data'


  # Account-infos have to be requested sequentially. 
  # 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 |  |
    # don't repeat the query until 170 sec. have passed since the previous update
    if .lad.nil?  || ( Time.now - .lad ) > 170 # sec   
      logger.debug{ "#{account.account} :: Requesting AccountData " }
      [:active] =  false  # indicates: AccountUpdate in Progress, volatile
      # reset account and portfolio-values
      .portfolio_values =  []
      . =  []
      send_message :RequestAccountData, subscribe: true, account_code: .
      loop{ sleep 0.1; break if .active  }
#       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
      send_message :RequestAccountData, subscribe: false  ## do this only once
    else
      logger.info{ "#{account.account} :: Using stored AccountData " }
    end
  end
  nil
end

#subscribe_account_updates(continously: true) ⇒ Object

The subscription method should called only once per session. It places subscribers to AccountValue and PortfolioValue Messages, which should remain active through its session.

Account- and Portfolio-Values are stored in account.account_values and account.portfolio_values The Arrays are volatile.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ib/account-init.rb', line 120

def  continously: true
  puts "SELF: #{self.class.to_s}"
   IB::Connection.current.subscribe( :AccountValue, :PortfolioValue,:AccountDownloadEnd )  do | msg |
     = @accounts.detect{|a| a. == msg. }
    case msg
    when IB::Messages::Incoming::AccountValue
      . = [] unless ..present?  
      ..<< msg.

      [:lad] = Time.now
#       logger.debug { "#{account.account} :: #{msg.account_value.to_human }"}
    when IB::Messages::Incoming::AccountDownloadEnd 
      if ..size > 10
        # simply don't cancel the subscription if continuously is specified
        # the connected flag is set in any case, indicating that valid data are present
        send_message :RequestAccountData, subscribe: false, account_code: . unless continously
        [:active] = true   ## flag: Account is completely initialized
        logger.info { "#{account.account} => Count of AccountValues: #{account.account_values.size}"  }
      else # unreasonable account_data received -  request is still active
        error  "#{account.account} => Count of AccountValues too small: #{account.account_values.size}" , :reader 
      end
    when IB::Messages::Incoming::PortfolioValue
      .contracts << msg.contract.save
      .portfolio_values << msg.portfolio_value 
      logger.debug { "#{ account.account } :: #{ msg.contract.to_human }" }
    end # case
  end # subscribe
end