By XTRAVAGANZA · Posted
By XTRAVAGANZA · Posted
IG | Sitemap | Terms and agreements | Privacy | How to fund | Cookies | About IG
Spread bets and CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. 68% of retail investor accounts lose money when trading spread bets and CFDs with this provider. You should consider whether you understand how spread bets and CFDs work, and whether you can afford to take the high risk of losing your money. Professional clients can lose more than they deposit. All trading involves risk.
The value of shares, ETFs and ETCs bought through a share dealing account, a stocks and shares ISA or a SIPP can fall as well as rise, which could mean getting back less than you originally put in. Past performance is no guarantee of future results.
CFD, share dealing and stocks and shares ISA accounts provided by IG Markets Ltd, spread betting provided by IG Index Ltd. IG is a trading name of IG Markets Ltd (a company registered in England and Wales under number 04008957) and IG Index Ltd (a company registered in England and Wales under number 01190902). Registered address at Cannon Bridge House, 25 Dowgate Hill, London EC4R 2YA. Both IG Markets Ltd (Register number 195355) and IG Index Ltd (Register number 114059) are authorised and regulated by the Financial Conduct Authority.
The information on this site is not directed at residents of the United States, Belgium or any particular country outside the UK and is not intended for distribution to, or use by, any person in any country or jurisdiction where such distribution or use would be contrary to local law or regulation.
Question
stonkyboy
Hello! I've been lurking in the community for a little while and recieved a lot of tips & tricks so thanks to all who have helped me get this far through my project.
I've built my 1st trading bot in python and i'm trying to test it using the demo API. I've had no issues using most of the endpoints (fetch markets, get open orders, get open positions, etc) but for some reason most of my 'create new position' requests are failing witout explaination. The strange thing is when I try to open positions using the same parameters and same demo credentials on the API companion it succeedes and gives me a deal reference.
About 20% of my open position requests either succeed or return a sensible error like 'MINIMUM_SIZE_ORDER' or 'MARKET_CLOSED_WITH_EDITS' so I think my headers are fine.
I'm wondering if anyone has had a similar issue when trying to work with the demo api?
Here's what i'm recieving after I try to open a position:
Here's the python code that i'm using to open a position:
Connect to API:
data = {"identifier": DEMO_USERNAME, "password": DEMO_PASSWORD, "encryptedPassword":None}
headers = {'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json; charset=utf-8',
'X-IG-API-KEY': API_KEY,
'Version': '2'
}
r = requests.post(API_ENDPOINT, data=json.dumps(data), headers=headers)
headers_json = dict(r.headers)
CST_token = headers_json['CST']
x_sec_token = headers_json["X-SECURITY-TOKEN"]
authenticated_headers = {'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json; charset=utf-8',
'X-IG-API-KEY': API_KEY,
'CST': CST_token,
'X-SECURITY-TOKEN': x_sec_token}
auth_r = requests.get(base_url, headers=authenticated_headers)
d = json.loads(auth_r.text)
base_url = 'https://demo-api.ig.com/gateway/deal/session'
data = {
"accountId": DEMO_ACC_ID,
"defaultAccount": "True"} # Main Demo acc
auth_r = requests.put(
base_url,
data=json.dumps(data),
headers=authenticated_headers)
Open Position:
base_url = 'https://demo-api.ig.com/gateway/deal/positions/otc'
data = {
"epic": "UB.D.INTC.DAILY.IP",
"expiry": "DFB",
"direction": "BUY",
"size": "1.144",
"orderType": "MARKET",
"level": None,
"guaranteedStop": "false",
"stopLevel": None,
"stopDistance": "1748",
"forceOpen": "true",
"limitLevel": None,
"limitDistance": None,
"quoteId": None,
"currencyCode": "USD"
}
r = requests.post(base_url,data=json.dumps(data),headers=authenticated_headers)
Any help in solving this issue would be greatly appreciated!
Cheers.
Link to comment
1 answer to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now