Class: IGMarkets::Streaming::AccountState
- Inherits:
-
Object
- Object
- IGMarkets::Streaming::AccountState
- Defined in:
- lib/ig_markets/streaming/account_state.rb
Overview
This class tracks the current state of a dealing platform’s accounts, positions and working orders by subscribing to the relevant streaming data feeds supported by the IG Markets streaming API. This is much more efficient than making repeated calls to the REST API to get current account, position or working order details, and also avoids running into API traffic allowance limits.
Instance Attribute Summary collapse
-
#accounts ⇒ Array<Account>
The current state of the accounts.
-
#positions ⇒ Array<Position>
The current positions for this account.
-
#working_orders ⇒ Array<WorkingOrder>
The current working orders for this account.
Instance Method Summary collapse
-
#data_queue_empty? ⇒ Boolean
Returns whether the data queue is empty.
-
#initialize(dealing_platform) ⇒ AccountState
constructor
Initializes this account state with the specified dealing platform.
-
#process_queued_data ⇒ Object
Processes all queued data and updates #accounts, #positions and #working_orders accordingly.
-
#start ⇒ Object
Starts all the relevant streaming subscriptions that are needed to keep this account state up to date using live streaming updates.
Constructor Details
#initialize(dealing_platform) ⇒ AccountState
Initializes this account state with the specified dealing platform.
23 24 25 26 27 28 29 30 |
# File 'lib/ig_markets/streaming/account_state.rb', line 23 def initialize(dealing_platform) @dealing_platform = dealing_platform @data_queue = Queue.new @market_subscriptions_manager = MarketSubscriptionManager.new dealing_platform @market_subscriptions_manager.on_data { |_data, merged_data| @data_queue << [:on_market_update, merged_data] } end |
Instance Attribute Details
#accounts ⇒ Array<Account>
Returns the current state of the accounts. The balances will be updated automatically as new streaming data arrives.
10 11 12 |
# File 'lib/ig_markets/streaming/account_state.rb', line 10 def accounts @accounts end |
#positions ⇒ Array<Position>
Returns the current positions for this account. This set of positions will be updated automatically as new streaming data arrives.
14 15 16 |
# File 'lib/ig_markets/streaming/account_state.rb', line 14 def positions @positions end |
#working_orders ⇒ Array<WorkingOrder>
Returns the current working orders for this account. This set of working orders will be updated automatically as new streaming data arrives.
18 19 20 |
# File 'lib/ig_markets/streaming/account_state.rb', line 18 def working_orders @working_orders end |
Instance Method Details
#data_queue_empty? ⇒ Boolean
Returns whether the data queue is empty. If this returns false then there is data waiting to be processed by a call to #process_queued_data.
53 54 55 |
# File 'lib/ig_markets/streaming/account_state.rb', line 53 def data_queue_empty? @data_queue.empty? end |
#process_queued_data ⇒ Object
Processes all queued data and updates #accounts, #positions and #working_orders accordingly. If there are no queued data updates then this method blocks until at least one data update is available.
59 60 61 62 63 64 65 66 67 |
# File 'lib/ig_markets/streaming/account_state.rb', line 59 def process_queued_data loop do send(*@data_queue.pop) break if @data_queue.empty? end update_market_subscriptions end |
#start ⇒ Object
Starts all the relevant streaming subscriptions that are needed to keep this account state up to date using live streaming updates. After this account state has been started the #process_queued_data method must be called repeatedly to process the queue of streaming updates and apply them to #accounts, #positions and #working_orders as appropriate.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ig_markets/streaming/account_state.rb', line 36 def start start_accounts_subscription start_trades_subscription @accounts = @dealing_platform.account.all @positions = @dealing_platform.positions.all @working_orders = @dealing_platform.working_orders.all @trades_subscription.unsilence update_market_subscriptions end |