My EA only seems to place buy orders. I need a helping hand so it can place both buy and sell orders!

 
//--- input parameters
extern double   Lots=20;

double MAX_RISK = 0.005; //The maximum to be risked of the account.
int MAX_ORDERS = 3;
int StopLoss; //Defining a stop loss.
int MagicNumber = 99;
int ticket;

double Ma1,Ma2;


void start()
{

//Write a program which calculates a StopLoss value at each tick.

StopLoss = AccountBalance()*MAX_RISK; //This ensures that only a certain percentage is risked of the account at each new tick.
Print("The StopLoss is now: #",StopLoss);
Print("The account balance is now: #",AccountBalance());

//The below section is with regards to opening a buy or sell trade.

if(OrdersTotal()<MAX_ORDERS) //Defines the initial parameter which sets the total number of orders.
 {
  Ma1=iMA(Symbol(),PERIOD_M5,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
  Ma2=iMA(Symbol(),PERIOD_M5,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
  
  if (Ma1<Ma2) //The begining of a buy signal.
   {
    ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,StopLoss,0,"test_4 placed this trade", MagicNumber,0,Green); //Defines the placement of an order.
    if (ticket>0) //This is to check if an order has been successfully placed.
     {
      Print("Order Placed #", ticket);
     }
    else
     {
      Print("Order Placed Failed , error #", GetLastError());
     }
   }
   
   if (Ma1>Ma2) //The begining of a sell signal.
   {
    ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,StopLoss,0,"test_4 placed this trade", MagicNumber,0,Blue); //Defines the placement of an order.
    if (ticket>0) //This is to check if an order has been successfully placed.
     {
      Print("Order Placed #", ticket);
     }
    else
     {
      Print("Order Placed Failed , error #", GetLastError());
     }
   }
 }

//The section below is with regards to closing live orders.

// MAX_ORDERS = OrdersTotal(); Don't use this, never change this.

for(ticket = OrdersTotal() - 1; ticket >= 0; ticket --) //The decrement allows the program to count through all the trades in the loop.
 {
  if(!OrderSelect(ticket,SELECT_BY_POS,MODE_TRADES)) continue;//If the OrderSelect fails advance the loop to the next 'ticket'
  
  if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == OP_BUY) //This part aims to close a buy order.
   {
    Ma1=iMA(Symbol(),PERIOD_M5,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    Ma2=iMA(Symbol(),PERIOD_M5,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    
   
    if(Ma1>Ma2)
     {
      if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Red)) //Trying to close the order.
       Print("OrderClose failed, order number:",ticket,"Error:",GetLastError()); //If OrderClose failed.
     }
    }
   
  if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == OP_SELL) //This part aims to close a sell order.
   {
    Ma1=iMA(Symbol(),PERIOD_M5,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    Ma2=iMA(Symbol(),PERIOD_M5,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    
    if(Ma1<Ma2)
     {
      if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Red)) //Trying to close the order.
       Print("OrderClose failed, order number:",ticket,"Error:",GetLastError()); //If OrderClose failed.
     }
   }
 } 

Hello,

As the title states I need my EA to place both buy and sell orders. However, it seems to only place by orders. Any help would be greatly appreciated. Here is my EA:

 
  1. Don't double post
  2. StopLoss = AccountBalance()*MAX_RISK; //This ensures that only a certain percentage is risked of the account at each new tick.
    
    SL is a price not a dollar amount. Reread risk and solve the equation.
 

Why bother with

Print("Order Placed Failed , error #", GetLastError());

 When you don't even check the log?

It is quite obvious that you are getting the error "Invalid stops" because your calculation for the stoploss is nonsense. 

 
So I changed the StopLoss value to a fixed constant. Why is it that this EA only buys and not sell?
 
BartWinter:
So I changed the StopLoss value to a fixed constant. Why is it that this EA only buys and not sell?
StopLoss = AccountBalance()*MAX_RISK;
double MAX_RISK = 0.005;

What does this give you?

On a 10,000 balance, it gives you 50.0000

