Problem with cross over of EMAs

 

Hi all,


I am trying to create the following crossover check between EMA30 and EMA10 and get to long or short accordingly

but unfortunately i am getting bad results such as : entering to the if statements even if EMA10 < EMA30 (both are global doubles) using Build 225 with

Digits = 5 in the demo server.

int checkedCondition = 0 and bool goingUp gets its value according to graphs in init();


I even tried without NormalizeDouble (),

tried with changing a different variable than the checkedCondition in Switch Case clause statement in a fear that it might affect it but alas.


I executed it on the GBPUSD 30min between 1/9/2009 and 10/9/2009 and it opens 26(!) positions whereas it should be only 10 !!!

It can't open consecutives Shorts nor consecutives Long cause it toggles between the Short and Long.

Could you please assist me in understanding what is wrong with my code ?



int start() {

int err, ticket;
double myLow, myHigh;
double myAsk, myBid, myTP;

RefreshRates();
if (lowestLow > Low[0])
lowestLow = Low[0];
if (highestHigh < High[0])
highestHigh = High[0];
// get exponential MA 10, MA 30

EMA10 = NormalizeDouble(iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0),Digits-1);
EMA30 = NormalizeDouble(iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,0),Digits-1);

switch (checkedCondition){
case 0:
if ((!goingUp) && (EMA10 > EMA30)) {
checkedCondition = 1;
myAsk = NormalizeDouble(Ask,Digits);
//???
Print("case 0 BUY : EMA10=",DoubleToStr(EMA10,Digits-1)," EMA30=",DoubleToStr(EMA30,Digits-1)," MathAbs(EMA30 - EMA10)=",DoubleToStr(MathAbs(EMA30 - EMA10),Digits-1)," SL=",lowestLow," ASK=",myAsk); //???
ticket = OrderSend(Symbol(),OP_BUY,1.0,myAsk,3,NULL,NULL,"Erez DAS2009",magicNumber,0,Green);
//???

lowestLow = 999999; // start recounting the lowest one
highestHigh = 0; // start recounting the highest one
} else
if (goingUp && (EMA10 < EMA30)) {
checkedCondition = 2;
myBid = NormalizeDouble(Bid,Digits);
//???
Print("case 0 SELL : EMA10=",DoubleToStr(EMA10,Digits-1)," EMA30=",DoubleToStr(EMA30,Digits-1)," MathAbs(EMA30 - EMA10)=",DoubleToStr(MathAbs(EMA30 - EMA10),Digits-1)," SL=",highestHigh," BID=",myBid);
ticket = OrderSend(Symbol(),OP_SELL,1.0,myBid,3,NULL,NULL,"Erez DAS2009",magicNumber,0,Red);
//???

highestHigh = 0; // start recounting the highest one
lowestLow = 999999; // start recounting the lowest one
}
break;
case 1:

if (EMA10 < EMA30) {
checkedCondition = 2;
myBid = NormalizeDouble(Bid,Digits);
//???
Print("case 1 SELL : EMA10=",DoubleToStr(EMA10,Digits-1)," EMA30=",DoubleToStr(EMA30,Digits-1)," MathAbs(EMA30 - EMA10)=",DoubleToStr(MathAbs(EMA30 - EMA10),Digits-1)," SL=",lowestLow," BID=",myBid);
ticket = OrderSend(Symbol(),OP_SELL,1.0,myBid,3,NULL,NULL,"Erez DAS2009",magicNumber,0,Red);
//???

highestHigh = 0; // start recounting the highest one
lowestLow = 999999; // start recounting the lowest one
}
break;
case 2:

if (EMA10 > EMA30){
checkedCondition = 1;
myAsk = NormalizeDouble(Ask,Digits);
myTP = NormalizeDouble(myAsk * 1.15, Digits);
//???
Print("case 2 BUY : EMA10=",DoubleToStr(EMA10,Digits-1)," EMA30=",DoubleToStr(EMA30,Digits-1)," MathAbs(EMA30 - EMA10)=",DoubleToStr(MathAbs(EMA30 - EMA10),Digits-1)," SL=",lowestLow," ASK=",myAsk); //???
ticket = OrderSend(Symbol(),OP_BUY,1.0,myAsk,3,NULL,NULL,"Erez DAS2009",magicNumber,0,Green);
//???

highestHigh = 0; // start recounting the highest one
lowestLow = 999999; // start recounting the lowest one
}
}
break;
default:
Print("Default Clause"); // calculate new highestHigh, lowestLow
break;

}//Switch
Sleep(5);
return(0);
}

 

shpado

This has all got very complicated and obscures the (probably) basic problem

Some random observations:-

You should'nt need to use NormalizeDouble for this comparison, indeed this may be part of the problem as you are losing data by truncating the values

I wouldnt use Switch for this context

Is checkedCondition a static vaiable - if not it will lose its value between ticks

No need to do RefreshRates() first thing as start() initiates on a new tick (i.e. an implicit refresh of MarketInfo)

I would start again, testing each element as you add it :(

FWIW

-BB-

 
BarrowBoy:

shpado

This has all got very complicated and obscures the (probably) basic problem

Some random observations:-

You should'nt need to use NormalizeDouble for this comparison, indeed this may be part of the problem as you are losing data by truncating the values

I wouldnt use Switch for this context

Is checkedCondition a static vaiable - if not it will lose its value between ticks

No need to do RefreshRates() first thing as start() initiates on a new tick (i.e. an implicit refresh of MarketInfo)

I would start again, testing each element as you add it :(

FWIW

-BB-

Thanks BB,

If I put off the NormalizeDouble() it creates 120 positions, when i put it, it creates "only" 26 whereas it should open only 10 as mentioned above.

I put off the Switch clause and put the static on the "checkedCondition" - it didn't change a thing.

Fearing it relates to the feeds of the Strategy Tester - I checked it on various MT4 platforms - though different feeds but creates more than the 10 positions.


HELP anyone ?

 
Your curly brackets don't match up properly... to begin with.