Drawing Lines Question

526

I'm trying to figure out how to draw a horizontal line from the start of a day to the end at the open price, of the day.  Not sure how to represent start of day in the following code.  Any thoughts would be greatly appreciated.  I was thinking of converting time to seconds, but then, I would have to create a loop to establish the start of the day at zero seconds.  Which I think would create conflict, in the loop, since I'm trying to draw a line to the end of the day.

```int start()
{

double  vO=iOpen(NULL,1440,0);            // Current Daily Open

ObjectCreate("Sm_Line", OBJ_TREND, 0, BeginTime, vO, EndTime, vO );  //  TimeCurrent()
ObjectSet("Sm_Line", OBJPROP_COLOR, Blue);
ObjectSet("Sm_Line", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Sm_Line", OBJPROP_WIDTH, 2);
ObjectSet("Sm_Line", OBJPROP_RAY, False);

//----
return(0);
}```
Moderator
10376

Well, you know how to get the open for D1. It's a small step from there to get the time that the D1 opens
2202

Since you are looking at the Current Daily Bar (as you used "0" for the shift in "iOpen()"), then the Begin and End times are as follows:

```datetime
BeginTime = iTime( NULL, PERIOD_D1, 0 ),  // Open Time of the daily bar
EndTime   = TimeCurrent();                // Current Time as current bar has not closed yet```

Alternative "EndTime":

```datetime
BeginTime = iTime( NULL, PERIOD_D1, 0 ),  // Open Time of the daily bar
EndTime   = BeginTime + 86400;            // Extend it by 24 hrs x 60 mins x 60 secs = 86400 seconds```

526

Thanks again, FMIC!  Once again you have pointed me in the right direction.  Here is the code which will put a line at the open price of each day on any chart.  Though it is best on daily and lower.  Will only appear as dots within the candles on timeframes above the daily.

```//+------------------------------------------------------------------+
//|                                                Daily Open.mq4 |
//|                                                               me |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern int Hour_Num = 0;
extern int Minute_Num = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----

//ObjectsDeleteAll();

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{
int t = (-1);
datetime BeginTime, EndTime;
int Counted_bars=IndicatorCounted();                                                         // Number of counted bars
int i=Bars-Counted_bars-1;                                                                   // Index of the first uncounted

while(i>=0)                                                                                    // Loop for uncounted bars             for (i = 0; i <=5; i++)
{
if(TimeHour(Time[i]) == Hour_Num && TimeMinute(Time[i]) == Minute_Num)

{ t=t+1;

Alert("Hour =  ",Hour_Num,"     Minute =  ",Minute_Num,"     i =  ",i,"     t =  ",t);

double  vO=iOpen(NULL, PERIOD_D1,t);                                                          // Daily Open

if(t==0)
{
BeginTime = iTime( NULL, PERIOD_D1, 0 );                                                      // Open Time of the daily bar
EndTime   = TimeCurrent();                                                                    // Current Time as current bar has not closed yet
}

if(i>0)
{
BeginTime = iTime( NULL, PERIOD_D1, t );                                                      // Open Time of the daily bar
EndTime   = BeginTime + 86400;                                                                // Extend it by 24 hrs x 60 mins x 60 secs = 86400 seconds
}

Alert("i =  ",i,"    vO =  ",vO,"   BeginTime =  ",BeginTime,"   EndTime =  ",EndTime);

ObjectCreate("Sm_Line"+t, OBJ_TREND, 0, BeginTime, vO, EndTime, vO );                         // TimeCurrent()
ObjectSet("Sm_Line"+t, OBJPROP_COLOR, Blue);
ObjectSet("Sm_Line"+t, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Sm_Line"+t, OBJPROP_WIDTH, 2);
ObjectSet("Sm_Line"+t, OBJPROP_RAY, False);

}
i--;

}

//----
return(0);
}
//+------------------------------------------------------------------+

```
15321

Your code will have the same problem as cashcube has here: Object is not automatically refreshing (Mimi) - MQL4 forum

Once you create the object

`ObjectCreate("Sm_Line"+t, OBJ_TREND, 0, BeginTime, vO, EndTime, vO );                         // TimeCurrent()`
The end point (TimeCurrent) will not move because the object already exists.
1188

