Belajar Membuat EA Martingale

Belajar Membuat EA Martingale Adalah Tutorial Caranya Membuat EA Robot Trading Yang Sistem Transaksinya Adalah Dengan Menggunakan Strategi Martingale. Coding Mql4 Untuk Membuat EA’nya Adalah Sebagai Berikut:

///////

//+——————————————————————+
//| EA MARTINGALE v2.mq4 |
//| Trader |
//| http://kursusforex.net |
//+——————————————————————+
#property copyright “Copyright © Trader”
#property link “http://kursusforex.net”
//#property version “1.00”
//#property strict

#include <stdlib.mqh>
#include <WinUser32.mqh>

extern double MaxSpread = 5;

extern bool Monday1 = true;
extern bool Tuesday2 = true;
extern bool Wednesday3 = true;
extern bool Thursday4 = true;
extern bool Friday5 = true;
extern bool Saturday6 = true;
extern bool Sunday7 = false;

extern int StartHours = 0;
extern int FinishHours = 24;

extern double LotsOp1 = 0.01;
extern double LotsOp2 = 0.02;
extern double JarakOp2dariOp1 = -1;
extern int Stoploss = 0;
extern int Takeprofit = 30;

extern bool UseTrailingStop = False;
extern int TrailingStop = 12;

extern double MaxCloseLoss = -50;
extern bool UseMaxCloseProfit = False;
extern double MaxCloseProfit = 5;
extern double MaxCloseLabaSell = 10;
extern double MaxCloseLabaBuy = 10;

extern int Slippage = 4;

double PipValue=1;
string LF = “\n”;
int NDigits = 4;
int current = 0;

int Count18 = 0;

//+——————————————————————+
//| Expert initialization function |
//+——————————————————————+
int OnInit()
{
//—
NDigits = Digits;
//—
return(INIT_SUCCEEDED);
}
//+——————————————————————+
//| Expert deinitialization function |
//+——————————————————————+
void OnDeinit(const int reason)
{
//—
if (false) ObjectsDeleteAll();
}
//+——————————————————————+
//| Expert tick function |
//+——————————————————————+
void OnTick()
{
if (Bars < 10)
{
return;
}
CekDigits();
return;
}
//+——————————————————————+
void CekDigits()
{
PipValue = 1;
if (NDigits == 3 || NDigits == 5) PipValue = 10;

FilterSpread();
Label();
if(UseTrailingStop) Trailing_Stop();

CekLossBuy1_1();
CekLossSell1_501();

CekLossAllOrder();
if(UseMaxCloseProfit) CekProfitAllOrder();
CekLabaAllOpBuy();
CekLabaAllOpSell();

}
//+——————————————————————+
void FilterSpread()
{
if ((Ask-Bid) / Point / PipValue < MaxSpread)
{
FilterDay();

}
}
//+——————————————————————+
void FilterDay()
{
if ((Monday1 && DayOfWeek() == 1)
|| (Tuesday2 && DayOfWeek() == 2)
|| (Wednesday3 && DayOfWeek() == 3)
|| (Thursday4 && DayOfWeek() == 4)
|| (Friday5 && DayOfWeek() == 5)
|| (Saturday6 && DayOfWeek() == 6)
|| (Sunday7 && DayOfWeek() == 0))
{
FilterHours();

}
}
//+——————————————————————+
void FilterHours()
{
int datetime800 = TimeLocal();
int hour0 = TimeHour(datetime800);

if ((StartHours < FinishHours && hour0 >= StartHours && hour0 < FinishHours)
|| (StartHours > FinishHours && (hour0 < FinishHours
|| hour0 >= StartHours)))
{
CekTrade();

}
}
//+——————————————————————+
void CekTrade()
{
bool exists = false;
for (int i=OrdersTotal()-1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol())
{
exists = true;
}
}
if (exists == false)
{
AnalysaBuyRsi();
AnalysaSellRsi();
}
}
//+——————————————————————+
void AnalysaBuyRsi()
{
if (iRSI(NULL, NULL,14,PRICE_CLOSE,current) > 50)
{
BuyOrder1_1();

}
}
//+——————————————————————+
void BuyOrder1_1()
{
double SL = Ask – Stoploss*PipValue*Point;
if (Stoploss == 0) SL = 0;
double TP = Ask + Takeprofit*PipValue*Point;
if (Takeprofit == 0) TP = 0;

int ticket = OrderSend(Symbol(), OP_BUY, LotsOp1, Ask, Slippage, SL, TP, “EA buy 1-1”, 1, 0, Blue);
}
//+——————————————————————+
void AnalysaSellRsi()
{
if (iRSI(NULL, NULL,14,PRICE_CLOSE,current) < 50)
{
SellOrder1_501();

}
}
//+——————————————————————+
void SellOrder1_501()
{
double SL = Bid + Stoploss*PipValue*Point;
if (Stoploss == 0) SL = 0;
double TP = Bid – Takeprofit*PipValue*Point;
if (Takeprofit == 0) TP = 0;

int ticket = OrderSend(Symbol(), OP_SELL, LotsOp1, Bid, 4, SL, TP, “EA sell 1-501”, 501, 0, Red);
}
//+——————————————————————+
void Label()
{
string temp = “\nExecuted : ” + Count18 + “\n”
+ “Spread: ” + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD)/PipValue, 2)+ “\n”
+ “————————————————\n”
+ “ACCOUNT INFORMATION:\n”
+ “\n”
+ “Account Name: ” + AccountName()+ “\n”
+ “Account Leverage: ” + DoubleToStr(AccountLeverage(), 0)+ “\n”
+ “Account Balance: ” + DoubleToStr(AccountBalance(), 2)+ “\n”
+ “Account Equity: ” + DoubleToStr(AccountEquity(), 2)+ “\n”
+ “Free Margin: ” + DoubleToStr(AccountFreeMargin(), 2)+ “\n”
+ “Used Margin: ” + DoubleToStr(AccountMargin(), 2)+ “\n”
+ “————————————————\n”;
Comment(temp);
Count18++;

}
//+——————————————————————+
void Trailing_Stop()
{
int ts = TrailingStop;
for (int i=OrdersTotal()-1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol())
{
if (OrderType() == OP_BUY && Ask – OrderOpenPrice() > ts*PipValue*Point)
{
if (OrderStopLoss() < Ask-(ts+3)*PipValue*Point || OrderStopLoss() == 0)
if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask-ts*PipValue*Point, OrderExpiration(), White))
Print(“OrderModify() error – “, ErrorDescription(GetLastError()));
}
if (OrderType() == OP_SELL && OrderOpenPrice() – Bid > ts*PipValue*Point)
{
if (OrderStopLoss() > Bid+(ts+3)*PipValue*Point || OrderStopLoss() == 0)
if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid+ts*PipValue*Point, OrderExpiration(), White))
Print(“OrderModify() error – “, ErrorDescription(GetLastError()));
}
}
}
}
//+——————————————————————+
void CekLossBuy1_1()
{
double profit = 0;
for (int i=OrdersTotal()-1; i >= 0; i–)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
{
profit += OrderProfit();
}
}
else
{
Print(“OrderSelect() error – “, ErrorDescription(GetLastError()));
}
}

