1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. 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. Ahmad Khan

    Ahmad Khan Well-Known Member Credit Hunter

    Equity
    Credit
    Ref Point
    Telegram @ AhmadKhanEA

    ----------------------------------------------- -----------------------------

    10 ACCOUNT DETAILS

    ------------------ -------------------------------------------------- --------

    1- RSI ROBOT
    ACCOUNT # 32425112 PASSWORD japan123 SERVER OctaFX-Demo

    2- CCI ROBOT
    ACCOUNT # 32425128 PASSWORD japan123 SERVER OctaFX-Demo

    3- MA ROBOT
    ACCOUNT # 32425146 PASSWORD japan123 SERVER OctaFX-Demo

    4- MACD ROBOT
    ACCOUNT # 32610110 PASSWORD japan123 SERVER OctaFX-Demo

    5- STOCHASTIC ROBOT
    ACCOUNT # 32616331 PASSWORD japan123 SERVER OctaFX-Demo

    6- MASTER ROBOT
    ACCOUNT # 32708622 PASSWORD japan123 SERVER OctaFX-Demo

    7- CRUISE ROBOT
    ACCOUNT # 32708639 PASSWORD japan123 SERVER OctaFX-Demo

    8- TREND ROBOT
    ACCOUNT # 32708654 PASSWORD japan123 SERVER OctaFX-Demo

    9- BUY SELL ROBOT
    ACCOUNT # 32709027 PASSWORD japan123 SERVER OctaFX-Demo

    10- BULLS BEARS ROBOT
    ACCOUNT # 32709372 PASSWORD japan123 SERVER OctaFX-Demo
     
  3. SamIdub

    SamIdub New Member

    Equity
    Credit
    Ref Point
    Kalau script close all Order by symbol ada brow?
     
  4. 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");
                  }
               }
            }
         }
      }
    
     
  5. 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
     
  6. Sudaeng FX

    Sudaeng FX New Member

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

Share This Page