MA Crossing System with a Filter-MA

Hiho again,

my idea is like that: you have three EMA's 20, 50 and 200. I want to buy every 20 and 50 crossing below the 200, and want to short every 20 and 50 crossing above the 200. I tried different things all day: if, while, two buy conditions with AND etc. but no success. Here is my code:

SetOption("FuturesMode", True );
SetPositionSize( 1, spsShares );

         True );
         True );

FastMA = EMA(C, 20);
SlowMA = EMA(C, 50);
FilterMA = EMA(C, 200);

Buy =  Cross (FastMA, SlowMA);
Sell = DayOfWeek() == 5 AND TimeNum() == 200000;
Short = Cross (SlowMA, FastMA);
Cover = DayOfWeek() == 5 AND TimeNum() == 200000;

Help very appreciate!


where is FilterMA used in your code?

You create this variable and assign to it the EMA(C, 200) array but then you no longer use it.

I thought that could be a good approach, so I kept it in there first...

@seraphin, Your description says "Buy when 20 crosses below 200", yet the Cross code you have refers to the 20 and 50 MAs.

Should you have the FilterMA in your Cross conditions?

@snoopy.pa30 I think he means the following:

I want to buy every 20 and 50 crossing, when the close is below the 200, and want to short every 20 and 50 crossing, when the close is above the 200.

If you want to do what is described in above modified quote, you need to add some code to evaluate if the "Close" is above or below EMA(C, 200), otherwise you are always buying each time the Cross (FastMA, SlowMA) is True and shorting each time Cross (SlowMA, FastMA) is True.

If so, try to modify your code adding the condition to make use of the EMA(C, 200) array.

BuyCondition1 = ... // here the code comparing C to FilterMA
BuyCondition2 = Cross (FastMA, SlowMA);

Buy = BuyCondition1 AND BuyCondition2;
// etc....
1 Like

Sorry, may I was a bit unclear: I want to buy the longcrossings of ema20&50 beneath the ema200, and I want to short the shortcrossings of ema20&50 above the ema200.

The 'FilterMA' was such experimantial. I tried different things with it, but with no success.

Cond1 = FastMA < FilterMA;

That's something I tried before, but I was skeptic when I scanned the chart afterwards, because some signals seemed not catched by that algorythm. Propably if the slower MA's crossing each other still before they cross the EMA200 on the next bar...whatever...thank you for your efforts!

There is some confusion here regarding the meaning of Cross. Cross(fast, slow) will be true for one bar ONLY if fast < slow on previous bar and fast > slow on this bar. On following bar, Cross (fast, slow) is no longer true, because bar previous to next bar (ie this bar) fast !< slow. In otherwords the Cross is forgotten immediately after it happens. If you want to remember the Cross on later bars you would have to use something like the Flip() function.