DIYers, copy this over the top of the existing bool LookForTradeClosure(int ticket) functionn - note there is a new function:[*]Trade1TradeComment: this is to help you identify which time frame generated a trade. This will aid gathering information about results on the different time frames. PoS uses the trade comment during the function that checks to see if a trade needs to be closed. It uses the comment to identify which time frame generated the trade. YOU MUST NOT alter the trade comment once there are open trades. PoS will be exceptionally rude to you if you do.
Code: Select all
int ExtractIndexFromTradeComment(string symbol, string comment)
{
//Return the the time frame index from the order comment passed by LookForTradeClosure()
if (comment == Trade1TradeComment)
return(0);
if (comment == Trade2TradeComment)
return(1);
if (comment == Trade3TradeComment)
return(2);
if (comment == Trade4TradeComment)
return(3);
//User has buggered up the order comment, so drive him nuts with an alert.
Alert(symbol, ". You have buggered up your trade comments so PoS cannot work properly. Read the damn user guide and stop being an asshole. Cretins fail when trading Forex.");
return(5);//Impossible value
}//End int ExtractIndexFromTradeComment(string symbol, string comment)
bool LookForTradeClosure(int ticket)
{
//Close the trade if the close conditions are met.
//Called from within CountOpenTrades(). Returns true if a close is needed and succeeds, so that COT can increment cc,
//else returns false
if (!BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES) )
return(true);
if (BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES) )
if (OrderCloseTime() > 0)
return(true);
bool CloseThisTrade = false;
//We need to know which time frame this trade belongs to
int cc = ExtractIndexFromTradeComment(OrderSymbol(), OrderComment() );
///////////////////////////////////////////////////////////////////////////////////////////////////////////
if (OrderType() == OP_BUY || OrderType() == OP_BUYSTOP)
{
//TP
if (bid >= OrderTakeProfit() )
if (!CloseEnough(OrderTakeProfit(), 0) )
CloseThisTrade = true;
//SL
if (bid <= OrderStopLoss() )
if (!CloseEnough(OrderStopLoss(), 0) )
CloseThisTrade = true;
//Close trade on opposite direction signal
if (SixthsTradingStatus[cc] == tradableshort)
CloseThisTrade = true;
}//if (OrderType() == OP_BUY || OrderType() == OP_BUYSTOP)
///////////////////////////////////////////////////////////////////////////////////////////////////////////
if (OrderType() == OP_SELL || OrderType() == OP_SELLSTOP)
{
//TP
if (bid <= OrderTakeProfit() )
if (!CloseEnough(OrderTakeProfit(), 0) )
CloseThisTrade = true;
//SL
if (bid >= OrderStopLoss() )
if (!CloseEnough(OrderStopLoss(), 0) )
CloseThisTrade = true;
//Close trade on opposite direction signal
if (SixthsTradingStatus[cc] == tradablelong)
CloseThisTrade = true;
}//if (OrderType() == OP_SELL || OrderType() == OP_SELLSTOP)
///////////////////////////////////////////////////////////////////////////////////////////////////////////
if (CloseThisTrade)
{
bool result = false;
if (OrderType() < 2)//Market orders
result = CloseOrder(ticket);
else
result = OrderDelete(ticket, clrNONE);
//Actions when trade close succeeds
if (result)
{
TicketNo = -1;//TicketNo is the most recently trade opened, so this might need editing in a multi-trade EA
OpenTrades--;//Rather than OpenTrades = 0 to cater for multi-trade EA's
return(true);//Makes CountOpenTrades increment cc to avoid missing out ccounting a trade
}//if (result)
//Actions when trade close fails
if (!result)
{
return(false);//Do not increment cc
}//if (!result)
}//if (CloseThisTrade)
//Got this far, so no trade closure
return(false);//Do not increment cc
}//End bool LookForTradeClosure()