By cryptomaga · 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. 69% 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 accounts provided by IG Markets Ltd, spread betting provided by IG Index Ltd and share dealing and stocks and shares ISA accounts provided by IG Trading and Investments Ltd. IG is a trading name of IG Markets Ltd (a company registered in England and Wales under number 04008957), IG Index Ltd (a company registered in England and Wales under number 01190902) and IG Trading and Investments Ltd (a company registered in England and Wales under number 11628764). Registered address at Cannon Bridge House, 25 Dowgate Hill, London EC4R 2YA. IG Markets Ltd (Register number 195355), IG Index Ltd (Register number 114059) and IG Trading and Investments Ltd (Register number 944492) 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
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
Archived
This topic is now archived and is closed to further replies.