executing the sell order

 
void CheckForSHORTTrade()
{
   if((calcShort1()||calcShort2()) && calcShort3() && calcShort4() && calcShort5())
      {
      OrderEntry(0);
      }
}

void OrderEntry(int direction)

{
 if(direction==0)
    Alert ("SHORT");
           OrderSend(
            Symbol(),
            OP_SELL,
            GetSNumcontracts(),
            Bid,
            5,
            GetStopLossPriceShort(),
            GetTakeProfitPriceShort(),
            NULL,
            0,
            0,
            Green);
}
my code alerts "SHORT" but doesn't execute the sell order, why would this be ?
 
George Johnson: my code alerts "SHORT" but doesn't execute the sell order, why would this be ?
  1. The code you supplied is not sufficient for us to analyse the problem. It is too dependent on other functions for us the know the outcome.
  2. You are not checking the return value of the OrderSend() function to see if it executed or not. The compiler should have issued a warning. Always pay attention and fix warnings. Don't ignore them.
  3. You are not checking for Error codes. After checking the return value of OrderSend, if it fails, then you should check the last error to find out why.
  4. Also check the log files. So please also supply the output of the log files that will show the errors reported for the OrderSend()
 
Also, as a suggestion, irrespective of the above problems, don't use "0" for Magic Number as that is reserved for manual trades. Use a unique and specific magic number for your EA so as not to clash with manual trades nor with other EAs.
 

One more note! Always style your code properly or use the MetaEditor styler. It will help you see some problems that you may not be aware of.

For example, currently the OrderSend() is called irrespective of it being a Sell Direction or a Buy direction because your code is actually like this:

void OrderEntry(int direction)
{
   if(direction==0) Alert ("SHORT");
   OrderSend(Symbol(),OP_SELL,GetSNumcontracts(),Bid,5,GetStopLossPriceShort(),GetTakeProfitPriceShort(),NULL,0,0,Green);
}

Notice how due to lack of braces the OrderSend function is always called irrespective of the "direction" parameter.

 
Fernando Carreiro:
Also, as a suggestion, irrespective of the above problems, don't use "0" for Magic Number as that is reserved for manual trades. Use a unique and specific magic number for your EA so as not to clash with manual trades nor with other EAs.

OK thanks I have changed it to 12345

Fernando Carreiro:

One more note! Always style your code properly or use the MetaEditor styler. It will help you see some problems that you may not be aware of.

For example, currently the OrderSend() is called irrespective of it being a Sell Direction or a Buy direction because your code is actually like this:

Notice how due to lack of braces the OrderSend function is always called irrespective of the "direction" parameter.

void OrderEntry(int direction)

{
   if(direction==1)
   int TicketNumber;
      int TicketNumber = OrderSend(Symbol(),OP_BUY,GetLNumcontracts(),Ask,0,GetStopLossPriceLong(),GetTakeProfitPriceLong(),NULL,12345,Green);

      if( TicketNumber > 0 )
       {
         Print("Order placed # ", TicketNumber);
       }
      else
       {
         Print("Order Send failed, error # ", GetLastError() );
       }
         

this got rid of the original warning but instead there is now a warning that says "variable "TicketNumber" not used ??

is this code above better or not ?

 
George Johnson:

OK thanks I have changed it to 12345

this got rid of the original warning but instead there is now a warning that says "variable "TicketNumber" not used ??

is this code above better or not ?

void OrderEntry(int direction)

{
   if(direction==1)
   if( OrderSend(Symbol(),OP_BUY,GetLNumcontracts(),Ask,0,GetStopLossPriceLong(),GetTakeProfitPriceLong(),NULL,12345,Green) < 0 )
   {
   Print("Order Send failed, error # ", GetLastError() );
   }      
   

this seemed to work for order send, how would i fix an order select error or a possible data loss error ??

 
George Johnson: this seemed to work for order send, how would i fix an order select error or a possible data loss error ??

This is my version of the code (untested as I would need the rest of your code to test). See if you understand it and if it will help structure your own code:

#property strict

input int MagicNumber = 12345;

// ... 

void OrderEntry( bool BuyFlag )
{
   double PriceEntry      = BuyFlag ? Ask                      : Bid,
          PriceStopLoss   = BuyFlag ? GetStopLossPriceLong()   : GetStopLossPriceShort(),
          PriceTakeProfit = BuyFlag ? GetTakeProfitPriceLong() : GetTakeProfitPriceShort();
   color  ColourArrow     = BuyFlag ? clrGreen                 : clrRed;
   int    TypeOrder       = BuyFlag ? OP_BUY                   : OP_SELL,
          TicketOrder     = OrderSend( _Symbol, TypeOrder, GetLNumcontracts(), PriceEntry,
                               0, PriceStopLoss, PriceTakeProfit, NULL, MagicNumber, ColourArrow );
   if( TicketOrder < 0 )
      PrintFormat( "OrderSend failed    (Error Code:    %d)", _LastError  );
   else
      PrintFormat( "OrderSend succeeded (Ticket Number: %d)", TicketOrder );
};
 
Fernando Carreiro:

This is my version of the code (untested as I would need the rest of your code to test). See if you understand it and if it will help structure your own code:

Thanks, i will try that out