This indicator works perfectly when attached to a chart.
However, I got a
"Zero Divide"
error when using it in Expert Advisor.
How do I work around this line to get it to work in EA?
_PipsRatio = (_CurRangeHigh - _CurRangeLow) / (_SubRangeHigh - _SubRangeLow)
Thank you!
However, I got a
"Zero Divide"
error when using it in Expert Advisor.
How do I work around this line to get it to work in EA?
_PipsRatio = (_CurRangeHigh - _CurRangeLow) / (_SubRangeHigh - _SubRangeLow)
Thank you!
Inserted Code
//+------------------------------------------------------------------+
//| OverlayChart.mq5 |
//| Copyright 2014, EarnForex.com |
//| http://www.earnforex.com |
//| Converted from MT4 version by http://www.irxfx.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, EarnForex.com"
#property link "http://www.earnforex.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 1
#property indicator_type1 DRAW_COLOR_BARS
#property indicator_color1 clrMediumSeaGreen, clrOrange
#property indicator_width1 1
//Indicator Parameters
input string SubSymbol = "CHFJPY";
input bool Mirroring = false;
input ENUM_DRAW_TYPE DrawType = DRAW_COLOR_BARS;
extern color GridColor = Black;
//Indicator Buffers
double O[];
double H[];
double L[];
double C[];
double Color[];
// Global variables
double SubOpen[];
double SubHigh[];
double SubLow[];
double SubClose[];
string Prefix; //Indicator Prefix
int Grid = 10; //Grid Lines
int SnapPips = 10; //Snap Pips For Grid Lines
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorSetString(INDICATOR_SHORTNAME, "OverLay Chart (" + SubSymbol + ")");
SetIndexBuffer(0, O, INDICATOR_DATA);
SetIndexBuffer(1, H, INDICATOR_DATA);
SetIndexBuffer(2, L, INDICATOR_DATA);
SetIndexBuffer(3, C, INDICATOR_DATA);
SetIndexBuffer(4, Color, INDICATOR_COLOR_INDEX);
ArraySetAsSeries(O, true);
ArraySetAsSeries(H, true);
ArraySetAsSeries(L, true);
ArraySetAsSeries(C, true);
ArraySetAsSeries(Color, true);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DrawType);
ArraySetAsSeries(SubOpen, true);
ArraySetAsSeries(SubHigh, true);
ArraySetAsSeries(SubLow, true);
ArraySetAsSeries(SubClose, true);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
for (int _i = 1; _i <= Grid; _i ++)
{
ObjectDelete(0, Prefix + "Grid" + _i );
ObjectDelete(0, Prefix + "Price" + _i );
}
}
//+------------------------------------------------------------------+
//| 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[])
{
int _BarsCount;
double _CurRangeHigh, _CurRangeLow, _CurRangeCenter;
double _SubRangeHigh, _SubRangeLow, _SubRangeCenter;
double _SubPoint;
int _SubDigit;
double _SubOpen, _SubHigh, _SubLow, _SubClose;
double _PipsRatio;
double _GridPips, _GridPrice;
int _i;
ArraySetAsSeries(Open, true);
ArraySetAsSeries(High, true);
ArraySetAsSeries(Low, true);
ArraySetAsSeries(Close, true);
ArraySetAsSeries(Time, true);
ArrayInitialize(O, 0);
ArrayInitialize(H, 0);
ArrayInitialize(L, 0);
ArrayInitialize(C, 0);
//Calculate Visible Bars
_BarsCount = ChartGetInteger(0, CHART_VISIBLE_BARS) + 1;
int _FirstBar = ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR);
int _LastBar = _FirstBar - _BarsCount + 1;
if ( _LastBar < 0 ) {
_LastBar = 0;
_BarsCount = _FirstBar + 1;
}
//Calculate Chart Ratio
_CurRangeHigh = High[ArrayMaximum(High, _LastBar, _BarsCount)];
_CurRangeLow = Low[ArrayMinimum(Low, _LastBar, _BarsCount)];
_CurRangeCenter = (_CurRangeHigh + _CurRangeLow) / 2;
CopyOpen(SubSymbol, 0, _LastBar, _BarsCount, SubOpen);
CopyHigh(SubSymbol, 0, _LastBar, _BarsCount, SubHigh);
CopyLow(SubSymbol, 0, _LastBar, _BarsCount, SubLow);
CopyClose(SubSymbol, 0, _LastBar, _BarsCount, SubClose);
double SubMax = SubHigh[ArrayMaximum(SubHigh)];
double SubMin = SubLow[ArrayMinimum(SubLow)];
if (Mirroring)
{
_SubRangeHigh = SubMin;
_SubRangeLow = SubMax;
}
else
{
_SubRangeHigh = SubMax;
_SubRangeLow = SubMin;
}
_SubRangeCenter = (_SubRangeHigh + _SubRangeLow) / 2;
_SubPoint = SymbolInfoDouble(SubSymbol, SYMBOL_POINT);
_SubDigit = SymbolInfoInteger(SubSymbol, SYMBOL_DIGITS);
_PipsRatio = (_CurRangeHigh - _CurRangeLow) / (_SubRangeHigh - _SubRangeLow);
_GridPips = (_SubRangeHigh - _SubRangeLow) / Grid;
_GridPips = MathRound((_SubRangeHigh - _SubRangeLow) / Grid / (_SubPoint * SnapPips)) * (_SubPoint * SnapPips);
//Draw Candlesticks
for (_i = _LastBar; _i < _LastBar + _BarsCount; _i++)
{
int i = _i - _LastBar;
_SubOpen = SubOpen[i] - _SubRangeCenter;
_SubHigh = SubHigh[i] - _SubRangeCenter;
_SubLow = SubLow[i] - _SubRangeCenter;
_SubClose = SubClose[i] - _SubRangeCenter;
if (Mirroring)
{
if (_SubOpen < _SubClose)
{
H[_i] = _CurRangeCenter + _SubHigh * _PipsRatio;
L[_i] = _CurRangeCenter + _SubLow * _PipsRatio;
Color[_i] = 0;
}
else
{
L[_i] = _CurRangeCenter + _SubLow * _PipsRatio;
H[_i] = _CurRangeCenter + _SubHigh * _PipsRatio;
Color[_i] = 1;
}
C[_i] = _CurRangeCenter + _SubClose * _PipsRatio;
O[_i] = _CurRangeCenter + _SubOpen * _PipsRatio;
}
else
{
if (_SubOpen < _SubClose)
{
H[_i] = _CurRangeCenter + _SubHigh * _PipsRatio;
L[_i] = _CurRangeCenter + _SubLow * _PipsRatio;
Color[_i] = 0;
}
else
{
L[_i] = _CurRangeCenter + _SubLow * _PipsRatio;
H[_i] = _CurRangeCenter + _SubHigh * _PipsRatio;
Color[_i] = 1;
}
C[_i] = _CurRangeCenter + _SubClose * _PipsRatio;
O[_i] = _CurRangeCenter + _SubOpen * _PipsRatio;
}
}
for (_i = 1; _i <= Grid; _i ++)
{
_GridPrice = MathRound(_SubRangeCenter / (_SubPoint * SnapPips)) * (_SubPoint * SnapPips);
_GridPrice = ((_GridPrice + _GridPips / 2) + _GridPips * (Grid / 2 - 1)) - (_GridPips * (_i - 1));
ObjectCreate(0, Prefix + "Grid" + _i, OBJ_TREND, 0, 0, 0);
ObjectSetInteger(0, Prefix + "Grid" + _i, OBJPROP_TIME, 0, Time[_FirstBar]);
ObjectSetDouble(0, Prefix + "Grid" + _i, OBJPROP_PRICE, 0, _CurRangeCenter + (_GridPrice - _SubRangeCenter) * _PipsRatio);
ObjectSetInteger(0, Prefix + "Grid" + _i, OBJPROP_TIME, 1, Time[_LastBar]);
ObjectSetDouble(0, Prefix + "Grid" + _i, OBJPROP_PRICE, 1, _CurRangeCenter + (_GridPrice - _SubRangeCenter) * _PipsRatio);
ObjectSetInteger(0, Prefix + "Grid" + _i, OBJPROP_COLOR, GridColor);
ObjectSetInteger(0, Prefix + "Grid" + _i, OBJPROP_STYLE, STYLE_DOT);
ObjectSetInteger(0, Prefix + "Grid" + _i, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, Prefix + "Grid" + _i, OBJPROP_RAY_RIGHT, true);
ObjectCreate(0, Prefix + "Price" + _i, OBJ_TEXT, 0, 0, 0);
ObjectSetInteger(0, Prefix + "Price" + _i, OBJPROP_TIME, 0, Time[_FirstBar - _BarsCount / 10]);
ObjectSetDouble(0, Prefix + "Price" + _i, OBJPROP_PRICE, 0, _CurRangeCenter + (_GridPrice - _SubRangeCenter) * _PipsRatio);
ObjectSetInteger(0, Prefix + "Price" + _i, OBJPROP_COLOR, GridColor);
ObjectSetString(0, Prefix + "Price" + _i, OBJPROP_TEXT, DoubleToString(_GridPrice, _SubDigit));
}
return(rates_total);
}
//+------------------------------------------------------------------+