Tuesday, 25 December 2012

Belajar Membuat EA Sederhana 13


http://jumforex.blogspot.com/2012/10/belajar-membuat-ea-sederhana-13.html

Pastikan sebelumnya sudah membaca di part 12 disini

Pada bagian ini saya akan coba membahas tentang EA dari crossing MA permintaan seorang trader sbb:
"Saya minta tolong untuk dibuatkan EA dari indicator MA4 Open dan MA4 Close master "



Kita buat costum dan logikanya dulu  sbb:

double MA_1O =iMA(Symbol(),0,Pereode_1,0,MODE_SMA,PRICE_OPEN ,1);
double MA_1C =iMA(Symbol(),0,Pereode_1,0,MODE_SMA,PRICE_CLOSE ,1);
double MA_2O =iMA(Symbol(),0,Pereode_2,0,MODE_SMA,PRICE_OPEN ,2);
double MA_2C =iMA(Symbol(),0,Pereode_2,0,MODE_SMA,PRICE_CLOSE ,2);

int signal;
if(MA_2O >MA_2C && MA_1O < MA_1C) signal=1;// aturan buy
if(MA_2O <MA_2C && MA_1O > MA_1C) signal=2;//aturan sell

logika diatas biasa digunakan untuk crosing MA

Nah pada EA yang akan kita buat ini saya akan tambahkan untuk tp in money   (hiden tp dengan acuan jumlah profit daam money $ )

kita buat fungsi untuk menghitung profit dalam $


double money()
{
double dp = 0;
int i;
for (i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol()  || OrderMagicNumber()!=Magic) continue;
dp += OrderProfit();
}
return(dp);
}

Langkah berikutnya adalah membuat fungsi closeall yaitu untuk menutup semua order ketika tpin money tercapai:

void closeall()
{
for (int i = OrdersTotal() - 1; i >= 0; i--) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber()!=Magic ) continue;
if (OrderType() > 1) OrderDelete(OrderTicket());
else {
if (OrderType() == 0) OrderClose(OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
else               OrderClose(OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
}
}
}

Jadi pembuatan ea kali ini ada penambahan tp in money sehingga pada fungsi diatas bisa dibuat perintah untuk mengeksekusi  tp in money

if(tp_in_money<= money()) closeall();

Jadi ketika  profit sudah lebih besar atau sama dengan tp_in_money maka semua order di closeall

pada Ea yang kita buat kali ini EA yang hanya 1 arah (bukan hedge)

Nah dari fungsi2 diatas kita gabungkan dengan EA pada part 12 , sehingga jadinya sebagai berikut :


//+------------------------------------------------------------------+
//|                                             EA MA Cross AntiQ+   |
//|                                                gifaesa@yahoo.com |
//|                                     http://JumForex.blogspot.com |
//+------------------------------------------------------------------+

#property copyright "JumForex.blogspot.com"
#property link      "Gifaesa@yahoo.com"

extern string  Nama_EA                 = "Cross AntiQ+";
extern bool    Trade_buy               = true;
extern bool    Trade_sell              = true;
extern int     Mulai_Jam               = 6;
extern int     Akhir_Jam               = 20;
extern bool    Tp_in_Money             = true;
extern double  TP_in_money             = 2;
extern int     TP                      = 100;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     TrailingStop            = 20;
extern int     Magic                   = 69;
extern string  Seting_MA               = "Sesuka Hati";
extern int     Pereode_1                 = 4;
extern int     Pereode_2                 = 4;

double slb,tpb,sls,tps,pt;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits==3 || Digits==5) pt=10*Point;   else   pt=Point;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

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

dtrailing();
if(Tp_in_Money && TP_in_money<= money()) closeall();
//----
double MA_1O =iMA(Symbol(),0,Pereode_1,0,MODE_SMA,PRICE_OPEN ,1);
double MA_1C =iMA(Symbol(),0,Pereode_1,0,MODE_SMA,PRICE_CLOSE ,1);
double MA_2O =iMA(Symbol(),0,Pereode_2,0,MODE_SMA,PRICE_OPEN ,2);
double MA_2C =iMA(Symbol(),0,Pereode_2,0,MODE_SMA,PRICE_CLOSE ,2);

