Jump to content
  • 0

API: How to delete a position, Java


KML

Question

Dear all,

I'm having trouble deleting a position using the API. As I understand https://labs.ig.com/rest-trading-api-reference/service-detail?id=678, I'm supposed to send a DELETE request with a body. But isn't a DELETE-request supposed to be without a body?

The line

     HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
           .headers("Content-Type", "application/json; charset=UTF-8", "Accept", "application/json; charset=UTF-8",
            "Version", "2", "IG-ACCOUNT-ID", CAI,
            "X-IG-API-KEY", APIKEY, "CST", CST)
            .method("DELETE", HttpRequest.BodyPublishers.ofString(body))  
            .build();

returns a 404 not found error, but if I change DELETE to POST as in 

     HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
           .headers("Content-Type", "application/json; charset=UTF-8", "Accept", "application/json; charset=UTF-8",
            "Version", "2", "IG-ACCOUNT-ID", CAI,
            "X-IG-API-KEY", APIKEY, "CST", CST)
            .method("POST", HttpRequest.BodyPublishers.ofString(body))  
            .build();

I get an expected error {"errorCode":"invalid.request.format.line-1.column-3"}. Does this mean that I'm supposed to use the POST method anyway? How does it then know that I',m deleting the position and not creating a new one?

I could of course delete a long position by creating a short position and vice versa, but that looks a bit messier.

Thanks for any help or clarifications.

Link to comment

6 answers to this question

Recommended Posts

  • 0
1 hour ago, KML said:

Dear all,

I'm having trouble deleting a position using the API. As I understand https://labs.ig.com/rest-trading-api-reference/service-detail?id=678, I'm supposed to send a DELETE request with a body. But isn't a DELETE-request supposed to be without a body?

The line

     HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
           .headers("Content-Type", "application/json; charset=UTF-8", "Accept", "application/json; charset=UTF-8",
            "Version", "2", "IG-ACCOUNT-ID", CAI,
            "X-IG-API-KEY", APIKEY, "CST", CST)
            .method("DELETE", HttpRequest.BodyPublishers.ofString(body))  
            .build();

returns a 404 not found error, but if I change DELETE to POST as in 

     HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
           .headers("Content-Type", "application/json; charset=UTF-8", "Accept", "application/json; charset=UTF-8",
            "Version", "2", "IG-ACCOUNT-ID", CAI,
            "X-IG-API-KEY", APIKEY, "CST", CST)
            .method("POST", HttpRequest.BodyPublishers.ofString(body))  
            .build();

I get an expected error {"errorCode":"invalid.request.format.line-1.column-3"}. Does this mean that I'm supposed to use the POST method anyway? How does it then know that I',m deleting the position and not creating a new one?

I could of course delete a long position by creating a short position and vice versa, but that looks a bit messier.

Thanks for any help or clarifications.

Dear @KML,

Thank you for your post, please note that many members on our community can assist with complex API-related queries. You can check out posts and replies by @andysinclair and @bug-or-feature on similar topics.

Thanks,

KoketsoIG

Please rate us on Trustpilot: IG Trustpilot 

Link to comment
  • 0
27 minutes ago, bug-or-feature said:

The way you do it is to create a POST, and a header named "_method" with value "DELETE"

I was just about to reply with this.

I have it working with the following in the body: dealId, size, direction and orderType

  • Like 1
  • Thanks 1
Link to comment
  • 0

Thanks for all your replies! Highly appreciated! 🙂

However, I'm still not able to cross the finish line. If I proved the following headers:
{_method=[DELETE], Accept=[application/json; charset=UTF-8], Content-Type=[application/json; charset=UTF-8], CST=[removed], IG-ACCOUNT-ID=[removed], Version=[2], X-IG-API-KEY=[removed]}

I get the 404 File not found error.

However, when I use the following headers:
{Accept=[application/json; charset=UTF-8], Content-Type=[application/json; charset=UTF-8], CST=[removed], IG-ACCOUNT-ID=[removed], Version=[2], X-IG-API-KEY=[removed]}

I get {"errorCode":"validation.null-not-allowed.request.expiry"} which is expected as I don't provide sufficient information to create a new position. (I guess that is what IG thinks I'm trying to do when I don't add the _method= DELETE?

Any ide what I'm missing/misunderstanding?

