look back one candle

 

Hi Guys,


I need some help please, im a newby to mql. Im trying to combine two indicators into one but have hit a brick wall. I can get an arrow to print using "is not equal to" for one indicator. But for the second the arrow is printed on the pervious candle. what is the best way to do that?


so

indicator 1 prints an arrow i use the "is not equal to" 

indicator 2 prints arrow on the previous candle.

what i want to achieve is for the indicator in combining to check both conditions and print a single arrow.



Many Thanks

 
The best way is to first calculate the arrow type and then plot that one result above (or below) the newest candle.
 
Marco vd Heijden:
The best way is to first calculate the arrow type and then plot that one result above (or below) the newest candle.

Hi Marco,


Thanks for the reply my friend, what function do i use to read the arrow on the pervious candle?

 

Depends on the source.

If you are using a iCustom call then you can pass the buffer and it's position as parameters.

 
Marco vd Heijden:

Depends on the source.

If you are using a iCustom call then you can pass the buffer and it's position as parameters.

Sorry for the ignorance Marco, where can i find the instructions to do that or even an example? or would you know the code the for the call using icustom please mate?


Many Thanks

 

Yes look back one candle is candle 1 since the current candle is candle 0.

So depending on which value you need you can just call for it directly by specifying the buffer and candle number.

But first you have to post at least one coding attempt so that we can identify what language you are using.

This is to prevent us from answering in the wrong language.
 
Marco vd Heijden:

Yes look back one candle is candle 1 since the current candle is candle 0.

So depending on which value you need you can just call for it directly by specifying the buffer and candle number.

But first you have to post at least one coding attempt so that we can identify what language you are using.

This is to prevent us from answering in the wrong language.

Hi Marco, 

please see attached the code, i need indicator 2 (indi 2) to look at the previous  candle and see if there is an arrow there

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

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- 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+" | 100820v1 @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 233);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 234);
   //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[],
                const int& spread[])
  {
   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(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(iCustom(NULL, PERIOD_CURRENT, "indi 1", 2, false, 0, i) != 0 && iCustom(NULL, PERIOD_CURRENT, "indi 1", 2, false, 0, i) != EMPTY_VALUE 
      && iCustom(NULL, PERIOD_CURRENT, "indi 2", 3, false, false, 1, i) != 0 && iCustom(NULL, PERIOD_CURRENT, "indi 2", 3, false, false, 1, i) != EMPTY_VALUE // look back 1 candle to see if ether is an an arrow there.
      
      
       )
        {
         Buffer1[i] = Low[0+i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iCustom(NULL, PERIOD_CURRENT, "indi 1", 2, false, 1, i) != 0 && iCustom(NULL, PERIOD_CURRENT, "indi 1", 2, false, 1, i) != EMPTY_VALUE
      && iCustom(NULL, PERIOD_CURRENT, "indi 2", 3, false, false, 0, i) != 0 && iCustom(NULL, PERIOD_CURRENT, "indi 2", 3, false, false, 0, i) != EMPTY_VALUE     
     
      )
      
        {
         Buffer2[i] = High[0+i]; //Set indicator value at Candlestick High
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
shabaz:

Hi Marco, 

please see attached the code, i need indicator 2 (indi 2) to look at the previous  candle and see if there is an arrow there

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

Hi Keith,


Got it, will ensure i do so going forward.

 
shabaz:

Hi Marco, 

please see attached the code, i need indicator 2 (indi 2) to look at the previous  candle and see if there is an arrow there

Okay so since your loop is counting down.

   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      //...
     }

Then the previous candle will be i+1.

So you shift one bar in the iCustom call.

For example:

iCustom(NULL, PERIOD_CURRENT, "indi 1", 2, false, 1, i); // Current Bar
iCustom(NULL, PERIOD_CURRENT, "indi 1", 2, false, 1, i+1)// Previous Bar

However if you only want realtime results then you do not need to run the entire loop.

It would suffice to just check the last bar on every new tick, this is for example when you only want to use the reading to make a trading decision.

If you want to create new arrows (in the past) on the chart visually, then you do want to run the entire loop, so at this point it depends what is your exact target. 

 
Marco vd Heijden:

Okay so since your loop is counting down.

Then the previous candle will be i+1.

So you shift one bar in the iCustom call.

For example:

However if you only want realtime results then you do not need to run the entire loop.

It would suffice to just check the last bar on every new tick, this is for example when you only want to use the reading to make a trading decision.

If you want to create new arrows (in the past) on the chart visually, then you do want to run the entire loop, so at this point it depends what is your exact target. 

Marco, your a star!! that was exactly what I needed.


Thank you

Reason: