//+------------------------------------------------------------------+
//|                                                 Previous Open.mq4|
//| Shows a horizontal line at the open of the previous candle.      |
//| The line is green if the previous candle was up, and red if down.|
//+------------------------------------------------------------------+
#property copyright   "Copyright 2026"
#property description "Plots the open price of the previous candle using a horizontal line."
#property version     "2.00"
#property strict

#property indicator_chart_window

//--- input parameters
input ENUM_TIMEFRAMES   InpTimeFrame  = PERIOD_CURRENT;        // Timeframe
input color             InpColorUp    = clrGreenYellow;        // Color for Up Candle
input color             InpColorDown  = clrOrangeRed;          // Color for Down Candle
input int               InpWidth      = 1;                     // Line Width (1-5)
input ENUM_LINE_STYLE   InpStyle      = STYLE_DASHDOTDOT;      // Line Style

string LineName = "OpenPreviousBar";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   CreateOrUpdateLine();
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   ObjectDelete(0, LineName);
}

//+------------------------------------------------------------------+
//| 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[])
{
   CreateOrUpdateLine();
   return(rates_total);
}

//+------------------------------------------------------------------+
//| Create or update the horizontal line                             |
//+------------------------------------------------------------------+
void CreateOrUpdateLine()
{
   // Check if we have at least 2 bars to calculate previous candle status
   if(iBars(Symbol(), InpTimeFrame) < 2)
      return;

   double prevOpen  = iOpen(Symbol(), InpTimeFrame, 1);
   double prevClose = iClose(Symbol(), InpTimeFrame, 1);
   
   // Determine color based on whether previous candle was Up or Down
   color lineColor = (prevClose >= prevOpen) ? InpColorUp : InpColorDown;

   if(ObjectFind(0, LineName) < 0)
   {
      ObjectCreate(0, LineName, OBJ_HLINE, 0, 0, prevOpen);
      ObjectSetInteger(0, LineName, OBJPROP_COLOR, lineColor);
      ObjectSetInteger(0, LineName, OBJPROP_WIDTH, InpWidth);
      ObjectSetInteger(0, LineName, OBJPROP_STYLE, InpStyle);
      ObjectSetInteger(0, LineName, OBJPROP_BACK, false); // Draw on foreground
      ObjectSetInteger(0, LineName, OBJPROP_SELECTABLE, false);
      ObjectSetInteger(0, LineName, OBJPROP_SELECTED, false);
   }
   else
   {
      ObjectSetDouble(0, LineName, OBJPROP_PRICE, prevOpen);
      ObjectSetInteger(0, LineName, OBJPROP_COLOR, lineColor);
      ObjectSetInteger(0, LineName, OBJPROP_WIDTH, InpWidth);
      ObjectSetInteger(0, LineName, OBJPROP_STYLE, InpStyle);
   }
}