if (profit < JarakOp2dariOp1)
{
PlanBuy2_2();

}
}
//+——————————————————————+
void PlanBuy2_2()
{
bool exists = false;
for (int i=OrdersTotal()-1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 2)
{
exists = true;
}
}
else
{
Print(“OrderSelect() error – “, ErrorDescription(GetLastError()));
}

if (exists == false)
{
BuyOrder2_2();

}
}
//+——————————————————————+
void BuyOrder2_2()
{
double SL = Ask – Stoploss*PipValue*Point;
if (Stoploss == 0) SL = 0;
double TP = Ask + Takeprofit*PipValue*Point;
if (Takeprofit == 0) TP = 0;

int ticket = OrderSend(Symbol(), OP_BUY, LotsOp2, Ask, Slippage, SL, TP, “EA buy 2-2”, 2, 0, Blue);
}
//+——————————————————————+
void CekLossSell1_501()
{
double profit = 0;
for (int i=OrdersTotal()-1; i >= 0; i–)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 501)
{
profit += OrderProfit();
}
}
else
{
Print(“OrderSelect() error – “, ErrorDescription(GetLastError()));
}
}

if (profit < JarakOp2dariOp1)
{
PlanBuy2_502();

}
}
//+——————————————————————+
void PlanBuy2_502()
{
bool exists = false;
for (int i=OrdersTotal()-1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 502)
{
exists = true;
}
}
if (exists == false)
{
SellOrder2_502();
}
}
//+——————————————————————+
void SellOrder2_502()
{
double SL = Bid + Stoploss*PipValue*Point;
if (Stoploss == 0) SL = 0;
double TP = Bid – Takeprofit*PipValue*Point;
if (Takeprofit == 0) TP = 0;

int ticket = OrderSend(Symbol(), OP_SELL, LotsOp2, Bid, Slippage, SL, TP, “EA sell 2-502”, 502, 0, Red);
}
//+——————————————————————+
void CekLossAllOrder()
{
double profit = 0;
for (int i=OrdersTotal()-1; i >= 0; i–)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol())
{
profit += OrderProfit();
}
}
else
{
Print(“OrderSelect() error – “, ErrorDescription(GetLastError()));
}
}