How do you want to figure out when the day ends? There are fridays, days before holidays, there are indices which dont run 24hrs and so on. This way this will never work proper. The ony chance you have is to choose a lower timeframe, such as M5, update the end point of the line with each new candle and as soon the day of the candle changes (use iTime) you know that the old day ends and that the new one begins.
526

Yes.  I discovered, after posting, that when a new day begins, the line drawn, for the open of the day, wasn't.  I'll figure it out, and post.

Thanks, again, for all the help!

526

O.k.  I think that I have it.  Use the following code for the past day(s) open values:

```//+------------------------------------------------------------------+
//|                                                   Daily Open.mq4 |
//|                                                               me |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern int Hour_Num = 0;
extern int Minute_Num = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----

ObjectsDeleteAll();

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{
int t = 0;
datetime BeginTime, EndTime;
int Counted_bars=IndicatorCounted();                                                         // Number of counted bars
int i=Bars-Counted_bars-1;                                                                   // Index of the first uncounted
// Loop for uncounted bars
while(i>=0)
{
if(TimeHour(Time[i]) == Hour_Num && TimeMinute(Time[i]) == Minute_Num)

{ t=t+1;

double  vO=iOpen(NULL, PERIOD_D1,t);                                                          // Daily Open

if(i>0)
{
BeginTime = iTime( NULL, PERIOD_D1, t );                                                      // Open Time of the daily bar
EndTime   = BeginTime + 86400;                                                                // Extend it by 24 hrs x 60 mins x 60 secs = 86400 seconds
}

ObjectCreate("Sm_Line"+t, OBJ_TREND, 0, BeginTime, vO, EndTime, vO );
ObjectSet("Sm_Line"+t, OBJPROP_COLOR, Blue);
ObjectSet("Sm_Line"+t, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Sm_Line"+t, OBJPROP_WIDTH, 2);
ObjectSet("Sm_Line"+t, OBJPROP_RAY, False);

}
i--;

}

//----
return(0);
}
//+------------------------------------------------------------------+

```
`And the following code for the current day's open value.`
```//+------------------------------------------------------------------+
//|                                           Daily Open Current.mq4 |
//|                                                               me |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern int Hour_Num = 0;
extern int Minute_Num = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----

ObjectDelete("Sm_Line");

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{
int t = (-1);
datetime BeginTime, EndTime;
int Counted_bars=IndicatorCounted();                                                           // Number of counted bars
int i=Bars-Counted_bars-1;                                                                     // Index of the first uncounted
// Loop for uncounted bars

while(i>=0)
{
if(TimeHour(Time[i]) == Hour_Num && TimeMinute(Time[i]) == Minute_Num)

{

double  vO=iOpen(NULL, PERIOD_D1,0);                                                           // Daily Open

{
BeginTime = iTime( NULL, PERIOD_D1, 0 );                                                       // Open Time of the daily bar
EndTime   = TimeCurrent();                                                                     // Current Time as current bar has not closed yet
}

ObjectCreate("Sm_Line", OBJ_TREND, 0, BeginTime, vO, EndTime, vO );
ObjectSet("Sm_Line", OBJPROP_COLOR, Blue);
ObjectSet("Sm_Line", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Sm_Line", OBJPROP_WIDTH, 2);
ObjectSet("Sm_Line", OBJPROP_RAY, False);

}
i--;

}

//----
return(0);
}
//+------------------------------------------------------------------
```
Moderator
10376

```                                                                                    // Loop for uncounted bars
while(i>=0)
{
if(TimeHour(Time[i]) == Hour_Num && TimeMinute(Time[i]) == Minute_Num)

{ t=t+1;

double  vO=iOpen(NULL, PERIOD_D1,t);                ```

You are checking times starting from the most distant past, but getting the iOpen value from the opposite direction

ie Time from the oldest bar, price from the most recent.

Moderator
10376

In your second code, why the loop?

You have already been told that a line can only be created once, then it has to be moved

15321

GumRai:

You are checking times starting from the most distant past, but getting the iOpen value from the opposite direction

ie Time from the oldest bar, price from the most recent.

You are confusing OnCalculate's time[] with the predefined Time[] which is a series.