通用自定义函数-平调所有持仓单(含挂单)
2012-03-21 12:53:50 来源: 作者:
void CloseAllOrder()//平掉所有仓位包括挂单
{
int tick[200];
PingFlag=0;
int j=0;
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{j=j+1;
tick[j]=OrderTicket();
Print("全部平仓! :",tick[j]);
}
}
if (j!=0)
{
for(i=1;i<=j;i++)
{
OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUY)
{
if(OrderClose(OrderTicket(),OrderLots(),Bid,滑点可接受范围)==false)
{
PingFlag=1;
Print("多头平仓失败"+GetLastError());
}
}
//OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUYSTOP)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("多头挂单撤销失败"+GetLastError());
}
}
//OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUYLIMIT)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("多头挂单撤销失败"+GetLastError());
}
}
if(OrderType()==OP_SELL)
{
if(OrderClose(OrderTicket(),OrderLots(),Ask,滑点可接受范围)==false)
{
PingFlag=1;
Print("空头平仓失败"+GetLastError());
}
}
if(OrderType()==OP_SELLSTOP)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("空头挂单撤销失败"+GetLastError());
}
}
if(OrderType()==OP_SELLLIMIT)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("空头挂单撤销失败"+GetLastError());
}
}
}
}
if (PingFlag==0) PingStop=1;
}