Problems with EMA

Posted By: Lennardd

Problems with EMA - 03/11/21 15:22

Hi guys,

I only recently started to use Zorro for backtesting my trading idea's but I've fallen in love with it immediately blush. However, I'm having trouble with using EMA, while other indicators don't cause any problems. However, if I use the following code snippet, it doesn't matter whether I use 'EMA50 > EMA200' or 'EMA50 < EMA200', backtest result is the same. Yet, if I look at the chart there are trades both when EMA50 is higher and when it's lower dan EMA200.
Code
	vars Price = series(priceClose());
	vars EMA50 = series(EMA(Price, 50));
	vars EMA200 = series(EMA(Price, 200));
	vars Macd = series(MACD(Price, 12, 26, 9));
	var *MacdHist = series(rMACDHist);
	
	if (!TradeIsOpen && EMA50 > EMA200 && crossOver(MacdHist, 0)){
		enterLong();
	}
	
	else if (TradeIsOpen && crossOver(MacdHist, 0)){
		exitLong();
	}


So I'm certainly making a mistake with using EMA indicator (Zorro doesn't give any error messages). Does anybody know what I'm doing wrong? Thanks in advance!
Posted By: ZorroTradeAA

Re: Problems with EMA - 03/11/21 18:39

Hi,

I see here two potential issues:
1. Can you tell us to which value you set LookBack? For a 200-EMA you need a high number in LookBack (I would say more than 200 as this is only valid for SMA and not EMA).
2. The variable TradeIsOpen has only valid entries when using in a tmf or in a loop like for(open_trades). If you use it outside of those two this is incorrect.

To tell more I need to see the rest of you code. But maybe you can search in the manual for tmf and you will find the answer by your own.

Cheers
Posted By: Grat

Re: Problems with EMA - 03/12/21 07:58

try this:
Code
	if (!TradeIsOpen && EMA50[0] > EMA200[0] && crossOver(MacdHist, 0)){
		enterLong();
	}
Posted By: Lennardd

Re: Problems with EMA - 03/12/21 10:21

Hi Grat,

Thanks a lot! This worked for me! smile

Regards,
Lennardd
Posted By: Lennardd

Re: Problems with EMA - 03/12/21 10:25

Hi ZorroTradeAA,

Thanks for your reply. My LookBack period was at 250 and I used it indeed in a loop. The problem was the absense of a index in my array as Grat indicated.
© 2024 lite-C Forums