Problems with SELL Orders by EMA_CROSS Script

 
hello,
I have a problem with order to open by the EMA_CROSS Script from Coders Guru.
I often get the message: "Error opening SELL order error 130" of GetLastError() function back.
I do not point why, here the code, some changes has I made in the code :
//+------------------------------------------------------------------+
//|                                                    EMA_CROSS.mq4 |
//|                                                      Coders Guru |
//|                                         https://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Coders Guru"
#property link      "https://www.forex-tsd.com"
 
#define MAGIC 035633334
 
//---- input parameters
extern double    TakeProfit=60.0;
extern double    Lots=0.01;
extern double    TrailingStop=35.0; 
extern double    StopLoss=100.0;
 
extern int ShortEma = 5;
extern int LongEma = 10;
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
 
int Crossed (double line1 , double line2)
{
      static int last_direction = 0;
      static int current_direction = 0;
      
      if(line1>line2)current_direction = 1; //up
      if(line1<line2)current_direction = 2; //down
      if(current_direction != last_direction) //changed 
      {
            last_direction = current_direction;
            return (last_direction);
      }
      else
      {
            return (0);
      }
}
bool ExistPosition() {
bool Exist=False;
   for (int i=0; i<OrdersTotal(); i++) 
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      {//eine offene Order auswählen
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) 
         {//Währungssymbol und Kennummer vergleichen, Kennummer ist nur für das Script.
            if (OrderType()==OP_BUY || OrderType()==OP_SELL) 
            {//kucken ob buy/sell Order gibt, egal welche
            Exist=True; 
            break; //Es existiert bereits eine Order, abbrechen.
            }
         }
      }
   }
return(Exist);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
 
int start()
  {
//---- 
   int cnt, ticket, total;
   double SEma, LEma;
  
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0); //check Bars 
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
  
   SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,1); 
   LEma = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,1);
   
   int isCrossed  = 0;
   isCrossed = Crossed(SEma,LEma); 
 
if(!ExistPosition()) 
{
   if(isCrossed == 1)
   {
   ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
   "My EA",MAGIC,0,Green);
      if(ticket>0)
      {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
         Print("BUY order opened : ",OrderOpenPrice());
      } 
   else 
   {
   OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"My EA",MAGIC,0,Green);
   Print("Error opening BUY order error: ",GetLastError());
   } 
   return(0);
   }
   
   if(isCrossed == 2)
   {
   ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid-StopLoss*Point,Bid-TakeProfit*Point,
   "My EA",MAGIC,0,Red);
      if(ticket>0)
      {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
         Print("SELL order opened : ",OrderOpenPrice());
      }
   else 
   {
   ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid-StopLoss*Point,Bid-TakeProfit*Point,
   "My EA",MAGIC,0,Red);
   Print("Error opening SELL order error: ",GetLastError()); 
   }
   return(0);
   }
return(0);
}
 
 
/*-----------------------------------------------------------------------------*/
 
for(cnt=0;cnt<OrdersTotal();cnt++)
   {
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
   {
      if(OrderType()==OP_BUY)
      {           
        if(isCrossed == 2)
        {
        OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close buy position
        ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid-StopLoss*Point,Bid-TakeProfit*Point,
        "My EA",MAGIC,0,Red);//open sell position
            if(ticket>0)
            {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
            Print("SELL order opened : ",OrderOpenPrice());
            }
            else 
            {
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid-StopLoss*Point,Bid-TakeProfit*Point,
            "My EA",MAGIC,0,Red);
            Print("Error opening SELL order error: ",GetLastError()); 
            }
        return(0); 
        }
            
        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 
      {   
        if(isCrossed == 1)
        {
        OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
        "My EA",MAGIC,0,Green);
          if(ticket>0)
          {
          if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
          Print("BUY order opened : ",OrderOpenPrice());
          } 
          else 
          {
          OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
          "My EA",MAGIC,0,Green);
          Print("Error opening BUY order error: ",GetLastError());
          } 
        return(0); // exit
        }
           
        if(TrailingStop>0)// check for trailing stop 
        {                
          if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
          {
            if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
            {
            OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
            return(0);
            }
          }
        }
      }
   }
}//for
 
return(0);
}
//+------------------------------------------------------------------+

I probably accepted also still the problem that to high prices are often taken with OrderSend() function, I believe that are because of slippage parameter on 3 stand, can one that away leave without problems, or become then no more orders?


OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
   "My EA",MAGIC,0,Green);

 

#import "stdlib.ex4"

string ErrorDescription(int error_code);
int RGB(int red_value,int green_value,int blue_value);
bool CompareDoubles(double number1,double number2);
string DoubleToStrMorePrecision(double number,int precision);
string IntegerToHexString(int integer_number);

 

int start() { int i,counted_bars=IndicatorCounted();//---- i=Bars-counted_bars-1; while(i>=0) { double high =High[i]; double low =Low[i]; double open =Open[i]; double close=Close[i]; ExtMapBuffer1[i]=(close-low)-(high-close); if(ExtMapBuffer1[i]!=0) { double diff=high-low; if(0==diff) ExtMapBuffer1[i]=0; else { ExtMapBuffer1[i]/=diff; ExtMapBuffer1[i]*=Volume[i]; } } if(i<Bars-1) ExtMapBuffer1[i]+=ExtMapBuffer1[i+1]; i--; }//---- return(0); }//+------------------------------------------------------------------+

 
hello,
thank you very much for help.
I could solve the first problem with your assistance.
I had for the Sell Orders invalid stoploss because I instead of + I use a - to calculate the stoploss fpr Sell orders and that was not right.

But now I don´t now how I can use your second tip to solve my second problem.

My problem is that often bad course for to buy and sells orders are be generated with the OrderSend() function.

The arrows for buy and sales orders stand usually some points over the bars. And I ask myself why, the bars have never reached such high courses if one look at chart, but the orders is made so high.

I am unfortunately absolutely beginner and unfortunately do not get ahead with your second tip.

Is this tip also meant for my second problem?