Strange Problem when running more than one instance of a script

 

Hi @all,


it's me again and I still have questions. sorry :( So here is a simple script that opens an order for me and maintains it. THe script works as supposed but when I'm running the script simultaneously on two different pairs it fails to find one of the orders given its magic number and symbol.

I've manually checked if the magicNumber is unique (which is the case - its the hash function posted here in the forum) but somehow it fails to find the order (OrderSelect has no errors). Help is very muich appreciated. Cheers.

PS: Sorry for the bad code. There are a few dead vars and sometime no error handling..its still under construction :)

int start()
  {
   symbol=Symbol();
   calcDigitPrecision();
   double StopLoss=getInitialStop();
   Alert("StopLoss: "+StopLoss);

   //Calculate Lot Size
   double volume=getLots(getAbsRisk(RiskInPercent),getStopLossInPips(StopLoss));
   //Get MagicNumber for Order
   magicNumber=makeMagicNumber(WindowExpertName() + symbol + Period()+iTime(symbol,PERIOD_MN1,0));

   //Try to Send Order
   int ticket=OrderSend(symbol,OP_BUY,volume,Ask,3,StopLoss,0.0,NULL,magicNumber,0,Green);
   if(ticket<0)
   {
       if(GetLastError()==131)
         Alert("OrderSend failed: Invalid trade volume ("+volume+")");
       if(GetLastError()==130)
         Alert("OrderSend failed: Invalid stops ("+StopLoss+")");
       if(GetLastError()==4109)
         Alert("OrderSend failed: Please Allow Live Trading!");
         
       Print("OrderSend failed with error #",GetLastError());
       return(0);
       
   }else{
      Alert("Trade open: "+magicNumber);
      ThisBarTrade=Bars;
      isStopped=false;
   }
   
   
   while(!isStopped){
      if (Bars != ThisBarTrade ) {                            
         Alert("Searching for Order:"+magicNumber+" on symbol "+symbol);
         ThisBarTrade=Bars;
         int count=0;
         int total=OrdersTotal();
         Alert("OrdersTotal:"+total);
         for(int iPos = 0; iPos < total ; iPos++){
            if(OrderSelect(iPos, SELECT_BY_POS) && OrderMagicNumber()==magicNumber && OrderSymbol()==symbol){
               count++;
               if(checkLastBar()){
                  RefreshRates();
                  double newStop1=stopForInner(lastOutCandle,Ask);
                  if(newStop1!=OrderStopLoss()){
                     OrderModify(OrderTicket(),OrderOpenPrice(),newStop1,OrderTakeProfit(),OrderExpiration(),CLR_NONE);
                  }
               }else{
                  RefreshRates();
                  double newStop2=stopForNormal(Ask);
                  if(newStop2!=OrderStopLoss()){
                     OrderModify(OrderTicket(),OrderOpenPrice(),newStop2,OrderTakeProfit(),OrderExpiration(),CLR_NONE);
                  }                  
               }
               
               
               
            }else{
               isStopped=true;
               return(0); //Order not found
            }
         }
      }
      Sleep(10000);
      RefreshRates();
   }
   
   
   
   return(0);
  }


 

I think your error handling is flawed and may be why you think there is no error when actually there is . . .

       if(GetLastError()==131)                                             //  this reads the error and clears it to 0
          Alert("OrderSend failed: Invalid trade volume ("+volume+")");
       if(GetLastError()==130)                                             //  the error has been cleared this cannot be true
         Alert("OrderSend failed: Invalid stops ("+StopLoss+")");
       if(GetLastError()==4109)                                            //  the error has been cleared this cannot be true
         Alert("OrderSend failed: Please Allow Live Trading!");
         
       Print("OrderSend failed with error #",GetLastError());              //  the error has been cleared 
       return(0);

GetLastError() documentation says . . . "The function returns the last occurred error, then the value of special last_error variable where the last error code is stored will be zeroized"

Also . . . don't use Bars, it isn't reliable as it stops incrementing when you have reached Max bars on chart use time instead.

 
RaptorUK:

I think your error handling is flawed and may be why you think there is no error when actually there is . . .

GetLastError() documentation says . . . "The function returns the last occurred error, then the value of special last_error variable where the last error code is stored will be zeroized"

Also . . . don't use Bars, it isn't reliable as it stops incrementing when you have reached Max bars on chart use time instead.


Thanks for your hints. The problem seems to be at the point where I want to select the appropriate order

for(int iPos = 0; iPos < total ; iPos++){
            if(OrderSelect(iPos, SELECT_BY_POS) && OrderMagicNumber()==magicNumber && OrderSymbol()==symbol){
I've checked the return values from OrderSelect(iPos,SELECT_BY_POS) again and it is always true. So it seems that it can't find an order with the corresponding magicNumber and symbol..?
 
I think your "if" finds an order, that is not matching to the condition, therefore stops the entire script.
 
erzo:
I think your "if" finds an order, that is not matching to the condition, therefore stops the entire script.

It's actually not entering into the if brackets. So it doesn't find any matching orders.
 

That is i'am talking about. It enters immediately into the else brackets, and stops. Not because there is no order matching, but there is an order earlier in the list that is not matching.

 
erzo:

That is i'am talking about. It enters immediately into the else brackets, and stops. Not because there is no order matching, but there is an order earlier in the list that is not matching.


Just re-thought your suggestion. You are absolutly right. the return(0); in the else clause is wrong. Thank you very much! Stupid mistake ...
 
erzo:

That is i'am talking about. It enters immediately into the else brackets, and stops. Not because there is no order matching, but there is an order earlier in the list that is not matching.

Top marks for spotting the actual error!


Unless the required order is first in the list the faulty logic will stop the execution.

 
Hi Dabbler,
Yes, it is easier to me than english :))) thanks!
Reason: