Navigation´╝ÜHome > Content >

G101.mq4

Time: 2013-02-25 | Download file:G101.mq4

//+------------------------------------------------------------------+
//|                                                         G101.mq4 |
//|                        Copyright 2012, Darren, Angel65 & dare123 |
//|              http://www.forexfactory.com/showthread.php?t=393470 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, Darren, Angel65 & dare123"
#property link      "http://www.forexfactory.com/showthread.php?t=393470"
#include 
#include 

 extern int Time_Diff=2;         //Broker time minus GMT time in advance. 2=2 hour faster than GMT.
 extern bool BO_ONOFF=true;      //Breakout+Grid. ON=true and OFF=false
 extern int UK00=0600;           //UK breakout initial of time 
 extern int UK01=0800;           //UK breakout end of time or initial trading period
 extern int UK02=1600;           //UK end of trading period
 extern int US00=1100;           //US breakout initial of time 
 extern int US01=1300;           //US breakout end of time or initial trading period
 extern int US02=2200;           //US end of trading period
 extern int SD=40;               //Stop loss distant in pips
 extern int TD=20;               //Target distant in pips
 extern int GD=20;               //Grid size in pips
 extern int DMax=200;            //Grid size max either uptrend or downtrend in pips
 extern double Lots_P =20;       //Free margin in percentage. 2=2.00% of free margin
 extern double Max_Profit=200;   //Max profit in percentage. 2=2.00% of account balance

 int Magic_Num;
 bool Ini_01=true;
 bool Ini_02=true; 
 bool Ini_03=true; 
 bool Ini_04=false;  
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
      Magic_Num = CurTime()/10000;
      return(0);
  }
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
      return(0);
  }
  
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {        
      if (BO_ONOFF==true)
         {
            if (Ini_01==true && Time_Check(UK01,UK02)==true)    
               {
                  string SN="UK";
                  Order_E0(UK00,UK01,SN);    
                  Ini_01=false;
               }
            if (Ini_02==true && Time_Check(US01,US02)==true)    
               {
                  SN="US";
                  Order_E0(US00,US01,SN);    
                  Ini_02=false;           
               }
         } 
         
      if (BO_ONOFF!=true)
         {
            if (Ini_01==true && Time_Check(UK01,UK02)==true)    
               {
                  SN="UK";
                  Order_E1(SN);             
                  Ini_01=false;
               }
            if (Ini_02==true && Time_Check(US01,US02)==true)    
               {
                  SN="US";
                  Order_E1(SN);             
                  Ini_02=false;    
               }       
         }          
         
      if (Ini_03==true && Time_Check(UK01,UK02)!=true)  
         {
            CO_UK();  
            Ini_03=false;   
            Ini_04=true;
         }
         
      if (Ini_04==true && Time_Check(US01,US02)!=true)  
         {
            CO_US();  
            Ini_01=true;    
            Ini_02=true;   
            Ini_03=true;   
            Ini_04=false;
         }                   
      if (Profit_Check()==true)    
         {
            CO_UK();
            CO_US();
         }
 }
  
//+------------------------------------------------------------------+
//| Time Function                                                    |
//+------------------------------------------------------------------+
 bool Time_Check(int &T01,int &T02)
    {  
      int RT;
      int GMT = ((TimeHour(CurTime())-Time_Diff)*100)+TimeMinute(CurTime());
      
      if (GMT < 0)                  RT=GMT+2400;
      if (GMT >= 2400)              RT=GMT-2400;
      if (GMT >=0 && GMT < 2400)    RT=GMT;
      
      if (RT >= T01 && RT < T02)  return(true);
   }
     
//+------------------------------------------------------------------+
//| Order Entry Function                                             |
//+------------------------------------------------------------------+
 void Order_E0(int &x,int &y,string &SN)
   {  
      string TimeBegin = StringConcatenate((x/100)+Time_Diff,":00");   //If time more than 9:00 is errors
      string TimeEnd = StringConcatenate((y/100)+Time_Diff,":00");     //If time more than 9:00 is errors
      int TimeNow = CurTime()-(Time_Diff*3600);
        
      datetime dtTimeBegin = StrToTime(TimeToStr(TimeNow, TIME_DATE) + " " + TimeBegin);
      datetime dtTimeEnd = StrToTime(TimeToStr(TimeNow, TIME_DATE) + " " + TimeEnd);
       
      int iBarBegin = iBarShift(Symbol(), 0, dtTimeBegin);
      int iBarEnd = iBarShift(Symbol(), 0, dtTimeEnd);  
        
      double PriceHigh = High[iHighest(Symbol(), 0, MODE_HIGH, iBarBegin-iBarEnd, iBarEnd)];
      double PriceLow  = Low [iLowest (Symbol(), 0, MODE_LOW , iBarBegin-iBarEnd, iBarEnd)];
      
      double point;
      for (int a=GD;a<=DMax;a=a+GD)
         {  
            Price_Check(SD,TD,GD,point);
            double EnL = PriceHigh + a*point;
            double SptL = EnL - SD*point;
            double TgtL = EnL + TD*point;       
            
            double EnS = PriceLow - a*point;
            double SptS = EnS + SD*point;
            double TgtS = EnS - TD*point;              
            
            double Lots=LotsCalc();
            OrderSend(Symbol(),OP_BUYSTOP,Lots,EnL,3,SptL,TgtL,SN,Magic_Num,0,Blue); 
            OrderSend(Symbol(),OP_SELLSTOP,Lots,EnS,3,SptS,TgtS,SN,Magic_Num,0,Blue);
        }
   }  

