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

Suggestion bantu untuk perbaiki simple hedging

Discussion in 'Belajar MQL dari Nol' started by putra suwira, 01 Dec 2020.

  1. putra suwira

    putra suwira New Member

    Equity
    Credit
    Ref Point
    Salam,
    saya coba untuk membuat hedging sederhana. Mohon dibantu untuk koreksi bagian mana yang salah sehingga bisa belajar
    makasi
     

    Attached Files:

  2. SoeHoe

    SoeHoe Administrator Moderator

    Equity
    Credit
    Ref Point
    Kalau bertanya mengenai script atau kode MQL, sebainya langsung copy-paste scriptnya ke sini, jangan filenya, agar mudah ditanggapi.
     
  3. putra suwira

    putra suwira New Member

    Equity
    Credit
    Ref Point
    berikut bang codenya

    Code:
    input  int    MagicNumber     =135;
    input  string EAComment       ="Simple Hedging";
    input  color  Warna_Buy       =clrBlue;
    input  color  Warna_Sell      =clrRed;
    extern double Lot_exponent1   =3;
    extern double Lot_exponent2   =2;
    extern int    PipStep         =30;
    extern int    MaxHedging      =6;
    extern double myLots1         =0.02;
    extern int    TimeFrame       =PERIOD_M1;
    double myOP    =OrderOpenPrice(); //OP=order price
    double myLots2 =NormalizeDouble((OrderLots()*Lot_exponent1),2);
    double myOPs   =OrderOpenPrice()-PipStep;
    double myOPb   =OrderOpenPrice()+PipStep;
    double myTPs   =OrderOpenPrice()-(PipStep*Lot_exponent2); //take profit level
    double myTPb   =OrderOpenPrice()+(PipStep*Lot_exponent2);
    double myLots  =NormalizeDouble((OrderLots()*Lot_exponent2),2);
    //====================================
    //check jumlah order,
    //jika ada order yang masih terbuka maka biarkan sampai close
    //jika Totalorder=0 maka check posisi candle terakhir di timeframe M1
    //jika candle bulish maka buka posisi(1) buy 0.1 lots, TP 30 pips
    //jika candle bearish maka buka posisi(1) sell 0.1 lots TP 30 pips
    //jika profit negatif (-30 pips) buka posisi(2) berlawanan dengan posisi(1) sebesar (3x =0.3 lots), TP 30 pips
    //jika profit posisi(2) negatif (-30 pips) buka posisi(3) berlawanan dengan posisi(2) sebesar (2X =0.6 lots), TP 30 pips
    //jika profit posisi(3) negatif (-30 pips) buka posisi(4) berlawanan dengan posisi(3) sebesar (2X =1.2 lots), TP 30 pips
    //jika profit posisi(4) negatif (-30 pips) buka posisi(5) berlawanan dengan posisi(4) sebesar (2X =2.4 lots), TP 30 pips
    //jika profit posisi(5) negatif (-30 pips) buka posisi(6) berlawanan dengan posisi(5) sebesar (2X =4.8 lots), TP 30 pips
    //dan seterusnya sampai maksimal totalorder =6
    //====================================
    int QnPips(double n1, double n2){
       //sekumpulan perintah
       //double n1 =High[3];
       //double n2 =Low[3];
       int result = int(MathRound((n1-n2)/Point()));
    return (result);}
    //====================================
    //menghitung total order selain pending order
    int QnTotalOrder1(){
       int result=0;
       for(int i=OrdersTotal()-1; i>=0; i--){
       if(!OrderSelect(i,SELECT_BY_POS)
          ||OrderSymbol()!=Symbol()
          ||OrderType()>1//order type buy=0, order type sell=1, order type pending >1
          ||OrderMagicNumber()!=MagicNumber)  continue;
       result++;
       }
    return(result);}
    //====================================
    //menghitung total pending order
    int QnTotalOrder2(){
       int result=0;
       for(int i=OrdersTotal()-1; i>=0; i--){
       if(!OrderSelect(i,SELECT_BY_POS)
          ||OrderSymbol()!=Symbol()
          ||OrderType()<=1//order type buy=0, order type sell=1, order type pending >1
          ||OrderMagicNumber()!=MagicNumber)  continue;
       result++;
       }
    return(result);}
    //====================================
    //menentukan posisi candle "0" bulish or bearish
    // order buy jika bulish, order sell jika bearish
    int QnOrder1(int shift=0){
       int result =0;
       if (Open[shift]<Close[shift]){ //bulish;
          int ticket =OrderSend(Symbol(),OP_BUY,myLots1,myOP,0,myTPs,myOPb,EAComment,MagicNumber);
       }
       if (Open[shift]>=Close[shift]){ //bearish;
          int ticket =OrderSend(Symbol(),OP_SELL,myLots1,myOP,0,myTPb,myOPs,EAComment,MagicNumber);
       }
    return (result);}
    //====================================
    //menentukan posisi order apakah ada atau tidak
    int QnOrder2(){
       int result =0;
       for (int i=OrdersTotal()-1; i>=0; i--){ //i>=0 total order hanya 1.
          if ( !OrderSelect(i, SELECT_BY_POS)
             || OrderSymbol()!=Symbol()
             || OrderType()>1 //order type buy=0, order type sell=1, order type pending >1
             || OrderMagicNumber()!=MagicNumber) continue; // continue = lanjut ke urutan berikutnya
          //yang pertama dicheck di sini pasti order yang dibuat terakhir
          if(OrderProfit()>0) break;
          //hitung minusnya
          int profit =0;
          if(OrderType()==OP_BUY){
             profit = QnPips(OrderClosePrice(),OrderOpenPrice());
          }else {
             profit = QnPips(OrderOpenPrice(),OrderClosePrice());   
          }
          if(profit<=-(PipStep/2)){
             //buat pending order, triple lots, ordertype berlawanan   
             QnPendingOrder1();
          }
          break;
       }
    return(result);}
    //==========
    int QnPendingOrder1(){
       int result=0;
       int order1 =OrderSelect(0, SELECT_BY_POS);
       if (OrderType()==OP_BUY){ //order type buy=0, order type sell=1, order type pending >1;
          int ticket =OrderSend(Symbol(),OP_SELLLIMIT,myLots2,myOPs,0,myTPb,myTPs,EAComment,MagicNumber);
       }
       if (OrderType()==OP_SELL){ //order type buy=0, order type sell=1, order type pending >1
          int ticket =OrderSend(Symbol(),OP_BUYLIMIT,myLots2,myOPb,0,myTPs,myTPb,EAComment,MagicNumber);
       }
    return(result);}
    //====================================
    int QnOrder3(){
       int result =0;
       for (int i=OrdersTotal()-1; i>=0; i--){ //i>=0 total order hanya 1.
          if ( !OrderSelect(i, SELECT_BY_POS)
             || OrderSymbol()!=Symbol()
             || OrderType()>1 //order type buy=0, order type sell=1, order type pending >1
             || OrderMagicNumber()!=MagicNumber) continue; // continue = lanjut ke urutan berikutnya
          //yang pertama dicheck di sini pasti order yang dibuat terakhir
          if(OrderProfit()<0) break;
          if(OrderProfit()>0
             && OrderProfit()<PipStep) break;
          if(OrderProfit()>=PipStep){
             //close semua order dan pending order
             QnCOrder();
          }
          break;
       }
    return(result);}
    //====================================
    //jika order terakhir sudah mencapai take profit maka semua pending order di close
    void QnCOrder(){
       int result=0;
       for(int i=OrdersTotal()-1; i>=1; i--){
          if(!OrderSelect(i,SELECT_BY_POS)
             ||OrderSymbol()!=Symbol()
             ||OrderType()<=1
             ||OrderMagicNumber()!=MagicNumber)  continue;
          //tutup pending order
          int close =OrderDelete(OrderTicket(),0);
       }
    }
    //====================================
    int QnOrder4(){
       int result =0;
       for (int i=OrdersTotal()-1; i>=1; i--){ //i>1 karena hedging dilakukan pada order 2++
          if ( !OrderSelect(i, SELECT_BY_POS)
             || OrderSymbol()!=Symbol()
             || OrderType()>1 //order type buy=0, order type sell=1, order type pending >1
             || OrderMagicNumber()!=MagicNumber) continue; // continue = lanjut ke urutan berikutnya
          //yang pertama dicheck di sini pasti order yang dibuat terakhir
          if(OrderProfit()>0) break;
          //hitung minusnya
          int profit =0;
          if(OrderType()==OP_BUY){
             profit = QnPips(OrderClosePrice(),OrderOpenPrice());
          }else {
             profit = QnPips(OrderOpenPrice(),OrderClosePrice());   
          }
          if(profit<=(-PipStep/2)){
             //lakukan hedging
             QnPendingOrder2 ();
          }
          break;
       }
    return(result);}
    //====================================
    void QnPendingOrder2(){
       int ticket =0;
       if(OrderType()==OP_BUY){
          ticket =OrderSend(Symbol(),OP_SELLLIMIT,myLots,myOPs,0,myTPb,myTPs,EAComment,MagicNumber);
       }
       if(OrderType()==OP_SELL){
          ticket =OrderSend(Symbol(),OP_BUYLIMIT,myLots,myOPb,0,myTPs,myTPb,EAComment,MagicNumber);
       }
    }
    //====================================
    void QnStart(){
       int TotalPOrder =QnTotalOrder1();  //jumlah pending order
       int TotalNPOrder =QnTotalOrder2(); //jumlah non pending order
       int TotalOrder =QnTotalOrder1()+QnTotalOrder2();
       if (TotalOrder<=0){//belum ada order yang dibuat maka buat order baru.
          QnOrder1();
       }
       if (0<TotalNPOrder<=1
          && TotalPOrder==0){//baru 1 order yang dibuat dan profit negative maka buat pending order baru
          QnOrder2();
       }
       if (1<TotalNPOrder<=6
          && TotalPOrder>0){//pending order sudah terbentuk.
          QnOrder3();
       }
       if (2<=TotalNPOrder<=6
          && TotalPOrder==0){ //maksudnyajumlah pending order =0 (sudah tidak ada pending order)
          QnOrder4();
       }
    }
     
    Last edited by a moderator: 09 Dec 2020
  4. SoeHoe

    SoeHoe Administrator Moderator

    Equity
    Credit
    Ref Point
    Buat bagian ini di dalam fungsi yang menggunakan variable itu mas.
    Sebaiknya pahami dulu mengenai variable mas. Karena kalau salah meletakkan variable berarti blm paham dasarnya. Dari segi logika2 mas sudah mulai paham. Coba kembali memahami soal variable, global variable dan local variable.
     

Share This Page