Download MetaTrader 5

HI/LO of week-opening candle.

To add comments, please log in or register
zharoffs
18
zharoffs  

Hi.

I'm in programming some EA, and I have an issue with identification of index of specific candle.

I need the High and Low values of the first H4 candle of the current week, it means High/Low of Monday 00:00-03:59 candle.

I thought to solve that with    DayOfWeek ===> The shift of Monday will be  DayOfWeek-1  ===> Get the date of Monday ===> Get the H4candle shift by iBarShift using the date of Monday

I just can't solve the problem despite the simplicity of one.

Can somebody help me with that?

Keith Watford
Moderator
10652
Keith Watford  

Use iTime to get the start time of the current weekly candle.

Use iBarShift with that time to find the first H4 candle of the week. As the week starts midnight on Sunday and there is no H4 candle at this time, this will actually return the last H4 candle of Friday. So subtract 1 from the shift value to get the 1st of the week.

whroeder1
15900
whroeder1  
Use iTime of the weekly TF to get the start time of the current weekly candle. you must handle 4066/4073 errors. See Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
zharoffs
18
zharoffs  

Thank you a lot!

zharoffs
18
zharoffs  

The EA works wrong.

I attached "Print"s to check the prices and can't realise why I get wrong prices assumed to "HighPrice" and "LowPrice".

Is there any logical mistake I don't see?

 

zharoffs
18
zharoffs  

I downloaded appropriate history for W1 and H4, also, Editor shows no errors in EA.  

whroeder1
15900
whroeder1  
  1. Copy and paste code into SRC, don't paste an image, it's easier also.
  2. Downloading W1 was futile. Unless you have a W1 running, the history is obsolete at the start of a new week.
  3. H4 the same, unless your chart is the H4, in which case it was already up to date.
  4. You get the time of the start of the week but you ignored my previous post about waiting. #2
  5. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
zharoffs
18
zharoffs  

1.

//------------------------------------------
//     I N P U T S   &   G L O B A L S
//------------------------------------------


extern int    ID             =38787301;
extern double Lots           =0.01;
extern double StopLoss       =15;
extern double TakeProfit     =20;
extern int    TrailingStop   =0;
extern int    Slippage       =3;

int           ThisBar        =0;


//------------------------------------------
//     S T A R T
//------------------------------------------

int start()
{
   datetime BarTime       =iTime(NULL,PERIOD_W1,0);
   int      BarShift      =iBarShift(NULL,PERIOD_H4,BarTime)-1;
   double   HighPrice     =NormalizeDouble(iHigh(NULL,PERIOD_H4,BarShift),Digits); Print(HighPrice);
   double   LowPrice      =NormalizeDouble(iLow(NULL,PERIOD_H4,BarShift),Digits); Print(LowPrice);
  
   double   TheStopLoss   =0;
   double   TheTakeProfit =0;

   double   MyPoint       =Point;
   if(Digits==2 || Digits==3 || Digits==5) MyPoint=Point*10; 

      
   if (Bars != ThisBar)
   {
      ThisBar=Bars;
      
      if (TotalOrdersCount()==0) 
      {
         int result=0;

         
//------------------------------------------
//     O P E N   B U Y
//------------------------------------------         

         if ((iOpen(NULL,PERIOD_M1,1)<HighPrice) &&
             (iClose(NULL,PERIOD_M1,1)>HighPrice))
         {
            result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"38787301 BUY",ID,0,clrBlue);
            if(result>0)
            {
               TheStopLoss=0;
               TheTakeProfit=0;
               if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
               if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
               OrderSelect(result,SELECT_BY_TICKET);
               OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,clrGreen);
            }
            return(0);
         }   


//------------------------------------------
//     O P E N   S E L L
//------------------------------------------
    
         if ((iOpen(NULL,PERIOD_M1,1)>LowPrice) &&
             (iClose(NULL,PERIOD_M1,1)<LowPrice))
         {
            result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"38787301 BUY",ID,0,clrRed);
            if(result>0)
            {
               TheStopLoss=0;
               TheTakeProfit=0;
               if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
               if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
               OrderSelect(result,SELECT_BY_TICKET);
               OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,clrGreen);
            }
            return(0);
         }
      }
      
      for(int cnt=0;cnt<OrdersTotal();cnt++)
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderType()<=OP_SELL &&   
            OrderSymbol()==Symbol() &&
            OrderMagicNumber()==ID)  
         {
            if(OrderType()==OP_BUY)  
            {
            
            
//------------------------------------------
//     C L O S E   B U Y
//------------------------------------------     

               if (iStochastic(NULL,0,4,3,3,MODE_SMA,1,MODE_MAIN,1)<0)
               {
                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
               }
               if(TrailingStop>0)  
               {                 
                  if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                  {
                     if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                     {
                        OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,clrGreen);
                     return(0);
                     }
                  } 
               }
            }
            else 
            {

            
//------------------------------------------
//     C L O S E   S E L L
//------------------------------------------     
            
               if (iStochastic(NULL,0,4,3,3,MODE_SMA,1,MODE_MAIN,1)<0)
               {
                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrYellow);
               }
               if(TrailingStop>0)  
               {                 
                  if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                  {
                     if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                     {
                        OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,clrGreen);
                        return(0);
                     }
                  }
               }
            }
         }
      }
   }
   return(0);
}


//-----------------------------------------------------------------------------------------------------------------------------------

int TotalOrdersCount()
   {
   int result=0;
   for(int i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
      if (OrderMagicNumber()==ID) result++;
   }
   return (result);
   }

Thats kind of template, so I put always false conditions for CLOSE in it


2. & 3. & 4.  I have Duka's historical data of few years for all majors all TFs, so I don't think history is the issue in this case


5. Normalize - Got it!

To add comments, please log in or register