函数执行持仓订单的移动止损功能。
- extern int Magic = 20080829;
- extern color clModify = Silver;
- extern int TrailingStart = 30;
- extern int TrailingSize = 30;
- void TrailPositions()
- {
- int cnt = OrdersTotal();
- for (int i=0; i<cnt; i++)
- {
- if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
- if (OrderSymbol() != Symbol()) continue;
- if (OrderMagicNumber() != Magic) continue;
- int type = OrderType();
- if (type == OP_BUY)
- {
- if (Bid-OrderOpenPrice() > TrailingStart*Point)
- {
- if (OrderStopLoss() < Bid - (TrailingSize+1)*Point)
- {
- OrderModify(OrderTicket(),
- OrderOpenPrice(),
- Bid-TrailingSize*Point,
- OrderTakeProfit(), 0, clModify);
- }
- }
- }
- if (type == OP_SELL)
- {
- if (OrderOpenPrice()-Ask > TrailingStart*Point)
- {
- if (OrderStopLoss() > Ask + (TrailingSize+1)*Point || OrderStopLoss() == 0)
- {
- OrderModify(OrderTicket(),
- OrderOpenPrice(),
- Ask+TrailingSize*Point,
- OrderTakeProfit(), 0, clModify);
- }
- }
- }
- }
- }
|