Different values returned compared to displayed

 

I am currently using the iCustom function to call 2 indicators that I have created. It seems that sometime it return the same values as those that are displayed and then sometimes its not? I am passing in the same values as the default ones being used on the display. The values are not from the active bar Time[0] but are from Time[1]. So I would have thought that the values returned from iCustom would match the values on the screen?

The purple dots are the values "Sniper Simon" which can be seen that the comment has 1.6178, and the data windows shows 1.6178 Yay! How come iCustom would give me the 1.6188 value for "Sniper Simon V2", when the data window shows no value. As I said sometimes the values match correctly, and most times they do not.

In one sense, the value of 1.6188 is a correct setting for the indicator as it sets its value to either the high or low of the day. 1.6188 is the high of that day. So it sort of seems valid, and the values I am getting back, whilst they are not what is being displayed they are values of either the high or low. Is there something else I need to read with regard to using iCustom or be aware of?

 
midworld08:
How come iCustom would give me the 1.6188 value for "Sniper Simon V2", when the data window shows no value. As I said sometimes the values match correctly, and most times they do not.
No mind readers here. Show the indicator code and the iCustom code.
 

OK no problem, thought it might be something that happens with iCustom. Here is the code for SniperSimonV2 and below that is the iCustom code used to call the indicator;

It basically looks for an alignment of the MACD of the current TF with a higher TF that the user can select. If it sees an alignment it sets the value to a high or low depending on long or short.

extern bool UseTF1=false,     //allow user to set if the higher time zones are to be used.
            UseTF2=true,
            UseTF3=false; 
extern int  MACDFastMAPeriod=12,
            MACDSlowMAPeriod=26,
            MACDSignalMAPeriod=9;

color       SignalColor=Gold;

int         HigherTF1 = 60,
            HigherTF2 = 240,
            HigherTF3 = 1440;            

double      ActivatorBuffer[];
int         ExtCountedBars=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
 
 //set up the activator symbol to show the sniper active Symbol
   SetIndexStyle(0,DRAW_ARROW,EMPTY,EMPTY,SignalColor);
   SetIndexArrow(0,181);
   SetIndexBuffer(0,ActivatorBuffer);
   SetIndexEmptyValue(0,0.0);

   return(0);
  }int 
start()
  {
   //time to figure out which bars of the currently shown graph have the MACD travelling in the same direction
   double MACD_Histo[4];
   int   i=0,ChartPos[4],Base_Chart_Period;
   datetime Bar_Time;
   if (Period()>30) return(0);
   ChartPos[0]=Bars-2;
   if(ExtCountedBars>2) ChartPos[0]=Bars-ExtCountedBars-1;
   if (ChartPos[0]>WindowBarsPerChart()) ChartPos[0]=WindowBarsPerChart();
//make sure that not more than 1 time zone is selected
   if ((UseTF1 && UseTF2 && UseTF3) || (UseTF1 && UseTF2 && !UseTF3)||(UseTF1 && !UseTF2 &&UseTF3)||(!UseTF1 && UseTF2 &&UseTF3))
   {
      UseTF1=false;
      UseTF2=true;
      UseTF3=false;
   }
   while(ChartPos[0]>=0)
   {
      Bar_Time=Time[ChartPos[0]];  //fetch the current bar time
      //fetch MACD histogram values for previous 2 bars of lowest Time frame
      MACD_Histo[0] = iMACD(NULL,Period(),MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,PRICE_CLOSE,MODE_MAIN,(ChartPos[0]));
      MACD_Histo[1] = iMACD(NULL,Period(),MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,PRICE_CLOSE,MODE_MAIN,(ChartPos[0]+1));
      if (UseTF1)
      {     
         //locate next higher time frame that the current low time frame bar applies to in the higher time frame
         ChartPos[1]=iBarShift(NULL,HigherTF1,Bar_Time);
         //fetch MACD histogram values for previous 2 bars of higher Time frame
         MACD_Histo[2] = iMACD(NULL,HigherTF1,MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,PRICE_CLOSE,MODE_MAIN,(ChartPos[1]));
         MACD_Histo[3] = iMACD(NULL,HigherTF1,MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,PRICE_CLOSE,MODE_MAIN,(ChartPos[1]+1));
      }
      if (UseTF2)
      {     
         //locate next higher time frame that the current low time frame bar applies to in the higher time frame
         ChartPos[1]=iBarShift(NULL,HigherTF2,Bar_Time);
         //fetch MACD histogram values for previous 2 bars of higher Time frame
         MACD_Histo[2] = iMACD(NULL,HigherTF2,MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,PRICE_CLOSE,MODE_MAIN,(ChartPos[1]));
         MACD_Histo[3] = iMACD(NULL,HigherTF2,MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,PRICE_CLOSE,MODE_MAIN,(ChartPos[1]+1));
      }
      if (UseTF3)
      {     
         //locate next higher time frame that the current low time frame bar applies to in the higher time frame
         ChartPos[1]=iBarShift(NULL,HigherTF3,Bar_Time);
         //fetch MACD histogram values for previous 2 bars of higher Time frame
         MACD_Histo[2] = iMACD(NULL,HigherTF3,MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,PRICE_CLOSE,MODE_MAIN,(ChartPos[1]));
         MACD_Histo[3] = iMACD(NULL,HigherTF3,MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,PRICE_CLOSE,MODE_MAIN,(ChartPos[1]+1));
      }
      //zero the buffer
      ActivatorBuffer[ChartPos[0]]=0.0;    //set buffer to zero for default value
      //check to see if we have an MACD alignment
      if ((MACD_Histo[0]-MACD_Histo[1])>0 && (MACD_Histo[2]-MACD_Histo[3])>0)
         ActivatorBuffer[ChartPos[0]]=Low[ChartPos[0]];
      if ((MACD_Histo[0]-MACD_Histo[1])<0 && (MACD_Histo[2]-MACD_Histo[3])<0)
         ActivatorBuffer[ChartPos[0]]=High[ChartPos[0]];
           ChartPos[0]--;
   }
  return(0);
  }
