MT4 Restart makes EA repeat recent tasks again? - page 3

 

lol alright let me write a script to illustrate what I mean.

 

ok run this test script. A datetime is really just like an integer value. It is actually a count of seconds since 1970 so it has to be an integer value.

int start()
  {
//---- using TimeCurrent() for this test
   
   double timeandpartcloses = TimeCurrent();
   GlobalVariableSet("GV1",timeandpartcloses);
   
//---- simulate one part Close added to the GV
   timeandpartcloses+= 0.01;
   GlobalVariableSet("GV1",timeandpartcloses);
   
//---- get the date out of the GV
   datetime GVdate = GlobalVariableGet("GV1");
   
//---- test the GVdate to make sure it is still a valid date
   Alert("date = ", TimeToStr( GVdate,TIME_DATE|TIME_SECONDS ) );
   
//---- get amount of part closes out of the GV
   int partcloses = NormalizeDouble( (GlobalVariableGet("GV1") - GVdate)*100,0 );
   Alert("part closes = ",partcloses);
   
//---- delete test GV
   GlobalVariableDel("GV1");
//----
   return(0);
  }
//+------------------------------------------------------------------+

I normalized the part closes count and made it integer in case any of those nasty double rounding errors happens.

 

I appreciate you illustrating that for me :)

What I am not getting though, is how I am using double partcloses in an equality statement before OrderClose() function, so that the next time the loop is run, it will see in the "if" statement that there is something different, as to not complete the OrderClose() again...

I'm really sorry if I am being a pain in the backside and not getting this, but I just don't see how I am using that in an equality statement to filter continuous closing of OrderClose()...

 

I dont know what else to tell you....

The GV is just there to hold some data you might need to resume after a restart. It's down to you to decide what data that is, I just gave you an example based on what you posted earlier, but you could do the same thing by naming the GV with the date to associate it with the correct trade and store into it whatever data you need to store for recovery purposes.

What are your usual conditions for not repeatedy part closing each time ? Define what criteria that is and the data it uses that would be lost if mt4 restart happens, store that data in the global variable and extract it from there when mt4 is restarted.

 

UPDATE: Just to get straight to the point and more concise; why is the print below, showing this and different times....

