Need assistance: entering trades near EMA

 

Hi all,

Trying to catch a price to execute a trade when I'm not far from the EMA using GoodPrice function below

but with no success. what am I'm doing wrong here?

Thank you!!! 

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

extern double FarFromAverage = 1;

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

int OnInit()

  {

//---

  double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);

         if (ticksize == 0.00001 || ticksize == 0.001)

         pips = ticksize*10;

         else pips = ticksize;

  

//---

  } 

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

bool GoodPrice (){

double Average = iMA (Symbol(), 0, FastMA, 0, MODE_EMA, 3, 0);

if (Ask - Average<FarFromAverage*pips)

   return (true);

      else return (false);

      }       
 

Please use the SRC button when posing code, I've done it for you this time.

if (Ask - Average<FarFromAverage*pips)

 if Ask is below the EMA then Ask-Average will be a negative number, so it will be true whatever the distance

if (MathAbs(Ask - Average)<FarFromAverage*pips)

 Using MathAbs should do as you want

 
GumRai:

Please use the SRC button when posing code, I've done it for you this time.

 if Ask is below the EMA then Ask-Average will be a negative number, so it will be true whatever the distance

 Using MathAbs should do as you want

Thank you very much GumRai.

The Issue I'm expiriencing is that I'm opening a trades with prices that are far from the EMA (more then:  FarFromAverage*pips).

I tried to use MathAbs and backtest it but still some trades opened witn 10p from the EMA instead of 1p that I set.

thanks. 

 
There are no mind readers here. We can not see your code.
 
WHRoeder:
There are no mind readers here. We can not see your code.

I managed to figure this- the code is fine.

 thanks! 

Reason: