//+------------------------------------------------------------------+
//|                     PipAtlas - Previous Day & Week High/Low.mq5  |
//|            MIT License - original PipAtlas code, education only  |
//+------------------------------------------------------------------+
#property copyright "PipAtlas - MIT"
#property link      "https://pipatlas.com"
#property version   "1.00"
#property description "Draws the previous day, week and month high and low as levels. Educational - test on demo."
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

input bool            ShowDay      = true;              // Previous day high/low
input bool            ShowWeek     = true;              // Previous week high/low
input bool            ShowMonth    = false;             // Previous month high/low
input bool            ShowLabels   = true;              // Price labels at the right edge
input color           DayColor     = clrDodgerBlue;     // Day colour
input color           WeekColor    = clrDarkOrange;     // Week colour
input color           MonthColor   = clrMediumOrchid;   // Month colour
input ENUM_LINE_STYLE LineStyle    = STYLE_DASH;        // Line style
input bool            AlertOnCross = false;             // Alert when price crosses a level

// Levels come from the finished previous period (shift 1 on D1/W1/MN1), so a
// level never moves during the current day, week or month. Days follow your
// broker's server clock: a broker with a short Sunday candle has a "previous
// day" on Monday that is only that Sunday session.

string Pref = "PA-PDL-";
double gLastBid = 0;

int OnInit(){ gLastBid = 0; return(INIT_SUCCEEDED); }
void OnDeinit(const int reason){ ObjectsDeleteAll(0, Pref); }

void Level(const string tag, const ENUM_TIMEFRAMES tf, const bool isHigh, const color clr, const string text)
{
   string n = Pref + tag, lb = Pref + tag + "-L";
   double price = isHigh ? iHigh(_Symbol, tf, 1) : iLow(_Symbol, tf, 1);
   datetime t1 = iTime(_Symbol, tf, 0);
   if(price <= 0 || t1 == 0){ ObjectDelete(0, n); ObjectDelete(0, lb); return; }  // history not loaded yet
   datetime t2 = iTime(_Symbol, _Period, 0) + PeriodSeconds() * 8;
   if(ObjectFind(0, n) < 0) ObjectCreate(0, n, OBJ_TREND, 0, t1, price, t2, price);
   ObjectMove(0, n, 0, t1, price);
   ObjectMove(0, n, 1, t2, price);
   ObjectSetInteger(0, n, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, n, OBJPROP_STYLE, LineStyle);
   ObjectSetInteger(0, n, OBJPROP_WIDTH, 1);
   ObjectSetInteger(0, n, OBJPROP_RAY_RIGHT, false);
   ObjectSetInteger(0, n, OBJPROP_BACK, true);
   ObjectSetInteger(0, n, OBJPROP_SELECTABLE, false);
   if(ShowLabels)
   {
      if(ObjectFind(0, lb) < 0) ObjectCreate(0, lb, OBJ_TEXT, 0, t2, price);
      ObjectMove(0, lb, 0, t2, price);
      ObjectSetString(0, lb, OBJPROP_TEXT, " " + text + " " + DoubleToString(price, _Digits));
      ObjectSetInteger(0, lb, OBJPROP_COLOR, clr);
      ObjectSetInteger(0, lb, OBJPROP_FONTSIZE, 8);
      ObjectSetInteger(0, lb, OBJPROP_ANCHOR, ANCHOR_LEFT);
      ObjectSetInteger(0, lb, OBJPROP_SELECTABLE, false);
   }
   else ObjectDelete(0, lb);

   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   if(AlertOnCross && gLastBid > 0)
   {
      if((gLastBid < price && bid >= price) || (gLastBid > price && bid <= price))
         Alert("PipAtlas: ", _Symbol, " crossed the ", text, " (", DoubleToString(price, _Digits), ") - an observation, not a signal");
   }
}

void Hide(const string tag){ ObjectDelete(0, Pref + tag); ObjectDelete(0, Pref + tag + "-L"); }

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(rates_total < 2) return(0);
   // a level is only useful on a chart below its own timeframe
   if(ShowDay   && _Period < PERIOD_D1){ Level("PDH", PERIOD_D1, true,  DayColor,   "PDH"); Level("PDL", PERIOD_D1, false, DayColor,   "PDL"); } else { Hide("PDH"); Hide("PDL"); }
   if(ShowWeek  && _Period < PERIOD_W1){ Level("PWH", PERIOD_W1, true,  WeekColor,  "PWH"); Level("PWL", PERIOD_W1, false, WeekColor,  "PWL"); } else { Hide("PWH"); Hide("PWL"); }
   if(ShowMonth && _Period < PERIOD_MN1){ Level("PMH", PERIOD_MN1, true, MonthColor, "PMH"); Level("PML", PERIOD_MN1, false, MonthColor, "PML"); } else { Hide("PMH"); Hide("PML"); }
   gLastBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   return(rates_total);
}
//+------------------------------------------------------------------+
