Forum

Algorithmic trading, quantitative analysis and Zorro Trader.

Please ask all Zorro Trader questions in the Zorro Trader forum. You need to sign up first.

Please or Register to create posts and topics.

Question about MAs

Hi, I'm trying to do something above my skill level, so i have managed to code a simple cross over strategy where it takes trades on a MA crossover but i would like it to only take trades when the MAs both touch the same candle stick and the MAs are in the correct order i have been looking at learning to code with C but i haven't yet come across what I'm  looking for to help me code in zorro but it is early days. Does anyone know of any resources out there that could speed the process up that's more trading focused because i don't even know where to start with coding what I'm after.

Thanks

Bump. Algomike where are you?

Hi, and thanks for the interesting question! Let's assume we trade a two moving averages cross-over and cross-under system, and the moving averages are constructed by using the daily closing prices. At the most basic level, what we want to check here is if the "fast" moving average is "inside" the candle where the crossover occurs. Mathematically that means the fast moving average is greater than the opening price of the candle, but less than the closing price of the same candle (for a cross-over) and vice-versa for a cross-under. This also ensures that we take long trades only if the cross-over occurs on a green candle, and we take short trades only on red candles. The full code of the strategy is below:

function run()
{
BarPeriod = 1440;
LookBack = 5;

vars price_close = series(priceClose());

vars sma_fast = series(SMA(price_close, 2));
vars sma_slow = series(SMA(price_close, 5));

if(crossOver(sma_fast, sma_slow) && (priceOpen() < sma_fast[0]) && (priceClose() > sma_fast[0]))
enterLong();

if(crossUnder(sma_fast, sma_slow) && (priceClose() < sma_fast[0]) && (priceOpen() > sma_fast[0]))
enterShort();

plot("SMA Fast", sma_fast, LINE, ORANGE);
plot("SMA Slow", sma_slow, LINE, BLUE);
}

A few comments are necessary. We are adding to each if() clause two more conditions using the && ("and" operator). We use the priceOpen() and priceClose() internal Zorro Trader functions to get the open and close prices for the current candle, and the array index 0 of sma_fast[] to get the current value of the fast simple moving average.  We then compare the value of the fast sma to the values of the opening and closing prices of the current candle. Please note that these comparisons are different for crossOver and crossUnder, as we only want to take long trades on green candles (priceOpen() < priceClose()), and short trades on red candles (priceOpen() > priceClose()).

We did not check if the slow moving average is also "inside" the candle. Unless volatility is very high, or the formation periods extremely different, the values of the moving averages after a cross should be close.  However, if one wants to check that condition as well, it is as simple as adding the same comparisons for the slow moving average.

The definition of a "touch" we are using here is very loose. We simply check if  the moving average is "inside" the candle. If we wanted to check if the moving average is inside the "lower half" of the candle, we need to adjust the code accordingly. For example:

if(crossOver(sma_fast, sma_slow) && (priceOpen() < sma_fast[0]) && (priceClose()/2 > sma_fast[0]))

This would make sure that the moving average is in the lower half of the candle (for a green candle).

Also, please note that we chose very small values for the moving averages formation periods. That is because using greater values increases the "distance"  between the price and the moving averages, and we get no trades because the fast moving average is never "inside" the candles. Feel free to play with these values and see what happens. The script works as expected on the SPY, as you can see in the picture below.

I hope this helps! Please let me know if you have any further questions!

 

Uploaded files:
  • SimpleSMA_SPY.png

Thanks for the reply Algo Mike, I am currently working away from home but when I return I will test this with other MAs and different settings and different pairs. When will you be adding some new content to your website and blog, I have found the blog to be very interesting,

regards

 

Rob

bobdole has reacted to this post.
bobdole

Any luck with this? Please post anything interesting 🙂