int signal;
if(MA_2O >MA_2C && MA_1O < MA_1C) signal=1;// aturan buy
if(MA_2O <MA_2C && MA_1O > MA_1C) signal=2;//aturan sell
if(Jam_trade()==1){
   if(SL==0)slb=0;else slb=Ask-SL*pt;
   if(SL==0)sls=0;else sls=Bid+SL*pt;
   if(TP==0)tpb=0;else tpb=Ask+TP*pt;
   if(TP==0)tps=0;else tps=Bid-TP*pt;
  if(jumlahorder(0)==0 && jumlahorder(1)==0 && Trade_buy  && signal==1) {OrderSend(Symbol(),OP_BUY,NR(Lots),Ask,3,slb, tpb,Nama_EA,Magic,0,Blue);}
  if(jumlahorder(0)==0 && jumlahorder(1)==0 && Trade_sell && signal==2) {OrderSend(Symbol(),OP_SELL,NR(Lots),Bid,3,sls, tps,Nama_EA,Magic,0,Red);}
 }

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

int Jam_trade()
{
   bool trade = false;
   if(Mulai_Jam > Akhir_Jam){
     if (Hour() >= Mulai_Jam || Hour() < Akhir_Jam) trade = true;
   } else
     if (Hour() >= Mulai_Jam && Hour() < Akhir_Jam) trade = true;

   return (trade);
}

int jumlahorder( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
  {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
     total++;
  }

return(total);
}
double NR(double thelot)
{
    double maxlots = MarketInfo(Symbol(), MODE_MAXLOT),
    minilot = MarketInfo(Symbol(), MODE_MINLOT),
    lstep = MarketInfo(Symbol(), MODE_LOTSTEP);
    double lots = lstep * NormalizeDouble(thelot / lstep, 0);
    lots = MathMax(MathMin(maxlots, lots), minilot);
    return (lots);
}

void dtrailing()
{
for(int i=0; i<OrdersTotal(); i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
        if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic ) continue;
        if(OrderType()==OP_BUY)  {
         if(Bid-OrderOpenPrice()>pt*TrailingStop)  {
           if((OrderStopLoss()<Bid-pt*TrailingStop) || (OrderStopLoss()==0)) {
              OrderModify(OrderTicket(),OrderOpenPrice(),Bid-pt*TrailingStop,OrderTakeProfit(),0,Green);
              return(0);
              }
              }
           }
        if(OrderType()==OP_SELL)  {
         if((OrderOpenPrice()-Ask)>(pt*TrailingStop)){
           if(OrderStopLoss()>(Ask+pt*TrailingStop) || (OrderStopLoss()==0)){
              OrderModify(OrderTicket(),OrderOpenPrice(),Ask+pt*TrailingStop,OrderTakeProfit(),0,Red);
              return(0);
              }
              }
           }
     }
  
}
void closeall()
{
for (int i = OrdersTotal() - 1; i >= 0; i--) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber()!=Magic ) continue;
if (OrderType() > 1) OrderDelete(OrderTicket());
else {
if (OrderType() == 0) OrderClose(OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
else               OrderClose(OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
}
}
}
double money()
{
double dp = 0;
int i;
for (i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol()  || OrderMagicNumber()!=Magic) continue;
dp += OrderProfit();
}
return(dp);
}

Jadi deh silahkan di coba untuk di test di buat sebagai tambahan wawasan

sampai jumpa di part berikutnya

Semoga sukses



2 comments:

  1. Hello Everyone,

    I've created a list of the most recommended FOREX brokers:
    1. Most Recommended Forex Broker
    2. eToro - $50 min. deposit.

    Here is a list of the best forex instruments:
    1. ForexTrendy - Recommended Odds Software.
    2. EA Builder - Custom Indicators Autotrading.
    3. Fast FX Profit - Secret Forex Strategy.

    I hope you find these lists beneficial.

    ReplyDelete
  2. QUANTUM BINARY SIGNALS

    Professional trading signals delivered to your mobile phone every day.

    Follow our trades today & make up to 270% a day.

    ReplyDelete