close
How to set trailing stop manually in MetaTrader?
To place a trailing stop you must have an opened position. You simply go to the position you want to set its trailing stop in the Terminal Window and right click it, that will bring the context menu showed in figure 1, choose from it Trailing Stop sub-menu and you'll get the levels of trailing stop you can choose one of them.
If you didn't find the trailing stop level you want in this menu you can click the last command in the menu "Custom" to bring an input window to set the trailing stop (Figure 2).
MQL4 Trailing stop sample code:
// YOUR CODE HERE!
extern double TrailingStop = 100;
int MagicNumber = 101090;
//+------------------------------------------------------------------+
int start()
{
int cnt,total;
total = OrdersTotal();
// YOUR CODE HERE!
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) //<-- Long position is opened
{
TrailOrder(OrderType()); return(0); //<-- Trailling the order
}
if(OrderType()==OP_SELL) //<-- Go to short position
{
TrailOrder(OrderType()); return(0); //<-- Trailling the order
}
}
}
return(0);
}
//+------------------------------------------------------------------+
void TrailOrder(int type)
{
if(TrailingStop>0)
{
if(OrderMagicNumber() == MagicNumber)
{
if(type==OP_BUY)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),
Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
}
}
}
if(type==OP_SELL)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
}
}
}
}
}
}
全站熱搜