# Help needed - line of code not working as intended.

22

Hi,

I have an indicator with various parameters. But one line isn't functioning as intended:

MathAbs(iHigh(NULL, PERIOD_M1, iHighest(NULL, PERIOD_M1, MODE_HIGH, 11, 0)) - iClose(NULL, PERIOD_M1, 1+i)) > MathAbs(iClose(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, iLowest(NULL, PERIOD_M1, MODE_LOW, 3, 0)) * 0.95) //Ensures a minimum of 1:0.95 risk/reward ratio assuming high of past 11 bars as t.p. and low of past 3 bars as s.l. in long position opened at close of candle just painted

Is there a mistake in here I have missed or using wrong function or param somewhere? coding isn't my strong suit....

Here is the block it sits in:

//--- main loop

for(int i = limit-1; i >= 0; i--)

{

if (i >= MathMin(500-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation

//Indicator Buffer 1

if(MathAbs(iOpen(NULL, PERIOD_M1, 1+i) - iClose(NULL, PERIOD_M1, 1+i)) > MathAbs(iOpen(NULL, PERIOD_M1, 2+i) - iClose(NULL, PERIOD_M1, 2+i)) * 1.1 //Candlestick Body > Candlestick Body * fixed value

&& iClose(NULL, PERIOD_M1, 1+i) > iOpen(NULL, PERIOD_M1, 1+i) //Candlestick Close > Candlestick Open

&& iOpen(NULL, PERIOD_M1, 2+i) > iClose(NULL, PERIOD_M1, 2+i) //Candlestick Open > Candlestick Close

&& MathAbs(iOpen(NULL, PERIOD_M1, 1+i) - iClose(NULL, PERIOD_M1, 1+i)) > (iHigh(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, 1+i)) * 0.25 //Candlestick Body > Candlestick Range * fixed value

&& iHigh(NULL, PERIOD_M1, 1+i) > iHigh(NULL, PERIOD_M1, 2+i) //Candlestick High > Candlestick High

&& MathAbs(iHigh(NULL, PERIOD_M1, iHighest(NULL, PERIOD_M1, MODE_HIGH, 11, 0)) - iClose(NULL, PERIOD_M1, 1+i)) > MathAbs(iClose(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, iLowest(NULL, PERIOD_M1, MODE_LOW, 3, 0)) * 0.95) //Ensures a minimum of 1:0.95 risk/reward ratio

{

Buffer1[i] = iLow(NULL, PERIOD_M1, 1+i); //Set indicator value at Candlestick Low

if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "M1 bull eng."); time_alert = Time[0]; } //Instant alert, only once per bar

}

Any help appreciated, thanks

Moderator
8293

`//Candlestick Body > Candlestick Range * fixed value`

When will a candle's body ever be larger than the candle's range?

It is simply not possible.

3922

GumRai: When will a candle's body ever be larger than the candle's range? It is simply not possible.

I think the OP means this (see below), which is possible if the fixed range < 1.0.

`// Candlestick Body > ( Candlestick Range * fixed value )`
22

Are you refering to :

&& MathAbs(iOpen(NULL, PERIOD_M1, 1+i) - iClose(NULL, PERIOD_M1, 1+i)) > (iHigh(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, 1+i)) * 0.25 //Candlestick Body > Candlestick Range * fixed value

Yes the body will never be bigger than range.

That line satisfies the body being larger than a quarter of the range size.  To ensure there is a half decent directional movement in price. Hence the * 0.25.

Everything works fine except the last line relating to:

Highest price or resistance of the last 11 bars - close of candle(1) > close candle(1) - lowest price or support of the past 3 bars.

Moderator
8293

FMIC:

I think the OP means this (see below), which is possible if the fixed range < 1.0.

Of course, I see what you mean.
Moderator
8293

`for(int i = limit-1; i >= 0; i--)`

you are looping through difference values for i

then

```&& MathAbs(iHigh(NULL, PERIOD_M1, iHighest(NULL, PERIOD_M1, MODE_HIGH, 11, 0)) - iClose(NULL, PERIOD_M1, 1+i)) >
MathAbs(iClose(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, iLowest(NULL, PERIOD_M1, MODE_LOW, 3, 0)) * 0.95)
//Ensures a minimum of 1:0.95 risk/reward ratio```

getting iHighest and iLowest values relative to bar 0

157

Maybe there should always be an i involved in your bar-index.

22

So replace those yellow highlighted 0's with "i" to give:

&& MathAbs(iHigh(NULL, PERIOD_M1, iHighest(NULL, PERIOD_M1, MODE_HIGH, 11, i)) - iClose(NULL, PERIOD_M1, 1+i)) > MathAbs(iClose(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, iLowest(NULL, PERIOD_M1, MODE_LOW, 3, i)) * 0.95) //Ensures a minimum of 1:0.95 risk/reward ratio //Custom Code

No improvement unfortunately, still no indicators painting.

17112

1.  Play video Please edit your post. For large amounts of code, attach it.

2. ```for(int i = limit-1; i >= 0; i--){
if (i >= MathMin(500-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   ```
Do your lookbacks correctly.
3. ```if(MathAbs(iOpen(NULL, PERIOD_M1, 1+i) - iClose(NULL, PERIOD_M1, 1+i)) ...
&& iClose(NULL, PERIOD_M1, 1+i) > iOpen(NULL, PERIOD_M1, 1+i) ...
&& iOpen(NULL, PERIOD_M1, 2+i) > iClose(NULL, PERIOD_M1, 2+i) ...```
I is the index your are computing, drop the +1 and change the +2 to +1. Simplify your code.
4. `iClose(NULL, PERIOD_M1, 1+i) >`
Why are you limiting your indicator to only run on the M1? Drop your function calls, use the predefines and simplify your code.
22

thanks
22

```//+------------------------------------------------------------------+
//|                               Indicator: engulfing candle M1.mq4 |
//|                                       Created with EABuilder.com |
//|                                             http://eabuilder.com |
//+------------------------------------------------------------------+
#property copyright "Created with EABuilder.com"
#property version   "1.00"
#property description "eng. range & body SERIES"

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0x00FF00
#property indicator_label1 "M1 bull eng."

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "M1 bear eng."

//--- indicator buffers
double Buffer1[];
double Buffer2[];

double myPoint; //initialized in OnInit

void myAlert(string type, string message)
{
if(type == "print")
Print(message);
else if(type == "error")
{
Print(type+" | engulfing candle M1 @ "+Symbol()+","+Period()+" | "+message);
}
else if(type == "order")
{
}
else if(type == "modify")
{
}
else if(type == "indicator")
{
Print(type+" | engulfing candle M1 @ "+Symbol()+","+Period()+" | "+message);
}
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorBuffers(2);
SetIndexBuffer(0, Buffer1);
SetIndexEmptyValue(0, 0);
SetIndexArrow(0, 241);
SetIndexBuffer(1, Buffer2);
SetIndexEmptyValue(1, 0);
SetIndexArrow(1, 242);
//initialize myPoint
myPoint = Point();
if(Digits() == 5 || Digits() == 3)
{
myPoint *= 10;
}
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| 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[],
{
int limit = rates_total - prev_calculated;
//--- counting from 0 to rates_total
ArraySetAsSeries(Buffer1, true);
ArraySetAsSeries(Buffer2, true);
//--- initial zero
if(prev_calculated < 1)
{
ArrayInitialize(Buffer1, 0);
ArrayInitialize(Buffer2, 0);
}
else
limit++;

//--- main loop
for(int i = limit-1; i >= 0; i--)
{
if (i >= MathMin(500-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation
//Indicator Buffer 1
if(MathAbs(iOpen(NULL, PERIOD_M1, 1+i) - iClose(NULL, PERIOD_M1, 1+i)) > MathAbs(iOpen(NULL, PERIOD_M1, 2+i) - iClose(NULL, PERIOD_M1, 2+i)) * 1.1 //Candlestick Body > Candlestick Body * fixed value
&& iClose(NULL, PERIOD_M1, 1+i) > iOpen(NULL, PERIOD_M1, 1+i) //Candlestick Close > Candlestick Open
&& iOpen(NULL, PERIOD_M1, 2+i) > iClose(NULL, PERIOD_M1, 2+i) //Candlestick Open > Candlestick Close
&& MathAbs(iOpen(NULL, PERIOD_M1, 1+i) - iClose(NULL, PERIOD_M1, 1+i)) > (iHigh(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, 1+i)) * 0.25 //Candlestick Body > Candlestick Range * fixed value
&& iHigh(NULL, PERIOD_M1, 1+i) > iHigh(NULL, PERIOD_M1, 2+i) //Candlestick High > Candlestick High
&& MathAbs(iHigh(NULL, PERIOD_M1, iHighest(NULL, PERIOD_M1, MODE_HIGH, 11, i)) - iClose(NULL, PERIOD_M1, 1+i)) > MathAbs(iClose(NULL, PERIOD_M1, 1+i) -iLow(NULL, PERIOD_M1, iLowest(NULL, PERIOD_M1, MODE_LOW, 3, i))) * 0.95  //Ensures a minimum of 1:0.95 risk/reward ratio //Custom Code
)
{
Buffer1[i] = iLow(NULL, PERIOD_M1, 1+i); //Set indicator value at Candlestick Low
if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "M1 bull eng."); time_alert = Time[0]; } //Instant alert, only once per bar
}
else
{
Buffer1[i] = 0;
}
//Indicator Buffer 2
if(MathAbs(iOpen(NULL, PERIOD_M1, 1+i) - iClose(NULL, PERIOD_M1, 1+i)) > MathAbs(iOpen(NULL, PERIOD_M1, 2+i) - iClose(NULL, PERIOD_M1, 2+i)) * 1.1 //Candlestick Body > Candlestick Body * fixed value
&& iClose(NULL, PERIOD_M1, 1+i) < iOpen(NULL, PERIOD_M1, 1+i) //Candlestick Close < Candlestick Open
&& iOpen(NULL, PERIOD_M1, 2+i) < iClose(NULL, PERIOD_M1, 2+i) //Candlestick Open < Candlestick Close
&& MathAbs(iOpen(NULL, PERIOD_M1, 1+i) - iClose(NULL, PERIOD_M1, 1+i)) > (iHigh(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, 1+i)) * 0.25 //Candlestick Body > Candlestick Range * fixed value
&& iLow(NULL, PERIOD_M1, 1+i) < iLow(NULL, PERIOD_M1, 2+i) //Candlestick Low < Candlestick Low
&& iClose(NULL, PERIOD_M1, 1+i) - iLow(NULL, PERIOD_M1, iLowest(NULL, PERIOD_M1, MODE_LOW, 11)) > MathAbs(iHigh(NULL, PERIOD_M1, iHighest(NULL, PERIOD_M1, MODE_HIGH, 3)) - iClose(NULL, PERIOD_M1, 1+i)* 0.95) //Ensures a minimum of 1:0.95 risk/reward ratio //Custom Code
)
{
Buffer2[i] = iHigh(NULL, PERIOD_M1, 1+i); //Set indicator value at Candlestick High
if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "M1 bear eng."); time_alert = Time[0]; } //Instant alert, only once per bar
}
else
{
Buffer2[i] = 0;
}
}
return(rates_total);
}
//+------------------------------------------------------------------+```