//+--------------------------------------------------------------------------------------------------+
//| Close OP_SELL Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder1()
{   
   
   datetime Closed_FirstTarget; 
      // << - This holds the OrderOpenTime() if OrderClose() function is true.
   datetime FetchDateOfFirstClose = GlobalVariableGet(" GV_1st "); 
     // << - This then gets hold in the equality statement != below, before the OrderClose() function...
   
   double minLot=MarketInfo(Symbol(),MODE_MINLOT);
   double lotStep=MarketInfo(Symbol(),MODE_LOTSTEP);
   
   double EMA_Bar = iClose(NULL, PERIOD_H1, 1);
   double EMA_MA = iMA(Symbol(),60,21,0,1,0,0);
   
   int PositionIndex;    
   int TotalNumberOfOrders;   
   TotalNumberOfOrders = OrdersTotal();   

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  
   {
   bool CurrentOrder = OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES);
   if( ! CurrentOrder ) continue;  
      if( OrderMagicNumber() == MagicNumber )  
         if( OrderSymbol() == Symbol())        
          {  
           if( CurrentOrder == True )
            {
             double time = OrderOpenTime();
            } 
           

           //<<-- First Target Order Close function -->>\\ 
           if( time != FetchDateOfFirstClose && OrderType()==OP_SELL && FirstTarget_Sell - Ask > - Point )
               //<-- "FetchDatOfFirstClose" is GVGet from " GV_1st " in second to bottom line...
               {
               
               if(  minLot - half_1st > Point / 2. && OrderLots() - (minLot*2) > - Point ) // Could only close minLot
                  {
                   bool FirstTarget_MinLotClose = OrderClose( OrderTicket(), minLot, Bid, 5, CLR_NONE );
                   
                   if( FirstTarget_MinLotClose != True )Print(" tim != FetchDateOfFirstClose = ", time, " != ", FetchDateOfFirstClose);
                   

                   if( FirstTarget_MinLotClose == True )Closed_FirstTarget = GlobalVariableSet(" GV_1st ", time); 
                    //<< -- This is now setting OrderOpenTime() to GVset and used in the equality "if" statement.
                     
                  }
2013.10.25 12:18:28	2001.02.15 20:03  Trend Fishing - V1 - Refined Exits EURUSD,H1:  time != FetchDateOfThirdClose = 982224765 != 1000452548

 
If you put your comments on a different line, your post wouldn't be so wide and will make it easier to read without scrolling back and forth.
 
GumRai:
If you put your comments on a different line, your post wouldn't be so wide and will make it easier to read without scrolling back and forth.

Done - hopefully that makes it easier for you?
 
DomGilberto:

UPDATE: Just to get straight to the point and more concise; why is the print below, showing this and different times....



If this is the print

2013.10.25 12:18:28 2001.02.15 20:03 Trend Fishing - V1 - Refined Exits EURUSD,H1: time != FetchDateOfThirdClose = 982224765 != 1000452548

it is related to a different section of code to the code that you have posted

 
I missed a few obvious things... one of which:

 if(  minLot - half_1st > Point / 2. && OrderLots() - (minLot*2) > - Point ) // Could only close minLot
                  {
                   bool FirstTarget_MinLotClose = OrderClose( OrderTicket(), minLot, Ask, 5, CLR_NONE ); //< -- Bid changed to Ask

Sorry for wasting your time in helping me with such a stupidly missed area... I have it working now with this:

//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder()
{   

   static datetime Closed_FirstTarget;
   datetime        FetchDateOfFirstClose = GlobalVariableGet(" GV_1st ");
   
   static datetime Closed_EMA_Target;
   datetime        FetchDateOfEMAClose = GlobalVariableGet(" GV_EMA ");
   
   static datetime Closed_ThirdTarget;
   datetime        FetchDateOfThirdClose = GlobalVariableGet(" GV_3rd ");
   
   
   double minLot = MarketInfo(Symbol(),MODE_MINLOT);
   double lotStep=MarketInfo(Symbol(),MODE_LOTSTEP);

   double EMA_Bar = iClose(NULL, PERIOD_H1, 1);
   double EMA_MA = iMA(Symbol(),60,21,0,1,0,0);
   
   int PositionIndex;    //  <-- this variable is the index used for the loop
   int TotalNumberOfOrders;   //  <-- this variable will hold the number of orders currently in the Trade pool
   TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable
   
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
    {
     bool CurrentOrder = OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES);
      if(  ! CurrentOrder ) continue;  
       if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol() == Symbol())
        { 
         if( CurrentOrder == True)  //<<<<--------  If the OrderSelect returned true, then find the answer to the formula' below?
            {
               double half_1st = MathFloor(OrderLots()/First_Target/lotStep)*lotStep;
               //Print("The Lots to close is: ",DoubleToStr(half_1st,Digits), " On: ", OrderSymbol());
               double half_2nd = MathFloor(OrderLots()/EMA_Target/lotStep)*lotStep;
               //Print("The Lots to close is: ",DoubleToStr(half_2nd,Digits), " On: ", OrderSymbol());
               double Target_2 = MathFloor(OrderLots()/Second_Target/lotStep)*lotStep;
               //Print("The Lots to close is: ",DoubleToStr(Target_2,Digits), " On: ", OrderSymbol());
               FirstTarget_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice()) / RewardRatio );  
               TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/ ( RewardRatio / 2 )); 
            }
            
                            
          
          //<<-- First Target Order Close function -->>\\  
          if( FetchDateOfFirstClose != OrderOpenTime() && OrderType()==OP_BUY && Bid - FirstTarget_Buy > - Point ) 
              {
              
              if( minLot - half_1st > Point / 2. && OrderLots() - (minLot*2) > - Point ) // Could only close minLot
                  {
                   bool FirstTarget_MinLotClose = OrderClose( OrderTicket(), minLot, Bid, 5, CLR_NONE );
                   if( FirstTarget_MinLotClose != True )Print(" FirstTarget_MinLotClose failed, last error: ", GetLastError());
                   if( FirstTarget_MinLotClose == True )Print(" First Target Hit - Only MinLot was closed ");
                   if( FirstTarget_MinLotClose == True )Closed_FirstTarget = GlobalVariableSet(" GV_1st ", OrderOpenTime());
                     
                     Alert("First Target - Min lot was closed out: ", Symbol());
                  }
...
Reason: