I trade on 1H
& 4H
cycles, & I'm trying to implement coding that closes a position if it is in a loss & by the time it is a half way through the trade cycle ( PERIOD_CURRENT
).
Trade Cycle 4H
= 4 [hr]
= 240 [min]
= 14,400 [s]
A trade cycle is 4H
, 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.
Based on the time, being in seconds since 1/1/1970, I've compared the TimeCurrent()
with the OrderOpenTime()
+ the 4H
in seconds, but divided this by two to get the time after two hours.
This however isn't working - I've added the code below.
If anyone can shed any light on this, it would be greatly appreciated.
Example
OrderStartTime = 14,000,000
OrderCloseTime = 14,000,000 + 60 minutes * 240 minutes = 14,014,400 <<<<<
Close half way through
if trade is in loss at time = 14,000,000 + (60 * 240*0.5) = 140,007,200
for ( int earlcloses = OrdersTotal() - 1; earlcloses >= 0; earlcloses-- )
{
if ( OrderSelect( earlcloses, SELECT_BY_POS, MODE_TRADES ) )
if ( OrderType() == OP_SELL )
if ( OrderMagicNumber() == Period() )
if ( OrderOpenTime() + ( 60 * Period() * 0.5 ) <= TimeCurrent() )
if ( OrderOpenPrice() < 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() );
}
}
}
Trade Cycle = 4 Hours = 240 seconds
- may be minutes obviously? – user280708360 minutes * 240minutes
again, what is it, squared minutes? If you want to use dimensions, than it will something like60 sec/min * 240 min = 14400 sec
– user2807083