请教删挂单代码的问题

 

高手们,谁能帮我修改下这个代码,我想让ea自动删除超过5分钟的挂单 ,用gpt写了一个代码,编译没错误,但不执行删 挂单

//+------------------------------------------------------------------+

//| DeleteExpiredOrdersEA.mq4                                       |

//+------------------------------------------------------------------+

input int orderExpirationTime = 5;  // 挂单超时时间(分钟)


//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

{

    return(INIT_SUCCEEDED);

}


//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

{

    DeleteExpiredOrders();

}


//+------------------------------------------------------------------+

//| Function to delete expired orders                                |

//+------------------------------------------------------------------+

void DeleteExpiredOrders()

{

    for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)

    {

        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderSymbol() == Symbol())

        {

            if ((OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP) && OrderOpenPrice() != 0)

            {

                datetime orderExpiration = OrderExpiration();

                datetime currentTime = TimeCurrent();

                int elapsedMinutes = (currentTime - orderExpiration) / 60;


                if (elapsedMinutes >= orderExpirationTime)

                {

                    Print("Deleting expired order: ", OrderType(), " OrderTicket: ", OrderTicket());


                    // 使用 OrderDelete 函数删除挂单

                    OrderDelete(OrderTicket());

                }

            }

        }

    }

}









或者是把下面这个代码修改一下也行,挂单时带好时间,超过时间没成交就取消挂单


//+------------------------------------------------------------------+

//| PlaceBuyStopOrdersScript.mq4                                   |

//|                        Generated by Bing AI                      |

//+------------------------------------------------------------------+


input double lotSize = 0.05;        // 手数

input int stopOrderDistance = 10;  // 挂单距离

input int stopLossDistance = 50;   // 止损设置距离

input int previousCandle = 1;      // 前一根K线


void OnStart() {

    PlaceBuyStopOrders();

}


void PlaceBuyStopOrders() {

    double previousHigh = iHigh(NULL, 0, previousCandle);


    // 计算挂Buy Stop订单的价格和止损水平

    double buyStopPrice = previousHigh + stopOrderDistance * Point;

    double stopLossBuy = buyStopPrice - stopLossDistance * Point;


    // 挂Buy Stop订单

    int buyStopTicket = OrderSend(Symbol(), OP_BUYSTOP, lotSize, buyStopPrice, 2, stopLossBuy, 0, "Buy Stop Order", 0, 1, clrNONE);


    // 检查是否成功挂单

    if (buyStopTicket > 0) {

        Print("Buy Stop order placed at price: ", buyStopPrice, " with stop loss at: ", stopLossBuy);

    } else {

        Print("Failed to place Buy Stop order");

    }

}

 
既然程式碼是GPT產生的,你可以再問一次GPT,我相信很快就能夠得到解決。
 
void DelOrder(int overtime)
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS))
         if(OrderType()>1)
            if((TimeCurrent()-OrderOpenTime())/60>=overtime)
               if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),60))
                  i=OrdersTotal();
               else
                  Print((string)OrderTicket()," Order Close fasle !! code ",(string)GetLastError());
     }
  }

overtime輸入分鐘數就可以使用

我沒測試 輸好代碼自己測試一下

 
Hong Yi Li #:
既然程式碼是GPT產生的,你可以再問一次GPT,我相信很快就能夠得到解決。
好的好的,非常感谢