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

Tutorial MACD Close Order MQL4

Discussion in 'Belajar MQL dari Nol' started by Hayrman, 16 Oct 2020 at 02:50.

  1. Hayrman

    Hayrman New Member

    Equity
    Credit
    Ref Point
    Hello All,

    Im totally new in MQL4 coding and really need help. Once i Open Buy and Sell, how do i code to close-order if

    1. MACD Main is lesser than MACD Signal Line

    The code for OPEN and SELL as below.


    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    void OpenBuy()
    {
    // Open Buy Order
    int ticket=OrderSend(_Symbol,OP_BUY,LotSize,Ask,MySlippage,0,0,"BUY",MagicNumber);

    if(ticket<0) Print("Buy Order Send failed with error #",GetLastError());
    else Print("Buy Order placed successfully");

    // Modify Buy Order
    bool res=OrderModify(ticket,OrderOpenPrice(),Ask-StopLoss*MyPoint,Ask+TakeProfit*MyPoint,0);

    if(!res) Print("Error in OrderModify. Error code=",GetLastError());
    else Print("Order modified successfully.");
    }
    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    void OpenSell()
    {
    //Open Sell Order
    int ticket=OrderSend(_Symbol,OP_SELL,LotSize,Bid,MySlippage,0,0,"SELL",MagicNumber);

    if(ticket<0) Print("Sell Order Send failed with error #",GetLastError());
    else Print("Sell Order placed successfully");

    // Modify Sell Order
    bool res=OrderModify(ticket,OrderOpenPrice(),Bid+StopLoss*MyPoint,Bid-TakeProfit*MyPoint,0);

    if(!res) Print("Error in OrderModify. Error code=",GetLastError());
    else Print("Order modified successfully.");
    }
    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+

    Many Thanks in Advance. :)
     
  2. Onyx MIB

    Onyx MIB Member Credit Hunter

    Equity
    Credit
    Ref Point
    Code:
    int cekSignal(){
       int signal = -1;
       //your code to find a signal according to your rule
       
       if (...){
          signal   = OP_BUY;
       }else if (...){
          signal   = OP_SELL;
       }
       
       return (signal);
    }
    
    void closeOrder(int orderType){
       //buat fungsi utk close order
       
       Print ("Close Order Done!");
    }
    
    void OnTick(){
    
       static int lastSignal = -1;
       int signal  = cekSignal();
       if (signal != lastSignal){
          if (signal == OP_BUY){
             lastSignal = signal; 
             Print ("Signal BUY");
             closeOrder (OP_SELL);
          }else if (signal == OP_SELL){
             lastSignal = signal;
             Print ("Signal SELL");
             closeOrder (OP_BUY);
          }
       }
    
    }
    
    
    
     

Share This Page