Upon hard investigation, I found that the problem was defining orderChoice = "OP_SELL" with quotes. Removing the quotes fixes the problem.
Posting the solution without the deficient code doesn't help later readers to gain any knowledge from this topic. Please post at least a code snippet to illustrate your issue. You probably had orderChoice declared as integer?
Very well. Here is the complete EA that I wrote to debug that problem specifically:
string AskBid() { if (orderChoice == "OP_BUY") {return "Ask";} else {return "Bid";} } int orderTicket; double orderSize = 0.01; string orderChoice = "OP_SELL"; void init() { orderTicket = OrderSend (Symbol(), orderChoice, orderSize, AskBid(), 2, 0, 0, NULL, 0, 0, CLR_NONE); Print ("ORDER OrderSend (", Symbol(), " , ", orderChoice, " , ", orderSize, " , ", AskBid(), " , ", "2, ", "0, ", "0, ", "NULL, "); if(OrderSelect(orderTicket, SELECT_BY_TICKET, MODE_TRADES) == true) { Print ("ticket number is ", orderTicket); } else {Print("ERROR: ", GetLastError());} ExpertRemove(); }
Such an extremely basic piece of code that STILL caused the problem I was having proved that the error was not in the larger, more complex code.
I eventually found the problem was in this line:
string orderChoice = "OP_SELL";
This is the line that works:
string orderChoice = OP_SELL;
HTH.
This is the line that works:
string orderChoice = OP_SELL;
orderTicket = OrderSend (Symbol(), orderChoice, orderSize, AskBid(), 2, 0, 0, NULL, 0, 0, CLR_NONE);
HTH.
This will cause a conversion from number (int) to string, and back when the string is passed to OrderSend(). You'll see the warnings with #property strict, did you turn it on?
Just use an int variable like so:
int orderChoice = OP_SELL;
if(orderChoice == OP_SELL) ...
No, I don't have #property strict on.
The code works anyway, but I will certainly check up on the flaw you pointed out.
Thank you for your input.
Welcome. Always use strict when dealing with MQL4 code. MQL5 has it 'on' by default.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
My EA sends orders with this line which is present only once in the entire code, so I am sure the error can't be elsewhere:
Debugging with Print right above that line, I get this string:
OrderSend (EURUSD, OP_SELL, 0.01, Bid, 2, 0, 0, NULL, 0, 0, CLR_NONE);
But all orders are placed as BUY for some reason. The code never sells. What am I doing wrong?
TIA