追踪止损怎么自动移动?
分享一段自动移动止损的代码,趋势单能锁利润,很实用。
// 追踪止损(移动止损)
double trailingStop = 500; // 追踪距离,单位点
void OnTick() {
for(int i = OrdersTotal() - 1; i >= 0; i--) {
ulong ticket = OrderGetTicket(i);
if(OrderSelect(ticket)) {
if(OrderGetString(ORDER_SYMBOL) != Symbol()) continue;
double openPrice = OrderGetDouble(ORDER_OPENPRICE);
double currentSL = OrderGetDouble(ORDER_SL);
int type = OrderGetInteger(ORDER_TYPE);
if(type == OP_BUY) {
double newSL = Bid - trailingStop * Point;
// 如果新止损比当前止损高,就上移
if(newSL > currentSL && Bid - openPrice > trailingStop * Point) {
OrderModify(ticket, openPrice, newSL, OrderGetDouble(ORDER_TP), 0, clrBlue);
Print("买单 #", ticket, " 追踪止损上移到:", DoubleToString(newSL, Digits));
}
} else if(type == OP_SELL) {
double newSL = Ask + trailingStop * Point;
// 如果新止损比当前止损低,就下移
if(newSL < currentSL || currentSL == 0) {
if(openPrice - Ask > trailingStop * Point) {
OrderModify(ticket, openPrice, newSL, OrderGetDouble(ORDER_TP), 0, clrRed);
Print("卖单 #", ticket, " 追踪止损下移到:", DoubleToString(newSL, Digits));
}
}
}
}
}
}全部回复(1)
发表回复
请先登录后再回复。
