int sendOrder(string symbolName, int cmd, double volume, double price) { double stoploss; if (cmd == OP_BUY) { stoploss = price - stoplossRange; } else { stoploss = price + stoplossRange; } errorMessage = ""; int ticket = -1; int delay = 0; for (int counter = 0; counter < 4 && ticket < 0; counter++) { ticket = OrderSend(symbolName, cmd, volume, NormalizeDouble(price, Digits), ordersSlippage, stoploss, 0, errorMessage, 255, 0, Green); if (ticket < 0) { int error = GetLastError(); if (error == ERR_NO_ERROR) { errorMessage = errorMessage + symbolName + "OrderSend returned " + ticket + " but with no error. Checking manually"; ticket = getTicketBySymbol(symbolName); if (ticket < 0) { errorMessage = errorMessage + symbolName + " twice"; Sleep(5000); ticket = getTicketBySymbol(symbolName); } break; } errorMessage = errorMessage + symbolName + " failed (" + ErrorDescription(error) + "). Updating " + symbolName + " position from " + price; Sleep(delay); delay += 100; RefreshRates(); if (cmd == OP_BUY) { price = MarketInfo(symbolName, MODE_ASK); } else { price = MarketInfo(symbolName, MODE_BID); } errorMessage = errorMessage + " to " + price + ". "; } } Print("sendOrder for " + symbolName + " ticket:" + ticket + " errorMessage: " + errorMessage); return (ticket); }