EA DwiM Martingale2MA.mq4


//+------------------------------------------------------------------+
//| EA DwiM Martingale2MA.mq4 |
//| @DwiMalistyo |
//| http://ea.dwim.web.id |
//+------------------------------------------------------------------+
#property copyright "@DwiMalistyo"
#property link "http://ea.dwim.web.id"

/*-- Parameter ini ditampilkan di EA dan bisa diubah --*/
extern int TakeProfit = 10; // Nilai TP setiap OP di EA Martingale ini
extern int PipStep = 10; // Jarak pip dimana akan buka OP baru
extern int Slippage = 3; // Apa itu slippage ? Googling sendiri ya..
extern double Lots = 0.1; // Nilai lots awal, nantinya akan digandakan setiap step
extern double Multiply = 2.0; // Nilai pengali setiap step OP baru
extern int MaxTrade = 11; // Banyak OP maksimal yang boleh dijalankan
extern bool UseTime = TRUE; // Pilihan apakah memakai waktu bekerjanya EA ?
extern int HourStart = 18; // Waktu mulai EA bekerja
extern int HourEnd = 24; // Waktu akhir EA bekerja

/*-- Parameter ini tidak ditampilkan di EA --*/
string EAName = "EA DwiM Martingale2MA ver 1.0"; // Nama EA, untuk ditampilkan di layar
string EAComment = "DwiM"; // Variabel ini akan kita masukkan di setiap OP sebagai Comment
int EAMagicNumber = 8095; // Magic Number
double SetPoint = 0; // Variabel SetPoint untuk kode digit broker 4 atau 5

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

SetBroker();

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

int iTrade=0;

double SMA18=0, EMA200=0;

SMA18=iMA(NULL, 0, 18, 0, MODE_SMA, PRICE_CLOSE, 0 );
EMA200=iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);

Comment(EAName); // Tampilkan Nama EA di layar

/* -- Jika tidak ada OP sama sekali, maka jalankan fungsi berikut --*/
/* -- Disinilah tempat anda memasukkan koding indikator untuk memicu OP --*/
if(OrdersTotal()==0)
{

if((Bid > SMA18) && (SMA18 > EMA200))
{
/*-- Order Buy --*/
OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, Ask+TakeProfit*SetPoint, EAComment, EAMagicNumber);

}
else if((Ask < SMA18) && (SMA18 < EMA200))
{
/*-- Order Sell --*/
OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, Bid-TakeProfit*SetPoint, EAComment, EAMagicNumber);
}

}


/* -- Inilah Fungsi Martingale. Jika ada OP yang salah, maka lakukan martingale --*/
if(OrdersTotal()>=1)
{
GoMartingale();
}


//----
return(0);
}
//+------------------------------------------------------------------+

/*--agar EA bisa running di Broker 4 Digit atau 5 Digit--*/
void SetBroker()
{
if (Digits==3 || Digits==5) // Perintah untuk broker 5 Digits
{SetPoint=Point*10;}
else // Perintah untuk broker 4 Digits
{SetPoint=Point;}
}

void GoMartingale()
{
int iCount = 0;
double LastOP = 0;
double LastLots = 0;
bool LastIsBuy = FALSE;
int iTotalBuy = 0;
int iTotalSell = 0;
int Spread=0;

Spread= MarketInfo(Symbol(), MODE_SPREAD);


for(iCount=0;iCount<OrdersTotal();iCount++)
{

OrderSelect(iCount,SELECT_BY_POS,MODE_TRADES);

if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==EAMagicNumber)
{
if(LastOP==0) {LastOP=OrderOpenPrice();}
if(LastOP>OrderOpenPrice()) {LastOP=OrderOpenPrice();}
if(LastLots<OrderLots()) {LastLots=OrderLots();}
LastIsBuy=TRUE;
iTotalBuy++;

/* Bila mencapai batas OP maksimal, jangan tambah OP lagi */
if(iTotalBuy==MaxTrade) {return(0);}
}

if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==EAMagicNumber)
{
if(LastOP==0) {LastOP=OrderOpenPrice();}
if(LastOP<OrderOpenPrice()) {LastOP=OrderOpenPrice();}
if(LastLots<OrderLots()) {LastLots=OrderLots();}
LastIsBuy=FALSE;
iTotalSell++;

/* Bila mencapai batas OP maksimal, jangan tambah OP lagi */
if(iTotalBuy==MaxTrade) {return(0);}
}

}

/* Jika arah Price adalah DOWNTREND...., Periksa nilai Bid (*/
if(LastIsBuy)
{
if(Bid<=LastOP-(Spread*SetPoint)-(PipStep*SetPoint))
{
OrderSend(Symbol(), OP_BUY, Multiply*LastLots, Ask, Slippage, 0, Ask+TakeProfit*SetPoint, EAComment, EAMagicNumber);
ModifyTP();
LastIsBuy=FALSE;
return(0);
}
}
/* Jika arah Price adalah Sell...., Periksa nilai Ask (*/
else if(!LastIsBuy)
{
if(Ask>=LastOP+(Spread*SetPoint)+(PipStep*SetPoint))
{
OrderSend(Symbol(), OP_SELL, Multiply*LastLots, Bid, Slippage, 0, Bid-TakeProfit*SetPoint, EAComment, EAMagicNumber);
ModifyTP();
return(0);
}
}

}

/*-- Fungsi ModifyTP ini untuk mengubah semua OP agar TP di titik yang sama --*/
void ModifyTP()
{
int iCount=0;
double NewTP=0;

/*- Ambil nilai Take Profit dari Order terakhir -*/
for(iCount=0;iCount<OrdersTotal();iCount++)
{

OrderSelect(iCount,SELECT_BY_POS,MODE_TRADES);

/*-- Kalau OP-nya adalah BUY, ambil nilai TP yang paling kecil. Jadikan TP bersama --*/
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==EAMagicNumber)
{
if(NewTP==0) {NewTP=OrderTakeProfit();}
if(NewTP>OrderTakeProfit()) {NewTP=OrderTakeProfit();}

}

/*-- Kalau OP-nya adalah SELL, ambil nilai TP yang paling besar. Jadikan TP bersama --*/
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==EAMagicNumber)
{
if(NewTP==0) {NewTP=OrderTakeProfit();}
if(NewTP<OrderTakeProfit()) {NewTP=OrderTakeProfit();}
}

}

/*- Ubah semua nilai OP TakeProfit dengan yang baru (2x lipat) -*/
for(iCount=0;iCount<OrdersTotal();iCount++)
{

OrderSelect(iCount,SELECT_BY_POS,MODE_TRADES);

/*- Kalau semua OP adalah BUY, maka ubahlah TP mereka -*/
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==EAMagicNumber)
{
OrderModify(OrderTicket(), OrderLots(), 0, NewTP, 0);
}

/*- Kalau semua OP adalah SELL, maka ubahlah TP mereka -*/
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==EAMagicNumber)
{
OrderModify(OrderTicket(), OrderLots(), 0, NewTP, 0);
}

}
}