为什么我的MQL4 EA代码不会将我的未结头寸改为盈亏平衡?

时间:2016-04-20 16:19:53

标签: trading algorithmic-trading mql4 metatrader4

尝试将添加止损给我的公开市场头寸,同时也考虑到我的经纪人。当我的交易获利100点时,我已经设定了这个以增加盈亏平衡止损。

这是代码 - 但它完全忽略了我在后面测试期间的任何订单修改。

好的,这就是我现在所拥有的,一个用于购买的循环,一个用于销售。我也宣布了“BuyMod”&正如前面的答案中所建议的那样,“SellMod”为true,以及OrderModify签名中的Normalzing价格。

 /*Breakeven Order Modification*/
bool BuyMod               =  true;
bool SellMod              =  true;
                    for(int b = OrdersTotal()-1;b>=0;b--)
                    {
                    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
                       {
                       double   aBidPrice   =  MarketInfo(Symbol(),MODE_BID);
                       double   anOpenPrice =  OrderOpenPrice();
                       double   aNewTpPrice =  OrderTakeProfit();
                       double   aCurrentSL  =  OrderStopLoss();
                       double   aNewSLPrice =  anOpenPrice;
                       double   pnlPoints   =  (aBidPrice - anOpenPrice)/_Point;
                       double   stopPoints  =  (aBidPrice - aNewSLPrice)/_Point;
                       int      stopLevel   =  int(MarketInfo(Symbol(),MODE_STOPLEVEL));
                       int      aTicket     =  OrderTicket();
                       if(OrderType() == OP_BUY)
                       if(stopPoints >= stopLevel)
                       if(aTicket > 0)
                       if(pnlPoints >= breakeven)
                       if(aNewSLPrice != aCurrentSL)
                          {
                          BuyMod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(aNewSLPrice,Digits),NormalizeDouble(aNewTpPrice,Digits),0,buycolor);
                          SendMail("Notification of Order Modification for Ticket#"+IntegerToString(OrderTicket(),10),"Good news! Order Ticket#"+IntegerToString(OrderTicket(),10)+"has been changed to breakeven");
                          }
                       }
                    }
                    for(int s = OrdersTotal()-1; s>=0; s--)
                    {
                    if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
                       {
                       double   anAskPrice  =  MarketInfo(Symbol(),MODE_ASK);
                       double   anOpenPrice =  OrderOpenPrice();
                       double   aNewTpPrice =  OrderTakeProfit();
                       double   aCurrentSL  =  OrderStopLoss();
                       double   aNewSLPrice =  anOpenPrice;
                       double   pnlPoints   =  (anOpenPrice - anAskPrice)/_Point;
                       double   stopPoints  =  (aNewSLPrice - anAskPrice)/_Point;
                       int      stopLevel   =  int(MarketInfo(Symbol(),MODE_STOPLEVEL));
                       int      aTicket     =  OrderTicket();
                       if(OrderType()== OP_SELL)
                       if(stopPoints >= stopLevel)
                       if(pnlPoints >= breakeven)
                       if(aNewSLPrice != aCurrentSL)
                       if(aTicket > 0)
                          {
                          SellMod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(aNewSLPrice,Digits),NormalizeDouble(aNewTpPrice,Digits),0,sellcolor);
                          SendMail("Notification of Order Modification for Ticket#"+IntegerToString(OrderTicket(),10),"Good news! Order Ticket#"+IntegerToString(OrderTicket(),10)+"has been changed to breakeven");
                          }
                       }
                    }

1 个答案:

答案 0 :(得分:0)

由于以下几个原因,此代码无效:

First:
OrderModify() 的函数调用签名是错误的。

您可能想知道,OrderModify()呼叫签名要求根据MQL4文档在呼叫中包含OrderExpiration值,即使订单当前不再是待处理订单。

查看文档和IDE工具提示,这有助于提醒函数调用参数的顺序。

Second:
GetLastError()函数调用返回 {后,OrderModify()可以报告其他不太明显的原因{1}} 表示失败。