一、首先我们来看下修改订单函数
- int OrderModify(ulong ticket,double tpp,double slp)
- {
- //--- 准备请求
- MqlTradeRequest request= {};
- request.action=TRADE_ACTION_SLTP; // 设置
- // request.symbol=symbol;
- // request.type = type;
- request.position =ticket;
- request.sl=slp; // 止损价
- request.tp=tpp; // 止盈价
- //--- 发送交易请求
- MqlTradeResult result= {0};
- bool res=OrderSend(request,result);
- if(!res)
- msgbox(GetLastError());
- return(0);
- }
复制代码
需要注意的是结构体request的元素action的值应该是TRADE_ACTION_SLTP
request.action=TRADE_ACTION_SLTP;
如何使用以上函数来修改订单的止损止盈呢?看例子,跟MT4很相似。
- for(int i=0; i<PositionsTotal(); i++)
- {
- ulong ticket=PositionGetTicket(i);
- string ordsymbol = PositionGetString(POSITION_SYMBOL);
- string ordcom = PositionGetString(POSITION_COMMENT);
- long ordmagic = PositionGetInteger(POSITION_MAGIC);
- double ordslp = PositionGetDouble(POSITION_SL);
- double ordtpp = PositionGetDouble(POSITION_TP);
- double ordoop = PositionGetDouble(POSITION_PRICE_OPEN);
- long ordtype = PositionGetInteger(POSITION_TYPE);
- double ordlot = PositionGetDouble(POSITION_VOLUME);
- double ordcp = PositionGetDouble(POSITION_PRICE_CURRENT);
- if(ordsymbol==Symbol() && PositionGetInteger(POSITION_MAGIC)==magic)
- {
- if(ordtype==POSITION_TYPE_BUY)
- {
- if(ordslp+ordtpp==0)
- {
- if(ordcp-ordoop>0)
- {
- ordslp=ordoop-2;
- }
- else
- {
- ordtpp=ordoop+2;
- }
- OrderModify(ticket,ordtpp,ordslp);
- }
- }
- if(ordtype==POSITION_TYPE_SELL)
- {
- if(ordslp+ordtpp==0)
- {
- if(ordcp-ordoop<0)
- {
- ordslp=ordoop+2;
- }
- else
- {
- ordtpp=ordoop-2;
- }
- OrderModify(ticket,ordtpp,ordslp);
- }
- }
- }
- }
复制代码
二、删除挂单则需要注意结构体request的元素action的值应该是TRADE_ACTION_REMOVE 删除挂单函数如下: - void OrderDelete(ulong ticket)
-
- {
-
- MqlTradeRequest request= {};
-
- request.action=TRADE_ACTION_REMOVE; // 设置
-
- request.order =ticket;
-
- //--- 发送交易请求
-
- MqlTradeResult result= {0};
-
- bool res=OrderSend(request,result);
-
- if(!res)
-
- msgbox(GetLastError());
-
- }
复制代码
使用的例子,还是跟MT4一样,需要用for循环遍历所有挂单 - ulong ticket=0;
-
- double open_price;
-
- double initial_volume;
-
- datetime time_setup;
-
- string symbol;
-
- string type;
-
- long order_magic;
-
-
- int bg=0;
-
- int sg=0;
-
- for(int i=0; i<OrdersTotal(); i++)
-
- {
-
- //--- 通过列表中的仓位返回订单报价
-
- ticket=OrderGetTicket(i);
-
- //--- 返回订单属性
-
- open_price =OrderGetDouble(ORDER_PRICE_OPEN);
-
- time_setup =(datetime)OrderGetInteger(ORDER_TIME_SETUP);
-
- symbol =OrderGetString(ORDER_SYMBOL);
-
- order_magic =OrderGetInteger(ORDER_MAGIC);
-
- // positionID =OrderGetInteger(ORDER_POSITION_ID);
-
- initial_volume=OrderGetDouble(ORDER_VOLUME_INITIAL);
-
- type =EnumToString(ENUM_ORDER_TYPE(OrderGetInteger(ORDER_TYPE)));
-
- string ordcom=OrderGetString(ORDER_COMMENT);
-
- if(symbol==Symbol() && order_magic==magic)
-
- {
-
- OrderDelete(ticket);
-
- }
-
-
-
-
- }
-
-
复制代码
|