multiple orders - error 130

 

i'm trying to send 2 stop orders after each other, but i get a lot of error 130's.

this is the code i use:

So what should i change to get rid of the errors? Right now only the first order is taken it seems

//DIGIT FIX
   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; } 

//CALCULATIONS
    hh14=High[iHighest(NULL,0,MODE_HIGH,14,1)];
    ll14=Low[iLowest(NULL,0,MODE_LOW,14,1)];
    hh9=High[iHighest(NULL,0,MODE_HIGH,9,1)];
    ll9=Low[iLowest(NULL,0,MODE_LOW,9,1)];
      
//ORDER LEVELS
    spread=MathCeil(MarketInfo(Symbol(),MODE_SPREAD)*Point/pips2dbl)*(pips2points*Point);
    buyentry14=NormalizeDouble((hh14+spread*pips2dbl),Digits);
    sellentry14=NormalizeDouble((ll14-spread*pips2dbl),Digits);
    buyentry9=NormalizeDouble((hh9+spread*pips2dbl),Digits);
    sellentry9=NormalizeDouble((ll9-spread*pips2dbl),Digits);

//PLACE ORDERS
   if (Hour()==9 && Minute()==30) 
   {
    CloseOrders();
    RefreshRates();
    while(IsTradeContextBusy()) Sleep(100);
    OrderSend(Symbol(),OP_BUYSTOP,lots,buyentry14,100,buyentry14-20*pips2dbl,MarketInfo(Symbol(), MODE_ASK)+20*pips2dbl,NULL,MagicNumber,0,CLR_NONE);
    RefreshRates();
    while(IsTradeContextBusy()) Sleep(100);
    OrderSend(Symbol(),OP_SELLSTOP,lots,sellentry14,100,sellentry14+20*pips2dbl,MarketInfo(Symbol(), MODE_BID)-20*pips2dbl,NULL,MagicNumber,0,CLR_NONE);
   }
 
  1. RefreshRates AFTER sleeping and THEN calculate entry price.
  2. On 5 digit brokers you must adjust TP, SL, AND SLIPPAGE
  3. On ECN brokers you must OrderSend and THEN set the stops.
  4. You can not set pending orders closer to market than MarketInfo(Symbol(), MODE_STOPLEVEL)*Point On IBFX that's 3 pips 30 points
  5. MarketInfo(Symbol(), MODE_ASK) is equivalent to just Ask.
  6. MarketInfo(Symbol(),MODE_SPREAD)*Point/pips2dbl
    MI(spread) is in points. *Point/pips2dbl is the same as /pips2points On IBFX the spread is 2.2 which is less than the stoplevel.
  7. On buy orders you must add the spread to your entry price (highest high+spread*2). On sells you don't (Lowest low-spread)
  8. Once you open, you do not want to open more orders on the next tick. Add once per bar code.
 

thanks for your input, this is the complete code i have, with some change you suggested (refresh after sleep). BUt there's still a lot of error 130 now. Can you point me where i go wrong?

//+------------------------------------------------------------------+
//|                                                   Timetrader.mq4 |
//|                                      Copyright © 2011, Ido Kasse |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Ido Kasse"
#property link      ""

int MagicNumber;
extern double lots = 0.1;
double hh14, ll14, ll9, hh9;
double buyentry14, sellentry14, buyentry9, sellentry9, spread;
static datetime barStart;

//++++ 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)

//Close orders
void CloseOrders()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_SELL&& OrderMagicNumber()==MagicNumber) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),NULL, Yellow);
if(OrderType()==OP_SELLSTOP&& OrderMagicNumber()==MagicNumber) OrderDelete(OrderTicket());
if(OrderType()==OP_BUY&& OrderMagicNumber()==MagicNumber) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),NULL, Yellow);
if(OrderType()==OP_BUYSTOP&& OrderMagicNumber()==MagicNumber) OrderDelete(OrderTicket());
}
}


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
//DIGIT FIX
   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; } 
   
   //CREATE MAGICNUMBER - GLOBAL.    
   string id = WindowExpertName() + Symbol() + Period();   
   // If there isn't already a Global Variable with the id in wich search for the MagicNumber create it  
   if(!GlobalVariableCheck(id))
   {    
    MagicNumber=WindowHandle(Symbol(),0);   
    GlobalVariableSet(id,MagicNumber);
   }
   else // Get the MagicNumber for the unique id
   {
    MagicNumber = GlobalVariableGet(id);
   }  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

//RUN ONCE PER BAR
if (barStart < Time[0]) //start of new bar 
{
   barStart = Time[0];

//CALCULATIONS
    hh14=High[iHighest(NULL,0,MODE_HIGH,14,1)];
    ll14=Low[iLowest(NULL,0,MODE_LOW,14,1)];
    hh9=High[iHighest(NULL,0,MODE_HIGH,9,1)];
    ll9=Low[iLowest(NULL,0,MODE_LOW,9,1)]; 
    spread=MathCeil(MarketInfo(Symbol(),MODE_SPREAD)*Point/pips2dbl)*(pips2points*Point);
    double stoplevel=MarketInfo(Symbol(), MODE_STOPLEVEL)*pips2dbl;

//PLACE ORDERS
   if (Hour()==9 && Minute()==30) 
   {
    CloseOrders();
    while(IsTradeContextBusy()) Sleep(100);RefreshRates();
    buyentry14=NormalizeDouble((hh14+5*pips2dbl),Digits);
    sellentry14=NormalizeDouble((ll14-5*pips2dbl),Digits);
    OrderSend(Symbol(),OP_BUYSTOP,lots,buyentry14,100,sellentry14,Ask+20*pips2dbl,NULL,MagicNumber,0,CLR_NONE);
    while(IsTradeContextBusy()) Sleep(100);RefreshRates();
    buyentry14=NormalizeDouble((hh14+5*pips2dbl),Digits);
    sellentry14=NormalizeDouble((ll14-5*pips2dbl),Digits);
    OrderSend(Symbol(),OP_SELLSTOP,lots,sellentry14,100,buyentry14,Bid-20*pips2dbl,NULL,MagicNumber,0,CLR_NONE);
   }

//PLACE ORDERS
   if (Hour()==16 && Minute()==15) 
   {
    CloseOrders();
    while(IsTradeContextBusy()) Sleep(100);
    RefreshRates();
    buyentry9=NormalizeDouble((hh9+5*pips2dbl),Digits);
    sellentry9=NormalizeDouble((ll9-5*pips2dbl),Digits);
    OrderSend(Symbol(),OP_BUYSTOP,lots,buyentry9,50,sellentry9,Ask+20*pips2dbl,NULL,MagicNumber,0,CLR_NONE);
    while(IsTradeContextBusy()) Sleep(100);
    RefreshRates();
    buyentry9=NormalizeDouble((hh9+5*pips2dbl),Digits);
    sellentry9=NormalizeDouble((ll9-5*pips2dbl),Digits);
    OrderSend(Symbol(),OP_SELLSTOP,lots,sellentry9,50,buyentry9,Bid-20*pips2dbl,NULL,MagicNumber,0,CLR_NONE);
   }
      
}
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Reason: