Hi,
I have not found the code for partial closing on more than 1 level.
See the example below for only 1 partial close
Code: Select all
// ----- Partial Closeextern string ptcInputs = "===== Partial Close settings: =====";
extern double PartCloseTgt = 15; // Partial Close Target in pips
extern double PartClosePct = 50; // Partial Close Position Percent
Pct1 = 0.01*PartClosePct;
Tgt1 = PartCloseTgt;void PartClose()
{
double Profit, pLots = 0;
lot_step = MarketInfo(Symbol(),MODE_LOTSTEP);
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
if(!OrderSelect(cnt, SELECT_BY_POS)) continue;
if(OrderSymbol() != Symbol() || OrderMagicNumber() != ipiMagic) continue;
if(OrderType() == OP_BUY)
{
Profit = (MarketInfo(Symbol(),MODE_BID) - OrderOpenPrice())/gldPoint;
if(Profit >= Tgt1 && Tgt1 > 0)
{
pLots = MathMin(OrderLots(),MathCeil(Pct1 * OrderLots()/lot_step)*lot_step);
if(pLots >= lot_min)
{
CloseAtMarket(OrderTicket(),pLots,Aqua);
}
}
}
if (OrderType() == OP_SELL)
{
Profit = (OrderOpenPrice() - MarketInfo(Symbol(),MODE_ASK))/gldPoint;
if(Profit >= Tgt1 && Tgt1 > 0)
{
pLots = MathMin(OrderLots(),MathCeil(Pct1*OrderLots()/lot_step)*lot_step);
if(pLots >= lot_min)
{
CloseAtMarket(OrderTicket(),pLots,Pink);
}
}
}
}
}
bool CloseAtMarket(int ticket,double lot,color clr)
{
bool result = false;
double price;
int tries=0;
while(!result && tries < TriesNum)
{
RefreshRates();
price = NormalizeDouble(OrderClosePrice(),Digits);
result = OrderClose(ticket,lot,price,dRatio*Slippage,clr);
if (!result)
{
Print("Error closing order : ",ErrorDescription(GetLastError())," ticket=",ticket," lot=",lot," Price=",price);
Sleep(5000);
}
tries++;
}
return(result);
}