• 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 :)

Tutorial Buat EA Mudah Rame-rame seru (by WAME)

klo mau bikin ea yg open order berdasarkan previous candle, dan exit berdasarkan close candle saat ini.
klo previous canlde close hige maka open buy.
klo previous candle close down maka open sell.
simple tp profitable klo di tf dayli.
sourchcode nya apa aja ya master?
 
Ini khusus buat yang punya otak brilian gan karena menghafal koding serumit itu kalau hanya menggunakan otak mungkin akan sulit, ane juga susah kalau harus menghafal koding begitu, tapi mungkin pembuat ea juga sudah memiliki catatan tentang koding yang penting
kan tinggal kopas aja gan..g perlu di ketik dari nol....
 
kan tinggal kopas aja gan..g perlu di ketik dari nol....
Help gan, kalau EA keterangannya kayak ginie kenapa ya??? dia order ngasih modified TP tapi ada seperti itu malah TP nggak ke set... Bagaimana biar TP tetep berusaha di set EA ya?
 

Attachments

  • 2.png
    2.png
    4.9 KB · Views: 26
Murayrich mungkin ini bisa dicoba...

void OnTick(){
int JumlahOrder = OrdersTotal(); // untuk mengetahui jumlah order
double OpenCandel = Open[1]; //melihat nilai open candle sift 1
double CloseCandel= Close[1]; //melihat nilai close candle sift 1
double lot = 0.1;
bool SinyalBuy = false; // sebelum kita melihat kondisi candle sinyal harus false dulu
bool SinyalSell= false; // sebelum kita melihat kondisi candle sinyal harus false dulu

if(OpenCandel<CloseCandel)SinyalBuy =1; // menentukan sinyal buy jika candle naik
if(OpenCandel>CloseCandel)SinyalSell =1;

if(SinyalBuy) close(1); // close sell saat sinyal buy ( candle up)
if(SinyalSell)close(0); // close buy saat sinyal sell( candle down)

//OrderSend(symb,cmd,lot,price . . . .
if(JumlahOrder<1&&SinyalBuy==true ) { OrderSend( Symbol(), 0, lot, Ask, 3, 0, 0, "", 0, 0,clrNONE );}// send buy
if(JumlahOrder<1&&SinyalSell==true ) { OrderSend( Symbol(), 1, lot, Bid, 3, 0, 0, "", 0, 0,clrNONE );}// send sell
// perbedaan order send buy dan sell ada pada cmd dan price
}

void close( int x ) // x adalah tipe OP yang akan di close 0=buy 1=sell
{ // ini mulai start fungsi
for (int i= OrdersTotal()-1 ; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if (OrderSymbol() == Symbol()&&OrderType()==x )
{ if(OrderType()==0 ) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue); }
if(OrderType()==1 ) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Red); }
}
}
}
 
gan bantuin dong ini saya ada script ea mau nambahin marti nya eror trus,ini pengennya kalau sudah kena sl lgsg OP lagi di titik sl trsebut dengn double lot


//+------------------------------------------------------------------+
//|
//|
//|
//+------------------------------------------------------------------+
#property copyright "KUSUS UNTUK SODAKOH "
#property link "rejeki.com"
#property version "1.00"
#property strict
#define __STRATEGY_MAGIC 557835
#define __SLEEP_AFTER_EXECUTION_FAIL 400
//Input Variable
input double _Take_Profit = 500; //Bismillah
input double _Stop_Loss = 1000; //belum rejeki
input int _EMA_60 = 60; // goib 1
input int _SMA_90 = 90; // goib 2
//input int _EMA_70 = 70; // EMA 3
input double _Lot = 0.01; //Lot
//GLOBAL DECLARATION
double _iEMA_60;
double _iSMA_90;
int init() {
return(0);
}

//Local Declaration
bool _Buy = false;
bool _Sell = false;

_iEMA_60 = iMA ("",0,_EMA_60,0,MODE_EMA,0,0);
_iSMA_90 = iMA ("",0,_SMA_90,0,MODE_EMA,0,0);

