Download MetaTrader 5
To add comments, please log in or register
Let your idle PCs do something useful. Join MQL5 Cloud Network!
brettosm8
241
brettosm8 2016.02.20 04:41 

Hi there,

I've had a poke around and not found what I'm looking for.

I'm trying to create an indicator that draws a line along the prevailing HOD level during the trading session.

In the code below I've limited the indicator to work only since the most recent trading day of posting for testing.

I thought the simplest way would be to draw the highest price since the index of the bar at the start of the trading session. 

I'm getting results I don't understand.

#property copyright "Brett"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

//---- buffers
int i;

double   TimeFrame=PERIOD_M5;
int      HOD_Index;
double   HOD;
double   LOD;
double   Hv[];
double   Lv[];

bool     OpenBarSet;
int      OpenBar;
string   text;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

//--- indicator buffers mapping
   SetIndexStyle(0,DRAW_ARROW,STYLE_DOT,1,Red);
   SetIndexDrawBegin(0,i);
   SetIndexBuffer(0,Hv);
   SetIndexLabel(0,"Resistance");

   SetIndexStyle(1,DRAW_ARROW,STYLE_DOT,1,Blue);
   SetIndexDrawBegin(1,i);
   SetIndexBuffer(1,Lv);
   SetIndexLabel(1,"Support");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   i=Bars;
   
   while(i>=0)
     {
      //Trading starts at 15.30 server time. This system ignores the first hour of trading.
      if(TimeMonth(Time[i])==2 && TimeDay(Time[i]) >=19 && TimeHour(Time[i])>=16 && (TimeHour(Time[i])<=21 && TimeMinute(Time[i])<=45))
        {
         if(OpenBarSet==false)
           {
            text+="\nTrading open: "+TimeDay(Time[i])+"/"+TimeMonth(Time[i])+"/"+TimeYear(Time[i]);
            OpenBar=i;
            text+="\nOpenBar: "+OpenBar;
            OpenBarSet=true;
           }
         
         text+="\ni: "+i;
         text+="\nBars since open bar: "+(OpenBar-i);
         HOD_Index=iHighest(Symbol(),TimeFrame,MODE_HIGH,OpenBar-i,1);
         text+="\nHOD_Index: "+HOD_Index;
         HOD=High[HOD_Index];
         Hv[i]=HOD;
        }
      else
        {
         //text+="\nTrading closed";
         OpenBarSet=false;
        }

      Comment(text);

      i--;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

 The result this displays is:

 

 From the comments printed it appears the OpenBarSet keeps getting set to false somehow.

Why is the HOD_Index always 1?

It looks like the indicator is drawing something between the right times, but there are spaces because OpenBarSet gets set to false.

The drawing should appear at the horizontal line, and should increase with the higher highs of the day, but instead appears way above it.

Can you please assist/advice re the above issues?

Kind regards and thanks. 

whroeder1
13596
whroeder1 2016.02.20 12:56  
 if(TimeMonth(Time[i])==2 && TimeDay(Time[i]) >=19 && TimeHour(Time[i])>=16 && (TimeHour(Time[i])<=21 && TimeMinute(Time[i])<=45))

This will be false before Feb 19 16:00, 16:46-16:59, 17:46-17:59, 18:46-18:59, 18:46-18:59, 19:46-19:59, 20:46-20:59, and after 20:45

brettosm8
241
brettosm8 2016.02.21 06:22  

Thanks for that.

This helped to work out how to get the line to be drawn at the right level too.

I now have the below code:

i=Bars;      

while(i>=0)
        {
        //Trading starts at 15.30 server time. This system ignores the first hour of trading.      
        if(TimeMonth(Time[i])==2 && TimeDay(Time[i]) >=19 && TimeHour(Time[i])>=16)        
                {
                if(TradingOpen==false)           
                        {            
                        text+="Trading open: "+TimeDay(Time[i])+"/"+TimeMonth(Time[i])+"/"+TimeYear(Time[i]);
                        OpenBar=i;            
                        text+="\nOpenBar: "+OpenBar;            
                        TradingOpen=true;           
                        }                  

                text+="\ni: "+i;         
                text+="\nBars since open bar: "+(OpenBar-i);         
                HOD_Index=iHighest(Symbol(),TimeFrame,MODE_HIGH,OpenBar-i,i+1);         
                text+="\nHOD_Index: "+HOD_Index;         
                HOD=High[HOD_Index];         
                text+="\nHOD: "+HOD;         
                Hv[i+1]=HOD;        
                }      
                else if(TimeMonth(Time[i])==2 && 
                        TimeDay(Time[i]) >=19 && 
                        TimeHour(Time[i]>=21) && 
                        TimeMinute(Time[i])>=45)        
                        {         
                        //text+="\nTrading closed";         
                        TradingOpen=false;        
                        }      Comment(text);      
        i--;     
        }

  which looks like:

 

 That's what I want but when I apply it with the below code, which is the same except it starts a day earlier:

 

i=Bars;      
while(i>=0)
     {
      //Trading starts at 15.30 server time. This system ignores the first hour of trading.
      if(TimeMonth(Time[i])==2 && TimeDay(Time[i]) >=18 && TimeHour(Time[i])>=16)
        {
         if(TradingOpen==false)
           {
            text+="Trading open: "+TimeDay(Time[i])+"/"+TimeMonth(Time[i])+"/"+TimeYear(Time[i]);
            OpenBar=i;
            text+="\nOpenBar: "+OpenBar;
            TradingOpen=true;
           }
                  text+="\ni: "+i;
         text+="\nBars since open bar: "+(OpenBar-i);
         HOD_Index=iHighest(Symbol(),TimeFrame,MODE_HIGH,OpenBar-i,i+1);
         text+="\nHOD_Index: "+HOD_Index;
         HOD=High[HOD_Index];
         text+="\nHOD: "+HOD;
         Hv[i+1]=HOD;
        }
      else if(TimeMonth(Time[i])==2 && 
                TimeDay(Time[i]) >=18 && 
                TimeHour(Time[i]>=21) && 
                TimeMinute(Time[i])>=45)
        {         //text+="\nTrading closed";
         TradingOpen=false;
        }
      Comment(text);
      i--;
     }

 this happens:

 

The line for the first trading day is correct.

The line for the second day should be same as the first screen shot, but picks up on the same level as the previous trading day. 

Since OpenBar is assigned a new value every trading day, I think this should work.

Regards and thanks. 

brettosm8
241
brettosm8 2016.02.22 04:26  

TradingOpen doesn't get set to false.

brettosm8
241
brettosm8 2016.02.22 09:57  

There is a last bug I'm having trouble finding, or don't know how to find.

The indicator draws these long vertical lines at the start of each session.

Looks like some how the first value that prints out for the session is huge.

It looks like this.

 
The code I have for this is:

i=Bars;

   while(i>=0)
     {

      //Trading starts at 15.30 server time. This system ignores the first hour of trading.
      //else if(TimeMonth(Time[i])==2 && TimeDay(Time[i]) >=18 && TimeHour(Time[i])>=16)
      if(TimeToStr(Time[i],TIME_MINUTES)>="16:00" && 
         TimeToStr(Time[i],TIME_MINUTES<="21:45"))
        {
         if(TradingOpen==false)
           {
            OpenBar=i;
            TradingOpen=true;
           }

         //text+="\ni: "+i;
         //text+="\nBars since open bar: "+(OpenBar-i);
         HOD_Index=iHighest(Symbol(),TimeFrame,MODE_HIGH,OpenBar-i,i+1);
         LOD_Index=iLowest(Symbol(),TimeFrame,MODE_LOW,OpenBar-i,i+1);
         //text+="\nHOD_Index: "+HOD_Index;
         HOD=High[HOD_Index];
         LOD=Low[LOD_Index];
         text+="\nHOD: "+HOD;
         Hv[i+1]=HOD;
         Lv[i+1]=LOD;
        }
      else
        {
         //text+="\nTrading closed";
         TradingOpen=false;
        }

      Comment(text);

      i--;
     }

 

Could you please assist/advise?

whroeder1
13596
whroeder1 2016.02.22 11:53  
  1. You are mixing apples and oranges
    HOD_Index=iHighest(Symbol(),TimeFrame,MODE_HIGH,OpenBar-i,i+1);
    LOD_Index=iLowest(Symbol(),TimeFrame,MODE_LOW,OpenBar-i,i+1);
    
    HOD=High[HOD_Index];
    LOD=Low[LOD_Index];

  2. What happens when OpenBar == i?
brettosm8
241
brettosm8 2016.02.22 22:49  
Thanks, I think I see what you're getting at.
/
To add comments, please log in or register