Beranda Perdagangan Sistem Postingan

AK-47 Scalper EA: Panduan Lengkap untuk MetaTrader 4

Lampiran
42236.zip (5.76 KB, Unduh 2 kali)

1. Parameter Input

#define ExtBotName "AK-47 Scalper EA" // Nama Bot
#define  Version "1.00"

//--- parameter input---
extern string  EASettings        = "---------------------------------------------"; //-------- <Pengaturan EA> --------
input int      InpMagicNumber    = 124656;   //Nomor Keajaiban

extern string  TradingSettings   = "---------------------------------------------"; //-------- <Pengaturan Trading> --------
input double   Inpuser_lot       = 0.01;     //Lots
input double   InpSL_Pips        = 3.5;      //Stoploss (dalam Pips)
input double   InpMax_spread     = 0.5;      //Spread maksimum yang diizinkan (dalam Pips) (0 = mengambang)

extern string  MoneySettings     = "---------------------------------------------"; //-------- <Pengaturan Uang> --------
input bool     isVolume_Percent  = true;     //Izinkan Persentase Volume
input double   InpRisk           = 3        //Persentase Risiko dari Saldo (%)

input string   TimeSettings      = "---------------------------------------------"; //-------- <Pengaturan Waktu Trading> --------
input bool     InpTimeFilter     = true      //Filter Waktu Trading
input int      InpStartHour      = 2         //Jam Mulai
input int      InpStartMinute    = 30        //Menit Mulai
input int      InpEndHour        = 21        //Jam Selesai
input int      InpEndMinute      = 0         //Menit Selesai

2. Inisialisasi Variabel Lokal
//--- Variabel
int      Pips2Points;               // slippage  3 pips    3=points    30=points
double   Pips2Double;               // Stoploss 15 pips    0.015      0.0150
int      InpMax_slippage   = 3;     // Maksimal slippage yang diizinkan_Pips.
bool     isOrder           = false; // hanya buka 1 order
int      slippage;
string   strComment        = "";

3. Kode Utama

a/ Fungsi inisialisasi Expert

int OnInit()
  {
//---   
   //Deteksi 3 atau 5 digit
   //Pip dan point
   if (Digits % 2 == 1)
   {
      Pips2Double  = _Point*10; 
      Pips2Points  = 10;
      slippage = 10* InpMax_slippage;
   } 
   else
   {    
      Pips2Double  = _Point;
      Pips2Points  =  1;
      slippage = InpMax_slippage;
   }
   
//---
   return(INIT_SUCCEEDED);
  }

b/ Fungsi tick Expert

void OnTick()
  {
//---
     if(IsTradeAllowed() == false)
     {
      Comment("AK-47 EA\nTrading tidak diizinkan.");
      return;
     }
     
       MqlDateTime structTime;
       TimeCurrent(structTime);
       structTime.sec = 0;
       
       //Set waktu mulai
       structTime.hour = InpStartHour;
       structTime.min = InpStartMinute;       
       datetime timeStart = StructToTime(structTime);
       
       //Set waktu Selesai
       structTime.hour = InpEndHour;
       structTime.min = InpEndMinute;
       datetime timeEnd = StructToTime(structTime);
       
       double acSpread = MarketInfo(Symbol(), MODE_SPREAD);
       StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
      
      strComment = "\n" + ExtBotName + " - v." + (string)Version;
      strComment += "\nWaktu GMT = " + TimeToString(TimeGMT(),TIME_DATE|TIME_SECONDS);
      strComment += "\nWaktu Trading = [" + (string)InpStartHour + "h" + (string)InpStartMinute + " --> " +  (string)InpEndHour + "h" + (string)InpEndMinute + "]";
      
      strComment += "\nSpread Saat Ini = " + (string)acSpread + " Points";
      strComment += "\nStoppLevel Saat Ini = " + (string)StopLevel + " Points";
      
      Comment(strComment);
   
      //Perbarui Nilai
      UpdateOrders();
      
      TrailingStop();
      
      //Cek Waktu Trading
      if(InpTimeFilter)
      {
         if(TimeCurrent() >= timeStart && TimeCurrent() < timeEnd)
         {
            if(!isOrder) OpenOrder();
         }
      }
      else
      {
         if(!isOrder) OpenOrder();
      }
  }

3.1 Menghitung sinyal untuk mengirimkan order

void OpenOrder(){
   
   //int OrdType = OP_SELL;//-1;

   double TP = 0;
   double SL = 0;
   string comment = ExtBotName;

   //Menghitung Lots
   double lot1 = CalculateVolume();
   
   //if(OrdType == OP_SELL){
      double OpenPrice = NormalizeDouble(Bid - (StopLevel * _Point) - (InpSL_Pips/2) * Pips2Double, Digits);
      SL = NormalizeDouble(Ask + StopLevel * _Point + InpSL_Pips/2 * Pips2Double, Digits);
       
      if(CheckSpreadAllow())                                    //Cek Spread
      {
         if(!OrderSend(_Symbol, OP_SELLSTOP, lot1, OpenPrice, slippage, SL, TP, comment, InpMagicNumber, 0, clrRed))
         Print(__FUNCTION__,"--> Kesalahan OrderSend ",GetLastError());
      }
   //}
}

3.2 Menghitung Volume

double CalculateVolume()
  {
   double LotSize = 0;

   if(isVolume_Percent == false)
     {
      LotSize = Inpuser_lot;
     }
   else
     {

      LotSize = (InpRisk) * AccountFreeMargin();
      LotSize = LotSize /100000;
      double n = MathFloor(LotSize/Inpuser_lot);
      //Comment((string)n);
      LotSize = n * Inpuser_lot;

      if(LotSize < Inpuser_lot)
         LotSize = Inpuser_lot;

      if(LotSize > MarketInfo(Symbol(),MODE_MAXLOT))
         LotSize = MarketInfo(Symbol(),MODE_MAXLOT);

      if(LotSize < MarketInfo(Symbol(),MODE_MINLOT))
         LotSize = MarketInfo(Symbol(),MODE_MINLOT);
     }

   return(LotSize);
  }

3.3 EA memiliki fungsi "trailing Stop", SL akan berubah setiap kali harga berubah (turun)

void TrailingStop()
  {
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if((OrderMagicNumber() == InpMagicNumber) && (OrderSymbol() == Symbol()))   //_Symbol))
           {

            //Untuk Order Jual
            if(OrderType() == OP_SELL)
              {
                  //--Menghitung SL ketika harga berubah
                  double SL_in_Pip = NormalizeDouble(OrderStopLoss() - (StopLevel * _Point) - Ask, Digits) / Pips2Double;
                  if(SL_in_Pip > InpSL_Pips){
                        double newSL = NormalizeDouble(Ask + (StopLevel * _Point) + InpSL_Pips * Pips2Double, Digits);
                        if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrRed))
                        {
                           Print(__FUNCTION__,"--> Kesalahan OrderModify ",GetLastError());
                        {
                          continue;
                          
                          
                          }
                        }
            }
             
            //Untuk Order SellStop
            else if(OrderType() == OP_SELLSTOP)
              {
                  double SL_in_Pip = NormalizeDouble(OrderStopLoss() - (StopLevel * _Point) - Ask, Digits) / Pips2Double;
                  
                  if(SL_in_Pip < InpSL_Pips/2){
                     double newOP = NormalizeDouble(Bid - (StopLevel * _Point) - (InpSL_Pips/2) * Pips2Double, Digits);
                     double newSL = NormalizeDouble(Ask + (StopLevel * _Point) + (InpSL_Pips/2) * Pips2Double, Digits);
                     
                     if(!OrderModify(OrderTicket(), newOP, newSL, OrderTakeProfit(), 0, clrRed))
                        Print(__FUNCTION__,"--> Kesalahan Modify PendingOrder!", GetLastError());
                        continue;  
                         }
                          }
                          }
                          
        }
    } 
  }


Postingan terkait

Komentar (0)