[SOLVED] My EA buys when it should sell

 

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:


orderTicket = OrderSend (Symbol(), orderChoice, orderSize, orderPrice, 2, 0, 0, NULL, 0, 0, CLR_NONE);


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

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
  • www.mql5.com
The CLR_NONE constant is used to outline the absence of color, it means that the graphical object or graphical series of an indicator will not be plotted. This constant was not included into the Web-color constants list, but it can be applied everywhere where the color arguments are required. The EMPTY_VALUE constant usually corresponds...
 
whoowl: What am I doing wrong?

Not posting all relevant code, like your debug print before and your error check after.

 
Upon hard investigation, I found that the problem was defining orderChoice = "OP_SELL" with quotes. Removing the quotes fixes the problem.
 
whoowl:
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?
 
lippmaje:
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.

 
whoowl:

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.

 
whoowl:

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.

Reason: