Tuesday, 18 September 2012

Pelajaran Coding no 1


//+------------------------------------------------------------------+
//|                                                EA BykovTrend.mq4 |
//|                                             Dwi M ea@dwim.web.id |
//|                                            http://ea.dwim.web.id |
//+------------------------------------------------------------------+
#property copyright "Dwi M ea@dwim.web.id"
#property link      "http://ea.dwim.web.id"

extern   double   Lot            = 1.0;
extern   int      Slippage       = 3;
extern   bool     UseTrailing    = true;
extern   int      TrailingStop   = 15;

double            dPoint         = 0;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----

   CekDigitBroker(); // Periksa Jumlah Digit Broker
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int      iTrade=0;
   double   dUptrend=0, dDowntrend=0;

   dUptrend    =  iCustom(Symbol(),0,"BykovTrend",3,9,500,0,0);
   dDowntrend  =  iCustom(Symbol(),0,"BykovTrend",3,9,500,1,0);
   
   Comment("EA BykovTrend\nDwi M ea@dwim.web.id\nhttp://ea.dwim.web.id","\ndUptrend :",dUptrend,"\ndDowntrend :",dDowntrend,"\nHour :",Hour());

   if(OrdersTotal()<1)
   {
      if((Hour()==1) && (dUptrend==1 && dDowntrend==0))
      {
         Buy();      
      }
      else if((Hour()==1) && (dUptrend==0 && dDowntrend==1))
      {
         Sell();
      }
   }
   
   for(iTrade=0;iTrade<OrdersTotal();iTrade++)
   {
      OrderSelect(iTrade,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
      {
         if(Hour()==23)
         {
            OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);  
         }
         
         BuyTrailing();
      }
      else if(OrderType()==OP_SELL && OrderSymbol()==Symbol())  
      {
         if(Hour()==23)
         {
            OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);  
         }
         
         SellTrailing();
      }
   }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+


/* Kumpulan Library */

//Memeriksa jumlah digit broker
void CekDigitBroker()
{
   if(Digits==3 || Digits==5)
   {
      dPoint = Point*10;
   }
   else if(Digits==2 || Digits==4)
   {
      dPoint = Point;
   }
}

/* Melakukan OP Buy */
void Buy()
{
    OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,0);        

}

/* Melakukan OP Sell */
void Sell()
{
    OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,0,0,0);
}

/* Melakukan Trailing untuk Open Order Buy */
void BuyTrailing()
{

   if(UseTrailing==true)
   {
      if(Bid-OrderOpenPrice()>dPoint*TrailingStop)
      {
        if(OrderStopLoss()<Bid-dPoint*TrailingStop)
        {
             OrderModify(OrderTicket(),OrderOpenPrice(),Bid-dPoint*TrailingStop,OrderTakeProfit(),0,Green);
             return(0);
        }
      }
   }

}

/* Melakukan Trailing untuk Open Order Sell */
void SellTrailing()
{

   if(UseTrailing==true)
   {
     if((OrderOpenPrice()-Ask)>(dPoint*TrailingStop))
     {
         if((OrderStopLoss()>(Ask+dPoint*TrailingStop)) || (OrderStopLoss()==0))
         {
              OrderModify(OrderTicket(),OrderOpenPrice(),Ask+dPoint*TrailingStop,OrderTakeProfit(),0,Red);
              return(0);
         }
     }
   }
}

No comments:

Post a Comment