Indicator to get Lowest Low of the last 120 candles not working.

 

Hi, I am trying to write an indicator that puts an arrow below or above the current candle if it's Low/High is lower/higher than the Lowest/Highest value in the previous 120 candle but I can't work out why my code is putting arrows on every candle. Would appreciate some help.

Here is the code.


#property copyright "Copyright"

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 clrGreen

#property indicator_color2 clrRed

#property indicator_width1 1

#property indicator_width2 1

//--- input parameters

double up[];

double down[];

datetime candletime=0;

datetime currenttime=0;





//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,up);

   SetIndexStyle(0,DRAW_ARROW);

   SetIndexArrow(0,244);

   SetIndexLabel(0,"Up Arrow");

   SetIndexBuffer(1,down);

   SetIndexStyle(1,DRAW_ARROW);

   SetIndexArrow(1,244);

   SetIndexLabel(1,"Down Arrow");

//---

   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[],

                const int &spread[])

{



currenttime=Time[0];

int limit = MathMax(rates_total-prev_calculated,2);

for(int i=1;i<limit;i++)

{

//Up Trend Signal



if(Low[i] < iLowest(NULL,60, MODE_LOW,120,1))

   {

   up[i]=Low[i]-0.0005;

   candletime=Time[0];

   }



// Down Trend Signal



if(High[i] > iHighest(NULL,60, MODE_HIGH,120,1))

   {

   down[i]=High[i]+0.0005;

   candletime=Time[0];

   }

}

return(rates_total);

}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
W4v3r1d3r: but I can't work out why my code is putting arrows on every candle.
if(Low[i] < iLowest(NULL,60, MODE_LOW,120,1))
  1. Perhaps you should read the manual. iLowest - Timeseries and Indicators Access - MQL4 Reference
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    What do you think “a price < bar index” means? I think 1.54321 will be less than (say) 10 on almost every bar.


  2. Don't hard code constants. Use the proper supplied constants. Your code fails if you do not run on the PERIOD_H1. mixing apples and oranges.

 

Hi William, Thanks for responding. I read the section on iLowest and can see I missed getting the value of the lowest candle so have changed the code as below, however I still get arrows under all the candles for the Up Trend Signal yet for the Down Trend Signal it only shows arrows where the high of the candle is higher than the highest of the  previous 120 candles. Still can't see why the Down Trend Signal works but the Up Trend doesn't as the code is essentially the same just reversed.

btw, thanks for the observation regarding the chart period, I set the Period to 60 as I will only run this on a 60 minute chart, but I have changed to PERIOD_H1 as recommended.


Any thoughts?

#property copyright "Copyright"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrGreen
#property indicator_color2 clrRed
#property indicator_width1 1
#property indicator_width2 1
//--- input parameters
double up[];
double down[];
datetime candletime=0;
datetime currenttime=0;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,up);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,244);
   SetIndexLabel(0,"Up Arrow");
   SetIndexBuffer(1,down);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,244);
   SetIndexLabel(1,"Down Arrow");
//---
   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[],
                const int &spread[])
{

currenttime=Time[0];
int limit = MathMax(rates_total-prev_calculated,2);
for(int i=1;i<limit;i++)
{

Comment(iHigh(Symbol(),PERIOD_H1,iHighest(NULL,PERIOD_H1,MODE_HIGH,120,i+1))," - ",iHighest(NULL,PERIOD_H1,MODE_HIGH,120,i+1));
//Up Trend Signal

if(Low[i] < iLow(Symbol(),PERIOD_H1,iLowest(NULL,PERIOD_H1,MODE_LOW,120,i+1)));//iLowest(NULL,60, MODE_LOW,120,i+1))
   {
   up[i]=Low[i]-0.0005;
   candletime=Time[0];
   }

// Down Trend Signal

if(High[i] > iHigh(Symbol(),PERIOD_H1,iHighest(NULL,PERIOD_H1,MODE_HIGH,120,i+1)))
   {
   down[i]=High[i]+0.0005;
   candletime=Time[0];
   }
}
return(rates_total);
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Have answered my own question. Found the issue, I had a semi-colon after the if statement which shouldn't have been there, once removed the code works just fine.
 

i am try to get the lowest low of a certain number of previous candles, and i want it to be constant or fix. it should not change along with  further new candle

this how hoe the code is:

 if(!buyTrade && Cur_Ema10 > Cur_Ema140 )

  { 

          OpenManual_BuyOrder(); 

           double lowestlow = iLow(NULL,0,0);

            for(int k = 1; k <= 15; k++)

                {

                  double low = iLow(NULL,0,k);

                  if(low < lowestlow){

                   lowestlow = low;

                    const double lowestPrice = lowestlow;

                }

    }

           

          Alert("lowest Price ",lowestPrice);