I still have the option to delete a position by creating a new with a opposite buy/sell attribute, but it would feel more satisfying making this work. 🙂

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
  • image.png

  • Posts

    • ASX: NATIONAL AUSTRALIA BANK LIMITED - NAB   Elliott Elliott Wave Technical Analysis TradingLounge (1D Chart)   Greetings, Our Elliott Wave analysis today updates the Australian Stock Exchange (ASX) with NATIONAL AUSTRALIA BANK LIMITED - NAB . We determine that NAB will continue to push higher with wave iii-grey of wave (iii)-orange. We updated our Trade Recommendation (Long Trade with NAB) yesterday. NAB is opening up quite good profits in today's trading session.   ASX: NATIONAL AUSTRALIA BANK LIMITED - NAB   Elliott Wave Technical Analysis   ASX: NATIONAL AUSTRALIA BANK LIMITED - NAB   1D Chart (Semilog Scale) Analysis Function: Major trend (Minute degree, navy) Mode: Motive Structure: Impulse Position: Wave iii-grey of Wave (iii)-orange of Wave ((v))-navy Details: The short-term outlook suggests wave (iii)-orange is unfolding to push higher.   Invalidation point: 34.48   ASX: NATIONAL AUSTRALIA BANK LIMITED - NAB   Elliott Wave Technical Analysis TradingLounge (4-Hour Chart) ASX: NATIONAL AUSTRALIA BANK LIMITED - NAB   Elliott Wave Technical Analysis ASX: NATIONAL AUSTRALIA BANK LIMITED - NAB   4-Hour Chart Analysis Function: Major trend (Minuette degree, orange) Mode: Motive Structure: Impulse Position: Wave ((3))-navy of Wave iii-grey of Wave (iii)-orange Details: The short-term outlook shows that wave (iii)-orange is opening to push higher, it is subdividing into waves i,ii,iii-grey and waves ((1)),((2)),( (3))-navy. We updated our Trade Recommendation (Long Trade with NAB) yesterday. NAB is opening up quite good profits in today's trading session. Invalidation point: 34.83   Conclusion:   Our analysis, forecast of contextual trends, and short-term outlook for ASX: NATIONAL AUSTRALIA BANK LIMITED - NAB   aim to provide readers with insights into the current market trends and how to capitalize on them effectively. We offer specific price points that act as validation or invalidation signals for our wave count, enhancing the confidence in our perspective. By combining these factors, we strive to offer readers the most objective and professional perspective on market trends.   Technical Analyst: Hua (Shane) Cuong, CEWA-M (Master’s Designation). Source : Tradinglounge.com get trial here!  
    • Elliott Wave Analysis TradingLounge Daily Chart, ChainLink/ U.S. dollar(LINKUSD) LINKUSD Elliott Wave Technical Analysis Function: Counter Trend Mode: Corrective Structure: Double Corrective Position: Wave Y Direction Next higher Degrees: Wave ((II)) of Impulse Wave Cancel invalid Level: 8.498 Details: Wave (II) is equal to 61.8% of Wave (I) at 8.86 Level ChianLink/ U.S. dollar(LINKUSD)Trading Strategy: ChainLink overall is still in the period of correction. In the position of wave (II) with a double corrective pattern, the view is therefore for a short-term pullback before rising again in wave (III), waiting for the correction to complete to rejoin the uptrend. ChainLink/ U.S. dollar(LINKUSD)Technical Indicators: The price is below the MA200 indicating a downtrend, The Wave Oscillator is a Bearish Momentum.   Elliott Wave Analysis TradingLounge H4 Chart, ChainLink/ U.S. dollar(LINKUSD) LINKUSD Elliott Wave Technical Analysis Function: Counter Trend Mode: Corrective Structure: Double Corrective Position: Wave Y Direction Next higher Degrees: Wave ((II)) of Impulse Wave Cancel invalid Level: 8.498 Details: Wave (II) is equal to 61.8% of Wave (I) at 8.86 Level ChainLink/ U.S. dollar(LINKUSD)Trading Strategy: Chainlink overall is still in the period of correction. In the position of wave (II) with a double corrective pattern, the view is therefore for a short-term pullback before rising again in wave (III), waiting for the correction to complete to rejoin the uptrend. Chainlink/ U.S. dollar(LINKUSD)Technical Indicators: The price is below the MA200 indicating a downtrend, The Wave Oscillator is a Bearish Momentum.   Technical Analyst : Kittiampon Somboonsod Source : Tradinglounge.com get trial here!
    • All Those Token is FAKE The Moment The Token is Live For TRADE On The Exchanges The Price Goes Up Then You Buy At The Top Thinking The PRICE WILL Continue To Go Up And Then What Happen is immediately After You Hit The GREEN Button To Buy The PRICE Starts  GOING DOWN AND Never Back Up Again And You Loss You're Money Thanks What's Fake Token is .
×
×
  • Create New...
us