آکادمی ایران ام کیو ال ‹ انجمن ‹ سوالات MQL5 ‹ اختصاص سود یک معامله هج برای بستن قسمتی از حجم معامله جهت عکس ‹ پاسخ به: اختصاص سود یک معامله هج برای بستن قسمتی از حجم معامله جهت عکس
-
//+------------------------------------------------------------------+
//| Apply TT |
//+------------------------------------------------------------------+
void ApplyTT(double tt, long dealType)
{
double remainedTT = tt;
ulong furthestTicket = 0;
double furthestProfit = 0.0;
// If no opposite position is running
if(dealType == 0 && PositionCounter(0) == 0)
return;
if(dealType == 1 && PositionCounter(1) == 0)
return;
// If buy position hits tp, apply TT to sell positions
if(dealType == 1)
{
furthestTicket = Furthest(1);
if(PositionSelectByTicket(furthestTicket))
furthestProfit = PositionGetDouble(POSITION_PROFIT)+ PositionGetDouble(POSITION_SWAP);
while(remainedTT+ furthestProfit > 0)
{
remainedTT += furthestProfit;
Trade.PositionClose(furthestTicket);
// Go for next run
furthestTicket = Furthest(1);
if(furthestTicket == 0)
break;
if(PositionSelectByTicket(furthestTicket))
furthestProfit = PositionGetDouble(POSITION_PROFIT)+ PositionGetDouble(POSITION_SWAP);
}
// Partial close
if(remainedTT > 0)
{
furthestTicket = Furthest(1);
if(PositionSelectByTicket(furthestTicket))
furthestProfit = PositionGetDouble(POSITION_PROFIT)+ PositionGetDouble(POSITION_SWAP);
if(furthestProfit == 0.0)
return;
double exitLot = remainedTT* PositionGetDouble(POSITION_VOLUME)/ MathAbs(furthestProfit);
exitLot = NormalizeDouble(exitLot, 2);
remainedTT += furthestProfit;
Trade.PositionClosePartial(furthestTicket, exitLot);
}
}
// If sell position hits tp, apply TT to buy positions
if(dealType == 0)
{
furthestTicket = Furthest(0);
if(PositionSelectByTicket(furthestTicket))
furthestProfit = PositionGetDouble(POSITION_PROFIT)+ PositionGetDouble(POSITION_SWAP);
while(remainedTT+ furthestProfit > 0)
{
remainedTT += furthestProfit;
Trade.PositionClose(furthestTicket);
// Go for next run
furthestTicket = Furthest(0);
if(furthestTicket == 0)
break;
if(PositionSelectByTicket(furthestTicket))
furthestProfit = PositionGetDouble(POSITION_PROFIT)+ PositionGetDouble(POSITION_SWAP);
}
// Partial close
if(remainedTT > 0)
{
furthestTicket = Furthest(0);
if(PositionSelectByTicket(furthestTicket))
furthestProfit = PositionGetDouble(POSITION_PROFIT)+ PositionGetDouble(POSITION_SWAP);
if(furthestProfit == 0.0)
return;
double exitLot = remainedTT* PositionGetDouble(POSITION_VOLUME)/ MathAbs(furthestProfit);
exitLot = NormalizeDouble(exitLot, 2);
remainedTT += furthestProfit;
Trade.PositionClosePartial(furthestTicket, exitLot);
}
}
TT = 0.0;
}