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

Close All Orders

Discussion in 'Belajar MQL dari Nol' started by Onyx MIB, 31 Mar 2021.

  1. Onyx MIB

    Onyx MIB Member

    Equity
    Credit
    Ref Point
    Hello semua,

    Berikut saya share code EA untuk close semua order saat mencapai target tertentu atau floating loss.
    Bisa dipadukan dengan magicNumber untuk filter order.
    EA ini bisa menjadi alat bantu untuk melengkapi EA Anda yang tidak memiliki fitur close berdasarkan target atau harus cutloss dengan batas loss tertentu.

    Code:
    //+------------------------------------------------------------------+
    //|                                                  CloseOrders.mq4 |
    //|                                     Copyright 2021, Signal Forex |
    //|                                           https://signalforex.id |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2021, Signal Forex"
    #property link      "https://signalforex.id"
    #property version   "1.00"
    #property strict
    #property description   "Group sharing t.me/codeMQL"
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    extern    double         inTargetProfitMoney     = 10;       //Target Profit ($)
    extern    double         inCutLossMoney          = 0.0;      //Cut Loss ($)
    extern    int            inMagicNumber           = 0;        //Magic Number
    
    int      slippage    = 3;
    
    //+------------------------------------------------------------------+
    //| Expert initialization function                                   |
    //+------------------------------------------------------------------+
    int OnInit()
      {
    //---
       if(inTargetProfitMoney <= 0)
         {
          Alert("Invalid input");
          return(INIT_PARAMETERS_INCORRECT);
         }
    
       inCutLossMoney = MathAbs(inCutLossMoney) * -1;
    
    //---
       return(INIT_SUCCEEDED);
      }
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
      {
    //---
       Print("Thank you for using this EA");
      }
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
      {
    //---
    
       double   tFloating = 0.0;
       int tOrder  = OrdersTotal();
       for(int i=tOrder-1; i>=0; i--)
         {
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
             if(OrderMagicNumber() == inMagicNumber)
               {
                tFloating   += OrderProfit()+OrderCommission() + OrderSwap();
               }
            }
         }
    
       if(tFloating >= inTargetProfitMoney || (tFloating <= inCutLossMoney && inCutLossMoney < 0))
         {
          fCloseAllOrders();
         }
    
      }
    //+------------------------------------------------------------------+
    
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    void fCloseAllOrders()
      {
       double   priceClose = 0.0;
       int tOrders = OrdersTotal();
       for(int i=tOrders-1; i>=0; i--)
         {
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
             if(OrderMagicNumber() == inMagicNumber && (OrderType() == OP_BUY || OrderType() == OP_SELL))
               {
                priceClose  = (OrderType()==OP_BUY)?Bid:Ask;
                if(!OrderClose(OrderTicket(), OrderLots(), priceClose, slippage, clrGold))
                  {
                   Print("WARNING: Close Failed");
                  }
               }
            }
         }
      }
    //+------------------------------------------------------------------+
    
    
     

    Attached Files:

  2. SamIdub

    SamIdub New Member

    Equity
    Credit
    Ref Point
    Kalau script close all Order by symbol ada brow?
     
  3. Onyx MIB

    Onyx MIB Member

    Equity
    Credit
    Ref Point
    udah dikit aja modul fCloseAllOrders() nya.
    Tinggal di copy aja contoh code dibawah ini.

    Code:
    void fCloseAllOrders(string pair=all)
      {
       double   priceClose = 0.0;
       int tOrders = OrdersTotal();
       for(int i=tOrders-1; i>=0; i--)
         {
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
             if(OrderMagicNumber() == inMagicNumber && (OrderType() == OP_BUY || OrderType() == OP_SELL) && (OrderSymbol()== pair || pair == "all"))
               {
                priceClose  = (OrderType()==OP_BUY)?Bid:Ask;
                if(!OrderClose(OrderTicket(), OrderLots(), priceClose, slippage, clrGold))
                  {
                   Print("WARNING: Close Failed");
                  }
               }
            }
         }
      }
    
     
  4. Onyx MIB

    Onyx MIB Member

    Equity
    Credit
    Ref Point
    buat yang ingin ikutan belajar di group telegram, join aja di t.me/codeMQL
    Dan kalau ingin ikut pelatihan khusus via zoom, join kami di : https://robotop.id/bfh
    dan sekaligus juga kami perkenalkan broker yang enak untuk trading dengan robot : https://xsocioid.com/sf
     
  5. Sudaeng FX

    Sudaeng FX New Member

    Equity
    Credit
    Ref Point
    Scriptnya donk untuk menutup semua order berdasarkan waktu? Mksh
     
  6. Aryoboy

    Aryoboy New Member

    Equity
    Credit
    Ref Point
    Save codenya gan, terimakasih
     
  7. Ardian Fariz

    Ardian Fariz New Member

    Equity
    Credit
    Ref Point
    mungkin bisa dicoba juga pakai script ini

    Code:
    //+------------------------------------------------------------------+
    //|                                              CloseAllOrders.mq4  |
    //|                                  Script to close all orders       |
    //|                                           Copyright 2023, Ardian Fariz|
    //|                                      https://linktr.ee/officialfariz |
    //+------------------------------------------------------------------+
    
    // Pengaturan
    input bool closeProfitOrders = true;     // Menutup semua posisi yang menghasilkan profit
    input bool closeAllOrders = true;        // Menutup semua posisi
    input bool closeLimitOrders = true;      // Menutup semua limit orders
    input bool closePendingOrders = true;    // Menutup semua pending orders
    
    //+------------------------------------------------------------------+
    //| Script program start function                                    |
    //+------------------------------------------------------------------+
    void OnStart()
    {
       // Menutup semua posisi yang menghasilkan profit
       if (closeProfitOrders)
       {
          CloseProfitOrders();
       }
    
       // Menutup semua posisi
       if (closeAllOrders)
       {
          CloseAllOrders();
       }
    
       // Menutup semua limit orders
       if (closeLimitOrders)
       {
          CloseLimitOrders();
       }
    
       // Menutup semua pending orders
       if (closePendingOrders)
       {
          ClosePendingOrders();
       }
    }
    
    //+------------------------------------------------------------------+
    //| Function to close all positions with profit                      |
    //+------------------------------------------------------------------+
    void CloseProfitOrders()
    {
       int total = OrdersTotal();
       
       for (int i = total - 1; i >= 0; i--)
       {
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          {
             if (OrderProfit() > 0)
             {
                OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, clrGreen);
             }
          }
       }
    }
    
    //+------------------------------------------------------------------+
    //| Function to close all positions                                   |
    //+------------------------------------------------------------------+
    void CloseAllOrders()
    {
       int total = OrdersTotal();
       
       for (int i = total - 1; i >= 0; i--)
       {
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          {
             OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, clrRed);
          }
       }
    }
    
    //+------------------------------------------------------------------+
    //| Function to close all limit orders                                |
    //+------------------------------------------------------------------+
    void CloseLimitOrders()
    {
       int total = OrdersTotal();
       
       for (int i = total - 1; i >= 0; i--)
       {
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          {
             if (OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT)
             {
                OrderDelete(OrderTicket());
             }
          }
       }
    }
    
    //+------------------------------------------------------------------+
    //| Function to close all pending orders                              |
    //+------------------------------------------------------------------+
    void ClosePendingOrders()
    {
       int total = OrdersTotal();
       
       for (int i = total - 1; i >= 0; i--)
       {
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          {
             if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP)
             {
                OrderDelete(OrderTicket());
             }
          }
       }
    }
    
     

Share This Page