Maybe due to lack of a long enough lookback period thats been the cause of problem trades for me before. Not only that but your code compares EMA(1) which is the value for yesterday's EMA and won't be the exact value as shown on your plot line I don't believe. Check your printf values as Jcl says to be sure though.