//--------------------- ARGUMEN EA ------------------
// ARGUMEN BUY
if(_iEMA_60 > _iSMA_90 &&
iClose ("",0,1) > _iEMA_60 &&
_iEMA_60 > iOpen("",0,1)) _Buy = Buy (1,_Lot,0,_Stop_Loss,0,_Take_Profit,10,1,0,"");

//ARGUMEN SELL
if(_iEMA_60 < _iSMA_90 &&
iClose ("",0,1) < _iEMA_60 &&
_iEMA_60 < iOpen("",0,1)) _Sell = Sell (1,_Lot,0,_Stop_Loss,0,_Take_Profit,10,1,0,"");
return(0);
}
bool __selectOrderByMagic(int magic, string symbol)
{
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == __STRATEGY_MAGIC + magic && OrderSymbol() == symbol)
return(true);
}
return(false);
}
bool Close_All_Trades(int MagicIndex)
{
int total = OrdersTotal();
int type;
bool result = true;
for(int i=total-1;i>=0;i--)
{
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
type = OrderType();
if(type == OP_BUY)
{
if(!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ))
{
result = false;
Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
}
}else if(type == OP_SELL)
{
if(!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ))
{
result = false;
Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
}
}
}
return(result);
}
bool Buy(int MagicIndex, double Lots, int StopLossMethod, double StopLossPoints, int TakeProfitMethod, double TakeProfitPoints, int Slippage, int MaxOpenTrades,
int MaxFrequencyMins, string TradeComment)
{
static double pipSize = 0;
if(pipSize == 0) pipSize = Point * (1 + 9 * (Digits == 3 || Digits == 5));
double sl = 0, tp = 0;
double stopLossPoints = 0, takeProfitPoints = 0;

int numberOfOpenTrades = 0;

for(int i=OrdersTotal()-1;i>=0;i--){
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
numberOfOpenTrades ++;
}
if(MaxOpenTrades > 0 && numberOfOpenTrades >= MaxOpenTrades) return(false);
if(MaxFrequencyMins > 0)
{
int recentSeconds = MaxFrequencyMins * 60;
for(int i=OrdersTotal()-1;i>=0;i--){
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
}
int hstTotal=OrdersHistoryTotal();

for(int i=hstTotal-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
break;
}
}

if(Lots < MarketInfo(Symbol(),MODE_MINLOT)) return(false);

if(AccountFreeMarginCheck(Symbol(), OP_SELL,Lots) <= 0) {
Print("Buy error: insufficient capital");
return(false);
}
if (StopLossPoints > 0)
{
if(StopLossMethod == 0)
{
sl = NormalizeDouble(Ask - StopLossPoints * Point, Digits);
stopLossPoints = StopLossPoints;
}
else if (StopLossMethod == 1)
{
sl = NormalizeDouble(Ask - StopLossPoints * pipSize, Digits);
stopLossPoints = StopLossPoints * (1 + 9 * (Digits == 3 || Digits == 5));
}
else
{
sl = StopLossPoints;
stopLossPoints = (Ask - sl)/Point;
}
}

if (TakeProfitPoints > 0)
{
if(TakeProfitMethod == 0)
{
tp = NormalizeDouble(Ask + TakeProfitPoints * Point, Digits);
takeProfitPoints = TakeProfitPoints;
}
else if (TakeProfitMethod == 1)
{
tp = NormalizeDouble(Ask + TakeProfitPoints * pipSize, Digits);
takeProfitPoints = TakeProfitPoints * (1 + 9 * (Digits == 3 || Digits == 5));
}
else
{
tp = TakeProfitPoints;
takeProfitPoints = (tp - Ask)/Point;
}
}

double stopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD);

if( (sl > 0 && stopLossPoints <= stopLevel) || (tp > 0 && takeProfitPoints <= stopLevel) )
{
Print("Cannot Buy: Stop loss and take profit must be at least "
+ DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD),0)
+ " points away from the current price");
return (false);
}

RefreshRates();
int result = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, sl, tp, "EA BUY " + "(" + WindowExpertName() + ") " + TradeComment, __STRATEGY_MAGIC + MagicIndex);

