Hi iadip,
there are several ways to close orders. If you have to close them outside of onTick(..), you have to call the closing in it's won thread.
Here's an example:
/**
* closes all created/opened/filled orders af an instrument.
* the closings are processed in their own threads, so this function may be used outside of onTick.
* if an error occurs during a close, the procedure continues with the next order
* @param _iInstrument
* @return number of orders closed
*/
private int nCloseOrders(Instrument _iInstrument)
{
int nClosedOrders = 0;
for (final IOrder order : engine.getOrders(_iInstrument))
{
if (order.getState() == IOrder.State.CREATED ||
order.getState() == IOrder.State.OPENED ||
order.getState() == IOrder.State.FILLED)
{
//execute the close statement in it's own thread...
Future<Integer> nFutureCloseStatus = context.executeTask
(
new Callable<Integer>()
{
public Integer call()
{
int nStatus = 0;
try
{
order.close();
return 1;
}
catch (JFException e)
{
console.getOut().println"OnStop.CloseAllOpenPositions: Error occurred during closing order " + order.getId() + " : " + e.getMessage());
nStatus = -1;
}
return nStatus;
}
}
);
nClosedOrders++;
int nCloseStatus = nFutureCloseStatus.get(); //wait for the result of the close...
if ((int)nCloseStatus == 1)
console.getOut().println(">>> CloseAllPositions: order" + order.getLabel() + " successfully closed.");
else
console.getOut().println(">>> CloseAllPositions: order" + order.getLabel() + " could not get closed successfully.");
}
}
return nClosedOrders;
}
Best, RR.