//+------------------------------------------------------------------+
//|                                        OHLC Line with Offset.mq5 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_plots   0

enum enWhichLine {
Open,      // Plot Open Line 
High,      // Plot High Line
Low,       // Plot Low Line
Close,     // Plot Close Line
HighLow    // Plot High or Low Line
};

input enWhichLine        WhichLine                 = HighLow;
input int                WhichBar                  = 1;
input color              LineLineColor             =  Aqua;
input ENUM_LINE_STYLE    LineLineStyle             =  2;
input int                LineLineWidth             =  0;
input int                LineOffsetPoints          = 20;
input bool               SetLineOnForeground       = true;

double price;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//---- indicators
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//----
   ObjectDelete(ChartID(),"OHLC_Line");
   ChartRedraw(0);
//----
  }
//+------------------------------------------------------------------+
//| 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[])
                { 
               ArraySetAsSeries(close,true);
               ArraySetAsSeries(open,true);
               ArraySetAsSeries(high,true);
               ArraySetAsSeries(low,true);
               if(close[WhichBar]>open[WhichBar]) 
                {
                if(WhichLine==Close)
                price=close[WhichBar]+LineOffsetPoints*_Point;
                if(WhichLine==Open)
                price=open[WhichBar]-LineOffsetPoints*_Point;
                if(WhichLine==High||WhichLine==HighLow)
                price=high[WhichBar]+LineOffsetPoints*_Point;
                if(WhichLine==Low)
                price=low[WhichBar]-LineOffsetPoints*_Point;
                }
               else
               if(close[WhichBar]<=open[WhichBar]) 
                {
                if(WhichLine==Close)
                price=close[WhichBar]-LineOffsetPoints*_Point;
                if(WhichLine==Open)
                price=open[WhichBar]+LineOffsetPoints*_Point;
                if(WhichLine==High)
                price=high[WhichBar]+LineOffsetPoints*_Point;
                if(WhichLine==Low||WhichLine==HighLow)
                price=low[WhichBar]-LineOffsetPoints*_Point;
                } 
  if (IsNewBar()) 
  Level("OHLC_Line",0,price,LineLineWidth,LineLineStyle,LineLineColor);                      
                    
   return(prev_calculated);
  }
  
//+------------------------------------------------------------------+
void Level(string name, int window,double pr,int w, int s, color clr)
  {
   ObjectDelete(ChartID(),name);
   ObjectCreate(ChartID(),name,OBJ_HLINE,window,0,0,0);
   ObjectSetDouble(ChartID(),name, OBJPROP_PRICE,0, pr);
   ObjectSetInteger(ChartID(),name, OBJPROP_COLOR, clr);
   ObjectSetInteger(ChartID(),name,OBJPROP_BACK,!SetLineOnForeground);
   ObjectSetInteger(ChartID(),name,OBJPROP_WIDTH,w);
   ObjectSetInteger(ChartID(),name,OBJPROP_STYLE,s);
   ObjectSetInteger(ChartID(),name,OBJPROP_SELECTABLE,false);
   
   ChartRedraw(ChartID());
  } 
//+------------------------------------------------------------------+
bool IsNewBar()
{ 
  static datetime Trend_Candle_prevTime1 = -1;
  
  if(Trend_Candle_prevTime1 != iTime(_Symbol,_Period,6))
  { 
   Trend_Candle_prevTime1 = iTime(_Symbol,_Period,6); 
       
   return(true);  
  } 

  return(false); 
}
//+------------------------------------------------------------------+
  