title: How to close a trade after a certain time if a position is in loss | MQL4
body: <p>I trade on <strong><code>1H</code></strong> &amp; <strong><code>4H</code></strong> cycles, &amp; I'm trying to implement coding that closes a position if it is in a loss &amp; by the time it is a half way through the trade cycle ( <code>PERIOD_CURRENT</code> ).</p>
  
  <p><strong>Trade Cycle <code>4H</code> = 4 <code>[hr]</code> = 240 <code>[min]</code> = 14,400 <code>[s]</code></strong></p>
  
  <p>A trade cycle is <code>4H</code>, so I want to be able to automatically close a position if a trade is in loss 2 hours after the trade was initially opened.</p>
  
  <p>Based on the time, being in seconds since 1/1/1970, I've compared the <strong><code>TimeCurrent()</code></strong> with the <strong><code>OrderOpenTime()</code></strong> + the <strong><code>4H</code></strong> in seconds, but divided this by two to get the time after two hours.</p>
  
  <p>This however isn't working - I've added the code below.</p>
  
  <p>If anyone can shed any light on this, it would be greatly appreciated.</p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>OrderStartTime = 14,000,000
  
  OrderCloseTime = 14,000,000 + 60 minutes * 240 minutes = 14,014,400 &lt;&lt;&lt;&lt;&lt;
  </code></pre>
  
  <p><strong>Close half way through<br> if trade is in loss at time</strong> <code>= 14,000,000 + (60 * 240*0.5) = 140,007,200</code></p>
  
  <pre><code>for ( int earlcloses = OrdersTotal() - 1; earlcloses &gt;= 0; earlcloses-- )
  {
      if (                        OrderSelect( earlcloses, SELECT_BY_POS, MODE_TRADES ) )
           if (                   OrderType()                               == OP_SELL )
                 if (             OrderMagicNumber()                        == Period() )
                       if (       OrderOpenTime() + ( 60 * Period() * 0.5 ) &lt;= TimeCurrent() )
                             if ( OrderOpenPrice()                          &lt;  Bid )
                             { // |||||||||||||||||||||||||||||||||||||||||||
                               /* --serially-nested-if()-s CODEBLOCK-{}-START--- */
  
                                  earlcloses = OrderClose( OrderTicket(),
                                                           LotSize,
                                                           MarketInfo( sym, MODE_BID )
                                                         + MarketInfo( sym, MODE_SPREAD )
                                                         * MarketInfo( sym, MODE_POINT ),
                                                           300,
                                                           clrNONE
                                                           );
  
                               /* ------------------------ CODEBLOCK-{}-FINISH--- */
                             } // ||||||||||||||||||||||||||||||||||||||||||||
      if ( earlcloses == true )
      {    Sleep( 60000 );
           int  earlyclosesel =  OrdersHistoryTotal()-1;
           bool earlySelect   =  OrderSelect( earlyclosesel, SELECT_BY_POS, MODE_HISTORY );
           if ( earlySelect   == true )
           {
                int earlTicket = OrderTicket();
                SendMail( "Notification of early position closure", "Trade #" + IntegerToString( earlTicket, 0 ) + "has been closed at" + DoubleToStr( OrderClosePrice(), 5 ) + "due to timeout" );
           }
           else if ( earlySelect == false )
                {
                     Print( "EarlyClose failed with error #", GetLastError() );
                }
      }
   }
  </code></pre>