why im getting error 130 all the time

 

why im getting error 130 all the time ?

thanx

//+------------------------------------------------------------------+
//|                                                        SHAUL.mq4 |
//|                                                        |
//|                                                http://ftsa.co.il |
//+------------------------------------------------------------------+
#property link      ""
#include <stderror.mqh>
#include <stdlib.mqh>
#define IDLE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_TRBUY 3
#define SIGNAL_TRSELL 4
#define SIGNAL_P 5
#define SIGNAL_L 6
bool trade=false;
int tickets=0;
int ticketb=0;
int Order = IDLE;
int Barw;
int barc;

double BOLH;
double BOLL;

extern int TP=250;    //take profit
extern int SL=150;      //stop loss
extern double lotSize=0.1;
extern double DEV=3.0;
extern int Per=12;
extern int Bar_wait=5;
extern int MagicNumber=7272;



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----
   if (OrdersTotal()==0)   
   { trade=false;
     Order = IDLE;
   }
   
   if (OrdersTotal()!=0)
   {
       for (int i=1; i<=OrdersTotal(); i++)       //Cycle for all orders..
      {                                        //displayed in the terminal
       if(OrderSelect(i-1,SELECT_BY_POS)==true)//If there is the next one
        {                                     
          if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
          {
           trade=true;
           
               break;
            }
          else {trade=false;}
          }
        } 
     
   } 
double division =DEV;
double spred; //fix the price so it will open order when it's really touching the bands
spred=(Ask-Bid);
int time_test= Period();// define what is the time period we are using 

//////////////////// INDICATORS\

BOLH=iCustom(NULL,0,"Bands",Per,0,DEV,MODE_UPPER,0);///the last zero its the shift   
BOLL= iCustom(NULL,0,"Bands",Per,0,DEV,MODE_LOWER,0);///the last zero its the shift 
Comment("bollh = " ,BOLH ,"bol LOW= ",BOLL, "\nbid= ", Bid ,"\nask= ", Ask, "\ngoods= ",spred, "\nDEV= ",division); // show the price of the boll high


//////////////////CHECK FOR BUY

if (trade==false && Order==IDLE)
{

if ( Open[0]>=BOLL && (Ask-spred)<BOLL/* Open[0]<=BOLL && Ask<BOLL */)

{Order=SIGNAL_BUY;}     
}
//////////////////////////////


//////////////////CHECK FOR SELL

if (trade==false && Order==IDLE)
{

if ( Open[0]<=BOLH && (Bid-spred)>BOLH/* Open[0]>=BOLH && Ask>BOLH*/ )

{Order=SIGNAL_SELL;}
     // Alert("sell_signal");
      
}

/////////////////////////BUY
if (trade==false)
{
  if (Order == SIGNAL_BUY  )
  {

  ticketb=OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,Ask-(SL*Point), Ask+(TP *Point),"SHAUL",MagicNumber ,0 ,Green);   //open
   
   //if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)
  // OrderModify(ticketb,OrderOpenPrice(),Ask- SL*Point,Ask+ (TP * Point) ,0,Green);   ///working on it
   
  

   
      if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)
      {
       Alert("LONG ORDER OPENED IN SHAUL"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
      Order = SIGNAL_TRBUY;
      
       
        
      }
      else
      {
      Print("OrderSelect returned the error of ",GetLastError());
      Alert("EROR LONG ORDER IN SHAUL "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
      Order= IDLE;
      }
   }
} 
/////////////////////////////////// END


/////////////////////////sell
if (trade==false)
{
  if (Order == SIGNAL_SELL  )
  {

  tickets=OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0,"SHAUL",MagicNumber,0,Green); // open sell 

   if(OrderSelect( tickets, SELECT_BY_TICKET )==true)
   OrderModify(tickets,OrderOpenPrice(),Ask+(SL*Point),Ask-(TP*Point),0,Green);


      if(OrderSelect( tickets, SELECT_BY_TICKET )==true)
      {
       Alert("SHORT ORDER OPENED IN SHAUL"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
      Order = SIGNAL_TRSELL;
      
        
      }
      else
      {
      Print("OrderSelect returned the error of ",GetLastError());
      Alert("EROR SHORT ORDER IN SHAUL "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
      Order= IDLE;
      }
   }
} 
/////////////////////////////////// END
int how_much_to_wait= Bar_wait;
bool stopped =false;
/////////////////check order status buy

if (trade==false && Order==SIGNAL_TRBUY)
{
  if (OrderSelect( ticketb, SELECT_BY_TICKET,MODE_HISTORY )==true)
  { 
   if (OrderProfit()>0){Order=SIGNAL_P;}  //order made money
   if (OrderProfit()<0){Order=SIGNAL_L;}  //order lost money
   }
}
///////////////////////////////////


/////////////////check order status sell

if (trade==false && Order==SIGNAL_TRSELL)
{
  if (OrderSelect( ticketb, SELECT_BY_TICKET,MODE_HISTORY )==true)
  { 
   if (OrderProfit()>0){Order=SIGNAL_P;} //barc=Bars;}   made money
   if (OrderProfit()<0){Order=SIGNAL_L;}  // Barw=Bars;} lost money
   }
}
///////////////////////////////////
// if made profit change it back to idle
if (trade==false && Order==SIGNAL_P) 
      Order=IDLE;
/////////////////////////////////// 
//if lost wait
if (trade==false && Order==SIGNAL_L)
{
   
   //if (Bars>=Barw+Bar_wait) {Order=IDLE;}
   Sleep(time_test*60*1000*how_much_to_wait);
   stopped=true;
   if (stopped==true)
   {
      Alert("sleep work");
      Order=IDLE;
   }
   else
   Alert("sleep didnt work");
   
}
/////////////////////////////////// 


//////////////////////////////
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

Seperating the The stop loss and Take Profit entries from the OrderSend and placing them into an OrderModify, following the OrderSend will resolve your issue. You will most likely want 1 code path for non ECN brokers and one code path for ECN brokers. This way, fast exexcution on non ECN and somewhat slower on ECN.

 
You also have to adjust TP, SL, and slippage for 5 digit brokers
//++++ These are adjusted for 5 digit brokers.
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int init(){
    if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
Reason: