mql4algorithmic-tradingmetatrader4mt4

How can I try to place a pending order every X minutes till it's successfull?


From the research I've done, the stop level seems to prevent my pending order from being placed. I want to solve this by checking every X minutes if the pending order will go through till it has successfully been placed. How can I do this? I am placing my pending order in the following way:

double myEntryPrice=NormalizeDouble(Bid+(stopLevel*Point)+(bottom - 3*Point),Digits);
   int ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, myEntryPrice,0, stopLoss,dTakeProfit,"SellOrder",magicnumber,0,Red);


Solution

  • #include <Arrays\ArrayObj.mqh>
    class CPendingOrder : public CObject
       {
    public:
        int m_cmd;
        double m_lot;
        double m_osl;
        double m_otp;
    
        CPendingOrder(const int cmd,const double lot,const double osl,const double otp):
             m_cmd(cmd),m_lot(lot),m_osl(osl),m_otp(otp){}
       ~CPendingOrder(){}
       };
    
    CArrayObj *listOfPendingOrders;
    datetime lastPendingAttempt=0;
    
    int OnInit()
       {
        lastPendingAttempt=TimeCurrent();
        listOfPendingOrders=new CArrayObj();
        //---
        return (INIT_SUCCEDED);
       }
    void OnDeinit(const int reason){delete(listOfPendingOrders);}
    void OnTick()
       {
        checkPendingList();
        //other operations
    
        //adding a limit order to the list when you need to do that
        int cmd=OP_SELLLIMIT; 
        double lot=0.1, osl=0, otp=0;
        CPendingOrder *newOrder=new CPendingOrder(cmd,lot,osl,otp);
        listOfPendingOrders.Add(newOrder);
       }
    void checkPendingList()
       {
        if(iTime(_Symbol,PERIOD_M1,0)<=lastPendingAttempt)return;
        lastPendingAttempt=iTime(_Symbol,PERIOD_M1,0);
        double myEntryPrice=Bid;//set your formulae if needed here
        for(int i=listOfPendingOrders.Total()-1;i>=0;i--)
          {
           CPendingOrder *order=listOfPendingOrders.At(i);
           int ticket=OrderSend(_Symbol,order.m_cmd,order.m_lot,myEntryPrice,0,order.m_osl,order.m_otp,NULL,magicNumber,0);
           if(ticket>0)
                listOfPendingOrders.Delete(i);
          }
       }