if (result == -1){
Print("Failed to Buy: " + IntegerToString(GetLastError()));
Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
return(false);
}

return(true);
}
bool Sell(int MagicIndex, double Lots, int StopLossMethod, double StopLossPoints, int TakeProfitMethod, double TakeProfitPoints, int Slippage, int MaxOpenTrades,
int MaxFrequencyMins, string TradeComment)
{
static double pipSize = 0;
if(pipSize == 0) pipSize = Point * (1 + 9 * (Digits == 3 || Digits == 5));
double sl = 0, tp = 0;
double stopLossPoints = 0, takeProfitPoints = 0;
int numberOfOpenTrades = 0;

for(int i=OrdersTotal()-1;i>=0;i--){
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
numberOfOpenTrades ++;
}
if(MaxOpenTrades > 0 && numberOfOpenTrades >= MaxOpenTrades) return(false);

if(MaxFrequencyMins > 0)
{
int recentSeconds = MaxFrequencyMins * 60;
for(int i=OrdersTotal()-1;i>=0;i--){
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
}
int hstTotal=OrdersHistoryTotal();

for(int i=hstTotal-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
break;
}
}

if(Lots < MarketInfo(Symbol(),MODE_MINLOT)) return(false);

if(AccountFreeMarginCheck(Symbol(), OP_SELL,Lots) <= 0) {
Print("Sell order error: insufficient capital");
return(false);
}

if (StopLossPoints > 0)
{
if(StopLossMethod == 0)
{
sl = NormalizeDouble(Bid + StopLossPoints * Point, Digits);
stopLossPoints = StopLossPoints;
}
else if(StopLossMethod == 1)
{
sl = NormalizeDouble(Bid + StopLossPoints * pipSize, Digits);
stopLossPoints = StopLossPoints * (1 + 9 * (Digits == 3 || Digits == 5));
}
else
{
sl = StopLossPoints;
stopLossPoints = (sl - Bid)/Point;
}
}
if (TakeProfitPoints > 0)
{
if(TakeProfitMethod == 0)
{
tp = NormalizeDouble(Bid - TakeProfitPoints * Point, Digits);
takeProfitPoints = TakeProfitPoints;
}
else if (TakeProfitMethod == 1)
{
tp = NormalizeDouble(Bid - TakeProfitPoints * pipSize, Digits);
takeProfitPoints = TakeProfitPoints * (1 + 9 * (Digits == 3 || Digits == 5));
}
else
{
tp = TakeProfitPoints;
takeProfitPoints = (Bid - tp)/Point;
}
}

double stopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD);

if( (sl > 0 && stopLossPoints <= stopLevel) || (tp > 0 && takeProfitPoints <= stopLevel) )
{
Print("Cannot Sell: Stop loss and take profit must be at least "
+ DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD),0)
+ " points away from the current price");
return (false);
}

RefreshRates();
int result = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, sl, tp, "EA SELL " + "(" + WindowExpertName() + ") " + TradeComment,__STRATEGY_MAGIC + MagicIndex);
if (result == -1){
Print("Failed to Sell: " + IntegerToString(GetLastError()));
Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
return(false);
}

return(true);
}
 
Wadau...

Ada group sharing edukasi coding yg bagus, coba join t.me/codeMQL
Banyak contoh dan respon yang aktif disana.
 
suhu ada materi mengenai insert news filter ke ea gak ya,,. atau code custom indi ke dalam ea,.,.


thanks alot.,
 
Disini ane mau kasih tips bagi para programer nubie yang memang sedang dalam tahap pembelajaran mengenai seputar coding MQL.

Oke tanpa basa basi langsung aja ke POKOK PEMBAHASAN.
sesuai dengan judul kita akan bahas cara-cara coding mudah yang biasa dipakai para programer MQL, berikut penjelasannya :

Biasanya para Programer EA sudah punya kumpulan script-script dan koding-koding di database mereka, nah oleh karena itu nanti disini ane akan share kumpulan script-script dan koding-koding yang biasa dipakai.

PEMBAHASAN 1 ===ORDER===

