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

 
SDC:

I do it by OrderModify() add 1 pip to the broker SL before it is part closed. That way there is no need for recovery etc, the Order itself contains the information about how many times it was part closed already. (OpenPrice + SL) - (OpenPrice + Original SL) = nPartCloses.


In most cases this would work. And I use this already. The problem I have is that I have up to 3 partial closes on one open position via the use of OrderClose() function. The 2R target exit, for example, cannot use if(OrderOpenPrice() > OrderStopLoss()...) then use OrderClose() and move the StopLoss to 3 pips above the OrderOpenPrice, because in my case, 100% of the time, the stoploss will already be ABOVE the entry price once it reaches the 2R target through a trailing stop....

Therefore (assuming that makes sense to you), I can use what you're saying already, but it won't work with every OrderClose() function in play as it stands... I need something fairly robust in all contexts to use....
 

well if you are going to use a global variable, instead of naming it partclosedonce and putting the open time in it, why dont you name it with the open time, and put the amount of part closes in it ? Or you could put the time in it plus 0.1 for one part close plus 0.2 for 2 part closes. the time is an integer so adding 0.1 to it would make an easy calcuation to get both the time out of it and the number of closes

 
SDC:

well if you are going to use a global variable, instead of naming it partclosedonce and putting the open time in it, why dont you name it with the open time, and put the amount of part closes in it ? Or you could put the time in it plus 0.1 for one part close plus 0.2 for 2 part closes. the time is an integer so adding 0.1 to is would make an easy calcuation to get both the time out of it and the number of closes

Yes! This is what I am trying to understand. I've spent a long time trying to think of a clever way in converting GV into a string (date || time) > then into a datetime... Would you be so kind as to put me out of my misery and illustrate an example of how to go about doing that...
 

you dont need to make a string, just do GlobalVariableSet( GV1,OrderOpenTime() );

Remember a datetime is just like an integer value.

so you can store it as a double in a GV and it doesnt matter if you add 0.1 to it because when you convert a double to a datetime it drops the DP.

 
... that does not return a datetime format though.... It just gives you numbers.... OrderOpenTime() is a datetime and that parameter where you've put it is asking for a double....
datetime GlobalVariableSet( string name, double value) 
 
 
DomGilberto:
... that does not return a datetime format though.... It just gives you numbers.... OrderOpenTime() is a datetime and that parameter where you've put it is asking for a double....
TypeCast: Double --> Datetime
 

Yes Raptor knows what I mean, it doesnt matter that the datetime is a string of numbers it is supposed to be a string of numbers just put it in the GV.

 

Ok - I think I got it working now! Finally... coding takes longer when you have 101 other things going on at the same time!

   datetime Closed_FirstTarget;
   
   double FetchDateOfFirstClose = GlobalVariableGet(" GV1 ");          

...

/////////////////////////////////////////////////////////////////////////////////////

            //<<-- First Target Order Close function -->>\\ 
           if( OrderType()==OP_SELL && FirstTarget_Sell - Ask > - Point && 
               FetchDateOfFirstClose != OrderOpenTime() ) 
               {
               
               if(  minLot - half_1st > Point / 2. && OrderLots() - (minLot*2) > - Point ) // Could only close minLot
                  {
                   bool FirstTarget_MinLotClose = OrderClose( OrderTicket(), minLot, Ask, 5, CLR_NONE );
                   if( FirstTarget_MinLotClose == True )Closed_FirstTarget  = GlobalVariableSet(" GV1 ", OrderOpenTime()); 
                   
                   Print( " FetchDateOfFirstClose: ", FetchDateOfFirstClose, " Order Open Time: ", OrderOpenTime());                  
                  }
               
               
               if( half_1st - minLot > - Point ) // Correct Exit
                  {  
                  bool Close_Half_Order_Sell=OrderClose(OrderTicket(),half_1st,Ask,5,Blue);
                  if(Close_Half_Order_Sell==True)Closed_FirstTarget  = GlobalVariableSet(" GV1 ", OrderOpenTime()); 
                  
                  Print( " FetchDateOfFirstClose: ", FetchDateOfFirstClose, " Order Open Time: ", OrderOpenTime());
                  }   
               }
...
 

does it actually work as GlobalVariableSet(" GV1 ", OrderOpenTime()); ?

When I wrote that I wrote it rather flippantly, what I actually meant was to do,

double time = OrderOpenTime();
GlobalVariableSet("GV1",time);

because then when you part close an order you can do

time += 0.1

GlobalVariableSet("GV1",time);

so then you have the time plus the amount of part closes in the GV.

so to get the time out you can do

datetime GVopentime = GlobalVariableGet("GV1"); (because the conversion from double to datetime drops the decimal places.)

then to get the number of part closes out you can do

double PartCloses = (GlobalVariableGet("GV1") - GVopentime)*10

that idea assumes you dont part close an order more than 9 times. Probably it would be more robust to do time+=0.01 so it allows for 99 part closes without affecting the datetime.

 
You've lost me a little. Few things... "datetime GVopentime = GlobalVariableGet("GV1"); (because the conversion from double to datetime drops the decimal places.)" -- Shouldnt GVopentime, in this case, be a double? As GlobalVariableGet is a double....?

Secondly, how am I using this:
double PartCloses = (GlobalVariableGet("GV1") - GVopentime)*10 -- In the equality statements I am wanting to use, before the OrderClose() function?

i.e. -- If (OrderOpenTime() !=
FetchDateOfFirstClose ) -- "FetchDateOfFirstClose" being the GlobalVariableGet(" GV1 "); ?
Reason: