Class: IGMarkets::DealingPlatform::StreamingMethods

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(dealing_platform) ⇒ StreamingMethods

Initializes this class with the specified dealing platform.

Parameters:


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.

Parameters:

  • accounts (Array<#account_id>, nil) (defaults to: nil)

    The accounts to create a subscription for. If this is nil then the new subscription will apply to all the accounts for the active client.

Returns:


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..accounts

  items = Array(accounts).map { || "ACCOUNT:#{.}" }
  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.

Parameters:

  • epics (Array<String>)

    The EPICs of the markets to create a chart tick data subscription for.

Returns:


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.

Parameters:

  • epic (String)

    The EPIC of the market to create a consolidated chart data subscription for.

  • scale (:one_second, :one_minute, :five_minutes, :one_hour)

    The scale of the consolidated data.

Returns:


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.

Parameters:

  • epics (Array<String>)

    The EPICs of the markets to create a subscription for.

Returns:


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.

Parameters:

  • accounts (Array<#account_id>, nil) (defaults to: nil)

    The accounts to create a subscription for. If this is nil then the new subscription will apply to all the accounts for the active client.

Returns:


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..accounts

  items = Array(accounts).map { || "TRADE:#{.}" }
  fields = %i[confirms opu wou]

  build_subscription items: items, fields: fields, mode: :distinct
end

#connectObject

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 if @lightstreamer

  @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

#disconnectObject

Disconnects the streaming session.


23
24
25
26
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 23

def disconnect
  @lightstreamer.disconnect if @lightstreamer
  @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.

Parameters:

  • callback (Proc)

    The callback that is to be run.


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.

Parameters:

  • subscriptions (Array<Lightstreamer::Subscription>)

    The subscriptions to stop.


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.

Parameters:

  • subscriptions (Array<Streaming::Subscription>)
  • options (Hash) (defaults to: {})

    The options to start the subscriptions with. See the documentation for Lightstreamer::Subscription#start for details of the accepted options.

Returns:

  • (Array<Lightstreamer::LightstreamerError, nil>)

    An array with one entry per subscription which indicates the error state returned for that subscription's start request, or nil if no error occurred.


131
132
133
134
135
136
137
# File 'lib/ig_markets/dealing_platform/streaming_methods.rb', line 131

def start_subscriptions(subscriptions, options = {})
  lightstreamer_subscriptions = Array(subscriptions).compact.map(&:lightstreamer_subscription)

  return if lightstreamer_subscriptions.empty?

  @lightstreamer.start_subscriptions lightstreamer_subscriptions, options
end