#define EXPERT_MAGIC 123456 // EA交易的幻数
//+------------------------------------------------------------------+
//| 更改挂单 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 声明并初始化交易请求和交易请求结果
MqlTradeRequest request={};
MqlTradeResult result={};
int total=OrdersTotal(); // 已下挂单的总数
//--- 重做所有已下的挂单
for(int i=0; i<total; i++)
{
//--- 订单参数
ulong order_ticket=OrderGetTicket(i); // 订单价格
string order_symbol=Symbol(); // 交易品种
int digits=(int)SymbolInfoInteger(order_symbol,SYMBOL_DIGITS); // 小数位数
ulong magic=OrderGetInteger(ORDER_MAGIC); // 订单的幻数
double volume=OrderGetDouble(ORDER_VOLUME_CURRENT); // 订单的当前交易量
double sl=OrderGetDouble(ORDER_SL); // 订单的当前止损
double tp=OrderGetDouble(ORDER_TP); // 订单的当前止赢
ENUM_ORDER_TYPE type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE); // 订单类型
int offset = 50; // 以点数从当前价抵消下单
double price; // 订单触动价
double point=SymbolInfoDouble(order_symbol,SYMBOL_POINT); // 点值
//--- 输出订单信息
PrintFormat("#%I64u %s %s %.2f %s sl: %s tp: %s [%I64d]",
order_ticket,
order_symbol,
EnumToString(type),
volume,
DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
DoubleToString(sl,digits),
DoubleToString(tp,digits),
magic);
//--- 如果幻数匹配,不定义止损和止赢
if(magic==EXPERT_MAGIC && sl==0 && tp==0)
{
request.action=TRADE_ACTION_MODIFY; // 交易操作类型
request.order = OrderGetTicket(i); // 订单价格
request.symbol =Symbol(); // symbol
request.deviation=5; // 允许价格偏差
//--- 根据持仓类型设置价格水平,订单的止损和止赢
if(type==ORDER_TYPE_BUY_LIMIT)
{
price = SymbolInfoDouble(Symbol(),SYMBOL_ASK)-offset*point;
request.tp = NormalizeDouble(price+offset*point,digits);
request.sl = NormalizeDouble(price-offset*point,digits);
request.price =NormalizeDouble(price,digits); // 正常开盘价
}
else if(type==ORDER_TYPE_SELL_LIMIT)
{
price = SymbolInfoDouble(Symbol(),SYMBOL_BID)+offset*point;
request.tp = NormalizeDouble(price-offset*point,digits);
request.sl = NormalizeDouble(price+offset*point,digits);
request.price =NormalizeDouble(price,digits); // 正常开盘价
}
else if(type==ORDER_TYPE_BUY_STOP)
{
price = SymbolInfoDouble(Symbol(),SYMBOL_BID)+offset*point;
request.tp = NormalizeDouble(price+offset*point,digits);
request.sl = NormalizeDouble(price-offset*point,digits);
request.price =NormalizeDouble(price,digits); // 正常开盘价
}
else if(type==ORDER_TYPE_SELL_STOP)
{
price = SymbolInfoDouble(Symbol(),SYMBOL_ASK)-offset*point;
request.tp = NormalizeDouble(price-offset*point,digits);
request.sl = NormalizeDouble(price+offset*point,digits);
request.price =NormalizeDouble(price,digits); // 正常开盘价
}
//--- 发送请求
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // 如果不能发送请求,输出错误代码
//--- 操作信息
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
//--- 归零请求和结果值
ZeroMemory(request);
ZeroMemory(result);
}
}
}
//+------------------------------------------------------------------+
|