【问题标题】:MQL4 why I am getting error 130 on OrderModify as my SL should be perfect?MQL4 为什么我在 OrderModify 上收到错误 130,因为我的 SL 应该是完美的?
【发布时间】:2021-06-13 19:17:53
【问题描述】:

谁能告诉我为什么 SL 没有移动,我收到错误 130(ERR_INVALID_STOPS,无效止损)并且我运行了 Print("Minimum Stop Level=", minstoplevel, " points" );在我的代码中并得到“10”并尝试运行代码,即使是 11 并且仍然存在相同的错误。

有人可以看看并告诉我,我几乎没有任何头发可以尝试所有的可能性。

非常感谢

...

//+------------------------------------------------------------------+
//|                                                 Emotality EA.mq4 |
//|                                                    Daniel Fourie |
//|                                       https://www.emotality.com/ |
//+------------------------------------------------------------------+
#property copyright "Daniel Fourie"
#property link "https://www.emotality.com/"
#property version "1.00"
#property strict
#property script_show_inputs

extern int takeProfitPoints = 100;
extern int maxPipStopLoss = 11;
extern double volumeSize = 0.01;
extern double maxSlippageAllowed = 0.1;

int orderID;
int magicNB = 1337;
int trailingStop = 110;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
  //---

  //---
  return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
  //---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
  // Create an empty string for the signal
  string signal = "";

  // Calculate the two Fractal values
  double UpperFractalsValue = iFractals(NULL, PERIOD_M5, MODE_UPPER, 2);
  double LowerFractalsValue = iFractals(NULL, PERIOD_M5, MODE_LOWER, 2);

  // Buy Signal

  //When it is going up
  if (LowerFractalsValue != 0) {
    if (LowerFractalsValue < Low[1]) {
      signal = "buy";
    }
  }

  //When it is going down
  if (UpperFractalsValue != 0) {
    if (UpperFractalsValue > High[1]) {
      signal = "sell";
    }
  }

  // If we have a buy signal and no positions
  if (signal == "buy" && CheckIfOpenOrdersByMagicNB(magicNB) == false) //need to see if the argument is correct that new orders will open if other ones is open
    //Send a buy order
    int orderID = OrderSend(_Symbol, OP_BUY, volumeSize, Ask, maxSlippageAllowed, Ask - maxPipStopLoss, Ask + takeProfitPoints, NULL, magicNB);

  int openOrders = OrdersTotal();

  for (int i = 0; i < openOrders; i++) {
    if (OrderSelect(i, SELECT_BY_POS) == true) {
      if (OrderMagicNumber() == magicNB)
        if (OrderType() == 0) {
          if (UpperFractalsValue != 0) {
            OrderClose(OrderTicket(), volumeSize, Ask, 5);
          }
        }
      }
    }
    
  int newSl = Bid - NormalizeDouble(trailingStop * Point,Digits);
  Print(newSl);
  for (int i = 0; i < openOrders; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderMagicNumber() == magicNB) {
        if (OrderType() == 0) {
         if(Bid - OrderOpenPrice() > trailingStop * Point && OrderStopLoss() < Bid - trailingStop * Point){
            if(newSl < Bid) {
            OrderModify(OrderTicket(), OrderOpenPrice(),newSl , takeProfitPoints, 0);  
            }
          }
        }
      }
    }
  }
  
  // If we have a sell signal and no positions
  if (signal == "sell" && CheckIfOpenOrdersByMagicNB(magicNB) == false) //need to see if the argument is correct that new orders will open if other ones is open
    //Send a sell order
    int orderID = OrderSend(_Symbol, OP_SELL, volumeSize, Ask, maxSlippageAllowed, Bid + maxPipStopLoss, Bid - takeProfitPoints, NULL, magicNB);

  for (int i = 0; i < openOrders; i++) {
    if (OrderSelect(i, SELECT_BY_POS) == true) {
      if (OrderMagicNumber() == magicNB)
        if (OrderType() == 1) {
          if (LowerFractalsValue != 0) {
            OrderClose(OrderTicket(), volumeSize, Ask, 5);
          }
        }

    }
  }

  Comment(
    "Upper Fractals Value: ", UpperFractalsValue, "\n",
    "Lower Fractals Value: ", LowerFractalsValue, "\n",
    "The current signal is:", signal
  );

}

//+------------------------------------------------------------------+
   bool CheckIfOpenOrdersByMagicNB(int magicNM)
{
   int openOrders = OrdersTotal();
   
   for(int i = 0; i < openOrders; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS)==true)
      {
         if(OrderMagicNumber() == magicNB)
         {
            return true;
         }
      }
   }
   return false;
}
//+------------------------------------------------------------------+
 
...

【问题讨论】:

    标签: mql4


    【解决方案1】:

    您的 newSL 变量应该是 double 类型,而不是 int

    此外,当您发送类型为OP_SELL 的订单时,请使用Bid 价格而不是Ask

    Docs:

    市价单开盘时(OP_SELL 或 OP_BUY),只有最新的 Bid(卖出)或 Ask(买入)的价格可以作为开盘价使用 价格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多