Why My EA trail fails and I see error #130?

 
//+------------------------------------------------------------------+
//|                                                   testma1250.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input double lot=.05;
input int starttime=8;
input int finishttime=22;
//کار داره به عنوان آخرین اکس یعنی تریل را بازی کن 
//30+ trail 10----60trail 40----90trail 80
//و کاری کن در هر کندل فقط یک خرید کند و در عین حال در صورت وجود یک معامله از کندل های قبل باز هم خرید کند 
//شاید در صورت رسیدن قیمت به سربهسر یه معامله دگ هم وا کنه
//شایددرصورت استاپ شدن بعد از باز شدن سفارش و قبل از رسیدن به تریل اول باید یک معامله برعکس باز کرد چرا که سطح مهمی شکسته شده
void OnTick()
{
DeleteOrders();
Trail30();
Trail60();

Trail90();
//if(movStatus()=="BUY") close(2222);
//if(movStatus()=="SELL") close(1111);

if(Volume[0]<=1 && orders()==0 && Hourfilter()==true)
  {
  
  if(movStatus()=="BUY" )
{ double LongPurchasePrice=NormalizeDouble(Ask-((1-.43)*(Ask-(iLow(NULL,PERIOD_H1,iLowest(NULL,PERIOD_CURRENT,MODE_LOW,5,1))))),5);
  double SLlong=NormalizeDouble(iLow(NULL,PERIOD_H1,iLowest(NULL,PERIOD_CURRENT,MODE_LOW,5,1)),5);
  double TPLong=NormalizeDouble(Ask+.0120,5);
   Print("SL LONG= ",SLlong," Long Purchase Price= ",LongPurchasePrice," TP Long= ",TPLong);
  int ticketbuy=OrderSend(Symbol(),OP_BUYLIMIT,lot,LongPurchasePrice,5,SLlong,TPLong,"EDI",1111,0,clrGreen);
 } 
  
  if(movStatus()=="SELL" )
  {
 double ShortPurchasePrice=NormalizeDouble(Bid+((1-.43)*((iHigh(NULL,PERIOD_H1,iHighest(NULL,PERIOD_CURRENT,MODE_HIGH,5,1)))-Bid)),5);
 double SLshort=NormalizeDouble(iHigh(NULL,PERIOD_H1,iHighest(NULL,PERIOD_CURRENT,MODE_HIGH,5,1)),5);
 double TPShort=NormalizeDouble(Bid-.0120,5);
 Print("SL short= ",SLshort,"Short Purchase Price= ",ShortPurchasePrice," TP short= ",TPShort);
  int ticketsell=OrderSend(Symbol(),OP_SELLLIMIT,lot,ShortPurchasePrice,5,SLshort,TPShort,"EDI",2222,0,clrRed);
 } 
  }

}



////////////////////////////////تریل////

void Trail90()
{  
       for(int i=OrdersTotal()-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
          {
           if( OrderMagicNumber()==1111)
             {
                if(Ask>=OrderOpenPrice()+.009 && OrderOpenPrice()+.008>OrderStopLoss()
                    && OrderStopLoss()<OrderOpenPrice()+.0079)
                {
                  Print("Let's play Long order modifyyyyy  90 trail");  
                 bool longOrderModify=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+.008,OrderTakeProfit(),0,clrYellow);
                     if(!longOrderModify) 
                       Print("Error in OrderModify. Error code=",GetLastError()); 
                     else 
                       Print("Order modified successfully.");   
                  }
                 }
               if(  OrderMagicNumber()==2222)
                {
                 if(Bid<=OrderOpenPrice()-.009 && OrderOpenPrice()-.008<OrderStopLoss()
                     && OrderStopLoss()>OrderOpenPrice()-.0079)
                  { 
                   Print("Let's play Short order modifyyyyy  90 trail");
                   bool shortOrderModify=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-.008,OrderTakeProfit(),0,clrYellow);
                     if(!shortOrderModify) 
                       Print("Error in OrderModify. Error code=",GetLastError()); 
                     else 
                       Print("Order modified successfully.");   
      
                   }
                 }
              }
           }
        
}
////////////////////////////////تریل////

void Trail60()
{  
       for(int i=OrdersTotal()-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
          {
           if( OrderMagicNumber()==1111)
             {
                if(Ask>=OrderOpenPrice()+.006 && OrderOpenPrice()+.004>OrderStopLoss()
                    && OrderStopLoss()<OrderOpenPrice()+.0039)
                {
                  Print("Let's play Long order modifyyyyy  60 trail");  
                 bool longOrderModify=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+.004,OrderTakeProfit(),0,clrYellow);
                     if(!longOrderModify) 
                       Print("Error in OrderModify. Error code=",GetLastError()); 
                     else 
                       Print("Order modified successfully.");   
                  }
                 }
               if(  OrderMagicNumber()==2222)
                {
                 if(Bid<=OrderOpenPrice()-.006 && OrderOpenPrice()-.004<OrderStopLoss()
                     && OrderStopLoss()>OrderOpenPrice()-.0039)
                  { 
                   Print("Let's play Short order modifyyyyy  60 trail");
                   bool shortOrderModify=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-.004,OrderTakeProfit(),0,clrYellow);
                                  if(!shortOrderModify) 
                       Print("Error in OrderModify. Error code=",GetLastError()); 
                     else 
                       Print("Order modified successfully.");   
      
                   }
                 }
              }
           }
        
}


////////////////////////////////تریل////

