Cancelling orders with Cancel() can only be accomplished with "PENDING" orders. If an order is "FILLED" or rejected, it can't be cancelled.
When PlaceOrder() is called to execute a new order, it returns a valid "OrderID" (if the order was not rejected). This OrderID should be stored within your code for future reference.
NewEntryOrder = mentts.call("PlaceOrder","GAIN","BUY","MARKET",tempsymbol,(GAINMultiplier*DefEntryContracts),0,"GTC");
The variable "NewEntryOrder" contains the return value from PlaceOrder() . To determine if the order was properly placed, we should check to see if NewEntryOrder is greater than zero.
if (NewEntryOrder > 0) {
// new order placed successfully.
}
At this point, MTS has received a request for a new order and MTS returned a valid order ID. Thus, we could cancel this order with a call to Cancel() . But first, we need to make sure the status is still PENDING and not FILLED.
if (Time_To_Cancel_Entry) {
var OrderStatus = mentts.call("GetOrderStatus","GAIN",NewEntryOrder);
if (OrderStatus == "PENDING") {
mentts.call("Cancel","GAIN",NewEntryOrder);
}
}
Now, running an effective auto-execution trading system means we need to verify NewEntryOrder was actually cancelled