Please help on: OrderSend error 3

 

Please help on: OrderSend error 3

This is the code:

//+------------------------------------------------------------------+
//|                                                        Ma13.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

extern double TakeProfit = 5000;
extern double StopLoss = 2000;
extern double Lots = 0.05;
extern double TrailingStop = 1000;
extern double RSI_Period = 14;
extern double Mom_Period = 14;
extern double Ma_Period = 21;
extern int Risk = 2;

int New_Bar;
int Time_0;
int expire;
 


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double RSI, Curlev, Prevlev, Range, Ma, Close_1, Markup, Slope;
   int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
     Lots = AccountBalance()*Risk / 10000; Lots = NormalizeDouble(Lots,2);
// to simplify the coding and speed up access
// data are put into internal variables

   Ma = iMA(NULL,0,Ma_Period,0,MODE_SMA,PRICE_CLOSE,1);
   RSI = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,1);
   Curlev = iMomentum(NULL,Period(),Mom_Period,PRICE_OPEN,0);
   Prevlev = iMomentum(NULL,Period(),Mom_Period,PRICE_CLOSE,1);
   Close_1 = iClose(NULL,Period(),1);
   Range = MathAbs(Curlev-Prevlev);
   Slope = MathArctan(1/(Range+0.0001));
   Markup = Close_1-Ma;
   expire = OrderOpenTime()+57600;
    Comment("Slope =" ,Slope, "   Range =" ,Range);
     
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
        New_Bar=0;
    if(Time_0!=Time[0])
    {
    New_Bar=1;
    Time_0=Time[0];
    }
      // check for long position (BUY) possibility
      if(New_Bar==1 && Slope>1.57000)
        {
         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+500*Point,30,Ask+500*Point-StopLoss*Point,Ask+500*Point+TakeProfit*Point,"Ma13",5000,expire,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if(New_Bar==1 && Slope<1.57000)
        {
         ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-500*Point,30,Bid-500*Point+StopLoss*Point,Bid-500*Point-TakeProfit*Point,"Ma13",5000,expire,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(Close_1<Ma-50*Point && Markup>-0.001)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
               
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            // should it be closed?
            if(Close_1>Ma+50*Point && Markup<0.001)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
             
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
// the end.
 
oladapolaleye:

Please help on: OrderSend error 3

This is the code:

   switch(error_code)
     {
      //--- codes returned from trade server
      case 0:
      case 1:   error_string="no error";                                                   break;
      case 2:   error_string="common error";                                               break;
      case 3:   error_string="invalid trade parameters";                                   break;
      case 4:   error_string="trade server is busy";                                       break;
      case 5:   error_string="old version of the client terminal";                         break;
      case 6:   ....

      // check for long position (BUY) possibility
      if(New_Bar==1 && Slope>1.57000)
        {
         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+500*Point,30,Ask+500*Point-StopLoss*Point,Ask+500*Point+TakeProfit*Point,"Ma13",5000,expire,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }

Check the parameters

 
oladapolaleye:

Please help on: OrderSend error 3

This is the code:


Hi,

A good and recommended practice is to normalize double before sending the order :

double sl=NormalizeDouble(Ask+500*Point,Digits);
double tp=Norm....

Also, some brokers on live accounts only allow EA's to place orders without stop loss and take profits and then modify the orders to the desired stop and take profit.

Hope it helps.

 
thrdel:



Also, some brokers on live accounts only allow EA's to place orders without stop loss and take profits and then modify the orders to the desired stop and take profit.

I thought that that had changed and an EA can include SL and TP in the OrderSend.
 
GumRai:
I thought that that had changed and an EA can include SL and TP in the OrderSend.


Depends on broker taste.
 
deysmacro:

Depends on broker taste.
I don't think so. You can now send an order with SL/TP on any broker, independently of the usage of Instant Execution or Market Execution. I don't have checked every broker though, of course.
 
angevoyageur:
I don't think so. You can now send an order with SL/TP on any broker, independently of the usage of Instant Execution or Market Execution. I don't have checked every broker though, of course.

My broker is not fussy, so, can include SL/TP for Instant Execution or Market Execution though.