Expert Advisor help... counting candles to test for price range (span) as qualifying condtion

 
Friends:

If I understand correctly the following code snippet counts back in time to get price level of candles counting current candle first, then previous candle, then the candle 3 periods back, etc.:

double CandleMax=0.0000;
double CandleMin=10.0000;
double CandleSpan=0.0000;
int limit=14;
for(i=0; i<=limit,i++)
{
if (Low[i]<CandleMin)
CandleMin = Low[i]
if (High[i]>CandleMax)
CandleMax = Close[i];
CandleSpan = CandleMax - CandleMin
}

Now my question is this: Once I have established a span equal to or exceeding x pips, how do I test candles forward in time for an indicator event such as price closing below a moving average?

Thank you in advance for your help and patience with my lack of knowledge.

nipperdj
 
Hello nipperdj,

first of all, are you sure, that CandleMax should be the Close[x] value and not the High[x] value?

For your question, please can you try to better explain what you mean? Because I don't understand it. thank you.

cori
 
Yes I meant 'High[i]' I have since learned that I can use the Highest() function to accomplish what I want.

I didn't really define the question well. I am trying to get a feel for the use of MQL4 and I wanted to clarify that High[0] is the high of the current candle, High[1] is the high of the previous candle, High[2] is the high of the second candle previous, etc.

My question was if I examine candles in reverse (by looking at High[i++]), how would I switch back to candles forward in time? I studied some indicator samples and it became obvious that when moving forward through candles that one would start with i = Total Candles - 1 and then decrement i to arrive to the previous candle (i=1).

I expressed myself poorly and I am trying to rapidly learn. Thank you for your consideration.

nipperdj
 
First of all, it's clear that you cant o into the future.

The programming of an indicator as you described is because the indicator usually plot a line out of some calulation, for example high + low / 2.

To print this line from the beginning, it is necessary to to at least once through all bars to compute the value at each bar.

To program an indicator you need to define indicator buffers, color etc, which you best get out of an indicator sample.

you can go at each tick through all the bars to calculate the indicator, for example


for ( int i = Bars; i >= 0; i-- ) {
    calculateIndicator( i );
}



in the calculate Function you will use the i value to access the bar at the given position and fill the buffer(s). Thats mainly all.

To avoid running through 100000 Bars at each tick, there is a function IndicatorCounted() which gives you the number of bars the indicator was run on. The return from this value can be subtracted from Bars. Then the calculation is run only for the bars which was not calculated.

The example would now look like this:

for ( int i = Bars - IndicatorCounted(); i >= 0; i-- ) {
    calculateIndicator( i );
}



with regards,

cori

 
I am mainly interested in testing on each candle not on ticks. So I could have something like this:

//Get Value of Indicator at Candle
CandleCnt=Bars
for(i=CandleCnt-1; i>0; i--)
   {
    Print("Value of indicator for Candle time "+TimeToStr(iTime(NULL,0,i-1));
    Print("Indicator value is " + DoubleToStr(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,i));
   }



I would then get in the log the close time (open time of succeeding bar i-1) and 5 period EMA value for each candle on the chart to which the advisor was attached, correct?

Thanks for your input and help.

nipperdj

 
nipperdj,

the value is calculated on the values of the candle as it is at the time, the indicator is run. So a candle in the past have Open, Close etc. as it is. But the calculation is run at each tick.

So if you have a code like the one you show above, it will be executed at each tick. Assuming the default values, there will be 128000 bars to go thru on each tick.

Therefore I mentioned the use of IndicatorCounted().

you can also check, if the Bar has changed and calculate only, if there is a new bar. Example:

int lastTimeBars = 0;

int start() {

   if  ( lastTimeBars == Bars ) {
      return;
   }
   lastTimeBars = Bars;

   ... do some calculation ...
}



regards cori

Reason: