Trend lines

 

Good day. i need some help please. I am trying to get the highest and lowest price of 30 candles starting 2 candles back. if i use 2 at the start point it doesn't give me the value and time of the highest and lowest  bar. if i use 0 at the start point then it work fine but i don't want bar[0] and bar[1] to be part of the search for the highest and lowest price


//+------------------------------------------------------------------+
//|                                                       Plan B.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                     Trend Line Values                            |
//+------------------------------------------------------------------+
MqlRates Week[];
MqlRates Day[];
double Day_High[];
double Day_Low[];
double DayHighest;
double DayLowest;
double DHigh;
double DLow;
int Day_Highest,Day_Lowest;
MqlRates Prev_Week[];
MqlRates Prev_Day[];
string D1HighLine = "Trendline_High_Debug";
string D1LowLine  = "Trendline_Low_Debug";

int OnInit(){

ArraySetAsSeries(Prev_Day,true);
ArraySetAsSeries(Prev_Week,true);
ArraySetAsSeries(Day_High,true);
ArraySetAsSeries(Day_Low,true);
ArraySetAsSeries(Week,true);
ArraySetAsSeries(Day,true);   

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){

   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){

Draw_Function();


   
}//OnTick
//+------------------------------------------------------------------+
void Draw_Function(){

//=========================Day==================================
//-----------------------Point 1--------------------------------
CopyRates(_Symbol,PERIOD_D1,0,2,Prev_Day);
//-----------------------Point 2--------------------------------
CopyRates(_Symbol,PERIOD_D1,0,35,Day);

//=======================Day High===============================
//-----------------------Point 1--------------------------------
DHigh = Prev_Day[1].high;
//-----------------------Point 2--------------------------------
CopyHigh(_Symbol,PERIOD_D1,2,30,Day_High);
Day_Highest = ArrayMaximum(Day_High,0,WHOLE_ARRAY);
DayHighest = Day[Day_Highest].high;

//=======================Day Low================================
//-----------------------Point 1--------------------------------
DLow = Prev_Day[1].low;
//-----------------------Point 2--------------------------------
CopyLow(_Symbol,PERIOD_D1,2,30,Day_Low);
Day_Lowest = ArrayMinimum(Day_Low,0,WHOLE_ARRAY);
DayLowest = Day[Day_Lowest].low;

//=========================Week=================================
//-----------------------Point 1--------------------------------
CopyRates(_Symbol,PERIOD_W1,0,2,Prev_Week);
//-----------------------Point 2--------------------------------
CopyRates(_Symbol,PERIOD_W1,0,35,Week);

//=======================Week High==============================
//-----------------------Point 1--------------------------------
//=======================Week Low===============================
//-----------------------Point 2--------------------------------

//=========================Day Trend line=========================

double DayHighTrend = ObjectGetValueByTime(0,D1HighLine,TimeCurrent(),0);
double DayLowTrend = ObjectGetValueByTime(0,D1LowLine,TimeCurrent(),0);

//-------------------Day Point 1 Time---------------------------
datetime PrevDayTime = iTime(_Symbol,PERIOD_D1,0);
int d1PeriodSeconds = PeriodSeconds(PERIOD_D1);
datetime prevD1Time = PrevDayTime - d1PeriodSeconds;

//-------------------Day Point 2 Time---------------------------
datetime time_day_highest = iTime(_Symbol,PERIOD_D1,Day_Highest);
datetime time_day_lowest = iTime(_Symbol,PERIOD_D1,Day_Lowest);

//-------------------Day High Line------------------------------
ObjectCreate(0, D1HighLine, OBJ_TREND, 0,time_day_highest, DayHighest, prevD1Time, DHigh);
ObjectSetInteger(0, D1HighLine, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, D1HighLine, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, D1HighLine, OBJPROP_RAY_RIGHT, true);

//-------------------Day Low Line------------------------------
ObjectCreate(0, D1LowLine, OBJ_TREND, 0,time_day_lowest, DayLowest, prevD1Time, DLow);
ObjectSetInteger(0, D1LowLine, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, D1LowLine, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, D1LowLine, OBJPROP_RAY_RIGHT, true);



//-------------------------Check--------------------------------
Print("Prev Day: ",Prev_Day[0].high);
Print("Day High: ",DayHighest);
Print("Day Low: ",DayLowest);
Print("DayHighTrend : ",DayHighTrend); 
Print("DayLowTrend : ",DayLowTrend);
Print("time_day_highest :",time_day_highest);
Print("time_day_lowest :",time_day_lowest);
Print(prevD1Time);

}
 
OK I think I have solved the problem. I have moved the part where I look for the highest and lowest bar to the ontick function and have replaced the copyHigh with iHighest and iLowest and use iHigh and iLow 
 
Niel05otto #:
OK I think I have solved the problem. I have moved the part where I look for the highest and lowest bar to the ontick function and have replaced the copyHigh with iHighest and iLowest and use iHigh and iLow 

You need to validate the CopyXXX functions AND all iXXX functions, OR you will get wrong numbers often.