Draw line from High to High

 

Hi everyone, i'm new to programming and to this nice forum :D

with this code i'm trying to draw a line from the previous day high to the current day high,

i got the values for each but couldn't specify the time correctly 


void OnTick()
{
datetime YesterdayDate=iTime(NULL, PERIOD_D1, 1);  // Yesterday Time
double C1_D1_highestValue=iHigh(NULL, PERIOD_D1, 1);  // Yesterday's high
double C0_D1_highestValue=iHigh(NULL, PERIOD_D1, 0);  // Today's high

// Drawline
ObjectDelete("HighTrendLine");
ObjectCreate(  0,
               "HighTrendLine",
               OBJ_TREND,
               0,
               YesterdayDate, //first point time
               C1_D1_highestValue, //first point value
               Time[0], //second point time
               C0_D1_highestValue //second point value
               );
ObjectSetInteger(0,"HighTrendLine",OBJPROP_COLOR,White);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_STYLE,STYLE_DASHDOT);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_WIDTH,1);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_RAY,true);

// Show Figures on Chart
Comment(
       "Yesterday High Time: "      ,YesterdayDate,"\n",
       "Yesterday High Value: "     ,C1_D1_highestValue,"\n",
       "Today Time: "               ,Time[0],"\n",
       "Today High Value: "         ,C0_D1_highestValue
       );
}
 
But it is working...what do you need?
 
danielcioca #:
But it is working...what do you need?
Ok, it placed on the level of highest value, but for the time its placed on the beginning of the day, where it should be placed on the time of the highest value. I hope its clear now?

If its not I’ll make a screenshot explaining the issue, but im far from PC now.
 
  1. double C0_D1_highestValue=iHigh(NULL, PERIOD_D1, 0);  // Today's high

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

  2. Younis Alsulaimi #: , but for the time its placed on the beginning of the day, where it should be placed on the time of the highest value. 

    Stop using the D1. Find the time of the beginning of the day, and it's bar number. Find the highest bar of today. Now you have the time and value for your endpoint.
              date/time (2017)
              Find bar of the same time one day ago - MQL4 programming forum (2017)

 
Younis Alsulaimi #:
Ok, it placed on the level of highest value, but for the time its placed on the beginning of the day, where it should be placed on the time of the highest value. I hope its clear now?

If its not I’ll make a screenshot explaining the issue, but im far from PC now.
As William said… you code it this way… if you use it on D1 timeframe it will work… otherwise you have to code it differently 
 
If you ask for iTime with PERIOD_D1 I suppose there will only be one time at each day: The start of that day's candle, no?

But you started with a wrong approach, you need to get a smaller timeframe PERIOD_H1 or what you want and then ask for ArrayMaximum of all the previous days H1 highs. Then you get an integer which is the bar number of said maximum and then you use that integer with iTime and Period_H1 to get the time that you want to insert in the Object. Off course the time will only be exact to the hour.

Edit: I realised that is what William said. And you might wanna look into MQLRates, seriously.
 

try this way : 


int periodsecond = PeriodSeconds();
int day          = 86400;

int timecurent = Hour() * 60*60 + Minute()*60 + Seconds();
int candles = (timecurent/periodsecond);
double mod = fmod(timecurent,periodsecond);

 int todaycandles     = ( mod > 0 )? candles  : candles -1;
 int yesterdaycandles =  day / PeriodSeconds();
 int totalcandles     = todaycandles + yesterdaycandles;



 int todaycanlde         = iHighest(_Symbol,_Period,MODE_HIGH,todaycandles,0);
 datetime TodayHighCandleTime = iTime(_Symbol,_Period,todaycanlde);
 
 int yesterdaycandle     = iHighest(_Symbol,_Period,MODE_HIGH,(totalcandles-todaycandles),todaycandles);
 datetime YesterdayHighCndleTime = iTime(_Symbol,_Period,yesterdaycandle);
 
 double todayhigh        = iHigh(_Symbol,_Period,todaycanlde);
 double yesterdayhigh    = iHigh(_Symbol,_Period,yesterdaycandle);

//
//// Drawline
ObjectDelete("HighTrendLine");
ObjectCreate(  0,
               "HighTrendLine",
               OBJ_TREND,
               0,
               YesterdayHighCndleTime, //first point time
               yesterdayhigh, //first point value
               TodayHighCandleTime , //second point time
               todayhigh //second point value
               );
ObjectSetInteger(0,"HighTrendLine",OBJPROP_COLOR,White);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_STYLE,STYLE_DASHDOT);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_WIDTH,1);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_RAY,true);

// Show Figures on Chart
Comment(
       "Yesterday High Time: "      ,YesterdayHighCndleTime,"\n",
       "Yesterday High Value: "     ,yesterdayhigh,"\n",
       "Today Time: "               ,TodayHighCandleTime,"\n",
       "Today High Value: "         ,todayhigh
       );
 
danielcioca #:

try this way : 


I wokeup this morning to go through william response, just to see your code ready, Thank you so much its the start of my code i'll add to it the low point and continue from there, really appreciate it Daniel.


William Roeder #:
  1. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

  2. Stop using the D1. Find the time of the beginning of the day, and it's bar number. Find the highest bar of today. Now you have the time and value for your endpoint.
              date/time (2017)
              Find bar of the same time one day ago - MQL4 programming forum (2017)

Thank you William, i'll defently look into the error handling thing, its something i should learn.


pennyhunter #:
If you ask for iTime with PERIOD_D1 I suppose there will only be one time at each day: The start of that day's candle, no?

But you started with a wrong approach, you need to get a smaller timeframe PERIOD_H1 or what you want and then ask for ArrayMaximum of all the previous days H1 highs. Then you get an integer which is the bar number of said maximum and then you use that integer with iTime and Period_H1 to get the time that you want to insert in the Object. Off course the time will only be exact to the hour.

Edit: I realised that is what William said. And you might wanna look into MQLRates, seriously.

Thanks Penny.

 
danielcioca #: try this way : 
  1. int timecurent = Hour() * 60*60 + Minute()*60 + Seconds();
    
    simplify
    int timecurent = TimeCurrent();

  2. int candles = (timecurent/periodsecond);

    This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

    Use iBarShift.

 
William Roeder #:
  1. simplify - 
  2. This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

    Use iBarShift.

int timecurent = TimeCurrent();

  1. simplify - How? TimeCurrent() gives the time of last tick -- I need it to know how many seconds have past since midnight..

How about this version :

int day          = 86400;

 int today                       = Hour()*60*60 + Minute()*60 + Seconds();
 int todaybars                   = iBarShift(_Symbol,_Period,(TimeCurrent()-today));
 int totalbars                   = iBarShift(_Symbol,_Period,(TimeCurrent()-today)-day);
 int yesterdaybars               = totalbars - todaybars;
 

 int todaycanlde                 = iHighest(_Symbol,_Period,MODE_HIGH,todaybars,0);
 datetime TodayHighCandleTime    = iTime(_Symbol,_Period,todaycanlde);
 
 int yesterdaycandle             = iHighest(_Symbol,_Period,MODE_HIGH,yesterdaybars,todaybars);
 datetime YesterdayHighCndleTime = iTime(_Symbol,_Period,yesterdaycandle);
 
 double todayhigh                = iHigh(_Symbol,_Period,todaycanlde);
 double yesterdayhigh            = iHigh(_Symbol,_Period,yesterdaycandle);

//
//// Drawline
ObjectDelete("HighTrendLine");
ObjectCreate(  0,
               "HighTrendLine",
               OBJ_TREND,
               0,
               YesterdayHighCndleTime, //first point time
               yesterdayhigh, //first point value
               TodayHighCandleTime , //second point time
               todayhigh //second point value
               );
ObjectSetInteger(0,"HighTrendLine",OBJPROP_COLOR,White);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_STYLE,STYLE_DASHDOT);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_WIDTH,1);
ObjectSetInteger(0,"HighTrendLine",OBJPROP_RAY,true);

// Show Figures on Chart
Comment(
       "Yesterday High Time: "      ,YesterdayHighCndleTime,"\n",
       "Yesterday High Value: "     ,yesterdayhigh,"\n",
       "Today Time: "               ,TodayHighCandleTime,"\n",
       "Today High Value: "         ,todayhigh
       );
 
danielcioca #: simplify - How? TimeCurrent() gives the time of last tick -- I need it to know how many seconds have past since midnight..

In that case you simplify remove the date portion from TimeCurrent()

int today = time();
          date/time (2017)
          Find bar of the same time one day ago - MQL4 programming forum (2017)
Reason: