Python Code to Download Prices

18 posts / 0 new
Last post
kickitbass
Python Code to Download Prices

Hi All,
Can you please point me in the right direction.
I am new to python as well as the rest api.
I am trying to write a script to download price data for an epic.

I can't seem to get the syntax right to download price data.

My script successfully logs in.

The code block below is my latest attempt.
My suspicion is that either my syntax for the logged in headers is wrong. Or even more likely, that I have got the URL syntax wrong.

# Start Phase2 - wip

_EPIC = "CC.D.C.UNC.IP"
_RESOLUTION = "DAY"
_STARTDATE = "2017-09-05T12:00:00"
_ENDDATE = "2017-09-07T12:00:00"

#I got this syntax from the API Companion
phase2_url = "https://api.ig.com/gateway/deal/prices/"
phase2_url1 = phase2_url + _EPIC + "?resolution=" + _RESOLUTION + "&from=" + _STARTDATE + "&to=" + _ENDDATE

phase2_headers = {"Content-Type": "application/json", "Accept": "application/json; charset=utf-8", "X-IG-API-KEY": m_apiKey, "X-SECURITY-TOKEN": SECURITY_TOKEN, "CST": CLIENT_TOKEN}
phase2_r = requests.get(phase2_url1, headers=phase2_headers)

print(phase2_r.status_code)
print(phase2_r.text)
print(phase2_r.headers)

# End Phase2

When I run this code, I get the following response:

>>> print(phase2_r.status_code)
404
>>> print(phase2_r.text)
<html><head><title>Page Not Found</title><h3>Error 404</h3><h3>Sorry, the requested page is not available.</h3></body></html>

Thanks in advance.
Cheers.

hibernate
Hi,

Hi,

I have the exact same problem. I tried so many variations of the URL but nothing gets through. I also contacted the IG support but they also couldn't help me further. I really would appreciate if somebody could help.

Thanks in advance!

kickitbass
Fixed it

Hi Hibernate,

I managed to fix this myself. In my case, I just had to put version 3 in the header.
The Labs.IG documentation says version 2, but that's bogus. I you punch in a request in the api companion, you'll notice that version 3 is being used. Here's the code, hope it helps you out.

_EPIC = "CC.D.C.UNC.IP"
_RESOLUTION = "DAY"
_STARTDATE = "2017-09-05"
_ENDDATE = "2017-09-07"

phase2_url = "https://api.ig.com/gateway/deal/prices/"
phase2_url1 = phase2_url + _EPIC + "?resolution=" + _RESOLUTION + "&from=" + _STARTDATE + "&to=" + _ENDDATE
print(phase2_url1)

phase2_headers = {"Content-Type": "application/json; charset=utf-8", "Accept": "application/json; charset=utf-8", "X-IG-API-KEY": m_apiKey, "Version": "3", "X-SECURITY-TOKEN": SECURITY_TOKEN, "CST": CLIENT_TOKEN}
phase2_r = requests.get(phase2_url1, headers=phase2_headers)

print(phase2_r.status_code)
print(phase2_r.text)

hibernate
Hi,

Hi,

thank you very much, I got it to work with your fix. However I do not get the correct data. I receive only the last 10 bars on the instrument but I am requesting yesterday's data. Do you have any idea as to why this is happening?

--- code ---

import datetime

yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
print(yesterday)

year = str(yesterday)[0:4]
month = str(yesterday)[5:7]
day = str(yesterday)[8:10]

url = "https://api.ig.com/gateway/deal/prices/CC.D.FGBL.UME.IP?resolution=MINUT...{0}-{1}-{2}T09:00:00&to{0}-{1}-{2}T20:30:00".format(year, month, day)

--- code ---

hibernate
Ok my post got somewhat

Ok my post got somewhat squeezed so here is the actual link that my code produces:

"https://api.ig.com/gateway/deal/prices/CC.D.FGBL.UME.IP?resolution=MINUT...
2017-09-19T09:00:00&to2017-09-19T20:30:00"

Can you spot what is wrong?

kiann00
I have been trying to code

I have been trying to code/access IG data using python as well. Though not a direct represent, below is what I have to get it to work, for the latest price stream.

It works in my python Anaconda environment version 3.6

--------------
import requests
import json
import pandas as pd

url = 'https://demo-api.ig.com/gateway/deal'
s = requests.Session()
s.headers = { 'Content-Type' : 'application/json; charset=UTF-8', 'Accept' : 'application/json; charset=UTF-8', 'VERSION' : '2', 'X-IG-API-KEY' : 'XXXX'
}
data = { 'identifier' : 'username','password' : 'password'}

# first load and create your session
r = s.post(url + '/session', json=data)
s.headers.update({'X-SECURITY-TOKEN': r.headers['X-SECURITY-TOKEN'],'CST': r.headers['CST']})

# this is to check the session is loaded, and it shows your accounts in pandas dataframe
s.headers.update({'Version': '1'})
r = s.get(url + '/accounts/')
pd.DataFrame(r.json())

# finally, set the version to 3 explicitly as this is IG lab code specific
s.headers.update({'Version': '3'})

epic = 'CS.D.EURUSD.CFD.IP'
r = s.get(url + '/prices/' + epic)
data = pd.DataFrame(r.json()['prices'])
data

kiann00
I tested it via the API

I tested it via the API portal IG Labs provide.

This is an example URL which works perfectly (for me) when used in python code