double Sniper1Valid = iCustom(NULL,0,"Sniper_Simon",
            UseTF1,UseTF2,UseTF3,MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,0,1);
double Sniper2Valid = iCustom(NULL,0,"Sniper_Simon V2",
            UseTF1,UseTF2,UseTF3,MACDFastMAPeriod,MACDSlowMAPeriod,MACDSignalMAPeriod,0,1);
 
  1. if UseTF1-3 are all false, MACD_Histo[2] and [3] are never defined so buffer[] is always zero.
  2. if ((UseTF1 && UseTF2 && UseTF3) || (UseTF1 && UseTF2 && !UseTF3)||(Us...
    You're trying to prevent having more than one set, so this test can be simplified. Also this check can be in init()
    if ((UseTF1 && UseTF2) 
      ||(UseTF1 && UseTF3)
      ||(UseTF2 && UseTF3)
       ){

  3. int         HigherTF1 = 60,
                HigherTF2 = 240,
                HigherTF3 = 1440;            
    ..
    if (Period()>30) return(0);
    Why hard code, you could have just passed an exten int useTF=60 instead of three bools. Or use the bools to select relative to the chart:
    int  higher.TF;
    int     init(){
        int     TFperiod[]  = { PERIOD_M1,  PERIOD_M5,  PERIOD_M15, PERIOD_M30,
                                PERIOD_H1,  PERIOD_H4,  PERIOD_D1,  PERIOD_W1,
                                PERIOD_MN1  };                      static
        for(int index=0; TFperiod[index] < Period(); index++){}
        if (UseTF1) higher.TF = TFperiod[index+1];
        if (UseTF2) higher.TF = TFperiod[index+2];
        if (UseTF3) higher.TF = TFperiod[index+3];
    }
    ===
    MACD_Histo[2] = iMACD(NULL,Higher.TF, ...
    And then instead of three bool, you can pass one int useTF
    higher.TF = TFperiod[index+useTF];

  4. In this case there is only one indicator buffer, but I always create #define's for all buffers to document the icustom calls
    #define ActivatorBuffer 0
    //efine nextBuffer      1 ...
    ... iCustom( ..., ActivatorBuffer, 1);

  5. //int       HigherTF1 = 60,
    int         HigherTF1 = PERIOD_M60,
    Again use defines for periods.
  6. if (ChartPos[0]>WindowBarsPerChart()) ChartPos[0]=WindowBarsPerChart();
    This is wrong. Bars on the chart are WindowBarsPerChart .. zero ONLY if there are more bars than that (i.e not in the tester with only 100 initial bars) AND chart is not left shifted.
        shiftChart      =WindowFirstVisibleBar();
        shiftChartEnd   =shiftChart -WindowBarsPerChart();
        if (shiftChartEnd<0)    shiftChartEnd = 0;  // Tester/shift
    

  7. Why are you concerned with #5 at all. Just generate the indicator
    for (int shift = Bars - 1 - IndicatorCounted(); shift >= 0; shift--){ ...

 

Thanks again WHRoeder.

The indicator used to allow multiple alignment on the TF's. But the guys using it always got it confused (setting TF3 to less then TF2 and so forth), so it was removed. Hence the check to always align on just one, and remove the possibility that they can change the TF values. So the higher TF used to be exten.

I like your code simplification in #2.

Again for #3, the guys using it had didn't like the fact that a day in MT4 was 1440, and were setting it to 1400 or some such thing, never mind figuring out what a week and month was. So as much as passing in one exten Higher.TF=PERIOD_W1, if they got it wrong the code produced all sorts of wonders. Passing in 3 bools and hard coding the values so only I could change them easily left me where this was. OK so its not that desirable, but its still a work in progress :) However I appreciate the thought on this, as it is clunky as is.

Tips noted for #4 and #5 Cheers.

The code in #6 was actually borrowed from iMACD which I downloaded moons ago. I never fully grasped the way MT4 works on setting this up, but this seemed to work so I ran with it, and it got me producing stuff. So I never looked back. I think I see what you are getting at, and the use of "shift" is a better declaration to understand what is going on.

Much appreciated with all that, however it still produces different results with the iCustom call and what is seen in the data window. I have sort of solved it, I just copied and pasted the code into the EA and set a bool to valid trade long/short, instead of setting an indicator value, and it gives the correct signal for trade entry. So the problem lies in the iCustom call???

The reason I wanted to solve this, was that I could make a generic EA that would be used with any indicator that was written, and I just had to change the iCustom call. Anyway many thanks for your time and input here.

WHRoeder:
  1. if UseTF1-3 are all false, MACD_Histo[2] and [3] are never defined so buffer[] is always zero.
  2. You're trying to prevent having more than one set, so this test can be simplified. Also this check can be in init()
  3. Why hard code, you could have just passed an exten int useTF=60 instead of three bools. Or use the bools to select relative to the chart:
    And then instead of three bool, you can pass one int useTF

  4. In this case there is only one indicator buffer, but I always create #define's for all buffers to document the icustom calls
  5. Again use defines for periods.
  6. This is wrong. Bars on the chart are WindowBarsPerChart .. zero ONLY if there are more bars than that (i.e not in the tester with only 100 initial bars) AND chart is not left shifted.

  7. Why are you concerned with #5 at all. Just generate the indicator
Reason: