V 4c is inpost 1, with the scaling back in code added. Details in the user guide.
DIYers, add the inputs. Search for: extern int NoOfLevels=4;
Add the scale back in inputs underneath:
extern bool UseScaleBackIn=false;
extern string ScaleBackInTradeComment="Scale in";
Go to: bool scaleOutStopLoss(int ticket)
Replace the entire function with this:
Code: Select all
bool scaleOutStopLoss(int ticket)
{
/*
Called from countOpenTrades()
This function examines an open trade to see if the phased stop loss should be used.
Returns 'true' if there is a part-closure, else false.
This cannot be applied to hedged trades.
*/
//Check the order is still open
if (!betterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
return(false);
//Cannot work this function if there is no stop loss
//if (closeEnough(OrderStopLoss(), 0) )
// return(false);
//Save stuff for easier typing
int type = OrderType();
string symbol = OrderSymbol();
double price = OrderOpenPrice();
double stop = OrderStopLoss();
double take = OrderTakeProfit();
if (HideStopLossAndTakeProfit)
stop = calculateStopLoss(symbol, type, price);
getBasics(symbol);//Always good to include this in a function
//No further action needed if the trade is in profit
if (type == OP_BUY)
if (bid > price)
{
return(false);
}//if (bid > price)
if (type == OP_SELL)
if (ask < price)
{
return(false);
}//if (ask < price)
//We have a trade and it is a loser. Calculate the distance
//in between the order open price and the stop loss. The order open price
//does not change following a partial closure. Not when tested on a GP demo, at least. Fuck
//only knows what happens with the criminal element masquerading as 'brokers'.
double slpips = MathAbs(price - stop);
//Calculate the sl levels at which to partially close the trade.
double slLevel = slpips / NoOfLevels;
//A variable to tell the code that part-closure is needed.
bool closeNeeded = false;
//A variable to hold the calculated price at each level.
double calculatedLevel = price;
//A variable to hold the amount to part-close
double lotsToClose = normalizeLots(symbol, Lot / NoOfLevels);
//Alert(lotsToClose);
double targetLots = 0;
//Loop through the levels and set the closeNeeded bool if the market has moved to
//the calculatedLevel price
for (int cc = 1; cc <= NoOfLevels; cc++)
{
//Buy trade
if (type == OP_BUY)
{
//The trade is losing by at least one level
calculatedLevel-= slLevel;
if (bid <= calculatedLevel)
{
//We need to know if there has already been a partial close at this stop level
targetLots = Lot - (lotsToClose * cc);
if (OrderLots() > targetLots)
{
closeNeeded = true;
break;
}//if (OrderLots() > targetLots)
}//if (bid <= calculatedLevel)
}//if (type == OP_BUY)
//Sell trade
if (type == OP_SELL)
{
//The trade is losing by at least one level
calculatedLevel+= slLevel;
if (ask >= calculatedLevel)
{
//We need to know if there has already been a partial close at this stop level
targetLots = Lot - (lotsToClose * cc);
if (OrderLots() > targetLots)
{
closeNeeded = true;
break;
}//if (OrderLots() > targetLots)
}//if (ask <= calculatedLevel)
}//if (type == OP_SELL)
//Alert(calculatedLevel);
}//for (int cc = 1; cc <= NoOfLevels; cc++)
//Should part of the trade be closed:
if (closeNeeded)
{
bool result = OrderClose(ticket, lotsToClose, OrderClosePrice(), 5, clrNONE);
if (result)
{
//Replace the trade with a stop order if scaling back in
if (UseScaleBackIn)
{
int tries = 0;//To exit an endless loop because something went wrong
double newPrice = 0;
//Buy stop
if (type == OP_BUY)
{
//Calculate the price for the stop order
newPrice = NormalizeDouble(calculatedLevel + slLevel, digits);
//We must have the order sent
result = false;
while (!result)
{
if (IsTradeContextBusy() )
Sleep(2000);
result = sendSingleTrade(symbol, OP_BUYSTOP, ScaleBackInTradeComment, lotsToClose, newPrice, stop, take);
//Exit an endless loop if something went wrong
if (!result)
{
tries++;
if (tries >= 100)
break;
Sleep(1000);
}//if (!result)
}//while (!result)
}//if (type == OP_BUY)
//Sell stop
if (type == OP_SELL)
{
//Calculate the price for the stop order
newPrice = NormalizeDouble(calculatedLevel - slLevel, digits);
//We must have the order sent
result = false;
while (!result)
{
if (IsTradeContextBusy() )
Sleep(2000);
result = sendSingleTrade(symbol, OP_SELLSTOP, ScaleBackInTradeComment, lotsToClose, newPrice, stop, take);
//Exit an endless loop if something went wrong
if (!result)
{
tries++;
if (tries >= 100)
break;
Sleep(1000);
}//if (!result)
}//while (!result)
}//if (type == OP_SELL)
}//if (UseScaleBackIn)
return(true);
}//if (result)
}//if (closeNeeded)
//Got here, so no closure
return(false);
}//End bool scaleOutStopLoss(int ticket)
The scale back in code is the same as the code I added earlier to SPB. There may be bugs.

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.