Customer indicator works correct, but in EA has the indicator buffer incorrect content

 

Hi,

I have written a relatively complicated indicator (1.step haiken ashi calculation, 2.step supertrend). The indicator works correct.

Part of indicator listing with changeTrend[] buffer:

      changeTrend[i] = 1.40;                 // changeTrend-Buffer for EA application (NOP no opertation)  
      //-- Draw the indicator
      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            changeOfTrend = 0;
            changeTrend[i] = 1.38;            // changeTrend-Buffer for EA application(LONG) 
            Print(TimeToStr(Time[i])+": "+changeTrend[i]+" "+i+" long "+Bars);
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            changeOfTrend = 0; 
            changeTrend[i] = 1.42;            // changeTrend-Buffer for EA application (SHORT)
            Print(TimeToStr(Time[i])+" "+changeTrend[i]+" "+i+" short "+Bars);
         }
      }

print: buffer7 (chnageTrend[]) from indicator (see 10.11.,22.9,20.8., ...)

2010.11.21 14:00:37     Heiken Ashi and TK1 EURUSD,Daily: 2010.11.10 00:00 1.42000000 8 short 2115
2010.11.21 14:00:37     Heiken Ashi and TK1 EURUSD,Daily: 2010.09.22 00:00: 1.38000000 50 long 2115
2010.11.21 14:00:37     Heiken Ashi and TK1 EURUSD,Daily: 2010.08.20 00:00 1.42000000 78 short 2115
2010.11.21 14:00:37     Heiken Ashi and TK1 EURUSD,Daily: 2010.06.20 00:00: 1.38000000 131 long 2115



chart with indicator (where: white line - buffer7)





I use the content of buffer7 in EA (indicator call by iCustom...) as trade trigger. Unfortunately, the content of buffer7 is wrong in the EA(see chart and i.e. alert for 10.11.10)


part of EA with buffer7[1] (changeTrend[1] in indicator - see above)

// new Bar?
if (c_altBars < Bars) {
   c_altBars = Bars;
    
// only for test purposes
double sT_5   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,5);
double sT_4   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,4);
double sT_3   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,4);
double sT_2   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,2);
double sT_0   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,0);

// changeTrend[1] in indicator; buffer7[1] is trade trigger in EA
double sT_1   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,1);

Alert(Bars+" "+sT_5+" "+sT_4+" "+sT_3+" "+sT_2+" "+sT_1+" "+sT_0+" "+TimeToStr(Time[0]));

if (sT_1 == 1.38){
   isCrossed = 1;
   Print ("Crossed = 1 "+" "+TimeToStr(Time[0]));
}   
else if (sT_1 == 1.42){  
   isCrossed = 2; 
   Print ("Crossed = 2"+" "+TimeToStr(Time[0]));
   }
   else {
      isCrossed = 0;
   }

alert: buffer7 from EA

2010.11.21 16:20:25     2010.11.11 00:00  ADI_EA_14112010_1 EURUSD,Daily: Alert: 1089 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 2010.11.11 00:00
2010.11.21 16:20:25     2010.11.10 00:00  ADI_EA_14112010_1 EURUSD,Daily: Alert: 1088 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 2010.11.10 00:00
2010.11.21 16:20:25     2010.11.09 00:00  ADI_EA_14112010_1 EURUSD,Daily: Alert: 1087 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 2010.11.09 00:00



For full source see Attachment indicator: "Heiken Ashi and TK1.mql4" (name in attachment is wrong), EA: "ADI_EA_14112010_1.mql4".

Where is a bug?

Thank you for help!!!

Yours sincerely

 

I am a newbie to EA code but I think you should keep indicator code separate and independent of EA code.

The icustom(......) function calls the indicator code that is external to the EA

You should be getting errors on having TWO start() functions in your code.

 
Of course are the EA and the indicator in separates files. I join the files for simplicity in attachment.
 

Sorry (New to EA code mistake) Just realised that when I tried to compile.

When I tried to split the files and comple I got end of bracket (block) problems to sort out so simplification got complicated for me!

I will leave it to the more experienced helpers.

 
mql7:
Of course are the EA and the indicator in separates files. I jion the files for simplicity in attachment.

Embedding full indicator inside an EA is actually achieving the opposite. For one, the function for optimizing re-calculation 'IndicatorCounted()' doesn't work in EA. You'd have to emulate it. See this article

And seeing your code I don't think you wanted/needed to do that. A once-off call to iCustom to know value of current or previous closed bar is usually enough. See other EAs in codebase to get some ideas.

Good luck. 

double sT_5   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,5); // this is sufficient without the full indicators
double sT_4   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,4); // copy-pasted above.  
 

Clarification!!!!

I have implemented full indicator in "Heiken Ashi and TK1.mq4" (see second part of attachment) and in the EA "ADI_EA_14112010_1.mq4" (see first part of attachment) calls the indicator by iCustom(....).

I have not embedded indicator inside the EA (see right attachment).


Thank you for help!!!

 
Top of your file says "Heiken Ashi and TK.mq4" which doesn't match your iCustom(.... TK1, ....
 

Only comment was wrong, contents of indicators source code correct. I have corrected the comment. See above for attachment.

This is not the source of error!

 
mql7:

I updated and improved the description.

Thank you for help!!!

I don't know about other people, but I don't see any of the images - they are all broken. I recommend u paste your code using the SRC button like everybody else does. That way people can use the code and it would also be searchable for future reference. This is not just a help forum, it's also a body of knowledge... It's worthless if it's not searchable.
 
I've taken a skim read at your code. Too many return(0). return are used to immediately abort the function execution. So, nothing below that return is executed. 
if (c_altBars < Bars) {
   c_altBars = Bars;
    

This lines are probably meant for optimizing. But it's nowhere near it. Try using Time[0] to detect New Bar opening. Search for it.  

I recommend reading the book & documentation. You would save yourself much trouble in the future, if you have solid understanding of program logic...

 
This is very general, uncorrect and not helpful. Even though to many thank!
Reason: