void QnOrderMarti()
{
//---
// Judul Kelas SMP : Membuat EA Martingale
// Follow Trend : EMA200 (H4), Harga > EMA200 Signal BUY, Harga <EMA200 Signal SELL
// Trigger: OP Pertama saat garis RSI (M30) berada di area 30/70
// Martingale : Martingale berdasarkan Range Harian ATR(D1)
double dLot = 0.01, dMultiply = 2;
double dEma200 = NormalizeDouble(iMA(_Symbol,PERIOD_H4,200,0,MODE_EMA,PRICE_CLOSE,1),Digits);
double dRsi = NormalizeDouble(iRSI(_Symbol,PERIOD_M30,14,PRICE_CLOSE,1),Digits);
double dAtr = NormalizeDouble(iATR(_Symbol,PERIOD_D1,20,1),Digits);
double dAsk = MarketInfo(_Symbol, MODE_ASK);
double dBid = MarketInfo(_Symbol, MODE_BID);
double dPrcNow = NormalizeDouble((dAsk+dBid)/2,Digits);
int iSignal=0, i;
double dPrc = 0;
double dProfit=0, dTargetP=7; // target tiap OP $7
// Close All bila Total Profit tercapai
if (dTargetP > 0) {
dTargetP=dTargetP*(OrdersTotal());
for(i=0;i<=OrdersTotal()-1;i++) {
OrderSelect(i, SELECT_BY_POS);
dProfit += NormalizeDouble(OrderProfit()+OrderSwap()+OrderCommission(),Digits);
}
if (dProfit >= dTargetP) {
for(i=OrdersTotal()-1;i>=0;i--) {
OrderSelect(i, SELECT_BY_POS);
if(OrderType()==OP_BUY) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, clrRed);
if(OrderType()==OP_SELL) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 0, clrRed);
}
}
}
// New Entry
if (dPrcNow>dEma200 && dRsi<=30) iSignal=1; //1-BUY 2-SELL
else if (dPrcNow<dEma200 && dRsi>=70) iSignal=2;
// Martingale bila sudah ada OP Pertama
if (OrdersTotal()!=0) {
iSignal=0;
i = OrdersTotal()-1;
OrderSelect(i, SELECT_BY_POS);
dPrc = NormalizeDouble(OrderOpenPrice(),Digits);
dLot = OrderLots()*dMultiply;
if (OrderType()==OP_BUY && dPrcNow<dPrc-dAtr) iSignal=1;
else if (OrderType()==OP_SELL && dPrcNow>dPrc+dAtr) iSignal=2;
}
if (iSignal==1) OrderSend(_Symbol,OP_BUY,dLot,Ask,0,0,0,"EA Martingale",0,0,0);
else if (iSignal==2) OrderSend(_Symbol,OP_SELL,dLot,Bid,0,0,0,"EA Martingale",0,0,0);
}
//+------------------------------------------------------------------+
//| Belajar_Marti.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright@Skylin3 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
input double StartLots = 0.01;
input double Multiply = 2;
input double PipStep = 300;
input int Max_Order = 10;
input double TP = 300;
input double SL = 300;
input int Slip = 3;
input int MagicNumber = 1234;
int Tick;
datetime lastopendate;
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(TotalOrder()==0){
if(TodayCloseOrderCount(OP_BUY)==0){
if(Signal()==0&&TotalOP()==0)
OrderSend(Symbol(),OP_BUY,StartLots,Ask,3,0,0,"",MagicNumber,0,clrBlue);
if(TotalOP()>0)
OpenPO(LastOpenPrice(),OP_BUY);
}
if(TodayCloseOrderCount(OP_SELL)==0){
if(Signal()==1&&TotalOP()==0)
OrderSend(Symbol(),OP_SELL,StartLots,Bid,3,0,0,"",MagicNumber,0,clrRed);
if(TotalOP()>0){
OpenPO(LastOpenPrice(),OP_SELL);
}
}
}
if(TotalOP()>0&&TotalOP()<=Max_Order)
ModifTp();
if(TotalOP()==Max_Order)
SetSL();
if(TotalOP()==0&&(OrderTypeCount(OP_BUYLIMIT)>0||OrderTypeCount(OP_SELLLIMIT)>0))
DeletePendingOrder();
}
int TotalOrder(){
int total=0;
for(int i=0;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
total++;
}
return total;
}
int TotalOP(){
int total=0;
for(int i=0;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber&&OrderType()<=OP_SELL)
total++;
}
return total;
}
void OpenPO(double Layerprice, int tipe_op){
for(int i=0;i<Max_Order;i++){
double last_lot = NormalizeDouble(StartLots*MathPow(Multiply,TotalOrder()),2);
if(tipe_op==OP_BUY){
int res = OrderSend(Symbol(),OP_BUYLIMIT,last_lot,Layerprice,3,0,0,"",MagicNumber,0,clrNONE);
Layerprice = Layerprice-(PipStep*MarketInfo(Symbol(),MODE_POINT));
}
if(tipe_op==OP_SELL){
int res = OrderSend(Symbol(),OP_SELLLIMIT,last_lot,Layerprice,3,0,0,"",MagicNumber,0,clrNONE);
Layerprice = Layerprice+(PipStep*MarketInfo(Symbol(),MODE_POINT));
}
}
}
int Signal(){
double LastHi = iHigh(Symbol(),PERIOD_D1,1);
double LastLo = iLow(Symbol(),PERIOD_D1,1);
int sinyal=-1;
if(Bid<LastLo)
sinyal=0;
if(Bid>LastHi)
sinyal=1;
return sinyal;
}
int TodayCloseOrderCount(int typeorder){
int count_order=0;
if(OrdersHistoryTotal()>0){
for(int i=OrdersHistoryTotal()-1;i>=0; i--){
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber&&(OrderType()==typeorder)&&iBarShift(Symbol(),PERIOD_D1,OrderCloseTime())==0){
count_order++;
break;
}
}
}
return count_order;
}
int OrderTypeCount(int type){
int count=0;
for(int i=0;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber&&OrderType()==type)
count++;
}
return count;
}
double LastOpenPrice(){
double price=0;
datetime opentime=0;
if(TotalOrder()>0){
for(int a=OrdersTotal()-1;a>=0;a--){
if(!OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber&&(OrderType()==OP_BUY||OrderType()==OP_SELL)){
if(OrderOpenTime()>opentime){
opentime = OrderOpenTime();
price = OrderOpenPrice();
lastopendate = OrderOpenTime();
}
}
}
}
else if(TotalOrder()==0){
if(OrdersHistoryTotal()>0){
for(int x=OrdersHistoryTotal()-1;x>=0;x--){
if(!OrderSelect(x,SELECT_BY_POS,MODE_HISTORY))
continue;
if(Symbol()==OrderSymbol()&&OrderMagicNumber()==MagicNumber&&(OrderType()==OP_BUY||OrderType()==OP_SELL)){
if(OrderOpenTime()>opentime){
opentime = OrderOpenTime();
price = OrderOpenPrice();
lastopendate = OrderOpenTime();
}
}
}
}
}
return price;
}
void SetSL(){
double sl_buy,sl_sell;
double sl_op=0;
int op_ct=0;
sl_buy = NormalizeDouble(LastOpenPrice()-SL*Point,Digits);
sl_sell = NormalizeDouble(LastOpenPrice()+SL*Point,Digits);
for(int c=0;c<OrdersTotal();c++){
if(!OrderSelect(c,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber&&OrderType()<=OP_SELL){
if(OrderType()==OP_BUY){
if(OrderStopLoss()==0.0||OrderStopLoss()!=sl_buy){
if(!OrderModify(OrderTicket(),OrderOpenPrice(),sl_buy,OrderTakeProfit(),0,Green))
Print("OrderModify error ",GetLastError());
}
}
if(OrderType()==OP_SELL){
if(OrderStopLoss()==0.0||OrderStopLoss()!=sl_sell){
if(!OrderModify(OrderTicket(),OrderOpenPrice(),sl_sell,OrderTakeProfit(),0,Green))
Print("OrderModify error ",GetLastError());
}
}
}
}
}
void ModifTp(){
double tp_buy,tp_sell;
tp_buy = NormalizeDouble(LastOpenPrice()+TP*Point,Digits);
tp_sell = NormalizeDouble(LastOpenPrice()-TP*Point,Digits);
for(int i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber&&(OrderType()<=OP_SELL)){
if(OrderType()==OP_BUY){
if(OrderTakeProfit()==0||OrderTakeProfit()!=tp_buy){
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp_buy,0,Green))
Print("OrderModify error "+IntegerToString(OrderTicket()),GetLastError());
}
}
if(OrderType()==OP_SELL){
if(OrderTakeProfit()==0||OrderTakeProfit()!=tp_sell){
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp_sell,0,Green))
Print("OrderModify error "+IntegerToString(OrderTicket()),GetLastError());
}
}
}
}
}
void DeletePendingOrder(){
for(int i=OrdersTotal()-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber){
if(OrderType()==OP_BUYLIMIT||OrderType()==OP_BUYSTOP||OrderType()==OP_SELLLIMIT||OrderType()==OP_SELLSTOP)
bool res = OrderDelete(OrderTicket(),clrNONE);
}
}
}
Selamat datang di kursus MQL dari Nol sampai mahir