- //==============================================================================================================================
- // 平仓
- bool OrderClose(ENUM_POSITION_TYPE type, ulong P_ticket, double Lot)
- {
- //--- 声明并初始化交易请求和交易请求结果
- MqlTradeRequest request={0};
- MqlTradeResult result={0};
- //--- 设置操作参数
- request.action =TRADE_ACTION_DEAL; // 操作类型为平仓
- request.position =P_ticket; // 持仓单号
- request.symbol =Symbol();
- request.volume =Lot;
- request.deviation =30;
- request.type_filling =ORDER_FILLING_IOC; // IOC如果无法执行全部订单交易量,那么剩下的交易量将被取消。而RETURN只用于挂单
- request.magic =Magic;
- //--- 根据持仓类型设置平仓价格和类型
- if(type==POSITION_TYPE_BUY)
- {
- request.price =SymbolInfoDouble(Symbol(),SYMBOL_BID);
- request.type =ORDER_TYPE_SELL;
- }
- else
- {
- request.price =SymbolInfoDouble(Symbol(),SYMBOL_ASK);
- request.type =ORDER_TYPE_BUY;
- }
- //--- 发送请求
- bool eg=false;
- eg=OrderSend(request,result);
- if(!eg) PrintFormat("OrderSend error %d",GetLastError());
- return(true);
- }
- //=======================================================================
复制代码
|