Indicator buffers and shift - page 2

 

well I still havent figured it out, I have narrowed it down to the buffer it doesnt make sense though, the lines for both buffers are drawn correctly on the screen lma and hma are doubles declared like this:

double hma=Buf_4[0], lma=Buf_5[0];
Buf_4[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_HIGH,i+1);
Buf_5[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_LOW,i+1);

I make Alerts like this at the relevent places at the point when the conditions are met, the bid is lower than lma or the bid is higher than hma

Alert("lma= ",lma," Bid= ",Bid);
Alert("hma= ",hma," Bid= ",Bid);

I get the expected value for hma but lma returns a crazy number, the alerts read like this:

lma=214782647 Bid=1.3502

hma= 1.3504 Bid= 1.3505

I let the Alerts run for a while, sometimes both Alerts returns a proper value like this:

lma=1.3499 Bid=1.3495

hma=1.3510 Bid=1.3511

it doesnt make sense how can hma return the expected value, while lma returns a totaly irrelevent value but yet the lines are drawn correctly on the screen and then other times both values returns the expected values i beleive this must be a bug im going to reboot my computer and see what happens.

 

Looks like you're using drawing buffers. They are automatically initialized with EMPTY_VALUE which is the 'crazy number' you're seeing. 

Where's the loop? Using index usually involves a loop. I usually use plain variables if buffers are not necessary : 

  double hma, lma;

  hma=iMA(NULL,0,3,0,MODE_LWMA,PRICE_HIGH,1); // shift 1 = last completed bar
  lma=iMA(NULL,0,3,0,MODE_LWMA,PRICE_LOW,1);

  if(grf!=0 && dir()==2 && Bid<lma)
    { // remove unnecessary variables if possible
     tc++;
     grc=tc;
     grf=0;    //direction flag
     make_arrow(grc,grf,"mixed buy",Ask); 
    }

void make_arrow(int id,bool dir,string type,double price)
  {
   ObjectCreate(S1[2,1]+id+"a"+type,OBJ_ARROW,0,Time[0],price,0);
   if(dir==0)
     {ObjectSet(S1[2,1]+id+"a"+type,OBJPROP_ARROWCODE,172);
     }
   if(dir==1)
     {ObjectSet(S1[2,1]+id+"a"+type,OBJPROP_ARROWCODE,172);
     }
   return;
  }
 

yes they are drawing buffers in an indicator to draw the lines for the EA that uses the same values, the variables lma and hma are declared and initialized with the values of Buf_4[0] and Buf_5[0] respectively before the buffers are filled is that the reason for this ?

i didnt think that would matter as the buffers are subsequently filled before those variables are called in the program, I am going to modify the code to initialize those two doubles after the buffers are filled, but why would this happen intermittently sometimes the variable lma returns the expected value, sometimes it doesnt, it seems this is related to a post last week https://www.mql5.com/en/forum/129872 I tried to replicate his results they also returned that same number when he did it, but when I ran the same test as him it didnt happen.

 
I've added code to last post...
 

loops are like this

int init() 
  {int i,j,k;
//---- indicators
   SetIndexBuffer(0,Buf_0);
   SetIndexBuffer(1,Buf_1);
   SetIndexBuffer(2,Buf_2);
   SetIndexBuffer(3,Buf_3);
   SetIndexBuffer(4,Buf_4);
   SetIndexBuffer(5,Buf_5);
   SetIndexBuffer(6,Buf_6);
   SetIndexBuffer(7,Buf_7);
   for(i=0; i<=7; i++)
    {SetIndexStyle(i,DRAW_LINE);
    }
//----
   return(0);
  }
int start()
  {
   int    counted_bars=IndicatorCounted();
          int i=Bars-counted_bars-1;
   double adj=0,gma=Buf_0[0],rma=Buf_1[0],yma=Buf_2[0],bma=Buf_3[0],hma=Buf_4[0],
          lma=Buf_5[0],ahb=Buf_6[0],alb=Buf_7[0],
          grcp;

   while(i>=0)
    {Buf_0[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_OPEN,i+1);
     Buf_1[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_CLOSE,i+1);
     Buf_2[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_MEDIAN,i+1);
     Buf_3[i]=iMA(NULL,0,5,0,MODE_LWMA,PRICE_WEIGHTED,i+1);
     Buf_4[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_HIGH,i+1);
     Buf_5[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_LOW,i+1);
     adj=(Buf_4[i]-Buf_5[i])/3;
     Buf_6[i]=Buf_4[i]-adj; //higher band
     Buf_7[i]=Buf_5[i]+adj; //lower band
     i--;
    }

so then when i use those doubles declared as the buffer values should I assume they are unreliable ? I will need to change more than just the arrow function if that is the case because I used the values of the buffers for other things too.

 

Posting that earlier could save us couple of goose-chases ;)

 No, both are reliable. We should use the right tool for each purpose. Let me see if I can sort your code from the two pages. 

 

thanks for looking at it cameofx, I have run up against a simalar problem before with another indicator i wrote that also appeared to work correctly untill i tried using its line values to buy or sell if the price was above or below the line, the results were simalar, the buy/ sell signals were in the wrong place, I narrowed that down to iCustom was not returning the same values for the buffers as what was being displayed on the screen, that was one of my first forrays into indicator coding and I figured I must have done something wrong and discarded that indicator, it looks like I am still making the same mistake.

 

Try this. Amend it to your need of course. There are still omitted parts from what you posted. 

Files:
 

Thanks for taking the time to look at that for me I'll change it according to your code and see what happens

 

Your welcome, I got your full version. I can't promise a quick fix though. I'll see what I can do.  

Reason: