How to use IG’s trading APIs
Learn how to start building your API setup with IG – including what REST and streaming APIs are and how to use them.
Call +35 318 009 95362 or email newaccounts.uk@ig.com to talk about opening a trading account. We’re here 24 hours a day, from 8am Saturday to 10pm Friday.
Contact us: +35 318 009 95362
What APIs can I use with IG?
There are two APIs that you can use with IG:
- Web API
- A FIX API
The first offering comprises our REST API and streaming API, and is available for retail clients via our IG labs site.
Our FIX API is only available for institutional clients.
It is important to note that these APIs do not have their own platform. They are solutions that provide direct access to the IG ecosystem, which enables you to retrieve historic data, real-time pricing and execute orders.
Effective use of the API does require previous development or coding experience. However, we do have a team of experts ready to help you. Alternatively, if you want to use an advanced platform that is capable of trading with algorithms – as well as a range of other features – you can use the ProRealTime platform.
REST API guide
What is a REST API?
REST stands for representational state transfer, which is the standard way of accessing resources on the internet. The process involves two parties; the client and the resource. In this case, the person using the API (you) is the client, and the resource is the IG trading ecosystem.
A REST API requires an automated request, written in code, in order to trigger an automatic, static response from the IG system. The REST API only retrieves a screenshot of information at that point in time, so it is used to gather specific information and take a variety of actions, such as accessing account history and opening or closing positions.
How to get started with IG’s REST API
To begin using our API all you need to do is:
- Create an IG account
- Generate your API key and security token
- Practise building REST code using our sample apps
- Start coding on any programme that supports HTTP
Learn more about getting started.
You can also use an IG demo account to practise using our APIs, but you must ensure you sign up with the same email address as your live account.
Note: a security token is required to log into and access the API. You will need to submit a request for an access token. Each token is only valid for a limited period of time, which will need to be specified on the request.
How to use IG’s REST API
Using IG’s REST API requires an understanding of development and coding. There are four classifications of operations you should understand, these are:
- GET, which enables you to retrieve a resource
- POST, which enables you to create a resource
- PUT, which enables you to replace a resource
- DELETE, which enables you to delete a resource
These enable you to automatically set the parameters of your commands and interact with the IG trading system. There are a variety of different actions that fall into these four categories and cover everything from accessing your account’s history to creating an OTC order.
For a more in-depth look at how REST API components work, visit our IG Labs REST API guide. You can even try out our REST API by using our API companion – it gives you the complete experience so that you can build your confidence making requests and viewing responses.
REST API example requests
Let’s say that you wanted a list of all account history within a given date range. This would be classified as a ‘GET’ command, as you will be retrieving information. The coded command would be:
https://demo-api.ig.com/gateway/deal/history/activity/{fromDate}/{toDate}
If you wanted to deal instead, you could create an over-the-counter (OTC) position through the ‘POST’ function. The command would be:
https://demo-api.ig.com/gateway/deal/positions/otc
See more REST API example requests.
Streaming API guide
What is a streaming API?
A streaming API, as the name suggests, provides a continuous flow of information to the user. Unlike the REST API, which is just a snapshot of a specific moment, the streaming API will continue to update the user after the initial request. This makes it useful for subscribing to markets and viewing the most up-to-date price information.
To use a streaming API, it is advisable to have a base knowledge of REST APIs as the two are used in conjunction with each other.
How to get started with IG’s streaming API
To begin using our API all you need to do is:
- Download the Lightstreamer client library to your programming platform
- Create an IG account
- Generate your API key and your security token
- Start coding on any programme that supports HTTP
Note: you will need a security token for logging into and accessing the API. You will need to submit a request for an access token, and each is only valid for a limited period of time, which will need to be specified by the request.
How to use IG’s streaming API
The IG steaming API enables you to get real-time market prices, trade updates and account status notifications. To get the benefits of the live API, you will need to subscribe to Lightstreamer – this consists of filing a list of all the items you’d like access to (eg IG markets) and all of the fields relevant to those items (eg bid and ask prices).
For a more in-depth look at how our streaming API works, visit our IG Labs streaming API guide. You can even try out our streaming companion to test out the service in an interactive environment.
The full list of streaming subscriptions can be found here.
The basics of trading with APIs
Even if you’re ready to start trading APIs, it is important to understand some of the basic concepts involved, such as what instruments you’ll be trading, how they are priced and how to open a position.
Market instruments
Each market instrument you see on IG’s API represents either a CFD or spread bet. These are both derivative products, which take their price from an underlying asset. Trading CFDs or spread betting enables you to go long or short on the market in question, profiting from rising and falling prices.
Markets will have specific identifiers, which on APIs are called EPICS.
For example: IX.D.FTSE.CFD.IP
This would mean that you are looking to trade a CFD contract on the FTSE 100 cash price market.
API pricing
There are two ways that you can access the most up-to-date pricing from IG:
- Subscribe to the instrument via the streaming API
- Obtain a snapshot of the instrument’s price via the REST API
Historical prices can also be requested using the REST API.
Opening and closing a position via an API
Just like opening a position on a regular market, when you open a position through an API, you will need to make some basic choices. Firstly, you’ll need to decide which market to trade – this should always be based on your personal preferences and interests. Second, you’ll need to decide at which price level to open your trade. Most traders will use fundamental and technical analysis to create a methodology for opening a position. And finally, you’d need to decide whether you are buying or selling the market.
Once you have reached a certain level of profit or loss, you might decide it’s time to close your position.
Both the opening and closing of positions occurs via the /positions command of the API, or the /workingorders API – working orders will only open and close the position when a certain price level is met, in much the same way as a regular stop or limit.
Learn more about API order types.
FAQs
Can I use Python with IG APIs?
Yes, you can use the Python programming language with IG, but it is important to ensure that you have an understanding of the package and how to test it. In fact, you can use any programming language that supports HTTP.
Get started by trying out our API companion and streaming companion.
What historical data is available?
We provide up to 20 years of historical price data, which will be available to you depending on different resolutions – the time it takes for an entity to gather the data. For example, it could only take one second to retrieve four days’ worth of data, but it could take a full day to gather 15 years’ worth of data.
Some of our markets hold more historical data than others for each timeframe. You’ll be able to see the full scope of each market’s data by logging into IG’s web platform – everything you see here is what you’ll be able to access through the API.
Your order might be rejected for a number of reasons, such as the market you are attempting to trade is closed, we have had to disable API traders or there is an error with your submission.
You can use the contact us form to raise any issues.
Why can't I apply for an API key in my demo account?
If you are having trouble applying for an API through your demo account, it is likely because your demo account email differs from your live account. You will need to apply for another demo account and ensure that the email address you use on the application is the same as your live account.