If you were manually opening a buy on EURUSD at 1.1000 would you set the stoploss at 50.0000? 

 
GumRai:

What does this give you?

On a 10,000 balance, it gives you 50.0000

If you were manually opening a buy on EURUSD at 1.1000 would you set the stoploss at 50.0000? 

I understand my error in this. Thanks for pointing it out. My question is ,however, why do you think the code is only placing buy orders and not sell orders?
 
BartWinter:
I understand my error in this. Thanks for pointing it out. My question is ,however, why do you think the code is only placing buy orders and not sell orders?

What GumRai is trying to tell you, and what WHRoeder has already told you, is that the stop-loss value for OrderSend() should be a price, not a cash amount. Your value for the Stoploss variable is currently going to be a value such as 50 (i.e. $50). It needs to be a price such as 1.0498. If you want the stop-loss to equate to $50, or 0.5% of your account, then you need to calculate the equivalent price.

What you are doing will make a buy order fail on most symbols because MT4 is expecting a price for the stop-loss rather than a cash amount; you are giving it a price such as 50; that price is higher than the current ask price (e.g. 1.0596 on EUR/USD); and therefore the buy order is rejected because the stop-loss is not valid.

Your code "works" on sell orders because the value such as 50 is higher than the bid price on most symbols, and therefore the value of 50 is acceptable as a stop-loss - though does not have the meaning you intend, and will be creating a far larger risk than you intend.

Once last attempt at making this clear: when your code "successfully" creates a sell order with an s/l of something like 50, that means that that order will be closed when the symbol's price hits 50, not when the cash loss reaches $50.

 
WHRoeder:
  1. Don't double post
  2. SL is a price not a dollar amount. Reread risk and solve the equation.
jjc:

What GumRai is trying to tell you, and what WHRoeder has already told you, is that the stop-loss value for OrderSend() should be a price, not a cash amount. Your value for the Stoploss variable is currently going to be a value such as 50 (i.e. $50). It needs to be a price such as 1.0498. If you want the stop-loss to equate to $50, or 0.5% of your account, then you need to calculate the equivalent price.

What you are doing will make a buy order fail on most symbols because MT4 is expecting a price for the stop-loss rather than a cash amount; you are giving it a price such as 50; that price is higher than the current ask price (e.g. 1.0596 on EUR/USD); and therefore the buy order is rejected because the stop-loss is not valid.

Your code "works" on sell orders because the value such as 50 is higher than the bid price on most symbols, and therefore the value of 50 is acceptable as a stop-loss - though does not have the meaning you intend, and will be creating a far larger risk than you intend.

Thank you very much for this. It is extremely helpful. May I ask how long have you been coding MQL4? I've only started coding 7 weeks ago in C as part of my degree (which is not computer science) and I've been doing MQL4 for only 3 weeks.
 
BartWinter:
Thank you very much for this. It is extremely helpful. May I ask how long have you been coding MQL4? I've only started coding 7 weeks ago in C as part of my degree (which is not computer science) and I've been doing MQL4 for only 3 weeks.
There's nothing wrong with your code per se; in fact it's unusually clean by the standards of this forum, and avoids common problems such as looping up to OrdersTotal() when closing positions. The issue is your understanding of trading platforms in general, and MT4 in particular, not the code.
 
jjc:
There's nothing wrong with your code per se; in fact it's unusually clean by the standards of this forum, and avoids common problems such as looping up to OrdersTotal() when closing positions. The issue is your understanding of trading platforms in general, and MT4 in particular, not the code.
Thanks a lot for your help jjc it works now. :) it places orders when it should
 
jjc:
There's nothing wrong with your code per se; in fact it's unusually clean by the standards of this forum, and avoids common problems such as looping up to OrdersTotal() when closing positions. The issue is your understanding of trading platforms in general, and MT4 in particular, not the code.
jjc upon testing the code it works for opening trades but it doesn't close them when the MAs cross over again. I'm sorry to take more of your time but do you think you could have a look at the code to see if the problem can be deduced.
Reason: