//+------------------------------------------------------------------+
//| 一键平仓含挂单全商品版脚本.mq4 |
//| Copyright 2015, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
int tick[200]={-1};
int pingFlag=0,slipPage=3;
int j=0,i;
for(i=0;i
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
j++;
tick[j]=OrderTicket();
Print("全部平仓:",tick[j]);
}
else{Print("订单选择失败:",GetLastError());}
}
if(j!=0) //如果有持仓
{
for(i=1;i<=j;i++)
{
int ticket=tick[i];
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{
int cmd=OrderType();
if(cmd==OP_BUY)
{
if(OrderClose(ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),slipPage)==false)
{pingFlag=1;Print("多头平仓失败:",GetLastError()," 订单号:",ticket);}
}
else if(cmd==OP_SELL)
{
if(OrderClose(ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),slipPage)==false)
{pingFlag=1;Print("空头平仓失败:",GetLastError()," 订单号:",ticket);}
}
else if(cmd==OP_BUYLIMIT)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{pingFlag=1;Print("多头Limit挂单撤销失败:",GetLastError()," 订单号:",ticket);}
}
else if(cmd==OP_SELLLIMIT)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{pingFlag=1;Print("空头Limit挂单撤销失败:",GetLastError()," 订单号:",ticket);}
}
else if(cmd==OP_BUYSTOP)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{pingFlag=1;Print("多头Stop挂单撤销失败:",GetLastError()," 订单号:",ticket);}
}
else if(cmd==OP_SELLSTOP)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{pingFlag=1;Print("空头Stop挂单撤销失败:",GetLastError()," 订单号:",ticket);}
}
}
else
{Print("选择订单失败:",GetLastError()," 订单号:",ticket);}
}//for
}//if
if(pingFlag==0)
{Alert("平仓成功");}
else
{Alert("平仓失败,再来一次");}
}
//+------------------------------------------------------------------+