if (profit < MaxCloseLoss)
{
CloseAllOpSell();
CloseAllOpBuy();

}
}
//+——————————————————————+
void CloseAllOpSell()
{
int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[90][2];
for (int i = 0; i < orderstotal; i++)
{
bool sel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderType() != OP_SELL || OrderSymbol() != Symbol())
{
continue;
}
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}
for (i = 0; i < orders; i++)
{
if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
{
bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Red);
if (ret == false)
Print(“OrderClose() error – “, ErrorDescription(GetLastError()));
}
}

}
//+——————————————————————+
void CloseAllOpBuy()
{
int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[90][2];
for (int i = 0; i < orderstotal; i++)
{
bool sel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderType() != OP_BUY || OrderSymbol() != Symbol())
{
continue;
}
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}
for (i = 0; i < orders; i++)
{
if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
{
bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Blue);
if (ret == false)
Print(“OrderClose() error – “, ErrorDescription(GetLastError()));
}
}

}
//+——————————————————————+
void CekProfitAllOrder()
{
double profit = 0;
for (int i=OrdersTotal()-1; i >= 0; i–)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol())
{
profit += OrderProfit();
}
}
else
{
Print(“OrderSelect() error – “, ErrorDescription(GetLastError()));
}
}

if (profit > MaxCloseProfit)
{
CloseAllSell();
CloseAllBuy();

}
}
//+——————————————————————+
void CloseAllSell()
{
int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[90][2];
for (int i = 0; i < orderstotal; i++)
{
bool sel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderType() != OP_SELL || OrderSymbol() != Symbol())
{
continue;
}
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}
for (i = 0; i < orders; i++)
{
if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
{
bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Red);
if (ret == false)
Print(“OrderClose() error – “, ErrorDescription(GetLastError()));
}
}

}
//+——————————————————————+
void CloseAllBuy()
{
int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[90][2];
for (int i = 0; i < orderstotal; i++)
{
bool sel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderType() != OP_BUY || OrderSymbol() != Symbol())
{
continue;
}
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}
for (i = 0; i < orders; i++)
{
if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
{
bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Blue);
if (ret == false)
Print(“OrderClose() error – “, ErrorDescription(GetLastError()));
}
}

}
//+——————————————————————+
void CekLabaAllOpBuy()
{
double profit = 0;
for (int i=OrdersTotal()-1; i >= 0; i–)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderSymbol() == Symbol())
{
profit += OrderProfit();
}
}
}

if (profit > MaxCloseLabaBuy)
{
CloseAll_Buy();

}
}
//+——————————————————————+
void CloseAll_Buy()
{
int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[90][2];
for (int i = 0; i < orderstotal; i++)
{
bool sel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderType() != OP_BUY || OrderSymbol() != Symbol())
{
continue;
}
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}
for (i = 0; i < orders; i++)
{
if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
{
bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Blue);
if (ret == false)
Print(“OrderClose() error – “, ErrorDescription(GetLastError()));
}
}

}
//+——————————————————————+
void CekLabaAllOpSell()
{
double profit = 0;
for (int i=OrdersTotal()-1; i >= 0; i–)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderSymbol() == Symbol())
{
profit += OrderProfit();
}
}
}

if (profit > MaxCloseLabaSell)
{
CloseAll_Sell();

}
}
//+——————————————————————+
void CloseAll_Sell()
{
int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[90][2];
for (int i = 0; i < orderstotal; i++)
{
bool sel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderType() != OP_SELL || OrderSymbol() != Symbol())
{
continue;
}
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}
for (i = 0; i < orders; i++)
{
if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
{
bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Red);
if (ret == false)
Print(“OrderClose() error – “, ErrorDescription(GetLastError()));
}
}

}
//+——————————————————————+

///////

Demikian Tutorial Belajar Membuat EA Martingale Yang Dapat Kami Sampaikan Kepada Anda, Semoga Bermanfaat. Kalau Ada Pertanyaan atau Anda Perlu Bantuan Bimbingan Belajar Untuk Membuat EA, Silahkan Anda Hubungi ke Kontak Kami.