Jump to content
  • 0

Unknown error when placing order on the demo API



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: 

"Order Failed: {'date': '2020-06-11T14:00:47.022', 'status': None, 'reason': 'UNKNOWN', 'dealStatus': 'REJECTED', 'epic': 'SB.D.EBSUS.DAILY.IP', 'expiry': None, 'dealReference': 'V5NMJXCZUFF44TE', 'dealId': 'DIAAAADW5TZM8BA', 'affectedDeals': [], 'level': None, 'size': None, 'direction': 'BUY', 'stopLevel': None, 'limitLevel': None, 'stopDistance': None, 'limitDistance': None, 'guaranteedStop': False, 'trailingStop': False, 'profit': None, 'profitCurrency': None}"

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(

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! 


  • Great! 1
Link to comment

1 answer to this question

Recommended Posts

  • 0

The problem was that I was specifying a size with three decimal places... but the limit is two. Think this limit holds with every number you can provide to the API. Would be good if the tech team could explain that in the API response. 

  • Like 2
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • General Statistics

    • Total Topics
    • Total Posts
    • Total Members
    • Most Online
      10/06/21 10:53

    Newest Member
    Joined 29/01/23 03:20
  • Posts

    • Capital, win loss ratio. If you have a trading edge and you can consistently win 50% of your trades, so your winning 5 trades out of 10. So if your risking 1% of your capital per trade, out of your 10 trades 5 would be losers, so that’s 5% loss and realistically out of the 5 winning trades, some would make small profits, some break even and 1, 2 or 3 could run nicely IF you can let your profits run, basically your making money out of 2 trades out of the 10 trades (80/20 Rule Pareto principle) So a $20,000 acct risking 1% is $200 per trade, this will keep the trader with his trade risk based on being able to win 50% of his trades. A long term trend trader can win with 30% wining trade. Basically you need to know your numbers. Rgds Pete
    • Investing in stocks can be a great way to grow your wealth over time. However, there are different approaches that investors can take when choosing which stocks to buy. Two of the most popular approaches are growth investing and value investing. Growth Investing Growth investing is an investment strategy that focuses on buying stocks of companies that are expected to grow at a faster rate than the overall market. These companies are often in industries that are growing quickly, such as technology or healthcare. Investors who use this approach believe that these companies will be able to generate higher profits in the future, which will lead to higher stock prices. One of the main advantages of growth investing is that it can potentially provide higher returns than the overall market. However, it is also riskier than other investment strategies, as these companies often have higher valuations and more volatile stock prices. Value Investing Value investing is an investment strategy that focuses on buying stocks of companies that are undervalued by the market. These companies may be in industries that are out of favour or have recently experienced challenges, but they have strong fundamentals and a history of profitability. Investors who use this approach believe that these companies are undervalued and that their true value will be recognized in the future, leading to higher stock prices. One of the main advantages of value investing is that it can potentially provide lower risk than growth investing. However, it may also provide lower returns in the long run, as these companies may not have the same growth potential as companies in the growth investing category. Comparing Growth and Value Investing Growth and value investing are two different approaches to stock investing, each with its own advantages and disadvantages. Growth investing can potentially provide higher returns but is riskier, while value investing can provide lower risk but potentially lower returns. An investor may choose one approach or a combination of both. A portfolio that contains a mix of growth and value stocks can provide a balance of potential returns and risk. Conclusion Both growth investing and value investing can be effective ways to invest in stocks. The key is to understand the potential risks and rewards of each approach and to choose the one that aligns with your investment goals and risk tolerance. Analyst Peter Mathers TradingLounge™ 
    • I am a beginner, and I must say, there are a lot of rules to the trading game that one must abide by if they want to be successful.   Here, the writer mentions several basic rules for day vs swing trading.  However, I find that often times, the reasoning for these rules is not as  obvious for a beginner as it may be for an expert.   The 'why' factor if I may. For example, why must you have a large capital to trade with as a day trader? Because your positions must be large so that a small change in price will be augmented and turned into a large profit. Also, with such high risk, the margin will be specially high, given the trader is taking up large positions at a time.  Without a large amount of capital, positions may be forced to close due to funds being below margin requirements.  When this happens, you can expect to lose tons of cash, fast.  I learned the hard way. All the best, David Franco      
  • Create New...