Overmet TP After Minimum Trade Period

 

I want to force a minimum trade period of 1 bar. For this I did not set TP in orders but only after 1 period has passed.

To find a solution for a minimum trade period has been challenging. Finally I found this:

// Check if you have an opened position             
   if(PositionSelect(Symbol()) == true) 
         {

// Count the number of BARS between the time you opened the position and the current time.
         int nPosition_OpeningTime_Difference = Bars(Symbol(), Period(), PositionGetInteger(POSITION_TIME), TimeCurrent()) - 1;

// Check if the number of BARS is greater or equal than 5.
         if(nPosition_OpeningTime_Difference == 1)
               {
               
SetNewTPSL();
               
               }
         }

Setting new TP and SL works but I experience this problem. If TP in 2nd period is overmet, position is not closed but the trade waits till the - in this case - lower TP is met. The problem is that TP are set with `=´ only.A `>=´cannot be set when defining a TP.

How can I force the trade to close in 2nd bar where the TP is set immediatedly (at opening) even when TP is already overmet?

Maybe there is a better way to combine minimum trade period and take all profit in 2nd trade bar even if set TP is lower than profit at open course. Any hint is welcome.

Dokumentation zu MQL5: Zustandspruefung / Period
Dokumentation zu MQL5: Zustandspruefung / Period
  • www.mql5.com
Period - Zustandspruefung - Nachschlagewerk MQL5 - Nachschlagewerk über die Sprache des algothitmischen/automatischen Handels für MetaTrader 5
 
  1. Wir können hier Deutsch sprechen :)
  2. Dein bisschen Code ist zu wenig.
  3. amando hat hier veröffentlicht, wie man offenen Positionen auswählt die Werte abruft (unten).
  4. Ich würde beim Eröffnen einer Position einen zukünftigen Zeitpunkt festlegen (TimeCurrent() + n* PeriodSeconds()) und dann danach aktiv werden.
  5. Übrigens die Liste aller Funktionen (Schlüsselwortsuche mit Ctrl+F) ist hier: https://www.mql5.com/de/docs/function_indices
Order - Deals - Position - Histroy
Order - Deals - Position - Histroy
  • www.mql5.com
Get all your Deal, Positions and order Information to .csv Files. Ready for Excel import