Class: IGMarkets::DealingPlatform

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/dealing_platform.rb,
lib/ig_markets/dealing_platform/market_methods.rb,
lib/ig_markets/dealing_platform/account_methods.rb,
lib/ig_markets/dealing_platform/position_methods.rb,
lib/ig_markets/dealing_platform/streaming_methods.rb,
lib/ig_markets/dealing_platform/watchlist_methods.rb,
lib/ig_markets/dealing_platform/working_order_methods.rb,
lib/ig_markets/dealing_platform/client_sentiment_methods.rb,
lib/ig_markets/dealing_platform/sprint_market_position_methods.rb

Overview

This is the primary class for interacting with the IG Markets API. After signing in using #sign_in most functionality can be accessed using the following methods:

See ‘README.md` for code examples.

If any errors occur while executing requests to the IG Markets API then an IGMarketsError subclass will be raised.

Defined Under Namespace

Classes: AccountMethods, ClientSentimentMethods, MarketMethods, PositionMethods, SprintMarketPositionMethods, StreamingMethods, WatchlistMethods, WorkingOrderMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDealingPlatform

Returns a new instance of DealingPlatform.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ig_markets/dealing_platform.rb', line 68

def initialize
  @session = Session.new

  @account = AccountMethods.new self
  @client_sentiment = ClientSentimentMethods.new self
  @markets = MarketMethods.new self
  @positions = PositionMethods.new self
  @sprint_market_positions = SprintMarketPositionMethods.new self
  @watchlists = WatchlistMethods.new self
  @working_orders = WorkingOrderMethods.new self
  @streaming = StreamingMethods.new self
end

Instance Attribute Details

#accountAccountMethods (readonly)

Methods for working with the balances and history of the logged in account.

Returns:



31
32
33
# File 'lib/ig_markets/dealing_platform.rb', line 31

def 
  @account
end

#client_account_summaryClientAccountSummary (readonly)

The summary of the client account that is returned as part of a successful sign in.



26
27
28
# File 'lib/ig_markets/dealing_platform.rb', line 26

def 
  @client_account_summary
end

#client_sentimentClientSentimentMethods (readonly)

Methods for working with client sentiment.



36
37
38
# File 'lib/ig_markets/dealing_platform.rb', line 36

def client_sentiment
  @client_sentiment
end

#marketsMarketMethods (readonly)

Methods for working with markets.

Returns:



41
42
43
# File 'lib/ig_markets/dealing_platform.rb', line 41

def markets
  @markets
end

#positionsPositionMethods (readonly)

Methods for working with positions.

Returns:



46
47
48
# File 'lib/ig_markets/dealing_platform.rb', line 46

def positions
  @positions
end

#sessionSession (readonly)

The session used by this dealing platform.

Returns:



21
22
23
# File 'lib/ig_markets/dealing_platform.rb', line 21

def session
  @session
end

#sprint_market_positionsSprintMarketPositionMethods (readonly)

Methods for working with sprint market positions.



51
52
53
# File 'lib/ig_markets/dealing_platform.rb', line 51

def sprint_market_positions
  @sprint_market_positions
end

#streamingStreamingMethods (readonly)

Methods for working with live streaming of IG Markets data.

Returns:



66
67
68
# File 'lib/ig_markets/dealing_platform.rb', line 66

def streaming
  @streaming
end

#watchlistsWatchlistMethods (readonly)

Methods for working with watchlists.

Returns:



56
57
58
# File 'lib/ig_markets/dealing_platform.rb', line 56

def watchlists
  @watchlists
end

#working_ordersWorkingOrderMethods (readonly)

Methods for working with working orders.

Returns:



61
62
63
# File 'lib/ig_markets/dealing_platform.rb', line 61

def working_orders
  @working_orders
end

Instance Method Details

#applicationsArray<Application>

Returns details on the IG Markets applications for the accounts associated with this login.

Returns:



117
118
119
# File 'lib/ig_markets/dealing_platform.rb', line 117

def applications
  instantiate_models Application, session.get('operations/application')
end

#deal_confirmation(deal_reference) ⇒ DealConfirmation

Returns a full deal confirmation for the specified deal reference.

Returns:



110
111
112
# File 'lib/ig_markets/dealing_platform.rb', line 110

def deal_confirmation(deal_reference)
  instantiate_models DealConfirmation, session.get("confirms/#{deal_reference}")
end

#disable_api_keyApplication

Disables the API key currently being used by the logged in session. This means that any further requests to the IG Markets API with this key will raise Errors::APIKeyDisabledError. Disabled API keys can only be re-enabled through the web platform.

Returns:



126
127
128
# File 'lib/ig_markets/dealing_platform.rb', line 126

def disable_api_key
  instantiate_models Application, session.put('operations/application/disable')
end

#instantiate_models(model_class, source) ⇒ nil, ...

This method is used to instantiate the various ‘Model` subclasses from data returned by the IG Markets API. It recurses through arrays and sub-hashes present in `source`, instantiating the required models based on the types of each attribute as defined on the models. All model instances returned by this method will have their `@dealing_platform` instance variable set.

Parameters:

  • model_class (Class)

    The top-level model class to create from ‘source`.

  • source (nil, Hash, Array, Model)

    The source object to construct the model(s) from. If ‘nil` then `nil` is returned. If an instance of `model_class` subclass then a deep copy of it is returned. If a `Hash` then it will be interpreted as the attributes for a new instance of `model_class. If an `Array` then each entry will be passed through this method individually.

Returns:

  • (nil, `model_class`, Array<`model_class`>)

    The resulting instantiated model(s).



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ig_markets/dealing_platform.rb', line 145

def instantiate_models(model_class, source)
  return nil if source.nil?

  source = prepare_source model_class, source

  if source.is_a? Array
    source.map { |entry| instantiate_models model_class, entry }
  elsif source.is_a? Hash
    instantiate_model_from_attributes_hash model_class, source
  else
    raise ArgumentError, "#{model_class}: can't instantiate from a source of type #{source.class}"
  end
end

#instantiate_models_from_json(model_class, json) ⇒ nil, `model_class`

This method is the same as #instantiate_models but takes an unparsed JSON string as its input.

Parameters:

  • model_class (Class)

    The top-level model class to create from ‘json`.

  • json (String)

    The JSON string to parse.

Returns:

  • (nil, `model_class`)

    The resulting instantiated model.



167
168
169
# File 'lib/ig_markets/dealing_platform.rb', line 167

def instantiate_models_from_json(model_class, json)
  instantiate_models model_class, ResponseParser.parse(JSON.parse(json))
end

#sign_in(username, password, api_key, platform) ⇒ ClientAccountSummary

Signs in to the IG Markets Dealing Platform, either the live platform or the demo platform.

Parameters:

  • username (String)

    The IG Markets username.

  • password (String)

    The IG Markets password.

  • api_key (String)

    The IG Markets API key.

  • platform (:live, :demo)

    The platform to use.

Returns:



90
91
92
93
94
95
96
97
98
99
# File 'lib/ig_markets/dealing_platform.rb', line 90

def (username, password, api_key, platform)
  session.username = username
  session.password = password
  session.api_key = api_key
  session.platform = platform

  result = session.

  @client_account_summary = instantiate_models ClientAccountSummary, result
end

#sign_outObject

Signs out of the IG Markets Dealing Platform, ending any current session.



102
103
104
105
# File 'lib/ig_markets/dealing_platform.rb', line 102

def sign_out
  streaming.disconnect
  session.sign_out
end