//+------------------------------------------------------------------+
//| Plot Previous OHLC.mq5 |
//| https://www.mql5.com/en/users/phade/ |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, https://www.mql5.com/en/users/phade/"
#property link "https://www.mql5.com/en/users/phade/"
#property version "1.01"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 4
#property indicator_label1 "Previous High"
#property indicator_label2 "Previous Low"
#property indicator_label3 "Previous Open"
#property indicator_label4 "Previous Close"
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_LINE
#property indicator_type3 DRAW_LINE
#property indicator_type4 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_SOLID
#property indicator_style3 STYLE_SOLID
#property indicator_style4 STYLE_SOLID
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
// Define buffers
double HighBuffer[];
double LowBuffer[];
double OpenBuffer[];
double CloseBuffer[];
// global vars
datetime timeToUse;
bool valuesCalculated = false;
ENUM_TIMEFRAMES prevTimeFrame = PERIOD_CURRENT;
// inputs
input ENUM_TIMEFRAMES timeFrameSelector = PERIOD_D1; // Choose period to find the previous highs and lows
input color resistanceLineColor = clrDarkTurquoise; // Color of the previous highs
input color supportLineColor = clrDarkGray; // Color of the previous lows
input color candleOpenColor = clrLightGray; // Color of the previous candle open
input color candleCloseColor = clrPeachPuff; // Color of the previous candle close
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit(){
// Set the indicator label
IndicatorSetString(INDICATOR_SHORTNAME, "PrevDayHighLow");
// Create buffers
SetIndexBuffer(0, HighBuffer);
SetIndexBuffer(1, LowBuffer);
SetIndexBuffer(2, OpenBuffer);
SetIndexBuffer(3, CloseBuffer);
PlotIndexSetInteger(0,PLOT_LINE_COLOR, resistanceLineColor);
PlotIndexSetInteger(1,PLOT_LINE_COLOR, supportLineColor);
PlotIndexSetInteger(2,PLOT_LINE_COLOR, candleOpenColor);
PlotIndexSetInteger(3,PLOT_LINE_COLOR, candleCloseColor);
if (timeFrameSelector != prevTimeFrame) {
prevTimeFrame = timeFrameSelector;
ResetBuffers();
}
timeToUse = iTime(_Symbol, timeFrameSelector, 1);
// Return initialization result
return(INIT_SUCCEEDED);
}
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[]){
// Check if the timeframe has changed
if (ChartPeriod() != prevTimeFrame){
prevTimeFrame = ChartPeriod();
ResetBuffers(); // Reset indicator buffers when the timeframe changes
valuesCalculated = false; // Reset the flag
}
// Calculate and store the previous day's high and low in the buffers
if (!valuesCalculated){
for (int i = 0; i < rates_total; i++){
HighBuffer[i] = prevDayHigh(timeToUse, timeFrameSelector);
LowBuffer[i] = prevDayLow(timeToUse, timeFrameSelector);
OpenBuffer[i] = prevDayOpen(timeToUse, timeFrameSelector);
CloseBuffer[i] = prevDayClose(timeToUse, timeFrameSelector);
}
valuesCalculated = true; // Set the flag to true
}
return rates_total;
}
void ResetBuffers(){
ArrayInitialize(HighBuffer, 0); // Reset the HighBuffer to 0
ArrayInitialize(LowBuffer, 0); // Reset the LowBuffer to 0
ArrayInitialize(OpenBuffer, 0); // Reset the LowBuffer to 0
ArrayInitialize(CloseBuffer, 0); // Reset the LowBuffer to 0
}
double prevDayHigh(const datetime prevDay, const ENUM_TIMEFRAMES timeFrame){
int prevBar = iBarShift(_Symbol, timeFrame, prevDay); // Get the bar index of the previous day
double highPrice = iHigh(_Symbol, timeFrame, prevBar); // Get the high price of the previous day
return highPrice;
}
double prevDayLow(const datetime prevDay, const ENUM_TIMEFRAMES timeFrame){
int prevBar = iBarShift(_Symbol, timeFrame, prevDay); // Get the bar index of the previous day
double lowPrice = iLow(_Symbol, timeFrame, prevBar); // Get the low price of the previous day
return lowPrice;
}
double prevDayOpen(const datetime prevDay, const ENUM_TIMEFRAMES timeFrame){
int prevBar = iBarShift(_Symbol, timeFrame, prevDay); // Get the bar index of the previous day
double openPrice = iOpen(_Symbol, timeFrame, prevBar); // Get the low price of the previous day
return openPrice;
}
double prevDayClose(const datetime prevDay, const ENUM_TIMEFRAMES timeFrame){
int prevBar = iBarShift(_Symbol, timeFrame, prevDay); // Get the bar index of the previous day
double closePrice = iClose(_Symbol, timeFrame, prevBar); // Get the low price of the previous day
return closePrice;
}
Comments