Thursday, 17 January 2013

Belajar Membuat EA Sederhana 22 Martingel (Jebakan II)


Pastikan sudah mengikuti part 21 disini

Pada posting kali ini saya akan mengedit EA pada part 21 untuk ditambahi martingel .
Martingel yang di maksud juga mengacu pada jarak (range) order dan pergantian candel untuk mencegah terjadinya beberapa order ketika terjadi lonjakan harga dan candel panjang , seperti pada part 18

Penambahan coding martingelnya :

1. Fungsi untuk perintah order martingelnya

if(trad()==1 && wt!=Time[0] && jumlahorder(1)==0 && jumlahorder(0)<Max_order ) {OrderSend(Symbol(),OP_BUY,xlot(0),Ask,3,slb, tpb,Nama_EA,Magic,0,Blue); wt=Time[0]; }
if(trad()==2 && wk!=Time[0] && jumlahorder(0)==0 && jumlahorder(1)<Max_order ) {OrderSend(Symbol(),OP_SELL,xlot(1),Bid,3,sls, tps,Nama_EA,Magic,0,Red); wk=Time[0];  }

2. Fungsi untuk perhitungan jarak order dan  perintah ketika jarak terpenuhi


int trad()
{
 int type,dp; double opb,ops,lastlotb,lastlots; 
  for (int i = 0; i < OrdersTotal(); i++) {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
     type=OrderType();
   if(type==0)opb = OrderOpenPrice();
   if(type==1)ops = OrderOpenPrice();
  }
   double as = opb - Jarak_order * pt;
   double bi = ops + Jarak_order * pt;
   if ( Ask <= as &&  jumlahorder(0)> 0 ) dp=1; 
 if ( Bid >= bi &&  jumlahorder(1)> 0 ) dp=2; 

return(dp);
}

3. Fungsi untuk perhitungan Lots setiap pengulangan ordernya
double xlot(int m)
{
 double ylot,dlot; 
  for (int i = 0; i < OrdersTotal(); i++) {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic ||OrderType()!=m) continue;
     dlot=OrderLots();
     }
if(OrderType()==m)ylot=NR(Lots*MathPow(DiMarti,jumlahorder(m)));
return(ylot);
}


Nah setelah di rakit begini deh hasinya:


//+------------------------------------------------------------------+
//|                                                    EA Jum+Profit |
//|                                                gifaesa@yahoo.com |
//|                                     http://JumForex.blogspot.com |
//+------------------------------------------------------------------+

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

