当使用 'OR' 运算符的多个条件时,在 MQL4 中非常快速地关闭和打开订单

Order closing and opening very quickly unintendedly in MQL4 when using multiple criteria with 'OR' operator

我正在尝试根据 2 种不同类型的标准平仓,

  1. 无论盈利多少,1020秒后平仓

  2. 如果我的导入值从 -1 变为 1,反之亦然,请关闭它而不考虑任何其他变量。

到目前为止,我的代码在 1 个条件下运行良好,但是当我添加 2 个 if 语句时,MT4 会非常快速地打开和关闭交易,每半秒内,它看起来不像它工作正常。

如果有人能告诉我是什么让它这样做,我将不胜感激。

我的代码如下,

//Global Variables
//int Value = ReadCSVFile and get cell value(Either -1 or 1)
int Ticket;
int TicketBuy;
datetime SwitchBuy
datetime SwitchSell

void OnTick()
  {
    //Open Trades based on Value from CSV and if No other buy orders open
    if(Value == 1){
    if(getBuyOrderCount(Symbol(),magic) < 1){
    Ticket == OrderSend(NULL,OP_BUY,0.01,Ask,2,0,0,NULL,magic,0,clrAliceBlue);
    SwitchBuy = TimeCurrent();
    }}

    if(Value == (-1)){
    if(SellOrderCount(Symbol(),magic) < 1){
     TicketSell = OrderSend(NULL,OP_SELL,0.01,Bid,2,0,0,NULL,magic,0,clrAliceBlue);
     SwitchSell = TimeCurrent();
    }}

    //Close Trades based on value in CSV file changing
    if(Ticket > 0){
     if(Value == (-1)||(TimeCurrent()>(SwitchBuy+60))){
    CloseAllTradesBuy();
     Ticket = 0;
    Alert("Buy Trade Closed Because Probabilities Changed to -1!");
      } 
    }                

    if(TicketSell > 0){
     if(Value == 1||(TimeCurrent()>(SwitchBuy+60))){
    CloseAllTradesSell();
    TicketSell = 0;
    Alert("Sell Trade Closed Because Probabilities Changed to 1!");
     }
    }

    //Second Independent Criteria
    if((SwitchBuy+1020) < TimeCurrent()){
     if(OrderType() == OP_BUY){
     CloseAllTradesBuy();
      }
    }
 
     if((SwitchSell+1020) < TimeCurrent()){
     if(OrderType() == OP_SELL){
     CloseAllTradesSell();
      }
    }

  }

//Functions to Close Trades
double CloseAllTradesBuy(){
 for (int i =OrdersTotal(); i>=0;i--)
 {
   if(OrderSelect(i,SELECT_BY_POS)==true)
   if(OrderType()==OP_BUY)
   if (OrderSymbol()==Symbol())
   {
   OrderClose(Ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed);
   }
 }
}

double CloseAllTradesSell(){
 for (int i =OrdersTotal(); i>=0;i--)
 {
   if(OrderSelect(i,SELECT_BY_POS)==true)
   if(OrderType()==OP_SELL)
   if (OrderSymbol()==Symbol())
   {
   OrderClose(TicketSell,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed);
   }
 }
}

更改关闭订单的代码:

    //Functions to Close Trades
double CloseAllTradesBuy(){
 for (int i =OrdersTotal()-1; i>=0;i--)
 {
   if(OrderSelect(i,SELECT_BY_POS) && OrderType()==OP_BUY && OrderSymbol()==Symbol())
   {
   OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed);
   }
 }
}

double CloseAllTradesSell(){
 for (int i =OrdersTotal()-1; i>=0;i--)
 {
   if(OrderSelect(i,SELECT_BY_POS) && OrderType()==OP_SELL && OrderSymbol()==Symbol())
   {
   OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed);
   }
 }
}