Need help with EA: how to implement trend filtering via triple EMA of daily level for EA trading in H1?

 

Hello, I've written an EA which trades Pivot Points, you'll find their calculation inside the EA. It may look more complicated than needed, but I wanted to use a certain hourly Close for indicator calculation. This works fine.

What troubles me is the implementation of trend filtering. I'd like to use a simple filter from daily timeframe, i.e. comparison between EMA 9,12,26: buy would be allowed if 9>12>26, sell if 9<12<26, no trades allowed if other combination.

The way the EA is written, it seems that the condition is checked only once and not for every bar as intended. I'm clueless, why this is so. Either I get only buys or only sells, which means that some data is provided by the if-conditions in line 164ff, but it doesn't actualize as it should.

 

Where is the mistake? Can anybody provide an example of a solution? Please help!

 

Regards,

PomeGranate

//+------------------------------------------------------------------+
//|                                           PivotPoints_for_EA.mq4 |
//|                                             2015 (C) PomeGranate |
//+------------------------------------------------------------------+
#define MAGIC 20150518

//---- INPUT INDICATOR PARAMETERS
int closetime_UTC=20;

//---- DECLARING VARIABLES FOR CHOOSING A CHART
int timeframe=PERIOD_H1,
IndCounted=0;
string Symb=Symbol();                        // Symbol
extern double Lots=0.05;
//--- ARRAYS for PP
double PPBuffer[],R1Buffer[],R2Buffer[],R3Buffer[],R4Buffer[],
S1Buffer[],S2Buffer[],S3Buffer[],S4Buffer[];
int TrendArray[];
// GETTING THE AMOUNT OF ALL BARS OF THE CHART
int IBARS=iBars(Symb,timeframe);
double orderlevel[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//Array ops:
   if(ArraySize(orderlevel)<9)
      ArrayResize(orderlevel,9);

   if(ArraySize(PPBuffer)<IBARS)
     {
      ArraySetAsSeries(PPBuffer,false);
      ArraySetAsSeries(R1Buffer,false);
      ArraySetAsSeries(R2Buffer,false);
      ArraySetAsSeries(R3Buffer,false);
      ArraySetAsSeries(R4Buffer,false);
      ArraySetAsSeries(S1Buffer,false);
      ArraySetAsSeries(S2Buffer,false);
      ArraySetAsSeries(S3Buffer,false);
      ArraySetAsSeries(S4Buffer,false);
      ArraySetAsSeries(TrendArray,false);

      //      ArraySetAsSeries(Trend,false);
      //----  
      ArrayResize(PPBuffer,IBARS);
      ArrayResize(R1Buffer,IBARS);
      ArrayResize(R2Buffer,IBARS);
      ArrayResize(R3Buffer,IBARS);
      ArrayResize(R4Buffer,IBARS);
      ArrayResize(S1Buffer,IBARS);
      ArrayResize(S2Buffer,IBARS);
      ArrayResize(S3Buffer,IBARS);
      ArrayResize(S4Buffer,IBARS);
      ArrayResize(TrendArray,IBARS);

      //      ArrayResize(Trend,IBARS);
      //----  
      ArraySetAsSeries(PPBuffer,true);
      ArraySetAsSeries(R1Buffer,true);
      ArraySetAsSeries(R2Buffer,true);
      ArraySetAsSeries(R3Buffer,true);
      ArraySetAsSeries(R4Buffer,true);
      ArraySetAsSeries(S1Buffer,true);
      ArraySetAsSeries(S2Buffer,true);
      ArraySetAsSeries(S3Buffer,true);
      ArraySetAsSeries(S4Buffer,true);
      ArraySetAsSeries(TrendArray,true);

      //      ArraySetAsSeries(Trend,true);
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double pivotpoint,R1,R2,R3,R4,S1,S2,S3,S4;
//----+ Insertion of integer variables and GETTING ALREADY CALCULATED BARS
   int limit,MaxBar,bar,counted_bars=IndCounted;
//----+ REMEMBERING THE AMOUNT OF ALL BARS OF THE CHART
   IndCounted=IBARS-1;
//---- defining the number of the oldest bar,   
//     starting from which new bars will be recalculated
   limit=IBARS-counted_bars-1;
//---- defining the number of the oldest bar, 
//     starting from which new bars will be recalculated
   MaxBar=IBARS-1 -(10);

//----+ INDICATOR CALCULATION PivotPoints
   for(bar=limit; bar>=0; bar--)
     {
      int h=0,
      f=0,
      g=0;

      switch(TimeDayOfWeek(Time[bar]))
        {
         case 0:
            h=1;
            f=0;
            g=3+closetime_UTC;
            break;
         case 1:
            h=2;
            f=26;
            g=0;
            break;
         case 2:
            h=1;
            f=24;
            g=0;
            break;
         case 3:
            h=1;
            f=24;
            g=0;
            break;
         case 4:
            h=1;
            f=24;
            g=0;
            break;
         case 5:
            h=1;
            f=24;
            g=0;
            break;
         case 6:
            h=1;
            f=24;
            g=0;
            break;
        }
      int dshift=iBarShift(NULL,PERIOD_D1,Time[bar],false);
      datetime Today=iTime(NULL,PERIOD_D1,dshift); //determine time[i]'s 00:00 bar as anchor
      int hshift=iBarShift(NULL,PERIOD_H1,Today,false)+f; //go back f hourly bars

                                                          // High, low, close and open 
      double HIGH=iHigh(NULL,PERIOD_D1,dshift+h),
      LOW=iLow(NULL,PERIOD_D1,dshift+h),
      CLOSE=iClose(NULL,PERIOD_H1,hshift-closetime_UTC+g),//add the preset number of hours, thereby select 20:00 bar
      OPEN=iOpen(NULL,PERIOD_D1,dshift+h);

      // Pivot Point
      pivotpoint=NormalizeDouble((HIGH+LOW+CLOSE)/3,Digits);
      PPBuffer[bar]=pivotpoint;

      // Calcuations 
      R1 = NormalizeDouble(2 * pivotpoint - LOW,Digits);
      S1 = NormalizeDouble(2 * pivotpoint - HIGH,Digits);
      R2 = NormalizeDouble(pivotpoint + HIGH - LOW,Digits);
      S2 = NormalizeDouble(pivotpoint - HIGH + LOW,Digits);
      R3 = NormalizeDouble(2 * pivotpoint - 2*LOW + HIGH,Digits);
      S3 = NormalizeDouble(2 * pivotpoint - 2*HIGH + LOW,Digits);
      R4 = NormalizeDouble(R3 + pivotpoint - LOW,Digits);
      S4 = NormalizeDouble(S3 + pivotpoint - HIGH,Digits);
      R1Buffer[bar]=R1;R2Buffer[bar]=R2;R3Buffer[bar]=R3;R4Buffer[bar]=R4;
      S1Buffer[bar]=S1;S2Buffer[bar]=S2;S3Buffer[bar]=S3;S4Buffer[bar]=S4;
      //+------------------------------------------------------------------+
      //Trend=iCustom(NULL,PERIOD_D1,"Supertrend",10,4.0,4,1);
      //TrendArray[bar]=iCustom(NULL,PERIOD_D1,"Supertrend",10,4.0,4,1);   
      if(iMA(NULL,PERIOD_D1,9,0,1,0,dshift)>iMA(NULL,PERIOD_D1,12,0,1,0,dshift)>iMA(NULL,PERIOD_D1,26,0,1,0,dshift)) //(Trend>0)
         TrendArray[bar]=1;
      else if(iMA(NULL,PERIOD_D1,9,0,1,0,dshift)<iMA(NULL,PERIOD_D1,12,0,1,0,dshift)<iMA(NULL,PERIOD_D1,26,0,1,0,dshift)) //(Trend<0)
      TrendArray[bar]=-1;
      else TrendArray[bar]=0;
      //+------------------------------------------------------------------+
      //----------------------->TRADE CONDITIONS<--------------------------+
      //+------------------------------------------------------------------+

      //   if (iMA(NULL,PERIOD_D1,9,0,1,0,0)>iMA(NULL,PERIOD_D1,12,0,1,0,0)>iMA(NULL,PERIOD_D1,26,0,1,0,0))         //(Trend>0)
      if(TrendArray[1]>0)
        {
         //WORKING BUY CONDITION

        }
      //+------------------------------------------------------------------+
      //|                                                                  |
      //+------------------------------------------------------------------+
      if(TrendArray[1]<0)
         //   if (iMA(NULL,PERIOD_D1,9,0,1,0,0)<iMA(NULL,PERIOD_D1,12,0,1,0,0)<iMA(NULL,PERIOD_D1,26,0,1,0,0))         //(Trend<0)
        {
         //WORKING SELL CONDITION
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
Reason: