I threw together an adjust trailing stop function for EAs, can someone check it out?
Code:
//+-----------------------------------------------------+
//| adjust trailing stop |
//+-----------------------------------------------------+
void AdjustTrailingStop()
{
int orders = OrdersTotal();
for(int j=orders-1;j>=0;j--)
{
OrderSelect(j, SELECT_BY_POS);
int type = OrderType();
//-- Adjust trailing stop for BUYs
if (type == OP_BUY && OrderSymbol()==Symbol() && (OrderMagicNumber() == MagicNumber))
{
if (Ask-TrailingStop*Point > OrderOpenPrice() && Ask-TrailingStop*Point > OrderStopLoss())
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask-TrailingStop*Point,OrderTakeProfit(),0);
}
}
//-- Adjust trailing stop for SELLs
if (type == OP_SELL && OrderSymbol()==Symbol() && (OrderMagicNumber() == MagicNumber))
{
if (Bid+TrailingStop*Point < OrderOpenPrice() && Bid+TrailingStop*Point < OrderStopLoss())
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid+TrailingStop*Point,OrderTakeProfit(),0);
}
}
}
}