Class: IGMarkets::DealingPlatform::StreamingMethods
- Inherits:
-
Object
- Object
- IGMarkets::DealingPlatform::StreamingMethods
- Defined in:
- lib/ig_markets/dealing_platform/streaming_methods.rb
Overview
Provides methods for working with streaming of IG Markets data. Returned by #streaming.
Instance Method Summary collapse
-
#build_accounts_subscription(accounts = nil) ⇒ Streaming::Subscription
Creates a new subscription for balance updates on the specified account(s).
-
#build_chart_ticks_subscription(epics) ⇒ Streaming::Subscription
Creates a new Lightstreamer subscription for chart tick data for the specified EPICs.
-
#build_consolidated_chart_data_subscription(epic, scale) ⇒ Streaming::Subscription
Creates a new Lightstreamer subscription for consolidated chart data for the specified EPIC and scale.
-
#build_markets_subscription(epics) ⇒ Streaming::Subscription
Creates a new Lightstreamer subscription for updates to the specified market(s).
-
#build_trades_subscription(accounts = nil) ⇒ Streaming::Subscription
Creates a new Lightstreamer subscription for trade, position and working order updates on the specified account(s).
-
#connect ⇒ Object
Connects the streaming session.
-
#disconnect ⇒ Object
Disconnects the streaming session.
-
#initialize(dealing_platform) ⇒ StreamingMethods
constructor
Initializes this class with the specified dealing platform.
-
#on_error(&callback) ⇒ Object
Adds the passed block to the list of callbacks that will be run when the streaming session encounters an error.
-
#remove_subscriptions(subscriptions) ⇒ Object
Stops streaming data for the specified subscription(s) and removes them from the streaming session.
-
#start_subscriptions(subscriptions, options = {}) ⇒ Array<Lightstreamer::LightstreamerError, nil>
Starts streaming data from the passed Lightstreamer subscription(s).
Constructor Details
#initialize(dealing_platform) ⇒ StreamingMethods
Initializes this class with the specified dealing platform.
8 9 10 11 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 8 def initialize(dealing_platform) @dealing_platform = WeakRef.new dealing_platform @on_error_callbacks = [] end |
Instance Method Details
#build_accounts_subscription(accounts = nil) ⇒ Streaming::Subscription
Creates a new subscription for balance updates on the specified account(s). The returned Streaming::Subscription must be passed to #start_subscriptions in order to actually start streaming its data.
46 47 48 49 50 51 52 53 54 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 46 def build_accounts_subscription(accounts = nil) accounts ||= @dealing_platform.client_account_summary.accounts items = Array(accounts).map { |account| "ACCOUNT:#{account.account_id}" } fields = %i[available_cash available_to_deal deposit equity equity_used funds margin margin_lr margin_nlr pnl pnl_lr pnl_nlr] build_subscription items: items, fields: fields, mode: :merge end |
#build_chart_ticks_subscription(epics) ⇒ Streaming::Subscription
Creates a new Lightstreamer subscription for chart tick data for the specified EPICs. The returned Streaming::Subscription must be passed to #start_subscriptions in order to actually start streaming its data.
95 96 97 98 99 100 101 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 95 def build_chart_ticks_subscription(epics) items = Array(epics).map { |epic| "CHART:#{epic}:TICK" } fields = %i[bid day_high day_low day_net_chg_mid day_open_mid day_perc_chg_mid ltp ltv ofr ttv utm] build_subscription items: items, fields: fields, mode: :distinct end |
#build_consolidated_chart_data_subscription(epic, scale) ⇒ Streaming::Subscription
Creates a new Lightstreamer subscription for consolidated chart data for the specified EPIC and scale. The returned Streaming::Subscription must be passed to #start_subscriptions in order to actually start streaming its data.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 111 def build_consolidated_chart_data_subscription(epic, scale) scale = { one_second: 'SECOND', one_minute: '1MINUTE', five_minutes: '5MINUTE', one_hour: 'HOUR' }.fetch scale items = ["CHART:#{epic}:#{scale}"] fields = %i[bid_close bid_high bid_low bid_open cons_end cons_tick_count day_high day_low day_net_chg_mid day_open_mid day_perc_chg_mid ltp_close ltp_high ltp_low ltp_open ltv ofr_close ofr_high ofr_low ofr_open ttv utm] build_subscription items: items, fields: fields, mode: :merge end |
#build_markets_subscription(epics) ⇒ Streaming::Subscription
Creates a new Lightstreamer subscription for updates to the specified market(s). The returned Streaming::Subscription must be passed to #start_subscriptions in order to actually start streaming its data.
63 64 65 66 67 68 69 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 63 def build_markets_subscription(epics) items = Array(epics).map { |epic| "MARKET:#{epic}" } fields = %i[bid change change_pct high low market_delay market_state mid_open odds offer strike_price update_time] build_subscription items: items, fields: fields, mode: :merge end |
#build_trades_subscription(accounts = nil) ⇒ Streaming::Subscription
Creates a new Lightstreamer subscription for trade, position and working order updates on the specified account(s). The returned Streaming::Subscription must be passed to #start_subscriptions in order to actually start streaming its data.
79 80 81 82 83 84 85 86 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 79 def build_trades_subscription(accounts = nil) accounts ||= @dealing_platform.client_account_summary.accounts items = Array(accounts).map { |account| "TRADE:#{account.account_id}" } fields = %i[confirms opu wou] build_subscription items: items, fields: fields, mode: :distinct end |
#connect ⇒ Object
Connects the streaming session. Raises a ‘Lightstreamer::LightstreamerError` if an error occurs.
14 15 16 17 18 19 20 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 14 def connect @lightstreamer&.disconnect @lightstreamer = Lightstreamer::Session.new username: username, password: password, server_url: server_url @lightstreamer.on_error { |error| @on_error_callbacks.each { |callback| callback.call error } } @lightstreamer.connect end |
#disconnect ⇒ Object
Disconnects the streaming session.
23 24 25 26 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 23 def disconnect @lightstreamer&.disconnect @lightstreamer = nil end |
#on_error(&callback) ⇒ Object
Adds the passed block to the list of callbacks that will be run when the streaming session encounters an error. The block will be called on a worker thread and so the code that is run by the block must be thread-safe. The argument passed to the block is ‘|error|`, which will be a `Lightstreamer::LightstreamerError` subclass detailing the error that occurred.
34 35 36 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 34 def on_error(&callback) @on_error_callbacks << callback end |
#remove_subscriptions(subscriptions) ⇒ Object
Stops streaming data for the specified subscription(s) and removes them from the streaming session.
142 143 144 145 146 147 148 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 142 def remove_subscriptions(subscriptions) lightstreamer_subscriptions = Array(subscriptions).compact.map(&:lightstreamer_subscription) return if lightstreamer_subscriptions.empty? @lightstreamer.remove_subscriptions lightstreamer_subscriptions end |
#start_subscriptions(subscriptions, options = {}) ⇒ Array<Lightstreamer::LightstreamerError, nil>
Starts streaming data from the passed Lightstreamer subscription(s). The return value indicates the error state, if any, for each subscription.
131 132 133 134 135 136 137 |
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 131 def start_subscriptions(subscriptions, = {}) lightstreamer_subscriptions = Array(subscriptions).compact.map(&:lightstreamer_subscription) return if lightstreamer_subscriptions.empty? @lightstreamer.start_subscriptions lightstreamer_subscriptions, end |