Disliked{quote} I'm confused as to what is the purpose of using a GlobalVariable here to capture the button state. Is it so that changing the button on any chart toggles all the other charts with the same indicator? That's the only reason I can think of, not that I would see the need. A button by itself (if NOT a "CButton" object, but just a regular OBJ_BUTTON made with ObjectCreate) will remember it's pressed/unpressed state wheneven TF's or changed, or even restarting MT4. It's a regular object property. (A CButton, however, is stupid, and loses the pressed/unpressed...Ignored
Good Day may someone please help me here, I converted this code mql4 to mql5 from another trend here and modified it. its working perfectly on currencies however it doesnot work well on btcusd, us30 and oil, may you please help.
//+------------------------------------------------------------------+
//| Grid.mq5 |
//| 4xcoder |
//| [email protected] |
//+------------------------------------------------------------------+
#property copyright "4xcoder"
#property link "[email protected]"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//---- input parameters
input int HGrid_Weeks = 999999; // Period over which to calc High/Low of grid
input int HGrid_Pips = 100; // Size of grid in Pips
input int GridTime = 15; // Number of periods (days or weeks) to draw time grid
input int TimeGrid = PERIOD_H4; // Grid period in minutes
input int ColorHour = 16; // For hour grids, draw color line at this hour (broker time)
bool firstTime = true;
datetime lastTime = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
firstTime = true;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
for (int i = ObjectsTotal(0) - 1; i >= 0; i--)
{
string name = ObjectName(0, i);
if (StringFind(name, "grid_") >= 0)
ObjectDelete(0, name);
}
}
//+------------------------------------------------------------------+
//| 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 (firstTime || TimeCurrent() - lastTime > 5)
{
firstTime = false;
lastTime = TimeCurrent();
if (HGrid_Weeks > 0 && HGrid_Pips > 0)
{
double weekH = iHigh(NULL, PERIOD_W1, 0);
double weekL = iLow(NULL, PERIOD_W1, 0);
for (int i = 1; i < HGrid_Weeks; i++)
{
weekH = MathMax(weekH, iHigh(NULL, PERIOD_W1, i));
weekL = MathMin(weekL, iLow(NULL, PERIOD_W1, i));
}
double pipRange = HGrid_Pips * _Point;
if (Symbol() == "GOLD")
pipRange = pipRange * 10.0;
double topPips = (weekH + pipRange) - MathMod(weekH, pipRange);
double botPips = weekL - MathMod(weekL, pipRange);
for (double p = botPips; p <= topPips; p += pipRange)
{
string gridname = "grid_" + DoubleToString(p, _Digits);
ObjectCreate(0, gridname, OBJ_HLINE, 0, 0, p);
// Dynamic Color Logic (Corrected)
color gridColor = (p < close[rates_total - 1]) ? clrBlue : clrRed;
ObjectSetInteger(0, gridname, OBJPROP_COLOR, gridColor);
ObjectSetInteger(0, gridname, OBJPROP_WIDTH, 10);
ObjectSetInteger(0, gridname, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetDouble(0, gridname, OBJPROP_PRICE, 0, p);
ObjectSetInteger(0, gridname, OBJPROP_BACK, true);
}
}
}
datetime start;
if (TimeGrid == PERIOD_W1)
{
int weekCount = GridTime - 1;
int bar = 0;
while (weekCount >= 0 && bar < rates_total)
{
MqlDateTime tm;
TimeToStruct(time[bar], tm);
if (tm.day_of_week == 1 && tm.hour == 0) // Monday and 00:00 hour
{
start = time[bar];
string gridname = "grid_" + DoubleToString(start, 0);
ObjectCreate(0, gridname, OBJ_VLINE, 0, start, 0);
// Dynamic Color Logic for Vertical Lines (Corrected)
color timeGridColor = (close[rates_total - 1] < iClose(NULL, PERIOD_D1, 0)) ? clrBlue : clrRed;
ObjectSetInteger(0, gridname, OBJPROP_COLOR, timeGridColor);
ObjectSetInteger(0, gridname, OBJPROP_WIDTH, 10);
ObjectSetInteger(0, gridname, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, gridname, OBJPROP_TIME, 0, start);
ObjectSetInteger(0, gridname, OBJPROP_BACK, true);
weekCount--;
}
bar++;
}
}
if (TimeGrid > 0)
{
start = time[0];
int skip = TimeGrid * 60;
start = start - (start % skip) + skip;
MqlDateTime tmStart;
TimeToStruct(start, tmStart);
int thisDay = tmStart.day;
int dayCount = GridTime - 1;
while (dayCount >= 0)
{
string gridname = "grid_" + DoubleToString(start, 0);
ObjectCreate(0, gridname, OBJ_VLINE, 0, start, 0);
// Dynamic Color Logic for Vertical Lines (Corrected)
color timeGridColor = (close[rates_total - 1] < iClose(NULL, PERIOD_D1, 0)) ? clrBlue : clrRed;
ObjectSetInteger(0, gridname, OBJPROP_COLOR, timeGridColor);
ObjectSetInteger(0, gridname, OBJPROP_WIDTH, 10);
ObjectSetInteger(0, gridname, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, gridname, OBJPROP_TIME, 0, start);
ObjectSetInteger(0, gridname, OBJPROP_BACK, true);
start = start - skip;
TimeToStruct(start, tmStart);
if (tmStart.day != thisDay)
{
dayCount--;
thisDay = tmStart.day;
}
}
}
return(rates_total);
}
//+------------------------------------------------------------------+