pada pembahasan ini kita akan bahas seputar order, kalau ane order disini ane bagi menjadi 4 macam :
1. Order langsung ( Buy / Sell )
2. Order Tidak Langsung ( Pending Buy / Sell )
3. Order Perubah ( Modify )
4. Order Penutup ( Close / Delete )

berikut codebasenya.
Paste Code ini didalam OnInit
Code:
   if(Digits==3||Digits==5){pt=Point*10;}else {pt=Point*1;}

Paste Code ini dbagian paling bawah koding
Code:
double pt;
void myOrderSend(double myAsk,double myBid,string myPair,int StepOP,double ManualPrice,int OPType,double Lots,double myStopLoss,double myTakeProfit,string myCom){
   double myPrice=0,mySL=0,myTP=0,ticket=0;
   if(OPType==0){myPrice=myAsk;//Buy
   if(StopLoss>0){mySL=myPrice-StopLoss*pt;}else{mySL=0;}
   if(TakeProfit>0){myTP=myPrice+TakeProfit*pt;}else{myTP=0;}}else
   if(OPType==1){myPrice=myBid;//Sell
   if(StopLoss>0){mySL=myPrice+StopLoss*pt;}else{mySL=0;}
   if(TakeProfit>0){myTP=myPrice-TakeProfit*pt;}else{myTP=0;}}else
   if(OPType==2){if(ManualPrice==0)myPrice=myAsk-StepOP*pt;else myPrice=ManualPrice;//BuyLimit
   if(StopLoss>0){mySL=myPrice-StopLoss*pt;}else{mySL=0;}
   if(TakeProfit>0){myTP=myPrice+TakeProfit*pt;}else{myTP=0;}}else
   if(OPType==3){if(ManualPrice==0)myPrice=myBid+StepOP*pt;else myPrice=ManualPrice;//SellLimit
   if(StopLoss>0){mySL=myPrice-StopLoss*pt;}else{mySL=0;}
   if(TakeProfit>0){myTP=myPrice+TakeProfit*pt;}else{myTP=0;}}else
   if(OPType==4){if(ManualPrice==0)myPrice=myAsk+StepOP*pt;else myPrice=ManualPrice;//BuyStop
   if(StopLoss>0){mySL=myPrice-StopLoss*pt;}else{mySL=0;}
   if(TakeProfit>0){myTP=myPrice+TakeProfit*pt;}else{myTP=0;}}else
   if(OPType==5){if(ManualPrice==0)myPrice=myBid-StepOP*pt;else myPrice=ManualPrice;//SellStop
   if(StopLoss>0){mySL=myPrice-StopLoss*pt;}else{mySL=0;}
   if(TakeProfit>0){myTP=myPrice+TakeProfit*pt;}else{myTP=0;}}
   ticket=OrderSend(myPair,OPType,Lots,myPrice,Slippage,mySL,myTP,myCom,Magic,0,clrNONE);}

Penjelasan codebase :
2. Saat ingin melakukan order tinggal panggil aja dengan mengetikkan myOrderSend(isi parameternya)
3. Semua nilai sudah dalam ukuran satuan Pips, jadi jika kita mau input stoploss 20 pips dari harga order ya tinggal diisi aja parameter Stoplossnya dengan 20.
4. Codebase diatas sudah mencakup banyak kebutuhan dari ordersend.

oke sekian dulu pembahasannya nanti dilanjut lagi.
jika ada yang mau ditanyakan silahkan ditanyakan disini atau langsung hubungi ane langsung juga bisa, welcome kapan aja bagi yang mau bertanya seputar coding simple.

Kode yang bagus :):ok:

Saya bantu menyederhanakan kode di atas, supaya lebih mudah dipelajari, saya sertakan juga file untuk mempermudah belajar bagi yang kesulitan mengetikkan kode:

Code:
input double TakeProfit=100;
input double StopLoss=200;
int      Slippage=3,
         Magic=2022;
//-----------------------------------------------------------------------------------
void myOrderSend(int StepOP,double ManualPrice,int OPType,double Lots,double myStopLoss,double myTakeProfit,string myCom){
double pt=Digits==3||Digits==5?Point*10:Point;
double   myPrice  = OPType==0? Ask:(OPType==1? Bid:(OPType==2||OPType==3||OPType==4||OPType==5? ManualPrice:0)),
         mySL_buy = (StopLoss>0?   myPrice-StopLoss   *pt:0),
         myTP_buy = (TakeProfit>0? myPrice+TakeProfit *pt:0),
         mySL_sell= (StopLoss>0?   myPrice+StopLoss   *pt:0),
         myTP_sell= (TakeProfit>0? myPrice-TakeProfit *pt:0),
         //--------------------------------------------------
         mySL     =OPType==0||OPType==2||OPType==4? mySL_buy:mySL_sell,
         myTP     =OPType==0||OPType==2||OPType==4? myTP_buy:myTP_sell;
//--------------------------------------------------------
int ticket=OrderSend(_Symbol,OPType,Lots,myPrice,Slippage,mySL,myTP,myCom,Magic,0,clrNONE);}
 

Attachments

Murayrich mungkin ini bisa dicoba...

void OnTick(){
int JumlahOrder = OrdersTotal(); // untuk mengetahui jumlah order
double OpenCandel = Open[1]; //melihat nilai open candle sift 1
double CloseCandel= Close[1]; //melihat nilai close candle sift 1
double lot = 0.1;
bool SinyalBuy = false; // sebelum kita melihat kondisi candle sinyal harus false dulu
bool SinyalSell= false; // sebelum kita melihat kondisi candle sinyal harus false dulu

if(OpenCandel<CloseCandel)SinyalBuy =1; // menentukan sinyal buy jika candle naik
if(OpenCandel>CloseCandel)SinyalSell =1;

if(SinyalBuy) close(1); // close sell saat sinyal buy ( candle up)
if(SinyalSell)close(0); // close buy saat sinyal sell( candle down)

//OrderSend(symb,cmd,lot,price . . . .
if(JumlahOrder<1&&SinyalBuy==true ) { OrderSend( Symbol(), 0, lot, Ask, 3, 0, 0, "", 0, 0,clrNONE );}// send buy
if(JumlahOrder<1&&SinyalSell==true ) { OrderSend( Symbol(), 1, lot, Bid, 3, 0, 0, "", 0, 0,clrNONE );}// send sell
// perbedaan order send buy dan sell ada pada cmd dan price
}

void close( int x ) // x adalah tipe OP yang akan di close 0=buy 1=sell
{ // ini mulai start fungsi
for (int i= OrdersTotal()-1 ; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if (OrderSymbol() == Symbol()&&OrderType()==x )
{ if(OrderType()==0 ) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue); }
if(OrderType()==1 ) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Red); }
}
}
}

Mantap bro!
Bisa bikin teman-teman di forum sadar bahwa bikin EA sesungguhnya mudah, jadi mereka yang sedang belajar akan makin semangat dan termotivasi.

BTW .. Kode EA di atas bisa dibuat sedikit lebih singkat lagi, karena syarat untuk OrderSend adalah: "if(JumlahOrder<1 ... " maka pasti hanya ada 1 opening trade.
Berarti " for (int i= OrdersTotal()-1 ; i >= 0; i--) " bisa dihilangkan, karena untuk closing satu opening trade tidak diperlukan looping.
Semoga sharing ini bermanfaat .. selamat belajar dan tetap semangat. :):ok:
 
gan bantuin dong ini saya ada script ea mau nambahin marti nya eror trus,ini pengennya kalau sudah kena sl lgsg OP lagi di titik sl trsebut dengn double lot


//+------------------------------------------------------------------+
//|
//|
//|
//+------------------------------------------------------------------+
#property copyright "KUSUS UNTUK SODAKOH "
#property link "rejeki.com"
#property version "1.00"
#property strict
#define __STRATEGY_MAGIC 557835
#define __SLEEP_AFTER_EXECUTION_FAIL 400
//Input Variable
input double _Take_Profit = 500; //Bismillah
input double _Stop_Loss = 1000; //belum rejeki
input int _EMA_60 = 60; // goib 1
input int _SMA_90 = 90; // goib 2
//input int _EMA_70 = 70; // EMA 3
input double _Lot = 0.01; //Lot
//GLOBAL DECLARATION
double _iEMA_60;
double _iSMA_90;
int init() {
return(0);
}

