//+-------------------------------------------------------------------------------------+
//|TakeProfitLots |
//+-------------------------------------------------------------------------------------+
double TakeProfitLots(double lotPrimary, int approach)
{
double lotDealToClose; // лот для закрытия на данном уровне тейк-профита
if (approach == 1)
lotDealToClose = lotPrimary * i_tp1Percent / 100;
return (LotRound(lotDealToClose));
if (approach == 2)
lotDealToClose = lotPrimary * i_tp2Percent / 100;
return (LotRound(lotDealToClose));
if (approach == 3)
lotDealToClose = lotPrimary * i_tp3Percent / 100;
return (LotRound(lotDealToClose));
}
//+-------------------------------------------------------------------------------------+
//| Проверка объема на корректность и округление |
//+-------------------------------------------------------------------------------------+
double LotRound(double L)
{
return(MathRound(MathMin(MathMax(L, minLot), maxLot)/lotStep)*lotStep);
}
//+-------------------------------------------------------------------------------------+
//| Определяем количество закрытий ордеров |
//+-------------------------------------------------------------------------------------+
int OrderCloseCount(double ordOpPrice, datetime ordOpTime)
{
int count = 0;
for (int i = OrdersHistoryTotal() - 1; i >= 0 ; i--)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderOpenPrice() == ordOpPrice && OrderOpenTime() == ordOpTime)
count++;
}
return (count);
}
//+-------------------------------------------------------------------------------------+
//| Определяем тейк-профиты |
//+-------------------------------------------------------------------------------------+
void TakeProfits()
{
int type;
double OOP;
datetime OOT;
int ticket;
int counterOfCloses;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
type = OrderType();
OOP = OrderOpenPrice();
OOT = OrderOpenTime();
i_lot = OrderLots();
ticket = OrderTicket();
counterOfCloses = OrderCloseCount(OOP, OOT);
RefreshRates();
if (type == OP_SELL)
{
if (i_takeProfit1 > 0)
if (Ask <= OOP - i_takeProfit1 * pt && counterOfCloses == 0)
OrderClose(ticket, TakeProfitLots(i_lot, 1), Ask, i_slippage, Black);
if (i_takeProfit2 > 0)
if (Ask <= OOP - i_takeProfit2 * pt && counterOfCloses == 1)
OrderClose(ticket, TakeProfitLots(i_lot, 2), Ask, i_slippage, Black);
if (i_takeProfit3 > 0)
if (Ask <= OOP - i_takeProfit3 * pt && counterOfCloses == 2)
OrderClose(ticket, TakeProfitLots(i_lot, 3), Ask, i_slippage, Black);
}
if (type == OP_BUY)
{
if (i_takeProfit1 > 0)
if (Bid >= OOP + i_takeProfit1 * pt && counterOfCloses == 0)
OrderClose(ticket, TakeProfitLots(i_lot, 1), Bid, i_slippage, Black);
if (i_takeProfit2 > 0)
if (Bid >= OOP + i_takeProfit2 * pt && counterOfCloses == 1)
OrderClose(ticket, TakeProfitLots(i_lot, 2), Bid, i_slippage, Black);
if (i_takeProfit3 > 0)
if (Bid >= OOP + i_takeProfit3 * pt && counterOfCloses == 2)
OrderClose(ticket, TakeProfitLots(i_lot, 3), Bid, i_slippage, Black);
}
}
}