1. Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)
    Dismiss Notice

Question Close Pending Order Yang Gagal di Eksekusi

Discussion in 'Belajar MQL dari Nol' started by unmetered.co.id, 22 Oct 2017.

  1. unmetered.co.id

    unmetered.co.id New Member

    Equity
    Credit
    Ref Point
    Salam Profit untuk semua.....
    Saya mau tanya..., Pertanyaan saya ini mungkin sudah pernah di bahas di thread forum ini, namun kami juga perlu tanya lagi, sebab kode robot EA mengalami kegagalan ekseskusi Pending Order ( PO ).
    Pertanyaannya :
    1. Ketika salahsatu dari dari 2 pending order tereksekusi, bagaimana caranya agar 1 PO lainnya juga ikut tertutup.
    Sebab kode robot di bawah ini, jika Pending Order Sell yang tereksekusi, maka Pending Order Buy, juga ikut tersekusi. Anehnya, jika Pending Order Buy tereksekusi, Pending Order Sell tidak tereksekusi.

    2. Setelah profit dan sudah tertutup semua open posisi, kenapa robot EA ini tidak mau melakukan Pending Order lagi ? Tolong siapa saja yang bisa memperbaikinya, kami ucapkan terima kasih....

    Code:
    //+------------------------------------------------------------------+
    //|                                             Simple_EaPending.mq4 |
    //|                        Copyright 2012, MetaQuotes Software Corp. |
    //|                                                   create by 4605 |
    //+------------------------------------------------------------------+
    
    
    extern   int      TakeProfit           = 50;
    extern   int      PipStep              = 30;
    extern   double   Lots                 = 0.3;
    extern   int      LockProfit           = 15;
    extern   int      Magic                = 4605;
    extern   bool     UseTimeFilter        = true;
    extern   int      HourStart            = 18;
    extern   int      HourEnd              = 24;
    
    
    
    double lotx;
    double point;
    int iTrade = 0;
    int buy,sell;
    
    int init()
      {
    //----
    if (Digits==3 || Digits==5)
          {point=Point*10;}
       else                     
          {point=Point;}
    
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    //| expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    int deinit()
      {
    //----
       
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    //| expert start function                                            |
    //+------------------------------------------------------------------+
    int start()
      {
    if(AccountBalance()+profit_daily()>AccountBalance())
         {
           ClosePending();
           Comment("target tercapai");
            return(0);
         }
    //-----------Start EA----------//
    
    if(UseTimeFilter)
    {
    if((Hour() >= HourStart && Hour() <= HourEnd))
       {
       if(OrdersTotal()==0)
          {       
           OrderSend(Symbol(),OP_BUYSTOP ,Lots,Ask+PipStep*point,0,0,Ask+PipStep*point+TakeProfit*point,"4605",Magic,0,Green);
           OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-PipStep*point,0,0,Bid-PipStep*point-TakeProfit*point,"4605",Magic,0,Red); 
           }
       }
    }else
        {
         if(OrdersTotal()==0)
          {       
           OrderSend(Symbol(),OP_BUYSTOP ,Lots,Ask+PipStep*point,0,0,Ask+PipStep*point+TakeProfit*point,"4605",Magic,0,Green);
           OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-PipStep*point,0,0,Bid-PipStep*point-TakeProfit*point,"4605",Magic,0,Red); 
           }
        }
         
       for(int i = 0; i < OrdersTotal(); i++)
          {
             if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))continue; 
              if (OrderSymbol()!=Symbol() && OrderMagicNumber()!=Magic){ continue;}                                                   
              if (OrderType()== OP_BUY)
              {
              buy++;
              }
                                                               
              if (OrderType()== OP_SELL)
              {
               sell++;
              }
          }       
       if(buy>0){ClosePending();buy=0;}
       if(sell>0){ClosePending(); sell=0;}
       DO_LockSL();
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    
    void DO_LockSL()
    {
    for(iTrade=0; iTrade<OrdersTotal(); iTrade++)
       {
          if(OrderSelect(iTrade, SELECT_BY_POS, MODE_TRADES))continue;
           if(OrderSymbol()!=Symbol()) continue;
           
           if(OrderType() == OP_BUY && OrderMagicNumber() == Magic)
           {
          if (Bid-OrderOpenPrice() > LockProfit*Point&& OrderStopLoss()<Bid-Point*LockProfit)
                   {
                        OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+1*Point,OrderTakeProfit(),0);
                               
                   }
           if(Bid-OrderStopLoss()>LockProfit*Point && OrderStopLoss()<Bid-Point*LockProfit)
                         {
                            OrderModify(OrderTicket(),OrderOpenPrice(),Bid-LockProfit*Point,OrderTakeProfit(),0);                         
                         }       
           }
           if(OrderType() == OP_SELL && OrderMagicNumber() == Magic)
           {
                 
           if (OrderOpenPrice()-Ask > LockProfit*Point&& OrderStopLoss()<Ask-Point*LockProfit)
                   {     
                        OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-1*Point,OrderTakeProfit(),0);   
                   }
           if(OrderStopLoss()-Ask>LockProfit*Point && OrderStopLoss()<Ask-Point*LockProfit)
                         {
                            OrderModify(OrderTicket(),OrderOpenPrice(),Ask+LockProfit*Point,OrderTakeProfit(),0);                     
                         }       
           }
       }
    } 
    
    void ClosePending()
      {
       for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
          {
          if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))continue;
          if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
          if (OrderType() > OP_SELL && OrderType() > OP_BUY) {
          bool result=false;
          result=OrderDelete(OrderTicket());}
          }
        return (0);
      }
     
    double profit_daily()
    {
       int Now_day = Day();
       double profit = 0;
       for (int count = 0; count < OrdersHistoryTotal(); count++)
        {
          if(OrderSelect(count, SELECT_BY_POS, MODE_HISTORY))continue;
          if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
          if (TimeDay(OrderOpenTime()) == Now_day) profit += OrderProfit();
       }
       return (profit);
    }
    
     
  2. Niguru

    Niguru Member Credit Hunter

    Equity
    Credit
    Ref Point
    Coba ganti kode ini:

    upload_2024-6-9_15-30-12.png

    dengan:

    Code:
    if (OrderType() == OP_SELL || OrderType()== OP_BUY) {
    Semoga bisa menjadi solusi. Happy coding. Have a nice day :)
     
  3. MrFakrul95<>

    MrFakrul95<> New Member

    Equity
    Credit
    Ref Point
    udah di ganti om, masih sama,
     
  4. Niguru

    Niguru Member Credit Hunter

    Equity
    Credit
    Ref Point
    Oh iya pasti sama
    Ada kekeliruan di instruksi penutupan transaksi di atas.
    Kalau untuk menghapus ORDER (pending order) bukan pakai OP_BUY atau OP_SELL , tapi pakai ORDER_TYPE_BUY_STOP, ORDER_TYPE_SELL_STOP, ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT (tergantung jenis order apa yang mau dihapus).

    OP_BUY & OP_SELL diganti aja dengan tipe order yang sesuai.
    OP_BUY & OP_SELL digunakan untuk menghapus OP (open position) bukan order.
    Perintah untuk menghapus OP adalah OrderClose bukan OrderDelete.
    Instruksi OrderClose berikut parameternya adalah: OrderClose(ticket,lots,price,slippage,arrow_color);

    Jadi mau melakukan penutupan yang mana? OP atau Order?
    Solusi untuk kedua pilihan sudah saya jelaskan di atas.


    OK selamat mencoba. Happy coding, have a nice day :)

     

Share This Page