//Local Declaration
bool _Buy = false;
bool _Sell = false;

_iEMA_60 = iMA ("",0,_EMA_60,0,MODE_EMA,0,0);
_iSMA_90 = iMA ("",0,_SMA_90,0,MODE_EMA,0,0);

//--------------------- ARGUMEN EA ------------------
// ARGUMEN BUY
if(_iEMA_60 > _iSMA_90 &&
iClose ("",0,1) > _iEMA_60 &&
_iEMA_60 > iOpen("",0,1)) _Buy = Buy (1,_Lot,0,_Stop_Loss,0,_Take_Profit,10,1,0,"");

//ARGUMEN SELL
if(_iEMA_60 < _iSMA_90 &&
iClose ("",0,1) < _iEMA_60 &&
_iEMA_60 < iOpen("",0,1)) _Sell = Sell (1,_Lot,0,_Stop_Loss,0,_Take_Profit,10,1,0,"");
return(0);
}
bool __selectOrderByMagic(int magic, string symbol)
{
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == __STRATEGY_MAGIC + magic && OrderSymbol() == symbol)
return(true);
}
return(false);
}
bool Close_All_Trades(int MagicIndex)
{
int total = OrdersTotal();
int type;
bool result = true;
for(int i=total-1;i>=0;i--)
{
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
type = OrderType();
if(type == OP_BUY)
{
if(!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ))
{
result = false;
Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
}
}else if(type == OP_SELL)
{
if(!OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ))
{
result = false;
Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
}
}
}
return(result);
}
bool Buy(int MagicIndex, double Lots, int StopLossMethod, double StopLossPoints, int TakeProfitMethod, double TakeProfitPoints, int Slippage, int MaxOpenTrades,
int MaxFrequencyMins, string TradeComment)
{
static double pipSize = 0;
if(pipSize == 0) pipSize = Point * (1 + 9 * (Digits == 3 || Digits == 5));
double sl = 0, tp = 0;
double stopLossPoints = 0, takeProfitPoints = 0;

int numberOfOpenTrades = 0;

for(int i=OrdersTotal()-1;i>=0;i--){
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
numberOfOpenTrades ++;
}
if(MaxOpenTrades > 0 && numberOfOpenTrades >= MaxOpenTrades) return(false);
if(MaxFrequencyMins > 0)
{
int recentSeconds = MaxFrequencyMins * 60;
for(int i=OrdersTotal()-1;i>=0;i--){
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
}
int hstTotal=OrdersHistoryTotal();

for(int i=hstTotal-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
break;
}
}

if(Lots < MarketInfo(Symbol(),MODE_MINLOT)) return(false);

if(AccountFreeMarginCheck(Symbol(), OP_SELL,Lots) <= 0) {
Print("Buy error: insufficient capital");
return(false);
}
if (StopLossPoints > 0)
{
if(StopLossMethod == 0)
{
sl = NormalizeDouble(Ask - StopLossPoints * Point, Digits);
stopLossPoints = StopLossPoints;
}
else if (StopLossMethod == 1)
{
sl = NormalizeDouble(Ask - StopLossPoints * pipSize, Digits);
stopLossPoints = StopLossPoints * (1 + 9 * (Digits == 3 || Digits == 5));
}
else
{
sl = StopLossPoints;
stopLossPoints = (Ask - sl)/Point;
}
}

if (TakeProfitPoints > 0)
{
if(TakeProfitMethod == 0)
{
tp = NormalizeDouble(Ask + TakeProfitPoints * Point, Digits);
takeProfitPoints = TakeProfitPoints;
}
else if (TakeProfitMethod == 1)
{
tp = NormalizeDouble(Ask + TakeProfitPoints * pipSize, Digits);
takeProfitPoints = TakeProfitPoints * (1 + 9 * (Digits == 3 || Digits == 5));
}
else
{
tp = TakeProfitPoints;
takeProfitPoints = (tp - Ask)/Point;
}
}

double stopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD);