extern string  Nama_EA                 = "Jum+Profit";
extern bool    Trade_buystop           = true;
extern bool    Trade_sellstop          = true;
extern string  Jam_server              = "Set sesuai selera";
extern int     Jam                     = 0;
extern int     Menit                   = 30;
extern bool    Tp_in_Money             = false;
extern double  TP_in_money             = 5;
extern int     Jarak_order             = 35;
extern int     Max_order               = 10;
extern double  DiMarti                 = 1.3;
extern int     TP                      = 100;
extern int     SL                      = 20;
extern double  Lots                    = 0.1;
extern int     Jarak_stop              = 25;
extern bool    TrailingStop_           = true;
extern int     TrailingStop            = 12;
extern int     Magic                   = 69;
double slb,tpb,sls,tps,pt,lslb,ltpb;
int wt,wk,tiket,ticet;
//+------------------------------------------------------------------+
//| 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()
  {
label();
if(TrailingStop_)dtrailing();
if(Tp_in_Money ){
  if(TP_in_money<= money(0)){ 
   closeall(0);
   closeall(2);
   }
  if(TP_in_money<= money(1)){ 
   closeall(1);
   closeall(3);
   }
 }
if(jumlahorder(0)==1 && jumlahorder(1)==0){ 
   closeall(5);
   }
if(jumlahorder(0)==0 && jumlahorder(1)==1){ 
   closeall(4);
   }
if(Hour()==23){
   closeall(4);
   closeall(5);
   }
//----
   double as = Ask + Jarak_stop*pt;
   double bi = Bid - Jarak_stop*pt;
   if(SL==0)slb=0;else slb=as-SL*pt;
   if(SL==0)sls=0;else sls=bi+SL*pt;
   if(TP==0)tpb=0;else tpb=as+TP*pt;
   if(TP==0)tps=0;else tps=bi-TP*pt;
if(DayOfWeek()!=1 && Hour()==Jam && Minute()==Menit && jumlahorder(0)==0 && jumlahorder(1)==0){
  if(jumlahorder(4)==0  && Trade_buystop  ) {OrderSend(Symbol(),OP_BUYSTOP,NR(Lots),as,3,slb, tpb,Nama_EA,Magic,0,Blue);}
  if(jumlahorder(5)==0  && Trade_sellstop ) {OrderSend(Symbol(),OP_SELLSTOP,NR(Lots),bi,3,sls, tps,Nama_EA,Magic,0,Red);}
 }
if(trad()==1 && wt!=Time[0] && jumlahorder(1)==0 && jumlahorder(0)<Max_order ) {OrderSend(Symbol(),OP_BUY,xlot(0),Ask,3,slb, tpb,Nama_EA,Magic,0,Blue); wt=Time[0]; }
if(trad()==2 && wk!=Time[0] && jumlahorder(0)==0 && jumlahorder(1)<Max_order ) {OrderSend(Symbol(),OP_SELL,xlot(1),Bid,3,sls, tps,Nama_EA,Magic,0,Red); wk=Time[0];  }

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

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(int m)
{
 for (int i = OrdersTotal() - 1; i >= 0; i--) {
  OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
  if (OrderSymbol() != Symbol() || OrderMagicNumber()!=Magic /*|| m!= OrderType()*/) continue;
  if (OrderType() == m) OrderDelete(OrderTicket());
  if (OrderType() == m) OrderDelete(OrderTicket());
  if (OrderType() == m) OrderClose(OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
  if (OrderType() == m)OrderClose(OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
 }
}
double money(int m)
{
 double dp = 0;
 int i;
 for (i = 0; i < OrdersTotal(); i++) {
  OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
  if (OrderSymbol() != Symbol()  || OrderMagicNumber()!=Magic || m!=OrderType()) continue;
  dp += OrderProfit();
 }
 return(dp);
}

void label()
{
 Comment("\n ",
   "\n ",
   "\n ------------------------------------------------",
   "\n :: =>+Jum+Profit<=",
   "\n :: =>JumForex.blogspot.com<=",
   "\n ------------------------------------------------",
   "\n :: Spread                 : ", MarketInfo(Symbol(), MODE_SPREAD),
   "\n :: Leverage               : 1 : ", AccountLeverage(),
   "\n :: Equity                 : ", AccountEquity(),
   "\n :: Jam Server             :", Hour(), ":", Minute(),
   "\n ------------------------------------------------",
   "\n :: Floting buy : ",money(0),
   "\n :: Floting sell : ",money(1),
   "\n ------------------------------------------------",
   "\n :: >>By: Jum69<<",
   "\n ------------------------------------------------");
}
int trad()
{
 int type,dp; double opb,ops,lastlotb,lastlots; 
  for (int i = 0; i < OrdersTotal(); i++) {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
     type=OrderType();
   if(type==0)opb = OrderOpenPrice();
   if(type==1)ops = OrderOpenPrice();
  }
   double as = opb - Jarak_order * pt;
   double bi = ops + Jarak_order * pt;
   if ( Ask <= as &&  jumlahorder(0)> 0 ) dp=1; 
 if ( Bid >= bi &&  jumlahorder(1)> 0 ) dp=2; 

return(dp);
}
double xlot(int m)
{
 double ylot,dlot; 
  for (int i = 0; i < OrdersTotal(); i++) {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic ||OrderType()!=m) continue;
     dlot=OrderLots();
     }
if(OrderType()==m)ylot=NR(Lots*MathPow(DiMarti,jumlahorder(m)));
return(ylot);
}

Ini hasil BTnya:



Okey seilahkan de coba ya...
Bila mana ada yang kurang jelas dan ada ide positif silahkan comment ya!

How brokers can mess with your account and take your money

Oh they can mess with you alright...

You've got to be warey when a broker rings you for business lol. Called me asking to try their new MT4 trading platform. Even the demo spreads are 33% larger than an average broker, no thanks.

I kept on getting trade context error 146, so I added some nice code to fix that... guess what... still get them, not as frequent but still messing with orders. Should expect that from a market making broker... wont be opening an account with them.

SD, have you ever noticed a significant delay in ticks on the chart your EA is trading every time you recompile that EA to be a little better? somethin fishy like EA transmission to broker me thinks...
I get my EA's to place an arrow on the chart its trading to provide a visual means that the signal happened on that very bar & time. If the trade doesn't go through I know that its broker manipulation then.



// my context busy code just before orderclose & ordersend, shouldn't get error 146

for (double tb=0; IsTradeContextBusy() && tb < 30; tb++) Sleep(100); //Wait upto 3 seconds (ie 30 x 100milliseconds)
if (tb > 0) Print("Trade context was busy ", DoubleToStr(100 * tb / 1000, 2)," seconds");
if (!IsTradeAllowed()) return;


// my arrow codes to tell me that my EA had a signal but if No Trade was executed? That would more than make me suspicious, I'd have proof!

// Place code before or after ordersend BUY

ObjectCreate("X137_Buy_" + Time[0], OBJ_ARROW, 0, TimeCurrent(), Bid);
ObjectSet("X137_Buy_" + Time[0], OBJPROP_ARROWCODE, 228);
ObjectSet("X137_Buy_" + Time[0], OBJPROP_COLOR, ArrowUP);

// Place code before or after ordersend SELL

ObjectCreate("X137_Sell_" + Time[0], OBJ_ARROW, 0, TimeCurrent(), Bid);
ObjectSet("X137_Sell_" + Time[0], OBJPROP_ARROWCODE, 230);
ObjectSet("X137_Sell_" + Time[0], OBJPROP_COLOR, ArrowDOWN);