Jump to content
  • 0

Java REST API close postion problem


Guest Sideshow

Question

Guest Sideshow

Hi,

 

I am using the Java REST API, and have created an application using your ig-webapi-java-sample application as the basis of it. I am having trouble with closing an open position, so I hope someone can point out what I am doing wrong please!

 

I have created a working order as per the following code. It creates ok, and then as the price moves, becomes an open position:

 

GetMarketDetailsV2Response marketDetails = restApi.getMarketDetailsV2(authenticationContext.getConversationContext(), getTradeableEpic());

CreateOTCWorkingOrderV2Request createWorkingOrderRequest = new CreateOTCWorkingOrderV2Request();

createWorkingOrderRequest.setEpic(getTradeableEpic());

createWorkingOrderRequest.setExpiry(marketDetails.getInstrument().getExpiry());

createWorkingOrderRequest.setDirection(direction);

List<CurrenciesItem> currencies = marketDetails.getInstrument().getCurrencies();

createWorkingOrderRequest.setCurrencyCode(currencies.size() > 0 ? currencies.get(0).getCode() : "GBP");

createWorkingOrderRequest.setSize(BigDecimal.valueOf(event.getForexPair().getMinimumSize()));

createWorkingOrderRequest.setGuaranteedStop(false);

createWorkingOrderRequest.setForceOpen(true);

createWorkingOrderRequest.setTimeInForce(TimeInForce.GOOD_TILL_CANCELLED);

createWorkingOrderRequest.setGoodTillDate(null);

// createWorkingOrderRequest.setStopLevel(stopLevel);

createWorkingOrderRequest.setStopDistance(null);

createWorkingOrderRequest.setLimitLevel(null);

createWorkingOrderRequest.setLimitDistance(null);

createWorkingOrderRequest.setType(Type.STOP);

createWorkingOrderRequest.setLevel(level);

restApi.createOTCWorkingOrderV2(authenticationContext.getConversationContext(), createWorkingOrderRequest);

 

 

Now when I try to close this open position, it fails. Here is my code:

 

GetMarketDetailsV2Response marketDetails = restApi.getMarketDetailsV2(authenticationContext.getConversationContext(), getTradeableEpic());

CloseOTCPositionV1Request closePositionRequest = new CloseOTCPositionV1Request();

closePositionRequest.setDealId(position.getDealId());

closePositionRequest.setEpic(getTradeableEpic());

closePositionRequest.setExpiry(marketDetails.getInstrument().getExpiry());

if(position.getDirection() == Direction.SELL) {

closePositionRequest.setDirection(com.iggroup.webapi.samples.client.rest.dto.positions.otc.closeOTCPositionV1.Direction.BUY);

}else {

closePositionRequest.setDirection(com.iggroup.webapi.samples.client.rest.dto.positions.otc.closeOTCPositionV1.Direction.SELL);

}

closePositionRequest.setSize(position.getSize());

closePositionRequest.setLevel(null);

closePositionRequest.setOrderType(OrderType.MARKET);

closePositionRequest.setTimeInForce(TimeInForce.EXECUTE_AND_ELIMINATE);

restApi.closeOTCPositionV1(authenticationContext.getConversationContext(), closePositionRequest);

 

 

This fails with

Delete failed: HTTP/1.1 400 Bad Request
java.lang.RuntimeException: Delete failed: HTTP/1.1 400 Bad Request
at com.iggroup.webapi.samples.client.RestAPI.closeOTCPositionV1(RestAPI.java:263)
at com.me.myapp.closePosition(Blah.java:93)

...

at com.me.myapp.closePositions(Foo.java:193)

at com.lightstreamer.ls_client.FullTableManager.processUpdate(FullTableManager.java:161)
at com.lightstreamer.ls_client.FullTableManager.doUpdate(FullTableManager.java:78)
at com.lightstreamer.ls_client.MyServerListener.onUpdate(MyServerListener.java:142)
at com.lightstreamer.ls_client.ServerManager.tableUpdate(ServerManager.java:1229)
at com.lightstreamer.ls_client.ServerManager.waitEvent(ServerManager.java:1298)
at com.lightstreamer.ls_client.ServerManager$SessionActivityManager.runEvent(ServerManager.java:238)
at com.lightstreamer.ls_client.ServerManager$SessionActivityManager.runSession(ServerManager.java:338)
at com.lightstreamer.ls_client.ServerManager$SessionActivityManager.access$300(ServerManager.java:194)
at com.lightstreamer.ls_client.ServerManager$SessionActivityManager$1.run(ServerManager.java:212)

 

 

Link to comment

1 answer to this question

Recommended Posts

Guest Sideshow

Problem solved, here's the answer:

 

GetMarketDetailsV2Response marketDetails = restApi.getMarketDetailsV2(authenticationContext.getConversationContext(), getTradeableEpic());
CloseOTCPositionV1Request closePositionRequest = new CloseOTCPositionV1Request();
closePositionRequest.setDealId(position.getDealId());
closePositionRequest.setExpiry(marketDetails.getInstrument().getExpiry());
if(position.getDirection() == Direction.SELL) {
closePositionRequest.setDirection(com.iggroup.webapi.samples.client.rest.dto.positions.otc.closeOTCPositionV1.Direction.BUY);
}else {
closePositionRequest.setDirection(com.iggroup.webapi.samples.client.rest.dto.positions.otc.closeOTCPositionV1.Direction.SELL);
}
closePositionRequest.setSize(position.getSize());
closePositionRequest.setOrderType(OrderType.MARKET);
CloseOTCPositionV1Response response = restApi.closeOTCPositionV1(authenticationContext.getConversationContext(), closePositionRequest);
closingDealReference = response.getDealReference();

Link to comment

Archived

This topic is now archived and is closed to further replies.

  • image.png

  • Posts

    • Do you mean daily check-in the app could earn me 1 SOL? please how do i participate?
    • Hey everyone! 🌟 I recently came across the Bitget GoldVerse Community Campaign, and I think it's something you might want to check out, especially if you're into crypto gaming and airdrops. This campaign is exclusively for new Bitget APP users who register and download through a specified link. GoldVerse is a launch platform based on the TON ecosystem, designed to provide a fair, transparent, and secure environment for emerging projects. It's quickly becoming one of the most trusted token issuance platforms on TON, focusing on fairness, transparency, and community involvement. One of the coolest features is the GoldVersebot Mini-app on Telegram. Within just 10 days of its launch, it achieved over 1 million users! The game is super engaging—you can mine GDV coins by tapping in the Social Game, complete tasks, participate in activities, and invite friends to earn $GDV If you're looking for a fun way to earn some crypto and be part of a growing community, this might be worth your time. Plus, with the Bitget event, there are even more opportunities to earn rewards. Happy mining! ⛏️💰
×
×
  • Create New...
us