if( (sl > 0 && stopLossPoints <= stopLevel) || (tp > 0 && takeProfitPoints <= stopLevel) )
{
Print("Cannot Buy: Stop loss and take profit must be at least "
+ DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD),0)
+ " points away from the current price");
return (false);
}

RefreshRates();
int result = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, sl, tp, "EA BUY " + "(" + WindowExpertName() + ") " + TradeComment, __STRATEGY_MAGIC + MagicIndex);

if (result == -1){
Print("Failed to Buy: " + IntegerToString(GetLastError()));
Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
return(false);
}

return(true);
}
bool Sell(int MagicIndex, double Lots, int StopLossMethod, double StopLossPoints, int TakeProfitMethod, double TakeProfitPoints, int Slippage, int MaxOpenTrades,
int MaxFrequencyMins, string TradeComment)
{
static double pipSize = 0;
if(pipSize == 0) pipSize = Point * (1 + 9 * (Digits == 3 || Digits == 5));
double sl = 0, tp = 0;
double stopLossPoints = 0, takeProfitPoints = 0;
int numberOfOpenTrades = 0;

for(int i=OrdersTotal()-1;i>=0;i--){
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
numberOfOpenTrades ++;
}
if(MaxOpenTrades > 0 && numberOfOpenTrades >= MaxOpenTrades) return(false);

if(MaxFrequencyMins > 0)
{
int recentSeconds = MaxFrequencyMins * 60;
for(int i=OrdersTotal()-1;i>=0;i--){
if(!OrderSelect(i, SELECT_BY_POS)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
}
int hstTotal=OrdersHistoryTotal();

for(int i=hstTotal-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
break;
}
}

if(Lots < MarketInfo(Symbol(),MODE_MINLOT)) return(false);

if(AccountFreeMarginCheck(Symbol(), OP_SELL,Lots) <= 0) {
Print("Sell order error: insufficient capital");
return(false);
}

if (StopLossPoints > 0)
{
if(StopLossMethod == 0)
{
sl = NormalizeDouble(Bid + StopLossPoints * Point, Digits);
stopLossPoints = StopLossPoints;
}
else if(StopLossMethod == 1)
{
sl = NormalizeDouble(Bid + StopLossPoints * pipSize, Digits);
stopLossPoints = StopLossPoints * (1 + 9 * (Digits == 3 || Digits == 5));
}
else
{
sl = StopLossPoints;
stopLossPoints = (sl - Bid)/Point;
}
}
if (TakeProfitPoints > 0)
{
if(TakeProfitMethod == 0)
{
tp = NormalizeDouble(Bid - TakeProfitPoints * Point, Digits);
takeProfitPoints = TakeProfitPoints;
}
else if (TakeProfitMethod == 1)
{
tp = NormalizeDouble(Bid - TakeProfitPoints * pipSize, Digits);
takeProfitPoints = TakeProfitPoints * (1 + 9 * (Digits == 3 || Digits == 5));
}
else
{
tp = TakeProfitPoints;
takeProfitPoints = (Bid - tp)/Point;
}
}

double stopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD);

if( (sl > 0 && stopLossPoints <= stopLevel) || (tp > 0 && takeProfitPoints <= stopLevel) )
{
Print("Cannot Sell: Stop loss and take profit must be at least "
+ DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD),0)
+ " points away from the current price");
.......
}

Ehm, ini masih ada kesalahan sintaks dan juga logika. Itulah sebabnya EA tidak bisa berfungsi dengan benar.
Boleh tahu tujuan dari penggunaan " OrdersHistoryTotal() " pada EA ini?
" OrdersHistoryTotal() " digunakan untuk mengetahui order sebelumnya yang sudah CLOSE, mungkin tidak diperlukan dalam proses untuk membentuk fungsi martingale pada suatu EA, kecuali programmer EA-nya memang punya tujuan lain selain dari pembentukan fungsi martingale.

Salam hangat dan semangat selalu bro! :):ok:
 
Back
Top