--------
mport requests
import json
import pandas as pd
url = 'https://demo-api.ig.com/gateway/deal'
s = requests.Session()
s.headers = { 'Content-Type' : 'application/json; charset=UTF-8', 'Accept' : 'application/json; charset=UTF-8', 'VERSION' : '2', 'X-IG-API-KEY' : 'XXXX'
}
data = { 'identifier' : 'username','password' : 'password'}
# first load and create your session
r = s.post(url + '/session', json=data)
s.headers.update({'X-SECURITY-TOKEN': r.headers['X-SECURITY-TOKEN'],'CST': r.headers['CST']})
# this is to check the session is loaded, and it shows your accounts in pandas dataframe
s.headers.update({'Version': '3'})
r = s.get(url + '/accounts/')

loc = 'https://demo-api.ig.com/gateway/deal/prices/CS.D.EURUSD.CFD.IP? _
_resolution=DAY&from=2017-09-05T12%3A00%3A00&to=2017-09-10T12%3A00%3A00'

test = s.get(loc)

Rbinvestor
Question

Hello,

Sorry to piggy back your question but I have been looking for 1 month to find a way to download price data on my Mac. I have come across using Python, which is where I come to your question. Would anybody mind telling me (as simply as possible) how I go about getting the prices from IG through Python, I am guessing it is the code I see above?

I am just looking to download historic HLOC data on a few different equities from the last 12 months.

Any help would be really appreciated! Thank you.

kiann00
Hi Rbinvestor, I had the same

Hi Rbinvestor, I had the same problem as the longest time.

Use the exact code I have above. It should work. You need to fill in your own API key, username and password.

Hope that helps!

Rbinvestor
Hello,

Hello,

Thank you so much! I have 2 questions if you don't mind, I have indented the code in question and put a # with my question. This first is regarding the 'Identifier' and 'password' the second is how to get it to look for a stock like Apple rather than EURUSD.

--------
mport requests
import json
import pandas as pd
url = 'https://demo-api.ig.com/gateway/deal'
s = requests.Session()
s.headers = { 'Content-Type' : 'application/json; charset=UTF-8', 'Accept' : 'application/json; charset=UTF-8', 'VERSION' : '2', 'X-IG-API-KEY' : 'XXXX'
}

data = { 'identifier' : 'username','password' : 'password'}
# Do I keep 'identifier' or do I fill this in with something? Also what is in the second password box?

r = s.post(url + '/session', json=data)
s.headers.update({'X-SECURITY-TOKEN': r.headers['X-SECURITY-TOKEN'],'CST': r.headers['CST']})
s.headers.update({'Version': '3'})
r = s.get(url + '/accounts/')

loc = 'https://demo-api.ig.com/gateway/deal/prices/CS.D.EURUSD.CFD.IP? _
_resolution=DAY&from=2017-09-05T12%3A00%3A00&to=2017-09-10T12%3A00%3A00'
test = s.get(loc)

# Am I correct this is looking for the EURUSD from 2017-09-05 to 2017-09-10 on a Daily resolution? If I wanted say Apple stock what would I type?

kiann00
The syntax provided by IG is

The syntax provided by IG is quite limited and document quite sparse.
THe format occurs in the form of = 'https://demo-api.ig.com/gateway/deal/prices/' + + '?resolution' + + "=" + …

you get the idea.... unfortunately, for Apple you need to try to dig out from the REST API companion or load it from IG the equivalent EPIC code....

loc = 'https://demo-api.ig.com/gateway/deal/prices/CS.D.EURUSD.CFD.IP? _
_resolution=DAY&from=2017-09-05T12%3A00%3A00&to=2017-09-10T12%3A00%3A00'

Rbinvestor
Hi,

Hi,

Thanks for your help.

Sorry I am not very au fait with python.

Do I use the API where you have written ‘identifier’ then user name and password?

Then for the code you said I need to get the Apple equivalent EPIC, is an EPIC the identifier for the market prices I want I.e EURUSD or Apple or AMD etc.

ExhaleTrading
Yes, EPIC is the IG version

Yes, EPIC is the IG version of symbol a.k.a. name of the security in the system.

ExhaleTrading
I just remembered one thing

I just remembered one thing that might be important for you.

You have to check if it is possible to retrieve price data for stocks via IG's API. I know that it is not possible for ETFs and also for the VIX futures. You can trade those via the API but not request price data. I would not be surprised, if it was the same for stocks.

Rbinvestor
Hello,

Hello,

Thanks, I have emailed IG, will let you know what they come back with.

Assuming it is possible how would I find the Epic for stocks. I am not up to speed on the API comparison so if you can keep it simple as possible.

Thank you so much for your help.

ExhaleTrading
This is a little bit strange

This is a little bit strange but you will not find it on any website.

If you start the IG platform and open a market in a detached window you can see the epic in the URL. I'd love to attach a picture but somehow one can't attach files in this forum.

Rbinvestor
Hello,

Hello,

Just posting on the community a follow up to help other people. Having spent, far too much, time on this, I can confirm IG do not let you get their data for equities through any API. This is because they do not own the data, Reuters do. You can get the IG data for currencies however. FYI Reuters basic HLOC, Volume etc. data is around £350 per month having spoken to them if it helps. 

However in Apple Numbers you can use a formal to search a stock price which is pretty accurate. Thanks all for your help :)

ExhaleTrading
Just to elaborate this, you

Just to elaborate this, you can download history for currencies, stock index CFDs and futures. The only data you can not download are prices for ETFs, stocks and the VIX futures.

Anyway I'm glad you found a workaround for your problem.

Thanks for figuring out that IG uses Reuters data feed. This was interesting.

Log in or register to post comments