//+------------------------------------------------------------------+
//| Order Entry Function                                             |
//+------------------------------------------------------------------+
 void Order_E1(string &SN)
   {  
      double point;
      for (int a=GD;a<=DMax;a=a+GD)
         {  
            Price_Check(SD,TD,GD,point);
            double EnL = Ask + a*point;
            double SptL = EnL - SD*point;
            double TgtL = EnL + TD*point;       
            
            double EnS = Bid - a*point;
            double SptS = EnS + SD*point;
            double TgtS = EnS - TD*point;             
            
            double Lots=LotsCalc();
            OrderSend(Symbol(),OP_BUYSTOP,Lots,EnL,3,SptL,TgtL,SN,Magic_Num,0,Red); 
            OrderSend(Symbol(),OP_SELLSTOP,Lots,EnS,3,SptS,TgtS,SN,Magic_Num,0,Red);
        }
   } 
     
//+------------------------------------------------------------------+
//| Broker Pricing Check Function                                    |
//+------------------------------------------------------------------+
 void Price_Check(int &SD,int &TD,int &GD,double &point)
   {  
      int Min_Dist = MarketInfo(Symbol(),MODE_STOPLEVEL);                
      if (SD < Min_Dist)
         {  
            SD = Min_Dist;                                            
            Alert("Stop Loss Distant must be minimum = ",Min_Dist);
         }                                                           
      if (TD < Min_Dist)
         {  
            TD = Min_Dist;                                            
            Alert("Target Distant must be minimum = ",Min_Dist);
         }       
      if (GD < Min_Dist)
         {  
            GD = Min_Dist;                                            
            Alert("Confluence Distant must be minimum = ",Min_Dist);
         }
               
      point = MarketInfo(Symbol(),MODE_POINT);
      if (Digits==5)  point=0.00001*10;
      if (Digits==4)  point=0.0001;
      if (Digits==3)  point=0.001;
      if (Digits<3)   Alert("Broker digits is less than 3");                                                           
   }    

//+------------------------------------------------------------------+
//| Close UK Order Function                                          |
//+------------------------------------------------------------------+
 void CO_UK()
   {  
      int ticket;
      
      RefreshRates();
      for (int i=OrdersTotal()-1;i>=0;i--)
         {  
            OrderSelect(i,SELECT_BY_POS);
            if (OrderMagicNumber()==Magic_Num && OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderComment()=="UK" && OrderCloseTime()==0)      
               {  
                  ticket=OrderTicket();
                  double Lots=OrderLots();
                  OrderClose(ticket,Lots,Bid,3); 
               }
            if (OrderMagicNumber()==Magic_Num && OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderComment()=="UK" && OrderCloseTime()==0)      
               {  
                  ticket=OrderTicket();
                  Lots=OrderLots();
                  OrderClose(ticket,Lots,Ask,3); 
               }            
            if (OrderMagicNumber()==Magic_Num && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) && OrderComment()=="UK" && OrderCloseTime()==0)  
               {
                  ticket=OrderTicket();
                  OrderDelete(ticket);
               }      
         }
   } 

//+------------------------------------------------------------------+
//| Close US Order Function                                          |
//+------------------------------------------------------------------+
 void CO_US()
   {  
      int ticket;
      
      RefreshRates();
      for (int i=OrdersTotal()-1;i>=0;i--)
         {  
            OrderSelect(i,SELECT_BY_POS);
            if (OrderMagicNumber()==Magic_Num && OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderComment()=="US" && OrderCloseTime()==0)      
               {  
                  ticket=OrderTicket();
                  double Lots=OrderLots();
                  OrderClose(ticket,Lots,Bid,3); 
               }
            if (OrderMagicNumber()==Magic_Num && OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderComment()=="US" && OrderCloseTime()==0)      
               {  
                  ticket=OrderTicket();
                  Lots=OrderLots();
                  OrderClose(ticket,Lots,Ask,3); 
               }            
            if (OrderMagicNumber()==Magic_Num && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) && OrderComment()=="US" && OrderCloseTime()==0)  
               {
                  ticket=OrderTicket();
                  OrderDelete(ticket);
               }      
         }
   }    
   
//+------------------------------------------------------------------+
//| Order Profit Check Function                                      |
//+------------------------------------------------------------------+
 bool Profit_Check()
   {
      double dblProfit=0;
      int intPOS=0;
      bool boolTerm=false;
      
      while(boolTerm==false)
         {
            if(OrderSelect(intPOS,SELECT_BY_POS))
               {
                  if(OrderMagicNumber()==Magic_Num) dblProfit=dblProfit+OrderProfit();  
                  intPOS++;
               }
            else
                  boolTerm=true;
         }
      double AcBal = NormalizeDouble(AccountBalance(),2);
      if (dblProfit>=Max_Profit/100*AcBal)  return(true);
   }
   
//+------------------------------------------------------------------+
//| Lot size Function                                                |
//+------------------------------------------------------------------+   
 double LotsCalc() 
   {
      if(MarketInfo(Symbol(),MODE_MINLOT) == 0.1) int LotsDigit = 1;
      else if(MarketInfo(Symbol(),MODE_MINLOT) == 0.01) LotsDigit = 2;
      double MinLots = NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),LotsDigit);
      double AcFrMar = NormalizeDouble(AccountFreeMargin(),2);
   
      double Lots = (AcFrMar*(Lots_P/100))/1000;
   
      if(Lots > MinLots)   return(Lots); 
      if(Lots <= MinLots)  return(MinLots); 
   }

Recommend