Error 130 Invalid stops.

 

Hi Guys,

I have trouble with SL and TP. Anyone any ideas? It works well when SL=0; Backtest also works, but when I go live I get Error 130!!!

thanks, Sebastian


//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  extern int      Magic=                 40;
  extern double   VolumeLots=             0.01;
  extern int      StopPips=               0;
  extern int      TakePips=               0;
  extern int      EMAperiod=             40;
  extern int      ATRperiod=             14;
  extern int      ATRlevelPips=          1000;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
         int      WaitSeconds=          220;    // Trade-context arbitrage time-out
         int      TOT=                    1;    // Number of orders opened
         int      TRL=                    1;    // Period of trailing
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
         int      SlipPips,MP,MAG0,MAG1,
                  TFW,TB,TS,
                    BB[16],  SS[16];
         bool     ENABLE,SCANNED,
                  F_UP,F_DN,UP,DN;
         double   CHECK,ATRL,TAKE,STOP,
                    ST[16],  TK[16],
                  Obuy[16],Osel[16],
                  Sbuy[16],Ssel[16],
                  TRAIL_E2,TRAIL_P2;
         string   CHART;
         datetime Tbuy[16],Tsel[16],
                  SaveW,OpenW,
                  SaveT,OpenT;
//---------------------------------------------------------------------------------------
int      init()
{
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   CHART=Symbol(); TFW=Period();
   SlipPips=(SLIP_INTERVAL_PPIPS*0.0001*Open[1])/Point;
   if(Digits==3||Digits==5)MP=10; else MP=1;
   ATRL=NormalizeDouble(MP*Point*ATRlevelPips,Digits);
   TAKE=NormalizeDouble(MP*Point*TakePips    ,Digits);
   STOP=NormalizeDouble(MP*Point*StopPips    ,Digits);
   ENABLE=true; F_UP=false; F_DN=false;
   MAG0=Magic; MAG1=Magic+TOT-1;
   SaveW=iTime(CHART,TFW,0); SaveT=iTime(CHART,TRL,0);
   Scan();
                                                                              return(0);
                                                              return(0);
}
//---------------------------------------------------------------------------------------
void     Scan()
{        int      k,t,m;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   for(m=0;m<TOT;m++)
   {  BB[m]=0; SS[m]=0;
   }TB=0; TS=0;
   for(k=OrdersTotal()-1;0<=k;k--)
   {  if(!OrderSelect(k,SELECT_BY_POS))continue;
      t=OrderType();m=OrderMagicNumber();
      if(OrderSymbol()!=CHART||m<MAG0||MAG1<m||2<=t)continue;
      m-=Magic;
      if(t==OP_BUY)
      {  BB[m]=OrderTicket(); Tbuy[m]=OrderOpenTime(); Obuy[m]=OrderOpenPrice();
         TB++;
      }
      if(t==OP_SELL)
      {  SS[m]=OrderTicket(); Tsel[m]=OrderOpenTime(); Osel[m]=OrderOpenPrice();
         TS++;
}  }  }
//---------------------------------------------------------------------------------------
int      start()
{        double   EMA[2],ATR[2];
         bool     c,o;
         int      i,j;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 //OpenT=iTime(CHART,TRL,0);              if(SaveT!=OpenT)SaveT=OpenT;  else  return(0);
 //
   OpenW=iTime(CHART,TFW,0);              if(SaveW!=OpenW)SaveW=OpenW;  else  return(0);
   Scan();
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   for(j=0;j<=1;j++)
   {  EMA[j]=iMA( CHART,TFW,EMAperiod,0,MODE_EMA,PRICE_CLOSE,j+1);
      ATR[j]=iATR(CHART,TFW,ATRperiod,                       j+1);
   }
   F_UP=false; F_DN=false;
   if(Close[2]<EMA[1]&&Close[1]>EMA[0])
   {  if(ATR[0]<ATRL){F_DN=true;F_UP=false;}else{F_UP=true;F_DN=false;}
   }
   if(Close[2]>EMA[1]&&Close[1]<EMA[0])
   {  if(ATR[0]<ATRL){F_UP=true;F_DN=false;}else{F_DN=true;F_UP=false;}
   }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   if(F_UP)
   {  c=CloseSel();  o=OpenBuy();                                             return(0);
   }
   if(F_DN)
   {  c=CloseBuy();  o=OpenSel();                                             return(0);
   }
                                                                              return(0);
}
//---------------------------------------------------------------------------------------
bool     OpenSel()
{        int      k,t,er,cn;
         bool     r=false;
         double   s,p;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   Alert("*** SELL 40 ***");
   if(TOT<=TS)                                                                return(r);
   for(k=0;k<TOT;k++)
   {  if(SS[k]<=0)break;
   }if(0<SS[k])                                                               return(r);
   Ssel[k]=0.0;
   k+=Magic;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   Comment("Waiting for the SELL...");
   if(AccountFreeMarginCheck(CHART,OP_SELL,VolumeLots)<0.0)
   {  Alert("Not enough money");                                              return(r);
   }cn=0;
   while(cn<TRIES_REQUOTE&&!r)
   {  WaitForEnable(); er=0;
      if(0<STOP)s=NormalizeDouble(Bid+STOP,Digits);else s=0.0;
      if(0<TAKE)p=NormalizeDouble(Bid-TAKE,Digits);else p=0.0;
      t=OrderSend(CHART,OP_SELL,VolumeLots,Bid,SlipPips,s,p,"40sell",k,0,OrangeRed);
      if(0<t)  {  Comment("SELL - success at ",cn+1,"-th attempt."); r=true;
                //Scan();
                  break;
               }
      else     {  er=GetLastError(); r=false; cn++; if(er==138)continue;
                  cn+=TRIES_REQUOTE2; Sleep(5000);
   }           }
   if(!r){Alert("Failure while opening SELL = ",er);
         }
                                                                              return(r);
}
//---------------------------------------------------------------------------------------
bool     OpenBuy()
{        int      k,t,er,cn;
         bool     r=false;
         double   s,p;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   while(cn<TRIES_REQUOTE&&!r)
   {  WaitForEnable(); er=0;
      if(0<TAKE)p=NormalizeDouble(Ask+TAKE,Digits);else p=0.0;
      if(0<STOP)s=NormalizeDouble(Ask-STOP,Digits);else s=0.0;
      t=OrderSend(CHART,OP_BUY ,VolumeLots,Ask,SlipPips,s,p,"40buy",k,0,DodgerBlue);
      if(0<t)  {  Comment("BUY  - success at ",cn+1,"-th attempt."); r=true;
                //Scan();
                  break;
               }
      else     {  er=GetLastError(); r=false; cn++; if(er==138)continue;
                  cn+=TRIES_REQUOTE2; Sleep(5000);
   }           }
   if(!r){Alert("Failure while opening BUY = ",er);
         }

 
sebastianhh:

Hi Guys,

I have trouble with SL and TP. Anyone any ideas? It works well when SL=0; Backtest also works, but when I go live I get Error 130!!!

thanks, Sebastian

The Strategy tester does not simulate an ECN Broker, is your Broker an ECN Broker ?

ECN