Need help with icustom value!

 

Hi, guys, im new to mql4 and this forum~ i hope i can get some help with my problem here. A couple days ago, i just started my first simple ea by just using icustome function with some indicator, i thought everything is done until i started to backtest it. There was a serious problem: the ea couldn't get the right value from one mtf indicator, so i tried to solve it myself, two days passed, no solution...even i have Google searched the related topics and tried those ways mentioned in the threads, it still didn't work. Since i just know something basic to the proramming, so i decide to post my problem here hoping some experts can solve it.  I will attach the two indicators, the "MTF_Bar" one is the one i wanna get value, and it need use the other indicator "NonLag".

Below is my EA code:



int Signal()
   {
   double sig1, sig2, sig3, sig4,sig5, sig6, sig7, sig8;  
   sig1=iCustom(NULL,0,"4BARS-MTF-NLMA 2",21,0,0,0,1);
   sig2=iCustom(NULL,0,"4BARS-MTF-NLMA 2",21,0,0,1,1);
   sig3=iCustom(NULL,0,"4BARS-MTF-NLMA 2",21,0,0,2,1);
   sig4=iCustom(NULL,0,"4BARS-MTF-NLMA 2",21,0,0,3,1);
   sig5=iCustom(NULL,0,"4BARS-MTF-NLMA 2",21,0,0,4,1);
   sig6=iCustom(NULL,0,"4BARS-MTF-NLMA 2",21,0,0,5,1);
   sig7=iCustom(NULL,0,"4BARS-MTF-NLMA 2",21,0,0,6,1);
   sig8=iCustom(NULL,0,"4BARS-MTF-NLMA 2",21,0,0,7,1);
   if(sig1==2.0 && sig3==1.5 && sig5==1.0 && sig7==0.5) 
   {
   return(1);  // 4 Green Bar = Buy Order
   }
   if(sig2==2.0 && sig4==1.5 && sig6==1.0 && sig8==0.5)
   {
   return(2); // 4 Red Bar = Sell Order
   }
   } 

I check the ea log, sometime when two bars are still green, one sell order is placed, i use the Print function to get the values :

  sig2=2.0 sig4=1.5 sig6=1.0 sig8=0.5 sig1=0 sig3=0 sig5=0 sig7=0 (it is same as 4 red bars)

Checked the indicator code, below is just my guess the reason for this unmatch issue, getting revalued everytime the price change?? 

for(i=limit; i>=0; i--)
 {
  Period_1_shift = iBarShift(NULL,Period_1,Time[i]);
  Period_2_shift = iBarShift(NULL,Period_2,Time[i]);
  Period_3_shift = iBarShift(NULL,Period_3,Time[i]);
  Period_4_shift = iBarShift(NULL,Period_4,Time[i]);
  
  
  NonLagUpPeriod_1 = iCustom(NULL,Period_1,"NonLagMA_v7.1",Price,Length,0,0,1,ColorBarBack,1,Period_1_shift) ;
  NonLagDnPeriod_1 = iCustom(NULL,Period_1,"NonLagMA_v7.1",Price,Length,0,0,1,ColorBarBack,2,Period_1_shift) ;
  NonLagUpPeriod_2 = iCustom(NULL,Period_2,"NonLagMA_v7.1",Price,Length,0,0,1,ColorBarBack,1,Period_2_shift) ;
  NonLagDnPeriod_2 = iCustom(NULL,Period_2,"NonLagMA_v7.1",Price,Length,0,0,1,ColorBarBack,2,Period_2_shift) ;
  NonLagUpPeriod_3 = iCustom(NULL,Period_3,"NonLagMA_v7.1",Price,Length,0,0,1,ColorBarBack,1,Period_3_shift) ;
  NonLagDnPeriod_3 = iCustom(NULL,Period_3,"NonLagMA_v7.1",Price,Length,0,0,1,ColorBarBack,2,Period_3_shift) ;
  NonLagUpPeriod_4 = iCustom(NULL,Period_4,"NonLagMA_v7.1",Price,Length,0,0,1,ColorBarBack,1,Period_4_shift) ;
  NonLagDnPeriod_4 = iCustom(NULL,Period_4,"NonLagMA_v7.1",Price,Length,0,0,1,ColorBarBack,2,Period_4_shift) ;
  
  if  ( NonLagUpPeriod_1 != EMPTY_VALUE && NonLagDnPeriod_1 == EMPTY_VALUE) {buf1_up[i] = Gap1; buf1_dn[i] = 0.0;}
  else if ( NonLagUpPeriod_1 == EMPTY_VALUE && NonLagDnPeriod_1 != EMPTY_VALUE) {buf1_dn[i] = Gap1; buf1_up[i] = 0.0;}
  else if ( NonLagUpPeriod_1 != EMPTY_VALUE && NonLagDnPeriod_1 != EMPTY_VALUE) {buf1_dn[i] = buf1_dn[i+1]; buf1_up[i] = buf1_up[i+1];}
  
  if  ( NonLagUpPeriod_2 != EMPTY_VALUE && NonLagDnPeriod_2 == EMPTY_VALUE) {buf2_up[i] = Gap2; buf2_dn[i] = 0.0;}
  else if ( NonLagUpPeriod_2 == EMPTY_VALUE && NonLagDnPeriod_2 != EMPTY_VALUE) {buf2_dn[i] = Gap2; buf2_up[i] = 0.0;}
  else if ( NonLagUpPeriod_2 != EMPTY_VALUE && NonLagDnPeriod_2 != EMPTY_VALUE) {buf2_dn[i] = buf2_dn[i+1]; buf2_up[i] = buf2_up[i+1];}
  
  if  ( NonLagUpPeriod_3 != EMPTY_VALUE && NonLagDnPeriod_3 == EMPTY_VALUE) {buf3_up[i] = Gap3; buf3_dn[i] = 0.0;}
  else if ( NonLagUpPeriod_3 == EMPTY_VALUE && NonLagDnPeriod_3 != EMPTY_VALUE) {buf3_dn[i] = Gap3; buf3_up[i] = 0.0;}
  else if ( NonLagUpPeriod_3 != EMPTY_VALUE && NonLagDnPeriod_3 != EMPTY_VALUE) {buf3_dn[i] = buf3_dn[i+1]; buf3_up[i] = buf3_up[i+1];}
  
  if  ( NonLagUpPeriod_4 != EMPTY_VALUE && NonLagDnPeriod_4 == EMPTY_VALUE) {buf4_up[i] = Gap4; buf4_dn[i] = 0.0;}
  else if ( NonLagUpPeriod_4 == EMPTY_VALUE && NonLagDnPeriod_4 != EMPTY_VALUE) {buf4_dn[i] = Gap4; buf4_up[i] = 0.0;}
  else if ( NonLagUpPeriod_4 != EMPTY_VALUE && NonLagDnPeriod_4 != EMPTY_VALUE) {buf4_dn[i] = buf4_dn[i+1]; buf4_up[i] = buf4_up[i+1];}
 }

 Above is my problem, so im waiting for someone can help, i really appreaciate any help or advices, thanks!!!

Two indicators  

Files:
 
if(sig1==2.0 && sig3==1.5 && sig5==1.0 && sig7==0.5)
Don't use equal sign with doubles. MoreInfo.
 
ubzen: Don't use equal sign with doubles. MoreInfo.
Normally true, but in this case, those are the exact values the indicator places in the buffers. I would have just used
if(sig1 * sig3 * sig5 * sig7 != 0) return(1); // All nonzero
 

ftwfx: Below is my EA code:

int Signal(){
   double sig1=...
   if(sig1==2.0 && sig3==1.5 && sig5==1.0 && sig7==0.5)
      return(1);  // 4 Green Bar = Buy Order
   if(sig2==2.0 && sig4==1.5 && sig6==1.0 && sig8==0.5)
      return(2); // 4 Red Bar = Sell Order
} 

I check the ea log, sometime when two bars are still green, one sell order is placed, i use the Print function to get the values :

If it's not 4 Green bars and not 4 red bars, what do you return from Signal?


Maybe the problem is not with your indicator code (Signal) but with what you do with it.

  1. Print the value from Signal()
  2. Check your return codes.
  3. Post your code. There are no mind-readers here.
 

  thank for the reply,  use the return value from Signal by this way

 

