mql4 newbie - need help understanding this - thank you in advance!

 

I'm a newbie at mql4 so bare with me.

I've been playing with a simple mql4 indicator file and I have a question. First of all let me say that I prefer longer trading timeframes such as the H1 timeframe. As I understand it you have the predefined "Bars" variable and such which are required when looping through the display bars(Candles) on a given chart window. As I see the loop is fairly standard on many indicators. Since I am using the H1 timeframe each new candle is drawn every hour of course, however if I am looking to alert off of a simple crossover of 2 MA's this could happen at any time prior to the current candles close. I've noticed on my simple indicator that I don't get any alerts until the current candle actually closes. How would I code it to send my alert at the moment the crossover occurs rather than at the close of the current candle? I've noticed on some EA's they don't use loops but instead calculate the trigger alert off of a simple crossover formula with the shift set to zero. Bottom line is that I want to trigger my alert the moment it happens as opposed to when the current candle closes. Most of the code below was copied directly from a few mq4 files.

int start()

{

int counter, // Bar index

int Counted_bars; // Number of counted bars

Counted_bars=IndicatorCounted(); // Number of counted bars

i=Bars-Counted_bars-1; // Index of the first uncounted

while(i>=0) // Loop for uncounted bars

{


RSIpl_now = iCustom(NULL,0,"TDI Red Green",13,0,34,2,0,7,0,4,i);

RSIpl_prev = iCustom(NULL,0,"TDI Red Green",13,0,34,2,0,7,0,4,i+1);

RSIpl_after = iCustom(NULL,0,"TDI Red Green",13,0,34,2,0,7,0,4,i-1);

RSItsl_now = iCustom(NULL,0,"TDI Red Green",13,0,34,2,0,7,0,5,i);

RSItsl_prev = iCustom(NULL,0,"TDI Red Green",13,0,34,2,0,7,0,5,i+1);

RSItsl_after = iCustom(NULL,0,"TDI Red Green",13,0,34,2,0,7,0,5,i-1);


if ((RSIpl_now >= RSItsl_now) && (RSIpl_prev <= RSItsl_prev) && (RSIpl_after >= RSItsl_after)) {

ArrowUp[i] = Low[i];

Alert("Something Something...", TimeToStr(TimeLocal(),TIME_SECONDS)); // This is just for demonstration purposes

}

else if ((RSIpl_now <= RSItsl_now) && (RSIpl_prev > RSItsl_prev) && (RSIpl_after < RSItsl_after)) {

ArrowDown[i] = High[i];

Alert("Something Something...", TimeToStr(TimeLocal(),TIME_SECONDS)); // This is just for demonstration purposes

}

i--; // Calculating index of the next bar

}

 
In your code you have the shift i and it is using a while loop so it counts backwards i-- until it gets to the condition which makes it false which is coded with this:

while(i>=0 code....... i-- counting backwards from i=Bars-Counted_bars-1

So when it hits -1 it terminates, and thus your may not be getting values during the time of your conditional statement.


A shift of 0 by itself is different in that it is always true when the condition occurs during the current candle and thus does not terminate

Use some Print(statements of your variables to see the conditions that exist and the values so that you can code as needed

P.S

I'm a Noob so gurus please correct me if I'm wrong

 
ferdware:
Bottom line is that I want to trigger my alert the moment it happens as opposed to when the current candle closes.

  1. RSIpl_after = iCustom(NULL,0,"TDI Red Green",13,0,34,2,0,7,0,4,i-1);
    i-1 is the future, don't read the future.
  2. Counted_bars=IndicatorCounted(); // Number of counted bars
    i=Bars-Counted_bars-1; // Index of the first uncounted
    while(i>=0) // Loop for uncounted bars
    You are looking one bar in the past with iCustom(... i+1) so on the initial run IndicatorCounted() is zero and i=Bars-1, i+1 is Bars which does not exist (0 to Bars-1.) In addition the tdi has
    extern int RSI_Period=13;         //8-25
    extern int Volatility_Band=34;    //20-40
    The tdi red green is not valid until you have at least 34 bars (max of 13, 34)
    #define DRAW_BEGIN 35 // TDI 34 + 1 back.
    int init(){
       :
       SetIndexDrawBegin(0,DRAW_BEGIN);
       SetIndexBuffer(0,ArrowUp);
       SetIndexDrawBegin(1,DRAW_BEGIN);
       SetIndexBuffer(1,ArrowDown);
    }
    int start(){
       static datetime LastAlert;
       int Counted_bars=IndicatorCounted(); // Number of counted bars
       if (Counted_bars < DRAW_BEGIN) Counted_bars = DRAW_BEGIN;
       for (int i=Bars - 1 - Counted_bars; i >= 0; i--){ // Loop for uncounted bars
          :
          if ((RSIpl_now >= RSItsl_now) && (RSIpl_prev <= RSItsl_prev)) {
             ArrowUp[i] = Low[i];
             if (LastAlert != Time[i]){ LastAlert = Time[i]; // One alert per bar
                 Alert("Something Something...", 
                       TimeToStr(TimeLocal(),TIME_SECONDS)); // demonstration purposes
             }
          }
          else if (...
    The LastAlert will prevent multiple alerts on bar zero.
 
WHRoeder:

  1. i-1 is the future, don't read the future.
  2. You are looking one bar in the past with iCustom(... i+1) so on the initial run IndicatorCounted() is zero i=Bars-1, i+1 is Bars which does not exist. In addition the tdi has The tdi red green is not valid until you have at least 34 bars (max of 13, 34) The LastAlert will prevent multiple alerts on bar zero.

Thank You for your replies. I will test them out, I'm sure with better results!
Reason: