Đây, bạn tham khảo dịch về hòa vốn và Trailling stop nhé:
---------------------------------------------------------------------
void MoveToBreakeven() // Ve diem hoa von
{
double PipProfit=0;
for(int b = OrdersTotal()-1; b >=0; b--)
{
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)== false)continue;
if (OrderMagicNumber() != MagicNumber)continue;
if(OrderSymbol()!=Symbol())continue;
if(OrderType()==OP_BUY)
RefreshRates();
double SL = NormalizeDouble(OrderOpenPrice()+ PipToLockIn*
pips, Digits);
PipProfit = NormalizeDouble(Bid - OrderOpenPrice(), Digits);
double MinProfit = WhenToMoveToBe*
pips;
if(PipProfit > MinProfit)
if ((SL > NormalizeDouble(OrderStopLoss(), Digits)) || (NormalizeDouble(OrderStopLoss(), Digits) == 0))
if(!OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,clrNONE))
{
Print("OrderModify error ",GetLastError());
}
//return;
}
for(int s = OrdersTotal()-1; s>=0; s--)
{
if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)==false)continue;
if (OrderMagicNumber() != MagicNumber)continue;
if(OrderSymbol()!=Symbol())continue;
if(OrderType()==OP_SELL)
RefreshRates();
double SL = NormalizeDouble(OrderOpenPrice()- PipToLockIn*
pips, Digits);
PipProfit = NormalizeDouble(OrderOpenPrice()- Ask, Digits);
double MinProfit = WhenToMoveToBe*
pips;
if(PipProfit > MinProfit)
if ((SL < NormalizeDouble(OrderStopLoss(), Digits)) || (NormalizeDouble(OrderStopLoss(), Digits) == 0))
if(!OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()- PipToLockIn*
pips),OrderTakeProfit(),0,clrNONE))
{
Print("OrderModify error ",GetLastError());
}
//return;
}
}
void Adjustrailing()// Trailing Stop
{
//adjust buy
for(int b = OrdersTotal()-1; b>=0; b--)
{
double MaxStopLoss =0 ;
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
if (OrderMagicNumber() != MagicNumber)continue;
if(OrderSymbol()!=Symbol())continue;
if(OrderType()==OP_BUY)
RefreshRates();
MaxStopLoss = NormalizeDouble(Bid-(AmountTrailing*
pips), Digits);
double CurrentStop = NormalizeDouble (OrderStopLoss(),Digits);
if((NormalizeDouble(Bid-OrderOpenPrice(), Digits)> WhenMoveTrailing*
pips) && (CurrentStop==0 ||(CurrentStop < MaxStopLoss)))
if(!OrderModify(OrderTicket(),OrderOpenPrice(),MaxStopLoss,OrderTakeProfit(),0,clrNONE))
{
Print("OrderModify error ",GetLastError());
}
//return;
}
//adjust sell
for(int s = OrdersTotal()-1; s>=0; s--)
{
double MaxStopLoss = 0;
if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
if (OrderMagicNumber() != MagicNumber)continue;
if(OrderSymbol()!=Symbol())continue;
if(OrderType()==OP_SELL)
RefreshRates();
MaxStopLoss = NormalizeDouble(Ask+(AmountTrailing*
pips),Digits);
double CurrentStop = NormalizeDouble (OrderStopLoss(),Digits);
if((NormalizeDouble(OrderOpenPrice()-Ask,Digits) > WhenMoveTrailing*
pips) &&(CurrentStop==0 ||(CurrentStop > MaxStopLoss)))
if(!OrderModify(OrderTicket(),OrderOpenPrice(),MaxStopLoss,OrderTakeProfit(),0,clrNONE))
{
Print("OrderModify error ",GetLastError());
}
//return;
}
}
//+------------------------------------------------------------------+