Ruby IG Markets Dealing Platform Gem
Easily access the IG Markets Dealing Platform from Ruby with this gem, either directly through code or by using the provided command-line client. Written against the official REST API.
Includes support for:
- Activity and transaction history
- Positions
- Sprint market positions
- Working orders
- Market navigation, searches and snapshots
- Historical prices
- Watchlists
- Client sentiment
An IG Markets production or demo trading account is needed in order to use this gem.
License
Licensed under the MIT license. You must read and agree to its terms to use this software.
Installation
Install the latest version of the ig_markets gem with the following command:
$ gem install ig_markets
Usage — Command-Line Client
The general form for invoking the command-line client is:
$ ig_markets COMMAND [SUBCOMMAND] --username USERNAME --password PASSWORD --api-key API-KEY [--demo] [...]
Config File
On startup ig_markets searches for config files named "./.ig_markets" and then "~/.ig_markets", and if they are
present interprets their contents as command-line arguments. This can be used to avoid having to specify authentication
details with every invocation.
To do this create a config file at "./.ig_markets" or "~/.ig_markets" with the following contents:
--username USERNAME
--password PASSWORD
--api-key API-KEY
--demo # Include only if this is a demo account
The following examples assume the presence of a config file that contains valid authentication details.
Commands
Use ig_markets help to get details on the options accepted by the commands and subcommands. The list of available
commands and their subcommands is:
ig_markets accountig_markets activities --days N [--start-date YYYY-MM-DD]ig_markets confirmation DEAL-REFERENCEig_markets help [COMMAND]ig_markets orders [list]ig_markets orders create ...ig_markets orders update DEAL-ID ...ig_markets orders delete DEAL-IDig_markets positions [list] [--aggregate]ig_markets positions create ...ig_markets positions update DEAL-ID ...ig_markets positions close DEAL-ID [...]ig_markets prices --epic EPIC --resolution RESOLUTION [...]ig_markets search QUERY [--type TYPE]ig_markets sentiment MARKETig_markets sprints [list]ig_markets sprints create ...ig_markets transactions --days N [--start-date YYYY-MM-DD] [--instrument INSTRUMENT] [--no-interest]ig_markets watchlists [list]ig_markets watchlists create NAME [EPIC EPIC ...]ig_markets watchlists add-markets WATCHLIST-ID [EPIC EPIC ...]ig_markets watchlists remove-markets WATCHLIST-ID [EPIC EPIC ...]ig_markets watchlists delete WATCHLIST-ID
Examples
# Print account details and balances
ig_markets account
# Print EUR/USD transactions from the last week, excluding interest transactions
ig_markets transactions --days 7 --instrument EUR/USD --no-interest
# Search for EURUSD currency markets
ig_markets search EURUSD --type currencies
# Print current positions in aggregate
ig_markets positions --aggregate
# Create a EURUSD long position of size 2
ig_markets positions create --direction buy --epic CS.D.EURUSD.CFD.IP --size 2
# Change the limit and stop levels for an existing position
ig_markets positions update DEAL-ID --limit-level 1.15 --stop-level 1.10
# Fully close a position
ig_markets positions close DEAL-ID
# Partially close a position (assuming its size is greater than 1)
ig_markets positions close DEAL-ID --size 1
# Create a EURUSD sprint market short position of size 100 that expires in 20 minutes
ig_markets sprints create --direction sell --epic FM.D.EURUSD24.EURUSD24.IP --expiry-period 20 --size 100
# Create a working order to buy 1 unit of EURUSD at the level 1.1
ig_markets orders create --direction buy --epic CS.D.EURUSD.CFD.IP --level 1.1 --size 1 --type limit
# Print daily prices for EURUSD from the last two weeks
ig_markets prices --epic CS.D.EURUSD.CFD.IP --resolution day --number 14
Account Time Zone
Some timestamps returned by the IG Markets API are in an unspecified time zone, which can result in some times
displayed by the command-line client being incorrect. By default the unknown time zone is assumed to be UTC, but if this
is incorrect then use the --account-time-zone argument to specify the actual time zone these times are in.
For example, an IG Markets Australia account requires --account-time-zone +1000.
The --account-time-zone argument should be put into an .ig_markets config file to avoid specifying it on every
invocation.
To check that the account time zone is correct run the following command and verify that the date and time reported for the price is the current date/time in your local time zone.
ig_markets prices --epic CS.D.EURUSD.CFD.IP --resolution minute --number 1
Usage — Library
Documentation
API documentation is available here.
Examples
require 'ig_markets'
ig = IGMarkets::DealingPlatform.new
# Session
ig.sign_in 'username', 'password', 'api_key', :demo
ig.sign_out
# Set the time zone for the account, this is applied to time attributes returned from the IG Markets API that are not
# in a known time zone such as UTC
ig.account_time_zone = '+0000'
# Account
ig.account.all
ig.account.recent_activities 365
ig.account.recent_transactions 365
ig.account.activities_in_date_range Date.today - 14, Date.today - 7
ig.account.transactions_in_date_range Date.today - 14, Date.today - 7
# Dealing
ig.deal_confirmation 'deal_reference'
# Positions
ig.positions.all
ig.positions.create currency_code: 'USD', direction: :buy, epic: 'CS.D.EURUSD.CFD.IP', size: 2
ig.positions['deal_id']
ig.positions['deal_id'].profit_loss
ig.positions['deal_id'].update limit_level: 1.2, stop_level: 1.1
ig.positions['deal_id'].close
# Sprint market positions
ig.sprint_market_positions.all
ig.sprint_market_positions['deal_id']
ig.sprint_market_positions.create direction: :buy, epic: 'FM.D.EURUSD24.EURUSD24.IP',
expiry_period: :twenty_minutes, size: 100
# Working orders
ig.working_orders.all
ig.working_orders.create currency_code: 'USD', direction: :buy, epic: 'CS.D.EURUSD.CFD.IP', level: 0.99,
size: 1, type: :limit
ig.working_orders['deal_id']
ig.working_orders['deal_id'].update level: 1.25, limit_distance: 50, stop_distance: 0.02
ig.working_orders['deal_id'].delete
# Markets
ig.markets.hierarchy
ig.markets.search 'EURUSD'
ig.markets['CS.D.EURUSD.CFD.IP']
ig.markets['CS.D.EURUSD.CFD.IP'].recent_prices :day, 10
ig.markets['CS.D.EURUSD.CFD.IP'].prices_in_date_range :day, Date.today - 14, Date.today - 7
# Watchlists
ig.watchlists.all
ig.watchlists.create 'New Watchlist', 'CS.D.EURUSD.CFD.IP', 'UA.D.AAPL.CASH.IP'
ig.watchlists['watchlist_id']
ig.watchlists['watchlist_id'].markets
ig.watchlists['watchlist_id'].add_market 'CS.D.EURUSD.CFD.IP'
ig.watchlists['watchlist_id'].remove_market 'CS.D.EURUSD.CFD.IP'
ig.watchlists['watchlist_id'].delete
# Client sentiment
ig.client_sentiment['EURUSD']
ig.client_sentiment['EURUSD'].
# Miscellaneous
ig.applications
Contributors
Gem created by Richard Viney. All contributions welcome.