Create a Close Order
A close order closes a position at the specified market rate in case such rate is available on the market.
Please note that close orders must be permitted for the account in order to be used.
Parameter name |
Datatype |
Description |
||||||
COMMAND |
String |
The command. Must be |
||||||
ORDER_TYPE |
String |
The type of the order. Must be |
||||||
OFFER_ID |
String |
The identifier of the instrument the order should be placed for.
The value must be obtained from the Offers table, the |
||||||
ACCOUNT_ID |
String |
The identifier of the account the order should be placed for.
The value must be obtained from the Accounts table, the Please note that the account identifier may not necessarily be equal to the account name which is shown in the Trading Station application. |
||||||
TRADE_ID |
String |
The identifier of the trade to be closed. The value must be obtained from the Trades table, the Do not fill this value for a net quantity order. |
||||||
BUY_SELL |
String |
The order direction. The value must be |
||||||
AMOUNT |
int |
The amount of the order. In the case of FX instruments, the amount is expressed in the base currency of an instrument. In the case of CFD instruments, the amount is expressed in contracts. Must be divisible by the value of the lot size. The amount must be less than or equal to the size of the position (see the Do not fill this value for a net quantity order. |
||||||
NET_QUANTITY |
String |
The net quantity flag. Use this parameter with the value A close order in the netting mode closes all positions which
exist at the moment of the order filling and which are created for the
order account and order instrument in the direction (see |
||||||
RATE |
double |
The rate at which the order must be filled. |
||||||
TIME_IN_FORCE |
String |
The Time In Force value. Can be The value is optional. Close orders are |
||||||
CLIENT_RATE |
double |
The current price of the order instrument. The value is optional. It is used for logging purposes. |
||||||
CUSTOM_ID |
String |
The custom identifier of the order. This value will be populated into these columns of the trading tables:
The value is optional. |
Create a Close order [hide]
class ParamsFromLoginRules { String sAccountID; String sOfferID; int iBaseAmount; double dPointSize; double dAskRate; double dBidRate; } private void prepareParamsAndCallMarketCloseOrder(String sTradeID, int iAmount, String sBuySell, String sNetQty) { ParamsFromLoginRules params = new ParamsFromLoginRules(); boolean bParamsOK = prepareParamsFromLoginRules(params); if (!bParamsOK) return; double dRate; if (sBuySell.equals(Constants.Buy)) dRate = params.dAskRate; else dRate = params.dBidRate; createMarketCloseOrder(params.sOfferID, params.sAccountID, sTradeID, dRate, iAmount, sBuySell, sNetQty); } private void createMarketCloseOrder(String sOfferID, String sAccountID, String sTradeID, double dRate, int iAmount, String sBuySell, String sNetQty) { O2GRequestFactory factory = mSession.getRequestFactory(); if (factory == null) { return; } O2GValueMap valuemap = factory.createValueMap(); valuemap.setString(O2GRequestParamsEnum.COMMAND, Constants.Commands.CreateOrder); valuemap.setString(O2GRequestParamsEnum.ORDER_TYPE, Constants.Orders.MarketClose); valuemap.setDouble(O2GRequestParamsEnum.RATE, dRate); valuemap.setString(O2GRequestParamsEnum.ACCOUNT_ID, sAccountID); // The identifier of the account the order should be placed for. valuemap.setString(O2GRequestParamsEnum.OFFER_ID, sOfferID); // The identifier of the instrument the order should be placed for. if (sNetQty.equalsIgnoreCase("Y")) { valuemap.setString(O2GRequestParamsEnum.NET_QUANTITY, "Y"); } else { valuemap.setString(O2GRequestParamsEnum.TRADE_ID, sTradeID); // The identifier of the trade to be closed. valuemap.setInt(O2GRequestParamsEnum.AMOUNT, iAmount); // The quantity of the instrument to be bought or sold. Must be <= to the size of the position ("Trades" table, Lot column) } valuemap.setString(O2GRequestParamsEnum.BUY_SELL, sBuySell); // The order direction: Constants.Buy for Buy, Constants.Sell for Sell. Must be opposite to the direction of the trade. O2GRequest request = factory.createOrderRequest(valuemap); mSession.sendRequest(request); } private boolean prepareParamsFromLoginRules(ParamsFromLoginRules params) { boolean bOK = false; O2GResponseReaderFactory factory = mSession.getResponseReaderFactory(); if (factory == null) { return false; } O2GLoginRules loginRules = mSession.getLoginRules(); // get first account from login O2GResponse accountResponse = loginRules.getTableRefreshResponse(O2GTable.ACCOUNTS); O2GAccountsTableResponseReader accountsReader = factory.createAccountsTableReader(accountResponse); O2GAccountRow account = accountsReader.getRow(0); // get account id params.sAccountID = account.getAccountID(); // get base amount params.iBaseAmount = account.getBaseUnitSize(); params.sOfferID = ""; params.dAskRate = 0; params.dBidRate = 0; params.dPointSize = 0; // Get offers for EUR/USD O2GResponse offersResponse = loginRules.getTableRefreshResponse(O2GTable.OFFERS); O2GOffersTableResponseReader offersReader = factory.createOffersTableReader(offersResponse); for (int i = 0; i < offersReader.size(); i++) { O2GOfferRow offer = offersReader.getRow(i); if (offer.getInstrument().equals("EUR/USD")) { params.sOfferID = offer.getOfferID(); params.dAskRate = offer.getAsk(); params.dBidRate = offer.getBid(); params.dPointSize = offer.getPointSize(); bOK = true; break; } } return bOK; }