void Trail30()
{  
       for(int i=OrdersTotal()-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
          {
           if( OrderMagicNumber()==1111)
             {
                if(Ask>=OrderOpenPrice()+.003 && OrderOpenPrice()+.001>OrderStopLoss()
                    && OrderStopLoss()<OrderOpenPrice()+.0009)
                {
                  Print("Let's play Long order modifyyyyy  30 trail");  
                 bool longOrderModify=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+.001,OrderTakeProfit(),0,clrYellow);
                     if(!longOrderModify) 
                       Print("Error in OrderModify. Error code=",GetLastError()); 
                     else 
                       Print("Order modified successfully.");   
      
                  }
                 }
               if(  OrderMagicNumber()==2222)
                {
                 if(Ask<=OrderOpenPrice()-.003 && OrderOpenPrice()-.001<OrderStopLoss()
                     && OrderStopLoss()>OrderOpenPrice()-.0009)
                  {
                   Print("Let's play Short order modifyyyyy  30 trail");  
                   Print("Newstop= ",NormalizeDouble(OrderOpenPrice()-.001,5));
                   Print("SYMBOL_TRADE_STOPS_LEVEL= ",Point()*SYMBOL_TRADE_STOPS_LEVEL); 
                   Print("Ask-SYMBOL_TRADE_STOPS_LEVEL= ",Ask," - ",Point()*SYMBOL_TRADE_STOPS_LEVEL," =",Ask-Point()*SYMBOL_TRADE_STOPS_LEVEL);
                   bool shortOrderModify=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-.001,OrderTakeProfit(),0,clrYellow);
                     if(!shortOrderModify) 
                       Print("Error in OrderModify. Error code=",GetLastError()); 
                     else 
                       Print("Order modified successfully.");   
                   }
                 }
              }
           }
        
}


//-----------------------------وضعیت مویینگها
void DeleteOrders()
{
 for(int i=OrdersTotal()-1;i>=0;i--)//از آخر به اول سفارشات را بررسی کن
    {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))//سفارشات بررسی شده مد ترید را انتخاب کن
      {
       if(OrderMagicNumber()==1111 || OrderMagicNumber()==2222)//اگر مجیک نامبرشان همان است که نوشتم
         {
          if(TimeDay(OrderOpenTime())+1<Day())//یک روز پس از 
            {
             if(OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
               bool deleteorder=OrderDelete(OrderTicket(),clrYellow);
             }                  
         }
      }
    }
}
  
  
string movStatus()
{
 if(iMA(NULL,PERIOD_CURRENT,12,0,MODE_EMA,PRICE_CLOSE,2)<=iMA(NULL,PERIOD_CURRENT,50,0,MODE_EMA,PRICE_CLOSE,2)
   && iMA(NULL,PERIOD_CURRENT,12,0,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,PERIOD_CURRENT,50,0,MODE_EMA,PRICE_CLOSE,1))
 { 
  return("BUY");
 }
 else if(iMA(NULL,PERIOD_CURRENT,12,0,MODE_EMA,PRICE_CLOSE,2)>=iMA(NULL,PERIOD_CURRENT,50,0,MODE_EMA,PRICE_CLOSE,2)
   && iMA(NULL,PERIOD_CURRENT,12,0,MODE_EMA,PRICE_CLOSE,1)<iMA(NULL,PERIOD_CURRENT,50,0,MODE_EMA,PRICE_CLOSE,1))
 {
  return("SELL");
 }
 else 
 {
  return("No Signal");
 }
   
} 


/////////////////tic////////////شمارش پوزیشنها 
 int orders()
 {
  int num=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderMagicNumber()== 1111 || OrderMagicNumber()== 2222)
           num++;
         }
      }
   return(num);
  } 
//---------------tic----------تابع بستن پوزیشنها-------------------------------
void close(int Magic)
{ 
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderMagicNumber()== Magic)
           bool closeorder=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,clrGreen);
         }
      }
   
}
///////////////تیک/////فیلترساعت/////////////////////////////////////////////////
bool Hourfilter()
{
 if(Hour()>=starttime && Hour()<=finishttime)
 return(true);
 
 else
 return(false);
}
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • 2023.03.03
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
 

Totally this highlighted part is my 1st trailing stop which says if Ask is 30 pips bellow OrderOpenPrice, in a short Trade,then place order stop loss 10 pips bellow Orderopen price.

when i test this EA for the 1st sell order, All 3 trails work properly.

But for the 2nt sell order (Magic# 2222),the 1st trail which is highlighted in yellow does not work and sends this error: OrderModify error 130

Also here is the print functions results before the error:

2023.03.03 18:28:53.762 2023.01.05 16:00:00  testma1250 EURUSD,H1: open #2 sell limit 0.05 EURUSD at 1.05959 sl: 1.06289 tp: 1.04322 ok

2023.03.03 18:28:53.762 2023.01.05 16:00:00  testma1250 EURUSD,H1: Let's play Short order modifyyyyy  30 trail

2023.03.03 18:28:53.762 2023.01.05 16:00:00  testma1250 EURUSD,H1: Newstop= 1.05859

2023.03.03 18:28:53.762 2023.01.05 16:00:00  testma1250 EURUSD,H1: SYMBOL_TRADE_STOPS_LEVEL= 0.00031

2023.03.03 18:28:53.762 2023.01.05 16:00:00  testma1250 EURUSD,H1: Ask-SYMBOL_TRADE_STOPS_LEVEL= 1.05528 - 0.00031 =1.05497

2023.03.03 18:28:53.762 2023.01.05 16:00:00  testma1250 EURUSD,H1: OrderModify error 130

2023.03.03 18:28:53.762 2023.01.05 16:00:00  testma1250 EURUSD,H1: Error in OrderModify. Error code=130

so you can see that all the conditions for the trail is correct, and stop level as 3.1 pips and my trailing stop is about 30 pips above Ask , but it is not working.

Please help  i hope i explained clearly.


 

Don't double post! You already had another thread open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)
 
William Roeder #:

Don't double post! You already had another thread open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

I could not edit  that to send the total code.

Here is the total code.