V 1c is in post 1, with the changes I described in my previous post.
To hand-code these changes, copy this over your existing CountOpenTrades() function:
Code: Select all
bool CountOpenTrades()
{
//Not all these will be needed. Which ones are depends on the individual EA.
OpenTrades = 0;
TicketNo = -1;
BuyOpen = false;
SellOpen = false;
PendingBuyOpen = false;
PendingSellOpen = false;
int type;//Saves the OrderType() for consulatation later in the function
TradeHasPartClosed=false;
upl = 0;//Unrealised profit and loss for hedging/recovery basket closure decisions
if (OrdersTotal() == 0) return(true);
//Define a variable to avoid the situation that Phil describes at http://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=92449#p92449
double ot = OrdersTotal();
//Iterating backwards through the orders list caters more easily for closed trades than iterating forwards
for (int cc = OrdersTotal() - 1; cc >= 0; cc--)
{
if (ot != OrdersTotal() )
return(false);
bool TradeWasClosed = false;//See 'check for possible trade closure'
//Ensure the trade is still open
if (!OrderSelect(cc, SELECT_BY_POS, MODE_TRADES) ) continue;
//Ensure the EA 'owns' this trade
if (OrderSymbol() != Symbol() ) continue;
if (OrderMagicNumber() != TrendMagicNumber)
if (OrderMagicNumber() != CtMagicNumber)
continue;
if (OrderCloseTime() > 0) continue;
//All conditions passed, so carry on
type = OrderType();//Store the order type
OpenTrades++;
//Store the latest trade sent. Most of my EA's only need this final ticket number as either they are single trade
//bots or the last trade in the sequence is the important one. Adapt this code for your own use.
if (TicketNo == -1) TicketNo = OrderTicket();
//upl might not be needed. Depends on the individual EA
upl+= (OrderProfit() + OrderSwap() + OrderCommission());
//The next line of code calculates the pips upl of an open trade. As yet, I have done nothing with it.
//something = CalculateTradeProfitInPips()
//These might need extra coding if both stop and limit orders are used
if (OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT) PendingBuyOpen = true;
if (OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT) PendingSellOpen = true;
//Add missing tp/sl in case rapidly moving markets prevent their addition - ECN
if (CloseEnough(OrderStopLoss(), 0) && !CloseEnough(StopLoss, 0)) InsertStopLoss(OrderTicket());
if (CloseEnough(OrderTakeProfit(), 0) && !CloseEnough(TakeProfit, 0)) InsertTakeProfit(OrderTicket() );
//Replace missing tp and sl lines
if (HiddenPips > 0) ReplaceMissingSlTpLines();
TradeWasClosed = LookForTradeClosure(OrderTicket() );
if (TradeWasClosed)
{
if (type == OP_BUY) BuyOpen = false;//Will be reset if subsequent trades are buys that are not closed
if (type == OP_SELL) SellOpen = false;//Will be reset if subsequent trades are sells that are not closed
cc++;
continue;
}//if (TradeWasClosed)
//Profitable trade management
if (OrderProfit() > 0)
{
TradeManagementModule();
if (TradeHasPartClosed) continue;//Part-closing changes the ticket number, so we cannot continue the current loop iteration
}//if (OrderProfit() > 0)
//Trade types and half-close, and adjust sl/tp in case slippage has caused them to be inaccurate when the trade was sent.
if (OrderType() == OP_BUY)
{
BuyOpen = true;
if (PartCloseEnabled && OrderComment() == TradeComment && OrderStopLoss() >= OrderOpenPrice() ) PartCloseTrade(OrderTicket() );
}//if (OrderType() == OP_BUY)
if (OrderType() == OP_SELL)
{
SellOpen = true;
if (PartCloseEnabled && OrderComment() == TradeComment && OrderStopLoss() <= OrderOpenPrice() && !CloseEnough(OrderStopLoss(), 0)) PartCloseTrade(OrderTicket() );
}//if (OrderType() == OP_SELL)
}//for (int cc = OrdersTotal() - 1; cc <= 0; c`c--)
//Loop exited correctly, so tell the rest of the code
return(true);
}//End bool CountOpenTrades();
Then go to start() and replace the call to CountOpenTrades() with:
Code: Select all
if (!CountOpenTrades() )
return(0);
There is a call to CountOpenTrades in init() as well, but it doesn't matter if that call fails the OrdersHistory() test.
I reckon that anyone using my shells needs to implement these changes across their files.

Read the effing manual, ok?
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed
Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.