Draw arrow only when candle closed

 

hi sorry i need a help regarding mq4 code, i have a problem with the arrow of an indicator, it always appears before the candle closed (i mean like appear and dissaper till the condition met). i have set to appear when the candle closed, but it's not work. all i want is only appear after the condition met and the candle closed. thank's in advance.

here is my code

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

//|                                         Indicator: nobother.mq4 |

//|                                       Created with EABuilder.com |

//|                                             http://eabuilder.com |

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

#property copyright "Created with EABuilder.com"

#property link      "http://eabuilder.com"

#property version   "1.00"

#property description "just try"



#include <stdlib.mqh>

#include <stderror.mqh>



//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 2



#property indicator_type1 DRAW_ARROW

#property indicator_width1 3

#property indicator_color1 0xFFAA00

#property indicator_label1 "BUY"



#property indicator_type2 DRAW_ARROW

#property indicator_width2 3

#property indicator_color2 0x0000FF

#property indicator_label2 "Sell"



//--- indicator buffers

double Buffer1[];

double Buffer2[];



extern int Period1 = 120;

datetime time_alert; //used when sending alert

extern bool Send_Email = true;

extern bool Audible_Alerts = true;

extern bool Push_Notifications = true;

double myPoint; //initialized in OnInit



void myAlert(string type, string message)

  {

   if(type == "print")

      Print(message);

   else if(type == "error")

     {

      Print(type+" | indicatorx @ "+Symbol()+","+IntegerToString(Period())+" | "+message);

     }

   else if(type == "order")

     {

     }

   else if(type == "modify")

     {

     }

   else if(type == "indicator")

     {

      if(Audible_Alerts) Alert(type+" | indicatorx @ "+Symbol()+","+IntegerToString(Period())+" | "+message);

      if(Send_Email) SendMail("indicatorx", type+" | indicatorx @ "+Symbol()+","+IntegerToString(Period())+" | "+message);

      if(Push_Notifications) SendNotification(type+" | indicatorx @ "+Symbol()+","+IntegerToString(Period())+" | "+message);

     }

  }



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

//| Custom indicator initialization function                         |

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

int OnInit()

  {   

   IndicatorBuffers(2);

   SetIndexBuffer(0, Buffer1);

   SetIndexEmptyValue(0, 0);

   SetIndexArrow(0, 221);

   SetIndexBuffer(1, Buffer2);

   SetIndexEmptyValue(1, 0);

   SetIndexArrow(1, 222);

   //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(iMA(NULL, PERIOD_CURRENT, Period1, 0, MODE_LWMA, PRICE_CLOSE, i) < iCustom(NULL, PERIOD_CURRENT, "indicatory", i)

      && iMA(NULL, PERIOD_CURRENT, Period1, 0, MODE_LWMA, PRICE_CLOSE, i+1) > iCustom(NULL, PERIOD_CURRENT, "indicatory", i+1) //test it

      )

        {
            if (i>0
         Buffer1[i] = Low[2+i] - 5 * myPoint; //Set indicator value at Candlestick Low - fixed value

         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "BUY"); //Alert on next bar open

         time_alert = Time[1];

        }

      else

        {

         Buffer1[i] = 0;

        }

      //Indicator Buffer 2

      if(iMA(NULL, PERIOD_CURRENT, Period1, 0, MODE_LWMA, PRICE_CLOSE, i) > iCustom(NULL, PERIOD_CURRENT, "indicatory, i)

      && iMA(NULL, PERIOD_CURRENT, Period1, 0, MODE_LWMA, PRICE_CLOSE, i+1) < iCustom(NULL, PERIOD_CURRENT, "indicatory, i+1) //test it

      )

        {
            if (i>0
         Buffer2[i] = Low[2+i] + 5 * myPoint; //Set indicator value at Candlestick Low + fixed value

         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open

         time_alert = Time[1];

        }

      else

        {

         Buffer2[i] = 0;

        }

     }

   return(rates_total);

  }

//+------------------------------------------------------------------+
 
penguinblaster:

hi sorry i need a help regarding mq4 code, i have a problem with the arrow of an indicator, it always appears before the candle closed (i mean like appear and dissaper till the condition met). i have set to appear when the candle closed, but it's not work. all i want is only appear after the condition met and the candle closed. thank's in advance.

here is my code

Just don't assign value to Buffer1[] and Buffer2[] when i==0.

 
Seng Joo Thio:

Just don't assign value to Buffer1[] and Buffer2[] when i==0.

sorry iam a newbie in coding, i used eabuilder to make custom indicator,, i dont know how to correct it, can you please show the code i should change ?

thank's in advance

 
penguinblaster:

sorry iam a newbie in coding, i used eabuilder to make custom indicator,, i dont know how to correct it, can you please show the code i should change ?

thank's in advance

Change

         Buffer1[i] = Low[2+i] - 5 * myPoint; //Set indicator value at Candlestick Low - fixed value

to

         if (i>0)
	    Buffer1[i] = Low[2+i] - 5 * myPoint; //Set indicator value at Candlestick Low - fixed value
likewise for the line for Buffer2.
 
Seng Joo Thio:

Change

to

likewise for the line for Buffer2.
Yes you're right, it solved now. thank you very much.. 
 

if i may, i have one more question to you. in eabuilder we use 2 indicator and combine them in to one, but how can we make  two indicator into one, but all the code are inserted into that one.

eg. i make MA cross in eabuilder, but the resulted indicator code is only showed the buffer on the main indicator. no combined code inside it.

what i mean is that we insert those 2 indicator code into one, and build a new indicator based on them.

sorry for asking.

 
penguinblaster:

if i may, i have one more question to you. in eabuilder we use 2 indicator and combine them in to one, but how can we make  two indicator into one, but all the code are inserted into that one.

eg. i make MA cross in eabuilder, but the resulted indicator code is only showed the buffer on the main indicator. no combined code inside it.

what i mean is that we insert those 2 indicator code into one, and build a new indicator based on them.

sorry for asking.

You'll have to do some programming (i.e. beyond what eabuilder can do) in order to combine separate indicators' codes into one.

 
Seng Joo Thio:

You'll have to do some programming (i.e. beyond what eabuilder can do) in order to combine separate indicators' codes into one.

sorry for asking that,,

another one can i ?

my indicator is repaint, but thats not the problem, the problem is i cant see the history how bad it was. can i have all arrow that printed by the indicator, even if it makes a new one.

eg: it shows on candle one closed, but the condition still happend on candle two closed and so on. can we have all arrow printed to all those candle?

 
penguinblaster:

sorry for asking that,,

another one can i ?

my indicator is repaint, but thats not the problem, the problem is i cant see the history how bad it was. can i have all arrow that printed by the indicator, even if it makes a new one.

eg: it shows on candle one closed, but the condition still happend on candle two closed and so on. can we have all arrow printed to all those candle?

From the code, it does not repaint, and all past arrows remain displayed...

of course, I don't have your "iCustom(NULL, PERIOD_CURRENT, "indicatory", i)" so I cannot really run and test.

 
Seng Joo Thio:

From the code, it does not repaint, and all past arrows remain displayed...

of course, I don't have your "iCustom(NULL, PERIOD_CURRENT, "indicatory", i)" so I cannot really run and test.

iam combining repaint indicator and ma

and I wonder  Buffer2[i] = Low[2+i] what is that means ?

 
penguinblaster:

and I wonder  Buffer2[i] = Low[2+i] what is that means ?

Nothing, just a "suitable" price level for drawing arrows - you can play around with other values and see what happens.