Download MetaTrader 5

The Graal

To add comments, please log in or register
Fitzgibbons
30
Fitzgibbons  

Hi, I've been toying with the Graal 2 Ea from this article, https://www.mql5.com/en/articles/1413 and back testing gives different results from Everytick and Open Price.

I was wondering if someone would be able to code in a parameter to only work and modify the orders for a new bar or Open price.  

My First "Grail"
My First "Grail"
  • 2006.09.05
  • Сергей Ковалев
  • www.mql5.com
Examined are the most frequent mistakes that lead the first-time programmers to creation of a "super-moneymaking" (when tested) trading systems. Exemplary experts that show fantastic results in tester, but result in losses during real trading are presented.
Files:
Grail_2_3.ex4 18 kb
Francis Dogbe
9546
Francis Dogbe  
Fitzgibbons:

Hi, I've been toying with the Graal 2 Ea from this article, https://www.mql5.com/en/articles/1413 and back testing gives different results from Everytick and Open Price.

I was wondering if someone would be able to code in a parameter to only work and modify the orders for a new bar or Open price.  

First, you need to provide the source code before anyone can help you.
Fitzgibbons
30
Fitzgibbons  
//????????????????????????????????????????????????????????????????????????????????????????????
// Graal_2.mq4 (Grail 2).
// Used as an example in the article My First "Grail".
// Sergey Kovalyov, Dnepropetrovsk (Ukraine),sk@mail.dnepr.net,ICQ 64015987, http://autograf.dp.ua/
//????????????????????????????????????????????????????????????????????????????????????????????
extern int TakeProfit=5;                            // TakeProfit orders
extern int StopLoss= 29;                            // StopLoss orders
extern int Distan   = 2;                            // Distance from the ?? line
extern int Cls      = 2;                            // Close at ** points of profit
extern int period_MA=16;                            // MA period 
extern bool UseStartEndTime = true;                  // Use start and end time
//extern int GMT_Offset=3;                            //Offset between your broker time and GMT
extern int StartHour   = 21;                         // Starting hour
extern int StartMin   = 0;                          // Starting minute
extern int EndHour   = 5;                           // Finishing hour
extern int EndMin   = 0;                            // Finishing minute
extern int Prots    = 0;                            // Percentage of free assets
extern int Magic    = 7395467;                      // Magic number
extern double EquityPercentFree = 30;               // Minimum percentage of free equity
extern bool UseEquityPercentFree = true;            // Use minimum free equity percentage
//--------------------------------------------------------------------------------------------
int
   Nom_bl,                                          // BuyLimit order number
   Nom_sl,                                          // SellLimit
   total,                                           // Count of lots
   bl = 0,                                          // 1 = BuyLimit order availability
   sl = 0,                                          // 1 = SellLimit order availability
   b  = 0,                                          // 1 = Buy order availability
   s  = 0;                                          // 1 = Sell order availability
//--------------------------------------------------------------------------------------------
double 
   OP,                                              // OpenPrice (absolute points)
   SL,                                              // StopLoss orders (relative points)
   TP,                                              // TakeProfit orders (relative points)
   dist,                                            // Distance from ?? (relative points)
   Level,                                           // Min.allowed distance of a pending order
   OP_bl,                                           // OpenPrice BuyLimit (absolute points)
   OP_sl,                                           // OpenPrice SellLimit(absolute points)
   cls,                                             // Close at ** profit (absolute points)
   MA,                                              // MA value (rate)
   spred,                                           // Spread (absolute points)
   Lot;                                             // Count of lots
//????????????????????????????????????????????????????????????????????????????????????????????
int init()
{   
   Level=MarketInfo(Symbol(),MODE_STOPLEVEL);       // Check what the server shows us
   Level=(Level+1)*Point;                           // ?:)
   SL=StopLoss*Point;                               // StopLoss orders (relative points)
   TP=TakeProfit*Point;                             // TakeProfit orders (relative points)
   dist=Distan*Point;                               // Distance from the MA line(relative points)
   cls=Cls*Point;                                   // Close at ** profit (absolute points)
   spred=Ask-Bid;                                   // Spread (absolute points)
   return(0);
} 
//????????????????????????????????????????????????????????????????????????????????????????????
int start()
{   
//============================================================================================
   total=OrdersTotal();                             // Count of lots
   bl=0;                                            // Zeroize at the start of the pass
   sl=0;                                            // Zeroize at the start of the pass
   b=0;                                             // Zeroize at the start of the pass
   s=0;                                             // Zeroize at the start of the pass
//--------------------------------------------------------------------------------------------
   if(Hour() == EndHour+3)
   {
      ObjectsDeleteAll();
   }
   
   for (int i=total; i>=0; i--)                     // For all orders
   {                                             
      if (OrderSelect(i,SELECT_BY_POS)==true &&     // Select an order
         OrderSymbol()==Symbol() &&
         OrderMagicNumber() == Magic)               // opened by this EA
      {
      
//--------------------------------------------------------------------------------------------
         if (OrderType()==OP_BUY)                   // Buy order
         {
            b =1;                                   // The order found
            Close_B(OrderTicket(),OrderLots());     // Close the order (the function decides
                                                    // whether it is necessary)
         }
//--------------------------------------------------------------------------------------------
         if (OrderType()==OP_SELL)                   // Sell order
         {
            s =1;                                   // The order found
            Close_S(OrderTicket(),OrderLots());     // Close the order (if necessary)
         }
//--------------------------------------------------------------------------------------------
         if (OrderType()==OP_BUYLIMIT)              // BuyLimit order
         {
            OP_bl=NormalizeDouble(OrderOpenPrice(),Digits);//OpenPrice BuyLimit(absolute points)
            Nom_bl=OrderTicket();
            bl=1;                                   // The order found
         }
//--------------------------------------------------------------------------------------------
         if (OrderType()==OP_SELLLIMIT)             // SellLimit order
         {
            OP_sl=NormalizeDouble(OrderOpenPrice(),Digits);//OpenPrice SellLimit(absolute points)
            Nom_sl=OrderTicket();
            sl=1;                                   // The order found
         }
//--------------------------------------------------------------------------------------------
      }
   }   
//--------------------------------------------------------------------------------------------
   MA = iMA(NULL,0, period_MA, 0,MODE_LWMA, PRICE_TYPICAL, 0);// The MA current value
   Modify_order();                                  // Activate modification
   Open_order();                                   // Activate opening
//============================================================================================

   if(UseEquityPercentFree)
   {
      if((AccountEquity()/AccountBalance())*100 <= EquityPercentFree)
      {
         for (i=total; i>=0; i--)
         {
            if (OrderSelect(i,SELECT_BY_POS)==true && OrderSymbol()==Symbol() && OrderMagicNumber() == Magic)
            {
               OrderClose(i, OrderLots(), Bid, 10, Red);
            }
         }
         DeleteSellStop();
         DeleteBuyStop();
         return(0);
      }
   }
   return(0);
}
//????????????????????????????????????????????????????????????????????????????????????????????
void Close_B(int Nomber, double lots)               // Close Buy orders
{
//============================================================================================
   if (NormalizeDouble(Bid-OrderOpenPrice(),Digits)>=cls)// If the preset profit is reached
   {
      OrderClose( Nomber, lots, Bid, 1, Yellow);    // Close
      b = 0;                                        // No Buy order anymore
   }
//============================================================================================
   return;
}
//????????????????????????????????????????????????????????????????????????????????????????????
void Close_S(int Nomber, double lots)               // Close Sell orders
{
//============================================================================================
   if (NormalizeDouble(OrderOpenPrice()-Ask,Digits)>=cls)// If the preset order is reached
   {
      OrderClose( Nomber, lots, Ask, 1, Yellow);    // Close
      s = 0;                                        // No Sell order anymore
   }
//============================================================================================
   return;
}
//????????????????????????????????????????????????????????????????????????????????????????????
void Modify_order()                                 // Modification of orders
{
//============================================================================================
   if (bl==1)                                       // If there is BuyLimit
   {
      OP=MA-dist;                                   // it must be located here
      if (MathAbs(OP_bl-OP)>0.5*Point)              // if it is not located here
      {
         OrderModify(Nom_bl,OP,OP-SL,OP+TP,0,DeepSkyBlue);// The order modification 
      }
   }
//--------------------------------------------------------------------------------------------
   if (sl==1)                                       // If there is SeelLimit
   {
      OP=MA+spred+dist;                             // It must be located here
      if (MathAbs(OP_sl-OP)>0.5*Point)              // If it is not located here
      {
         OrderModify( Nom_sl, OP, OP+SL, OP-TP, 0, Pink);// The order modification 
      }
   }
//============================================================================================
   return;
   }
//????????????????????????????????????????????????????????????????????????????????????????????
void Open_order()                                   // An opening function
{
      if(UseStartEndTime)
   {
      int TimeNowH = Hour();
      int TimeNowM = Minute();
      if((TimeNowH == StartHour && TimeNowM >= StartMin) || TimeNowH > StartHour)  //Are we past the starting point of the day?
      {
         if(EndHour >= StartHour)  //Is the ending time the same day?
         {
            if((TimeNowH == EndHour && TimeNowM >= EndMin) || TimeNowH > EndHour)   //If so, are we still within that timeframe?
            {
               Comment("Trade disabled due to Start/End settings");
               DeleteBuyStop();
               DeleteSellStop();
               return;  //Yes, return back and don't trade.
            }
         }
         else  //If the ending time is in the next day, we are automatically in the restricted time frame.  Don't trade.
         {
            if(TimeNowH == StartHour || TimeNowM <= StartMin)
            {
               Comment("Trade disabled due to Start/End settings");
               DeleteBuyStop();
               DeleteSellStop();
               return;
            }
         }
      }
      else  //New day ... 
      {
         if(EndHour <= StartHour && ((TimeNowH == EndHour && TimeNowM >= EndMin) || TimeNowH > EndHour))
         {
            Comment("Trade disabled due to Start/End settings");
            DeleteBuyStop();
            DeleteSellStop();
            return;
         }
      }
   }
//============================================================================================
   if (b==0 && bl==0)                               // No Buy orders, open bl
   {
      OP=MA-dist;                                   // bl order open rate      
      if(OP>Ask-Level) OP=Ask-Level;                // OP precision according to the tolerance
      OP=NormalizeDouble(OP,Digits);                // Normalizing (MA gives the 5th digit) 
      OrderSend(Symbol(),OP_BUYLIMIT, Lots(),OP,3,OP-SL,OP+TP,"",Magic,0,Blue);// Open
      bl=1;                                         // Now there is a Buy order b1
   }      
//--------------------------------------------------------------------------------------------
   if (s==0 && sl==0)                               // No Sell orders, open sl
   {
      OP=MA+spred+dist;                             // sl order open rate    
      if(OP<Bid+Level) OP=Bid+Level;                // OP precision according to the tolerance
      OP=NormalizeDouble(OP,Digits);                // Normalizing (MA gives the 5th digit) 
      OrderSend(Symbol(),OP_SELLLIMIT,Lots(),OP,3,OP+SL,OP-TP,"",Magic,0,Red);// Open
      sl=1;                                         // Now there is a Sell order sl
   }
///============================================================================================
   return;
}
//????????????????????????????????????????????????????????????????????????????????????????????
double Lots()                                       // Calculation of lots
{
//============================================================================================
   Lot=NormalizeDouble(AccountEquity()*Prots/100/1000,1);// Calculate the amount of lots  
   double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT);   // Minimum lot values
   if (Lot == 0 ) Lot = Min_Lot;                         // For testing on const.min.lots
//============================================================================================
   return(Lot);
}
//????????????????????????????????????????????????????????????????????????????????????????????
void DeleteBuyStop()
{
   for (int i = 0; i < OrdersTotal(); i++)
   {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol()==Symbol() && (OrderMagicNumber() == Magic) && (OrderType()==OP_BUYSTOP))
     {
       OrderDelete(OrderTicket());
     }
       
   }
}
   
void DeleteSellStop()
{
   for (int i = 0; i < OrdersTotal(); i++)
   {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol()==Symbol() && (OrderMagicNumber() == Magic) && (OrderType()==OP_SELLSTOP))
     {
       OrderDelete(OrderTicket());
     }
       
   }
}
To add comments, please log in or register