//New Buy Order 

  if(Trend() && Signal()==1 && ok!=Time[0] && Mspread() && BuyOrderNumber()==0 && SellOrderNumber()==0)
  {
  BuyTicket=OrderSend(Symbol(),OP_BUY,GetLots(),Ask,SlipPage,0,0,TradeComment,MagicNumber,0,Blue);
  if (BuyTicket<0) 
  {
  Alert(Symbol()+" Buy OrderSend failed with error # "+GetLastError());
  }
  if (OrderModify(BuyTicket,OrderOpenPrice(),Bid-StopLoss*Point,Ask+TakeProfit*Point,0,0)==false) 
  Alert(Symbol()+" "+BuyTicket+" Buy Order Modify Fail, Error: "+GetLastError());
  else Alert(Symbol()+" "+BuyTicket+" Buy Order Modify Success");

  Print ("K ",sig1," - ",sig3," - ",sig5," - ",sig7," - ",sig2," - ",sig4," - ",sig6," - ",sig8);
  ok=Time[0];
  }  

 

//New Sell Order 

  if(Trend() && Signal()==2 && ok!=Time[0] && Mspread() && BuyOrderNumber()==0 && SellOrderNumber()==0)
  {
  SellTicket=OrderSend(Symbol(),OP_SELL,GetLots(),Bid,SlipPage,0,0,TradeComment,MagicNumber,0,Red);
  if (SellTicket<0) 
  {
  Alert(Symbol()+" Sell OrderSend failed with error # "+GetLastError());
  }
  if (OrderModify(SellTicket,OrderOpenPrice(),Ask+StopLoss*Point,Bid-TakeProfit*Point,0,0)==false) 
  Alert(Symbol()+" "+SellTicket+" Sell Order Modify Fail, Error: "+GetLastError());
  else Alert(Symbol()+" "+SellTicket+" Sell Order Modify Success"); 

  Print ("K ",sig2," - ",sig4," - ",sig6," - ",sig8," - ",sig1," - ",sig3," - ",sig5," - ",sig7);
  ok=Time[0];
  }   

 

3Red bar + 1 Green bar, a new sell order is placed.

2013.02.24 17:31:49 2013.02.13 07:00  GodGift - I EURUSDpro,M15: K 2 - 1.5 - 1 - 0.5 - 0 - 0 - 0 - 0
2013.02.24 17:31:49 2013.02.13 07:00  GodGift - I EURUSDpro,M15: Alert: EURUSDpro 1 Sell Order Modify Success
2013.02.24 17:31:49 2013.02.13 07:00  GodGift - I EURUSDpro,M15: modify #1 sell 2.00 EURUSDpro at 1.34426 sl: 1.35454 tp: 1.34226 ok
2013.02.24 17:31:49 2013.02.13 07:00  GodGift - I EURUSDpro,M15: open #1 sell 2.00 EURUSDpro at 1.34426 ok

 4 Red bar  another new sell order is placed, this time works fine :)

2013.02.24 17:33:20 2013.02.15 08:15  GodGift - I EURUSDpro,M15: K 2 - 1.5 - 1 - 0.5 - 0 - 0 - 0 - 0
2013.02.24 17:33:20 2013.02.15 08:15  GodGift - I EURUSDpro,M15: Alert: EURUSDpro 2 Sell Order Modify Success
2013.02.24 17:33:20 2013.02.15 08:15  GodGift - I EURUSDpro,M15: modify #2 sell 2.08 EURUSDpro at 1.33401 sl: 1.34429 tp: 1.33201 ok
2013.02.24 17:33:20 2013.02.15 08:15  GodGift - I EURUSDpro,M15: open #2 sell 2.08 EURUSDpro at 1.33401 ok
 
ftwfx:

  thank for the reply,  use the return value from Signal by this way

<CODE REMOVED>

Please edit your post . . . 

 

Use SRC 

 

finish editing, really sorry for that, i thought it is using the code mode, now i got it.

 
ftwfx: 3Red bar + 1 Green bar, a new sell order is placed.
If it's not 4 Green bars and not 4 red bars, what do you return from Signal?
 

because i just want the Signal to return 1 or 2 for buy or sell, so i didn't add the code to return other value. Just now i tried to add some code like this

if(sig1==2.0 && sig3==1.5 && sig5==1.0 && sig7==0.5) 
   {
   return(1); 
   }
   if(sig2==2.0 && sig4==1.5 && sig6==1.0 && sig8==0.5)
   { 
   return(2); 
   }
   else return(3);
   }

but the same result, sometimes, 3 Green + 1 Red it still do the buy order,  3 Red + 1 Green still do the sell order

I think maybe the color changes with the price change during the new candle, so could i add a limit to this, only get the value once when the second that a new candle begins?

 
Am interested in this EA. The 4bar NLMA tends to have arrows..can those be use to guide the EA to place trades?