buffer issues :(

 

Hi guys...

Im trying to learn how to use buffers properly in a custom indicator call...

so I found an indicator that plots multiple HA on a single chart and I wanted to code an indy that will make an alarm when the color changes...

here is where things get weird coz I can get it to work perfectly like this :

   double red5   = iCustom(Symbol(),0,"4 TF HAS Bar",2,6,3,2,0,0,1); // RED 5 min
   double blue5  = iCustom(Symbol(),0,"4 TF HAS Bar",2,6,3,2,0,1,1); // BLUE 5 min
   
      
   if(blue5>red5)
      {
      AlertOnce("color change 1min",1);
      return(0);
      }
      
   
   if(red5>blue5)
      {
      AlertOnce("color change 1min",2);
      return(0);
      }

the above alerts perfectly... but the minute I add another buffer to the alert criteria it just will not work at all...

here is what I mean (this just will not alert at all and I cant see why, because all i asked it to do is look one more candle back) :( :(

   double red5   = iCustom(Symbol(),0,"4 TF HAS Bar",2,6,3,2,0,0,1); // RED 5 min
   double blue5  = iCustom(Symbol(),0,"4 TF HAS Bar",2,6,3,2,0,1,1); // BLUE 5 min
   
   double red5l  = iCustom(Symbol(),0,"4 TF HAS Bar",2,6,3,2,0,0,2); // previous RED 5 min
   double blue5l = iCustom(Symbol(),0,"4 TF HAS Bar",2,6,3,2,0,1,2); // previous BLUE 5 min
      
   if(red5l>blue5l&&blue5>red5)
      {
      AlertOnce("color change 1min",1);
      return(0);
      }
      
   
   if(blue5l>red5l&&red5>blue5)
      {
      AlertOnce("color change 1min",2);
      return(0);
      }

the AlertOnce function works perfectly as I have used it tons before so thats not the issue...also the AlertOnce function works on the top code ...but when I add another criteria to look 2 candles back then it just does nothing...

Is there something obvious that im missing here? im completely stumped :(

thank in advance for any suggestions :)

have attached the custom indy that Im calling in the iCustom.

thanks guys

Files:
 

mqlearner:

Is there something obvious that im missing here? im completely stumped :(

   double red5   = iCustom(Symbol(),0,"4 TF HAS Bar",2,6,3,2,0,0,1); // RED 5 min
   double blue5  = iCustom(Symbol(),0,"4 TF HAS Bar",2,6,3,2,0,1,1); // BLUE 5 min

  1. You are passing 5 parameters "2,6,3,2,0" but the indicator has 8, pass ALL the parameters.
    extern int MaMetod  = 2;
    extern int MaPeriod = 6;
    extern int MaMetod2  = 3;
    extern int MaPeriod2 = 2;
    extern int BarWidth = 0;
    extern color UpBarColor = Blue;
    extern color DownBarColor = Red;
    extern int MaxBars=500;
    

 

thanx WHRoeder, Ill adjust that...but I am wondering why it still worked with the first instance only calling the last candle ....if it was the number of passed parameters wouldnt it be

faulty even if I called only the last candles value?

also Im sorry, i did press the src button...if there is something else about the src button i didnt use I apologize.

thanks for your help ;)

 
mqlearner:

..but I am wondering why it still worked with the first instance only calling the last candle ....if it was the number of passed parameters wouldnt it be

faulty even if I called only the last candles value?

iCustom is a truly weird function. Apparently (by test) you can pass no parameters to the indicator, all parameters, or any number in between and it still works! This is unusual behavior for a function as default parameters are usually at the end of a function call. Having realised that it does this, I had a think and figured out how you could make a function like that. It is not very efficient, computationally speaking, but it is quite doable if that's what you want to do.
 

Thanx Dabbler,

to be honest I am just trying to understand the buffer issues in this example. I thought I understood how buffers worked and had that confirmed when I got the indy to alert me when the HAS changed color but then had my smugness shattered when all of a sudden it decided that if I add another candle to the call then it just does nothing.

to put it in terms that I understand (maybe this is where I need correcting)

1: my indy calls the red and blue buffer for the last closed candle and then sees which one is greater ...then it returns an alert which tells me the color - this worked fine

2: now I added another criteria to look 1 candle further back [2] and if that candle was for example Red and the last closed candle [1] was blue then alert me there was a color change. - this just sat there and did nothing.

but the only difference was going from :

if(blue5>red5)
      {
      AlertOnce("color change 1min",1);
      return(0);
      }

to...

if(red5l>blue5l&&blue5>red5)
      {
      AlertOnce("color change 1min",1);
      return(0);
      }

I am confused as to why adding 1 more parameter in the if statement stops the indy from alerting me when it works perfectly in the 1st instance above...

I am not really trying to make an indicator out of all this I am trying to practice using and understanding buffers and thought that I had figured it out...now with the way that this indiy is acting im confused.

I have tried passing all the parameters as suggested by WHRoeder and hopefully this mornings testing will prove that was the problem... im holding thumbs it is :)

 
mqlearner:

I am confused as to why adding 1 more parameter in the if statement stops the indy from alerting me when it works perfectly in the 1st instance above...

Yes, well this practice of putting 15 tests in one if statement is really not that helpful when you don't have a debugger. I often write one test per if statement so it is easier to debug.

if( red5l>blue5l ){
    Print("That test worked");
    if( blue5>red5){
      AlertOnce("color change 1min",1);
      return(0);
    }
}

It just allows you to break the problem down. Once it works you can eliminate the print statement. If the test fails you could print the values of red51 and blue51 for this situation. You need to probe the code to see what is happening. A debugger would allow you to step through the code line by line and see values. Without that either Print to file or to the log to see what is happening at various points in the program.

 

thanx dabbler,

ill try all your suggestions by using OllyDBG and W32dasm & the Print function.

thanx for the direction, much appreciated.

 
mqlearner:

... by using OllyDBG and W32dasm & the Print function.

I had never heard of those. I just googled them. No, I would strongly not recommend you using them to debug MQL4. That really is making trouble for yourself.
 

I just cant get it to alert me when (as an example) : the 15min is red and then the 5min turns from blue to red.

what really bugs me about all this is that if I do this :

this works perfectly below : (this compares the last candles color on 4 time frames and checks that the last candle on the 60min changed from blue to red)

if(red5>blue5&&red15>blue15&&red30>blue30&&red60l<blue60l&&red60>blue60){AlertOnce("They are all the SAME color on : "+Symbol(),1);return(0);}

but the minute I use just 2 of the time frames like 5min and 15min like this : (this wont work at all, even tho exact same logic as above code which works on 4 time frames)

if(red15>blue15&&red5l<blue5l&&red5>blue5){AlertOnce("2 Are the Same color on : "+Symbol(),7);return(0);}

Now as it is above it wont do anything! urgh! :(

I have also tried it like this : (but doesn't want to alert) seems if I use any 1 of the time frames on its own...or all 4 time frames at once it works fine but if I use 2 time frames then it refuses to alert me at all

if(red15<blue15) // if 15min trend is BLUE
         {
         if(red5l>blue5l) // if before last 5min candle was RED
           {
         if(red5<blue5) // if last 5min candle turned BLUE
            {
        AlertOnce("2 Are the Same color on : "+Symbol(),7);return(0);
            }
           }
         }
 

Ok so you have a bug and you need to learn how to debug your programs.

Put in the print statements like I showed you earlier and see which tests work and which don't.

In your horrible examples above, with all the tests mashed together, you test something not tested in your earlier 4 test example.

red5l<blue5l
 

I apologize for the horrible examples...I'm a self taught coder (dont think I even deserve that title, more like aspiring wannabe coder).


I will do the print statements as soon as the market is open and I can get some movement on the indy to produce results to test.

I wont ask you for help again until I have tried to debug it with the print statements...promise. :)

thanx for your patience and responses... I think you need a medal for putting up with slow learner like me. :) :) :)

Reason: