Can't find error in logic

 
Hello friends, I'm writing an EA but I'm having trouble with the programming logic part. The EA should do the following: At each candle check if the fast moving average of 05 has crossed from low to high the slow ones of 75 and 85, if it has, check if MacD has changed from negative to positive for the current candle an the 2 subsequent ones, if so, buy, otherwise start from the beginning. The problem is that the program seems to buy only if both conditions happen in the same candle, which rarely happens. What can I do to solve it? Below is the code: 
#include<Trade\Trade.mqh>
CTrade trade;
static int SentinelaVenda =0;
static int SentinelaCompra =0;

void OnTick()
{
double myPriceArray[];
int MacDDefinition = iMACD(_Symbol,_Period,15,26,1,PRICE_CLOSE);
ArraySetAsSeries(myPriceArray,true);
CopyBuffer(MacDDefinition,0,0,3,myPriceArray);
double MacDValue=(myPriceArray[1]);
double MacDValueAnt=(myPriceArray[2]);
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
MqlRates PriceInfo[];
ArraySetAsSeries(PriceInfo,true);
double mymovingaveragearray1 [], mymovingaveragearray2[],mymovingaveragearray3[];
int movingaveragedefinition1 = iMA (_Symbol,_Period, 05,0,MODE_EMA,PRICE_CLOSE);
int movingaveragedefinition2 = iMA (_Symbol,_Period, 75,0,MODE_EMA,PRICE_CLOSE);
int movingaveragedefinition3 = iMA (_Symbol,_Period, 85,0,MODE_EMA,PRICE_CLOSE);
ArraySetAsSeries (mymovingaveragearray1, true);
ArraySetAsSeries (mymovingaveragearray2, true);
ArraySetAsSeries (mymovingaveragearray3, true);
CopyBuffer(movingaveragedefinition1,0,0,3,mymovingaveragearray1);
CopyBuffer(movingaveragedefinition2,0,0,3,mymovingaveragearray2);
CopyBuffer(movingaveragedefinition3,0,0,3,mymovingaveragearray3);

//COMPRA------------------------------------------------------
if(
SentinelaCompra==1
)
{SentinelaCompra=2;}

if(
SentinelaCompra==2
)
{SentinelaCompra=3;}

if(
SentinelaCompra==3
)
{SentinelaCompra=0;}
//PART 1
if(
SentinelaCompra==0
&&(mymovingaveragearray1[0]>mymovingaveragearray2[0]) 
&&(mymovingaveragearray1[0]>mymovingaveragearray3[0])
&&(mymovingaveragearray1[1]<mymovingaveragearray2[1])
&&(mymovingaveragearray1[1]<mymovingaveragearray3[1])
)
{SentinelaCompra=1;}

//PART 4
if(
(SentinelaCompra==3)
&& (MacDValue>0)
&& (MacDValueAnt<0)
&& (PositionsTotal()==0)
)
{
trade.Buy(1,NULL,Ask,Ask-300*_Point,Ask+300*_Point,NULL);
SentinelaCompra=0;
Comment ("Comprado em 3");
}


//PART 3
if(
SentinelaCompra==2
&& (MacDValue>0)
&& (MacDValueAnt<0)
&& (PositionsTotal()==0)
)
{
trade.Buy(1,NULL,Ask,Ask-300*_Point,Ask+300*_Point,NULL);
(SentinelaCompra=0);
Comment ("Comprado em 2");
}


//PART 2
if(
SentinelaCompra==1
&& (MacDValue>0)
&& (MacDValueAnt<0)
&& (PositionsTotal()==0)
)
{
trade.Buy(1,NULL,Ask,Ask-300*_Point,Ask+300*_Point,NULL);
(SentinelaCompra=0);
Comment ("Comprado em 1");
}
Thank You very much!
 

Why you complicate it in this way and you don't use a simple code like this?

void OnTick()
{
double MACD[];
ArraySetAsSeries(MACD,true);
int MacDDefinition = iMACD(_Symbol,_Period,15,26,1,PRICE_CLOSE);
CopyBuffer(MacDDefinition,0,0,3,MACD);

double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
MqlRates PriceInfo[];
ArraySetAsSeries(PriceInfo,true);

double MA1[], MA2[],MA3[];
ArraySetAsSeries (MA1, true);
ArraySetAsSeries (MA2, true);
ArraySetAsSeries (MA3, true);

int MA1_Handle = iMA (_Symbol,_Period, 05,0,MODE_EMA,PRICE_CLOSE);
int MA2_Handle = iMA (_Symbol,_Period, 75,0,MODE_EMA,PRICE_CLOSE);
int MA3_Handle = iMA (_Symbol,_Period, 85,0,MODE_EMA,PRICE_CLOSE);
CopyBuffer(MA1_Handle,0,0,3,MA1);
CopyBuffer(MA2_Handle,0,0,3,MA2);
CopyBuffer(MA3_Handle,0,0,3,MA3);

//COMPRA------------------------------------------------------
if( PositionsTotal()==0 && 
    MA1[0]>MA2[0] && MA1[0]>MA3[0] && MA1[1]<=MA2[1] && MA1[1]<=MA3[1] && 
    MACD[1]>0 && MACD[2]<=0 ) 
    {
        trade.Buy(1,NULL,Ask,Ask-300*_Point,Ask+300*_Point,NULL);
    }
}

Seriously I can't understand people that use 20+ chars of variable's names... 

Anyway the code, as is, is made for buying when there is a MA crossover on current candle and MACD changed from negative to positive on last 2 closed candles.

 
Really? You do the handle of indicator in ontick instead of oninit?
 
amando:
Really? You do the handle of indicator in ontick instead of oninit?
I didn't realized that...... He need to change also this part
Reason: