Price crossing moving average

 

Hello all,

I've been making a program so I can see what percent of the time a moving average cross (with price) gets faded and what percent of the time it is a breakout. This is purely based on the close of the next candle. Right now it appears that it is claiming every candle close is a breakout or fade, ignoring my condition of crossing the moving average.

Any help would be greatly appreciated!


#include <Trade\Trade.mqh>

#include <Trade\AccountInfo.mqh>

#include <Object.mqh>

#include <Trade\PositionInfo.mqh>

#include <Trade\SymbolInfo.mqh>


CTrade                  m_trade;      

CPositionInfo           m_position;  

CAccountInfo currentSymbol;

CSymbolInfo currentInfo;

int breakOutCount = 0;

int fadeCount = 0;

double pf = 0; //  pips for the position from entry (previous close)

double pa = 0; // pips against position from entry (previous close)


 

 int MA1_Period=200;

 int MA2_Period=200;

 int MA3_Period = 200;

 int MA4_Period = 200;

 int EA_Magic=12345;


int MA1Handle;

int MA2Handle;

int MA3Handle;

int MA4Handle;


double MA1Val[];

double MA2Val[];

double MA3Val[];

double MA4Val[];




bool isNewBar()

  {

//--- memorize the time of opening of the last bar in the static variable

   static datetime last_time=0;

//--- current time

   datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);


//--- if it is the first call of the function

   if(last_time==0)

     {

      //--- set the time and exit

      last_time=lastbar_time;

      return(false);

     }


//--- if the time differs

   if(last_time!=lastbar_time)

     {

      //--- memorize the time and return true

      last_time=lastbar_time;

      return(true);

     }

//--- if we passed to this line, then the bar is not new; return false

   return(false);

  }














  





int OnInit()

{







//---

   //adxHandle=iADX(NULL,0,ADX_Period);

   MA1Handle=iMA(_Symbol,_Period,MA1_Period,0,MODE_EMA,PRICE_CLOSE);

   MA2Handle=iMA(_Symbol,_Period,MA2_Period,0,MODE_EMA,PRICE_CLOSE);

   MA3Handle=iMA(_Symbol,_Period,MA3_Period,0,MODE_EMA,PRICE_CLOSE); 

   MA4Handle=iMA(_Symbol,_Period,MA4_Period,0,MODE_EMA,PRICE_CLOSE);

  

   

  

   if( MA1Handle<0 || MA2Handle<0 || MA3Handle<0 || MA4Handle<0)

   {

   Alert("Error creating handles for PSar and MA's - error: ",GetLastError(),"!!"); 

   }

   

   /*STP=StopLoss;

   TKP=TakeProfit;

   if(_Digits==5 || _Digits==3)

   {

   STP=STP*10;

   TKP=TKP*10;

   }*/

//---

   return(INIT_SUCCEEDED);

   }

  

  

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   //IndicatorRelease(adxHandle);

   IndicatorRelease(MA1Handle);

   IndicatorRelease(MA2Handle);

   IndicatorRelease(MA3Handle);

   IndicatorRelease(MA4Handle);

  

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

 

//+------------------------------------------------------------------+

static datetime Old_Time;

datetime New_Time[2];

bool IsNewBar=false;

int copied=CopyTime(_Symbol,_Period,0,1,New_Time);

if(copied>0)

{

if(Old_Time!=New_Time[0])

{

IsNewBar=true;

if(MQL5InfoInteger(MQL5_DEBUGGING))

{

Print("We have new bar here ",New_Time[0],"old time was",Old_Time);

Old_Time=New_Time[0];

}

}

}

else

{

Alert("Error in copying historical times data, error=",GetLastError());

ResetLastError();

return;

}


if(IsNewBar==false)

{

return;

}

int MyBars=Bars(_Symbol,_Period);

if(MyBars<60)

{

Alert("We have fewer than 60 bars, EA will now exit!!");

return;

}

MqlTick latest_price;

MqlTradeRequest mrequest;

MqlTradeResult mresult;

MqlRates mrate[];

MqlDateTime mtime;

   

    

 

ZeroMemory(mrequest);

ZeroMemory(mresult);


ArraySetAsSeries(mrate,true);

//ArraySetAsSeries(plsDI,true);

//ArraySetAsSeries(minDI,true);

//ArraySetAsSeries(adxVal,true);

ArraySetAsSeries(MA1Val,true);

ArraySetAsSeries(MA2Val,true);

ArraySetAsSeries(MA3Val,true);

ArraySetAsSeries(MA4Val,true);


if(!SymbolInfoTick(_Symbol,latest_price))

{

Alert("Error getting the latest price quote - error:",GetLastError(),"!!");

return;

}

 if(CopyRates(_Symbol,_Period,0,3,mrate)<0)

{

Alert("Error copying rates/history data - error:",GetLastError(),"!!");

return;

}

/*if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0 || CopyBuffer(adxHandle,2,0,3,minDI)<0)

{

Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");

return;

}*/

 if(CopyBuffer(MA1Handle,0,0,3,MA1Val)<0)

{

Alert("Error copying MA1 indicator buffer - error:",GetLastError(),"!!");

return;

} 

if(CopyBuffer(MA2Handle,0,0,3,MA2Val)<0)

{

return;

}




if(CopyBuffer(MA3Handle,0,0,3,MA3Val)<0)

{

return;

}

if(CopyBuffer(MA4Handle,0,0,3,MA4Val)<0)

{

return;

}


ArrayResize(mrate,10);

ArrayResize(MA1Val,10);


bool breakOut =( (mrate[3].close < MA1Val[3] && mrate[2].close > MA1Val[2] && mrate[1].close>mrate[2].close) || ( mrate[3].close > MA1Val[3] && mrate[2].close < MA1Val[2] && mrate[1].close<mrate[2].close));


bool fade = ( (mrate[3].close < MA1Val[3] && mrate[2].close > MA1Val[2] && mrate[1].close < mrate[2].close) || ( mrate[3].close > MA1Val[3] && mrate[2].close < MA1Val[2] && mrate[1].close > mrate[2].close));


bool bullish = (mrate[3].close < MA1Val[3] && mrate[2].close > MA1Val[2]);

bool bearish = mrate[3].close > MA1Val[3] && mrate[2].close < MA1Val[2];


if(isNewBar()){



if(breakOut){

breakOutCount++;

Print("Breakout at: ",mrate[2].close);

  

Print("breakOutCount: ",breakOutCount," pf: ",pf," pa: ",pa);

}

else if(fade && bullish){

fadeCount++;

pf+=MathAbs(mrate[1].close-mrate[2].close);

pa+=mrate[2].close-mrate[1].low;

  

Print("fadeCount: ",fadeCount," pf: ",pf," pa: ",pa);

}else if(fade && bearish){

fadeCount++;

pf+=MathAbs(mrate[1].close-mrate[2].close);

pa+=mrate[1].high-mrate[2].close;

  

Print("fadeCount: ",fadeCount," pf: ",pf," pa: ",pa);


}


}






  

}

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button


 
Please remove empty lines and indent your code properly.