//+------------------------------------------------------------------+
//|                                                3Day_HHLL_Line.mq4|
//|                        Copyright 2023, Your Company Name         |
//|                                       https://www.yourwebsite.com|
//+------------------------------------------------------------------+
#property copyright "Copyright 2022 mql5.com"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

input bool               Lines_Extend_Right            = false;
input ENUM_LINE_STYLE    Lines_Style                   = STYLE_SOLID;
input int                Lines_Width                   = 3;

datetime startTime;
datetime endTime;

int OnInit()
{
                if(_Period<=PERIOD_D1)
                 {
                // Set the start and end times for the 3-day period
                 startTime = iTime(_Symbol,PERIOD_D1,3);
                 endTime = iTime(_Symbol,PERIOD_D1,1)+PERIOD_D1*60-_Period;

                // Draw lines connecting the highest high and lowest low
                 ConnectHHLL();
                 }

    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[])
  {
//--- 
                if(_Period<=PERIOD_D1)
                 {
                // Set the start and end times for the 3-day period
                 startTime = iTime(_Symbol,PERIOD_D1,3);
                 endTime = iTime(_Symbol,PERIOD_D1,1)+PERIOD_D1*60-_Period;

                 int startBar = iBarShift(NULL, PERIOD_CURRENT, startTime, false);
                 int endBar = iBarShift(NULL, PERIOD_CURRENT, endTime, false);
    
                 double highestHigh = High[iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,startBar-endBar,endBar)];
                 double lowestLow = Low[iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW,startBar-endBar,endBar)];
                 if(_Period==PERIOD_D1)
                  {
                  highestHigh = High[iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,3,1)];
                  lowestLow = Low[iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW,3,1)];
                  }
                 
                 ObjectSetDouble(ChartID(),"HHLL_Line1",OBJPROP_PRICE1,highestHigh);
                 ObjectSetDouble(ChartID(),"HHLL_Line2",OBJPROP_PRICE1,lowestLow);
                 ObjectSetDouble(ChartID(),"HHLL_Line1",OBJPROP_PRICE2,highestHigh);
                 ObjectSetDouble(ChartID(),"HHLL_Line2",OBJPROP_PRICE2,lowestLow);
                 ObjectSetInteger(ChartID(),"HHLL_Line1",OBJPROP_TIME1,startTime);
                 ObjectSetInteger(ChartID(),"HHLL_Line1",OBJPROP_TIME2,endTime);
                 ObjectSetInteger(ChartID(),"HHLL_Line2",OBJPROP_TIME1,startTime);
                 ObjectSetInteger(ChartID(),"HHLL_Line2",OBJPROP_TIME2,endTime);
                 }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
{
    // Remove objects when the indicator is removed
    ObjectDelete("HHLL_Line1");
    ObjectDelete("HHLL_Line2");
}

void ConnectHHLL()
{
    // Find the highest high and lowest low within the specified period

    int startBar = iBarShift(NULL, PERIOD_CURRENT, startTime, false);
    int endBar = iBarShift(NULL, PERIOD_CURRENT, endTime, false);
    
    double highestHigh = High[iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,startBar-endBar,endBar)];
    double lowestLow = Low[iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW,startBar-endBar,endBar)];
    
                 if(_Period==PERIOD_D1)
                  {
                  highestHigh = High[iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,3,1)];
                  lowestLow = Low[iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW,3,1)];
                  }


    // Draw lines connecting the highest high and lowest low
    ObjectCreate(0, "HHLL_Line1", OBJ_TREND, 0, Time[startBar], highestHigh, Time[endBar], highestHigh);
    ObjectSetInteger(0, "HHLL_Line1", OBJPROP_COLOR, clrGreen);
    ObjectSetInteger(0, "HHLL_Line1", OBJPROP_STYLE, Lines_Style);
    ObjectSetInteger(0, "HHLL_Line1", OBJPROP_WIDTH, Lines_Width);
    ObjectSetInteger(0, "HHLL_Line1", OBJPROP_RAY, Lines_Extend_Right);

    ObjectCreate(0, "HHLL_Line2", OBJ_TREND, 0, Time[startBar], lowestLow, Time[endBar], lowestLow);
    ObjectSetInteger(0, "HHLL_Line2", OBJPROP_COLOR, clrRed);
    ObjectSetInteger(0, "HHLL_Line2", OBJPROP_STYLE, Lines_Style);
    ObjectSetInteger(0, "HHLL_Line2", OBJPROP_WIDTH, Lines_Width);
    ObjectSetInteger(0, "HHLL_Line2", OBJPROP_RAY, Lines_Extend_Right);
}