V 1c is in post 1. I have had a lot of fun playing with this today. The ea froze on me, just as it did in craptesting, so I went bug hunting.
All that was needed was a couple more calls to CountOpenTrades() from within ShouldTradesBeClosed() to re-populate the variables that keep track of the position. Coders will spot them immediately.
If you want to make the changes yourselves but are not confident, then the easiest way to to find ShouldTradesBeClosed() at line 1100 and copy this over the top of the existing code:
Code: Select all
void ShouldTradesBeClosed()
{
if (OpenTrades == 0)
return;//Nothing to do
int tries = 0;
//We only want trades closing if the trades are based on a previous candle's signal
if (LatestTradeTime < iTime(Symbol(), TradingTimeFrame, 0) )
{
//Opposite direction semafor
if (CloseTradesOnRelevantSemafor)
{
//Close buys
if (SemStatus == highsemafor)
{
tries = 0;
//Close buys
if (BuyOpen)
{
ForceTradeClosure = true;
while (ForceTradeClosure)
{
CloseAllTrades(OP_BUY);
CloseAllTrades(OP_BUYSTOP);
CloseAllTrades(OP_SELLSTOP);//Forces a regrid closer to the market
if (ForceTradeClosure)
Sleep(1000);
tries++;
if (tries >= 100)
{
ForceTradeClosure = false;
break;
}//if (tries >= 100)
}//while (ForceTradeClosure)
SendBuyGrid(Symbol(), OP_BUYSTOP, NormalizeDouble(Ask + (DistanceBetweenTrades / factor), Digits), Lot);
return;//Nothing more for this function to do
}//if (BuyOpen)
}//if (SemStatus == highsemafor)
if (SemStatus == lowsemafor)
{
tries = 0;
//Close sells
if (SellOpen)
{
ForceTradeClosure = true;
while (ForceTradeClosure)
{
CloseAllTrades(OP_SELL);
CloseAllTrades(OP_SELLSTOP);
CloseAllTrades(OP_BUYSTOP);//Forces a regrid closer to the market
if (ForceTradeClosure)
Sleep(1000);
tries++;
if (tries >= 100)
{
ForceTradeClosure = false;
break;
}//if (tries >= 100)
}//while (ForceTradeClosure)
SendSellGrid(Symbol(), OP_SELLSTOP, NormalizeDouble(Bid - (DistanceBetweenTrades / factor), Digits), Lot);
return;//Nothing more for this function to do
}//if (SellOpen)
}//if (SemStatus == lowsemafor)
}//if (CloseTradesOnRelevantSemafor)
}//if (LatestTradeTime < iTime(Symbol(), TradingTimeFrame, 0) )
//Hedged position. Has it hit tp?
if (Hedged)
{
bool ClosePosition = false;
//Have we hit pips upl
if (HedgeProfitPips > 0)
if (PipsUpl >= HedgeProfitPips)
ClosePosition = true;
//Have we hit cash upl
if (!ClosePosition)
if (HedgeProfitCash > 0)
if (CashUpl >= HedgeProfitCash)
ClosePosition = true;
if (ClosePosition)
{
tries = 0;
ForceTradeClosure = true;
while(ForceTradeClosure)
{
ForceTradeClosure = false;
if (BuyOpen)
CloseAllTrades(OP_BUY);
if (SellOpen)
CloseAllTrades(OP_SELL);
if (SellStopOpen)
CloseAllTrades(OP_SELLSTOP);
if (SellLimitOpen)
CloseAllTrades(OP_SELLLIMIT);
if (BuyStopOpen)
CloseAllTrades(OP_BUYSTOP);
if (BuyLimitOpen)
CloseAllTrades(OP_BUYLIMIT);
tries++;
if (tries >= 100)
{
break;
}//if (tries >= 100)
CountOpenTrades();//Recalculate the position
}//while(ForceTradeClosure)
return;//Nothing else to do
}//if (ClosePosition)
//Successive waves of group trade closures can leave a massive gap in between
//groups of market trades. Deal with this situation by setting up a new grid
//of pendings closer to the market.
if (IsGapBetweenGroupsTooBig() )
{
//No need to try to force trade deletions as subsequent ticks will
//repeat this process if necessary.
ForceTradeClosure = false;
CloseAllTrades(OP_BUYSTOP);
CloseAllTrades(OP_SELLSTOP);
if (!ForceTradeClosure)
{
SendBuyGrid(Symbol(), OP_BUYSTOP, NormalizeDouble(Ask + (DistanceBetweenTrades / factor), Digits), Lot);
SendSellGrid(Symbol(), OP_SELLSTOP, NormalizeDouble(Bid - (DistanceBetweenTrades / factor), Digits), Lot);
return;
}//if (!ForceTradeClosure)
CountOpenTrades();
}//if (IsGapBetweenGroupsTooBig() )
}//if (Hedged)
//We always want a grid of GridSize * 2 stop orders, so do a delete and resend when a trade fills
if (BuyStops <= 1)
{
ForceTradeClosure = true;
while(ForceTradeClosure)
{
tries = 0;
ForceTradeClosure = false;
CloseAllTrades(OP_BUYSTOP);
if (ReplaceOppositeGridAtL4)
CloseAllTrades(OP_SELLSTOP);
tries++;
if (tries >= 100)
{
ForceTradeClosure = false;
break;
}//if (tries >= 100)
}//while(ForceTradeClosure)
SendBuyGrid(Symbol(), OP_BUYSTOP, NormalizeDouble(Ask + (DistanceBetweenTrades / factor), Digits), Lot);
if (ReplaceOppositeGridAtL4)
SendSellGrid(Symbol(), OP_SELLSTOP, NormalizeDouble(Bid - (DistanceBetweenTrades / factor), Digits), Lot);
CountOpenTrades();
return;
}//if (BuyStops < GridSize)
if (SellStops <= 1)
{
ForceTradeClosure = true;
while(ForceTradeClosure)
{
tries = 0;
ForceTradeClosure = false;
if (ReplaceOppositeGridAtL4)
CloseAllTrades(OP_BUYSTOP);
CloseAllTrades(OP_SELLSTOP);
tries++;
if (tries >= 100)
{
ForceTradeClosure = false;
break;
}//if (tries >= 100)
}//while(ForceTradeClosure)
if (ReplaceOppositeGridAtL4)
SendBuyGrid(Symbol(), OP_BUYSTOP, NormalizeDouble(Ask + (DistanceBetweenTrades / factor), Digits), Lot);
SendSellGrid(Symbol(), OP_SELLSTOP, NormalizeDouble(Bid - (DistanceBetweenTrades / factor), Digits), Lot);
CountOpenTrades();
}//if (BuyStops < GridSize)
}//void ShouldTradesBeClosed()

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.